Move setPropertyForIndex to IndexTableWrapper

This commit is contained in:
Michael Lieberman
2015-01-20 14:41:15 -05:00
parent 36ca8641ef
commit c66d86d123
6 changed files with 67 additions and 40 deletions

View File

@@ -86,7 +86,7 @@ public abstract class AccumuloElement implements Element {
@Override
public void setProperty(String key, Object value) {
makeCache();
globals.getGraph().setPropertyForIndexes(type, this, key, value);
globals.getIndexWrapper(type).setPropertyForIndex(this, key, value);
// MDL 31 Dec 2014: The above calls getProperty, so this
// order is important (for now).
globals.getElementWrapper(type).writeProperty(this, key, value);

View File

@@ -686,42 +686,6 @@ public class AccumuloGraph implements Graph, KeyIndexableGraph, IndexableGraph {
// methods used by AccumuloElement, AccumuloVertex, AccumuloEdge to interact
// with the backing Accumulo data store...
/**
* Sets the property. Requires a round-trip to Accumulo to see if the property exists
* iff the provided key has an index. Therefore, for best performance, if at
* all possible, create indices after bulk ingest.
*
* @deprecated Move to appropriate place
* @param type
* @param id
* @param key
* @param val
*/
@Deprecated
void setPropertyForIndexes(Class<? extends Element> type, Element element, String key, Object val) {
AccumuloGraphUtils.validateProperty(key, val);
try {
if (config.getAutoIndex() || getIndexedKeys(type).contains(key)) {
byte[] newByteVal = AccumuloByteSerializer.serialize(val);
BatchWriter bw = getIndexBatchWriter(type);
Object old = element.getProperty(key);
if (old != null) {
byte[] oldByteVal = AccumuloByteSerializer.serialize(old);
Mutation m = new Mutation(oldByteVal);
m.putDelete(key, element.getId().toString());
bw.addMutation(m);
}
Mutation m = new Mutation(newByteVal);
m.put(key.getBytes(), element.getId().toString().getBytes(), Constants.EMPTY);
bw.addMutation(m);
checkedFlush();
}
} catch (MutationsRejectedException e) {
e.printStackTrace();
}
}
private BatchWriter getIndexBatchWriter(Class<? extends Element> type) {
if (type.equals(Edge.class))
return getEdgeIndexWriter();

View File

@@ -26,6 +26,7 @@ import edu.jhuapl.tinkerpop.tables.EdgeIndexTableWrapper;
import edu.jhuapl.tinkerpop.tables.EdgeTableWrapper;
import edu.jhuapl.tinkerpop.tables.ElementTableWrapper;
import edu.jhuapl.tinkerpop.tables.IndexNameTableWrapper;
import edu.jhuapl.tinkerpop.tables.IndexTableWrapper;
import edu.jhuapl.tinkerpop.tables.IndexedKeysTableWrapper;
import edu.jhuapl.tinkerpop.tables.VertexIndexTableWrapper;
import edu.jhuapl.tinkerpop.tables.VertexTableWrapper;
@@ -108,6 +109,16 @@ public class GlobalInstances {
}
}
public <T extends Element> IndexTableWrapper getIndexWrapper(Class<T> clazz) {
if (Vertex.class.equals(clazz)) {
return vertexIndexWrapper;
} else if (Edge.class.equals(clazz)) {
return edgeIndexWrapper;
} else {
throw new AccumuloGraphException("Unrecognized class: "+clazz);
}
}
public ElementCaches getCaches() {
return caches;
}

View File

@@ -24,6 +24,7 @@ import edu.jhuapl.tinkerpop.GlobalInstances;
public class EdgeIndexTableWrapper extends IndexTableWrapper {
public EdgeIndexTableWrapper(GlobalInstances globals) {
super(globals, globals.getConfig().getEdgeKeyIndexTableName());
super(globals, Edge.class, globals.getConfig()
.getEdgeKeyIndexTableName());
}
}

View File

@@ -14,6 +14,16 @@
*/
package edu.jhuapl.tinkerpop.tables;
import org.apache.accumulo.core.client.BatchWriter;
import org.apache.accumulo.core.client.MutationsRejectedException;
import org.apache.accumulo.core.data.Mutation;
import com.tinkerpop.blueprints.Element;
import edu.jhuapl.tinkerpop.AccumuloByteSerializer;
import edu.jhuapl.tinkerpop.AccumuloGraphException;
import edu.jhuapl.tinkerpop.AccumuloGraphUtils;
import edu.jhuapl.tinkerpop.Constants;
import edu.jhuapl.tinkerpop.GlobalInstances;
/**
@@ -21,7 +31,47 @@ import edu.jhuapl.tinkerpop.GlobalInstances;
*/
public abstract class IndexTableWrapper extends BaseTableWrapper {
protected IndexTableWrapper(GlobalInstances globals, String tableName) {
protected final Class<? extends Element> elementType;
protected IndexTableWrapper(GlobalInstances globals,
Class<? extends Element> elementType, String tableName) {
super(globals, tableName);
this.elementType = elementType;
}
/**
* Add the property to this index.
*
* <p/>Note that this requires a round-trip to Accumulo to see
* if the property exists if the provided key has an index.
* So for best performance, create indices after bulk ingest.
* @param element
* @param key
* @param val
*/
public void setPropertyForIndex(Element element, String key, Object val) {
AccumuloGraphUtils.validateProperty(key, val);
try {
if (globals.getConfig().getAutoIndex() ||
globals.getGraph().getIndexedKeys(elementType).contains(key)) {
BatchWriter bw = getWriter();
Object old = element.getProperty(key);
if (old != null) {
byte[] oldByteVal = AccumuloByteSerializer.serialize(old);
Mutation m = new Mutation(oldByteVal);
m.putDelete(key, element.getId().toString());
bw.addMutation(m);
}
byte[] newByteVal = AccumuloByteSerializer.serialize(val);
Mutation m = new Mutation(newByteVal);
m.put(key.getBytes(), element.getId().toString().getBytes(), Constants.EMPTY);
bw.addMutation(m);
globals.checkedFlush();
}
} catch (MutationsRejectedException e) {
throw new AccumuloGraphException(e);
}
}
}

View File

@@ -24,6 +24,7 @@ import edu.jhuapl.tinkerpop.GlobalInstances;
public class VertexIndexTableWrapper extends IndexTableWrapper {
public VertexIndexTableWrapper(GlobalInstances globals) {
super(globals, globals.getConfig().getVertexKeyIndexTableName());
super(globals, Vertex.class, globals.getConfig()
.getVertexKeyIndexTableName());
}
}