-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support integer key offset using a PartitionFilter
- Loading branch information
Showing
4 changed files
with
97 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/com/aerospike/jdbc/scan/PartitionScanHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.aerospike.jdbc.scan; | ||
|
||
import com.aerospike.client.IAerospikeClient; | ||
import com.aerospike.client.Key; | ||
import com.aerospike.client.ScanCallback; | ||
import com.aerospike.client.cluster.Node; | ||
import com.aerospike.client.cluster.Partition; | ||
import com.aerospike.client.policy.ScanPolicy; | ||
import com.aerospike.client.query.PartitionFilter; | ||
import com.aerospike.jdbc.model.AerospikeQuery; | ||
|
||
import java.util.Objects; | ||
import java.util.logging.Logger; | ||
|
||
public class PartitionScanHandler { | ||
|
||
private static final Logger logger = Logger.getLogger(PartitionScanHandler.class.getName()); | ||
|
||
private final IAerospikeClient client; | ||
|
||
private ScanRecordSequenceListener listener; | ||
|
||
private int currentPartition; | ||
private int count; | ||
|
||
public PartitionScanHandler(IAerospikeClient client) { | ||
this.client = client; | ||
this.listener = new ScanRecordSequenceListener(); | ||
} | ||
|
||
public RecordSet scanPartition(ScanPolicy scanPolicy, AerospikeQuery query) { | ||
if (Objects.nonNull(query.getOffset())) { | ||
long maxRecords = scanPolicy.maxRecords; | ||
PartitionFilter filter = getPartitionFilter(query); | ||
while (isScanRequired(maxRecords)) { | ||
client.scanPartitions(scanPolicy, filter, query.getSchema(), query.getTable(), | ||
callback, query.getBinNames()); | ||
scanPolicy.maxRecords = maxRecords > 0 ? maxRecords - count : maxRecords; | ||
filter = PartitionFilter.id(++currentPartition); | ||
} | ||
listener.onSuccess(); | ||
} else { | ||
logger.info("scanAll"); | ||
client.scanAll(null, listener, scanPolicy, query.getSchema(), | ||
query.getTable(), query.getBinNames()); | ||
} | ||
return listener.getRecordSet(); | ||
} | ||
|
||
private PartitionFilter getPartitionFilter(AerospikeQuery query) { | ||
Key key = new Key(query.getSchema(), query.getTable(), query.getOffset()); | ||
currentPartition = Partition.getPartitionId(key.digest); | ||
return PartitionFilter.after(key); | ||
} | ||
|
||
private boolean isScanRequired(final long maxRecords) { | ||
return (maxRecords == 0 || count < maxRecords) && isValidPartition(); | ||
} | ||
|
||
private boolean isValidPartition() { | ||
return currentPartition >= 0 && currentPartition < Node.PARTITIONS; | ||
} | ||
|
||
private final ScanCallback callback = ((key, record) -> { | ||
listener.onRecord(key, record); | ||
count++; | ||
}); | ||
|
||
public static PartitionScanHandler create(IAerospikeClient client) { | ||
return new PartitionScanHandler(client); | ||
} | ||
} |