Browse Source

update on Tree

Boyoung- 8 years ago
parent
commit
3e50cf3045

+ 0 - 2
src/Util/Array64.java

@@ -22,7 +22,6 @@ public class Array64<T> {
 		return size;
 	}
 
-	// WARNING: shallow copy!
 	public T get(long index) {
 		if (index < 0 || index >= size)
 			throw new ArrayIndexOutOfBoundsException("" + index);
@@ -32,7 +31,6 @@ public class Array64<T> {
 		// return SerializationUtils.clone(data[chunk][offset]);
 	}
 
-	// WARNING: shallow copy!
 	public void set(long index, T item) {
 		if (index < 0 || index >= size)
 			throw new ArrayIndexOutOfBoundsException("" + index);

+ 16 - 0
src/exceptions/InvalidPathLabelException.java

@@ -0,0 +1,16 @@
+package exceptions;
+
+public class InvalidPathLabelException extends RuntimeException {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public InvalidPathLabelException() {
+		super();
+	}
+
+	public InvalidPathLabelException(String message) {
+		super(message);
+	}
+}

+ 16 - 0
src/exceptions/LengthNotMatchException.java

@@ -0,0 +1,16 @@
+package exceptions;
+
+public class LengthNotMatchException extends RuntimeException {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public LengthNotMatchException() {
+		super();
+	}
+
+	public LengthNotMatchException(String message) {
+		super(message);
+	}
+}

+ 8 - 5
src/oram/Bucket.java

@@ -1,8 +1,14 @@
 package oram;
 
 public class Bucket {
+	private Tree tree;
 	private Tuple[] tuples;
 
+	public Bucket(Tree tree, Tuple[] tuples) {
+		this.tree = tree;
+		this.tuples = tuples;
+	}
+
 	public Tuple[] getTuples() {
 		return tuples;
 	}
@@ -12,12 +18,9 @@ public class Bucket {
 	}
 
 	public byte[] toByteArray() {
-		// TODO: remove this firstTuple if has access to tree metadata
-		byte[] firstTuple = tuples[0].toByteArray();
-		int tupleBytes = firstTuple.length;
+		int tupleBytes = tree.getTupleBytes();
 		byte[] bucket = new byte[tupleBytes * tuples.length];
-		System.arraycopy(firstTuple, 0, bucket, 0, tupleBytes);
-		for (int i = 1; i < tuples.length; i++) {
+		for (int i = 0; i < tuples.length; i++) {
 			byte[] tuple = tuples[i].toByteArray();
 			System.arraycopy(tuple, 0, bucket, i * tupleBytes, tupleBytes);
 		}

+ 65 - 120
src/oram/Tree.java

@@ -1,6 +1,10 @@
 package oram;
 
+import java.math.BigInteger;
+
 import Util.Array64;
+import exceptions.InvalidPathLabelException;
+import exceptions.LengthNotMatchException;
 
 public class Tree {
 	private int treeIndex;
@@ -33,151 +37,92 @@ public class Tree {
 		d = lBits + 1;
 
 		buckets = new Array64<Bucket>(numBuckets);
+	}
+
+	public int getTreeIndex() {
+		return treeIndex;
+	}
 
+	public int getW() {
+		return w;
 	}
 
-	/*
-	private byte[] getByteBucket(long bucketNum) {
-		if (bucketNum < 0 || bucketNum >= ForestMetadata.getNumBuckets(index))
-			try {
-				throw new TreeException("Bucket number error");
-			} catch (TreeException e) {
-				e.printStackTrace();
-			}
+	public int getStashSize() {
+		return stashSize;
+	}
 
-		int bucketBytes = ForestMetadata.getBucketBytes(index);
-		long start = ForestMetadata.getTreeOffset(index) + bucketNum * bucketBytes;
-		return Forest.getForestData(start, bucketBytes);
+	public int getNBits() {
+		return nBits;
 	}
 
-	public Bucket getBucket(long bucketNum) {
-		return new Bucket(index, getByteBucket(bucketNum));
+	public int getLBits() {
+		return lBits;
 	}
 
-	private void setByteBucket(byte[] bucket, long bucketNum) {
-		if (bucketNum < 0 || bucketNum >= ForestMetadata.getNumBuckets(index))
-			try {
-				throw new TreeException("Bucket number error");
-			} catch (TreeException e) {
-				e.printStackTrace();
-			}
+	public int getABits() {
+		return aBits;
+	}
 
-		int bucketBytes = ForestMetadata.getBucketBytes(index);
-		if (bucket.length != bucketBytes)
-			try {
-				throw new TreeException("Bucket length error");
-			} catch (TreeException e) {
-				e.printStackTrace();
-			}
+	public int getNBytes() {
+		return nBytes;
+	}
 
-		long start = ForestMetadata.getTreeOffset(index) + bucketNum * bucketBytes;
-		Forest.setForestData(start, bucket);
+	public int getLBytes() {
+		return lBytes;
 	}
 
-	public void setBucket(Bucket bucket, long bucketNum) {
-		setByteBucket(bucket.toByteArray(), bucketNum);
+	public int getABytes() {
+		return aBytes;
 	}
 
-	private List<Long> getBucketIndicesOnPath(long L) {
-		List<Long> indices = new ArrayList<Long>();
-		if (index == 0) {
-			indices.add(0L);
-			return indices;
-		}
+	public int getTupleBytes() {
+		return tupleBytes;
+	}
 
-		if (L < 0 || L >= ForestMetadata.getNumLeaves(index))
-			try {
-				throw new TreeException("L=" + L + ": Invalid path");
-			} catch (TreeException e1) {
-				e1.printStackTrace();
-			}
+	public long getNumBucket() {
+		return numBuckets;
+	}
 
-		int e = ForestMetadata.getLeafExpansion();
-		int lBits = ForestMetadata.getLBits(index);
+	public int getD() {
+		return d;
+	}
 
-		for (int i = 0; i < lBits; i++) {
-			long bucketIndex = (L >> (lBits - i)) + (long) Math.pow(2, i) - 1;
-			indices.add(bucketIndex);
-		}
+	public Array64<Bucket> getBuckets() {
+		return buckets;
+	}
 
-		long bucketIndex = ForestMetadata.getNumLeaves(index) - 1 + L * e;
-		for (int i = 0; i < e; i++)
-			indices.add(bucketIndex + i);
+	public Bucket getBucket(long bucketIndex) {
+		return buckets.get(bucketIndex);
+	}
 
-		return indices;
+	public void setBucket(long bucketIndex, Bucket bucket) {
+		buckets.set(bucketIndex, bucket);
 	}
 
-	public Bucket[] getBucketsOnPath(BigInteger L) {
-		if (L == null && index != 0) {
-			try {
-				throw new TreeException("L is null");
-			} catch (TreeException e) {
-				e.printStackTrace();
-			}
-			return null;
-		} else if (L == null && index == 0)
-			return getBucketsOnPath(0);
-		else
-			return getBucketsOnPath(L.longValue());
+	private long[] getBucketIndicesOnPath(long L) {
+		if (treeIndex == 0)
+			return new long[] { 0 };
+		if (L < 0 || L > numBuckets / 2)
+			throw new InvalidPathLabelException(BigInteger.valueOf(L).toString(2));
+		long[] indices = new long[d];
+		for (int i = 0; i < d; i++)
+			indices[i] = (L >> (d - i)) + (long) Math.pow(2, i) - 1;
+		return indices;
 	}
 
 	public Bucket[] getBucketsOnPath(long L) {
-		List<Long> indices = getBucketIndicesOnPath(L);
-		Bucket[] buckets = new Bucket[indices.size()];
-		for (int i = 0; i < indices.size(); i++)
-			buckets[i] = getBucket(indices.get(i));
+		long[] indices = getBucketIndicesOnPath(L);
+		Bucket[] buckets = new Bucket[indices.length];
+		for (int i = 0; i < indices.length; i++)
+			buckets[i] = getBucket(indices[i]);
 		return buckets;
 	}
 
-	public Bucket[] getBucketsOnPath(String L) throws TreeException, BucketException {
-		if (L == null)
-			throw new TreeException("L is null");
-		if (L.equals("") && index != 0)
-			throw new TreeException("Invalid L");
-
-		if (L.equals(""))
-			return getBucketsOnPath(0);
-		return getBucketsOnPath(new BigInteger(L, 2).longValue());
-	}
-
-	public void setBucketsOnPath(Bucket[] buckets, BigInteger L) {
-		if (L == null && index != 0)
-			try {
-				throw new TreeException("L is null");
-			} catch (TreeException e) {
-				e.printStackTrace();
-			}
-		else if (L == null && index == 0) {
-			setBucketsOnPath(buckets, 0);
-			return;
-		} else
-			setBucketsOnPath(buckets, L.longValue());
-	}
-
-	public void setBucketsOnPath(Bucket[] buckets, long L) {
-		List<Long> indices = getBucketIndicesOnPath(L);
-		if (indices.size() != buckets.length)
-			try {
-				throw new TreeException("Number of buckets is not correct");
-			} catch (TreeException e) {
-				e.printStackTrace();
-			}
-
-		for (int i = 0; i < indices.size(); i++)
-			setBucket(buckets[i], indices.get(i));
-	}
-
-	public void setBucketsOnPath(Bucket[] buckets, String L) throws TreeException {
-		if (L == null)
-			throw new TreeException("L is null");
-		if (L.equals("") && index != 0)
-			throw new TreeException("Invalid L");
-
-		if (L.equals("")) {
-			setBucketsOnPath(buckets, 0);
-			return;
-		}
-		setBucketsOnPath(buckets, new BigInteger(L, 2).longValue());
-	}
-*/
+	public void setBucketsOnPath(long L, Bucket[] buckets) {
+		long[] indices = getBucketIndicesOnPath(L);
+		if (indices.length != buckets.length)
+			throw new LengthNotMatchException(indices.length + " != " + buckets.length);
+		for (int i = 0; i < indices.length; i++)
+			setBucket(indices[i], buckets[i]);
+	}
 }