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