1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.io.hfile;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.KeyValue;
26
27 /**
28 * A scanner allows you to position yourself within a HFile and
29 * scan through it. It allows you to reposition yourself as well.
30 *
31 * <p>A scanner doesn't always have a key/value that it is pointing to
32 * when it is first created and before
33 * {@link #seekTo()}/{@link #seekTo(byte[])} are called.
34 * In this case, {@link #getKey()}/{@link #getValue()} returns null. At most
35 * other times, a key and value will be available. The general pattern is that
36 * you position the Scanner using the seekTo variants and then getKey and
37 * getValue.
38 */
39 @InterfaceAudience.Private
40 public interface HFileScanner {
41 /**
42 * SeekTo or just before the passed <code>key</code>. Examine the return
43 * code to figure whether we found the key or not.
44 * Consider the key stream of all the keys in the file,
45 * <code>k[0] .. k[n]</code>, where there are n keys in the file.
46 * @param key Key to find.
47 * @return -1, if key < k[0], no position;
48 * 0, such that k[i] = key and scanner is left in position i; and
49 * 1, such that k[i] < key, and scanner is left in position i.
50 * The scanner will position itself between k[i] and k[i+1] where
51 * k[i] < key <= k[i+1].
52 * If there is no key k[i+1] greater than or equal to the input key, then the
53 * scanner will position itself at the end of the file and next() will return
54 * false when it is called.
55 * @throws IOException
56 */
57 int seekTo(byte[] key) throws IOException;
58 int seekTo(byte[] key, int offset, int length) throws IOException;
59 /**
60 * Reseek to or just before the passed <code>key</code>. Similar to seekTo
61 * except that this can be called even if the scanner is not at the beginning
62 * of a file.
63 * This can be used to seek only to keys which come after the current position
64 * of the scanner.
65 * Consider the key stream of all the keys in the file,
66 * <code>k[0] .. k[n]</code>, where there are n keys in the file after
67 * current position of HFileScanner.
68 * The scanner will position itself between k[i] and k[i+1] where
69 * k[i] < key <= k[i+1].
70 * If there is no key k[i+1] greater than or equal to the input key, then the
71 * scanner will position itself at the end of the file and next() will return
72 * false when it is called.
73 * @param key Key to find (should be non-null)
74 * @return -1, if key < k[0], no position;
75 * 0, such that k[i] = key and scanner is left in position i; and
76 * 1, such that k[i] < key, and scanner is left in position i.
77 * @throws IOException
78 */
79 int reseekTo(byte[] key) throws IOException;
80 int reseekTo(byte[] key, int offset, int length) throws IOException;
81 /**
82 * Consider the key stream of all the keys in the file,
83 * <code>k[0] .. k[n]</code>, where there are n keys in the file.
84 * @param key Key to find
85 * @return false if key <= k[0] or true with scanner in position 'i' such
86 * that: k[i] < key. Furthermore: there may be a k[i+1], such that
87 * k[i] < key <= k[i+1] but there may also NOT be a k[i+1], and next() will
88 * return false (EOF).
89 * @throws IOException
90 */
91 boolean seekBefore(byte[] key) throws IOException;
92 boolean seekBefore(byte[] key, int offset, int length) throws IOException;
93 /**
94 * Positions this scanner at the start of the file.
95 * @return False if empty file; i.e. a call to next would return false and
96 * the current key and value are undefined.
97 * @throws IOException
98 */
99 boolean seekTo() throws IOException;
100 /**
101 * Scans to the next entry in the file.
102 * @return Returns false if you are at the end otherwise true if more in file.
103 * @throws IOException
104 */
105 boolean next() throws IOException;
106 /**
107 * Gets a buffer view to the current key. You must call
108 * {@link #seekTo(byte[])} before this method.
109 * @return byte buffer for the key. The limit is set to the key size, and the
110 * position is 0, the start of the buffer view.
111 */
112 ByteBuffer getKey();
113 /**
114 * Gets a buffer view to the current value. You must call
115 * {@link #seekTo(byte[])} before this method.
116 *
117 * @return byte buffer for the value. The limit is set to the value size, and
118 * the position is 0, the start of the buffer view.
119 */
120 ByteBuffer getValue();
121 /**
122 * @return Instance of {@link KeyValue}.
123 */
124 KeyValue getKeyValue();
125 /**
126 * Convenience method to get a copy of the key as a string - interpreting the
127 * bytes as UTF8. You must call {@link #seekTo(byte[])} before this method.
128 * @return key as a string
129 */
130 String getKeyString();
131 /**
132 * Convenience method to get a copy of the value as a string - interpreting
133 * the bytes as UTF8. You must call {@link #seekTo(byte[])} before this method.
134 * @return value as a string
135 */
136 String getValueString();
137 /**
138 * @return Reader that underlies this Scanner instance.
139 */
140 HFile.Reader getReader();
141 /**
142 * @return True is scanner has had one of the seek calls invoked; i.e.
143 * {@link #seekBefore(byte[])} or {@link #seekTo()} or {@link #seekTo(byte[])}.
144 * Otherwise returns false.
145 */
146 boolean isSeeked();
147 }