public void push_back(String key, byte[] value) throws TException {
//first get the next key.
List<byte[]> nextKey = bdb.fwmkeys(key.getBytes(),2);
BDBCUR cursor = new BDBCUR(bdb);
//look for edge cases
//no forward keys
if(nextKey.isEmpty() ){
this.put(key, value);
return;
}
//the input is all that was found. jump to last record.
if( Arrays.equals(nextKey.get(0),key.getBytes()) && nextKey.size() == 1){
if(!cursor.last())
throw new RuntimeException("Can't jump to last record");
}else{
//jump to key ahead of our input key
if(nextKey.size() == 1){
if(!cursor.jump(nextKey.get(0)))
throw new RuntimeException("Key should exist but failed to jump to it's location");
} else {
if(!cursor.jump(nextKey.get(1)))
throw new RuntimeException("Key should exist but failed to jump to it's location");
}
}
//back one should be the input key, if it's not then the key
//does not exist yet, so we add it
if(!Arrays.equals(cursor.key(),key.getBytes())){
if(!cursor.prev()){
this.put(key, value);
return;
}
}
// the key under this cursor should match, if not we add a new key
//based on input
if(!Arrays.equals(cursor.key(),key.getBytes())){
this.put(key, value);
return;
}
//we are at the right spot, now append the value
if(!cursor.put(value, BDBCUR.CPAFTER))
throw new TException(bdb.errmsg());
}