Java Code Examples for io.objectbox.query.Query#setParameter()

The following examples show how to use io.objectbox.query.Query#setParameter() . 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: IndexReaderRenewTest.java    From objectbox-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testOldReaderWithIndex() {
    final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class);
    final int initialValue = 1;

    final Query<EntityLongIndex> query = box.query().equal(EntityLongIndex_.indexedLong, 0).build();
    assertNull(query.findUnique());
    System.out.println("BEFORE put: " + box.getReaderDebugInfo());
    System.out.println("count before: " + box.count());

    box.put(createEntityLongIndex(initialValue));

    System.out.println("AFTER put: " + box.getReaderDebugInfo());
    System.out.println("count after: " + box.count());

    query.setParameter(EntityLongIndex_.indexedLong, initialValue);
    assertNotNull(query.findUnique());

    query.setParameter(EntityLongIndex_.indexedLong, 0);
    assertNull(query.findUnique());
}
 
Example 2
Source File: IndexReaderRenewTest.java    From objectbox-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testOverwriteIndexedValue() throws InterruptedException {
    final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class);
    final int initialValue = 1;

    final EntityLongIndex[] transformResults = {null, null, null};
    final CountDownLatch transformLatch1 = new CountDownLatch(1);
    final CountDownLatch transformLatch2 = new CountDownLatch(1);
    final AtomicInteger transformerCallCount = new AtomicInteger();

    final Query<EntityLongIndex> query = box.query().equal(EntityLongIndex_.indexedLong, 0).build();
    store.subscribe(EntityLongIndex.class).transform(javaClass -> {
        int callCount = transformerCallCount.incrementAndGet();
        if (callCount == 1) {
            query.setParameter(EntityLongIndex_.indexedLong, 1);
            EntityLongIndex unique = query.findUnique();
            transformLatch1.countDown();
            return unique;
        } else if (callCount == 2) {
            query.setParameter(EntityLongIndex_.indexedLong, 1);
            transformResults[0] = query.findUnique();
            transformResults[1] = query.findUnique();
            query.setParameter(EntityLongIndex_.indexedLong, 0);
            transformResults[2] = query.findUnique();
            transformLatch2.countDown();
            return transformResults[0];
        } else {
            throw new RuntimeException("Unexpected: " + callCount);
        }
    }).observer(data -> {
        // Dummy
    });

    assertTrue(transformLatch1.await(5, TimeUnit.SECONDS));
    box.put(createEntityLongIndex(initialValue));

    assertTrue(transformLatch2.await(5, TimeUnit.SECONDS));
    assertEquals(2, transformerCallCount.intValue());

    assertNotNull(transformResults[0]);
    assertNotNull(transformResults[1]);
    assertNull(transformResults[2]);

    query.setParameter(EntityLongIndex_.indexedLong, initialValue);
    assertNotNull(query.findUnique());

    query.setParameter(EntityLongIndex_.indexedLong, initialValue);
    assertNotNull(query.findUnique());
    assertNotNull(query.findUnique());
}
 
Example 3
Source File: IndexReaderRenewTest.java    From objectbox-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testOldReaderInThread() throws InterruptedException {
    final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class);
    final int initialValue = 1;

    final EntityLongIndex[] results = new EntityLongIndex[5];
    final CountDownLatch latchRead1 = new CountDownLatch(1);
    final CountDownLatch latchPut = new CountDownLatch(1);
    final CountDownLatch latchRead2 = new CountDownLatch(1);
    final Query<EntityLongIndex> query = box.query().equal(EntityLongIndex_.indexedLong, 0).build();

    new Thread(() -> {
        query.setParameter(EntityLongIndex_.indexedLong, initialValue);
        EntityLongIndex unique = query.findUnique();
        assertNull(unique);
        latchRead1.countDown();
        System.out.println("BEFORE put: " + box.getReaderDebugInfo());
        System.out.println("count before: " + box.count());

        try {
            latchPut.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        System.out.println("AFTER put: " + box.getReaderDebugInfo());
        System.out.println("count after: " + box.count());

        query.setParameter(EntityLongIndex_.indexedLong, initialValue);
        results[0] = query.findUnique();
        results[1] = box.get(1);
        results[2] = query.findUnique();
        query.setParameter(EntityLongIndex_.indexedLong, 0);
        results[3] = query.findUnique();
        latchRead2.countDown();
        box.closeThreadResources();
    }).start();

    assertTrue(latchRead1.await(5, TimeUnit.SECONDS));
    box.put(createEntityLongIndex(initialValue));
    latchPut.countDown();

    assertTrue(latchRead2.await(5, TimeUnit.SECONDS));

    assertNotNull(results[1]);
    assertNotNull(results[0]);
    assertNotNull(results[2]);
    assertNull(results[3]);

    query.setParameter(EntityLongIndex_.indexedLong, initialValue);
    assertNotNull(query.findUnique());

    query.setParameter(EntityLongIndex_.indexedLong, initialValue);
    assertNotNull(query.findUnique());
    assertNotNull(query.findUnique());
}