14 Commits

Author SHA1 Message Date
Ryan Webb
f3f8115c91 Merge pull request #129 from gmlewis/master
Upgrade Apache Commons Collections to v3.2.2
2016-03-12 17:10:08 -08:00
Glenn Lewis
91623277fb Upgrade Apache Commons Collections to v3.2.2 2016-03-09 14:08:08 -08:00
Ryan Webb
8b0a131f5c Made flush and getConfiguration public.
#Baddevpushingtomaster
2015-07-17 10:34:42 -04:00
Ryan Webb
23df037c13 Merge pull request #111 from JHUAPL/webbrl1-coveritypatch
Made AccumuloGraphConfiguration implement cloneable
2015-07-16 17:07:04 -04:00
Ryan Webb
d954f1b4bd Merge pull request #119 from JHUAPL/passwordToken
Changes to support token changes in 1.6
2015-04-13 15:31:01 -04:00
Ryan Webb
033d29320c Changes to support token changes in 1.6 2015-04-13 14:41:48 -04:00
Ryan Webb
a9813d0236 Update changelog 2015-04-09 12:17:05 -04:00
Ryan Webb
46112bbc32 Merge pull request #117 from JHUAPL/issue116
Made Element serializable
2015-04-09 09:32:26 -04:00
Ryan Webb
c5a42df3aa Made Element serializable 2015-04-08 14:13:52 -04:00
Ryan Webb
9b66771627 Merge pull request #115 from JHUAPL/Issue113
Issue113
2015-04-03 18:20:54 -04:00
Ryan Webb
05c6067303 Update changelog 2015-04-03 18:15:51 -04:00
webbrl1
b1a9a88fd6 Added serilazable to MapReduce elements for Spark
Also addressed Issue 113 with conf instance name
2015-04-03 18:13:03 -04:00
Ryan Webb
e6464b59e3 Update README.md
Updated readme to have rexster info
2015-03-12 11:56:34 -04:00
Ryan Webb
13034977ea Made AccumuloGraphConfiguration implement cloneable
Detected by coverity
2015-02-20 12:53:20 -05:00
12 changed files with 431 additions and 311 deletions

View File

@@ -211,3 +211,23 @@ Job j = new Job();
j.setOutputFormatClass(ElementOutputFormat.class);
ElementOutputFormat.setAccumuloGraphConfiguration(j, cfg);
```
## Rexster Configuration
Below is a snippet to show an example of AccumuloGraph integration with Rexster. For a complete list of options for configuration, see [`AccumuloGraphConfiguration$Keys`](https://github.com/JHUAPL/AccumuloGraph/blob/master/src/main/java/edu/jhuapl/tinkerpop/AccumuloGraphConfiguration.java#L110)
```xml
<graph>
<graph-enabled>true</graph-enabled>
<graph-name>myGraph</graph-name>
<graph-type>edu.jhuapl.tinkerpop.AccumuloRexsterGraphConfiguration</graph-type>
<properties>
<blueprints.accumulo.instance.type>Distributed</blueprints.accumulo.instance.type>
<blueprints.accumulo.instance>accumulo</blueprints.accumulo.instance>
<blueprints.accumulo.zkhosts>zk1,zk2,zk3</blueprints.accumulo.zkhosts>
<blueprints.accumulo.user>user</blueprints.accumulo.user>
<blueprints.accumulo.password>password</blueprints.accumulo.password>
</properties>
<extensions>
</extensions>
</graph>
```

View File

@@ -1,3 +1,7 @@
[2.2 Snapshot]
-- 113 Applied instance name when mock instance is used
-- 114 Made map reduce elements serializable
-- 116 Made MapReduceElement serializable with a tranisent graph
[2.1]
Change Log started
--109,103 Merged Metadata tables

View File

@@ -65,7 +65,7 @@
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>

View File

@@ -136,6 +136,23 @@ public class AccumuloGraph implements Graph, KeyIndexableGraph, IndexableGraph {
return vert;
}
/**
*
* @return an immutable copy of the configuration running this graph
*/
public Configuration getConfiguration(){
return globals.getConfig().getConfiguration();
}
/**
* Flushes the backing writers so the data is persisted.
* @throws MutationsRejectedException
*/
public void flush() throws MutationsRejectedException{
globals.getMtbw().flush();
}
@Override
public Vertex getVertex(Object id) {

View File

@@ -14,9 +14,12 @@
*/
package edu.jhuapl.tinkerpop;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.lang.Cloneable;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.Arrays;
@@ -57,7 +60,7 @@ import com.tinkerpop.blueprints.KeyIndexableGraph;
* ease chained setting of parameters.
*/
public class AccumuloGraphConfiguration extends AbstractConfiguration
implements Serializable {
implements Serializable, Cloneable {
private static final long serialVersionUID = 7024072260167873696L;
@@ -256,6 +259,9 @@ implements Serializable {
setPassword("");
setCreate(true);
}
if(InstanceType.Mock.equals(type)){
setInstanceName("mock-instance");
}
return this;
}
@@ -326,6 +332,42 @@ implements Serializable {
return this;
}
public AccumuloGraphConfiguration setTokenWithFallback(byte[] token){
try{
setToken(token);
}catch(IllegalArgumentException e){
setPassword(token);
}
return this;
}
protected AccumuloGraphConfiguration setToken(byte[] token){
conf.setProperty(Keys.PASSWORD, new String(deserailize(token).getPassword()));
return this;
}
private PasswordToken deserailize(byte[] tokenBytes){
PasswordToken type = null;
try {
type = PasswordToken.class.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Cannot instantiate " + PasswordToken.class.getName(), e);
}
ByteArrayInputStream bais = new ByteArrayInputStream(tokenBytes);
DataInputStream in = new DataInputStream(bais);
try {
type.readFields(in);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot deserialize provided byte array as class " + PasswordToken.class.getName(), e);
}
try {
in.close();
} catch (IOException e) {
throw new IllegalStateException("Shouldn't happen", e);
}
return type;
}
public Authorizations getAuthorizations() {
return conf.containsKey(Keys.AUTHORIZATIONS) ?
new Authorizations(conf.getString(Keys.AUTHORIZATIONS).getBytes()) : null;
@@ -929,7 +971,7 @@ implements Serializable {
default:
throw new AccumuloGraphException("Unexpected instance type: " + inst);
}
connector = inst.getConnector(getUser(), new PasswordToken(getPassword()));
// Make the configuration immutable.

View File

@@ -1,5 +1,7 @@
package edu.jhuapl.tinkerpop.mapreduce;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
@@ -59,7 +61,7 @@ public class EdgeInputFormat extends InputFormatBase<Text,Edge> {
conf.setZooKeeperHosts(EdgeInputFormat.getInstance(attempt).getZooKeepers());
conf.setInstanceName(EdgeInputFormat.getInstance(attempt).getInstanceName());
conf.setUser(EdgeInputFormat.getPrincipal(attempt));
conf.setPassword(EdgeInputFormat.getToken(attempt));
conf.setTokenWithFallback(EdgeInputFormat.getToken(attempt));
conf.setGraphName(attempt.getConfiguration().get(GRAPH_NAME));
if (EdgeInputFormat.getInstance(attempt) instanceof MockInstance) {
conf.setInstanceType(InstanceType.Mock);
@@ -70,7 +72,7 @@ public class EdgeInputFormat extends InputFormatBase<Text,Edge> {
}
}
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
if (rowIterator.hasNext()) {

View File

@@ -1,6 +1,7 @@
package edu.jhuapl.tinkerpop.mapreduce;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map.Entry;
import org.apache.accumulo.core.client.AccumuloException;
@@ -70,10 +71,10 @@ public class ElementOutputFormat extends OutputFormat<NullWritable,Element> {
return new NullOutputFormat<Text,Mutation>().getOutputCommitter(context);
}
static class ElementRecordWriter extends RecordWriter<NullWritable,Element> {
static class ElementRecordWriter extends RecordWriter<NullWritable,Element> implements Serializable{
AccumuloGraphConfiguration config;
protected ElementRecordWriter(TaskAttemptContext context) {
public ElementRecordWriter(TaskAttemptContext context) {
config = new AccumuloGraphConfiguration();
Configuration jobconf = context.getConfiguration();
config.setUser(jobconf.get(USER));
@@ -85,7 +86,7 @@ public class ElementOutputFormat extends OutputFormat<NullWritable,Element> {
}
BatchWriter bw;
transient BatchWriter bw;
@Override
public void write(NullWritable key, Element value) throws IOException, InterruptedException {

View File

@@ -14,6 +14,8 @@
*/
package edu.jhuapl.tinkerpop.mapreduce;
import java.io.Serializable;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
@@ -21,7 +23,7 @@ import com.tinkerpop.blueprints.util.ExceptionFactory;
import edu.jhuapl.tinkerpop.AccumuloGraph;
public class MapReduceEdge extends MapReduceElement implements Edge {
public class MapReduceEdge extends MapReduceElement implements Edge, Serializable {
String sourceId;
String label;
@@ -30,6 +32,8 @@ public class MapReduceEdge extends MapReduceElement implements Edge {
MapReduceEdge(AccumuloGraph parent) {
super(parent);
}
MapReduceEdge() { super(); }
void setSourceId(String id) {
sourceId = id;

View File

@@ -17,6 +17,7 @@ package edu.jhuapl.tinkerpop.mapreduce;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -31,7 +32,7 @@ import com.tinkerpop.blueprints.Graph;
import edu.jhuapl.tinkerpop.AccumuloByteSerializer;
import edu.jhuapl.tinkerpop.AccumuloGraph;
public abstract class MapReduceElement implements Element, WritableComparable<MapReduceElement> {
public abstract class MapReduceElement implements Serializable, Element, WritableComparable<MapReduceElement> {
protected String id;
@@ -39,10 +40,14 @@ public abstract class MapReduceElement implements Element, WritableComparable<Ma
protected Map<String,Object> newProperties;
AccumuloGraph parent;
transient AccumuloGraph parent;
MapReduceElement(AccumuloGraph parent) {
this();
this.parent = parent;
}
MapReduceElement(){
properties = new HashMap<String,Object>();
newProperties = new HashMap<String,Object>();
}

View File

@@ -17,6 +17,7 @@ package edu.jhuapl.tinkerpop.mapreduce;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
@@ -33,7 +34,7 @@ import com.tinkerpop.blueprints.util.DefaultVertexQuery;
import edu.jhuapl.tinkerpop.AccumuloGraph;
public class MapReduceVertex extends MapReduceElement implements Vertex {
public class MapReduceVertex extends MapReduceElement implements Vertex, Serializable {
List<Edge> inEdges;
List<Edge> outEdges;
@@ -43,8 +44,14 @@ public class MapReduceVertex extends MapReduceElement implements Vertex {
inEdges = new LinkedList<Edge>();
outEdges = new LinkedList<Edge>();
}
MapReduceVertex(){
//super();
inEdges = new LinkedList<Edge>();
outEdges = new LinkedList<Edge>();
}
void prepareEdge(String id, String src, String label, String dest) {
MapReduceEdge prepareEdge(String id, String src, String label, String dest) {
MapReduceEdge mre = new MapReduceEdge(parent, id, src, label, dest);
if (src.equals(getId())) {
outEdges.add(mre);
@@ -54,6 +61,7 @@ public class MapReduceVertex extends MapReduceElement implements Vertex {
if (dest.equals(getId())) {
inEdges.add(mre);
}
return mre;
}
@Override

View File

@@ -1,5 +1,7 @@
package edu.jhuapl.tinkerpop.mapreduce;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
@@ -23,8 +25,8 @@ import com.tinkerpop.blueprints.Vertex;
import edu.jhuapl.tinkerpop.AccumuloByteSerializer;
import edu.jhuapl.tinkerpop.AccumuloGraph;
import edu.jhuapl.tinkerpop.AccumuloGraphConfiguration;
import edu.jhuapl.tinkerpop.AccumuloGraphException;
import edu.jhuapl.tinkerpop.AccumuloGraphConfiguration.InstanceType;
import edu.jhuapl.tinkerpop.AccumuloGraphException;
import edu.jhuapl.tinkerpop.Constants;
public class VertexInputFormat extends InputFormatBase<Text,Vertex> {
@@ -59,7 +61,7 @@ public class VertexInputFormat extends InputFormatBase<Text,Vertex> {
conf.setZooKeeperHosts(VertexInputFormat.getInstance(attempt).getZooKeepers());
conf.setInstanceName(VertexInputFormat.getInstance(attempt).getInstanceName());
conf.setUser(VertexInputFormat.getPrincipal(attempt));
conf.setPassword(VertexInputFormat.getToken(attempt));
conf.setTokenWithFallback(VertexInputFormat.getToken(attempt));
conf.setGraphName(attempt.getConfiguration().get(GRAPH_NAME));
if (VertexInputFormat.getInstance(attempt) instanceof MockInstance) {
conf.setInstanceType(InstanceType.Mock);
@@ -70,6 +72,8 @@ public class VertexInputFormat extends InputFormatBase<Text,Vertex> {
throw new AccumuloGraphException(e);
}
}
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {

View File

@@ -1,296 +1,309 @@
/* Copyright 2014 The Johns Hopkins University Applied Physics Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.jhuapl.tinkerpop;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.commons.configuration.Configuration;
import org.apache.hadoop.io.Text;
import org.junit.Test;
import com.tinkerpop.blueprints.GraphFactory;
import com.tinkerpop.blueprints.Vertex;
import edu.jhuapl.tinkerpop.AccumuloGraphConfiguration.InstanceType;
public class AccumuloGraphConfigurationTest {
@Test
public void testConfigurationInterface() throws Exception {
Configuration conf = AccumuloGraphTestUtils.generateGraphConfig("setPropsValid");
for (String key : AccumuloGraphConfiguration.getValidInternalKeys()) {
// This is bad... but we should allow them if they are valid keys.
conf.setProperty(key, "value");
}
conf = AccumuloGraphTestUtils.generateGraphConfig("setPropsInvalid");
try {
conf.setProperty("invalidKey", "value");
fail();
} catch (Exception e) { }
}
@Test
public void testSplits() throws Exception {
AccumuloGraphConfiguration cfg;
// Tests for splits string.
cfg = AccumuloGraphTestUtils.generateGraphConfig("nullSplits").setSplits((String) null);
AccumuloGraph graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("emptySplits").setSplits("");
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("threeSplits").setSplits(" a b c ");
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
Collection<Text> splits = cfg.getConnector().tableOperations().listSplits(table);
assertEquals(3, splits.size());
List<Text> arr = new ArrayList<Text>(splits);
assertEquals("a", arr.get(0).toString());
assertEquals("b", arr.get(1).toString());
assertEquals("c", arr.get(2).toString());
}
graph.shutdown();
// Tests for splits array.
cfg = AccumuloGraphTestUtils.generateGraphConfig("nullSplitsArray").setSplits((String[]) null);
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("emptySplitsArray").setSplits(new String[] {});
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("threeSplitsArray").setSplits(new String[] {"d", "e", "f"});
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
Collection<Text> splits = cfg.getConnector().tableOperations().listSplits(table);
assertEquals(3, splits.size());
List<Text> arr = new ArrayList<Text>(splits);
assertEquals("d", arr.get(0).toString());
assertEquals("e", arr.get(1).toString());
assertEquals("f", arr.get(2).toString());
}
graph.shutdown();
}
@Test
public void testPropertyValues() throws Exception {
AccumuloGraph graph = new AccumuloGraph(AccumuloGraphTestUtils.generateGraphConfig("propertyValues"));
// Tests for serialization/deserialization of properties.
QName qname = new QName("ns", "prop");
Vertex v = graph.addVertex(null);
v.setProperty("qname", qname);
assertTrue(v.getProperty("qname") instanceof QName);
assertTrue(qname.equals(v.getProperty("qname")));
}
@Test
public void testIsEmpty() throws Exception {
AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils.generateGraphConfig("isEmpty");
AccumuloGraph graph = new AccumuloGraph(cfg);
assertTrue(graph.isEmpty());
graph.addVertex("A");
assertFalse(graph.isEmpty());
graph.clear();
assertTrue(graph.isEmpty());
}
@Test
public void testCreateAndClear() throws Exception {
AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils.generateGraphConfig("noCreate").setCreate(false);
try {
new AccumuloGraph(cfg);
fail("Create is disabled and graph does not exist");
} catch (Exception e) {
assertTrue(true);
}
cfg = AccumuloGraphTestUtils.generateGraphConfig("yesCreate").setCreate(true);
for (String t : cfg.getTableNames()) {
assertFalse(cfg.getConnector().tableOperations().exists(t));
}
AccumuloGraph graph = new AccumuloGraph(cfg);
for (String t : cfg.getTableNames()) {
assertTrue(cfg.getConnector().tableOperations().exists(t));
}
graph.shutdown();
graph = new AccumuloGraph(cfg.clone().setCreate(false));
assertTrue(graph.isEmpty());
graph.addVertex("A");
graph.addVertex("B");
assertFalse(graph.isEmpty());
graph.shutdown();
graph = new AccumuloGraph(cfg.clone().setClear(true));
assertTrue(graph.isEmpty());
graph.shutdown();
}
@Test
public void testPrint() throws Exception {
AccumuloGraphConfiguration cfg =
AccumuloGraphTestUtils.generateGraphConfig("printTest");
cfg.print();
}
@Test
public void testInvalidCacheParams() throws Exception {
int size = 100;
int timeout = 30000;
AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils
.generateGraphConfig("cacheParams");
cfg.validate();
// Vertex cache.
assertFalse(cfg.getVertexCacheEnabled());
try {
cfg.setVertexCacheParams(-1, timeout);
fail();
} catch (Exception e) { }
try {
cfg.setVertexCacheParams(size, -1);
fail();
} catch (Exception e) { }
assertFalse(cfg.getVertexCacheEnabled());
cfg.setVertexCacheParams(size, timeout);
cfg.validate();
assertTrue(cfg.getVertexCacheEnabled());
assertEquals(size, cfg.getVertexCacheSize());
assertEquals(timeout, cfg.getVertexCacheTimeout());
cfg.setVertexCacheParams(-1, -1);
cfg.validate();
assertFalse(cfg.getVertexCacheEnabled());
// Edge cache.
assertFalse(cfg.getEdgeCacheEnabled());
try {
cfg.setEdgeCacheParams(-1, timeout);
fail();
} catch (Exception e) { }
try {
cfg.setEdgeCacheParams(size, -1);
fail();
} catch (Exception e) { }
assertFalse(cfg.getEdgeCacheEnabled());
cfg.setEdgeCacheParams(size, timeout);
cfg.validate();
assertTrue(cfg.getEdgeCacheEnabled());
assertEquals(size, cfg.getEdgeCacheSize());
assertEquals(timeout, cfg.getEdgeCacheTimeout());
cfg.setEdgeCacheParams(-1, -1);
cfg.validate();
assertFalse(cfg.getEdgeCacheEnabled());
}
/**
* Test different kinds of graph names (hyphens, punctuation, etc).
* @throws Exception
*/
@Test
public void testGraphNames() throws Exception {
AccumuloGraphConfiguration conf = new AccumuloGraphConfiguration();
String[] valid = new String[] {
"alpha", "12345", "alnum12345",
"12345alnum", "under_score1", "_under_score_2"};
String[] invalid = new String[] {"hyph-en",
"dot..s", "quo\"tes"};
for (String name : valid) {
conf.setGraphName(name);
}
for (String name : invalid) {
try {
conf.setGraphName(name);
fail();
} catch (Exception e) { }
}
}
@Test
public void testPreloadedProperties() {
// Don't allow "all" and "some" preloaded properties.
AccumuloGraphConfiguration conf = new AccumuloGraphConfiguration();
conf.setPreloadAllProperties(true);
conf.setPreloadedProperties(new String[]{"one", "two", "three"});
try {
conf.validate();
fail();
} catch (Exception e) { }
}
@Test
public void testImmutableConnector() throws Exception {
AccumuloGraphConfiguration cfg = new AccumuloGraphConfiguration().setInstanceType(
InstanceType.Mock).setGraphName("immutableConnector")
.setCreate(true).setAutoFlush(false);
cfg.getConnector();
try {
cfg.setCreate(false);
fail();
} catch (Exception e) { }
try {
cfg.setAutoFlush(true);
fail();
} catch (Exception e) { }
assertTrue(cfg.getCreate());
assertFalse(cfg.getAutoFlush());
}
}
/* Copyright 2014 The Johns Hopkins University Applied Physics Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.jhuapl.tinkerpop;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.accumulo.core.client.Instance;
import org.apache.accumulo.core.client.mock.MockInstance;
import org.apache.commons.configuration.Configuration;
import org.apache.hadoop.io.Text;
import org.junit.Test;
import com.tinkerpop.blueprints.GraphFactory;
import com.tinkerpop.blueprints.Vertex;
import edu.jhuapl.tinkerpop.AccumuloGraphConfiguration.InstanceType;
public class AccumuloGraphConfigurationTest {
@Test
public void testConfigurationInterface() throws Exception {
Configuration conf = AccumuloGraphTestUtils.generateGraphConfig("setPropsValid");
for (String key : AccumuloGraphConfiguration.getValidInternalKeys()) {
// This is bad... but we should allow them if they are valid keys.
conf.setProperty(key, "value");
}
conf = AccumuloGraphTestUtils.generateGraphConfig("setPropsInvalid");
try {
conf.setProperty("invalidKey", "value");
fail();
} catch (Exception e) { }
}
@Test
public void testSplits() throws Exception {
AccumuloGraphConfiguration cfg;
// Tests for splits string.
cfg = AccumuloGraphTestUtils.generateGraphConfig("nullSplits").setSplits((String) null);
AccumuloGraph graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("emptySplits").setSplits("");
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("threeSplits").setSplits(" a b c ");
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
Collection<Text> splits = cfg.getConnector().tableOperations().listSplits(table);
assertEquals(3, splits.size());
List<Text> arr = new ArrayList<Text>(splits);
assertEquals("a", arr.get(0).toString());
assertEquals("b", arr.get(1).toString());
assertEquals("c", arr.get(2).toString());
}
graph.shutdown();
// Tests for splits array.
cfg = AccumuloGraphTestUtils.generateGraphConfig("nullSplitsArray").setSplits((String[]) null);
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("emptySplitsArray").setSplits(new String[] {});
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
assertEquals(0, cfg.getConnector().tableOperations().listSplits(table).size());
}
graph.shutdown();
cfg = AccumuloGraphTestUtils.generateGraphConfig("threeSplitsArray").setSplits(new String[] {"d", "e", "f"});
graph = (AccumuloGraph) GraphFactory.open(cfg.getConfiguration());
for (String table : cfg.getTableNames()) {
Collection<Text> splits = cfg.getConnector().tableOperations().listSplits(table);
assertEquals(3, splits.size());
List<Text> arr = new ArrayList<Text>(splits);
assertEquals("d", arr.get(0).toString());
assertEquals("e", arr.get(1).toString());
assertEquals("f", arr.get(2).toString());
}
graph.shutdown();
}
@Test
public void testPropertyValues() throws Exception {
AccumuloGraph graph = new AccumuloGraph(AccumuloGraphTestUtils.generateGraphConfig("propertyValues"));
// Tests for serialization/deserialization of properties.
QName qname = new QName("ns", "prop");
Vertex v = graph.addVertex(null);
v.setProperty("qname", qname);
assertTrue(v.getProperty("qname") instanceof QName);
assertTrue(qname.equals(v.getProperty("qname")));
}
@Test
public void testIsEmpty() throws Exception {
AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils.generateGraphConfig("isEmpty");
AccumuloGraph graph = new AccumuloGraph(cfg);
assertTrue(graph.isEmpty());
graph.addVertex("A");
assertFalse(graph.isEmpty());
graph.clear();
assertTrue(graph.isEmpty());
}
@Test
public void testCreateAndClear() throws Exception {
AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils.generateGraphConfig("noCreate").setCreate(false);
try {
new AccumuloGraph(cfg);
fail("Create is disabled and graph does not exist");
} catch (Exception e) {
assertTrue(true);
}
cfg = AccumuloGraphTestUtils.generateGraphConfig("yesCreate").setCreate(true);
for (String t : cfg.getTableNames()) {
assertFalse(cfg.getConnector().tableOperations().exists(t));
}
AccumuloGraph graph = new AccumuloGraph(cfg);
for (String t : cfg.getTableNames()) {
assertTrue(cfg.getConnector().tableOperations().exists(t));
}
graph.shutdown();
graph = new AccumuloGraph(cfg.clone().setCreate(false));
assertTrue(graph.isEmpty());
graph.addVertex("A");
graph.addVertex("B");
assertFalse(graph.isEmpty());
graph.shutdown();
graph = new AccumuloGraph(cfg.clone().setClear(true));
assertTrue(graph.isEmpty());
graph.shutdown();
}
@Test
public void testPrint() throws Exception {
AccumuloGraphConfiguration cfg =
AccumuloGraphTestUtils.generateGraphConfig("printTest");
cfg.print();
}
@Test
public void testInvalidCacheParams() throws Exception {
int size = 100;
int timeout = 30000;
AccumuloGraphConfiguration cfg = AccumuloGraphTestUtils
.generateGraphConfig("cacheParams");
cfg.validate();
// Vertex cache.
assertFalse(cfg.getVertexCacheEnabled());
try {
cfg.setVertexCacheParams(-1, timeout);
fail();
} catch (Exception e) { }
try {
cfg.setVertexCacheParams(size, -1);
fail();
} catch (Exception e) { }
assertFalse(cfg.getVertexCacheEnabled());
cfg.setVertexCacheParams(size, timeout);
cfg.validate();
assertTrue(cfg.getVertexCacheEnabled());
assertEquals(size, cfg.getVertexCacheSize());
assertEquals(timeout, cfg.getVertexCacheTimeout());
cfg.setVertexCacheParams(-1, -1);
cfg.validate();
assertFalse(cfg.getVertexCacheEnabled());
// Edge cache.
assertFalse(cfg.getEdgeCacheEnabled());
try {
cfg.setEdgeCacheParams(-1, timeout);
fail();
} catch (Exception e) { }
try {
cfg.setEdgeCacheParams(size, -1);
fail();
} catch (Exception e) { }
assertFalse(cfg.getEdgeCacheEnabled());
cfg.setEdgeCacheParams(size, timeout);
cfg.validate();
assertTrue(cfg.getEdgeCacheEnabled());
assertEquals(size, cfg.getEdgeCacheSize());
assertEquals(timeout, cfg.getEdgeCacheTimeout());
cfg.setEdgeCacheParams(-1, -1);
cfg.validate();
assertFalse(cfg.getEdgeCacheEnabled());
}
/**
* Test different kinds of graph names (hyphens, punctuation, etc).
* @throws Exception
*/
@Test
public void testGraphNames() throws Exception {
AccumuloGraphConfiguration conf = new AccumuloGraphConfiguration();
String[] valid = new String[] {
"alpha", "12345", "alnum12345",
"12345alnum", "under_score1", "_under_score_2"};
String[] invalid = new String[] {"hyph-en",
"dot..s", "quo\"tes"};
for (String name : valid) {
conf.setGraphName(name);
}
for (String name : invalid) {
try {
conf.setGraphName(name);
fail();
} catch (Exception e) { }
}
}
@Test
public void testPreloadedProperties() {
// Don't allow "all" and "some" preloaded properties.
AccumuloGraphConfiguration conf = new AccumuloGraphConfiguration();
conf.setPreloadAllProperties(true);
conf.setPreloadedProperties(new String[]{"one", "two", "three"});
try {
conf.validate();
fail();
} catch (Exception e) { }
}
@Test
public void testImmutableConnector() throws Exception {
AccumuloGraphConfiguration cfg = new AccumuloGraphConfiguration().setInstanceType(
InstanceType.Mock).setGraphName("immutableConnector")
.setCreate(true).setAutoFlush(false);
cfg.getConnector();
try {
cfg.setCreate(false);
fail();
} catch (Exception e) { }
try {
cfg.setAutoFlush(true);
fail();
} catch (Exception e) { }
assertTrue(cfg.getCreate());
assertFalse(cfg.getAutoFlush());
}
@Test
public void testMockInstanceValue(){
AccumuloGraphConfiguration conf = new AccumuloGraphConfiguration().setInstanceType(InstanceType.Mock);
assertNotNull(conf.getInstanceName());
assertEquals("mock-instance", conf.getInstanceName());
}
}