1515package org .jnosql .diana .couchbase .document ;
1616
1717
18- import com .couchbase .client .java .Bucket ;
19- import com .couchbase .client .java .document .JsonDocument ;
2018import 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 ;
2419import com .couchbase .client .java .query .Statement ;
25- import org .jnosql .diana .api .document .Document ;
2620import org .jnosql .diana .api .document .DocumentCollectionManager ;
27- import org .jnosql .diana .api .document .DocumentDeleteQuery ;
2821import org .jnosql .diana .api .document .DocumentEntity ;
29- import org .jnosql .diana .api .document .DocumentQuery ;
3022
31- import java .time .Duration ;
32- import java .util .ArrayList ;
3323import java .util .List ;
34- import java .util .Objects ;
35-
36- import static java .util .Objects .nonNull ;
37- import static java .util .Objects .requireNonNull ;
38- import static java .util .concurrent .TimeUnit .MILLISECONDS ;
39- import static org .jnosql .diana .couchbase .document .EntityConverter .ID_FIELD ;
40- import static org .jnosql .diana .couchbase .document .EntityConverter .convert ;
41- import static org .jnosql .diana .couchbase .document .EntityConverter .getPrefix ;
4224
4325/**
4426 * The couchbase implementation of {@link DocumentCollectionManager}
4527 */
46- public class CouchbaseDocumentCollectionManager implements DocumentCollectionManager {
47-
48- private final Bucket bucket ;
49- private final String database ;
50-
51- CouchbaseDocumentCollectionManager (Bucket bucket , String database ) {
52- this .bucket = bucket ;
53- this .database = database ;
54- }
55-
56- @ Override
57- public DocumentEntity insert (DocumentEntity entity ) throws NullPointerException {
58- Objects .requireNonNull (entity , "entity is required" );
59- JsonObject jsonObject = convert (entity );
60- Document id = entity .find (ID_FIELD )
61- .orElseThrow (() -> new CouchbaseNoKeyFoundException (entity .toString ()));
62-
63- String prefix = getPrefix (id , entity .getName ());
64- jsonObject .put (ID_FIELD , prefix );
65- bucket .upsert (JsonDocument .create (prefix , jsonObject ));
66- entity .remove (ID_FIELD );
67- entity .add (Document .of (ID_FIELD , prefix ));
68- return entity ;
69- }
70-
71- @ Override
72- public DocumentEntity insert (DocumentEntity entity , Duration ttl ) {
73- Objects .requireNonNull (entity , "entity is required" );
74- requireNonNull (ttl , "ttl is required" );
75- JsonObject jsonObject = convert (entity );
76- Document id = entity .find (ID_FIELD )
77- .orElseThrow (() -> new CouchbaseNoKeyFoundException (entity .toString ()));
78-
79- String prefix = getPrefix (id , entity .getName ());
80- bucket .upsert (JsonDocument .create (prefix , jsonObject ), ttl .toMillis (), MILLISECONDS );
81- return entity ;
82- }
28+ public interface CouchbaseDocumentCollectionManager extends DocumentCollectionManager {
8329
84- @ Override
85- public DocumentEntity update (DocumentEntity entity ) {
86- return insert (entity );
87- }
88-
89- @ Override
90- public void delete (DocumentDeleteQuery query ) {
91- QueryConverter .QueryConverterResult delete = QueryConverter .delete (query , database );
92- if (nonNull (delete .getStatement ())) {
93- ParameterizedN1qlQuery n1qlQuery = N1qlQuery .parameterized (delete .getStatement (), delete .getParams ());
94- N1qlQueryResult result = bucket .query (n1qlQuery );
95- }
96- if (!delete .getIds ().isEmpty ()) {
97- delete .getIds ()
98- .stream ()
99- .map (s -> getPrefix (query .getCollection (), s ))
100- .forEach (bucket ::remove );
101- }
102-
103- }
104-
105- @ Override
106- public List <DocumentEntity > select (DocumentQuery query ) throws NullPointerException {
107-
108- QueryConverter .QueryConverterResult select = QueryConverter .select (query , database );
109- List <DocumentEntity > entities = new ArrayList <>();
110- if (nonNull (select .getStatement ())) {
111- ParameterizedN1qlQuery n1qlQuery = N1qlQuery .parameterized (select .getStatement (), select .getParams ());
112- N1qlQueryResult result = bucket .query (n1qlQuery );
113- entities .addAll (convert (result , database ));
114- }
115- if (!select .getIds ().isEmpty ()) {
116- entities .addAll (convert (select .getIds (), query .getCollection (), bucket ));
117- }
118-
119- return entities ;
120- }
12130
12231
12332 /**
@@ -128,12 +37,7 @@ public List<DocumentEntity> select(DocumentQuery query) throws NullPointerExcept
12837 * @return the query result
12938 * @throws NullPointerException when either n1qlQuery or params are null
13039 */
131- public List <DocumentEntity > n1qlQuery (String n1qlQuery , JsonObject params ) throws NullPointerException {
132- requireNonNull (n1qlQuery , "n1qlQuery is required" );
133- requireNonNull (params , "params is required" );
134- N1qlQueryResult result = bucket .query (N1qlQuery .parameterized (n1qlQuery , params ));
135- return convert (result , database );
136- }
40+ List <DocumentEntity > n1qlQuery (String n1qlQuery , JsonObject params ) throws NullPointerException ;
13741
13842 /**
13943 * Executes the n1qlquery with params and then result que result
@@ -143,12 +47,7 @@ public List<DocumentEntity> n1qlQuery(String n1qlQuery, JsonObject params) throw
14347 * @return the query result
14448 * @throws NullPointerException when either n1qlQuery or params are null
14549 */
146- public List <DocumentEntity > n1qlQuery (Statement n1qlQuery , JsonObject params ) throws NullPointerException {
147- requireNonNull (n1qlQuery , "n1qlQuery is required" );
148- requireNonNull (params , "params is required" );
149- N1qlQueryResult result = bucket .query (N1qlQuery .parameterized (n1qlQuery , params ));
150- return convert (result , database );
151- }
50+ List <DocumentEntity > n1qlQuery (Statement n1qlQuery , JsonObject params ) throws NullPointerException ;
15251
15352 /**
15453 * Executes the n1qlquery plain query and then result que result
@@ -157,11 +56,7 @@ public List<DocumentEntity> n1qlQuery(Statement n1qlQuery, JsonObject params) th
15756 * @return the query result
15857 * @throws NullPointerException when either n1qlQuery or params are null
15958 */
160- public List <DocumentEntity > n1qlQuery (String n1qlQuery ) throws NullPointerException {
161- requireNonNull (n1qlQuery , "n1qlQuery is required" );
162- N1qlQueryResult result = bucket .query (N1qlQuery .simple (n1qlQuery ));
163- return convert (result , database );
164- }
59+ List <DocumentEntity > n1qlQuery (String n1qlQuery ) throws NullPointerException ;
16560
16661 /**
16762 * Executes the n1qlquery plain query and then result que result
@@ -170,15 +65,6 @@ public List<DocumentEntity> n1qlQuery(String n1qlQuery) throws NullPointerExcept
17065 * @return the query result
17166 * @throws NullPointerException when either n1qlQuery or params are null
17267 */
173- public List <DocumentEntity > n1qlQuery (Statement n1qlQuery ) throws NullPointerException {
174- requireNonNull (n1qlQuery , "n1qlQuery is required" );
175- N1qlQueryResult result = bucket .query (N1qlQuery .simple (n1qlQuery ));
176- return convert (result , database );
177- }
178-
179- @ Override
180- public void close () {
181- bucket .close ();
182- }
68+ List <DocumentEntity > n1qlQuery (Statement n1qlQuery ) throws NullPointerException ;
18369
18470}
0 commit comments