|
@@ -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]);
|
|
|
+ }
|
|
|
}
|