Skip to content

Commit a8ea1a4

Browse files
committed
fixes orientdb
1 parent 71a3398 commit a8ea1a4

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ public List<DocumentEntity> select(DocumentQuery query) {
9999
}
100100
}
101101

102+
@Override
103+
public long count(String documentCollection) {
104+
requireNonNull(documentCollection, "query is required");
105+
try (ODatabaseDocumentTx tx = pool.acquire()) {
106+
String query = "select count(*) from ".concat(documentCollection);
107+
List<ODocument> result = tx.command(QueryOSQLFactory.parse(query)).execute();
108+
return result.stream()
109+
.findFirst()
110+
.map(e -> e.field("count"))
111+
.map(n -> Number.class.cast(n).longValue())
112+
.orElse(0L);
113+
114+
}
115+
}
116+
102117
@Override
103118
public List<DocumentEntity> sql(String query, Object... params) {
104119
requireNonNull(query, "query is required");

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ public void select(DocumentQuery query, Consumer<List<DocumentEntity>> callBack)
131131
tx.command(orientQuery.getQuery()).execute(orientQuery.getParams());
132132
}
133133

134+
@Override
135+
public void count(String documentCollection, Consumer<Long> callback) {
136+
requireNonNull(documentCollection, "query is required");
137+
requireNonNull(callback, "callBack is required");
138+
139+
ODatabaseDocumentTx tx = pool.acquire();
140+
141+
String query = "select count(*) from ".concat(documentCollection);
142+
Consumer<List<ODocument>> orientCallback = l ->
143+
callback.accept(l.stream()
144+
.findFirst()
145+
.map(e -> e.field("count"))
146+
.map(n -> Number.class.cast(n).longValue())
147+
.orElse(0L));
148+
QueryOSQLFactory.QueryResult orientQuery = toAsync(query, orientCallback);
149+
tx.command(orientQuery.getQuery()).execute(orientQuery.getParams());
150+
}
151+
134152
@Override
135153
public void sql(String query, Consumer<List<DocumentEntity>> callBack, Object... params) throws NullPointerException {
136154
requireNonNull(query, "query is required");

orientdb-driver/src/test/java/org/jnosql/diana/orientdb/document/OrientDBDocumentCollectionManagerAsyncTest.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
import java.util.Map;
3131
import java.util.Optional;
3232
import java.util.concurrent.atomic.AtomicBoolean;
33+
import java.util.concurrent.atomic.AtomicLong;
3334
import java.util.concurrent.atomic.AtomicReference;
34-
import java.util.logging.Logger;
35+
import java.util.function.Consumer;
3536

3637
import static org.awaitility.Awaitility.await;
3738
import static org.hamcrest.Matchers.notNullValue;
@@ -52,8 +53,6 @@ public class OrientDBDocumentCollectionManagerAsyncTest {
5253

5354
private DocumentCollectionManager entityManager;
5455

55-
private static final Logger LOGGER = Logger.getLogger(OrientDBDocumentCollectionManagerTest.class.getName());
56-
5756
@BeforeEach
5857
public void setUp() {
5958
entityManagerAsync = getAsync().getAsync("database");
@@ -62,15 +61,15 @@ public void setUp() {
6261

6362

6463
@Test
65-
public void shouldSaveAsync() throws InterruptedException {
64+
public void shouldInsertAsync() throws InterruptedException {
6665
AtomicReference<DocumentEntity> entityAtomic = new AtomicReference<>();
6766
entityManagerAsync.insert(getEntity(), entityAtomic::set);
6867
await().until(entityAtomic::get, notNullValue(DocumentEntity.class));
6968

7069
DocumentEntity entity = entityAtomic.get();
7170
Document id = entity.find("name").get();
7271

73-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
72+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
7473
List<DocumentEntity> entities = entityManager.select(query);
7574
assertFalse(entities.isEmpty());
7675

@@ -83,7 +82,8 @@ public void ShouldThrowExceptionWhenInsertWithTTL() {
8382

8483
@Test
8584
public void ShouldThrowExceptionWhenInsertWithTTLAndCallback() {
86-
assertThrows(UnsupportedOperationException.class, () -> entityManagerAsync.insert(getEntity(), Duration.ZERO, (d) -> {}));
85+
assertThrows(UnsupportedOperationException.class, () -> entityManagerAsync.insert(getEntity(), Duration.ZERO, (d) -> {
86+
}));
8787
}
8888

8989
@Test
@@ -124,7 +124,7 @@ public void shouldRemoveEntityAsync() throws InterruptedException {
124124
DocumentEntity documentEntity = entityManager.insert(getEntity());
125125
Document id = documentEntity.find("name").get();
126126

127-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
127+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
128128
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
129129

130130
AtomicBoolean condition = new AtomicBoolean(false);
@@ -138,7 +138,7 @@ public void shouldRemoveEntityAsyncWithoutCallback() throws InterruptedException
138138
DocumentEntity entity = entityManager.insert(getEntity());
139139
Document id = entity.find(OrientDBConverter.RID_FIELD).get();
140140

141-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
141+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
142142
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
143143

144144
entityManagerAsync.delete(deleteQuery);
@@ -193,6 +193,26 @@ public void shouldFindAsyncWithNativeQueryMapParam() {
193193
assertEquals(reference.get().get(0), entity);
194194
}
195195

196+
197+
@Test
198+
public void shouldCount() {
199+
AtomicReference<DocumentEntity> entityAtomic = new AtomicReference<>();
200+
AtomicBoolean condition = new AtomicBoolean(false);
201+
AtomicLong value = new AtomicLong(0L);
202+
203+
entityManagerAsync.insert(getEntity(), entityAtomic::set);
204+
await().until(entityAtomic::get, notNullValue(DocumentEntity.class));
205+
206+
Consumer<Long> callback = l -> {
207+
value.set(l);
208+
condition.set(true);
209+
210+
};
211+
entityManagerAsync.count(COLLECTION_NAME, callback);
212+
await().untilTrue(condition);
213+
assertTrue(value.get() > 0);
214+
}
215+
196216
private DocumentEntity getEntity() {
197217
DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
198218
Map<String, Object> map = new HashMap<>();

orientdb-driver/src/test/java/org/jnosql/diana/orientdb/document/OrientDBDocumentCollectionManagerTest.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void setUp() {
6666
}
6767

6868
@Test
69-
public void shouldSave() {
69+
public void shouldInsert() {
7070
DocumentEntity entity = getEntity();
7171
DocumentEntity documentEntity = entityManager.insert(entity);
7272
assertNotNull(documentEntity);
@@ -121,7 +121,7 @@ public void shouldRemoveEntity() {
121121

122122
Document id = documentEntity.find("name").get();
123123

124-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
124+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
125125
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
126126
entityManager.delete(deleteQuery);
127127
assertTrue(entityManager.select(query).isEmpty());
@@ -132,7 +132,7 @@ public void shouldFindDocument() {
132132
DocumentEntity entity = entityManager.insert(getEntity());
133133
Document id = entity.find("name").get();
134134

135-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
135+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
136136
List<DocumentEntity> entities = entityManager.select(query);
137137
assertFalse(entities.isEmpty());
138138
assertThat(entities, contains(entity));
@@ -165,7 +165,7 @@ public void shouldSaveSubDocument() {
165165
entity.add(Document.of("phones", Document.of("mobile", "1231231")));
166166
DocumentEntity entitySaved = entityManager.insert(entity);
167167
Document id = entitySaved.find("name").get();
168-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
168+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
169169
DocumentEntity entityFound = entityManager.select(query).get(0);
170170
Document subDocument = entityFound.find("phones").get();
171171
List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
@@ -179,7 +179,7 @@ public void shouldSaveSubDocument2() {
179179
entity.add(Document.of("phones", Arrays.asList(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231"))));
180180
DocumentEntity entitySaved = entityManager.insert(entity);
181181
Document id = entitySaved.find("name").get();
182-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
182+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
183183
DocumentEntity entityFound = entityManager.select(query).get(0);
184184
Document subDocument = entityFound.find("phones").get();
185185
List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
@@ -194,11 +194,11 @@ public void shouldQueryAnd() {
194194
entityManager.insert(entity);
195195

196196

197-
DocumentQuery query = select().from(COLLECTION_NAME).where("name").eq("Poliana")
198-
.and("age").gte( 10).build();
197+
DocumentQuery query = select().from(COLLECTION_NAME).where("name").eq("Poliana")
198+
.and("age").gte(10).build();
199199

200-
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where("name").eq("Poliana")
201-
.and("age").gte( 10).build();
200+
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where("name").eq("Poliana")
201+
.and("age").gte(10).build();
202202

203203
assertFalse(entityManager.select(query).isEmpty());
204204

@@ -213,11 +213,10 @@ public void shouldQueryOr() {
213213
entityManager.insert(entity);
214214

215215

216-
217-
DocumentQuery query = select().from(COLLECTION_NAME).where("name").eq("Poliana")
216+
DocumentQuery query = select().from(COLLECTION_NAME).where("name").eq("Poliana")
218217
.or("age").gte(10).build();
219218

220-
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where("name").eq("Poliana")
219+
DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where("name").eq("Poliana")
221220
.or("age").gte(10).build();
222221

223222
assertFalse(entityManager.select(query).isEmpty());
@@ -395,7 +394,7 @@ public void shouldLive() throws InterruptedException {
395394
DocumentEntity entity = entityManager.insert(getEntity());
396395
Document id = entity.find("name").get();
397396

398-
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
397+
DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
399398

400399
entityManager.live(query, OrientDBLiveCallbackBuilder.builder().onCreate(callback).build());
401400
entityManager.insert(getEntity());
@@ -470,6 +469,15 @@ public void shouldRetrieveListSubdocumentList() {
470469
assertTrue(contacts.stream().allMatch(d -> d.size() == 3));
471470
}
472471

472+
@Test
473+
public void shouldCount() {
474+
DocumentEntity entity = getEntity();
475+
DocumentEntity documentEntity = entityManager.insert(entity);
476+
assertNotNull(documentEntity);
477+
assertTrue(entityManager.count(COLLECTION_NAME) > 0);
478+
479+
}
480+
473481
private DocumentEntity createSubdocumentList() {
474482
DocumentEntity entity = DocumentEntity.of("AppointmentBook");
475483
entity.add(Document.of("_id", new Random().nextInt()));

0 commit comments

Comments
 (0)