2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.serialization.engines;
22 import org.janusgraph.core.JanusGraph;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
28 import org.onap.aai.dbmap.DBConnectionType;
29 import org.onap.aai.introspection.Loader;
30 import org.onap.aai.query.builder.*;
31 import org.onap.aai.serialization.db.InMemoryGraphSingleton;
32 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
33 import org.onap.aai.serialization.engines.query.QueryEngine;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
39 public class InMemoryDBEngine extends TransactionalGraphEngine {
42 * Instantiates a new JanusGraph DB engine.
49 private JanusGraph graph = null;
51 private static final TransactionalGraphEngine.Admin admin = null;
53 public InMemoryDBEngine(QueryStyle style, DBConnectionType connectionType, Loader loader, JanusGraph graph) {
54 super(style, loader, connectionType, InMemoryGraphSingleton.getInstance(graph));
59 * Instantiates a new JanusGraph DB engine.
68 public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, JanusGraph graph) {
71 this.singleton = InMemoryGraphSingleton.getInstance(graph);
77 public QueryEngine getQueryEngine() {
79 if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
81 GraphTraversalSource traversalSource = graph.traversal();
82 return new GraphTraversalQueryEngine(traversalSource);
85 throw new IllegalArgumentException("Query Engine type not recognized");
91 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
92 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
93 return new GremlinTraversal<>(loader, graph.traversal());
94 } else if (style.equals(QueryStyle.TRAVERSAL)) {
95 return new TraversalQuery<>(loader, graph.traversal());
96 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
97 return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
99 throw new IllegalArgumentException("Query Builder type is Not recognized");
108 public boolean setListProperty(Vertex v, String name, List<?> objs) {
110 // clear out list full replace style
112 Iterator<VertexProperty<Object>> iterator = v.properties(name);
113 while (iterator.hasNext()) {
114 iterator.next().remove();
117 for (Object obj : objs) {
118 v.property(name, obj);
128 public List<Object> getListProperty(Vertex v, String name) {
130 List<Object> result = new ArrayList<>();
132 Iterator<VertexProperty<Object>> iterator = v.properties(name);
134 while (iterator.hasNext()) {
135 result.add(iterator.next().value());
138 if (result.isEmpty()) {
147 public QueryBuilder<Vertex> getQueryBuilder() {
148 return getQueryBuilder(this.loader);
152 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
153 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
154 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
155 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
156 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
157 } else if (style.equals(QueryStyle.TRAVERSAL)) {
158 return new TraversalQuery<>(loader, graph.traversal());
159 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
160 return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
162 throw new IllegalArgumentException("Query Builder type not recognized");
168 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
169 return getQueryBuilder(this.loader, start);
172 public GraphTraversalSource getTraversalSource() {
173 return graph.traversal();
177 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
178 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
179 return new GremlinTraversal<>(loader, graph.traversal(), start);
180 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
181 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
182 } else if (style.equals(QueryStyle.TRAVERSAL)) {
183 return new TraversalQuery<>(loader, graph.traversal(), start);
184 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
185 return new TraversalURIOptimizedQuery<>(loader, graph.traversal(), start);
187 throw new IllegalArgumentException("Query Builder type not recognized");
193 public Graph startTransaction() {
194 if (this.tx() == null) {
195 this.currentTx = graph.newTransaction();
196 this.currentTraversal = this.tx().traversal();
197 this.readOnlyTraversal = this.tx()
198 .traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));