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.Map;
35 import java.util.Set;
36 import java.util.concurrent.ConcurrentHashMap;
37
38 import org.treetank.access.conf.StandardSettings;
39 import org.treetank.api.IMetaEntry;
40 import org.treetank.bucket.interfaces.IBucket;
41 import org.treetank.exception.TTIOException;
42
43 import com.google.common.hash.HashCode;
44 import com.google.common.hash.Hasher;
45
46
47
48
49
50
51
52
53
54
55
56 public final class MetaBucket implements IBucket {
57
58
59 private final ConcurrentHashMap<IMetaEntry, IMetaEntry> mMetaMap;
60
61
62 private final long mBucketKey;
63
64
65
66
67
68
69
70 public MetaBucket(final long pBucketKey) {
71 mMetaMap = new ConcurrentHashMap<IMetaEntry, IMetaEntry>();
72 mBucketKey = pBucketKey;
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public IMetaEntry put(final IMetaEntry pKey, final IMetaEntry pVal) {
86 return mMetaMap.put(pKey, pVal);
87 }
88
89
90
91
92
93
94
95
96
97 public IMetaEntry get(final IMetaEntry pKey) {
98 return mMetaMap.get(pKey);
99 }
100
101
102
103
104
105
106
107 public int size() {
108 return mMetaMap.size();
109 }
110
111
112
113
114
115
116
117 public Set<Map.Entry<IMetaEntry, IMetaEntry>> entrySet() {
118 return mMetaMap.entrySet();
119 }
120
121
122
123
124
125
126
127
128
129 public IMetaEntry remove(final IMetaEntry pKey) {
130 return mMetaMap.remove(pKey);
131 }
132
133
134
135
136 @Override
137 public void serialize(final DataOutput pOutput) throws TTIOException {
138 try {
139 pOutput.writeInt(IConstants.METABUCKET);
140 pOutput.writeLong(mBucketKey);
141 pOutput.writeInt(mMetaMap.size());
142 for (final Map.Entry<IMetaEntry, IMetaEntry> key : mMetaMap.entrySet()) {
143 key.getKey().serialize(pOutput);
144 key.getValue().serialize(pOutput);
145 }
146 } catch (final IOException exc) {
147 throw new TTIOException(exc);
148 }
149 }
150
151
152
153
154 @Override
155 public long getBucketKey() {
156 return mBucketKey;
157 }
158
159
160
161
162 @Override
163 public String toString() {
164 return toStringHelper(this).add("mBucketKey", mBucketKey).add("mMetaMap", mMetaMap).toString();
165 }
166
167
168
169
170 @Override
171 public int hashCode() {
172 final int prime = 42677;
173 int result = 1;
174 result = prime * result + (int)(mBucketKey ^ (mBucketKey >>> 32));
175 result = prime * result + ((mMetaMap == null) ? 0 : mMetaMap.hashCode());
176 return result;
177 }
178
179
180
181
182 @Override
183 public boolean equals(Object obj) {
184 return obj.hashCode()==this.hashCode();
185 }
186
187
188
189
190 @Override
191 public HashCode secureHash() {
192 final Hasher code = StandardSettings.HASHFUNC.newHasher().putLong(mBucketKey);
193 for (final IMetaEntry key : mMetaMap.keySet()) {
194 final IMetaEntry val = mMetaMap.get(key);
195 code.putObject(key, key.getFunnel());
196 code.putObject(val, val.getFunnel());
197 }
198 return code.hash();
199 }
200
201 }