Skip to content

Commit 21df157

Browse files
1.4.0 Scan to return SortedMap, Key now implements Comparable
1 parent 7cef5f0 commit 21df157

File tree

8 files changed

+572
-58
lines changed

8 files changed

+572
-58
lines changed

core/src/main/java/com/bettercloud/bigtable/orm/BigTableEntityDao.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
import org.apache.hadoop.hbase.client.ResultScanner;
1010
import org.apache.hadoop.hbase.client.Scan;
1111
import org.apache.hadoop.hbase.client.Table;
12+
import org.apache.hadoop.hbase.filter.BinaryComparator;
13+
import org.apache.hadoop.hbase.filter.CompareFilter;
14+
import org.apache.hadoop.hbase.filter.Filter;
15+
import org.apache.hadoop.hbase.filter.FilterList;
1216
import org.apache.hadoop.hbase.filter.PageFilter;
17+
import org.apache.hadoop.hbase.filter.RowFilter;
1318
import org.apache.hadoop.hbase.util.Bytes;
1419

20+
import javax.annotation.Nullable;
1521
import java.io.IOException;
1622
import java.time.Instant;
1723
import java.util.ArrayList;
@@ -22,6 +28,8 @@
2228
import java.util.Objects;
2329
import java.util.Optional;
2430
import java.util.Set;
31+
import java.util.SortedMap;
32+
import java.util.TreeMap;
2533
import java.util.function.Function;
2634
import java.util.function.Supplier;
2735
import java.util.stream.Collectors;
@@ -137,8 +145,10 @@ public <K extends Key<T>> Map<K, T> getAll(final Set<K> keys) throws IOException
137145
}
138146

139147
/**
148+
* Utility method for running scan without a provided constant.
149+
*
140150
* Runs a paging table scan from the provided starting key to the provided ending key,
141-
* and returns a list of entities in the order returned from BigTable.
151+
* and returns a list of paired Key/Value entities in the order returned from BigTable.
142152
*
143153
* Use the last returned entity to construct a new starting key for subsequent paging requests until no values are returned.
144154
*
@@ -151,31 +161,63 @@ public <K extends Key<T>> Map<K, T> getAll(final Set<K> keys) throws IOException
151161
* @throws IOException when an error occurs while communicating with BigTable
152162
*/
153163
@Override
154-
public <K extends Key<T>> List<T> scan(final K startKey,
155-
final boolean startKeyInclusive,
156-
final K endKey,
157-
final boolean endKeyInclusive,
158-
final int numRows) throws IOException {
164+
public <K extends Key<T>> SortedMap<Key<T>, T> scan(final K startKey,
165+
final boolean startKeyInclusive,
166+
final K endKey,
167+
final boolean endKeyInclusive,
168+
final int numRows) throws IOException {
169+
return scan(startKey, startKeyInclusive, endKey, endKeyInclusive, numRows, null);
170+
}
171+
172+
/**
173+
* Runs a paging table scan from the provided starting key to the provided ending key,
174+
* and returns a list of paired Key/Value entities in the order returned from BigTable.
175+
*
176+
* Use the last returned entity to construct a new starting key for subsequent paging requests until no values are returned.
177+
*
178+
* @param startKey key to start scanning from (does not have to have an existing record at the location)
179+
* @param startKeyInclusive whether to include result from startKey
180+
* @param endKey key to end scanning on (does not have to have an existing record at the location)
181+
* @param endKeyInclusive whether to include result from endKey
182+
* @param numRows max number of entries to return
183+
* @param constant optional field to be used to be included, should be the constant provided to KeyComponent if it exists
184+
* @return A list of entities in the order that they are stored in BigTable
185+
* @throws IOException when an error occurs while communicating with BigTable
186+
*/
187+
@Override
188+
public <K extends Key<T>> SortedMap<Key<T>, T> scan(final K startKey,
189+
final boolean startKeyInclusive,
190+
final K endKey,
191+
final boolean endKeyInclusive,
192+
final int numRows,
193+
@Nullable final String constant) throws IOException {
159194
Objects.requireNonNull(startKey);
160195
Objects.requireNonNull(endKey);
161196

197+
List<Filter> filters = new ArrayList<>();
198+
filters.add(new PageFilter(numRows));
199+
if (Objects.nonNull(constant) && !"".equals(constant)) {
200+
filters.add(new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(constant.getBytes())));
201+
}
202+
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, filters);
203+
162204
final Scan scan = new Scan();
163-
scan.setFilter(new PageFilter(numRows));
205+
scan.setFilter(filterList);
164206
scan.withStartRow(startKey.toBytes(), startKeyInclusive);
165207
scan.withStopRow(endKey.toBytes(), endKeyInclusive);
166208

167209
final ResultScanner scanner = table.getScanner(scan);
168-
final List<T> results = new ArrayList<>();
210+
final SortedMap<Key<T>, T> results = new TreeMap<>();
169211

170212
Result result;
171213
while ((result = scanner.next()) != null) {
172214
if (!result.isEmpty()) {
173215
final T entity = delegateToEntity(result);
174-
results.add(entity);
216+
results.put(new RawKey<T>(result.getRow()), entity);
175217
}
176218
}
177219

178-
return Collections.unmodifiableList(results);
220+
return Collections.unmodifiableSortedMap(results);
179221
}
180222

181223
/**

core/src/main/java/com/bettercloud/bigtable/orm/Dao.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.bettercloud.bigtable.orm;
22

33
import java.io.IOException;
4-
import java.util.List;
54
import java.util.Map;
65
import java.util.Optional;
76
import java.util.Set;
7+
import java.util.SortedMap;
88

99
public interface Dao<T extends Entity> {
1010

@@ -13,11 +13,19 @@ public interface Dao<T extends Entity> {
1313

1414
<K extends Key<T>> Map<K, T> getAll(final Set<K> keys) throws IOException;
1515

16-
<K extends Key<T>> List<T> scan(final K startKey,
17-
final boolean startKeyInclusive,
18-
final K endKey,
19-
final boolean endKeyInclusive,
20-
final int numRows) throws IOException;
16+
<K extends Key<T>> SortedMap<Key<T>, T> scan(final K startKey,
17+
final boolean startKeyInclusive,
18+
final K endKey,
19+
final boolean endKeyInclusive,
20+
final int numRows) throws IOException;
21+
22+
<K extends Key<T>> SortedMap<Key<T>, T> scan(final K startKey,
23+
final boolean startKeyInclusive,
24+
final K endKey,
25+
final boolean endKeyInclusive,
26+
final int numRows,
27+
final String constant) throws IOException;
28+
2129
@Deprecated
2230
<K extends Key<T>> T save(final K key, final T entity) throws IOException;
2331

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bettercloud.bigtable.orm;
22

3-
public interface Key<T extends Entity> {
3+
public interface Key<T extends Entity> extends Comparable<Key> {
44

55
byte[] toBytes();
66
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bettercloud.bigtable.orm;
2+
3+
import java.nio.ByteBuffer;
4+
import java.util.Arrays;
5+
6+
public class RawKey<T extends Entity> implements Key<T> {
7+
8+
private final byte[] key;
9+
10+
public RawKey(byte[] key) {
11+
this.key = Arrays.copyOf(key, key.length);
12+
}
13+
14+
@Override
15+
public byte[] toBytes() {
16+
return key;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
if (this == o) return true;
22+
if (o == null || getClass() != o.getClass()) return false;
23+
RawKey<?> rawKey = (RawKey<?>) o;
24+
return Arrays.equals(key, rawKey.key);
25+
}
26+
27+
@Override
28+
public int hashCode() {
29+
return Arrays.hashCode(key);
30+
}
31+
32+
@Override
33+
public int compareTo(Key otherKey) {
34+
return ByteBuffer.wrap(this.toBytes()).compareTo(ByteBuffer.wrap(otherKey.toBytes()));
35+
}
36+
}

core/src/main/java/com/bettercloud/bigtable/orm/StringKey.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.hadoop.hbase.util.Bytes;
44

5+
import java.nio.ByteBuffer;
56
import java.util.Objects;
67

78
public class StringKey<T extends Entity> implements Key<T> {
@@ -42,4 +43,9 @@ public int hashCode() {
4243
public String toString() {
4344
return keyString;
4445
}
46+
47+
@Override
48+
public int compareTo(Key otherKey) {
49+
return ByteBuffer.wrap(this.toBytes()).compareTo(ByteBuffer.wrap(otherKey.toBytes()));
50+
}
4551
}

0 commit comments

Comments
 (0)