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.hbase.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 protected boolean movedToPrevious = false;
49
50 public PrefixTreeSeeker(boolean includeMvccVersion) {
51 this.includeMvccVersion = includeMvccVersion;
52 }
53
54 @Override
55 public void setCurrentBuffer(ByteBuffer fullBlockBuffer) {
56 block = fullBlockBuffer;
57 ptSearcher = DecoderFactory.checkOut(block, includeMvccVersion);
58 rewind();
59 }
60
61
62
63
64
65
66
67 public void releaseCurrentSearcher(){
68 DecoderFactory.checkIn(ptSearcher);
69 }
70
71
72 @Override
73 public ByteBuffer getKeyDeepCopy() {
74 return KeyValueUtil.copyKeyToNewByteBuffer(ptSearcher.current());
75 }
76
77
78 @Override
79 public ByteBuffer getValueShallowCopy() {
80 return CellUtil.getValueBufferShallowCopy(ptSearcher.current());
81 }
82
83
84
85
86 @Override
87 public ByteBuffer getKeyValueBuffer() {
88 return KeyValueUtil.copyToNewByteBuffer(ptSearcher.current());
89 }
90
91
92
93
94 @Override
95 public KeyValue getKeyValue() {
96 if (ptSearcher.current() == null) {
97 return null;
98 }
99 return KeyValueUtil.copyToNewKeyValue(ptSearcher.current());
100 }
101
102
103
104
105
106
107
108
109
110
111
112 public Cell get() {
113 return ptSearcher.current();
114 }
115
116 @Override
117 public void rewind() {
118 ptSearcher.positionAtFirstCell();
119 }
120
121 @Override
122 public boolean next() {
123 return ptSearcher.advance();
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
215 ptSearcher.previous();
216 }
217 return 1;
218 }
219
220 throw new RuntimeException("unexpected CellScannerPosition:"+position);
221 }
222
223 @Override
224 public int compareKey(KVComparator comparator, byte[] key, int offset, int length) {
225
226 ByteBuffer bb = getKeyDeepCopy();
227 return comparator.compareFlatKey(key, offset, length, bb.array(), bb.arrayOffset(), bb.limit());
228 }
229 }