Skip to content

Commit 383ab12

Browse files
committed
Makes interface on orient document
1 parent c2ddea8 commit 383ab12

File tree

2 files changed

+11
-148
lines changed

2 files changed

+11
-148
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.jnosql.diana.orientdb.document;
2+
3+
/**
4+
* Created by otaviojava on 6/23/17.
5+
*/
6+
public class DefaultOrientDBDocumentCollectionManager {
7+
}

orientdb-driver/src/main/java/org/jnosql/diana/orientdb/document/OrientDBDocumentCollectionManager.java

Lines changed: 4 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,14 @@
1515
package org.jnosql.diana.orientdb.document;
1616

1717

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;
2618
import org.jnosql.diana.api.document.DocumentCollectionManager;
27-
import org.jnosql.diana.api.document.DocumentDeleteQuery;
2819
import org.jnosql.diana.api.document.DocumentEntity;
2920
import org.jnosql.diana.api.document.DocumentQuery;
30-
import org.jnosql.diana.driver.ValueUtil;
3121

3222
import java.time.Duration;
3323
import java.util.List;
34-
import java.util.Map;
35-
import java.util.Objects;
3624
import java.util.function.Consumer;
3725

38-
import static java.util.Collections.singletonMap;
39-
import static java.util.Objects.requireNonNull;
40-
import static java.util.stream.StreamSupport.stream;
41-
import static org.jnosql.diana.orientdb.document.OrientDBConverter.RID_FIELD;
42-
4326
/**
4427
* The orientdb implementation to {@link DocumentCollectionManager}, this implementation
4528
* does not support TTL.
@@ -49,78 +32,9 @@
4932
* <p>{@link OrientDBDocumentCollectionManager#live(DocumentQuery, Consumer)}</p>
5033
* <p>{@link OrientDBDocumentCollectionManager#live(String, Consumer, Object...)}</p>
5134
*/
52-
public class OrientDBDocumentCollectionManager implements DocumentCollectionManager {
53-
54-
55-
private static final Consumer<DocumentEntity> NOOPS = d -> {
56-
};
57-
private final OPartitionedDatabasePool pool;
58-
59-
OrientDBDocumentCollectionManager(OPartitionedDatabasePool pool) {
60-
this.pool = pool;
61-
}
62-
63-
@Override
64-
public DocumentEntity insert(DocumentEntity entity) throws NullPointerException {
65-
Objects.toString(entity, "Entity is required");
66-
try (ODatabaseDocumentTx tx = pool.acquire()) {
67-
ODocument document = new ODocument(entity.getName());
68-
69-
Map<String, Object> entityValues = toMap(entity);
70-
entityValues.keySet().stream().forEach(k -> document.field(k, entityValues.get(k)));
71-
ODocument save = null;
72-
try {
73-
save = tx.save(document);
74-
} catch (ONeedRetryException e) {
75-
save = tx.reload(document);
76-
}
77-
if (Objects.nonNull(save)) {
78-
ORecordId ridField = save.field("@rid");
79-
if (Objects.nonNull(ridField)) {
80-
entity.add(Document.of(RID_FIELD, ridField.toString()));
81-
}
82-
}
83-
84-
return entity;
85-
}
86-
}
87-
88-
89-
@Override
90-
public DocumentEntity insert(DocumentEntity entity, Duration ttl) {
91-
throw new UnsupportedOperationException("There is support to ttl on OrientDB");
92-
}
93-
94-
95-
@Override
96-
public DocumentEntity update(DocumentEntity entity) {
97-
return insert(entity);
98-
}
35+
public interface OrientDBDocumentCollectionManager extends DocumentCollectionManager {
9936

100-
@Override
101-
public void delete(DocumentDeleteQuery query) {
102-
Objects.requireNonNull(query, "query is required");
103-
try (ODatabaseDocumentTx tx = pool.acquire()) {
104-
OSQLQueryFactory.QueryResult orientQuery = OSQLQueryFactory
105-
.to(DocumentQuery.of(query.getCollection())
106-
.and(query.getCondition()
107-
.orElseThrow(() -> new IllegalArgumentException("Condition is required"))));
10837

109-
List<ODocument> result = tx.command(orientQuery.getQuery()).execute(orientQuery.getParams());
110-
result.forEach(tx::delete);
111-
}
112-
113-
}
114-
115-
116-
@Override
117-
public List<DocumentEntity> select(DocumentQuery query) throws NullPointerException {
118-
try (ODatabaseDocumentTx tx = pool.acquire()) {
119-
OSQLQueryFactory.QueryResult orientQuery = OSQLQueryFactory.to(query);
120-
List<ODocument> result = tx.command(orientQuery.getQuery()).execute(orientQuery.getParams());
121-
return OrientDBConverter.convert(result);
122-
}
123-
}
12438

12539
/**
12640
* Find using query
@@ -130,14 +44,7 @@ public List<DocumentEntity> select(DocumentQuery query) throws NullPointerExcept
13044
* @return the query result
13145
* @throws NullPointerException when either query or params are null
13246
*/
133-
public List<DocumentEntity> select(String query, Object... params) throws NullPointerException {
134-
requireNonNull(query, "query is required");
135-
try (ODatabaseDocumentTx tx = pool.acquire()) {
136-
List<ODocument> result = tx.command(OSQLQueryFactory.parse(query)).execute(params);
137-
return OrientDBConverter.convert(result);
138-
}
139-
140-
}
47+
List<DocumentEntity> select(String query, Object... params) throws NullPointerException;
14148

14249
/**
14350
* Execute live query
@@ -146,14 +53,7 @@ public List<DocumentEntity> select(String query, Object... params) throws NullPo
14653
* @param callBack when a new callback is coming
14754
* @throws NullPointerException when both query and callBack are null
14855
*/
149-
public void live(DocumentQuery query, Consumer<DocumentEntity> callBack) throws NullPointerException {
150-
requireNonNull(query, "query is required");
151-
requireNonNull(callBack, "callback is required");
152-
try (ODatabaseDocumentTx tx = pool.acquire();) {
153-
OSQLQueryFactory.QueryResult queryResult = OSQLQueryFactory.toLive(query, callBack);
154-
tx.command(queryResult.getQuery()).execute(queryResult.getParams());
155-
}
156-
}
56+
void live(DocumentQuery query, Consumer<DocumentEntity> callBack) throws NullPointerException;
15757

15858
/**
15959
* Execute live query
@@ -163,50 +63,6 @@ public void live(DocumentQuery query, Consumer<DocumentEntity> callBack) throws
16363
* @param params the params
16464
* @throws NullPointerException when both query, callBack are null
16565
*/
166-
public void live(String query, Consumer<DocumentEntity> callBack, Object... params) throws NullPointerException {
167-
requireNonNull(query, "query is required");
168-
requireNonNull(callBack, "callback is required");
169-
try (ODatabaseDocumentTx tx = pool.acquire()) {
170-
OLiveQuery<ODocument> liveQuery = new OLiveQuery<>(query, new LiveQueryLIstener(callBack));
171-
tx.command(liveQuery).execute(params);
172-
}
173-
174-
}
175-
176-
@Override
177-
public void close() {
178-
pool.close();
179-
}
180-
181-
private Map<String, Object> toMap(DocumentEntity entity) {
182-
Map<String, Object> entityValues = new HashedMap();
183-
for (Document document : entity.getDocuments()) {
184-
toDocument(entityValues, document);
185-
186-
}
187-
188-
return entityValues;
189-
}
190-
191-
private void toDocument(Map<String, Object> entityValues, Document document) {
192-
Object valueAsObject = ValueUtil.convert(document.getValue());
193-
if (Document.class.isInstance(valueAsObject)) {
194-
Document subDocument = Document.class.cast(valueAsObject);
195-
entityValues.put(document.getName(), singletonMap(subDocument.getName(), subDocument.get()));
196-
} else if (isADocumentIterable(valueAsObject)) {
197-
Map<String, Object> map = new java.util.HashMap<>();
198-
stream(Iterable.class.cast(valueAsObject).spliterator(), false)
199-
.forEach(d -> toDocument(map, Document.class.cast(d)));
200-
entityValues.put(document.getName(), map);
201-
} else {
202-
entityValues.put(document.getName(), document.get());
203-
}
204-
}
205-
206-
private static boolean isADocumentIterable(Object value) {
207-
return Iterable.class.isInstance(value) &&
208-
stream(Iterable.class.cast(value).spliterator(), false)
209-
.allMatch(d -> Document.class.isInstance(d));
210-
}
66+
void live(String query, Consumer<DocumentEntity> callBack, Object... params) throws NullPointerException;
21167

21268
}

0 commit comments

Comments
 (0)