Refactor conversion from entries to elements as ElementParser and other classes

This commit is contained in:
Michael Lieberman
2015-01-05 15:10:06 -05:00
parent 76b0007434
commit 37b4c8adc9
4 changed files with 103 additions and 15 deletions

View File

@@ -28,15 +28,16 @@ public class AccumuloEdge extends AccumuloElement implements Edge {
Vertex inVertex;
Vertex outVertex;
AccumuloEdge(GlobalInstances globals, String id) {
public AccumuloEdge(GlobalInstances globals, String id) {
this(globals, id, null);
}
AccumuloEdge(GlobalInstances globals, String id, String label) {
public AccumuloEdge(GlobalInstances globals, String id, String label) {
this(globals, id, label, (Vertex) null, (Vertex) null);
}
AccumuloEdge(GlobalInstances globals, String id, String label, Vertex inVertex, Vertex outVertex) {
public AccumuloEdge(GlobalInstances globals, String id,
String label, Vertex inVertex, Vertex outVertex) {
super(globals, id, Edge.class);
this.label = label;
this.inVertex = inVertex;

View File

@@ -0,0 +1,56 @@
/******************************************************************************
* COPYRIGHT NOTICE *
* Copyright (c) 2014 The Johns Hopkins University/Applied Physics Laboratory *
* All rights reserved. *
* *
* This material may only be used, modified, or reproduced by or for the *
* U.S. Government pursuant to the license rights granted under FAR clause *
* 52.227-14 or DFARS clauses 252.227-7013/7014. *
* *
* For any other permissions, please contact the Legal Office at JHU/APL. *
******************************************************************************/
package edu.jhuapl.tinkerpop.parser;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Value;
import edu.jhuapl.tinkerpop.AccumuloElement;
import edu.jhuapl.tinkerpop.GlobalInstances;
import edu.jhuapl.tinkerpop.mutator.property.PropertyUtils;
/**
* TODO
*/
public abstract class ElementParser<T extends AccumuloElement> {
protected GlobalInstances globals;
public ElementParser(GlobalInstances globals) {
this.globals = globals;
}
/**
* Given the element id and set of entries,
* create the type of element. This can leverage
* the property loader, etc.
* @param id
* @param entries
* @return
*/
public abstract T parse(String id, Iterable<Entry<Key, Value>> entries);
/**
* Parse out the property entries and set them for the given element.
* @param element
* @param entries
*/
protected void setInMemoryProperties(T element, Iterable<Entry<Key, Value>> entries) {
Map<String, Object> props = PropertyUtils.parseProperties(entries);
for (String key : props.keySet()) {
element.setPropertyInMemory(key, props.get(key));
}
}
}

View File

@@ -0,0 +1,37 @@
/******************************************************************************
* COPYRIGHT NOTICE *
* Copyright (c) 2014 The Johns Hopkins University/Applied Physics Laboratory *
* All rights reserved. *
* *
* This material may only be used, modified, or reproduced by or for the *
* U.S. Government pursuant to the license rights granted under FAR clause *
* 52.227-14 or DFARS clauses 252.227-7013/7014. *
* *
* For any other permissions, please contact the Legal Office at JHU/APL. *
******************************************************************************/
package edu.jhuapl.tinkerpop.parser;
import java.util.Map.Entry;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Value;
import edu.jhuapl.tinkerpop.AccumuloVertex;
import edu.jhuapl.tinkerpop.GlobalInstances;
/**
* TODO
*/
public class VertexParser extends ElementParser<AccumuloVertex> {
public VertexParser(GlobalInstances globals) {
super(globals);
}
@Override
public AccumuloVertex parse(String id, Iterable<Entry<Key,Value>> entries) {
AccumuloVertex vertex = new AccumuloVertex(globals, id);
setInMemoryProperties(vertex, entries);
return vertex;
}
}

View File

@@ -13,7 +13,6 @@ package edu.jhuapl.tinkerpop.tables;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.accumulo.core.client.Scanner;
@@ -32,10 +31,10 @@ import edu.jhuapl.tinkerpop.AccumuloGraph;
import edu.jhuapl.tinkerpop.AccumuloVertex;
import edu.jhuapl.tinkerpop.GlobalInstances;
import edu.jhuapl.tinkerpop.ScannerIterable;
import edu.jhuapl.tinkerpop.mutator.property.PropertyUtils;
import edu.jhuapl.tinkerpop.mutator.vertex.AddVertexMutator;
import edu.jhuapl.tinkerpop.mutator.Mutators;
import edu.jhuapl.tinkerpop.mutator.edge.EdgeEndpointsMutator;
import edu.jhuapl.tinkerpop.parser.VertexParser;
/**
@@ -160,14 +159,14 @@ public class VertexTableWrapper extends ElementTableWrapper {
}
}
final VertexParser parser = new VertexParser(globals);
return new ScannerIterable<Vertex>(scan) {
@Override
public Vertex next(PeekingIterator<Entry<Key, Value>> iterator) {
// TODO could also check local cache before creating a new instance?
AccumuloVertex vertex = new AccumuloVertex(globals,
iterator.peek().getKey().getRow().toString());
String rowId = vertex.getId().toString();
String rowId = iterator.peek().getKey().getRow().toString();
List<Entry<Key, Value>> entries =
new ArrayList<Entry<Key, Value>>();
@@ -176,13 +175,8 @@ public class VertexTableWrapper extends ElementTableWrapper {
.peek().getKey().getRow().toString())) {
entries.add(iterator.next());
}
Map<String, Object> props = PropertyUtils.parseProperties(entries);
for (String key : props.keySet()) {
vertex.setPropertyInMemory(key, props.get(key));
}
return vertex;
return parser.parse(rowId, entries);
}
};
}