Search in sources :

Example 1 with OptionalFields

use of net.morimekta.test.providence.storage.hazelcast.OptionalFields in project providence by morimekta.

the class HazelcastMessageBuilderStorageTest method testQueries.

@Test
public void testQueries() {
    // To ensure that the messages are stored in an indexable way, we add an index to query it directly.
    IMap<String, OptionalFields._Builder> map = instance.getMap(getClass().getName() + "_queries");
    HazelcastMessageBuilderStorage<String, OptionalFields, OptionalFields._Field, OptionalFields._Builder> storage = new HazelcastMessageBuilderStorage<>(map);
    map.addIndex(OptionalFields._Field.INTEGER_VALUE.getName(), true);
    for (int i = 0; i < 100; ++i) {
        storage.put(UUID.randomUUID().toString(), generator.generate(OptionalFields.kDescriptor));
    }
    generator.withGenerator(OptionalFields.kDescriptor, gen -> {
        gen.setAlwaysPresent(OptionalFields._Field.INTEGER_VALUE);
    });
    String theId = UUID.randomUUID().toString();
    OptionalFields toSearchFor = generator.generate(OptionalFields.kDescriptor);
    storage.put(theId, toSearchFor);
    EntryObject eo = new PredicateBuilder().getEntryObject();
    Predicate q = eo.get(OptionalFields._Field.INTEGER_VALUE.getName()).equal(toSearchFor.getIntegerValue());
    Set<Map.Entry<String, OptionalFields._Builder>> res = map.entrySet(q);
    // There is a 100 / 2 p 32 chance of collision.
    assertThat(res, hasSize(1));
    AtomicReference<String> id = new AtomicReference<>();
    AtomicReference<OptionalFields> value = new AtomicReference<>();
    res.forEach(e -> {
        id.set(e.getKey());
        value.set(e.getValue().build());
    });
    assertThat(id.get(), is(theId));
    assertThat(value.get(), is(toSearchFor));
}
Also used : EntryObject(com.hazelcast.query.EntryObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Predicate(com.hazelcast.query.Predicate) OptionalFields(net.morimekta.test.providence.storage.hazelcast.OptionalFields) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Test(org.junit.Test)

Example 2 with OptionalFields

use of net.morimekta.test.providence.storage.hazelcast.OptionalFields in project providence by morimekta.

the class TestBase method assertConformity.

void assertConformity(MessageListStore<String, OptionalFields, OptionalFields._Field> store) {
    assertThat(store.put("1234", list1), is(nullValue()));
    assertThat(store.putAll(ImmutableMap.of("2345", list2)).keySet(), is(empty()));
    assertThat(store.putBuilders("3456", list3), is(nullValue()));
    assertThat(store.putAllBuilders(ImmutableMap.of("4567", list4)).keySet(), is(empty()));
    assertThat(store.keys(), hasSize(4));
    assertThat(store.keys(), hasItems("1234", "2345", "3456", "4567"));
    assertThat(store.containsKey("1234"), is(true));
    assertThat(store.containsKey("5678"), is(false));
    List<OptionalFields> opts = store.get("1234");
    // Check that the values are the same.
    assertThat(opts, is(notNullValue()));
    assertThat(opts, is(list1));
    for (int i = 0; i < 100; ++i) {
        List<OptionalFields> list = new ArrayList<>();
        for (int j = 0; j < 10; ++j) {
            list.add(generator.generate(OptionalFields.kDescriptor));
        }
        store.put(UUID.randomUUID().toString(), list);
    }
    TreeSet<String> ids = new TreeSet<>(store.keys());
    assertThat(ids, hasSize(104));
    for (String id : ids) {
        assertThat(store.containsKey(id), Matchers.is(true));
    }
    TreeSet<String> missing = new TreeSet<>();
    for (int i = 0; i < 100; ++i) {
        String uuid = UUID.randomUUID().toString();
        assertThat(store.containsKey(uuid), Matchers.is(false));
        ;
        missing.add(uuid);
    }
    assertThat(store.getAll(missing).entrySet(), hasSize(0));
    store.remove(ids.first());
    store.removeAll(new ArrayList<>(ids).subList(45, 55));
    assertThat(store.getAll(ids).entrySet(), hasSize(93));
    Map<String, List<OptionalFields._Builder>> bld = store.getAllBuilders(new ArrayList<>(ids).subList(30, 45));
    bld.forEach((k, list) -> {
        for (OptionalFields._Builder b : list) {
            b.clearBinaryValue();
            b.clearBooleanValue();
            b.clearByteValue();
        }
    });
    store.putAllBuilders(bld);
    Map<String, List<OptionalFields>> tmp2 = store.getAll(bld.keySet());
    tmp2.forEach((k, list) -> {
        for (OptionalFields v : list) {
            assertThat(v.hasBooleanValue(), Matchers.is(false));
            assertThat(v.hasByteValue(), Matchers.is(false));
            assertThat(v.hasBinaryValue(), Matchers.is(false));
        }
    });
    OptionalFields._Builder builder = OptionalFields.builder();
    builder.setIntegerValue(10);
    builder.setBooleanValue(true);
    builder.setDoubleValue(12345.6789);
    String uuid = UUID.randomUUID().toString();
    store.putBuilders(uuid, ImmutableList.of(builder));
    List<OptionalFields> list = store.get(uuid);
    assertThat(list, hasSize(1));
    assertThat(list, Matchers.hasItem(builder.build()));
    List<OptionalFields._Builder> otherBuilder = store.getBuilders(uuid);
    assertThat(otherBuilder, is(Matchers.notNullValue()));
    assertThat(otherBuilder, hasSize(1));
    assertThat(otherBuilder.get(0).build(), Matchers.is(equalToMessage(builder.build())));
    String uuid2 = UUID.randomUUID().toString();
    List<OptionalFields> expectedEmpty = new ArrayList<>();
    store.put(uuid2, expectedEmpty);
    List<OptionalFields> actualEmpty = store.get(uuid2);
    assertThat(actualEmpty, is(not(sameInstance(expectedEmpty))));
    assertThat(actualEmpty, is(empty()));
}
Also used : ArrayList(java.util.ArrayList) OptionalFields(net.morimekta.test.providence.storage.hazelcast.OptionalFields) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 3 with OptionalFields

use of net.morimekta.test.providence.storage.hazelcast.OptionalFields in project providence by morimekta.

the class TestBase method assertConformity.

void assertConformity(MessageStore<String, OptionalFields, OptionalFields._Field> storage) {
    assertThat(storage.put("1234", orig1), is(nullValue()));
    assertThat(storage.putAll(ImmutableMap.of("2345", orig2)).keySet(), hasSize(0));
    assertThat(storage.putBuilder("3456", orig3), is(nullValue()));
    assertThat(storage.putAllBuilders(ImmutableMap.of("4567", orig4)).keySet(), hasSize(0));
    assertThat(storage.keys(), hasSize(4));
    assertThat(storage.keys(), hasItem("1234"));
    assertThat(storage.keys(), hasItem("2345"));
    assertThat(storage.keys(), hasItem("3456"));
    assertThat(storage.keys(), hasItem("4567"));
    OptionalFields get1 = storage.get("1234");
    OptionalFields._Builder get2 = storage.getBuilder("2345");
    OptionalFields get3 = storage.get("3456");
    OptionalFields._Builder get4 = storage.getBuilder("4567");
    OptionalFields get5 = storage.get("5678");
    OptionalFields._Builder get6 = storage.getBuilder("6789");
    // Check that the values are the same.
    assertThat(get1, is(notNullValue()));
    assertThat(get2, is(notNullValue()));
    assertThat(get3, is(notNullValue()));
    assertThat(get4, is(notNullValue()));
    assertThat(get5, is(nullValue()));
    assertThat(get6, is(nullValue()));
    assertThat(get1, CoreMatchers.is(orig1));
    assertThat(get2.build(), CoreMatchers.is(orig2));
    assertThat(get3, CoreMatchers.is(orig3.build()));
    assertThat(get4.build(), CoreMatchers.is(orig4.build()));
    // Check that getting all conforms.
    Map<String, OptionalFields> map1 = storage.getAll(ImmutableList.of("1234", "3456", "5678"));
    assertThat(map1.keySet(), hasSize(2));
    assertThat(map1, hasEntry("1234", orig1));
    assertThat(map1, Matchers.hasEntry("3456", orig3.build()));
    Map<String, OptionalFields._Builder> map2 = storage.getAllBuilders(ImmutableList.of("1234", "3456", "5678"));
    assertThat(map2.keySet(), hasSize(2));
    assertThat(map2, Matchers.hasEntry("1234", orig1.mutate()));
    assertThat(map2, hasEntry("3456", orig3));
    // Check remove and removeAll.
    assertThat(storage.remove("1234"), CoreMatchers.is(orig1));
    Map<String, OptionalFields> map3 = storage.removeAll(ImmutableList.of("1234", "3456"));
    assertThat(map3.keySet(), hasSize(1));
    assertThat(map3, Matchers.hasEntry("3456", orig3.build()));
    assertThat(storage.get("1234"), is(nullValue()));
    assertThat(storage.get("3456"), is(nullValue()));
    assertThat(storage.containsKey("1234"), is(false));
    assertThat(storage.containsKey("2345"), is(true));
    // Check put overriding.
    assertThat(storage.put("2345", orig1), CoreMatchers.is(orig2));
    assertThat(storage.putBuilder("2345", orig3), CoreMatchers.is(orig1.mutate()));
    Map<String, OptionalFields> put1 = storage.putAll(ImmutableMap.of("1234", orig1, "2345", orig2));
    assertThat(put1.keySet(), hasSize(1));
    assertThat(put1, Matchers.hasEntry("2345", orig3.build()));
    Map<String, OptionalFields._Builder> put2 = storage.putAllBuilders(ImmutableMap.of("3456", orig3, "4567", orig4));
    assertThat(put2.keySet(), hasSize(1));
    assertThat(put2, hasEntry("4567", orig4));
}
Also used : OptionalFields(net.morimekta.test.providence.storage.hazelcast.OptionalFields)

Aggregations

OptionalFields (net.morimekta.test.providence.storage.hazelcast.OptionalFields)3 ImmutableList (com.google.common.collect.ImmutableList)1 EntryObject (com.hazelcast.query.EntryObject)1 Predicate (com.hazelcast.query.Predicate)1 PredicateBuilder (com.hazelcast.query.PredicateBuilder)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1