io.vlingo.symbio.store.QueryExpression Java Examples

The following examples show how to use io.vlingo.symbio.store.QueryExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObjectEntity.java    From vlingo-lattice with Mozilla Public License 2.0 6 votes vote down vote up
private QueryExpression queryExpression() {
  if (queryExpression == null) {
    if (info.queryObjectExpression.isListQueryExpression()) {
      queryExpression =
              ListQueryExpression.using(
                      info.queryObjectExpression.type,
                      info.queryObjectExpression.query,
                      stateObject().queryList());
    } else if (info.queryObjectExpression.isMapQueryExpression()) {
      queryExpression =
              MapQueryExpression.using(
                      info.queryObjectExpression.type,
                      info.queryObjectExpression.query,
                      stateObject().queryMap());
    } else {
      throw new IllegalStateException("Unknown QueryExpression type: " + queryExpression);
    }
  }
  return queryExpression;
}
 
Example #2
Source File: InMemoryObjectStoreDelegate.java    From vlingo-symbio with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public QuerySingleResult queryObject(final QueryExpression expression) {
  final String id;
  if (expression.isListQueryExpression()) {
    id = idParameterAsString(expression.asListQueryExpression().parameters.get(0));
  } else if (expression.isMapQueryExpression()) {
    id = idParameterAsString(expression.asMapQueryExpression().parameters.get("id"));
  } else {
    throw new StorageException(Result.Error, "Unknown query type: " + expression);
  }

  final Map<Long, State<?>> store = stores.computeIfAbsent(expression.type, (type) -> new HashMap<>());
  final State<?> found = (id == null || id.equals("-1")) ? null : store.get(Long.parseLong(id));

  final Object result = Optional
          .ofNullable(found)
          .map(stateAdapterProvider::fromRaw)
          .orElse(null);
  return new QuerySingleResult(result);
}
 
Example #3
Source File: InMemoryObjectStoreActorTest.java    From vlingo-symbio with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testThatObjectPersistsQueries() {
  dispatcher.afterCompleting(1);
  final AccessSafely persistAccess = persistInterest.afterCompleting(1);
  final Person person = new Person("Tom Jones", 85);
  final Test1Source source = new Test1Source();
  objectStore.persist(StateSources.of(person, source), persistInterest);
  final int persistSize = persistAccess.readFrom("size");
  assertEquals(1, persistSize);
  assertEquals(person, persistAccess.readFrom("object", 0));

  final QueryExpression query = MapQueryExpression
          .using(Person.class, "find", MapQueryExpression.map("id", "" + person.persistenceId()));

  final AccessSafely queryAccess = queryResultInterest.afterCompleting(1);
  objectStore.queryObject(query, queryResultInterest, null);
  final int querySize = queryAccess.readFrom("size");
  assertEquals(1, querySize);
  assertEquals(person, queryAccess.readFrom("object", 0));

  assertEquals(1, dispatcher.dispatchedCount());
  final Dispatchable<Entry<?>, State<?>> dispatched = dispatcher.getDispatched().get(0);
  validateDispatchedState(person, dispatched);

  final List<Entry<?>> dispatchedEntries = dispatched.entries();
  Assert.assertEquals(1, dispatchedEntries.size());
  final Entry<?> entry = dispatchedEntries.get(0);
  Assert.assertNotNull(entry.id());
  Assert.assertEquals(source.getClass().getName(), entry.typeName());
  Assert.assertEquals(Metadata.nullMetadata(), entry.metadata());
}
 
Example #4
Source File: CalculationQueriesActor.java    From vlingo-examples with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Completes<Set<CalculationState>> allCalculations() {
    final QueryExpression queryExpression =
            MapQueryExpression.using(CalculationState.class, "findAll");

    return this.queryAll(Set.class, queryExpression, states -> (Set) states);
}
 
Example #5
Source File: OrderQueriesActor.java    From vlingo-examples with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Completes<Set<OrderState>> allOrders() {
    final QueryExpression queryExpression =
            MapQueryExpression.using(OrderState.class, "findAll");

    return this.queryAll(Set.class, queryExpression, states -> (Set) states);
}
 
Example #6
Source File: StockQueriesActor.java    From vlingo-examples with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Completes<StockState> queryByLocation(final Location location) {
    final QueryExpression queryExpression =
            MapQueryExpression.using(StockState.class, "findAll");

    final Function<Set<StockState>, StockState> filter = states ->
            states.stream().filter(state -> state.locatedIn(location)).findFirst().get();

    return this.queryAll(Set.class, queryExpression, filter);
}
 
Example #7
Source File: ObjectQueryFailedExceptionTest.java    From vlingo-lattice with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testThatFailedHasExceptionInfo() {
  final Exception cause = new Exception("TestInner", new Exception());
  final QueryAttempt<?,?,?> queryAttempt = new QueryAttempt(QueryAttempt.Cardinality.All, Object.class, QueryExpression.using(Object.class, ""), CompletionTranslator.translatorOrNull((o) -> null, null));
  final ObjectQueryFailedException e = new ObjectQueryFailedException(queryAttempt, "TestOuter", cause);

  Assert.assertNotNull(e);
  Assert.assertNotNull(e.queryAttempt);
  Assert.assertEquals("TestOuter", e.getMessage());
  Assert.assertNotNull(e.getCause());
  Assert.assertEquals("TestInner", e.getCause().getMessage());
  Assert.assertNotNull(e.getMessage());
  Assert.assertNotNull(e.getCause().getCause());
  Assert.assertNull(e.getCause().getCause().getMessage());
}
 
Example #8
Source File: ObjectQueryFailedExceptionTest.java    From vlingo-lattice with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testThatFailedHasAttempt() {
  final QueryAttempt<?,?,?> queryAttempt = new QueryAttempt(QueryAttempt.Cardinality.All, Object.class, QueryExpression.using(Object.class, ""), CompletionTranslator.translatorOrNull((o) -> null, null));
  final ObjectQueryFailedException e = new ObjectQueryFailedException(queryAttempt);

  Assert.assertNotNull(e);
  Assert.assertNotNull(e.queryAttempt);
  Assert.assertEquals(QueryAttempt.Cardinality.All, e.queryAttempt.cardinality);
  Assert.assertNotNull(e.queryAttempt.stateObjectType);
  Assert.assertNotNull(e.queryAttempt.query);
  Assert.assertNotNull(e.queryAttempt.completionTranslator);
  Assert.assertNull(e.getMessage());
  Assert.assertNull(e.getCause());
}
 
Example #9
Source File: StateObjectQueryActor.java    From vlingo-lattice with Mozilla Public License 2.0 5 votes vote down vote up
protected <S,O,R> Completes<R> queryObject(
        final Class<S> stateObjectType,
        final QueryExpression query,
        final Function<O,R> andThen) {

  objectStore.queryObject(
          query,
          queryResultInterest,
          QueryAttempt.with(Cardinality.Object, stateObjectType, query, CompletionTranslator.translatorOrNull(andThen, completesEventually())));

  return completes();
}
 
Example #10
Source File: StateObjectQueryActor.java    From vlingo-lattice with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Answer the {@code Completes<R>} through which the queried and translated result is provided.
 * @param stateObjectType the {@code Class<S>} of the type of the translated result elements
 * @param query the QueryExpression used to execute the query
 * @param andThen the {@code Function<O,R>} used to translate the O outcome to the R result
 * @return {@code Completes<R>}
 * @param <S> the type of the StateObject being queried
 * @param <O> the type of the outcome of the query
 * @param <R> the final result, being a {@code List<S>}
 */
protected <S,O,R> Completes<R> queryAll(
        final Class<S> stateObjectType,
        final QueryExpression query,
        final Function<O,R> andThen) {

  objectStore.queryAll(
          query,
          queryResultInterest,
          QueryAttempt.with(Cardinality.All, stateObjectType, query, CompletionTranslator.translatorOrNull(andThen, completesEventually())));

  return completes();
}
 
Example #11
Source File: InMemoryObjectStoreActorTest.java    From vlingo-symbio with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testThatMultiPersistQueryResolves() {
  dispatcher.afterCompleting(3);
  final AccessSafely persistAllAccess = persistInterest.afterCompleting(1);

  final Person person1 = new Person("Tom Jones", 78);
  final Person person2 = new Person("Dean Martin", 78);
  final Person person3 = new Person("Sally Struthers", 71);
  objectStore.persistAll(Arrays.asList(StateSources.of(person1), StateSources.of(person2), StateSources.of(person3)), persistInterest);
  final int persistSize = persistAllAccess.readFrom("size");
  assertEquals(3, persistSize);

  final AccessSafely queryAllAccess = queryResultInterest.afterCompleting(1);
  objectStore.queryAll(QueryExpression.using(Person.class, "findAll"), queryResultInterest, null);
  final int querySize = queryAllAccess.readFrom("size");
  assertEquals(3, querySize);
  assertEquals(person1, queryAllAccess.readFrom("object", 0));
  assertEquals(person2, queryAllAccess.readFrom("object", 1));
  assertEquals(person3, queryAllAccess.readFrom("object", 2));

  assertEquals(3, dispatcher.dispatchedCount());

  Dispatchable<Entry<?>, State<?>> dispatched = dispatcher.getDispatched().get(0);
  validateDispatchedState(person1, dispatched);
  List<Entry<?>> dispatchedEntries = dispatched.entries();
  Assert.assertTrue(dispatchedEntries.isEmpty());

  dispatched = dispatcher.getDispatched().get(1);
  validateDispatchedState(person2, dispatched);
  dispatchedEntries = dispatched.entries();
  Assert.assertTrue(dispatchedEntries.isEmpty());

  dispatched = dispatcher.getDispatched().get(2);
  validateDispatchedState(person3, dispatched);
  dispatchedEntries = dispatched.entries();
  Assert.assertTrue(dispatchedEntries.isEmpty());
}
 
Example #12
Source File: InMemoryObjectStoreActor.java    From vlingo-symbio with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void queryObject(final QueryExpression expression, final QueryResultInterest interest, final Object object) {
  final QuerySingleResult result = this.storeDelegate.queryObject(expression);

  if (result.stateObject != null) {
    interest.queryObjectResultedIn(Success.of(Result.Success), result, object);
  } else {
    interest.queryObjectResultedIn(Failure.of(new StorageException(Result.NotFound, "No object identified by expression: " + expression)), QuerySingleResult.of(null), object);
  }
}
 
Example #13
Source File: InMemoryObjectStoreDelegate.java    From vlingo-symbio with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public QueryMultiResults queryAll(final QueryExpression expression) {
  // NOTE: No query constraints accepted; selects all stored objects

  final Set<Object> all = new HashSet<>();
  final Map<Long, State<?>> store = stores.computeIfAbsent(expression.type, (type) -> new HashMap<>());
  for (final State<?> entry : store.values()) {
    final Object stateObject = stateAdapterProvider.fromRaw(entry);
    all.add(stateObject);
  }

  return new QueryMultiResults(all);
}
 
Example #14
Source File: StateStore__Proxy.java    From vlingo-symbio with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Completes<Stream> streamSomeUsing(final QueryExpression query) {
  if (!actor.isStopped()) {
    final SerializableConsumer<StateStore> consumer = (actor) -> actor.streamSomeUsing(query);
    final Completes<Stream> completes = new BasicCompletes<>(actor.scheduler());
    if (mailbox.isPreallocated()) { mailbox.send(actor, StateStore.class, consumer, Returns.value(completes), streamSomeUsingRepresentation5); }
    else { mailbox.send(new LocalMessage<StateStore>(actor, StateStore.class, consumer, Returns.value(completes), streamSomeUsingRepresentation5)); }
    return completes;
  } else {
    actor.deadLetters().failedDelivery(new DeadLetter(actor, streamSomeUsingRepresentation5));
  }
  return null;
}
 
Example #15
Source File: InMemoryObjectStoreActor.java    From vlingo-symbio with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void queryAll(final QueryExpression expression, final QueryResultInterest interest, final Object object) {
  final QueryMultiResults queryMultiResults = this.storeDelegate.queryAll(expression);
  interest.queryAllResultedIn(Success.of(Result.Success), queryMultiResults, object);
}
 
Example #16
Source File: QueryAttempt.java    From vlingo-lattice with Mozilla Public License 2.0 4 votes vote down vote up
public static <ST,OT,RT> QueryAttempt<ST,OT,RT> with(final Cardinality cardinality, final Class<ST> stateObjectType, final QueryExpression query, final CompletionTranslator<OT,RT> completionTranslator) {
  return new QueryAttempt<>(cardinality, stateObjectType, query, completionTranslator);
}
 
Example #17
Source File: QueryAttempt.java    From vlingo-lattice with Mozilla Public License 2.0 4 votes vote down vote up
public QueryAttempt(final Cardinality cardinality, final Class<S> stateObjectType, final QueryExpression query, final CompletionTranslator<O,R> completionTranslator) {
  this.cardinality = cardinality;
  this.stateObjectType = stateObjectType;
  this.query = query;
  this.completionTranslator = completionTranslator;
}
 
Example #18
Source File: InMemoryStateStoreActor.java    From vlingo-symbio with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Completes<Stream> streamSomeUsing(final QueryExpression query) {
  // TODO Auto-generated method stub
  return null;
}
 
Example #19
Source File: ObjectTypeRegistry.java    From vlingo-lattice with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Construct my default state.
 * @param store the ObjectStore instance
 * @param storeType the {@code Class<T>} Object type that uses the ObjectStore
 * @param storeName the String name of the ObjectStore
 * @param queryObjectExpression the QueryExpression used to retrieve a single instance
 * @param mapper the PersistentObjectMapper between Object type and persistent type
 */
public Info(final ObjectStore store, final Class<T> storeType, final String storeName, final QueryExpression queryObjectExpression, final StateObjectMapper mapper) {
  this.store = store;
  this.storeType = storeType;
  this.storeName = storeName;
  this.queryObjectExpression = queryObjectExpression;
  this.mapper = mapper;
}
 
Example #20
Source File: StateStoreReader.java    From vlingo-symbio with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Answer a new {@code Stream} for flowing all instances per {@code query}. Currently
 * the only supported query types are {@code QueryExpression} (no query parameters), and
 * {@code ListQueryExpression} (a {@code List<?>} of {@code Object} parameters).
 * In the future {@code ListQueryExpression} will be supported. Elements are streamed as
 * type {@code StateBundle} to the {@code Sink<StateBundle>}.
 * @param query the QueryExpression used to constrain the Stream
 * @return {@code Completes<Stream>}
 */
Completes<Stream> streamSomeUsing(final QueryExpression query);
 
Example #21
Source File: ObjectStoreDelegate.java    From vlingo-symbio with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Executes the query defined by {@code expression} that may result in one object.
 *
 * @param expression the QueryExpression
 * @return a {@code QuerySingleResult} with the result
 * @throws StorageException in case query failed
 */
QuerySingleResult queryObject(final QueryExpression expression) throws StorageException;
 
Example #22
Source File: ObjectStoreDelegate.java    From vlingo-symbio with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Executes the query defined by {@code expression} that may result in zero to many objects.
 *
 * @param expression the QueryExpression
 * @return a {@code Collection<QueryMultiResults>} with objects that matches the expression.
 * @throws StorageException in case query failed
 */
QueryMultiResults queryAll(final QueryExpression expression) throws StorageException;
 
Example #23
Source File: ObjectStoreReader.java    From vlingo-symbio with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Executes the query defined by {@code expression} that may result in one object,
 * and sends the result to {@code interest}.
 * @param expression the QueryExpression
 * @param interest the QueryResultInterest
 * @param object an Object sent to the QueryResultInterest when the query has succeeded or failed
 */
void queryObject(final QueryExpression expression, final QueryResultInterest interest, final Object object);
 
Example #24
Source File: ObjectStoreReader.java    From vlingo-symbio with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Executes the query defined by {@code expression} that may result in one object,
 * and sends the result to {@code interest}.
 * @param expression the QueryExpression
 * @param interest the QueryResultInterest
 */
default void queryObject(final QueryExpression expression, final QueryResultInterest interest) {
  queryObject(expression, interest, null);
}
 
Example #25
Source File: ObjectStoreReader.java    From vlingo-symbio with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Executes the query defined by {@code expression} that may result in zero to many objects,
 * and sends the result to {@code interest}.
 * @param expression the QueryExpression
 * @param interest the QueryResultInterest
 * @param object an Object sent to the QueryResultInterest when the query has succeeded or failed
 */
void queryAll(final QueryExpression expression, final QueryResultInterest interest, final Object object);
 
Example #26
Source File: ObjectStoreReader.java    From vlingo-symbio with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Executes the query defined by {@code expression} that may result in zero to many objects,
 * and sends the result to {@code interest}.
 * @param expression the QueryExpression
 * @param interest the QueryResultInterest
 */
default void queryAll(final QueryExpression expression, final QueryResultInterest interest) {
  queryAll(expression, interest, null);
}