Search in sources :

Example 1 with EntityIdFilter

use of io.spine.client.EntityIdFilter in project core-java by SpineEventEngine.

the class QueryBuilderShould method create_queries_with_ids.

@Test
public void create_queries_with_ids() {
    final int id1 = 314;
    final int id2 = 271;
    final Query query = factory().query().select(TestEntity.class).byId(id1, id2).build();
    assertNotNull(query);
    assertFalse(query.hasFieldMask());
    final Target target = query.getTarget();
    assertFalse(target.getIncludeAll());
    final EntityFilters entityFilters = target.getFilters();
    final EntityIdFilter idFilter = entityFilters.getIdFilter();
    final Collection<EntityId> idValues = idFilter.getIdsList();
    final Function<EntityId, Integer> transformer = new EntityIdUnpacker<>(int.class);
    final Collection<Integer> intIdValues = transform(idValues, transformer);
    assertSize(2, idValues);
    assertThat(intIdValues, containsInAnyOrder(id1, id2));
}
Also used : EntityId(io.spine.client.EntityId) Target(io.spine.client.Target) Query(io.spine.client.Query) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) Test(org.junit.Test)

Example 2 with EntityIdFilter

use of io.spine.client.EntityIdFilter in project core-java by SpineEventEngine.

the class AggregateQueryProcessor method doFetchWithFilters.

private Iterator<EntityRecord> doFetchWithFilters(Target target, FieldMask fieldMask) {
    final EntityFilters filters = target.getFilters();
    final boolean idsAreDefined = !filters.getIdFilter().getIdsList().isEmpty();
    if (!idsAreDefined) {
        return ImmutableList.<EntityRecord>of().iterator();
    }
    final EntityIdFilter idFilter = filters.getIdFilter();
    final Collection<AggregateStateId> stateIds = Collections2.transform(idFilter.getIdsList(), stateIdTransformer);
    final Iterator<EntityRecord> result = stateIds.size() == 1 ? readOne(stateIds.iterator().next(), fieldMask) : readMany(stateIds, fieldMask);
    return result;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters)

Example 3 with EntityIdFilter

use of io.spine.client.EntityIdFilter in project core-java by SpineEventEngine.

the class SubscriptionRecord method matchByFilters.

private static boolean matchByFilters(Object id, EntityFilters filters) {
    final boolean result;
    final EntityIdFilter givenIdFilter = filters.getIdFilter();
    final boolean idFilterSet = !EntityIdFilter.getDefaultInstance().equals(givenIdFilter);
    if (idFilterSet) {
        final Any idAsAny = Identifier.pack(id);
        final EntityId givenEntityId = EntityId.newBuilder().setId(idAsAny).build();
        final List<EntityId> idsList = givenIdFilter.getIdsList();
        result = idsList.contains(givenEntityId);
    } else {
        result = false;
    }
    return result;
}
Also used : EntityId(io.spine.client.EntityId) EntityIdFilter(io.spine.client.EntityIdFilter) Any(com.google.protobuf.Any)

Example 4 with EntityIdFilter

use of io.spine.client.EntityIdFilter in project core-java by SpineEventEngine.

the class RecordStorageShould method read_both_by_columns_and_IDs.

@Test
public void read_both_by_columns_and_IDs() {
    final I targetId = newId();
    final TestCounterEntity<I> targetEntity = new TestCounterEntity<>(targetId);
    final TestCounterEntity<I> noMatchEntity = new TestCounterEntity<>(newId());
    final TestCounterEntity<I> noMatchIdEntity = new TestCounterEntity<>(newId());
    final TestCounterEntity<I> deletedEntity = new TestCounterEntity<>(newId());
    targetEntity.setStatus(CANCELLED);
    deletedEntity.setStatus(CANCELLED);
    deletedEntity.delete();
    noMatchIdEntity.setStatus(CANCELLED);
    noMatchEntity.setStatus(DONE);
    write(targetEntity);
    write(noMatchEntity);
    write(noMatchIdEntity);
    write(deletedEntity);
    final EntityIdFilter idFilter = EntityIdFilter.newBuilder().addIds(toEntityId(targetId)).build();
    final CompositeColumnFilter columnFilter = all(eq("projectStatusValue", CANCELLED.getNumber()));
    final EntityFilters filters = EntityFilters.newBuilder().setIdFilter(idFilter).addFilter(columnFilter).build();
    final RecordStorage<I> storage = getStorage();
    final EntityQuery<I> query = EntityQueries.<I>from(filters, storage).withLifecycleFlags(storage);
    final Iterator<EntityRecord> read = storage.readAll(query, FieldMask.getDefaultInstance());
    final List<EntityRecord> readRecords = newArrayList(read);
    assertEquals(1, readRecords.size());
    final EntityRecord readRecord = readRecords.get(0);
    assertEquals(targetEntity.getState(), unpack(readRecord.getState()));
    assertEquals(targetId, Identifier.unpack(readRecord.getEntityId()));
}
Also used : CompositeColumnFilter(io.spine.client.CompositeColumnFilter) EntityRecord(io.spine.server.entity.EntityRecord) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) Test(org.junit.Test)

Example 5 with EntityIdFilter

use of io.spine.client.EntityIdFilter in project core-java by SpineEventEngine.

the class RecordBasedRepositoryShould method retrieve_all_records_with_entity_filters_and_field_mask_applied.

@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void retrieve_all_records_with_entity_filters_and_field_mask_applied() {
    final int count = 10;
    final List<E> entities = createAndStoreEntities(repository, count);
    final List<EntityId> ids = Lists.newLinkedList();
    // Find some of the records (half of them in this case)
    for (int i = 0; i < count / 2; i++) {
        final Message entityId = (Message) entities.get(i).getId();
        final EntityId id = EntityId.newBuilder().setId(pack(entityId)).build();
        ids.add(id);
    }
    final EntityIdFilter filter = EntityIdFilter.newBuilder().addAllIds(ids).build();
    final EntityFilters filters = EntityFilters.newBuilder().setIdFilter(filter).build();
    final Descriptors.Descriptor entityDescriptor = entities.get(0).getState().getDescriptorForType();
    final FieldMask firstFieldOnly = FieldMasks.maskOf(entityDescriptor, 1);
    final Iterator<E> readEntities = find(filters, firstFieldOnly);
    final Collection<E> foundList = newArrayList(readEntities);
    assertSize(ids.size(), foundList);
    for (E entity : foundList) {
        assertMatches(entity, firstFieldOnly);
    }
}
Also used : Message(com.google.protobuf.Message) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) EntityId(io.spine.client.EntityId) Descriptors(com.google.protobuf.Descriptors) FieldMask(com.google.protobuf.FieldMask) TenantAwareTest(io.spine.server.tenant.TenantAwareTest) Test(org.junit.Test)

Aggregations

EntityIdFilter (io.spine.client.EntityIdFilter)9 EntityFilters (io.spine.client.EntityFilters)7 EntityId (io.spine.client.EntityId)6 Test (org.junit.Test)6 Any (com.google.protobuf.Any)4 EntityRecord (io.spine.server.entity.EntityRecord)4 Message (com.google.protobuf.Message)2 CompositeColumnFilter (io.spine.client.CompositeColumnFilter)2 BoolValue (com.google.protobuf.BoolValue)1 Descriptors (com.google.protobuf.Descriptors)1 FieldMask (com.google.protobuf.FieldMask)1 ColumnFilter (io.spine.client.ColumnFilter)1 Query (io.spine.client.Query)1 Target (io.spine.client.Target)1 Version (io.spine.core.Version)1 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)1 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)1 LinkedList (java.util.LinkedList)1