Skip to content

Commit b5c4948

Browse files
committed
Makes a default implementation of document collection on orientdb
1 parent 383ab12 commit b5c4948

File tree

1 file changed

+185
-4
lines changed

1 file changed

+185
-4
lines changed
Lines changed: 185 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,188 @@
1+
/*
2+
* Copyright (c) 2017 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
115
package org.jnosql.diana.orientdb.document;
216

3-
/**
4-
* Created by otaviojava on 6/23/17.
5-
*/
6-
public class DefaultOrientDBDocumentCollectionManager {
17+
18+
import com.orientechnologies.common.concur.ONeedRetryException;
19+
import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
20+
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
21+
import com.orientechnologies.orient.core.id.ORecordId;
22+
import com.orientechnologies.orient.core.record.impl.ODocument;
23+
import com.orientechnologies.orient.core.sql.query.OLiveQuery;
24+
import org.apache.commons.collections.map.HashedMap;
25+
import org.jnosql.diana.api.document.Document;
26+
import org.jnosql.diana.api.document.DocumentDeleteQuery;
27+
import org.jnosql.diana.api.document.DocumentEntity;
28+
import org.jnosql.diana.api.document.DocumentQuery;
29+
import org.jnosql.diana.driver.ValueUtil;
30+
31+
import java.time.Duration;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.Objects;
35+
import java.util.function.Consumer;
36+
37+
import static java.util.Collections.singletonMap;
38+
import static java.util.Objects.requireNonNull;
39+
import static java.util.stream.StreamSupport.stream;
40+
import static org.jnosql.diana.orientdb.document.OrientDBConverter.RID_FIELD;
41+
42+
class DefaultOrientDBDocumentCollectionManager implements OrientDBDocumentCollectionManager {
43+
44+
45+
private static final Consumer<DocumentEntity> NOOPS = d -> {
46+
};
47+
private final OPartitionedDatabasePool pool;
48+
49+
DefaultOrientDBDocumentCollectionManager(OPartitionedDatabasePool pool) {
50+
this.pool = pool;
51+
}
52+
53+
@Override
54+
public DocumentEntity insert(DocumentEntity entity) throws NullPointerException {
55+
Objects.toString(entity, "Entity is required");
56+
try (ODatabaseDocumentTx tx = pool.acquire()) {
57+
ODocument document = new ODocument(entity.getName());
58+
59+
Map<String, Object> entityValues = toMap(entity);
60+
entityValues.keySet().stream().forEach(k -> document.field(k, entityValues.get(k)));
61+
ODocument save = null;
62+
try {
63+
save = tx.save(document);
64+
} catch (ONeedRetryException e) {
65+
save = tx.reload(document);
66+
}
67+
if (Objects.nonNull(save)) {
68+
ORecordId ridField = save.field("@rid");
69+
if (Objects.nonNull(ridField)) {
70+
entity.add(Document.of(RID_FIELD, ridField.toString()));
71+
}
72+
}
73+
74+
return entity;
75+
}
76+
}
77+
78+
79+
@Override
80+
public DocumentEntity insert(DocumentEntity entity, Duration ttl) {
81+
throw new UnsupportedOperationException("There is support to ttl on OrientDB");
82+
}
83+
84+
85+
@Override
86+
public DocumentEntity update(DocumentEntity entity) {
87+
return insert(entity);
88+
}
89+
90+
@Override
91+
public void delete(DocumentDeleteQuery query) {
92+
Objects.requireNonNull(query, "query is required");
93+
try (ODatabaseDocumentTx tx = pool.acquire()) {
94+
OSQLQueryFactory.QueryResult orientQuery = OSQLQueryFactory
95+
.to(DocumentQuery.of(query.getCollection())
96+
.and(query.getCondition()
97+
.orElseThrow(() -> new IllegalArgumentException("Condition is required"))));
98+
99+
List<ODocument> result = tx.command(orientQuery.getQuery()).execute(orientQuery.getParams());
100+
result.forEach(tx::delete);
101+
}
102+
103+
}
104+
105+
106+
@Override
107+
public List<DocumentEntity> select(DocumentQuery query) throws NullPointerException {
108+
try (ODatabaseDocumentTx tx = pool.acquire()) {
109+
OSQLQueryFactory.QueryResult orientQuery = OSQLQueryFactory.to(query);
110+
List<ODocument> result = tx.command(orientQuery.getQuery()).execute(orientQuery.getParams());
111+
return OrientDBConverter.convert(result);
112+
}
113+
}
114+
115+
/**
116+
* Find using query
117+
*
118+
* @param query the query
119+
* @param params the params
120+
* @return the query result
121+
* @throws NullPointerException when either query or params are null
122+
*/
123+
public List<DocumentEntity> select(String query, Object... params) throws NullPointerException {
124+
requireNonNull(query, "query is required");
125+
try (ODatabaseDocumentTx tx = pool.acquire()) {
126+
List<ODocument> result = tx.command(OSQLQueryFactory.parse(query)).execute(params);
127+
return OrientDBConverter.convert(result);
128+
}
129+
130+
}
131+
132+
@Override
133+
public void live(DocumentQuery query, Consumer<DocumentEntity> callBack) throws NullPointerException {
134+
requireNonNull(query, "query is required");
135+
requireNonNull(callBack, "callback is required");
136+
try (ODatabaseDocumentTx tx = pool.acquire();) {
137+
OSQLQueryFactory.QueryResult queryResult = OSQLQueryFactory.toLive(query, callBack);
138+
tx.command(queryResult.getQuery()).execute(queryResult.getParams());
139+
}
140+
}
141+
142+
@Override
143+
public void live(String query, Consumer<DocumentEntity> callBack, Object... params) throws NullPointerException {
144+
requireNonNull(query, "query is required");
145+
requireNonNull(callBack, "callback is required");
146+
try (ODatabaseDocumentTx tx = pool.acquire()) {
147+
OLiveQuery<ODocument> liveQuery = new OLiveQuery<>(query, new LiveQueryLIstener(callBack));
148+
tx.command(liveQuery).execute(params);
149+
}
150+
151+
}
152+
153+
@Override
154+
public void close() {
155+
pool.close();
156+
}
157+
158+
private Map<String, Object> toMap(DocumentEntity entity) {
159+
Map<String, Object> entityValues = new HashedMap();
160+
for (Document document : entity.getDocuments()) {
161+
toDocument(entityValues, document);
162+
163+
}
164+
165+
return entityValues;
166+
}
167+
168+
private void toDocument(Map<String, Object> entityValues, Document document) {
169+
Object valueAsObject = ValueUtil.convert(document.getValue());
170+
if (Document.class.isInstance(valueAsObject)) {
171+
Document subDocument = Document.class.cast(valueAsObject);
172+
entityValues.put(document.getName(), singletonMap(subDocument.getName(), subDocument.get()));
173+
} else if (isADocumentIterable(valueAsObject)) {
174+
Map<String, Object> map = new java.util.HashMap<>();
175+
stream(Iterable.class.cast(valueAsObject).spliterator(), false)
176+
.forEach(d -> toDocument(map, Document.class.cast(d)));
177+
entityValues.put(document.getName(), map);
178+
} else {
179+
entityValues.put(document.getName(), document.get());
180+
}
181+
}
182+
183+
private static boolean isADocumentIterable(Object value) {
184+
return Iterable.class.isInstance(value) &&
185+
stream(Iterable.class.cast(value).spliterator(), false)
186+
.allMatch(d -> Document.class.isInstance(d));
187+
}
7188
}

0 commit comments

Comments
 (0)