Skip to content

Commit

Permalink
avoid deleteRange() as possible for performance (#1375)
Browse files Browse the repository at this point in the history
Change-Id: I1667e062b8a6a99c52d534a314a0af4bfecb59a0
  • Loading branch information
javeme committed Mar 29, 2021
1 parent 1da7036 commit a495eea
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ public static abstract class Session extends AbstractBackendSession {
public abstract void merge(String table, byte[] key, byte[] value);
public abstract void increase(String table, byte[] key, byte[] value);

public abstract void remove(String table, byte[] key);
public abstract void delete(String table, byte[] keyFrom, byte[] keyTo);
public abstract void delete(String table, byte[] key);
public abstract void deleteSingle(String table, byte[] key);
public abstract void deletePrefix(String table, byte[] key);
public abstract void deleteRange(String table,
byte[] keyFrom, byte[] keyTo);

public abstract byte[] get(String table, byte[] key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,20 @@ public void increase(String table, byte[] key, byte[] value) {
* Delete a record by key from a table
*/
@Override
public void remove(String table, byte[] key) {
public void delete(String table, byte[] key) {
try (CFHandle cf = cf(table)) {
this.batch.delete(cf.get(), key);
} catch (RocksDBException e) {
throw new BackendException(e);
}
}

/**
* Delete the only one version of a record by key from a table
* NOTE: requires that the key exists and was not overwritten.
*/
@Override
public void deleteSingle(String table, byte[] key) {
try (CFHandle cf = cf(table)) {
this.batch.singleDelete(cf.get(), key);
} catch (RocksDBException e) {
Expand All @@ -852,7 +865,7 @@ public void remove(String table, byte[] key) {
* Delete a record by key(or prefix with key) from a table
*/
@Override
public void delete(String table, byte[] key) {
public void deletePrefix(String table, byte[] key) {
byte[] keyFrom = key;
byte[] keyTo = Arrays.copyOf(key, key.length);
keyTo = BinarySerializer.increaseOne(keyTo);
Expand All @@ -867,7 +880,7 @@ public void delete(String table, byte[] key) {
* Delete a range of keys from a table
*/
@Override
public void delete(String table, byte[] keyFrom, byte[] keyTo) {
public void deleteRange(String table, byte[] keyFrom, byte[] keyTo) {
try (CFHandle cf = cf(table)) {
this.batch.deleteRange(cf.get(), keyFrom, keyTo);
} catch (RocksDBException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void delete(Session session, BackendEntry entry) {
} else {
for (BackendColumn col : entry.columns()) {
assert entry.belongToMe(col) : entry;
session.remove(this.table(), col.name);
session.delete(this.table(), col.name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,20 @@ private static long l(byte[] bytes) {
}
}

public static class VertexLabel extends RocksDBTable {
public static class SchemaTable extends RocksDBTable {

public SchemaTable(String database, String table) {
super(database, table);
}

@Override
public void delete(Session session, BackendEntry entry) {
assert entry.columns().isEmpty();
session.deletePrefix(this.table(), entry.id().asBytes());
}
}

public static class VertexLabel extends SchemaTable {

public static final String TABLE = HugeType.VERTEX_LABEL.string();

Expand All @@ -84,7 +97,7 @@ public VertexLabel(String database) {
}
}

public static class EdgeLabel extends RocksDBTable {
public static class EdgeLabel extends SchemaTable {

public static final String TABLE = HugeType.EDGE_LABEL.string();

Expand All @@ -93,7 +106,7 @@ public EdgeLabel(String database) {
}
}

public static class PropertyKey extends RocksDBTable {
public static class PropertyKey extends SchemaTable {

public static final String TABLE = HugeType.PROPERTY_KEY.string();

Expand All @@ -102,7 +115,7 @@ public PropertyKey(String database) {
}
}

public static class IndexLabel extends RocksDBTable {
public static class IndexLabel extends SchemaTable {

public static final String TABLE = HugeType.INDEX_LABEL.string();

Expand Down Expand Up @@ -168,7 +181,7 @@ public void delete(Session session, BackendEntry entry) {
*/
for (BackendEntry.BackendColumn column : entry.columns()) {
// Don't assert entry.belongToMe(column), length-prefix is 1*
session.delete(this.table(), column.name);
session.deletePrefix(this.table(), column.name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,24 +325,33 @@ public void increase(String table, byte[] key, byte[] value) {
* Delete a record by key from a table
*/
@Override
public void remove(String table, byte[] key) {
throw new NotSupportException("RocksDBSstStore remove()");
public void delete(String table, byte[] key) {
throw new NotSupportException("RocksDBSstStore delete()");
}

/**
* Delete the only one version of a record by key from a table
* NOTE: requires that the key exists and was not overwritten.
*/
@Override
public void deleteSingle(String table, byte[] key) {
throw new NotSupportException("RocksDBSstStore deleteSingle()");
}

/**
* Delete a record by key(or prefix with key) from a table
*/
@Override
public void delete(String table, byte[] key) {
throw new NotSupportException("RocksDBSstStore delete()");
public void deletePrefix(String table, byte[] key) {
throw new NotSupportException("RocksDBSstStore deletePrefix()");
}

/**
* Delete a range of keys from a table
*/
@Override
public void delete(String table, byte[] keyFrom, byte[] keyTo) {
throw new NotSupportException("RocksDBSstStore delete()");
public void deleteRange(String table, byte[] keyFrom, byte[] keyTo) {
throw new NotSupportException("RocksDBSstStore deleteRange()");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ protected String get(String key) throws RocksDBException {

protected void clearData() throws RocksDBException {
for (String table : new ArrayList<>(this.rocks.openedTables())) {
this.rocks.session().delete(table, new byte[]{0}, new byte[]{-1});
this.rocks.session().deleteRange(table,
new byte[]{0}, new byte[]{-1});
}
this.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void testUpdate() throws RocksDBException {
for (int i = 0; i < n; i++) {
int value = comms.get(i);
String old = String.format("index:%3d:%d", i, value);
session.remove(TABLE, b(old));
session.delete(TABLE, b(old));

value = r.nextInt(n); // TODO: aggregate
value = i + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void testDeleteByKey() throws RocksDBException {
Assert.assertEquals("19", get("person:1gage"));
Assert.assertEquals("Beijing", get("person:1gcity"));

this.rocks.session().remove(TABLE, b("person:1gage"));
this.rocks.session().delete(TABLE, b("person:1gage"));
this.commit();

Assert.assertEquals("James", get("person:1gname"));
Expand All @@ -303,7 +303,7 @@ public void testDeleteByKeyButNotExist() throws RocksDBException {
Assert.assertEquals("19", get("person:1gage"));
Assert.assertEquals("Beijing", get("person:1gcity"));

this.rocks.session().remove(TABLE, b("person:1"));
this.rocks.session().delete(TABLE, b("person:1"));
this.commit();

Assert.assertEquals("James", get("person:1gname"));
Expand All @@ -325,7 +325,7 @@ public void testDeleteByPrefix() throws RocksDBException {
Assert.assertEquals("19", get("person:1gage"));
Assert.assertEquals("Beijing", get("person:1gcity"));

this.rocks.session().delete(TABLE, b("person:1"));
this.rocks.session().deletePrefix(TABLE, b("person:1"));
this.commit();

Assert.assertEquals(null, get("person:1gname"));
Expand Down Expand Up @@ -353,7 +353,7 @@ public void testDeleteByRange() throws RocksDBException {
Assert.assertEquals("Lisa", get("person:2gname"));
Assert.assertEquals("Hebe", get("person:3gname"));

this.rocks.session().delete(TABLE, b("person:1"), b("person:3"));
this.rocks.session().deleteRange(TABLE, b("person:1"), b("person:3"));
this.commit();

Assert.assertEquals(null, get("person:1gname"));
Expand Down Expand Up @@ -385,7 +385,7 @@ public void testDeleteByRangeWithBytes() throws RocksDBException {
byte[] value21 = b("value-2-1");
session.put(TABLE, key21, value21);

session.delete(TABLE, key11, new byte[]{1, 3});
session.deleteRange(TABLE, key11, new byte[]{1, 3});
this.commit();

Assert.assertArrayEquals(null, session.get(TABLE, key11));
Expand All @@ -409,14 +409,14 @@ public void testDeleteByRangeWithSignedBytes() throws RocksDBException {
byte[] value21 = b("value-2-1");
session.put(TABLE, key21, value21);

session.delete(TABLE, new byte[]{1, -3}, new byte[]{1, 3});
session.deleteRange(TABLE, new byte[]{1, -3}, new byte[]{1, 3});
this.commit();

Assert.assertArrayEquals(value11, session.get(TABLE, key11));
Assert.assertArrayEquals(value12, session.get(TABLE, key12));
Assert.assertArrayEquals(value21, session.get(TABLE, key21));

session.delete(TABLE, new byte[]{1, 1}, new byte[]{1, -1});
session.deleteRange(TABLE, new byte[]{1, 1}, new byte[]{1, -1});
this.commit();

Assert.assertArrayEquals(null, session.get(TABLE, key11));
Expand Down Expand Up @@ -448,7 +448,8 @@ public void testDeleteByRangeWithMinMaxByteValue() throws RocksDBException {
byte[] value20 = b("value-2-0");
session.put(TABLE, key20, value20);

session.delete(TABLE, new byte[]{1, 0}, new byte[]{1, (byte) 0xff});
session.deleteRange(TABLE,
new byte[]{1, 0}, new byte[]{1, (byte) 0xff});
this.commit();

Assert.assertArrayEquals(null, session.get(TABLE, key11));
Expand All @@ -457,7 +458,8 @@ public void testDeleteByRangeWithMinMaxByteValue() throws RocksDBException {
Assert.assertArrayEquals(value14, session.get(TABLE, key14));
Assert.assertArrayEquals(value20, session.get(TABLE, key20));

session.delete(TABLE, new byte[]{1, (byte) 0xff}, new byte[]{2, 0});
session.deleteRange(TABLE,
new byte[]{1, (byte) 0xff}, new byte[]{2, 0});
this.commit();

Assert.assertArrayEquals(null, session.get(TABLE, key11));
Expand Down

0 comments on commit a495eea

Please sign in to comment.