Skip to content

Commit

Permalink
convenience methods for deleteRange added
Browse files Browse the repository at this point in the history
  • Loading branch information
subes committed Feb 15, 2015
1 parent a582b86 commit c92c7b1
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 3 deletions.
43 changes: 42 additions & 1 deletion ezdb-api/src/main/java/ezdb/RangeTable.java
Expand Up @@ -209,12 +209,53 @@ public TableIterator<H, R, V> rangeReverse(H hashKey, R fromRangeKey,
*/
public void delete(H hashKey, R rangeKey);

/**
* Deletes all rows with a given hash key.
*
* @param hashKey
* A key used group rows together.
*/
public void deleteRange(H hashKey);

/**
* Deletes all rows with a given hash key, and a range key that's greater than
* or equal to the "from" range key provided.
*
* @param hashKey
* A key used group rows together.
* @param fromRangeKey
* The range key to use as the starting point. If an exact match
* doesn't exist for this hash/range pair, the nearest range key
* that is larger than this range key will be used as the
* starting point.
*/
public void deleteRange(H hashKey, R fromRangeKey);

/**
* Deletes all rows with a given hash key, and a range key that's greater than
* or equal to the "from" range key, and less than or equal to the "to"
* range key. That is, the "from" range key is inclusive, and the "to" range
* key is inclusive.
*
* @param hashKey
* A key used group rows together.
* @param fromRangeKey
* The range key to use as the starting point. If an exact match
* doesn't exist for this hash/range pair, the nearest range key
* that is larger than this range key will be used as the
* starting point.
* @param toRangeKey
* The range key to use as the end point (inclusive). If an exact
* match doesn't exist, the nearest range key that is less than
* this range key will be used as the end point (inclusive).
*/
public void deleteRange(H hashKey, R fromRangeKey, R toRangeKey);

/**
* With this it is possible to do bulk/batch puts and deletes.
*
* @return a new batch enabled transaction object
*/
public RangeBatch<H, R, V> newRangeBatch();


}
36 changes: 36 additions & 0 deletions ezdb-leveldb/src/main/java/ezdb/leveldb/EzLevelDbTable.java
Expand Up @@ -742,5 +742,41 @@ public Batch<H, V> newBatch() {
public RangeBatch<H, R, V> newRangeBatch() {
return new EzLevelDbBatch<H, R, V>(db, hashKeySerde, rangeKeySerde, valueSerde);
}

@Override
public void deleteRange(H hashKey) {
TableIterator<H, R, V> range = range(hashKey);
internalDeleteRange(range);
}

@Override
public void deleteRange(H hashKey, R fromRangeKey) {
TableIterator<H, R, V> range = range(hashKey, fromRangeKey);
internalDeleteRange(range);
}


@Override
public void deleteRange(H hashKey, R fromRangeKey, R toRangeKey) {
TableIterator<H, R, V> range = range(hashKey, fromRangeKey, toRangeKey);
internalDeleteRange(range);
}

private void internalDeleteRange(TableIterator<H, R, V> range) {
RangeBatch<H, R, V> batch = newRangeBatch();
try {
while (range.hasNext()) {
TableRow<H, R, V> next = range.next();
batch.delete(next.getHashKey(), next.getRangeKey());
}
batch.flush();
} finally {
try {
batch.close();
} catch (IOException e) {
throw new DbException(e);
}
}
}

}
40 changes: 38 additions & 2 deletions ezdb-rocksdb-jni/src/main/java/ezdb/rocksdb/EzRocksDbTable.java
Expand Up @@ -45,11 +45,11 @@ public EzRocksDbTable(File path, EzRocksDbFactory factory,
this.rangeKeyComparator = rangeKeyComparator;

this.options = new Options();

options.setCreateIfMissing(true);
options.setComparator(new EzRocksDbComparator(hashKeyComparator,
rangeKeyComparator));

try {
this.db = factory.open(path, options);
} catch (IOException e) {
Expand Down Expand Up @@ -762,4 +762,40 @@ public RangeBatch<H, R, V> newRangeBatch() {
valueSerde);
}

@Override
public void deleteRange(H hashKey) {
TableIterator<H, R, V> range = range(hashKey);
internalDeleteRange(range);
}

@Override
public void deleteRange(H hashKey, R fromRangeKey) {
TableIterator<H, R, V> range = range(hashKey, fromRangeKey);
internalDeleteRange(range);
}


@Override
public void deleteRange(H hashKey, R fromRangeKey, R toRangeKey) {
TableIterator<H, R, V> range = range(hashKey, fromRangeKey, toRangeKey);
internalDeleteRange(range);
}

private void internalDeleteRange(TableIterator<H, R, V> range) {
RangeBatch<H, R, V> batch = newRangeBatch();
try {
while (range.hasNext()) {
TableRow<H, R, V> next = range.next();
batch.delete(next.getHashKey(), next.getRangeKey());
}
batch.flush();
} finally {
try {
batch.close();
} catch (IOException e) {
throw new DbException(e);
}
}
}

}
27 changes: 27 additions & 0 deletions ezdb-treemap/src/main/java/ezdb/treemap/TreeMapTable.java
Expand Up @@ -275,5 +275,32 @@ public Batch<H, V> newBatch() {
public RangeBatch<H, R, V> newRangeBatch() {
throw new UnsupportedOperationException();
}

@Override
public void deleteRange(H hashKey) {
TableIterator<H, R, V> range = range(hashKey);
while(range.hasNext()){
TableRow<H, R, V> next = range.next();
delete(next.getHashKey(), next.getRangeKey());
}
}

@Override
public void deleteRange(H hashKey, R fromRangeKey) {
TableIterator<H, R, V> range = range(hashKey, fromRangeKey);
while(range.hasNext()){
TableRow<H, R, V> next = range.next();
delete(next.getHashKey(), next.getRangeKey());
}
}

@Override
public void deleteRange(H hashKey, R fromRangeKey, R toRangeKey) {
TableIterator<H, R, V> range = range(hashKey, fromRangeKey, toRangeKey);
while(range.hasNext()){
TableRow<H, R, V> next = range.next();
delete(next.getHashKey(), next.getRangeKey());
}
}

}

0 comments on commit c92c7b1

Please sign in to comment.