Skip to content

Commit

Permalink
Refactoring to accept one fallback for each operation on Live Query
Browse files Browse the repository at this point in the history
  • Loading branch information
furlaneto committed Feb 14, 2018
1 parent 81a25f9 commit 832ead8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Contributors:
*
* Otavio Santana
* Lucas Furlaneto
*/
package org.jnosql.diana.orientdb.document;

Expand All @@ -28,7 +29,6 @@
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import static java.util.Objects.requireNonNull;
import static org.jnosql.diana.orientdb.document.OrientDBConverter.RID_FIELD;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void delete(DocumentDeleteQuery query) {


@Override
public List<DocumentEntity> select(DocumentQuery query) throws NullPointerException {
public List<DocumentEntity> select(DocumentQuery query) {
requireNonNull(query, "query is required");
try (ODatabaseDocumentTx tx = pool.acquire()) {
QueryOSQLFactory.QueryResult orientQuery = QueryOSQLFactory.to(query);
Expand All @@ -100,7 +100,7 @@ public List<DocumentEntity> select(DocumentQuery query) throws NullPointerExcept
}

@Override
public List<DocumentEntity> sql(String query, Object... params) throws NullPointerException {
public List<DocumentEntity> sql(String query, Object... params) {
requireNonNull(query, "query is required");
try (ODatabaseDocumentTx tx = pool.acquire()) {
List<ODocument> result = tx.command(QueryOSQLFactory.parse(query)).execute(params);
Expand All @@ -110,7 +110,7 @@ public List<DocumentEntity> sql(String query, Object... params) throws NullPoint
}

@Override
public List<DocumentEntity> sql(String query, Map<String, Object> params) throws NullPointerException {
public List<DocumentEntity> sql(String query, Map<String, Object> params) {
requireNonNull(query, "query is required");
requireNonNull(params, "params is required");

Expand All @@ -121,24 +121,23 @@ public List<DocumentEntity> sql(String query, Map<String, Object> params) throws
}

@Override
public void live(DocumentQuery query, Consumer<DocumentEntity> callBack) throws NullPointerException {
public void live(DocumentQuery query, OrientDBLiveCallback callbacks) {
requireNonNull(query, "query is required");
requireNonNull(callBack, "callback is required");
requireNonNull(callbacks, "callbacks is required");
try (ODatabaseDocumentTx tx = pool.acquire();) {
QueryOSQLFactory.QueryResult queryResult = QueryOSQLFactory.toLive(query, callBack);
QueryOSQLFactory.QueryResult queryResult = QueryOSQLFactory.toLive(query, callbacks);
tx.command(queryResult.getQuery()).execute(queryResult.getParams());
}
}

@Override
public void live(String query, Consumer<DocumentEntity> callBack, Object... params) throws NullPointerException {
public void live(String query, OrientDBLiveCallback callbacks, Object... params) {
requireNonNull(query, "query is required");
requireNonNull(callBack, "callback is required");
requireNonNull(callbacks, "callbacks is required");
try (ODatabaseDocumentTx tx = pool.acquire()) {
OLiveQuery<ODocument> liveQuery = new OLiveQuery<>(query, new LiveQueryLIstener(callBack));
OLiveQuery<ODocument> liveQuery = new OLiveQuery<>(query, new LiveQueryLIstener(callbacks));
tx.command(liveQuery).execute(params);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,36 @@
* Contributors:
*
* Otavio Santana
* Lucas Furlaneto
*/
package org.jnosql.diana.orientdb.document;


import com.orientechnologies.common.exception.OException;
import com.orientechnologies.orient.core.db.record.ORecordOperation;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OLiveResultListener;
import org.jnosql.diana.api.document.DocumentEntity;

import java.util.function.Consumer;

class LiveQueryLIstener implements OLiveResultListener {

private final Consumer<DocumentEntity> entityConsumer;
private final OrientDBLiveCallback callbacks;

LiveQueryLIstener(Consumer<DocumentEntity> entityConsumer) {
this.entityConsumer = entityConsumer;
LiveQueryLIstener(OrientDBLiveCallback callbacks) {
this.callbacks = callbacks;
}

@Override
public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
ODocument oDocument = (ODocument) iOp.getRecord();
DocumentEntity entity = OrientDBConverter.convert(oDocument);
entityConsumer.accept(entity);

if (callbacks.getCreateCallback() != null && ORecordOperation.CREATED == iOp.type) {
callbacks.getCreateCallback().accept(entity);
} else if (callbacks.getUpdateCallback() != null && ORecordOperation.UPDATED == iOp.type) {
callbacks.getUpdateCallback().accept(entity);
} else if (callbacks.getDeleteCallback() != null && ORecordOperation.DELETED == iOp.type) {
callbacks.getDeleteCallback().accept(entity);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,15 @@

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
* The orientdb implementation to {@link DocumentCollectionManager}, this implementation
* does not support TTL.
* <p>{@link OrientDBDocumentCollectionManager#insert(DocumentEntity, java.time.Duration)}</p>
* Also this implementation has support SQL query and also live query.
* <p>{@link OrientDBDocumentCollectionManager#sql(String, Object...)}</p>
* <p>{@link OrientDBDocumentCollectionManager#live(DocumentQuery, Consumer)}</p>
* <p>{@link OrientDBDocumentCollectionManager#live(String, Consumer, Object...)}</p>
*/
public interface OrientDBDocumentCollectionManager extends DocumentCollectionManager {



/**
* Find using query
*
Expand All @@ -44,7 +38,7 @@ public interface OrientDBDocumentCollectionManager extends DocumentCollectionMan
* @return the query result
* @throws NullPointerException when either query or params are null
*/
List<DocumentEntity> sql(String query, Object... params) throws NullPointerException;
List<DocumentEntity> sql(String query, Object... params);

/**
* Find using query
Expand All @@ -54,25 +48,23 @@ public interface OrientDBDocumentCollectionManager extends DocumentCollectionMan
* @return the query result
* @throws NullPointerException when either query or params are null
*/
List<DocumentEntity> sql(String query, Map<String, Object> params) throws NullPointerException;
List<DocumentEntity> sql(String query, Map<String, Object> params);

/**
* Execute live query
*
* @param query the query
* @param callBack when a new callback is coming
* @param query the query
* @param callbacks Callbacks for create, update and delete operations
* @throws NullPointerException when both query and callBack are null
*/
void live(DocumentQuery query, Consumer<DocumentEntity> callBack) throws NullPointerException;
void live(DocumentQuery query, OrientDBLiveCallback callbacks);

/**
* Execute live query
*
* @param query the string query, you must add "live"
* @param callBack when a new entity is coming
* @param params the params
* @throws NullPointerException when both query, callBack are null
* @param query the query
* @param callbacks Callbacks for create, update and delete operations
* @throws NullPointerException when both query and callBack are null
*/
void live(String query, Consumer<DocumentEntity> callBack, Object... params) throws NullPointerException;

void live(String query, OrientDBLiveCallback callbacks, Object... params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery;
import com.orientechnologies.orient.core.sql.query.OSQLQuery;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import org.jnosql.diana.api.document.DocumentEntity;
import org.jnosql.diana.api.document.DocumentQuery;
import org.jnosql.diana.orientdb.document.QueryOSQLConverter.Query;

Expand Down Expand Up @@ -74,9 +73,10 @@ public Object getResult() {
}), query.getParams());
}

static QueryResult toLive(DocumentQuery documentQuery, Consumer<DocumentEntity> callBack) {
static QueryResult toLive(DocumentQuery documentQuery, OrientDBLiveCallback callbacks) {
Query query = QueryOSQLConverter.select(documentQuery);
OLiveQuery<ODocument> liveQuery = new OLiveQuery<>(LIVE + query.getQuery(), new LiveQueryLIstener(callBack));
OLiveQuery<ODocument> liveQuery = new OLiveQuery<>(LIVE + query.getQuery(),
new LiveQueryLIstener(callbacks));
return new QueryResult(liveQuery, query.getParams());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Contributors:
*
* Otavio Santana
* Lucas Furlaneto
*/
package org.jnosql.diana.orientdb.document;

Expand All @@ -32,7 +33,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -386,14 +386,14 @@ public void shouldQueryMultiOrderBy() {
@Test
public void shouldLive() throws InterruptedException {
List<DocumentEntity> entities = new ArrayList<>();
Consumer<DocumentEntity> callback = entities::add;
OrientDBLiveCreateCallback<DocumentEntity> callback = entities::add;

DocumentEntity entity = entityManager.insert(getEntity());
Document id = entity.find("name").get();

DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();

entityManager.live(query, callback);
entityManager.live(query, OrientDBLiveCallback.builder().onCreate(callback).build());
entityManager.insert(getEntity());
Thread.sleep(3_000L);
assertFalse(entities.isEmpty());
Expand All @@ -402,12 +402,12 @@ public void shouldLive() throws InterruptedException {
@Test
public void shouldLiveWithNativeQuery() throws InterruptedException {
List<DocumentEntity> entities = new ArrayList<>();
Consumer<DocumentEntity> callback = entities::add;
OrientDBLiveCreateCallback<DocumentEntity> callback = entities::add;

DocumentEntity entity = entityManager.insert(getEntity());
Document name = entity.find("name").get();

entityManager.live("LIVE SELECT FROM person WHERE name = ?", callback, name.get());
entityManager.live("LIVE SELECT FROM person WHERE name = ?", OrientDBLiveCallback.builder().onCreate(callback).build(), name.get());
entityManager.insert(getEntity());
Thread.sleep(3_000L);
assertFalse(entities.isEmpty());
Expand Down

0 comments on commit 832ead8

Please sign in to comment.