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=========================================================
21 package org.onap.aai.serialization.engines;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
32 import org.janusgraph.core.JanusGraph;
33 import org.onap.aai.dbmap.DBConnectionType;
34 import org.onap.aai.introspection.Loader;
35 import org.onap.aai.query.builder.*;
36 import org.onap.aai.serialization.db.InMemoryGraphSingleton;
37 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
38 import org.onap.aai.serialization.engines.query.QueryEngine;
40 public class InMemoryDBEngine extends TransactionalGraphEngine {
43 * Instantiates a new JanusGraph DB engine.
50 private JanusGraph graph = null;
52 private static final TransactionalGraphEngine.Admin admin = null;
54 public InMemoryDBEngine(QueryStyle style, DBConnectionType connectionType, Loader loader, JanusGraph graph) {
55 super(style, loader, connectionType, InMemoryGraphSingleton.getInstance(graph));
60 * Instantiates a new JanusGraph DB engine.
69 public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, JanusGraph graph) {
72 this.singleton = InMemoryGraphSingleton.getInstance(graph);
78 public QueryEngine getQueryEngine() {
80 if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
82 GraphTraversalSource traversalSource = graph.traversal();
83 return new GraphTraversalQueryEngine(traversalSource);
86 throw new IllegalArgumentException("Query Engine type not recognized");
92 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
93 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
94 return new GremlinTraversal<>(loader, graph.traversal());
95 } else if (style.equals(QueryStyle.TRAVERSAL)) {
96 return new TraversalQuery<>(loader, graph.traversal());
97 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
98 return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
100 throw new IllegalArgumentException("Query Builder type is Not recognized");
109 public boolean setListProperty(Vertex v, String name, List<?> objs) {
111 // clear out list full replace style
113 Iterator<VertexProperty<Object>> iterator = v.properties(name);
114 while (iterator.hasNext()) {
115 iterator.next().remove();
118 for (Object obj : objs) {
119 v.property(name, obj);
129 public List<Object> getListProperty(Vertex v, String name) {
131 List<Object> result = new ArrayList<>();
133 Iterator<VertexProperty<Object>> iterator = v.properties(name);
135 while (iterator.hasNext()) {
136 result.add(iterator.next().value());
139 if (result.isEmpty()) {
148 public QueryBuilder<Vertex> getQueryBuilder() {
149 return getQueryBuilder(this.loader);
153 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
154 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
155 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
156 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
157 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
158 } else if (style.equals(QueryStyle.TRAVERSAL)) {
159 return new TraversalQuery<>(loader, graph.traversal());
160 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
161 return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
163 throw new IllegalArgumentException("Query Builder type not recognized");
169 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
170 return getQueryBuilder(this.loader, start);
173 public GraphTraversalSource getTraversalSource() {
174 return graph.traversal();
178 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
179 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
180 return new GremlinTraversal<>(loader, graph.traversal(), start);
181 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
182 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
183 } else if (style.equals(QueryStyle.TRAVERSAL)) {
184 return new TraversalQuery<>(loader, graph.traversal(), start);
185 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
186 return new TraversalURIOptimizedQuery<>(loader, graph.traversal(), start);
188 throw new IllegalArgumentException("Query Builder type not recognized");
194 public Graph startTransaction() {
195 if (this.tx() == null) {
196 this.currentTx = graph.newTransaction();
197 this.currentTraversal = this.tx().traversal();
198 this.readOnlyTraversal =
199 this.tx().traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));