|
| 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 | + */ |
1 | 15 | package org.jnosql.diana.orientdb.document; |
2 | 16 |
|
3 | | -/** |
4 | | - * Created by otaviojava on 6/23/17. |
5 | | - */ |
6 | | -public class DefaultOrientDBDocumentCollectionManagerAsync { |
| 17 | + |
| 18 | +import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; |
| 19 | +import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; |
| 20 | +import com.orientechnologies.orient.core.record.impl.ODocument; |
| 21 | +import com.orientechnologies.orient.core.storage.ORecordCallback; |
| 22 | +import org.jnosql.diana.api.ExecuteAsyncQueryException; |
| 23 | +import org.jnosql.diana.api.document.Document; |
| 24 | +import org.jnosql.diana.api.document.DocumentDeleteQuery; |
| 25 | +import org.jnosql.diana.api.document.DocumentEntity; |
| 26 | +import org.jnosql.diana.api.document.DocumentQuery; |
| 27 | + |
| 28 | +import java.time.Duration; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.function.Consumer; |
| 33 | + |
| 34 | +import static com.orientechnologies.orient.core.db.ODatabase.OPERATION_MODE.ASYNCHRONOUS; |
| 35 | +import static java.util.Objects.requireNonNull; |
| 36 | +import static java.util.stream.Collectors.toList; |
| 37 | +import static org.jnosql.diana.orientdb.document.OSQLQueryFactory.toAsync; |
| 38 | +import static org.jnosql.diana.orientdb.document.OrientDBConverter.RID_FIELD; |
| 39 | + |
| 40 | +class DefaultOrientDBDocumentCollectionManagerAsync implements OrientDBDocumentCollectionManagerAsync { |
| 41 | + |
| 42 | + private static final Consumer<DocumentEntity> NOOPS = d -> { |
| 43 | + }; |
| 44 | + |
| 45 | + |
| 46 | + private final OPartitionedDatabasePool pool; |
| 47 | + |
| 48 | + DefaultOrientDBDocumentCollectionManagerAsync(OPartitionedDatabasePool pool) { |
| 49 | + this.pool = pool; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void insert(DocumentEntity entity, Duration ttl) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 54 | + throw new UnsupportedOperationException("There is support to ttl on OrientDB"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void insert(DocumentEntity entity) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 59 | + insert(entity, NOOPS); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void insert(DocumentEntity entity, Consumer<DocumentEntity> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 64 | + Objects.toString(entity, "Entity is required"); |
| 65 | + ODatabaseDocumentTx tx = pool.acquire(); |
| 66 | + ODocument document = new ODocument(entity.getName()); |
| 67 | + Map<String, Object> entityValues = entity.toMap(); |
| 68 | + entityValues.keySet().stream().forEach(k -> document.field(k, entityValues.get(k))); |
| 69 | + ORecordCallback<Number> createCallBack = (a, b) -> { |
| 70 | + entity.add(Document.of(RID_FIELD, a.toString())); |
| 71 | + callBack.accept(entity); |
| 72 | + }; |
| 73 | + ORecordCallback<Integer> updateCallback = (a, b) -> { |
| 74 | + entity.add(Document.of(RID_FIELD, a.toString())); |
| 75 | + callBack.accept(entity); |
| 76 | + }; |
| 77 | + tx.save(document, null, ASYNCHRONOUS, false, createCallBack, updateCallback); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void insert(DocumentEntity entity, Duration ttl, Consumer<DocumentEntity> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 82 | + throw new UnsupportedOperationException("There is support to ttl on OrientDB"); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void update(DocumentEntity entity) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 87 | + insert(entity); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void update(DocumentEntity entity, Consumer<DocumentEntity> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 92 | + insert(entity, callBack); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void delete(DocumentDeleteQuery query) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 97 | + delete(query, v -> { |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void delete(DocumentDeleteQuery query, Consumer<Void> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 103 | + ODatabaseDocumentTx tx = pool.acquire(); |
| 104 | + OSQLQueryFactory.QueryResult orientQuery = toAsync(DocumentQuery.of(query.getCollection()) |
| 105 | + .and(query.getCondition().orElseThrow(() -> new IllegalArgumentException("Condition is required"))), l -> { |
| 106 | + l.forEach(d -> d.delete()); |
| 107 | + callBack.accept(null); |
| 108 | + }); |
| 109 | + tx.command(orientQuery.getQuery()).execute(orientQuery.getParams()); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void select(DocumentQuery query, Consumer<List<DocumentEntity>> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException { |
| 114 | + ODatabaseDocumentTx tx = pool.acquire(); |
| 115 | + OSQLQueryFactory.QueryResult orientQuery = toAsync(query, l -> { |
| 116 | + callBack.accept(l.stream() |
| 117 | + .map(OrientDBConverter::convert) |
| 118 | + .collect(toList())); |
| 119 | + }); |
| 120 | + tx.command(orientQuery.getQuery()).execute(orientQuery.getParams()); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void find(String query, Consumer<List<DocumentEntity>> callBack, Object... params) throws NullPointerException { |
| 125 | + requireNonNull(query, "query is required"); |
| 126 | + requireNonNull(callBack, "callBack is required"); |
| 127 | + ODatabaseDocumentTx tx = pool.acquire(); |
| 128 | + OSQLQueryFactory.QueryResult orientQuery = toAsync(query, l -> { |
| 129 | + callBack.accept(l.stream() |
| 130 | + .map(OrientDBConverter::convert) |
| 131 | + .collect(toList())); |
| 132 | + }, params); |
| 133 | + tx.command(orientQuery.getQuery()).execute(orientQuery.getParams()); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + @Override |
| 138 | + public void close() { |
| 139 | + pool.close(); |
| 140 | + } |
7 | 141 | } |
0 commit comments