Skip to content

Commit

Permalink
fixup! properties: upgrade HBaseVertex to support multi-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Rappazzo committed Mar 7, 2019
1 parent 3847afe commit 0f11dfb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
6 changes: 1 addition & 5 deletions src/main/java/io/hgraphdb/HBaseEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ public Set<String> keys() {
return Collections.unmodifiableSet(properties.keySet());
}

@Override
public boolean removeProperty(String key) {
public void removeProperty(String key) {
Object value = getProperty(key);
if (value != null) {
// delete from index model before removing property
Expand All @@ -188,10 +187,7 @@ public boolean removeProperty(String key) {

Mutator writer = getModel().clearProperty(this, key);
Mutators.write(getTable(), writer);

return true;
}
return false;
}

@Override
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/hgraphdb/HBaseElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public void load() {

public abstract boolean hasProperty(String key);

public abstract boolean removeProperty(String key);

public abstract Stream<Map.Entry<String, Object>> propertyEntriesStream();

public abstract int propertySize();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/hgraphdb/HBaseProperty.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.hgraphdb;

import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
Expand All @@ -26,7 +27,11 @@ public Element element() {

@Override
public void remove() {
element.removeProperty(this.key);
if (this.element instanceof Edge) {
((HBaseEdge)this.element).removeProperty(this.key);
} else {
((HBaseVertex)this.element).removeProperty(this.key, this.value);
}
}

@Override
Expand Down
50 changes: 41 additions & 9 deletions src/main/java/io/hgraphdb/HBaseVertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,26 @@ public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinali
}

//this will mutate the graph
final Optional<VertexProperty<V>> existingProperty = ElementHelper.stageVertexProperty(this, cardinality, key, value, keyValues);
if (existingProperty.isPresent()) {
return existingProperty
.map(v -> v instanceof HBaseVertexProperty ? v : new HBaseVertexProperty<>(graph, this, v.key(), v.value()))
.get();
VertexProperty<V> existingProperty = null;
if (cardinality.equals(VertexProperty.Cardinality.single)) {
this.properties.remove(key);
} else if (cardinality.equals(VertexProperty.Cardinality.set)) {
Iterator<VertexProperty<V>> itty = this.properties(key);
while (itty.hasNext()) {
final VertexProperty<V> property = itty.next();
if (property.value().equals(value)) {
ElementHelper.attachProperties(property, keyValues);
existingProperty = property;
break;
}
}
}
if (existingProperty != null) {
if (existingProperty instanceof HBaseVertexProperty) {
return existingProperty;
} else {
return new HBaseVertexProperty<>(graph, this, existingProperty.key(), existingProperty.value());
}
}

if (hasIndex) {
Expand Down Expand Up @@ -263,10 +278,27 @@ public boolean hasProperty(String key) {
return !getProperties().getOrDefault(key, Collections.emptyList()).isEmpty();
}

@Override
public boolean removeProperty(String key) {
Collection<?> c = getProperties().remove(key);
return c != null && !c.isEmpty();
public boolean removeProperty(String key, Object value) {
boolean removed = false;
if (null != this.properties && this.properties.containsKey(key)) {
Collection<Object> values = this.properties.get(key);
removed = values.remove(value);
if (removed) {
updatedAt(System.currentTimeMillis());
Mutator writer;
if (values.isEmpty()) {
boolean hasIndex = hasIndex(OperationType.WRITE, key);
deleteFromIndexModel(key, null);
//clear
writer = getModel().clearProperty(this, key);
} else {
//cardinality is non-single
writer = getModel().writeProperty(this, key, preparePropertyWriteValue(VertexProperty.Cardinality.list, value, values));
}
Mutators.write(getTable(), writer);
}
}
return removed;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/hgraphdb/HBaseVertexProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public <U> Property<U> property(final String key, final U value) {

@Override
public void remove() {
vertex.removeProperty(this.key);
((HBaseVertex)vertex).removeProperty(this.key, this.value);
}

@Override
Expand Down

0 comments on commit 0f11dfb

Please sign in to comment.