|
| 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 | + */ |
| 15 | +package org.jnosql.diana.couchbase.document; |
| 16 | + |
| 17 | + |
| 18 | +import com.couchbase.client.java.Bucket; |
| 19 | +import com.couchbase.client.java.document.JsonDocument; |
| 20 | +import com.couchbase.client.java.document.json.JsonObject; |
| 21 | +import com.couchbase.client.java.query.N1qlQuery; |
| 22 | +import com.couchbase.client.java.query.N1qlQueryResult; |
| 23 | +import com.couchbase.client.java.query.ParameterizedN1qlQuery; |
| 24 | +import com.couchbase.client.java.query.Statement; |
| 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 | + |
| 30 | +import java.time.Duration; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Objects; |
| 34 | + |
| 35 | +import static java.util.Objects.nonNull; |
| 36 | +import static java.util.Objects.requireNonNull; |
| 37 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 38 | +import static org.jnosql.diana.couchbase.document.EntityConverter.ID_FIELD; |
| 39 | +import static org.jnosql.diana.couchbase.document.EntityConverter.convert; |
| 40 | +import static org.jnosql.diana.couchbase.document.EntityConverter.getPrefix; |
| 41 | + |
| 42 | +/** |
| 43 | + * The default implementation of {@link CouchbaseDocumentCollectionManager} |
| 44 | + */ |
| 45 | +class DefaultCouchbaseDocumentCollectionManager implements CouchbaseDocumentCollectionManager { |
| 46 | + private final Bucket bucket; |
| 47 | + private final String database; |
| 48 | + |
| 49 | + DefaultCouchbaseDocumentCollectionManager(Bucket bucket, String database) { |
| 50 | + this.bucket = bucket; |
| 51 | + this.database = database; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public DocumentEntity insert(DocumentEntity entity) throws NullPointerException { |
| 56 | + Objects.requireNonNull(entity, "entity is required"); |
| 57 | + JsonObject jsonObject = convert(entity); |
| 58 | + Document id = entity.find(ID_FIELD) |
| 59 | + .orElseThrow(() -> new CouchbaseNoKeyFoundException(entity.toString())); |
| 60 | + |
| 61 | + String prefix = getPrefix(id, entity.getName()); |
| 62 | + jsonObject.put(ID_FIELD, prefix); |
| 63 | + bucket.upsert(JsonDocument.create(prefix, jsonObject)); |
| 64 | + entity.remove(ID_FIELD); |
| 65 | + entity.add(Document.of(ID_FIELD, prefix)); |
| 66 | + return entity; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public DocumentEntity insert(DocumentEntity entity, Duration ttl) { |
| 71 | + Objects.requireNonNull(entity, "entity is required"); |
| 72 | + requireNonNull(ttl, "ttl is required"); |
| 73 | + JsonObject jsonObject = convert(entity); |
| 74 | + Document id = entity.find(ID_FIELD) |
| 75 | + .orElseThrow(() -> new CouchbaseNoKeyFoundException(entity.toString())); |
| 76 | + |
| 77 | + String prefix = getPrefix(id, entity.getName()); |
| 78 | + bucket.upsert(JsonDocument.create(prefix, jsonObject), ttl.toMillis(), MILLISECONDS); |
| 79 | + return entity; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public DocumentEntity update(DocumentEntity entity) { |
| 84 | + return insert(entity); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void delete(DocumentDeleteQuery query) { |
| 89 | + QueryConverter.QueryConverterResult delete = QueryConverter.delete(query, database); |
| 90 | + if (nonNull(delete.getStatement())) { |
| 91 | + ParameterizedN1qlQuery n1qlQuery = N1qlQuery.parameterized(delete.getStatement(), delete.getParams()); |
| 92 | + N1qlQueryResult result = bucket.query(n1qlQuery); |
| 93 | + } |
| 94 | + if (!delete.getIds().isEmpty()) { |
| 95 | + delete.getIds() |
| 96 | + .stream() |
| 97 | + .map(s -> getPrefix(query.getCollection(), s)) |
| 98 | + .forEach(bucket::remove); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public List<DocumentEntity> select(DocumentQuery query) throws NullPointerException { |
| 105 | + |
| 106 | + QueryConverter.QueryConverterResult select = QueryConverter.select(query, database); |
| 107 | + List<DocumentEntity> entities = new ArrayList<>(); |
| 108 | + if (nonNull(select.getStatement())) { |
| 109 | + ParameterizedN1qlQuery n1qlQuery = N1qlQuery.parameterized(select.getStatement(), select.getParams()); |
| 110 | + N1qlQueryResult result = bucket.query(n1qlQuery); |
| 111 | + entities.addAll(convert(result, database)); |
| 112 | + } |
| 113 | + if (!select.getIds().isEmpty()) { |
| 114 | + entities.addAll(convert(select.getIds(), query.getCollection(), bucket)); |
| 115 | + } |
| 116 | + |
| 117 | + return entities; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + @Override |
| 122 | + public List<DocumentEntity> n1qlQuery(String n1qlQuery, JsonObject params) throws NullPointerException { |
| 123 | + requireNonNull(n1qlQuery, "n1qlQuery is required"); |
| 124 | + requireNonNull(params, "params is required"); |
| 125 | + N1qlQueryResult result = bucket.query(N1qlQuery.parameterized(n1qlQuery, params)); |
| 126 | + return convert(result, database); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public List<DocumentEntity> n1qlQuery(Statement n1qlQuery, JsonObject params) throws NullPointerException { |
| 131 | + requireNonNull(n1qlQuery, "n1qlQuery is required"); |
| 132 | + requireNonNull(params, "params is required"); |
| 133 | + N1qlQueryResult result = bucket.query(N1qlQuery.parameterized(n1qlQuery, params)); |
| 134 | + return convert(result, database); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public List<DocumentEntity> n1qlQuery(String n1qlQuery) throws NullPointerException { |
| 139 | + requireNonNull(n1qlQuery, "n1qlQuery is required"); |
| 140 | + N1qlQueryResult result = bucket.query(N1qlQuery.simple(n1qlQuery)); |
| 141 | + return convert(result, database); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public List<DocumentEntity> n1qlQuery(Statement n1qlQuery) throws NullPointerException { |
| 146 | + requireNonNull(n1qlQuery, "n1qlQuery is required"); |
| 147 | + N1qlQueryResult result = bucket.query(N1qlQuery.simple(n1qlQuery)); |
| 148 | + return convert(result, database); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void close() { |
| 153 | + bucket.close(); |
| 154 | + } |
| 155 | +} |
0 commit comments