Search in sources :

Example 86 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerTest method shouldRemoveEntity.

@Test
public void shouldRemoveEntity() {
    DocumentEntity documentEntity = entityManager.insert(getEntity());
    Optional<Document> id = documentEntity.find("_id");
    DocumentQuery query = select().from(COLLECTION_NAME).where("_id").eq(id.get().get()).build();
    DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where("_id").eq(id.get().get()).build();
    entityManager.delete(deleteQuery);
    assertTrue(entityManager.select(query).isEmpty());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 87 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerTest method shouldSaveSubDocument2.

@Test
public void shouldSaveSubDocument2() {
    DocumentEntity entity = getEntity();
    entity.add(Document.of("phones", asList(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231"))));
    DocumentEntity entitySaved = entityManager.insert(entity);
    Document id = entitySaved.find("_id").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentEntity entityFound = entityManager.select(query).get(0);
    Document subDocument = entityFound.find("phones").get();
    List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
    });
    assertThat(documents, containsInAnyOrder(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231")));
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 88 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class DefaultOrientDBDocumentCollectionManagerAsync method insert.

@Override
public void insert(DocumentEntity entity, Consumer<DocumentEntity> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException {
    requireNonNull(entity, "Entity is required");
    requireNonNull(callBack, "Callback is required");
    ODatabaseDocumentTx tx = pool.acquire();
    ODocument document = new ODocument(entity.getName());
    Map<String, Object> entityValues = entity.toMap();
    entityValues.keySet().stream().forEach(k -> document.field(k, entityValues.get(k)));
    ORecordCallback<Number> createCallBack = (rid, clusterPosition) -> {
        entity.add(Document.of(RID_FIELD, rid.toString()));
        callBack.accept(entity);
    };
    ORecordCallback<Integer> updateCallback = (rid, version) -> {
        entity.add(Document.of(RID_FIELD, rid.toString()));
        entity.add(Document.of(VERSION_FIELD, version));
        callBack.accept(entity);
    };
    tx.save(document, null, ASYNCHRONOUS, false, createCallBack, updateCallback);
}
Also used : ASYNCHRONOUS(com.orientechnologies.orient.core.db.ODatabase.OPERATION_MODE.ASYNCHRONOUS) VERSION_FIELD(org.jnosql.diana.orientdb.document.OrientDBConverter.VERSION_FIELD) Document(org.jnosql.diana.api.document.Document) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) ORecordCallback(com.orientechnologies.orient.core.storage.ORecordCallback) RID_FIELD(org.jnosql.diana.orientdb.document.OrientDBConverter.RID_FIELD) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OPartitionedDatabasePool(com.orientechnologies.orient.core.db.OPartitionedDatabasePool) ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) QueryOSQLFactory.toAsync(org.jnosql.diana.orientdb.document.QueryOSQLFactory.toAsync) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 89 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class OrientDBConverter method convert.

static DocumentEntity convert(ODocument document) {
    DocumentEntity entity = DocumentEntity.of(document.getClassName());
    Stream.of(document.fieldNames()).map(f -> Document.of(f, convert((Object) document.field(f)))).forEach(entity::add);
    entity.add(Document.of(RID_FIELD, document.field(RID_FIELD).toString()));
    entity.add(Document.of(VERSION_FIELD, document.getVersion()));
    return entity;
}
Also used : Document(org.jnosql.diana.api.document.Document) HashedMap(org.apache.commons.collections.map.HashedMap) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) Collections.singletonMap(java.util.Collections.singletonMap) ValueUtil(org.jnosql.diana.driver.ValueUtil) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity)

Example 90 with Document

use of org.jnosql.diana.api.document.Document in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentCollectionManagerAsyncTest method setUp.

@BeforeEach
public void setUp() {
    ElasticsearchDocumentConfiguration configuration = new ElasticsearchDocumentConfiguration();
    ElasticsearchDocumentCollectionManagerFactory managerFactory = configuration.get();
    entityManagerAsync = managerFactory.getAsync(COLLECTION_NAME);
    entityManager = managerFactory.get(INDEX);
    DocumentEntity documentEntity = getEntity();
    Document id = documentEntity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    entityManagerAsync.delete(deleteQuery);
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Document (org.jnosql.diana.api.document.Document)126 Test (org.junit.jupiter.api.Test)102 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)94 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)71 List (java.util.List)41 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)37 ArrayList (java.util.ArrayList)25 DocumentCondition (org.jnosql.diana.api.document.DocumentCondition)25 Arrays.asList (java.util.Arrays.asList)23 HashMap (java.util.HashMap)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Map (java.util.Map)11 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)11 TypeReference (org.jnosql.diana.api.TypeReference)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Matchers.contains (org.hamcrest.Matchers.contains)10 DocumentQueryBuilder.select (org.jnosql.diana.api.document.query.DocumentQueryBuilder.select)10 Arrays (java.util.Arrays)8 Optional (java.util.Optional)8 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)8