1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.treetank.bucket;
29
30 import static com.google.common.base.Objects.toStringHelper;
31
32 import java.io.DataOutput;
33 import java.io.IOException;
34 import java.util.Arrays;
35
36 import org.treetank.access.conf.StandardSettings;
37 import org.treetank.bucket.interfaces.IReferenceBucket;
38 import org.treetank.exception.TTIOException;
39
40 import com.google.common.hash.HashCode;
41 import com.google.common.hash.Hasher;
42
43
44
45
46
47
48
49
50
51
52
53 public final class IndirectBucket implements IReferenceBucket {
54
55
56 private final long[] mReferenceKeys;
57
58 private final byte[][] mReferenceHashs;
59
60
61 private final long mBucketKey;
62
63
64
65
66
67
68
69 public IndirectBucket(final long pBucketKey) {
70 mBucketKey = pBucketKey;
71 mReferenceKeys = new long[IConstants.CONTENT_COUNT];
72 mReferenceHashs = new byte[IConstants.CONTENT_COUNT][];
73 Arrays.fill(mReferenceHashs, new byte[0]);
74 }
75
76
77
78
79 @Override
80 public void serialize(final DataOutput pOutput) throws TTIOException {
81 try {
82 pOutput.writeInt(IConstants.INDIRCTBUCKET);
83 pOutput.writeLong(mBucketKey);
84 for (long key : mReferenceKeys) {
85 pOutput.writeLong(key);
86 }
87 for (byte[] hash : mReferenceHashs) {
88 pOutput.writeInt(hash.length);
89 pOutput.write(hash);
90 }
91 } catch (final IOException exc) {
92 throw new TTIOException(exc);
93 }
94 }
95
96
97
98
99 @Override
100 public long getBucketKey() {
101 return mBucketKey;
102 }
103
104
105
106
107 @Override
108 public long[] getReferenceKeys() {
109 return mReferenceKeys;
110 }
111
112
113
114
115 @Override
116 public void setReferenceKey(int pIndex, long pKey) {
117 mReferenceKeys[pIndex] = pKey;
118 }
119
120
121
122
123 @Override
124 public String toString() {
125 return toStringHelper(this).add("mBucketKey", mBucketKey).add("mReferenceKeys",
126 Arrays.toString(mReferenceKeys)).add("mReferenceHashs", Arrays.toString(mReferenceHashs))
127 .toString();
128 }
129
130
131
132
133 @Override
134 public byte[][] getReferenceHashs() {
135 return mReferenceHashs;
136 }
137
138
139
140
141 @Override
142 public void setReferenceHash(int pIndex, byte[] pHash) {
143 mReferenceHashs[pIndex] = pHash;
144 }
145
146
147
148
149 @Override
150 public int hashCode() {
151 final int prime = 81551;
152 int result = 1;
153 result = prime * result + (int)(mBucketKey ^ (mBucketKey >>> 32));
154 result = prime * result + Arrays.hashCode(mReferenceKeys);
155 for (byte[] hash : mReferenceHashs) {
156 result = prime * result + Arrays.hashCode(hash);
157 }
158 return result;
159 }
160
161
162
163
164 @Override
165 public boolean equals(Object obj) {
166 return obj.hashCode() == this.hashCode();
167 }
168
169
170
171
172 @Override
173 public HashCode secureHash() {
174 final Hasher code = StandardSettings.HASHFUNC.newHasher().putLong(mBucketKey);
175 for (int i = 0; i < mReferenceKeys.length; i++) {
176 code.putLong(mReferenceKeys[i]);
177 code.putBytes(mReferenceHashs[i]);
178 }
179 return code.hash();
180 }
181
182 }