Skip to content

Commit

Permalink
Make map set writes atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Jan 1, 2021
1 parent 2ee9a4d commit 4591c42
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/src/db/atomicfile/AtomicFileMap.java
Expand Up @@ -33,6 +33,7 @@ public void put(ByteString key, ByteString value)
}
catch(java.io.IOException e)
{
System.out.println("Writing to: " + f);
throw new RuntimeException(e);
}

Expand Down Expand Up @@ -74,6 +75,10 @@ public ByteString get(ByteString key)
public File getFileForKey(ByteString key)
{
String name = HexUtil.getHexString(key);
if (name.length() ==0)
{
name ="null";
}

return new File(base, name);

Expand Down
21 changes: 13 additions & 8 deletions lib/src/db/atomicfile/AtomicFileMapSet.java
Expand Up @@ -11,6 +11,8 @@
import java.util.List;
import java.io.FileInputStream;
import java.io.DataInputStream;
import duckutil.AtomicFileOutputStream;
import snowblossom.lib.Globals;

public class AtomicFileMapSet extends DBMapMutationSet
{
Expand All @@ -26,11 +28,10 @@ public void add(ByteString key, ByteString value)
{
try
{

File f = getFileForKeyValue(key, value);
f.getParentFile().mkdirs();

FileOutputStream f_out = new FileOutputStream(f);
AtomicFileOutputStream f_out = new AtomicFileOutputStream(f);
f_out.write(value.toByteArray());
f_out.flush();
f_out.close();
Expand Down Expand Up @@ -61,12 +62,16 @@ public List<ByteString> getSet(ByteString key, int max_reply)
{
for(File f : dir.listFiles())
{
int sz = (int)f.length();
byte b[]=new byte[sz];
DataInputStream d_in = new DataInputStream(new FileInputStream(f));
d_in.readFully(b);
d_in.close();
lst.add(ByteString.copyFrom(b));
// Avoid picking up any tmp files
if (f.getName().length() == Globals.BLOCKCHAIN_HASH_LEN * 2)
{
int sz = (int)f.length();
byte b[]=new byte[sz];
DataInputStream d_in = new DataInputStream(new FileInputStream(f));
d_in.readFully(b);
d_in.close();
lst.add(ByteString.copyFrom(b));
}

if (lst.size() >= max_reply) break;
}
Expand Down
1 change: 1 addition & 0 deletions node/BUILD
Expand Up @@ -9,6 +9,7 @@ java_library(
"@duckutil//:lobstack_lib",
"//lib:lib",
"//lib:rocksdb",
"//lib:atomicfiledb",
"//client:client",
"@io_netty_netty_handler//:io_netty_netty_handler",
"@io_grpc_grpc_java//netty",
Expand Down
5 changes: 5 additions & 0 deletions node/src/SnowBlossomNode.java
Expand Up @@ -21,6 +21,7 @@
import snowblossom.lib.db.DB;
import snowblossom.lib.db.lobstack.LobstackDB;
import snowblossom.lib.db.rocksdb.JRocksDB;
import snowblossom.lib.db.atomicfile.AtomicFileDB;
import snowblossom.lib.tls.CertGen;
import snowblossom.lib.trie.HashedTrie;
import snowblossom.proto.WalletDatabase;
Expand Down Expand Up @@ -231,6 +232,10 @@ else if (db_type.equals("lobstack"))
{
db = new DB(config, new LobstackDB(config));
}
else if (db_type.equals("atomic_file"))
{
db = new DB(config, new AtomicFileDB(config));
}
else
{
logger.log(Level.SEVERE, String.format("Unknown db_type: %s", db_type));
Expand Down

0 comments on commit 4591c42

Please sign in to comment.