1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.codec.prefixtree;
20
21 import java.nio.ByteBuffer;
22
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.Cell;
25 import org.apache.hadoop.hbase.CellUtil;
26 import org.apache.hadoop.hbase.KeyValue;
27 import org.apache.hadoop.hbase.KeyValueUtil;
28 import org.apache.hadoop.hbase.KeyValue.KVComparator;
29 import org.apache.hadoop.hbase.codec.prefixtree.decode.DecoderFactory;
30 import org.apache.hadoop.hbase.codec.prefixtree.decode.PrefixTreeArraySearcher;
31 import org.apache.hadoop.hbase.codec.prefixtree.scanner.CellScannerPosition;
32 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoder.EncodedSeeker;
33
34
35
36
37
38
39
40
41
42 @InterfaceAudience.Private
43 public class PrefixTreeSeeker implements EncodedSeeker {
44
45 protected ByteBuffer block;
46 protected boolean includeMvccVersion;
47 protected PrefixTreeArraySearcher ptSearcher;
48
49 public PrefixTreeSeeker(boolean includeMvccVersion) {
50 this.includeMvccVersion = includeMvccVersion;
51 }
52
53 @Override
54 public void setCurrentBuffer(ByteBuffer fullBlockBuffer) {
55 block = fullBlockBuffer;
56 ptSearcher = DecoderFactory.checkOut(block, includeMvccVersion);
57 rewind();
58 }
59
60
61
62
63
64
65
66 public void releaseCurrentSearcher(){
67 DecoderFactory.checkIn(ptSearcher);
68 }
69
70
71 @Override
72 public ByteBuffer getKeyDeepCopy() {
73 return KeyValueUtil.copyKeyToNewByteBuffer(ptSearcher.current());
74 }
75
76
77 @Override
78 public ByteBuffer getValueShallowCopy() {
79 return CellUtil.getValueBufferShallowCopy(ptSearcher.current());
80 }
81
82
83
84
85 @Override
86 public ByteBuffer getKeyValueBuffer() {
87 return KeyValueUtil.copyToNewByteBuffer(ptSearcher.current());
88 }
89
90
91
92
93 @Override
94 public KeyValue getKeyValue() {
95 if (ptSearcher.current() == null) {
96 return null;
97 }
98 return KeyValueUtil.copyToNewKeyValue(ptSearcher.current());
99 }
100
101
102
103
104
105
106
107
108
109
110
111 public Cell get() {
112 return ptSearcher.current();
113 }
114
115 @Override
116 public void rewind() {
117 ptSearcher.positionAtFirstCell();
118 }
119
120 @Override
121 public boolean next() {
122 return ptSearcher.advance();
123 }
124
125
126 public boolean advance() {
127 return ptSearcher.advance();
128 }
129
130
131 private static final boolean USE_POSITION_BEFORE = false;
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 @Override
151 public int seekToKeyInBlock(byte[] keyOnlyBytes, int offset, int length,
152 boolean forceBeforeOnExactMatch) {
153 if (USE_POSITION_BEFORE) {
154 return seekToOrBeforeUsingPositionAtOrBefore(keyOnlyBytes, offset, length,
155 forceBeforeOnExactMatch);
156 }else{
157 return seekToOrBeforeUsingPositionAtOrAfter(keyOnlyBytes, offset, length,
158 forceBeforeOnExactMatch);
159 }
160 }
161
162
163
164
165
166
167
168
169 protected int seekToOrBeforeUsingPositionAtOrBefore(byte[] keyOnlyBytes, int offset, int length,
170 boolean seekBefore){
171
172 KeyValue kv = KeyValue.createKeyValueFromKey(keyOnlyBytes, offset, length);
173
174 CellScannerPosition position = ptSearcher.seekForwardToOrBefore(kv);
175
176 if(CellScannerPosition.AT == position){
177 if (seekBefore) {
178 ptSearcher.previous();
179 return 1;
180 }
181 return 0;
182 }
183
184 return 1;
185 }
186
187
188 protected int seekToOrBeforeUsingPositionAtOrAfter(byte[] keyOnlyBytes, int offset, int length,
189 boolean seekBefore){
190
191 KeyValue kv = KeyValue.createKeyValueFromKey(keyOnlyBytes, offset, length);
192
193
194 CellScannerPosition position = ptSearcher.seekForwardToOrAfter(kv);
195
196 if(CellScannerPosition.AT == position){
197 if (seekBefore) {
198 ptSearcher.previous();
199 return 1;
200 }
201 return 0;
202
203 }
204
205 if(CellScannerPosition.AFTER == position){
206 if(!ptSearcher.isBeforeFirst()){
207 ptSearcher.previous();
208 }
209 return 1;
210 }
211
212 if(position == CellScannerPosition.AFTER_LAST){
213 if (seekBefore) {
214 ptSearcher.previous();
215 }
216 return 1;
217 }
218
219 throw new RuntimeException("unexpected CellScannerPosition:"+position);
220 }
221
222 @Override
223 public int compareKey(KVComparator comparator, byte[] key, int offset, int length) {
224
225 ByteBuffer bb = getKeyDeepCopy();
226 return comparator.compareFlatKey(key, offset, length, bb.array(), bb.arrayOffset(), bb.limit());
227 }
228 }