org.mapdb.serializer.SerializerStringDelta2 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapdb Show documentation
Show all versions of mapdb Show documentation
MapDB provides concurrent Maps, Sets and Queues backed by disk storage or off-heap memory. It is a fast, scalable and easy to use embedded Java database.
package org.mapdb.serializer;
import org.jetbrains.annotations.NotNull;
import org.mapdb.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created by jan on 2/29/16.
*/
public class SerializerStringDelta2 implements GroupSerializer {
public interface StringArrayKeys {
int commonPrefixLen();
int length();
int[] getOffset();
StringArrayKeys deleteKey(int pos);
StringArrayKeys copyOfRange(int from, int to);
StringArrayKeys putKey(int pos, String newKey);
int compare(int pos1, String string);
int compare(int pos1, int pos2);
String getKeyString(int pos);
boolean hasUnicodeChars();
void serialize(DataOutput out, int prefixLen) throws IOException;
}
//PERF right now byte[] contains 7 bit characters, but it should be expandable to 8bit.
public static final class ByteArrayKeys implements StringArrayKeys {
final int[] offset;
final byte[] array;
ByteArrayKeys(int[] offset, byte[] array) {
this.offset = offset;
this.array = array;
if(CC.ASSERT && ! (array.length==0 || array.length == offset[offset.length-1]))
throw new DBException.DataCorruption("inconsistent array size");
}
ByteArrayKeys(DataInput2 in, int[] offsets, int prefixLen) throws IOException {
this.offset = offsets;
array = new byte[offsets[offsets.length-1]];
in.readFully(array, 0, prefixLen);
for(int i=0; i127)
return true;
}
return false;
}
public ByteArrayKeys putKey(int pos, byte[] newKey) {
byte[] bb = new byte[array.length+ newKey.length];
int split1 = pos==0? 0: offset[pos-1];
System.arraycopy(array,0,bb,0,split1);
//$DELAY$
System.arraycopy(newKey,0,bb,split1,newKey.length);
System.arraycopy(array,split1,bb,split1+newKey.length,array.length-split1);
int[] offsets = new int[offset.length+1];
int plus = 0;
int plusI = 0;
for(int i=0;i127)
return true;
}
return false;
}
@Override
public void serialize(DataOutput out, int prefixLen) throws IOException {
//write rest of the suffix
outWrite(out, 0, prefixLen);
//$DELAY$
//write suffixes
int aa = prefixLen;
for(int o:offset){
outWrite(out, aa, o);
aa = o+prefixLen;
}
}
private void outWrite(DataOutput out, int from, int to) throws IOException {
for(int i=from;i>>=1;
//$DELAY$
return useUnicode?
new CharArrayKeys(in2,offsets,prefixLen):
new ByteArrayKeys(in2,offsets,prefixLen);
}
@Override
public void valueArraySerialize(DataOutput2 out, Object vals) throws IOException {
StringArrayKeys keys = (StringArrayKeys) vals;
int offset = 0;
//write sizes
for(int o: keys.getOffset()){
out.packInt(o-offset);
offset = o;
}
//$DELAY$
int unicode = keys.hasUnicodeChars()?1:0;
//find and write common prefix
int prefixLen = keys.commonPrefixLen();
out.packInt((prefixLen<<1) | unicode);
keys.serialize(out, prefixLen);
}
@Override
public StringArrayKeys valueArrayCopyOfRange(Object vals, int from, int to) {
return ((StringArrayKeys)vals).copyOfRange(from,to);
}
@Override
public StringArrayKeys valueArrayDeleteValue(Object vals, int pos) {
//return vals.deleteKey(pos);
Object[] vv = valueArrayToArray(vals);
vv = DBUtil.arrayDelete(vv, pos, 1);
return valueArrayFromArray(vv);
}
@Override
public StringArrayKeys valueArrayEmpty() {
return new ByteArrayKeys(new int[0], new byte[0]);
}
@Override
public StringArrayKeys valueArrayFromArray(Object[] keys) {
if(keys.length==0)
return valueArrayEmpty();
//$DELAY$
boolean unicode = false;
//fill offsets
int[] offsets = new int[keys.length];
int old=0;
for(int i=0;i