http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/DefineTripleQueryRangeFactoryTest.java ---------------------------------------------------------------------- diff --git a/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/DefineTripleQueryRangeFactoryTest.java b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/DefineTripleQueryRangeFactoryTest.java new file mode 100644 index 0000000..7c3331d --- /dev/null +++ b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/DefineTripleQueryRangeFactoryTest.java @@ -0,0 +1,265 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 mvm.rya.accumulo; + +// +//import junit.framework.TestCase; +//import mvm.rya.accumulo.AccumuloRdfConfiguration; +//import mvm.rya.accumulo.DefineTripleQueryRangeFactory; +//import mvm.rya.accumulo.AccumuloRdfConfiguration; +//import mvm.rya.accumulo.DefineTripleQueryRangeFactory; +//import mvm.rya.api.domain.RangeValue; +//import org.apache.accumulo.core.data.Range; +//import org.openrdf.model.URI; +//import org.openrdf.model.Value; +//import org.openrdf.model.ValueFactory; +//import org.openrdf.model.impl.ValueFactoryImpl; +// +//import java.util.Map; +// +//import static mvm.rya.api.RdfCloudTripleStoreConstants.*; +// +///** +// */ +//public class DefineTripleQueryRangeFactoryTest extends TestCase { +// +// public static final String DELIM_BYTES_STR = new String(DELIM_BYTES); +// public static final String URI_MARKER_STR = "\u0007"; +// public static final String RANGE_ENDKEY_SUFFIX = "\u0000"; +// DefineTripleQueryRangeFactory factory = new DefineTripleQueryRangeFactory(); +// ValueFactory vf = ValueFactoryImpl.getInstance(); +// static String litdupsNS = "urn:test:litdups#"; +// +// private AccumuloRdfConfiguration conf = new AccumuloRdfConfiguration(); +// +// public void testSPOCases() throws Exception { +// URI cpu = vf.createURI(litdupsNS, "cpu"); +// URI loadPerc = vf.createURI(litdupsNS, "loadPerc"); +// URI obj = vf.createURI(litdupsNS, "uri1"); +// +// //spo +// Map.Entry<TABLE_LAYOUT, Range> entry = +// factory.defineRange(cpu, loadPerc, obj, conf); +// assertEquals(TABLE_LAYOUT.SPO, entry.getKey()); +// String expected_start = URI_MARKER_STR + cpu.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + loadPerc.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + obj.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(expected_start + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// +// //sp +// entry = factory.defineRange(cpu, loadPerc, null, conf); +// assertEquals(TABLE_LAYOUT.SPO, entry.getKey()); +// expected_start = URI_MARKER_STR + cpu.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + loadPerc.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(expected_start + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //s +// entry = factory.defineRange(cpu, null, null, conf); +// assertEquals(TABLE_LAYOUT.SPO, entry.getKey()); +// expected_start = URI_MARKER_STR + cpu.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(expected_start + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //all +// entry = factory.defineRange(null, null, null, conf); +// assertEquals(TABLE_LAYOUT.SPO, entry.getKey()); +// assertEquals("", +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(new String(new byte[]{Byte.MAX_VALUE}) + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// } +// +// public void testSPOCasesWithRanges() throws Exception { +// URI subj_start = vf.createURI(litdupsNS, "subj_start"); +// URI subj_end = vf.createURI(litdupsNS, "subj_stop"); +// URI pred_start = vf.createURI(litdupsNS, "pred_start"); +// URI pred_end = vf.createURI(litdupsNS, "pred_stop"); +// URI obj_start = vf.createURI(litdupsNS, "obj_start"); +// URI obj_end = vf.createURI(litdupsNS, "obj_stop"); +// +// Value subj = new RangeValue(subj_start, subj_end); +// Value pred = new RangeValue(pred_start, pred_end); +// Value obj = new RangeValue(obj_start, obj_end); +// +// //spo - o has range +// Map.Entry<TABLE_LAYOUT, Range> entry = +// factory.defineRange(subj_start, pred_start, obj, conf); +// assertEquals(TABLE_LAYOUT.SPO, entry.getKey()); +// String expected_start = URI_MARKER_STR + subj_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + pred_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + obj_start.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// String expected_end = URI_MARKER_STR + subj_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + pred_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + obj_end.stringValue(); +// assertEquals(expected_end + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //sp - p has range +// entry = factory.defineRange(subj_start, pred, null, conf); +// assertEquals(TABLE_LAYOUT.SPO, entry.getKey()); +// expected_start = URI_MARKER_STR + subj_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + pred_start.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// expected_end = URI_MARKER_STR + subj_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + pred_end.stringValue(); +// assertEquals(expected_end + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //s - s has range +// entry = factory.defineRange(subj, null, null, conf); +// assertEquals(TABLE_LAYOUT.SPO, entry.getKey()); +// expected_start = URI_MARKER_STR + subj_start.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// expected_end = URI_MARKER_STR + subj_end.stringValue(); +// assertEquals(expected_end + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// } +// +// public void testPOCases() throws Exception { +// URI loadPerc = vf.createURI(litdupsNS, "loadPerc"); +// URI obj = vf.createURI(litdupsNS, "uri1"); +// +// //po +// Map.Entry<TABLE_LAYOUT, Range> entry = +// factory.defineRange(null, loadPerc, obj, conf); +// assertEquals(TABLE_LAYOUT.PO, entry.getKey()); +// String expected_start = URI_MARKER_STR + loadPerc.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + obj.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(expected_start + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //p +// entry = factory.defineRange(null, loadPerc, null, conf); +// assertEquals(TABLE_LAYOUT.PO, entry.getKey()); +// expected_start = URI_MARKER_STR + loadPerc.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(expected_start + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// } +// +// public void testPOCasesWithRanges() throws Exception { +// URI pred_start = vf.createURI(litdupsNS, "pred_start"); +// URI pred_end = vf.createURI(litdupsNS, "pred_stop"); +// URI obj_start = vf.createURI(litdupsNS, "obj_start"); +// URI obj_end = vf.createURI(litdupsNS, "obj_stop"); +// +// Value pred = new RangeValue(pred_start, pred_end); +// Value obj = new RangeValue(obj_start, obj_end); +// +// //po +// Map.Entry<TABLE_LAYOUT, Range> entry = +// factory.defineRange(null, pred_start, obj, conf); +// assertEquals(TABLE_LAYOUT.PO, entry.getKey()); +// String expected_start = URI_MARKER_STR + pred_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + obj_start.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// String expected_end = URI_MARKER_STR + pred_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + obj_end.stringValue(); +// assertEquals(expected_end + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //p +// entry = factory.defineRange(null, pred, null, conf); +// assertEquals(TABLE_LAYOUT.PO, entry.getKey()); +// expected_start = URI_MARKER_STR + pred_start.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// expected_end = URI_MARKER_STR + pred_end.stringValue(); +// assertEquals(expected_end + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// } +// +// public void testOSPCases() throws Exception { +// URI cpu = vf.createURI(litdupsNS, "cpu"); +// URI obj = vf.createURI(litdupsNS, "uri1"); +// +// //so +// Map.Entry<TABLE_LAYOUT, Range> entry = +// factory.defineRange(cpu, null, obj, conf); +// assertEquals(TABLE_LAYOUT.OSP, entry.getKey()); +// String expected_start = URI_MARKER_STR + obj.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + cpu.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(expected_start + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //o +// entry = factory.defineRange(null, null, obj, conf); +// assertEquals(TABLE_LAYOUT.OSP, entry.getKey()); +// expected_start = URI_MARKER_STR + obj.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// assertEquals(expected_start + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// } +// +// +// public void testOSPCasesWithRanges() throws Exception { +// URI subj_start = vf.createURI(litdupsNS, "subj_start"); +// URI subj_end = vf.createURI(litdupsNS, "subj_stop"); +// URI obj_start = vf.createURI(litdupsNS, "obj_start"); +// URI obj_end = vf.createURI(litdupsNS, "obj_stop"); +// +// Value subj = new RangeValue(subj_start, subj_end); +// Value obj = new RangeValue(obj_start, obj_end); +// +// //so - s should be the range +// Map.Entry<TABLE_LAYOUT, Range> entry = +// factory.defineRange(subj, null, obj_start, conf); +// assertEquals(TABLE_LAYOUT.OSP, entry.getKey()); +// String expected_start = URI_MARKER_STR + obj_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + subj_start.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// String expected_end = URI_MARKER_STR + obj_start.stringValue() + DELIM_BYTES_STR + +// URI_MARKER_STR + subj_end.stringValue(); +// assertEquals(expected_end + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// +// //o - o is range +// entry = factory.defineRange(null, null, obj, conf); +// assertEquals(TABLE_LAYOUT.OSP, entry.getKey()); +// expected_start = URI_MARKER_STR + obj_start.stringValue(); +// assertEquals(expected_start, +// entry.getValue().getStartKey().getRow().toString()); +// expected_end = URI_MARKER_STR + obj_end.stringValue(); +// assertEquals(expected_end + DELIM_STOP + RANGE_ENDKEY_SUFFIX, +// entry.getValue().getEndKey().getRow().toString()); +// } +// +//}
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/MiniAccumuloClusterInstance.java ---------------------------------------------------------------------- diff --git a/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/MiniAccumuloClusterInstance.java b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/MiniAccumuloClusterInstance.java new file mode 100644 index 0000000..eb11ed8 --- /dev/null +++ b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/MiniAccumuloClusterInstance.java @@ -0,0 +1,119 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 mvm.rya.accumulo; + +import java.io.File; +import java.io.IOException; + +import org.apache.accumulo.core.client.AccumuloException; +import org.apache.accumulo.core.client.AccumuloSecurityException; +import org.apache.accumulo.core.client.Connector; +import org.apache.accumulo.minicluster.MiniAccumuloCluster; +import org.apache.accumulo.minicluster.MiniAccumuloConfig; +import org.apache.log4j.Logger; + +import com.google.common.io.Files; + +/** + * Contains boilerplate code that can be used by an integration test that + * uses a {@link MiniAccumuloCluster}. + * <p> + * You can just extend {@link AccumuloITBase} if your test only requires Accumulo. + */ +public class MiniAccumuloClusterInstance { + + private static final Logger log = Logger.getLogger(MiniAccumuloClusterInstance.class); + + private static final String USERNAME = "root"; + private static final String PASSWORD = "password"; + + /** + * A mini Accumulo cluster that can be used by the tests. + */ + private static MiniAccumuloCluster cluster = null; + + /** + * Start the {@link MiniAccumuloCluster}. + */ + public void startMiniAccumulo() throws IOException, InterruptedException, AccumuloException, AccumuloSecurityException { + final File miniDataDir = Files.createTempDir(); + + // Setup and start the Mini Accumulo. + final MiniAccumuloConfig cfg = new MiniAccumuloConfig(miniDataDir, PASSWORD); + cluster = new MiniAccumuloCluster(cfg); + cluster.start(); + } + + /** + * Stop the {@link MiniAccumuloCluster}. + */ + public void stopMiniAccumulo() throws IOException, InterruptedException { + if(cluster != null) { + try { + log.info("Shutting down the Mini Accumulo being used as a Rya store."); + cluster.stop(); + log.info("Mini Accumulo being used as a Rya store shut down."); + } catch(final Exception e) { + log.error("Could not shut down the Mini Accumulo.", e); + } + } + } + + /** + * @return The {@link MiniAccumuloCluster} managed by this class. + */ + public MiniAccumuloCluster getCluster() { + return cluster; + } + + /** + * @return An accumulo connector that is connected to the mini cluster. + */ + public Connector getConnector() throws AccumuloException, AccumuloSecurityException { + return cluster.getConnector(USERNAME, PASSWORD); + } + + /** + * @return The root username. + */ + public String getUsername() { + return USERNAME; + } + + /** + * @return The root password. + */ + public String getPassword() { + return PASSWORD; + } + + /** + * @return The MiniAccumulo's zookeeper instance name. + */ + public String getInstanceName() { + return cluster.getInstanceName(); + } + + /** + * @return The MiniAccumulo's zookeepers. + */ + public String getZookeepers() { + return cluster.getZooKeepers(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/instance/AccumuloRyaDetailsRepositoryIT.java ---------------------------------------------------------------------- diff --git a/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/instance/AccumuloRyaDetailsRepositoryIT.java b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/instance/AccumuloRyaDetailsRepositoryIT.java new file mode 100644 index 0000000..bdd6059 --- /dev/null +++ b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/instance/AccumuloRyaDetailsRepositoryIT.java @@ -0,0 +1,304 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 mvm.rya.accumulo.instance; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Date; + +import org.apache.accumulo.core.client.AccumuloException; +import org.apache.accumulo.core.client.AccumuloSecurityException; +import org.apache.accumulo.core.client.Connector; +import org.apache.accumulo.core.client.Instance; +import org.apache.accumulo.core.client.ZooKeeperInstance; +import org.apache.accumulo.core.client.mock.MockInstance; +import org.apache.accumulo.core.client.security.tokens.PasswordToken; +import org.apache.accumulo.minicluster.MiniAccumuloCluster; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.zookeeper.ClientCnxn; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.google.common.base.Optional; + +import mvm.rya.accumulo.AccumuloITBase; +import mvm.rya.accumulo.MiniAccumuloClusterInstance; +import mvm.rya.api.instance.RyaDetails; +import mvm.rya.api.instance.RyaDetails.EntityCentricIndexDetails; +import mvm.rya.api.instance.RyaDetails.FreeTextIndexDetails; +import mvm.rya.api.instance.RyaDetails.GeoIndexDetails; +import mvm.rya.api.instance.RyaDetails.JoinSelectivityDetails; +import mvm.rya.api.instance.RyaDetails.PCJIndexDetails; +import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails; +import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails; +import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails.PCJUpdateStrategy; +import mvm.rya.api.instance.RyaDetails.ProspectorDetails; +import mvm.rya.api.instance.RyaDetails.TemporalIndexDetails; +import mvm.rya.api.instance.RyaDetailsRepository; +import mvm.rya.api.instance.RyaDetailsRepository.AlreadyInitializedException; +import mvm.rya.api.instance.RyaDetailsRepository.ConcurrentUpdateException; +import mvm.rya.api.instance.RyaDetailsRepository.NotInitializedException; +import mvm.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException; + +/** + * Tests the methods of {@link AccumuloRyaDetailsRepository} by using a {@link MiniAccumuloCluster}. + */ +public class AccumuloRyaDetailsRepositoryIT extends AccumuloITBase { + + @Test + public void initializeAndGet() throws AccumuloException, AccumuloSecurityException, AlreadyInitializedException, RyaDetailsRepositoryException { + final String instanceName = "testInstance"; + + // Create the metadata object the repository will be initialized with. + final RyaDetails details = RyaDetails.builder() + .setRyaInstanceName(instanceName) + .setRyaVersion("1.2.3.4") + .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) + .setGeoIndexDetails( new GeoIndexDetails(true) ) + .setTemporalIndexDetails( new TemporalIndexDetails(true) ) + .setFreeTextDetails( new FreeTextIndexDetails(true) ) + .setPCJIndexDetails( + PCJIndexDetails.builder() + .setEnabled(true) + .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 1") + .setUpdateStrategy(PCJUpdateStrategy.BATCH) + .setLastUpdateTime( new Date() )) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 2"))) + .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) + .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ) + .build(); + + // Setup the repository that will be tested using a mini instance of Accumulo. + final Connector connector = getClusterInstance().getConnector(); + final RyaDetailsRepository repo = new AccumuloRyaInstanceDetailsRepository(connector, instanceName); + + // Initialize the repository + repo.initialize(details); + + // Fetch the stored details. + final RyaDetails stored = repo.getRyaInstanceDetails(); + + // Ensure the fetched object is equivalent to what was stored. + assertEquals(details, stored); + } + + @Test(expected = AlreadyInitializedException.class) + public void initialize_alreadyInitialized() throws AlreadyInitializedException, RyaDetailsRepositoryException, AccumuloException, AccumuloSecurityException { + final String instanceName = "testInstance"; + + // Create the metadata object the repository will be initialized with. + final RyaDetails details = RyaDetails.builder() + .setRyaInstanceName(instanceName) + .setRyaVersion("1.2.3.4") + .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) + .setGeoIndexDetails( new GeoIndexDetails(true) ) + .setTemporalIndexDetails( new TemporalIndexDetails(true) ) + .setFreeTextDetails( new FreeTextIndexDetails(true) ) + .setPCJIndexDetails( + PCJIndexDetails.builder() + .setEnabled(true) + .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 1") + .setUpdateStrategy(PCJUpdateStrategy.BATCH) + .setLastUpdateTime( new Date() )) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 2"))) + .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) + .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ) + .build(); + + // Setup the repository that will be tested using a mini instance of Accumulo. + final Connector connector = getClusterInstance().getConnector(); + final RyaDetailsRepository repo = new AccumuloRyaInstanceDetailsRepository(connector, instanceName); + + // Initialize the repository + repo.initialize(details); + + // Initialize it again. + repo.initialize(details); + } + + @Test(expected = NotInitializedException.class) + public void getRyaInstance_notInitialized() throws AccumuloException, AccumuloSecurityException, NotInitializedException, RyaDetailsRepositoryException { + // Setup the repository that will be tested using a mini instance of Accumulo. + final Connector connector = getClusterInstance().getConnector(); + final RyaDetailsRepository repo = new AccumuloRyaInstanceDetailsRepository(connector, "testInstance"); + + // Try to fetch the details from the uninitialized repository. + repo.getRyaInstanceDetails(); + } + + @Test + public void isInitialized_true() throws AccumuloException, AccumuloSecurityException, AlreadyInitializedException, RyaDetailsRepositoryException { + final String instanceName = "testInstance"; + + // Create the metadata object the repository will be initialized with. + final RyaDetails details = RyaDetails.builder() + .setRyaInstanceName(instanceName) + .setRyaVersion("1.2.3.4") + .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) + .setGeoIndexDetails( new GeoIndexDetails(true) ) + .setTemporalIndexDetails( new TemporalIndexDetails(true) ) + .setFreeTextDetails( new FreeTextIndexDetails(true) ) + .setPCJIndexDetails( + PCJIndexDetails.builder() + .setEnabled(true) + .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 1") + .setUpdateStrategy(PCJUpdateStrategy.BATCH) + .setLastUpdateTime( new Date() )) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 2"))) + .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) + .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ) + .build(); + + // Setup the repository that will be tested using a mini instance of Accumulo. + final MiniAccumuloClusterInstance clusterInstance = getClusterInstance(); + final Connector connector = clusterInstance.getConnector(); + final RyaDetailsRepository repo = new AccumuloRyaInstanceDetailsRepository(connector, "testInstance"); + + // Initialize the repository + repo.initialize(details); + + // Ensure the repository reports that it has been initialized. + assertTrue( repo.isInitialized() ); + } + + @Test + public void isInitialized_false() throws AccumuloException, AccumuloSecurityException, RyaDetailsRepositoryException { + // Setup the repository that will be tested using a mock instance of Accumulo. + final Connector connector = getClusterInstance().getConnector(); + final RyaDetailsRepository repo = new AccumuloRyaInstanceDetailsRepository(connector, "testInstance"); + + // Ensure the repository reports that is has not been initialized. + assertFalse( repo.isInitialized() ); + } + + @Test + public void update() throws AlreadyInitializedException, RyaDetailsRepositoryException, AccumuloException, AccumuloSecurityException { + final String instanceName = "testInstance"; + + // Create the metadata object the repository will be initialized with. + final RyaDetails details = RyaDetails.builder() + .setRyaInstanceName(instanceName) + .setRyaVersion("1.2.3.4") + .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) + .setGeoIndexDetails( new GeoIndexDetails(true) ) + .setTemporalIndexDetails( new TemporalIndexDetails(true) ) + .setFreeTextDetails( new FreeTextIndexDetails(true) ) + .setPCJIndexDetails( + PCJIndexDetails.builder() + .setEnabled(true) + .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 1") + .setUpdateStrategy(PCJUpdateStrategy.BATCH) + .setLastUpdateTime( new Date() )) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 2"))) + .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) + .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ) + .build(); + + // Setup the repository that will be tested using a mini instance of Accumulo. + final Connector connector = getClusterInstance().getConnector(); + final RyaDetailsRepository repo = new AccumuloRyaInstanceDetailsRepository(connector, "testInstance"); + + // Initialize the repository + repo.initialize(details); + + // Create a new state for the details. + final RyaDetails updated = new RyaDetails.Builder( details ) + .setGeoIndexDetails( new GeoIndexDetails(false) ) + .build(); + + // Execute the update. + repo.update(details, updated); + + // Show the new state that is stored matches the updated state. + final RyaDetails fetched = repo.getRyaInstanceDetails(); + assertEquals(updated, fetched); + } + + @Test(expected = ConcurrentUpdateException.class) + public void update_outOfDate() throws AccumuloException, AccumuloSecurityException, AlreadyInitializedException, RyaDetailsRepositoryException { + final String instanceName = "testInstance"; + + // Create the metadata object the repository will be initialized with. + final RyaDetails details = RyaDetails.builder() + .setRyaInstanceName(instanceName) + .setRyaVersion("1.2.3.4") + .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) + .setGeoIndexDetails( new GeoIndexDetails(true) ) + .setTemporalIndexDetails( new TemporalIndexDetails(true) ) + .setFreeTextDetails( new FreeTextIndexDetails(true) ) + .setPCJIndexDetails( + PCJIndexDetails.builder() + .setEnabled(true) + .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 1") + .setUpdateStrategy(PCJUpdateStrategy.BATCH) + .setLastUpdateTime( new Date() )) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 2"))) + .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) + .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ) + .build(); + + // Setup the repository that will be tested using a mini instance of Accumulo. + final Connector connector = getClusterInstance().getConnector(); + final RyaDetailsRepository repo = new AccumuloRyaInstanceDetailsRepository(connector, "testInstance"); + + // Initialize the repository + repo.initialize(details); + + // Create a new state for the details. + final RyaDetails updated = new RyaDetails.Builder( details ) + .setGeoIndexDetails( new GeoIndexDetails(false) ) + .build(); + + // Try to execute the update where the old state is not the currently stored state. + repo.update(updated, updated); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/utils/VisibilitySimplifierTest.java ---------------------------------------------------------------------- diff --git a/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/utils/VisibilitySimplifierTest.java b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/utils/VisibilitySimplifierTest.java new file mode 100644 index 0000000..3d22e2f --- /dev/null +++ b/dao/accumulo.rya/src/test/java/org/apache/rya/accumulo/utils/VisibilitySimplifierTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 mvm.rya.accumulo.utils; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Tests the methods of {@link VisibilitySimplifier}. + */ +public class VisibilitySimplifierTest { + + @Test + public void noneRequired() { + final String simplified = new VisibilitySimplifier().simplify("u"); + assertEquals("u", simplified); + } + + @Test + public void parenthesis() { + final String simplified = new VisibilitySimplifier().simplify("(u&u)&u"); + assertEquals("u", simplified); + } + + @Test + public void manyAnds() { + final String simplified = new VisibilitySimplifier().simplify("u&u&u"); + assertEquals("u", simplified); + } + + @Test + public void complex() { + final String simplified = new VisibilitySimplifier().simplify("(a|b)|(a|b)|a|b"); + assertEquals("a|b", simplified); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoConnectorFactory.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoConnectorFactory.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoConnectorFactory.java deleted file mode 100644 index 77a9f16..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoConnectorFactory.java +++ /dev/null @@ -1,138 +0,0 @@ -package mvm.rya.mongodb; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.net.UnknownHostException; -import java.util.Arrays; -import java.io.IOException; - -import org.apache.commons.configuration.ConfigurationRuntimeException; -import org.apache.hadoop.conf.Configuration; - -import com.mongodb.MongoClient; -import com.mongodb.MongoCredential; -import com.mongodb.MongoException; -import com.mongodb.ServerAddress; - -import de.flapdoodle.embed.mongo.distribution.Version; -import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; - -/** - * Mongo convention generally allows for a single instance of a {@link MongoClient} - * throughout the life cycle of an application. This MongoConnectorFactory lazy - * loads a Mongo Client and uses the same one whenever {@link MongoConnectorFactory#getMongoClient(Configuration)} - * is invoked. - */ -public class MongoConnectorFactory { - private static MongoClient mongoClient; - - private final static String MSG_INTRO = "Failed to connect to MongoDB: "; - - /** - * @param conf The {@link Configuration} defining how to construct the MongoClient. - * @return A {@link MongoClient}. This client is lazy loaded and the same one - * is used throughout the lifecycle of the application. - * @throws IOException - if MongodForTestsFactory constructor has an io exception. - * @throws ConfigurationRuntimeException - Thrown if the configured server, port, user, or others are missing. - * @throws MongoException if can't connect despite conf parameters are given - */ - public static synchronized MongoClient getMongoClient(final Configuration conf) - throws ConfigurationRuntimeException, MongoException { - if (mongoClient == null) { - // The static client has not yet created, is it a test/mock instance, or a service? - if (conf.getBoolean(MongoDBRdfConfiguration.USE_TEST_MONGO, false)) { - createMongoClientForTests(); - } else { - createMongoClientForServer(conf); - } - } - return mongoClient; - } - - /** - * Create a local temporary MongoDB instance and client object and assign it to this class's static mongoClient - * @throws MongoException if can't connect - */ - private static void createMongoClientForTests() throws MongoException { - try { - MongodForTestsFactory testsFactory = MongodForTestsFactory.with(Version.Main.PRODUCTION); - mongoClient = testsFactory.newMongo(); - } catch (IOException e) { - // Rethrow as an unchecked error. Since we are in a test mode here, just fail fast. - throw new MongoException(MSG_INTRO+"creating a factory for a test/mock MongoDB instance.",e); - } - } - - /** - * Create a MongoDB client object and assign it to this class's static mongoClient - * @param conf configuration containing connection parameters - * @throws ConfigurationRuntimeException - Thrown if the configured server, port, user, or others are missing. - * @throws MongoException if can't connect despite conf parameters are given - */ - private static void createMongoClientForServer(final Configuration conf) - throws ConfigurationRuntimeException, MongoException { - // Connect to a running Mongo server - final String host = requireNonNull(conf.get(MongoDBRdfConfiguration.MONGO_INSTANCE), MSG_INTRO+"host name is required"); - final int port = requireNonNullInt(conf.get(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT), MSG_INTRO+"Port number is required."); - ServerAddress server = new ServerAddress(host, port); - // check for authentication credentials - if (conf.get(MongoDBRdfConfiguration.MONGO_USER) != null) { - final String username = conf.get(MongoDBRdfConfiguration.MONGO_USER); - final String dbName = requireNonNull(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME), - MSG_INTRO + MongoDBRdfConfiguration.MONGO_DB_NAME + " is null but required configuration if " - + MongoDBRdfConfiguration.MONGO_USER + " is configured."); - final char[] pswd = requireNonNull(conf.get(MongoDBRdfConfiguration.MONGO_USER_PASSWORD), - MSG_INTRO + MongoDBRdfConfiguration.MONGO_USER_PASSWORD + " is null but required configuration if " - + MongoDBRdfConfiguration.MONGO_USER + " is configured.").toCharArray(); - final MongoCredential cred = MongoCredential.createCredential(username, dbName, pswd); - mongoClient = new MongoClient(server, Arrays.asList(cred)); - } else { - // No user was configured: - mongoClient = new MongoClient(server); - } - } - - /** - * Throw exception for un-configured required values. - * - * @param required String to check - * @param message throw configuration exception with this description - * @return unaltered required string - * @throws ConfigurationRuntimeException if required is null - */ - private static String requireNonNull(String required, String message) throws ConfigurationRuntimeException { - if (required == null) - throw new ConfigurationRuntimeException(message); - return required; - } - - /* - * Same as above, check that it is a integer and return the parsed integer. - */ - private static int requireNonNullInt(String required, String message) throws ConfigurationRuntimeException { - if (required == null) - throw new ConfigurationRuntimeException(message); - try { - return Integer.parseInt(required); - } catch (NumberFormatException e) { - throw new ConfigurationRuntimeException(message); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBQueryEngine.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBQueryEngine.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBQueryEngine.java deleted file mode 100644 index afa0a77..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBQueryEngine.java +++ /dev/null @@ -1,202 +0,0 @@ -package mvm.rya.mongodb; -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.io.Closeable; -import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import org.calrissian.mango.collect.CloseableIterable; -import org.openrdf.query.BindingSet; - -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; -import com.mongodb.MongoClient; - -import info.aduna.iteration.CloseableIteration; -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.persist.RyaDAOException; -import mvm.rya.api.persist.query.BatchRyaQuery; -import mvm.rya.api.persist.query.RyaQuery; -import mvm.rya.api.persist.query.RyaQueryEngine; -import mvm.rya.mongodb.dao.MongoDBStorageStrategy; -import mvm.rya.mongodb.dao.SimpleMongoDBStorageStrategy; -import mvm.rya.mongodb.iter.NonCloseableRyaStatementCursorIterator; -import mvm.rya.mongodb.iter.RyaStatementBindingSetCursorIterator; -import mvm.rya.mongodb.iter.RyaStatementCursorIterable; -import mvm.rya.mongodb.iter.RyaStatementCursorIterator; - -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; - -/** - * Date: 7/17/12 - * Time: 9:28 AM - */ -public class MongoDBQueryEngine implements RyaQueryEngine<MongoDBRdfConfiguration>, Closeable { - - private MongoDBRdfConfiguration configuration; - private final MongoClient mongoClient; - private final DBCollection coll; - private final MongoDBStorageStrategy strategy; - - public MongoDBQueryEngine(final MongoDBRdfConfiguration conf, final MongoClient mongoClient) { - this.mongoClient = checkNotNull(mongoClient); - final DB db = mongoClient.getDB( conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - coll = db.getCollection(conf.getTriplesCollectionName()); - strategy = new SimpleMongoDBStorageStrategy(); - } - - - @Override - public void setConf(final MongoDBRdfConfiguration conf) { - configuration = conf; - } - - @Override - public MongoDBRdfConfiguration getConf() { - return configuration; - } - - @Override - public CloseableIteration<RyaStatement, RyaDAOException> query( - final RyaStatement stmt, MongoDBRdfConfiguration conf) - throws RyaDAOException { - if (conf == null) { - conf = configuration; - } - final Long maxResults = conf.getLimit(); - final Set<DBObject> queries = new HashSet<DBObject>(); - final DBObject query = strategy.getQuery(stmt); - queries.add(query); - final RyaStatementCursorIterator iterator = new RyaStatementCursorIterator(coll, queries, strategy); - - if (maxResults != null) { - iterator.setMaxResults(maxResults); - } - return iterator; - } - @Override - public CloseableIteration<? extends Entry<RyaStatement, BindingSet>, RyaDAOException> queryWithBindingSet( - final Collection<Entry<RyaStatement, BindingSet>> stmts, - MongoDBRdfConfiguration conf) throws RyaDAOException { - if (conf == null) { - conf = configuration; - } - final Long maxResults = conf.getLimit(); - final Multimap<DBObject, BindingSet> rangeMap = HashMultimap.create(); - - //TODO: cannot span multiple tables here - try { - for (final Map.Entry<RyaStatement, BindingSet> stmtbs : stmts) { - final RyaStatement stmt = stmtbs.getKey(); - final BindingSet bs = stmtbs.getValue(); - final DBObject query = strategy.getQuery(stmt); - rangeMap.put(query, bs); - } - - // TODO not sure what to do about regex ranges? - final RyaStatementBindingSetCursorIterator iterator = new RyaStatementBindingSetCursorIterator(coll, rangeMap, strategy); - - if (maxResults != null) { - iterator.setMaxResults(maxResults); - } - return iterator; - } catch (final Exception e) { - throw new RyaDAOException(e); - } - - } - @Override - public CloseableIteration<RyaStatement, RyaDAOException> batchQuery( - final Collection<RyaStatement> stmts, MongoDBRdfConfiguration conf) - throws RyaDAOException { - if (conf == null) { - conf = configuration; - } - final Long maxResults = conf.getLimit(); - final Set<DBObject> queries = new HashSet<DBObject>(); - - try { - for (final RyaStatement stmt : stmts) { - queries.add( strategy.getQuery(stmt)); - } - - // TODO not sure what to do about regex ranges? - final RyaStatementCursorIterator iterator = new RyaStatementCursorIterator(coll, queries, strategy); - - if (maxResults != null) { - iterator.setMaxResults(maxResults); - } - return iterator; - } catch (final Exception e) { - throw new RyaDAOException(e); - } - - } - @Override - public CloseableIterable<RyaStatement> query(final RyaQuery ryaQuery) - throws RyaDAOException { - final Set<DBObject> queries = new HashSet<DBObject>(); - - try { - queries.add( strategy.getQuery(ryaQuery)); - - // TODO not sure what to do about regex ranges? - // TODO this is gross - final RyaStatementCursorIterable iterator = new RyaStatementCursorIterable(new NonCloseableRyaStatementCursorIterator(new RyaStatementCursorIterator(coll, queries, strategy))); - - return iterator; - } catch (final Exception e) { - throw new RyaDAOException(e); - } - } - @Override - public CloseableIterable<RyaStatement> query(final BatchRyaQuery batchRyaQuery) - throws RyaDAOException { - try { - final Set<DBObject> queries = new HashSet<DBObject>(); - for (final RyaStatement statement : batchRyaQuery.getQueries()){ - queries.add( strategy.getQuery(statement)); - - } - - // TODO not sure what to do about regex ranges? - // TODO this is gross - final RyaStatementCursorIterable iterator = new RyaStatementCursorIterable(new NonCloseableRyaStatementCursorIterator(new RyaStatementCursorIterator(coll, queries, strategy))); - - return iterator; - } catch (final Exception e) { - throw new RyaDAOException(e); - } - } - - @Override - public void close() throws IOException { - if (mongoClient != null){ mongoClient.close(); } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRdfConfiguration.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRdfConfiguration.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRdfConfiguration.java deleted file mode 100644 index e8e301d..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRdfConfiguration.java +++ /dev/null @@ -1,128 +0,0 @@ -package mvm.rya.mongodb; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - - - -import java.util.List; - -import mvm.rya.api.RdfCloudTripleStoreConfiguration; -import mvm.rya.api.persist.index.RyaSecondaryIndexer; - -import org.apache.hadoop.conf.Configuration; - -import com.google.common.collect.Lists; -import com.mongodb.MongoClient; - -public class MongoDBRdfConfiguration extends RdfCloudTripleStoreConfiguration { - public static final String MONGO_INSTANCE = "mongo.db.instance"; - public static final String MONGO_INSTANCE_PORT = "mongo.db.port"; - public static final String MONGO_GEO_MAXDISTANCE = "mongo.geo.maxdist"; - public static final String MONGO_DB_NAME = "mongo.db.name"; - public static final String MONGO_COLLECTION_PREFIX = "mongo.db.collectionprefix"; - public static final String MONGO_USER = "mongo.db.user"; - public static final String MONGO_USER_PASSWORD = "mongo.db.userpassword"; - public static final String USE_TEST_MONGO = "mongo.db.test"; - public static final String CONF_ADDITIONAL_INDEXERS = "ac.additional.indexers"; - private MongoClient mongoClient; - - public MongoDBRdfConfiguration() { - super(); - } - - public MongoDBRdfConfiguration(Configuration other) { - super(other); - } - - @Override - public MongoDBRdfConfiguration clone() { - return new MongoDBRdfConfiguration(this); - } - - public boolean getUseTestMongo() { - return this.getBoolean(USE_TEST_MONGO, false); - } - - public void setUseTestMongo(boolean useTestMongo) { - this.setBoolean(USE_TEST_MONGO, useTestMongo); - } - - public String getTriplesCollectionName() { - return this.get(MONGO_COLLECTION_PREFIX, "rya") + "_triples"; - } - - public String getCollectionName() { - return this.get(MONGO_COLLECTION_PREFIX, "rya"); - } - - public void setCollectionName(String name) { - this.set(MONGO_COLLECTION_PREFIX, name); - } - - public String getMongoInstance() { - return this.get(MONGO_INSTANCE, "localhost"); - } - - public void setMongoInstance(String name) { - this.set(MONGO_INSTANCE, name); - } - - public String getMongoPort() { - return this.get(MONGO_INSTANCE_PORT, "27017"); - } - - public void setMongoPort(String name) { - this.set(MONGO_INSTANCE_PORT, name); - } - - public String getMongoDBName() { - return this.get(MONGO_DB_NAME, "rya"); - } - - public void setMongoDBName(String name) { - this.set(MONGO_DB_NAME, name); - } - - public String getNameSpacesCollectionName() { - return this.get(MONGO_COLLECTION_PREFIX, "rya") + "_ns"; - } - - public void setAdditionalIndexers(Class<? extends MongoSecondaryIndex>... indexers) { - List<String> strs = Lists.newArrayList(); - for (Class<?> ai : indexers){ - strs.add(ai.getName()); - } - - setStrings(CONF_ADDITIONAL_INDEXERS, strs.toArray(new String[]{})); - } - - public List<MongoSecondaryIndex> getAdditionalIndexers() { - return getInstances(CONF_ADDITIONAL_INDEXERS, MongoSecondaryIndex.class); - } - - public void setMongoClient(MongoClient client){ - this.mongoClient = client; - } - - public MongoClient getMongoClient() { - return mongoClient; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java deleted file mode 100644 index bb5d58e..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java +++ /dev/null @@ -1,233 +0,0 @@ -package mvm.rya.mongodb; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - - -import java.io.IOException; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import org.apache.commons.io.IOUtils; -import org.apache.log4j.Logger; - -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; -import com.mongodb.DuplicateKeyException; -import com.mongodb.InsertOptions; -import com.mongodb.MongoClient; - -import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; -import mvm.rya.api.RdfCloudTripleStoreConfiguration; -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.domain.RyaURI; -import mvm.rya.api.persist.RyaDAO; -import mvm.rya.api.persist.RyaDAOException; -import mvm.rya.api.persist.RyaNamespaceManager; -import mvm.rya.api.persist.index.RyaSecondaryIndexer; -import mvm.rya.api.persist.query.RyaQueryEngine; -import mvm.rya.mongodb.dao.MongoDBNamespaceManager; -import mvm.rya.mongodb.dao.MongoDBStorageStrategy; -import mvm.rya.mongodb.dao.SimpleMongoDBNamespaceManager; -import mvm.rya.mongodb.dao.SimpleMongoDBStorageStrategy; - -/** - * Default DAO for mongo backed RYA allowing for CRUD operations. - */ -public final class MongoDBRyaDAO implements RyaDAO<MongoDBRdfConfiguration>{ - private static final Logger log = Logger.getLogger(MongoDBRyaDAO.class); - - private MongoDBRdfConfiguration conf; - private MongoClient mongoClient; - private DB db; - private DBCollection coll; - private MongoDBQueryEngine queryEngine; - private MongoDBStorageStrategy storageStrategy; - private MongoDBNamespaceManager nameSpaceManager; - private MongodForTestsFactory testsFactory; - - private List<MongoSecondaryIndex> secondaryIndexers; - - /** - * Creates a new {@link MongoDBRyaDAO} - * @param conf - * @throws RyaDAOException - */ - public MongoDBRyaDAO(final MongoDBRdfConfiguration conf) throws RyaDAOException, NumberFormatException, UnknownHostException { - this.conf = conf; - mongoClient = MongoConnectorFactory.getMongoClient(conf); - conf.setMongoClient(mongoClient); - init(); - } - - - public MongoDBRyaDAO(final MongoDBRdfConfiguration conf, final MongoClient mongoClient) throws RyaDAOException{ - this.conf = conf; - this.mongoClient = mongoClient; - conf.setMongoClient(mongoClient); - init(); - } - - @Override - public void setConf(final MongoDBRdfConfiguration conf) { - this.conf = conf; - } - - public MongoClient getMongoClient(){ - return mongoClient; - } - - public void setDB(final DB db) { - this.db = db; - } - - - public void setDBCollection(final DBCollection coll) { - this.coll = coll; - } - - @Override - public MongoDBRdfConfiguration getConf() { - return conf; - } - - @Override - public void init() throws RyaDAOException { - secondaryIndexers = conf.getAdditionalIndexers(); - for(final MongoSecondaryIndex index: secondaryIndexers) { - index.setConf(conf); - index.setClient(mongoClient); - } - - db = mongoClient.getDB(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - coll = db.getCollection(conf.getTriplesCollectionName()); - nameSpaceManager = new SimpleMongoDBNamespaceManager(db.getCollection(conf.getNameSpacesCollectionName())); - queryEngine = new MongoDBQueryEngine(conf, mongoClient); - storageStrategy = new SimpleMongoDBStorageStrategy(); - storageStrategy.createIndices(coll); - for(final MongoSecondaryIndex index: secondaryIndexers) { - index.init(); - } - } - - @Override - public boolean isInitialized() throws RyaDAOException { - return true; - } - - @Override - public void destroy() throws RyaDAOException { - if (mongoClient != null) { - mongoClient.close(); - } - if (conf.getUseTestMongo()) { - testsFactory.shutdown(); - } - - IOUtils.closeQuietly(queryEngine); - } - - @Override - public void add(final RyaStatement statement) throws RyaDAOException { - // add it to the collection - try { - coll.insert(storageStrategy.serialize(statement)); - for(final RyaSecondaryIndexer index: secondaryIndexers) { - index.storeStatement(statement); - } - } catch (IOException e) { - log.error("Unable to add: " + statement.toString()); - throw new RyaDAOException(e); - } - catch (DuplicateKeyException e){ - log.error("Attempting to load duplicate triple: " + statement.toString()); - } - } - - @Override - public void add(final Iterator<RyaStatement> statement) throws RyaDAOException { - final List<DBObject> dbInserts = new ArrayList<DBObject>(); - while (statement.hasNext()){ - final RyaStatement ryaStatement = statement.next(); - final DBObject insert = storageStrategy.serialize(ryaStatement); - dbInserts.add(insert); - - try { - for (final RyaSecondaryIndexer index : secondaryIndexers) { - index.storeStatement(ryaStatement); - } - } catch (final IOException e) { - log.error("Failed to add: " + ryaStatement.toString() + " to the indexer"); - } - - } - coll.insert(dbInserts, new InsertOptions().continueOnError(true)); - } - - @Override - public void delete(final RyaStatement statement, final MongoDBRdfConfiguration conf) - throws RyaDAOException { - final DBObject obj = storageStrategy.getQuery(statement); - coll.remove(obj); - } - - @Override - public void dropGraph(final MongoDBRdfConfiguration conf, final RyaURI... graphs) - throws RyaDAOException { - - } - - @Override - public void delete(final Iterator<RyaStatement> statements, - final MongoDBRdfConfiguration conf) throws RyaDAOException { - while (statements.hasNext()){ - final RyaStatement ryaStatement = statements.next(); - coll.remove(storageStrategy.getQuery(ryaStatement)); - } - - } - - @Override - public String getVersion() throws RyaDAOException { - return "1.0"; - } - - @Override - public RyaQueryEngine<MongoDBRdfConfiguration> getQueryEngine() { - return queryEngine; - } - - @Override - public RyaNamespaceManager<MongoDBRdfConfiguration> getNamespaceManager() { - return nameSpaceManager; - } - - @Override - public void purge(final RdfCloudTripleStoreConfiguration configuration) { - - } - - @Override - public void dropAndDestroy() throws RyaDAOException { - db.dropDatabase(); // this is dangerous! - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoSecondaryIndex.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoSecondaryIndex.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoSecondaryIndex.java deleted file mode 100644 index e32216f..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoSecondaryIndex.java +++ /dev/null @@ -1,31 +0,0 @@ -package mvm.rya.mongodb; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import com.mongodb.MongoClient; - -import mvm.rya.api.persist.index.RyaSecondaryIndexer; - -public interface MongoSecondaryIndex extends RyaSecondaryIndexer{ - public void init(); - - public void setClient(MongoClient client); - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBNamespaceManager.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBNamespaceManager.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBNamespaceManager.java deleted file mode 100644 index fd9b659..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBNamespaceManager.java +++ /dev/null @@ -1,35 +0,0 @@ -package mvm.rya.mongodb.dao; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - - -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.persist.RyaNamespaceManager; -import mvm.rya.api.persist.query.RyaQuery; -import mvm.rya.mongodb.MongoDBRdfConfiguration; - -import com.mongodb.DBCollection; -import com.mongodb.DBObject; - -public interface MongoDBNamespaceManager extends RyaNamespaceManager<MongoDBRdfConfiguration>{ - - public void createIndices(DBCollection coll); - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBStorageStrategy.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBStorageStrategy.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBStorageStrategy.java deleted file mode 100644 index 5ae371b..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/MongoDBStorageStrategy.java +++ /dev/null @@ -1,45 +0,0 @@ -package mvm.rya.mongodb.dao; - -import com.mongodb.DBCollection; -import com.mongodb.DBObject; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - - -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.persist.query.RyaQuery; - -/** - * Defines how objects are stored in MongoDB. - * <T> - The object to store in MongoDB - */ -public interface MongoDBStorageStrategy<T> { - - public DBObject getQuery(T statement); - - public RyaStatement deserializeDBObject(DBObject queryResult); - - public DBObject serialize(T statement); - - public DBObject getQuery(RyaQuery ryaQuery); - - public void createIndices(DBCollection coll); - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBNamespaceManager.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBNamespaceManager.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBNamespaceManager.java deleted file mode 100644 index 259420b..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBNamespaceManager.java +++ /dev/null @@ -1,181 +0,0 @@ -package mvm.rya.mongodb.dao; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - - -import info.aduna.iteration.CloseableIteration; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Map; - -import mvm.rya.api.persist.RyaDAOException; -import mvm.rya.mongodb.MongoDBRdfConfiguration; - -import org.apache.commons.codec.binary.Hex; -import org.openrdf.model.Namespace; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBCollection; -import com.mongodb.DBCursor; -import com.mongodb.DBObject; - -public class SimpleMongoDBNamespaceManager implements MongoDBNamespaceManager { - - public class NamespaceImplementation implements Namespace { - - private String namespace; - private String prefix; - - public NamespaceImplementation(String namespace, String prefix) { - this.namespace = namespace; - this.prefix = prefix; - } - - @Override - public int compareTo(Namespace o) { - if (!namespace.equalsIgnoreCase(o.getName())) return namespace.compareTo(o.getName()); - if (!prefix.equalsIgnoreCase(o.getPrefix())) return prefix.compareTo(o.getPrefix()); - return 0; - } - - @Override - public String getName() { - return namespace; - } - - @Override - public String getPrefix() { - return prefix; - } - - } - - public class MongoCursorIteration implements - CloseableIteration<Namespace, RyaDAOException> { - private DBCursor cursor; - - public MongoCursorIteration(DBCursor cursor2) { - this.cursor = cursor2; - } - - @Override - public boolean hasNext() throws RyaDAOException { - return cursor.hasNext(); - } - - @Override - public Namespace next() throws RyaDAOException { - DBObject ns = cursor.next(); - Map values = ns.toMap(); - String namespace = (String) values.get(NAMESPACE); - String prefix = (String) values.get(PREFIX); - - Namespace temp = new NamespaceImplementation(namespace, prefix); - return temp; - } - - @Override - public void remove() throws RyaDAOException { - next(); - } - - @Override - public void close() throws RyaDAOException { - cursor.close(); - } - - } - - private static final String ID = "_id"; - private static final String PREFIX = "prefix"; - private static final String NAMESPACE = "namespace"; - private MongoDBRdfConfiguration conf; - private DBCollection nsColl; - - - public SimpleMongoDBNamespaceManager(DBCollection nameSpaceCollection) { - nsColl = nameSpaceCollection; - } - - @Override - public void createIndices(DBCollection coll){ - coll.createIndex(PREFIX); - coll.createIndex(NAMESPACE); - } - - - @Override - public void setConf(MongoDBRdfConfiguration paramC) { - this.conf = paramC; - } - - @Override - public MongoDBRdfConfiguration getConf() { - // TODO Auto-generated method stub - return conf; - } - - @Override - public void addNamespace(String prefix, String namespace) - throws RyaDAOException { - String id = prefix; - byte[] bytes = id.getBytes(); - try { - MessageDigest digest = MessageDigest.getInstance("SHA-1"); - bytes = digest.digest(bytes); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - BasicDBObject doc = new BasicDBObject(ID, new String(Hex.encodeHex(bytes))) - .append(PREFIX, prefix) - .append(NAMESPACE, namespace); - nsColl.insert(doc); - - } - - @Override - public String getNamespace(String prefix) throws RyaDAOException { - DBObject query = new BasicDBObject().append(PREFIX, prefix); - DBCursor cursor = nsColl.find(query); - String nameSpace = prefix; - while (cursor.hasNext()){ - DBObject obj = cursor.next(); - nameSpace = (String) obj.toMap().get(NAMESPACE); - } - return nameSpace; - } - - @Override - public void removeNamespace(String prefix) throws RyaDAOException { - DBObject query = new BasicDBObject().append(PREFIX, prefix); - nsColl.remove(query); - } - - @Override - public CloseableIteration<? extends Namespace, RyaDAOException> iterateNamespace() - throws RyaDAOException { - DBObject query = new BasicDBObject(); - DBCursor cursor = nsColl.find(query); - return new MongoCursorIteration(cursor); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java deleted file mode 100644 index d09316a..0000000 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java +++ /dev/null @@ -1,162 +0,0 @@ -package mvm.rya.mongodb.dao; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import static org.openrdf.model.vocabulary.XMLSchema.ANYURI; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Map; - -import org.apache.commons.codec.binary.Hex; -import org.apache.log4j.Logger; -import org.openrdf.model.impl.ValueFactoryImpl; -import org.openrdf.model.vocabulary.XMLSchema; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; - -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.domain.RyaType; -import mvm.rya.api.domain.RyaURI; -import mvm.rya.api.persist.query.RyaQuery; - -/** - * Defines how {@link RyaStatement}s are stored in MongoDB. - */ -public class SimpleMongoDBStorageStrategy implements MongoDBStorageStrategy<RyaStatement> { - private static final Logger LOG = Logger.getLogger(SimpleMongoDBStorageStrategy.class); - protected static final String ID = "_id"; - protected static final String OBJECT_TYPE = "objectType"; - protected static final String OBJECT_TYPE_VALUE = XMLSchema.ANYURI.stringValue(); - protected static final String CONTEXT = "context"; - protected static final String PREDICATE = "predicate"; - protected static final String OBJECT = "object"; - protected static final String SUBJECT = "subject"; - public static final String TIMESTAMP = "insertTimestamp"; - protected ValueFactoryImpl factory = new ValueFactoryImpl(); - - @Override - public void createIndices(final DBCollection coll){ - BasicDBObject doc = new BasicDBObject(); - doc.put(SUBJECT, 1); - doc.put(PREDICATE, 1); - coll.createIndex(doc); - doc = new BasicDBObject(PREDICATE, 1); - doc.put(OBJECT, 1); - doc.put(OBJECT_TYPE, 1); - coll.createIndex(doc); - doc = new BasicDBObject(OBJECT, 1); - doc = new BasicDBObject(OBJECT_TYPE, 1); - doc.put(SUBJECT, 1); - coll.createIndex(doc); - } - - @Override - public DBObject getQuery(final RyaStatement stmt) { - final RyaURI subject = stmt.getSubject(); - final RyaURI predicate = stmt.getPredicate(); - final RyaType object = stmt.getObject(); - final RyaURI context = stmt.getContext(); - final BasicDBObject query = new BasicDBObject(); - if (subject != null){ - query.append(SUBJECT, subject.getData()); - } - if (object != null){ - query.append(OBJECT, object.getData()); - query.append(OBJECT_TYPE, object.getDataType().toString()); - } - if (predicate != null){ - query.append(PREDICATE, predicate.getData()); - } - if (context != null){ - query.append(CONTEXT, context.getData()); - } - - return query; - } - - @Override - public RyaStatement deserializeDBObject(final DBObject queryResult) { - final Map result = queryResult.toMap(); - final String subject = (String) result.get(SUBJECT); - final String object = (String) result.get(OBJECT); - final String objectType = (String) result.get(OBJECT_TYPE); - final String predicate = (String) result.get(PREDICATE); - final String context = (String) result.get(CONTEXT); - final Long timestamp = (Long) result.get(TIMESTAMP); - RyaType objectRya = null; - if (objectType.equalsIgnoreCase(ANYURI.stringValue())){ - objectRya = new RyaURI(object); - } - else { - objectRya = new RyaType(factory.createURI(objectType), object); - } - - final RyaStatement statement; - if (!context.isEmpty()){ - statement = new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya, - new RyaURI(context)); - } else { - statement = new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya); - } - - if(timestamp != null) { - statement.setTimestamp(timestamp); - } - return statement; - } - - @Override - public DBObject serialize(final RyaStatement statement){ - return serializeInternal(statement); - } - - public BasicDBObject serializeInternal(final RyaStatement statement){ - String context = ""; - if (statement.getContext() != null){ - context = statement.getContext().getData(); - } - final String id = statement.getSubject().getData() + " " + - statement.getPredicate().getData() + " " + statement.getObject().getData() + " " + context; - byte[] bytes = id.getBytes(); - try { - final MessageDigest digest = MessageDigest.getInstance("SHA-1"); - bytes = digest.digest(bytes); - } catch (final NoSuchAlgorithmException e) { - LOG.error("Unable to perform SHA-1 on the ID, defaulting to raw bytes.", e); - } - final BasicDBObject doc = new BasicDBObject(ID, new String(Hex.encodeHex(bytes))) - .append(SUBJECT, statement.getSubject().getData()) - .append(PREDICATE, statement.getPredicate().getData()) - .append(OBJECT, statement.getObject().getData()) - .append(OBJECT_TYPE, statement.getObject().getDataType().toString()) - .append(CONTEXT, context) - .append(TIMESTAMP, statement.getTimestamp()); - return doc; - - } - - @Override - public DBObject getQuery(final RyaQuery ryaQuery) { - return getQuery(ryaQuery.getQuery()); - } -}
