Skip to content

Commit

Permalink
optimize: don't do remove-index if it's CREATING status (#465)
Browse files Browse the repository at this point in the history
about 10% performance improvement with tinkerpop tests.
also fix bug: delete-index-by-label deosn't work in InMemory backend.

Change-Id: Ie2cab7aef7e6e992d9dc3e3814eddf4adbc9f1bd
  • Loading branch information
javeme authored and zhoney committed Apr 19, 2019
1 parent 2890c96 commit 2418d51
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.page.PageState;
import com.baidu.hugegraph.backend.store.Shard;
import com.baidu.hugegraph.iterator.Metadatable;
import com.baidu.hugegraph.schema.EdgeLabel;
import com.baidu.hugegraph.schema.IndexLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.store.Shard;
import com.baidu.hugegraph.schema.EdgeLabel;
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.schema.PropertyKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public BackendEntry writeIndex(HugeIndex index) {
* meaningful for deletion of index data in secondary/range index.
*/
if (index.fieldValues() == null && index.elementIds().size() == 0) {
entry.column(formatSyspropName(HugeKeys.INDEX_LABEL_ID),
entry.column(HugeKeys.INDEX_LABEL_ID,
writeId(index.indexLabel()));
} else {
// TODO: field-values may be a number (range index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentSkipListMap;

import com.baidu.hugegraph.backend.id.EdgeId;
Expand Down Expand Up @@ -358,7 +360,8 @@ protected SecondaryIndex(HugeType type) {
}

@Override
public Iterator<BackendEntry> query(BackendSession session, Query query) {
public Iterator<BackendEntry> query(BackendSession session,
Query query) {
Set<Condition> conditions = query.conditions();
E.checkState(query instanceof ConditionQuery &&
conditions.size() == 2,
Expand Down Expand Up @@ -387,6 +390,23 @@ public Iterator<BackendEntry> query(BackendSession session, Query query) {
q.limit(query.limit());
return super.query(session, q);
}

@Override
public void delete(BackendSession session, TextBackendEntry entry) {
// Delete by index label
assert entry.columnsSize() == 1;
String indexLabel = entry.column(HugeKeys.INDEX_LABEL_ID);
E.checkState(indexLabel != null, "Expect index label");

Iterator<Entry<Id, BackendEntry>> iter;
for (iter = this.store().entrySet().iterator(); iter.hasNext();) {
Entry<Id, BackendEntry> e = iter.next();
// Delete if prefix with index label
if (e.getKey().asString().startsWith(indexLabel)) {
iter.remove();
}
}
}
}

public static class SearchIndex extends SecondaryIndex {
Expand All @@ -408,7 +428,8 @@ protected NavigableMap<Id, BackendEntry> store() {
}

@Override
public Iterator<BackendEntry> query(BackendSession session, Query query) {
public Iterator<BackendEntry> query(BackendSession session,
Query query) {
Set<Condition> conditions = query.conditions();
E.checkState(query instanceof ConditionQuery &&
(conditions.size() == 3 || conditions.size() == 2),
Expand Down Expand Up @@ -559,5 +580,28 @@ private Iterator<BackendEntry> betweenQuery(Id indexLabelId,
}
return results.values().iterator();
}

@Override
public void delete(BackendSession session, TextBackendEntry entry) {
// Delete by index label
assert entry.columnsSize() == 1;
String indexLabel = entry.column(HugeKeys.INDEX_LABEL_ID);
E.checkState(indexLabel != null, "Expect index label");

Id indexLabelId = IdGenerator.of(indexLabel);
Id min = HugeIndex.formatIndexId(HugeType.RANGE_INDEX,
indexLabelId, 0L);
indexLabelId = IdGenerator.of(indexLabelId.asLong() + 1L);
Id max = HugeIndex.formatIndexId(HugeType.RANGE_INDEX,
indexLabelId, 0L);
SortedMap<Id, BackendEntry> subStore;
subStore = this.store().subMap(min, max);
Iterator<Entry<Id, BackendEntry>> iter;
for (iter = subStore.entrySet().iterator(); iter.hasNext();) {
iter.next();
// Delete if prefix with index label
iter.remove();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void updateIndex(IndexLabel indexLabel, Object propValue,
* @return converted id query
*/
@Watched(prefix = "index")
public List<IdHolder> indexQuery(ConditionQuery query) {
public List<IdHolder> queryIndex(ConditionQuery query) {
// Index query must have been flattened in Graph tx
query.checkFlattened();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ private List<IdHolder> indexQuery(ConditionQuery query) {
*/
this.beforeRead();
try {
return this.indexTx.indexQuery(query);
return this.indexTx.queryIndex(query);
} finally {
this.afterRead();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void rebuildIndex(SchemaLabel label, Collection<Id> indexLabelIds) {
locks.lockWrites(LockUtil.INDEX_LABEL_DELETE, indexLabelIds);

Set<IndexLabel> ils = indexLabelIds.stream()
.map(schemaTx::getIndexLabel)
.map(this.graph()::indexLabel)
.collect(Collectors.toSet());
for (IndexLabel il : ils) {
if (il.status() == SchemaStatus.CREATING) {
Expand Down Expand Up @@ -147,15 +147,15 @@ private void removeIndex(Collection<Id> indexLabelIds) {
GraphTransaction graphTx = this.graph().graphTransaction();

for (Id id : indexLabelIds) {
IndexLabel indexLabel = schemaTx.getIndexLabel(id);
if (indexLabel == null) {
IndexLabel il = schemaTx.getIndexLabel(id);
if (il == null || il.status() == SchemaStatus.CREATING) {
/*
* TODO: How to deal with non-existent index name:
* continue or throw exception?
*/
continue;
}
graphTx.removeIndex(indexLabel);
graphTx.removeIndex(il);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ public void add(Node node) {
}

public long degree() {
E.checkArgument(degree > 0,
"The degree must be > 0, but got %s", degree);
E.checkArgument(this.degree > 0,
"The degree must be > 0, but got %s", this.degree);
return this.degree;
}

Expand All @@ -290,6 +290,8 @@ public void degree(long degree) {

private static class Ranks extends OrderLimitMap<Id, Double> {

private static final long serialVersionUID = 2041529946017356029L;

public Ranks(int capacity) {
super(capacity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private Map<Id, Double> calcNewRanks(Set<Id> outSeeds, Set<Id> inSeeds,
newRanks.put(seed, oldRank);
continue;
}
double incrRank = oldRank * alpha / degree;
double incrRank = oldRank * this.alpha / degree;

// Collect all neighbors increment
for (Id neighbor : neighbors) {
Expand Down Expand Up @@ -159,11 +159,6 @@ private Directions getStartDirection(Id source, String label) {
}
}

private long degreeOfVertex(Id source, Directions dir, Id label) {
return IteratorUtils.count(this.edgesOfVertex(source, dir, label,
this.degree));
}

private static void removeAll(Map<Id, Double> map, Set<Id> keys) {
for (Id key : keys) {
map.remove(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.apache.hadoop.hbase.util.VersionInfo;

import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.serializer.BinarySerializer;
import com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn;
import com.baidu.hugegraph.backend.store.BackendEntry.BackendIterator;
import com.baidu.hugegraph.backend.store.BackendSession;
Expand Down

0 comments on commit 2418d51

Please sign in to comment.