Skip to content

Commit

Permalink
Rewrite dirty buffer tracking
Browse files Browse the repository at this point in the history
Serialization of node graphs is temporarily broken :-(
  • Loading branch information
Kasper Jeppesen committed May 3, 2017
1 parent f92edb9 commit 1518d7f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 201 deletions.
92 changes: 47 additions & 45 deletions src/main/java/me/doubledutch/lazyjson/LazyArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ public LazyArray(String raw) throws LazyException{
throw new LazyException("JSON Array must start with [",0);
}
root=parser.root;
cbuf=parser.cbuf;
}

public LazyArray() throws LazyException{
LazyParser parser=new LazyParser("[]");
parser.tokenize();
root=parser.root;
cbuf=parser.cbuf;
}

protected LazyArray(LazyNode root,char[] source,StringBuilder dirtySource){
super(root,source,dirtySource);
protected LazyArray(LazyNode root){
super(root);
}
/*
protected LazyArray(LazyNode root,char[] source){
Expand All @@ -51,12 +49,12 @@ protected String serializeElementToString(){
buf.append(",");
}
if(pointer.type==LazyNode.OBJECT){
buf.append(new LazyObject(pointer,cbuf,dirtyBuf).toString());
buf.append(new LazyObject(pointer).toString());
}else if(pointer.type==LazyNode.ARRAY){
buf.append(new LazyArray(pointer,cbuf,dirtyBuf).toString());
buf.append(new LazyArray(pointer).toString());
}else if(pointer.type==LazyNode.VALUE_STRING || pointer.type==LazyNode.VALUE_ESTRING){
buf.append("\"");
buf.append(pointer.getStringValue(cbuf,dirtyBuf));
buf.append(pointer.getStringValue());
buf.append("\"");
}else if(pointer.type==LazyNode.VALUE_TRUE){
buf.append("true");
Expand All @@ -65,7 +63,7 @@ protected String serializeElementToString(){
}else if(pointer.type==LazyNode.VALUE_NULL){
buf.append("null");
}else{
buf.append(pointer.getStringValue(cbuf,dirtyBuf));
buf.append(pointer.getStringValue());
}
pointer=pointer.next;
}
Expand Down Expand Up @@ -139,19 +137,19 @@ public Object get(int index) throws LazyException{
LazyNode token=getValueToken(index);
if(token!=null){
switch(token.type){
case LazyNode.OBJECT: LazyObject obj=new LazyObject(token,cbuf,dirtyBuf);
case LazyNode.OBJECT: LazyObject obj=new LazyObject(token);
obj.parent=this;
return obj;
case LazyNode.ARRAY: LazyArray arr= new LazyArray(token,cbuf,dirtyBuf);
case LazyNode.ARRAY: LazyArray arr= new LazyArray(token);
arr.parent=this;
return arr;
case LazyNode.VALUE_TRUE: return (Boolean)true;
case LazyNode.VALUE_FALSE: return (Boolean)false;
case LazyNode.VALUE_NULL: return LazyObject.NULL;
case LazyNode.VALUE_STRING: return token.getStringValue(cbuf,dirtyBuf);
case LazyNode.VALUE_ESTRING: return token.getStringValue(cbuf,dirtyBuf);
case LazyNode.VALUE_INTEGER: return (Long)token.getLongValue(cbuf,dirtyBuf);
case LazyNode.VALUE_FLOAT: return (Double)token.getDoubleValue(cbuf,dirtyBuf);
case LazyNode.VALUE_STRING: return token.getStringValue();
case LazyNode.VALUE_ESTRING: return token.getStringValue();
case LazyNode.VALUE_INTEGER: return (Long)token.getLongValue();
case LazyNode.VALUE_FLOAT: return (Double)token.getDoubleValue();
}
}
// Should never happen
Expand All @@ -162,19 +160,19 @@ public Object opt(int index) throws LazyException{
LazyNode token=getOptionalValueToken(index);
if(token!=null){
switch(token.type){
case LazyNode.OBJECT: LazyObject obj=new LazyObject(token,cbuf,dirtyBuf);
case LazyNode.OBJECT: LazyObject obj=new LazyObject(token);
obj.parent=this;
return obj;
case LazyNode.ARRAY: LazyArray arr= new LazyArray(token,cbuf,dirtyBuf);
case LazyNode.ARRAY: LazyArray arr= new LazyArray(token);
arr.parent=this;
return arr;
case LazyNode.VALUE_TRUE: return (Boolean)true;
case LazyNode.VALUE_FALSE: return (Boolean)false;
case LazyNode.VALUE_NULL: return LazyObject.NULL;
case LazyNode.VALUE_STRING: return token.getStringValue(cbuf,dirtyBuf);
case LazyNode.VALUE_ESTRING: return token.getStringValue(cbuf,dirtyBuf);
case LazyNode.VALUE_INTEGER: return (Long)token.getLongValue(cbuf,dirtyBuf);
case LazyNode.VALUE_FLOAT: return (Double)token.getDoubleValue(cbuf,dirtyBuf);
case LazyNode.VALUE_STRING: return token.getStringValue();
case LazyNode.VALUE_ESTRING: return token.getStringValue();
case LazyNode.VALUE_INTEGER: return (Long)token.getLongValue();
case LazyNode.VALUE_FLOAT: return (Double)token.getDoubleValue();
}
}
return null;
Expand Down Expand Up @@ -262,7 +260,8 @@ public LazyArray put(boolean value) throws LazyException{
}

public LazyArray put(LazyArray value) throws LazyException{
if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
appendChild(value.root);
/*if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
value.root.dirty=true;
appendChild(value.root);
}else if(value.cbuf!=cbuf){
Expand All @@ -273,12 +272,13 @@ public LazyArray put(LazyArray value) throws LazyException{
appendChild(value.root);
value.dirtyBuf=buf;
// System.out.println("not matching put conditions");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");*/
return this;
}

public LazyArray put(LazyObject value) throws LazyException{
if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
appendChild(value.root);
/*if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
value.root.dirty=true;
appendChild(value.root);
}else if(value.cbuf!=cbuf){
Expand All @@ -289,7 +289,7 @@ public LazyArray put(LazyObject value) throws LazyException{
appendChild(value.root);
value.dirtyBuf=buf;
// System.out.println("not matching put conditions");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");*/
return this;
}

Expand Down Expand Up @@ -341,7 +341,8 @@ public LazyArray put(int index,boolean value) throws LazyException{
}

public LazyArray put(int index,LazyArray value) throws LazyException{
if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
insertChild(index,value.root);
/*if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
value.root.dirty=true;
insertChild(index,value.root);
}else if(value.cbuf!=cbuf){
Expand All @@ -351,12 +352,13 @@ public LazyArray put(int index,LazyArray value) throws LazyException{
value.root.dirty=true;
insertChild(index,value.root);
// System.out.println("not matching put conditions");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");*/
return this;
}

public LazyArray put(int index,LazyObject value) throws LazyException{
if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
insertChild(index,value.root);
/*if(value.cbuf==cbuf && value.dirtyBuf==dirtyBuf){
value.root.dirty=true;
insertChild(index,value.root);
}else if(value.cbuf!=cbuf){
Expand All @@ -366,7 +368,7 @@ public LazyArray put(int index,LazyObject value) throws LazyException{
value.root.dirty=true;
insertChild(index,value.root);
// System.out.println("not matching put conditions");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");
}// else throw new LazyException("Unknown data merge condition :-( :-( :-(");*/
return this;
}

Expand All @@ -377,7 +379,7 @@ public Object remove(int index) throws LazyException{
// System.out.println("found the token!");
LazyNode pointer=this.root.child;
if(pointer==token){
System.out.println("yes, it was the first");
// System.out.println("yes, it was the first");
root.child=token.next;
}else{
while(pointer!=null){
Expand All @@ -404,7 +406,7 @@ public Object remove(int index) throws LazyException{
public LazyArray getJSONArray(int index) throws LazyException{
LazyNode token=getValueToken(index);
if(token.type!=LazyNode.ARRAY)throw new LazyException("Requested value is not an array",token);
LazyArray arr= new LazyArray(token,cbuf,dirtyBuf);
LazyArray arr= new LazyArray(token);
arr.parent=this;
return arr;
}
Expand All @@ -421,7 +423,7 @@ public LazyArray optJSONArray(int index) throws LazyException{
if(token==null)return null;
if(token.type==LazyNode.VALUE_NULL)return null;
if(token.type!=LazyNode.ARRAY)return null;
LazyArray arr= new LazyArray(token,cbuf,dirtyBuf);
LazyArray arr= new LazyArray(token);
arr.parent=this;
return arr;
}
Expand All @@ -436,7 +438,7 @@ public LazyArray optJSONArray(int index) throws LazyException{
public LazyObject getJSONObject(int index) throws LazyException{
LazyNode token=getValueToken(index);
if(token.type!=LazyNode.OBJECT)throw new LazyException("Requested value is not an object",token);
LazyObject obj= new LazyObject(token,cbuf,dirtyBuf);
LazyObject obj= new LazyObject(token);
obj.parent=this;
return obj;
}
Expand All @@ -453,7 +455,7 @@ public LazyObject optJSONObject(int index) throws LazyException{
if(token==null)return null;
if(token.type==LazyNode.VALUE_NULL)return null;
if(token.type!=LazyNode.OBJECT)return null;
LazyObject obj= new LazyObject(token,cbuf,dirtyBuf);
LazyObject obj= new LazyObject(token);
obj.parent=this;
return obj;
}
Expand Down Expand Up @@ -514,7 +516,7 @@ public boolean optBoolean(int index,boolean defaultValue){
*/
public String getString(int index) throws LazyException{
LazyNode token=getValueToken(index);
return token.getStringValue(cbuf,dirtyBuf);
return token.getStringValue();
}

/**
Expand All @@ -528,7 +530,7 @@ public String optString(int index){
LazyNode token=getOptionalValueToken(index);
if(token==null)return null;
if(token.type==LazyNode.VALUE_NULL)return null;
return token.getStringValue(cbuf,dirtyBuf);
return token.getStringValue();
}

/**
Expand All @@ -543,7 +545,7 @@ public String optString(int index,String defaultValue){
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getStringValue(cbuf,dirtyBuf);
return token.getStringValue();
}

/**
Expand All @@ -555,7 +557,7 @@ public String optString(int index,String defaultValue){
*/
public int getInt(int index) throws LazyException{
LazyNode token=getValueToken(index);
return token.getIntValue(cbuf,dirtyBuf);
return token.getIntValue();
}

/**
Expand All @@ -569,7 +571,7 @@ public int optInt(int index){
LazyNode token=getOptionalValueToken(index);
if(token==null)return 0;
if(token.type==LazyNode.VALUE_NULL)return 0;
return token.getIntValue(cbuf,dirtyBuf);
return token.getIntValue();
}

/**
Expand All @@ -584,7 +586,7 @@ public int optInt(int index,int defaultValue){
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getIntValue(cbuf,dirtyBuf);
return token.getIntValue();
}

/**
Expand All @@ -596,7 +598,7 @@ public int optInt(int index,int defaultValue){
*/
public long getLong(int index) throws LazyException{
LazyNode token=getValueToken(index);
return token.getLongValue(cbuf,dirtyBuf);
return token.getLongValue();
}

/**
Expand All @@ -610,7 +612,7 @@ public long optLong(int index){
LazyNode token=getOptionalValueToken(index);
if(token==null)return 0l;
if(token.type==LazyNode.VALUE_NULL)return 0l;
return token.getLongValue(cbuf,dirtyBuf);
return token.getLongValue();
}

/**
Expand All @@ -625,7 +627,7 @@ public long optLong(int index,long defaultValue){
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getLongValue(cbuf,dirtyBuf);
return token.getLongValue();
}

/**
Expand All @@ -637,7 +639,7 @@ public long optLong(int index,long defaultValue){
*/
public double getDouble(int index) throws LazyException{
LazyNode token=getValueToken(index);
return token.getDoubleValue(cbuf,dirtyBuf);
return token.getDoubleValue();
}

/**
Expand All @@ -651,7 +653,7 @@ public double optDouble(int index){
LazyNode token=getOptionalValueToken(index);
if(token==null)return 0.0;
if(token.type==LazyNode.VALUE_NULL)return 0.0;
return token.getDoubleValue(cbuf,dirtyBuf);
return token.getDoubleValue();
}

/**
Expand All @@ -666,7 +668,7 @@ public double optDouble(int index,double defaultValue){
LazyNode token=getOptionalValueToken(index);
if(token==null)return defaultValue;
if(token.type==LazyNode.VALUE_NULL)return defaultValue;
return token.getDoubleValue(cbuf,dirtyBuf);
return token.getDoubleValue();
}

/**
Expand Down
31 changes: 6 additions & 25 deletions src/main/java/me/doubledutch/lazyjson/LazyElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,40 @@

public abstract class LazyElement{
protected LazyNode root;
protected char[] cbuf;
protected StringBuilder dirtyBuf=null;
protected LazyElement parent;

// Cache value for length
private int length=-1;

protected LazyElement(LazyNode root,char[] source,StringBuilder dirtySource){
protected LazyElement(LazyNode root){
this.root=root;
this.cbuf=source;
this.dirtyBuf=dirtySource;
}

protected LazyElement() throws LazyException{

}

protected LazyNode appendAndSetDirtyString(byte type,String value) throws LazyException{
dirtyBuf=getDirtyBuf();
StringBuilder dirtyBuf=root.getDirtyBuf();
LazyNode child=new LazyNode(type,dirtyBuf.length());
dirtyBuf.append(value);
child.endIndex=dirtyBuf.length();
child.dirty=true;
child.dirtyBuf=dirtyBuf;
return child;
}

protected StringBuilder getDirtyBuf(){
if(dirtyBuf!=null){
return dirtyBuf;
}
if(parent!=null){
dirtyBuf=parent.getDirtyBuf();
return dirtyBuf;
}
dirtyBuf=new StringBuilder();
return dirtyBuf;
}

protected char[] getCharBuffer(){
return cbuf;
}

public Template extractTemplate(){
Template t=new Template();
root.addSegments(cbuf,t);
root.addSegments(t);
t.compact();
return t;
}

public abstract LazyType getType();

public void writeTemplateValues(ByteBuffer buf,DictionaryCache dict) throws BufferOverflowException{
root.writeSegmentValues(cbuf,dirtyBuf,buf,dict);
root.writeSegmentValues(buf,dict);
}

/**
Expand Down Expand Up @@ -176,7 +157,7 @@ public String toString(){
if(root.isDirty()){
return serializeElementToString();
}else{
return new String(cbuf,root.startIndex,root.endIndex-root.startIndex);
return new String(root.cbuf,root.startIndex,root.endIndex-root.startIndex);
}
}

Expand Down
Loading

0 comments on commit 1518d7f

Please sign in to comment.