Explorar el Código

forest can be written to/read from disk correctly

Boyoung- hace 8 años
padre
commit
8516d0c7e6

+ 0 - 0
data/.gitkeep


+ 1 - 1
src/crypto/OramCrypto.java

@@ -5,7 +5,7 @@ import java.security.SecureRandom;
 
 public class OramCrypto {
 	public static SecureRandom sr;
-	
+
 	static {
 		try {
 			sr = SecureRandom.getInstance("SHA1PRNG");

+ 7 - 3
src/oram/Bucket.java

@@ -1,10 +1,16 @@
 package oram;
 
+import java.io.Serializable;
 import java.util.Random;
 
 import exceptions.LengthNotMatchException;
 
-public class Bucket {
+public class Bucket implements Serializable {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
 	private int numBytes;
 	private Tuple[] tuples;
 
@@ -18,8 +24,6 @@ public class Bucket {
 	}
 
 	public Bucket(Tuple[] tuples) {
-		if (tuples == null)
-			throw new NullPointerException();
 		this.tuples = tuples;
 		numBytes = tuples.length * tuples[0].getNumBytes();
 	}

+ 101 - 13
src/oram/Forest.java

@@ -1,5 +1,11 @@
 package oram;
 
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
 import java.math.BigInteger;
 import java.util.HashMap;
 import java.util.Random;
@@ -8,20 +14,32 @@ import crypto.OramCrypto;
 import exceptions.LengthNotMatchException;
 import util.Util;
 
-public class Forest {
+public class Forest implements Serializable {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	private static String folderName = "data/";
+	private String defaultFileName;
+
+	private int tau;
+	private int addrBits;
+	private long numInsertRecords;
+
 	private long numBytes;
 	private Tree[] trees;
 
 	// build empty forest and insert records according to config file
 	public Forest() {
 		Metadata md = new Metadata();
-		numBytes = md.getForestBytes();
+		init(md);
 		initTrees(md, null);
 		insertRecords(md);
 	}
 
 	public Forest(Metadata md) {
-		numBytes = md.getForestBytes();
+		init(md);
 		initTrees(md, null);
 		insertRecords(md);
 	}
@@ -29,17 +47,43 @@ public class Forest {
 	// build empty/random content forest
 	public Forest(Random rand) {
 		Metadata md = new Metadata();
-		numBytes = md.getForestBytes();
+		init(md);
 		initTrees(md, rand);
 	}
 
 	// only used in xor operation
-	private Forest(Tree[] trees) {
-		if (trees == null)
-			throw new NullPointerException();
-		this.trees = trees;
-		for (int i = 0; i < trees.length; i++)
-			numBytes += trees[i].getNumBytes();
+	// does not shallow/deep copy trees
+	private Forest(Forest f) {
+		defaultFileName = f.getDefaultFileName();
+		tau = f.getTau();
+		addrBits = f.getAddrBits();
+		numInsertRecords = f.getNumInsertRecords();
+		numBytes = f.getNumBytes();
+		trees = new Tree[f.getNumTrees()];
+	}
+
+	private void init(Metadata md) {
+		defaultFileName = md.getDefaultForestFileName();
+		tau = md.getTau();
+		addrBits = md.getAddrBits();
+		numInsertRecords = md.getNumInsertRecords();
+		numBytes = md.getForestBytes();
+	}
+
+	public String getDefaultFileName() {
+		return defaultFileName;
+	}
+
+	public int getTau() {
+		return tau;
+	}
+
+	public int getAddrBits() {
+		return addrBits;
+	}
+
+	public long getNumInsertRecords() {
+		return numInsertRecords;
 	}
 
 	public long getNumBytes() {
@@ -149,10 +193,10 @@ public class Forest {
 	public Forest xor(Forest f) {
 		if (!this.sameLength(f))
 			throw new LengthNotMatchException(numBytes + " != " + f.getNumBytes());
-		Tree[] newTrees = new Tree[trees.length];
+		Forest newForest = new Forest(f);
 		for (int i = 0; i < trees.length; i++)
-			newTrees[i] = trees[i].xor(f.getTree(i));
-		return new Forest(newTrees);
+			newForest.trees[i] = trees[i].xor(f.getTree(i));
+		return newForest;
 	}
 
 	public void setXor(Forest f) {
@@ -180,4 +224,48 @@ public class Forest {
 		System.out.println("===== End of Forest =====");
 		System.out.println();
 	}
+
+	public void writeToFile() {
+		writeToFile(defaultFileName);
+	}
+
+	public void writeToFile(String filename) {
+		FileOutputStream fos = null;
+		ObjectOutputStream oos = null;
+		try {
+			fos = new FileOutputStream(folderName + filename);
+			oos = new ObjectOutputStream(fos);
+			oos.writeObject(this);
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			if (oos != null)
+				try {
+					oos.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+		}
+	}
+
+	public static Forest readFromFile(String filename) {
+		FileInputStream fis = null;
+		ObjectInputStream ois = null;
+		Forest forest = null;
+		try {
+			fis = new FileInputStream(folderName + filename);
+			ois = new ObjectInputStream(fis);
+			forest = (Forest) ois.readObject();
+		} catch (IOException | ClassNotFoundException e) {
+			e.printStackTrace();
+		} finally {
+			if (ois != null)
+				try {
+					ois.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+		}
+		return forest;
+	}
 }

+ 25 - 7
src/oram/Metadata.java

@@ -12,7 +12,9 @@ import java.util.Map;
 import org.yaml.snakeyaml.Yaml;
 
 public class Metadata {
-	private String CONFIG_FILE = "config/config.yaml";
+	private String configFolder = "config/";
+	private String configFileName = "config.yaml";
+	private String defaultForestFileName;
 
 	private String TAU = "tau";
 	private String ADDRBITS = "addrBits";
@@ -48,7 +50,7 @@ public class Metadata {
 	private long forestBytes;
 
 	public Metadata() {
-		setup(CONFIG_FILE);
+		setup(configFileName);
 	}
 
 	public Metadata(String filename) {
@@ -59,7 +61,7 @@ public class Metadata {
 		Yaml yaml = new Yaml();
 		InputStream input = null;
 		try {
-			input = new FileInputStream(new File(filename));
+			input = new FileInputStream(new File(configFolder + filename));
 		} catch (FileNotFoundException e) {
 			e.printStackTrace();
 		}
@@ -74,6 +76,7 @@ public class Metadata {
 		tempStashSize = Integer.parseInt(configMap.get(STASH).toString());
 
 		init();
+		setDefaultForestFileName();
 	}
 
 	private void init() {
@@ -166,11 +169,11 @@ public class Metadata {
 		System.out.println();
 	}
 
-	public void write(String filename) {
+	public void writeToFile(String filename) {
 		Yaml yaml = new Yaml();
 		FileWriter writer = null;
 		try {
-			writer = new FileWriter(filename);
+			writer = new FileWriter(configFolder + filename);
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
@@ -186,8 +189,23 @@ public class Metadata {
 		yaml.dump(configMap, writer);
 	}
 
-	public void write() {
-		write(CONFIG_FILE);
+	public void writeToFile() {
+		writeToFile(configFileName);
+	}
+
+	private void setDefaultForestFileName() {
+		defaultForestFileName = "forest_";
+		defaultForestFileName += "t" + tau;
+		defaultForestFileName += "m" + addrBits;
+		defaultForestFileName += "w" + w;
+		defaultForestFileName += "d" + dBytes;
+		defaultForestFileName += "_i" + numInsertRecords;
+		defaultForestFileName += "s" + tempStashSize;
+		defaultForestFileName += ".bin";
+	}
+
+	public String getDefaultForestFileName() {
+		return defaultForestFileName;
 	}
 
 	public int getTau() {

+ 9 - 9
src/oram/Tree.java

@@ -1,5 +1,6 @@
 package oram;
 
+import java.io.Serializable;
 import java.math.BigInteger;
 import java.util.Random;
 
@@ -7,7 +8,12 @@ import exceptions.InvalidPathLabelException;
 import exceptions.LengthNotMatchException;
 import util.Array64;
 
-public class Tree {
+public class Tree implements Serializable {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
 	private int treeIndex;
 	private int w;
 	private int stashSize;
@@ -50,7 +56,7 @@ public class Tree {
 	}
 
 	// only used for xor operation
-	// does not deep copy buckets
+	// does not shallow/deep copy buckets
 	private Tree(Tree t) {
 		treeIndex = t.getTreeIndex();
 		w = t.getW();
@@ -66,15 +72,9 @@ public class Tree {
 		numBuckets = t.getNumBuckets();
 		numBytes = t.getNumBytes();
 		d = t.getD();
-
 		buckets = new Array64<Bucket>(numBuckets);
 	}
 
-	// only used for xor operation
-	private Array64<Bucket> getBuckets() {
-		return buckets;
-	}
-
 	public Bucket getBucket(long i) {
 		return buckets.get(i);
 	}
@@ -91,7 +91,7 @@ public class Tree {
 		Tree newTree = new Tree(t);
 		for (long i = 0; i < numBuckets; i++)
 			// cannot use newTree.setBucket() here
-			newTree.getBuckets().set(i, buckets.get(i).xor(t.getBucket(i)));
+			newTree.buckets.set(i, buckets.get(i).xor(t.getBucket(i)));
 		return newTree;
 	}
 

+ 7 - 3
src/oram/Tuple.java

@@ -1,5 +1,6 @@
 package oram;
 
+import java.io.Serializable;
 import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Random;
@@ -7,7 +8,12 @@ import java.util.Random;
 import exceptions.LengthNotMatchException;
 import util.Util;
 
-public class Tuple {
+public class Tuple implements Serializable {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
 	private int numBytes;
 	private byte[] F;
 	private byte[] N;
@@ -29,8 +35,6 @@ public class Tuple {
 	}
 
 	public Tuple(byte[] f, byte[] n, byte[] l, byte[] a) {
-		if (f == null || n == null || l == null || a == null)
-			throw new NullPointerException();
 		numBytes = f.length + n.length + l.length + a.length;
 		F = f;
 		N = n;

+ 8 - 1
src/util/Array64.java

@@ -1,6 +1,13 @@
 package util;
 
-public class Array64<T> {
+import java.io.Serializable;
+
+public class Array64<T> implements Serializable {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
 	private final int CHUNK_SIZE = 1024 * 1024 * 1024;
 
 	private long size;

+ 2 - 1
test/oram/TestForest.java

@@ -9,7 +9,8 @@ public class TestForest {
 
 	public static void main(String[] args) {
 		Metadata md = new Metadata();
-		Forest forest = new Forest(md);
+		// Forest forest = new Forest(md);
+		Forest forest = Forest.readFromFile(md.getDefaultForestFileName());
 		int tau = md.getTau();
 		int addrBits = md.getAddrBits();
 		long numRecords = md.getNumInsertRecords();

+ 12 - 0
test/ui/InitForest.java

@@ -0,0 +1,12 @@
+package ui;
+
+import oram.Forest;
+
+public class InitForest {
+
+	public static void main(String[] args) {
+		Forest forest = new Forest();
+		forest.writeToFile();
+	}
+
+}