Skip to content

Commit 81522c3

Browse files
authored
Merge pull request #80 from eclipse/couchbase_structure
Optimizes Couchbase collections structures and increase the test coverage
2 parents 852e47b + 0125957 commit 81522c3

14 files changed

+612
-102
lines changed

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseBucketManager.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class CouchbaseBucketManager implements BucketManager {
5656
}
5757

5858
@Override
59-
public <K, V> void put(K key, V value) throws NullPointerException {
59+
public <K, V> void put(K key, V value) {
6060
requireNonNull(key, "key is required");
6161
requireNonNull(value, "value is required");
6262
bucket.upsert(JsonDocument.create(key.toString(), JsonObjectCouchbaseUtil.toJson(JSONB, value)));
@@ -69,7 +69,7 @@ public <K> void put(KeyValueEntity<K> entity) throws NullPointerException {
6969
}
7070

7171
@Override
72-
public <K> void put(KeyValueEntity<K> entity, Duration ttl) throws NullPointerException, UnsupportedOperationException {
72+
public <K> void put(KeyValueEntity<K> entity, Duration ttl){
7373
requireNonNull(entity, "entity is required");
7474
requireNonNull(ttl, "ttl is required");
7575

@@ -81,13 +81,13 @@ public <K> void put(KeyValueEntity<K> entity, Duration ttl) throws NullPointerEx
8181
}
8282

8383
@Override
84-
public <K> void put(Iterable<KeyValueEntity<K>> keyValueEntities) throws NullPointerException {
84+
public <K> void put(Iterable<KeyValueEntity<K>> keyValueEntities){
8585
requireNonNull(keyValueEntities, "keyValueEntities is required");
8686
keyValueEntities.forEach(this::put);
8787
}
8888

8989
@Override
90-
public <K> void put(Iterable<KeyValueEntity<K>> keyValueEntities, Duration ttl) throws NullPointerException, UnsupportedOperationException {
90+
public <K> void put(Iterable<KeyValueEntity<K>> keyValueEntities, Duration ttl) {
9191
requireNonNull(keyValueEntities, "keyValueEntities is required");
9292
requireNonNull(ttl, "ttl is required");
9393
keyValueEntities.forEach(k -> this.put(k, ttl));
@@ -105,7 +105,7 @@ public <K> Optional<Value> get(K key) throws NullPointerException {
105105
}
106106

107107
@Override
108-
public <K> Iterable<Value> get(Iterable<K> keys) throws NullPointerException {
108+
public <K> Iterable<Value> get(Iterable<K> keys){
109109
requireNonNull(keys, "keys is required");
110110
return stream(keys.spliterator(), false)
111111
.map(this::get)
@@ -115,7 +115,7 @@ public <K> Iterable<Value> get(Iterable<K> keys) throws NullPointerException {
115115
}
116116

117117
@Override
118-
public <K> void remove(K key) throws NullPointerException {
118+
public <K> void remove(K key){
119119
requireNonNull(key, "key is required");
120120
try {
121121
bucket.remove(key.toString());
@@ -125,7 +125,7 @@ public <K> void remove(K key) throws NullPointerException {
125125
}
126126

127127
@Override
128-
public <K> void remove(Iterable<K> keys) throws NullPointerException {
128+
public <K> void remove(Iterable<K> keys) {
129129
requireNonNull(keys, "keys is required");
130130
keys.forEach(this::remove);
131131
}

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseBucketManagerFactory.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public interface CouchbaseBucketManagerFactory extends BucketManagerFactory<Couc
4444
* @throws UnsupportedOperationException when the database does not have to it
4545
* @throws NullPointerException when either bucketName or class are null
4646
*/
47-
<T> Queue<T> getQueue(String bucketName, String key, Class<T> clazz) throws UnsupportedOperationException,
48-
NullPointerException;
47+
<T> Queue<T> getQueue(String bucketName, String key, Class<T> clazz);
4948

5049
/**
5150
* Creates a {@link Set} from bucket name
@@ -58,8 +57,7 @@ <T> Queue<T> getQueue(String bucketName, String key, Class<T> clazz) throws Unsu
5857
* @throws UnsupportedOperationException when the database does not have to it
5958
* @throws NullPointerException when either bucketName or class are null
6059
*/
61-
<T> Set<T> getSet(String bucketName, String key, Class<T> clazz) throws UnsupportedOperationException,
62-
NullPointerException;
60+
<T> Set<T> getSet(String bucketName, String key, Class<T> clazz);
6361

6462

6563
/**
@@ -73,8 +71,7 @@ <T> Set<T> getSet(String bucketName, String key, Class<T> clazz) throws Unsuppor
7371
* @throws UnsupportedOperationException when the database does not have to it
7472
* @throws NullPointerException when either bucketName or class are null
7573
*/
76-
<T> List<T> getList(String bucketName, String key, Class<T> clazz) throws UnsupportedOperationException,
77-
NullPointerException;
74+
<T> List<T> getList(String bucketName, String key, Class<T> clazz);
7875

7976

8077
/**
@@ -90,6 +87,5 @@ <T> List<T> getList(String bucketName, String key, Class<T> clazz) throws Unsupp
9087
* @throws UnsupportedOperationException when the database does not have to it
9188
* @throws NullPointerException when either bucketName or class are null
9289
*/
93-
<K, V> Map<K, V> getMap(String bucketName, String key, Class<K> keyValue, Class<V> valueValue) throws
94-
UnsupportedOperationException, NullPointerException;
90+
<K, V> Map<K, V> getMap(String bucketName, String key, Class<K> keyValue, Class<V> valueValue);
9591
}

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseCollection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ abstract class CouchbaseCollection<T> {
3333
protected Function<String, T> fromJSON() {
3434
return s -> JSONB.fromJson(s, clazz);
3535
}
36+
3637
}

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseList.java

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.couchbase.client.java.Bucket;
1919
import com.couchbase.client.java.datastructures.collections.CouchbaseArrayList;
20+
import com.couchbase.client.java.document.json.JsonObject;
2021

2122
import java.util.Collection;
2223
import java.util.Iterator;
@@ -26,9 +27,11 @@
2627
import java.util.Spliterator;
2728
import java.util.stream.StreamSupport;
2829

30+
import static java.lang.Boolean.TRUE;
2931
import static java.util.Objects.requireNonNull;
3032
import static java.util.Spliterators.spliteratorUnknownSize;
3133
import static java.util.stream.Collectors.toList;
34+
import static org.jnosql.diana.couchbase.key.DefaultCouchbaseBucketManagerFactory.LIST;
3235

3336
/**
3437
* The couchbase implementation to {@link List}
@@ -38,14 +41,15 @@
3841
*
3942
* @param <T> the object to be stored.
4043
*/
41-
public class CouchbaseList<T> extends CouchbaseCollection<T> implements List<T> {
44+
class CouchbaseList<T> extends CouchbaseCollection<T> implements List<T> {
4245

46+
private static final int NOT_FOUND = -1;
4347
private final String bucketName;
44-
private final CouchbaseArrayList<String> arrayList;
48+
private final CouchbaseArrayList<JsonObject> arrayList;
4549

4650
CouchbaseList(Bucket bucket, String bucketName, Class<T> clazz) {
4751
super(clazz);
48-
this.bucketName = bucketName + ":list";
52+
this.bucketName = bucketName + LIST;
4953
this.arrayList = new CouchbaseArrayList(this.bucketName, bucket);
5054
}
5155

@@ -63,7 +67,7 @@ public boolean isEmpty() {
6367
@Override
6468
public boolean add(T t) {
6569
requireNonNull(t, "object is required");
66-
return arrayList.add(JSONB.toJson(t));
70+
return arrayList.add(JsonObjectCouchbaseUtil.toJson(JSONB, t));
6771
}
6872

6973
@Override
@@ -82,25 +86,26 @@ public void clear() {
8286

8387
@Override
8488
public T get(int i) {
85-
return JSONB.fromJson(arrayList.get(i), clazz);
89+
JsonObject jsonObject = arrayList.get(i);
90+
return JSONB.fromJson(jsonObject.toString(), clazz);
8691
}
8792

8893
@Override
8994
public T set(int i, T t) {
9095
requireNonNull(t, "object is required");
91-
String json = arrayList.set(i, JSONB.toJson(t));
96+
JsonObject json = arrayList.set(i, JsonObjectCouchbaseUtil.toJson(JSONB, t));
9297
if (Objects.nonNull(json)) {
93-
return JSONB.fromJson(json, clazz);
98+
return JSONB.fromJson(json.toString(), clazz);
9499
}
95100
return null;
96101
}
97102

98103

99104
@Override
100105
public T remove(int i) {
101-
String json = arrayList.remove(i);
106+
JsonObject json = arrayList.remove(i);
102107
if (Objects.nonNull(json)) {
103-
return JSONB.fromJson(json, clazz);
108+
return JSONB.fromJson(json.toString(), clazz);
104109
}
105110
return null;
106111
}
@@ -109,99 +114,128 @@ public T remove(int i) {
109114
@Override
110115
public Iterator<T> iterator() {
111116
return StreamSupport.stream(arrayList.spliterator(), false)
112-
.map(fromJSON())
117+
.map(s -> JSONB.fromJson(s.toString(), clazz))
113118
.collect(toList()).iterator();
114119
}
115120

116121
@Override
117122
public Object[] toArray() {
118123
return StreamSupport.stream(arrayList.spliterator(), false)
119-
.map(fromJSON())
124+
.map(s -> JSONB.fromJson(s.toString(), clazz))
120125
.toArray(Object[]::new);
121126
}
122127

123128
@Override
124129
public <T1> T1[] toArray(T1[] t1s) {
125130
requireNonNull(t1s, "arrys is required");
126131
return StreamSupport.stream(arrayList.spliterator(), false)
127-
.map(fromJSON())
132+
.map(s -> JSONB.fromJson(s.toString(), clazz))
128133
.toArray(size -> t1s);
129134
}
130135

131136
@Override
132137
public boolean retainAll(Collection<?> collection) {
133138
requireNonNull(collection, "collection is required");
134-
return arrayList.retainAll(collection.stream().map(JSONB::toJson).collect(toList()));
139+
collection.removeIf(e -> indexOf(e) == NOT_FOUND);
140+
return true;
135141
}
136142

137143
@Override
138144
public boolean removeAll(Collection<?> collection) {
139145
requireNonNull(collection, "collection is required");
140-
return arrayList.removeAll(collection.stream().map(JSONB::toJson).collect(toList()));
146+
147+
boolean removeAll = true;
148+
for (Object object : collection) {
149+
if (!this.remove(object)) {
150+
removeAll = false;
151+
}
152+
}
153+
return removeAll;
141154
}
142155

143156
@Override
144157
public void add(int i, T t) {
145158
requireNonNull(t, "object is required");
146-
arrayList.add(i, JSONB.toJson(t));
159+
arrayList.add(i, JsonObjectCouchbaseUtil.toJson(JSONB, t));
147160
}
148161

149162
@Override
150163
public int indexOf(Object o) {
151164
requireNonNull(o, "object is required");
152-
return arrayList.indexOf(JSONB.toJson(o));
165+
int index = 0;
166+
for (JsonObject jsonObject : arrayList) {
167+
if (jsonObject.toString().equals(JsonObjectCouchbaseUtil.toJson(JSONB, o).toString())) {
168+
return index;
169+
}
170+
index++;
171+
}
172+
return NOT_FOUND;
153173
}
154174

155175
@Override
156176
public int lastIndexOf(Object o) {
157177
requireNonNull(o, "object is required");
158-
return arrayList.lastIndexOf(JSONB.toJson(o));
178+
for (int index = arrayList.size() - 1; index >= 0; index--) {
179+
JsonObject jsonObject = arrayList.get(index);
180+
if (jsonObject.toString().equals(JsonObjectCouchbaseUtil.toJson(JSONB, o).toString())) {
181+
return index;
182+
}
183+
}
184+
return NOT_FOUND;
159185
}
160186

161187
@Override
162188
public ListIterator<T> listIterator() {
163189
return StreamSupport.stream(spliteratorUnknownSize(arrayList.listIterator(), Spliterator.ORDERED),
164-
false).map(fromJSON())
190+
false).map(s -> JSONB.fromJson(s.toString(), clazz))
165191
.collect(toList())
166192
.listIterator();
167193
}
168194

169195
@Override
170196
public ListIterator<T> listIterator(int i) {
171197
return StreamSupport.stream(spliteratorUnknownSize(arrayList.listIterator(i), Spliterator.ORDERED),
172-
false).map(fromJSON())
198+
false).map(s -> JSONB.fromJson(s.toString(), clazz))
173199
.collect(toList())
174200
.listIterator();
175201
}
176202

177203
@Override
178204
public List<T> subList(int i, int i1) {
179205
return arrayList.subList(i, i1).stream().
180-
map(fromJSON())
206+
map(s -> JSONB.fromJson(s.toString(), clazz))
181207
.collect(toList());
182208
}
183209

184210
@Override
185211
public boolean remove(Object o) {
186212
requireNonNull(o, "object is required");
187-
return arrayList.remove(JSONB.toJson(o));
213+
int index = indexOf(o);
214+
if (index >= 0) {
215+
arrayList.remove(index);
216+
return true;
217+
}
218+
return false;
188219
}
189220

190221
@Override
191222
public boolean containsAll(Collection<?> collection) {
192223
requireNonNull(collection, "collection is required");
193-
return arrayList.containsAll(collection.stream().map(JSONB::toJson).collect(toList()));
224+
return collection.stream().map(this::contains).allMatch(TRUE::equals);
194225
}
195226

196227
@Override
197228
public boolean contains(Object o) {
198229
requireNonNull(o, "object is required");
199-
return arrayList.contains(JSONB.toJson(o));
230+
return indexOf(o) != NOT_FOUND;
200231
}
201232

202233
@Override
203234
public boolean addAll(int i, Collection<? extends T> collection) {
204235
requireNonNull(collection, "collection is required");
205-
return arrayList.addAll(i, collection.stream().map(JSONB::toJson).collect(toList()));
236+
List<JsonObject> objects = collection.stream().map(s -> JsonObjectCouchbaseUtil.toJson(JSONB, s)).collect(toList());
237+
return arrayList.addAll(i, objects);
206238
}
239+
240+
207241
}

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @param <V> the object to be stored as value.
4040
* @param <K> the object to be stored as key.
4141
*/
42-
public class CouchbaseMap<K, V> implements Map<K, V> {
42+
class CouchbaseMap<K, V> implements Map<K, V> {
4343

4444
private static final Jsonb JSONB = JsonbBuilder.create();
4545

0 commit comments

Comments
 (0)