Skip to content

Commit

Permalink
Finalize revertible
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Sep 12, 2024
1 parent a49de20 commit d729d86
Show file tree
Hide file tree
Showing 7 changed files with 564 additions and 11 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/lbry/database/Prefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ public byte getValue(){
return this.value;
}

public static Prefix getByValue(char value){
return Prefix.getByValue((byte) value);
}

public static Prefix getByValue(byte value){
for(Prefix p : Prefix.values()){
if(p.value==value){
return p;
}
}
return null;
}

}
20 changes: 15 additions & 5 deletions src/main/java/com/lbry/database/PrefixDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import com.lbry.database.rows.*;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.rocksdb.ColumnFamilyDescriptor;
import org.rocksdb.ColumnFamilyHandle;
Expand Down Expand Up @@ -103,7 +100,20 @@ public PrefixDB(String path,int maxOpenFiles,String secondaryPath,int maxUndoDep

this.database = RocksDB.open(new DBOptions(options),path,columnFamilyDescriptors,this.columnFamilyHandles);

this.operationStack = new RevertibleOperationStack();
Set<Byte> unsafePrefixes = new HashSet<>();//TODO
boolean enforceIntegrity = false;//TODO
this.operationStack = new RevertibleOperationStack((byte[] key) -> {
try{
return Optional.of(this.get(key));
}catch(RocksDBException e){}
return Optional.empty();
},(List<byte[]> keys) -> {
List<Optional<byte[]>> optionalKeys = new ArrayList<>();
for(byte[] key : keys){
optionalKeys.add(Optional.of(key));
}
return optionalKeys;
},unsafePrefixes,enforceIntegrity);

this.maxUndoDepth = maxUndoDepth;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lbry.database.revert;

public class OperationStackIntegrityException extends RuntimeException{

public OperationStackIntegrityException(String message){
super(message);
}

}
47 changes: 43 additions & 4 deletions src/main/java/com/lbry/database/revert/RevertibleOperation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.lbry.database.revert;

import com.lbry.database.Prefix;
import com.lbry.database.rows.PrefixRow;
import com.lbry.database.util.Tuple2;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public abstract class RevertibleOperation{
Expand Down Expand Up @@ -30,9 +36,32 @@ public RevertibleOperation invert(){
throw new RuntimeException("Not implemented");
}

//TODO PACK
//TODO UNPACK
public byte[] pack(){
return ByteBuffer.allocate(1+4+4+this.key.length+this.value.length).order(ByteOrder.BIG_ENDIAN)
.put((byte) (this.isPut?0x01:0x00))
.putInt(this.key.length)
.putInt(this.value.length)
.put(this.key)
.put(this.value)
.array();
}

public static Tuple2<RevertibleOperation,byte[]> unpack(byte[] packed){
ByteBuffer bb = ByteBuffer.wrap(packed).order(ByteOrder.BIG_ENDIAN);
boolean isPut = (bb.get() & 0xFF)!=0x00;
int keyLength = bb.getInt();
int valueLength = bb.getInt();
byte[] keyBytes = new byte[keyLength];
bb.get(keyBytes);
byte[] valueBytes = new byte[valueLength];
bb.get(valueBytes);
byte[] remainingPacked = new byte[bb.remaining()];
bb.get(remainingPacked);
if(isPut){
return new Tuple2<>(new RevertiblePut(keyBytes,valueBytes),remainingPacked);
}
return new Tuple2<>(new RevertibleDelete(keyBytes,valueBytes),remainingPacked);
}

@Override
public boolean equals(Object obj){
Expand All @@ -43,7 +72,17 @@ public boolean equals(Object obj){
return false;
}

//TODO REPR
//TODO STR
@Override
public String toString() {
Prefix prefix = Prefix.getByValue(this.value[0]);
String prefixStr = (prefix!=null?prefix.name():"?");
String k = "?";
String v = "?";
if(prefix!=null){
k = PrefixRow.TYPES.get(prefix).unpackKey(this.key).toString();
v = PrefixRow.TYPES.get(prefix).unpackKey(this.value).toString();
}
return (this.isPut?"PUT":"DELETE")+" "+prefixStr+": "+k+" | "+v;
}

}
Loading

0 comments on commit d729d86

Please sign in to comment.