Skip to content

Commit

Permalink
Compression functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperjj committed Aug 16, 2016
1 parent e5d9714 commit eefca11
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ System.out.println("Hello "+obj.getString("title")+"!");
// Expected output: "Hello World!"
````

The library includes a JSON compression feature that uses template based encoding an http2 header cache inspired caching of repeated string values.

```java
String source="{\"title\":\"World\"}";
Compressor cmp=new Compressor("./compression_cache",100,1);
byte[] data=cmp.compress(source);
cmp.commit(); // Write out templates and dictionary data
String output=cmp.decompress(data);
````

## Performance

The following table shows a speed comparison for parsing around 551kb of JSON data.

JSON Library | Min | Max | Avg | Median
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/me/doubledutch/lazyjson/LazyElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public String toString(){
return new String(cbuf,root.startIndex,root.endIndex-root.startIndex);
}

/**
* Returns the character count of the source string.
*
* @return the length of the source string for this element
*/
public int getSourceLength(){
return root.endIndex-root.startIndex;
}

public byte[] toByteArray(){
int size=root.getBufferSize();
ByteBuffer buf=ByteBuffer.allocate(size);
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/me/doubledutch/lazyjson/compressor/Compressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ private boolean shouldCompress(Template t){
}

public byte[] compress(String str){
return compress(LazyElement.parse(str));
}


public byte[] compress(LazyElement elm){
// 1. Parse data
LazyElement elm=LazyElement.parse(str);
// LazyElement elm=LazyElement.parse(str);
// 2. Generate template
Template t=elm.extractTemplate();
// 3. If template satisfies criterea - compress
if(shouldCompress(t)){
try{
ByteBuffer buf=ByteBuffer.allocate(str.length()-2);
ByteBuffer buf=ByteBuffer.allocate(elm.getSourceLength()-2);
buf.putShort((short)templateSet.get(t));
elm.writeTemplateValues(buf,dictionary);
int pos=buf.position();
Expand All @@ -85,13 +90,25 @@ public byte[] compress(String str){
}
// 4. return encoded data
// TODO: this is incredibly inefficient... fix!
byte[] encoded=str.getBytes(StandardCharsets.UTF_8);
byte[] encoded=elm.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer buf=ByteBuffer.allocate(2+encoded.length);
buf.putShort((short)-1);
buf.put(encoded);
return buf.array();
}

public LazyElement decompressElement(byte[] data){
return LazyElement.parse(decompress(data));
}

public LazyObject decompressObject(byte[] data){
return new LazyObject(decompress(data));
}

public LazyArray decompressArray(byte[] data){
return new LazyArray(decompress(data));
}

public String decompress(byte[] data){
ByteBuffer buf=ByteBuffer.wrap(data);
// 1. If it's a raw value, simply decode
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Tue Aug 16 18:32:29 EDT 2016
#Tue Aug 16 18:54:28 EDT 2016
BUILD_VERSION=1.1.0
BUILD_DATE=2016-08-16T22\:32\:29Z
BUILD_NUMBER=556
BUILD_DATE=2016-08-16T22\:54\:28Z
BUILD_NUMBER=566

0 comments on commit eefca11

Please sign in to comment.