Search in sources :

Example 1 with AnnotationIndexTypes

use of io.realm.entities.AnnotationIndexTypes in project realm-java by realm.

the class RealmAsyncQueryTests method distinctAsync.

@Test
@RunTestInLooperThread
public void distinctAsync() throws Throwable {
    Realm realm = looperThread.realm;
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    final RealmResults<AnnotationIndexTypes> distinctBool = realm.where(AnnotationIndexTypes.class).distinctAsync("indexBoolean");
    final RealmResults<AnnotationIndexTypes> distinctLong = realm.where(AnnotationIndexTypes.class).distinctAsync("indexLong");
    final RealmResults<AnnotationIndexTypes> distinctDate = realm.where(AnnotationIndexTypes.class).distinctAsync("indexDate");
    final RealmResults<AnnotationIndexTypes> distinctString = realm.where(AnnotationIndexTypes.class).distinctAsync("indexString");
    assertFalse(distinctBool.isLoaded());
    assertTrue(distinctBool.isValid());
    assertTrue(distinctBool.isEmpty());
    assertFalse(distinctLong.isLoaded());
    assertTrue(distinctLong.isValid());
    assertTrue(distinctLong.isEmpty());
    assertFalse(distinctDate.isLoaded());
    assertTrue(distinctDate.isValid());
    assertTrue(distinctDate.isEmpty());
    assertFalse(distinctString.isLoaded());
    assertTrue(distinctString.isValid());
    assertTrue(distinctString.isEmpty());
    final Runnable changeListenerDone = new Runnable() {

        final AtomicInteger signalCallbackFinished = new AtomicInteger(4);

        @Override
        public void run() {
            if (signalCallbackFinished.decrementAndGet() == 0) {
                looperThread.testComplete();
            }
        }
    };
    looperThread.keepStrongReference.add(distinctBool);
    looperThread.keepStrongReference.add(distinctLong);
    looperThread.keepStrongReference.add(distinctDate);
    looperThread.keepStrongReference.add(distinctString);
    distinctBool.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(2, distinctBool.size());
            changeListenerDone.run();
        }
    });
    distinctLong.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctLong.size());
            changeListenerDone.run();
        }
    });
    distinctDate.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctDate.size());
            changeListenerDone.run();
        }
    });
    distinctString.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctString.size());
            changeListenerDone.run();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 2 with AnnotationIndexTypes

use of io.realm.entities.AnnotationIndexTypes in project realm-java by realm.

the class RealmAsyncQueryTests method batchUpdateDifferentTypeOfQueries.

@Test
@RunTestInLooperThread
public void batchUpdateDifferentTypeOfQueries() {
    final Realm realm = looperThread.realm;
    realm.beginTransaction();
    for (int i = 0; i < 5; ) {
        AllTypes allTypes = realm.createObject(AllTypes.class);
        allTypes.setColumnLong(i);
        allTypes.setColumnString("data " + i % 3);
        allTypes = realm.createObject(AllTypes.class);
        allTypes.setColumnLong(i);
        allTypes.setColumnString("data " + (++i % 3));
    }
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    realm.commitTransaction();
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    RealmResults<AllTypes> findAllAsync = realm.where(AllTypes.class).findAllAsync();
    RealmResults<AllTypes> findAllSorted = realm.where(AllTypes.class).findAllSortedAsync("columnString", Sort.ASCENDING);
    RealmResults<AllTypes> findAllSortedMulti = realm.where(AllTypes.class).findAllSortedAsync(new String[] { "columnString", "columnLong" }, new Sort[] { Sort.ASCENDING, Sort.DESCENDING });
    RealmResults<AnnotationIndexTypes> findDistinct = realm.where(AnnotationIndexTypes.class).distinctAsync("indexString");
    looperThread.keepStrongReference.add(findAllAsync);
    looperThread.keepStrongReference.add(findAllSorted);
    looperThread.keepStrongReference.add(findAllSortedMulti);
    looperThread.keepStrongReference.add(findDistinct);
    final CountDownLatch queriesCompleted = new CountDownLatch(4);
    final CountDownLatch bgRealmClosedLatch = new CountDownLatch(1);
    final AtomicInteger batchUpdateCompleted = new AtomicInteger(0);
    final AtomicInteger findAllAsyncInvocation = new AtomicInteger(0);
    final AtomicInteger findAllSortedInvocation = new AtomicInteger(0);
    final AtomicInteger findAllSortedMultiInvocation = new AtomicInteger(0);
    final AtomicInteger findDistinctInvocation = new AtomicInteger(0);
    findAllAsync.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            switch(findAllAsyncInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    findAllSorted.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            switch(findAllSortedInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    findAllSortedMulti.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            switch(findAllSortedMultiInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    findDistinct.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            switch(findDistinctInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    // Waits for the queries to complete then sends a commit from
    // another thread to trigger a batch update of the 4 queries.
    new Thread() {

        @Override
        public void run() {
            try {
                queriesCompleted.await();
                Realm bgRealm = Realm.getInstance(realm.getConfiguration());
                bgRealm.beginTransaction();
                bgRealm.createObject(AllTypes.class);
                bgRealm.createObject(AnnotationIndexTypes.class);
                bgRealm.commitTransaction();
                bgRealm.close();
                bgRealmClosedLatch.countDown();
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
        }
    }.start();
}
Also used : AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 3 with AnnotationIndexTypes

use of io.realm.entities.AnnotationIndexTypes in project realm-java by realm.

the class TestHelper method populateForDistinctFieldsOrder.

/*
     * Fields order test for Chained or Multi-Arguments Distinct()
     *
     * The idea is to interweave different values in 2's multiplier and 3's multiplier in a way that
     * the outcome is different if the order of distinct* operations alternates. More numbers of
     * fields can be constructed with the combination of multipliers in prime numbers such as 2, 3,
     * and 5.
     *
     * An example is illustrated below.
     *
     * Object      : O1| O2| O3| O4| O5| O6
     * indexString : A | A | B | B | A | A
     * indexLong   : 1 | 1 | 1 | 2 | 2 | 2
     *
     * @param realm a {@link Realm} instance.
     * @param numberOfBlocks number of times set of unique objects should be created.
     */
public static void populateForDistinctFieldsOrder(Realm realm, long numberOfBlocks) {
    realm.beginTransaction();
    for (int i = 0; i < numberOfBlocks; i++) {
        for (int j = 0; j < 6; j++) {
            AnnotationIndexTypes obj = realm.createObject(AnnotationIndexTypes.class);
            obj.setIndexString((((j / 2) % 2) == 0) ? "A" : "B");
            obj.setIndexLong((j < 3) ? 1 : 2);
        }
    }
    realm.commitTransaction();
}
Also used : AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes)

Example 4 with AnnotationIndexTypes

use of io.realm.entities.AnnotationIndexTypes in project realm-java by realm.

the class RealmQueryTests method distinctAsync_withNullValues.

@Test
@RunTestInLooperThread
public void distinctAsync_withNullValues() throws Throwable {
    final AtomicInteger changeListenerCalled = new AtomicInteger(2);
    final Realm realm = looperThread.realm;
    final long numberOfBlocks = 25;
    // must be greater than 1
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, true);
    final RealmResults<AnnotationIndexTypes> distinctDate = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_DATE);
    final RealmResults<AnnotationIndexTypes> distinctString = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_STRING);
    final Runnable endTest = new Runnable() {

        @Override
        public void run() {
            if (changeListenerCalled.decrementAndGet() == 0) {
                looperThread.testComplete();
            }
        }
    };
    looperThread.keepStrongReference.add(distinctDate);
    looperThread.keepStrongReference.add(distinctString);
    distinctDate.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(1, distinctDate.size());
            endTest.run();
        }
    });
    distinctString.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(1, distinctString.size());
            endTest.run();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 5 with AnnotationIndexTypes

use of io.realm.entities.AnnotationIndexTypes in project realm-java by realm.

the class RealmResultsTests method populateForDistinct.

// RealmResults.distinct(): requires indexing, and type = boolean, integer, date, string.
private void populateForDistinct(Realm realm, long numberOfBlocks, long numberOfObjects, boolean withNull) {
    realm.beginTransaction();
    for (int i = 0; i < numberOfObjects * numberOfBlocks; i++) {
        for (int j = 0; j < numberOfBlocks; j++) {
            AnnotationIndexTypes obj = realm.createObject(AnnotationIndexTypes.class);
            obj.setIndexBoolean(j % 2 == 0);
            obj.setIndexLong(j);
            obj.setIndexDate(withNull ? null : new Date(1000 * (long) j));
            obj.setIndexString(withNull ? null : "Test " + j);
            obj.setNotIndexBoolean(j % 2 == 0);
            obj.setNotIndexLong(j);
            obj.setNotIndexDate(withNull ? null : new Date(1000 * (long) j));
            obj.setNotIndexString(withNull ? null : "Test " + j);
        }
    }
    realm.commitTransaction();
}
Also used : Date(java.util.Date) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes)

Aggregations

AnnotationIndexTypes (io.realm.entities.AnnotationIndexTypes)12 Test (org.junit.Test)8 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Date (java.util.Date)3 UiThreadTest (android.support.test.annotation.UiThreadTest)2 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)2 AllTypes (io.realm.entities.AllTypes)1 RunInLooperThread (io.realm.rule.RunInLooperThread)1 CountDownLatch (java.util.concurrent.CountDownLatch)1