Search in sources :

Example 1 with BinaryTestKey

use of org.apache.ignite.cache.store.jdbc.model.BinaryTestKey in project ignite by apache.

the class CacheJdbcPojoStoreTest method testLoadCache.

/**
 * @throws Exception If failed.
 */
@Test
public void testLoadCache() throws Exception {
    Connection conn = store.openConnection(false);
    PreparedStatement orgStmt = conn.prepareStatement("INSERT INTO Organization(id, name, city) VALUES (?, ?, ?)");
    for (int i = 0; i < ORGANIZATION_CNT; i++) {
        orgStmt.setInt(1, i);
        orgStmt.setString(2, "name" + i);
        orgStmt.setString(3, "city" + i % 10);
        orgStmt.addBatch();
    }
    orgStmt.executeBatch();
    U.closeQuiet(orgStmt);
    conn.commit();
    PreparedStatement prnStmt = conn.prepareStatement("INSERT INTO Person(id, org_id, name) VALUES (?, ?, ?)");
    for (int i = 0; i < PERSON_CNT; i++) {
        prnStmt.setInt(1, i);
        prnStmt.setInt(2, i % 100);
        prnStmt.setString(3, "name" + i);
        prnStmt.addBatch();
    }
    prnStmt.executeBatch();
    conn.commit();
    U.closeQuiet(prnStmt);
    PreparedStatement prnComplexStmt = conn.prepareStatement("INSERT INTO Person_Complex(id, org_id, city_id, name, salary) VALUES (?, ?, ?, ?, ?)");
    for (int i = 0; i < PERSON_CNT; i++) {
        prnComplexStmt.setInt(1, i);
        prnComplexStmt.setInt(2, i % 500);
        prnComplexStmt.setInt(3, i % 100);
        prnComplexStmt.setString(4, "name" + i);
        if (i > 0)
            prnComplexStmt.setInt(5, 1000 + i * 500);
        else
            // Add person with null salary
            prnComplexStmt.setNull(5, Types.INTEGER);
        prnComplexStmt.addBatch();
    }
    prnComplexStmt.executeBatch();
    U.closeQuiet(prnComplexStmt);
    conn.commit();
    U.closeQuiet(prnStmt);
    PreparedStatement binaryStmt = conn.prepareStatement("INSERT INTO Binary_Entries(key, val) VALUES (?, ?)");
    byte[] bytes = new byte[16];
    for (byte i = 0; i < 16; i++) bytes[i] = i;
    binaryStmt.setInt(1, 1);
    binaryStmt.setBinaryStream(2, new ByteArrayInputStream(bytes));
    binaryStmt.addBatch();
    binaryStmt.executeBatch();
    U.closeQuiet(binaryStmt);
    conn.commit();
    U.closeQuiet(conn);
    final Collection<Object> orgKeys = new ConcurrentLinkedQueue<>();
    final Collection<Object> prnKeys = new ConcurrentLinkedQueue<>();
    final Collection<Object> prnComplexKeys = new ConcurrentLinkedQueue<>();
    final Collection<Object> binaryTestVals = new ConcurrentLinkedQueue<>();
    IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() {

        @Override
        public void apply(Object k, Object v) {
            if (binaryEnable) {
                if (k instanceof BinaryObject && v instanceof BinaryObject) {
                    BinaryObject key = (BinaryObject) k;
                    BinaryObject val = (BinaryObject) v;
                    String keyType = key.type().typeName();
                    String valType = val.type().typeName();
                    if (OrganizationKey.class.getName().equals(keyType) && Organization.class.getName().equals(valType))
                        orgKeys.add(key);
                    if (PersonKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
                        prnKeys.add(key);
                    if (PersonComplexKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
                        prnComplexKeys.add(key);
                    if (BinaryTestKey.class.getName().equals(keyType) && BinaryTest.class.getName().equals(valType))
                        binaryTestVals.add(val.field("bytes"));
                }
            } else {
                if (k instanceof OrganizationKey && v instanceof Organization)
                    orgKeys.add(k);
                else if (k instanceof PersonKey && v instanceof Person)
                    prnKeys.add(k);
                else if (k instanceof BinaryTestKey && v instanceof BinaryTest)
                    binaryTestVals.add(((BinaryTest) v).getBytes());
                else if (k instanceof PersonComplexKey && v instanceof Person) {
                    PersonComplexKey key = (PersonComplexKey) k;
                    Person val = (Person) v;
                    assertTrue("Key ID should be the same as value ID", key.getId() == val.getId());
                    assertTrue("Key orgID should be the same as value orgID", key.getOrgId() == val.getOrgId());
                    assertEquals("name" + key.getId(), val.getName());
                    prnComplexKeys.add(k);
                }
            }
        }
    };
    store.loadCache(c);
    assertEquals(ORGANIZATION_CNT, orgKeys.size());
    assertEquals(PERSON_CNT, prnKeys.size());
    assertEquals(PERSON_CNT, prnComplexKeys.size());
    assertEquals(1, binaryTestVals.size());
    assertTrue(Arrays.equals(bytes, (byte[]) binaryTestVals.iterator().next()));
    Collection<Object> tmpOrgKeys = new ArrayList<>(orgKeys);
    Collection<Object> tmpPrnKeys = new ArrayList<>(prnKeys);
    Collection<Object> tmpPrnComplexKeys = new ArrayList<>(prnComplexKeys);
    orgKeys.clear();
    prnKeys.clear();
    prnComplexKeys.clear();
    store.loadCache(c, OrganizationKey.class.getName(), "SELECT name, city, id FROM ORGANIZATION", PersonKey.class.getName(), "SELECT org_id, id, name FROM Person WHERE id < 1000");
    assertEquals(ORGANIZATION_CNT, orgKeys.size());
    assertEquals(1000, prnKeys.size());
    assertEquals(0, prnComplexKeys.size());
    store.deleteAll(tmpOrgKeys);
    store.deleteAll(tmpPrnKeys);
    store.deleteAll(tmpPrnComplexKeys);
    orgKeys.clear();
    prnKeys.clear();
    prnComplexKeys.clear();
    store.loadCache(c);
    assertTrue(orgKeys.isEmpty());
    assertTrue(prnKeys.isEmpty());
    assertTrue(prnComplexKeys.isEmpty());
}
Also used : Organization(org.apache.ignite.cache.store.jdbc.model.Organization) BinaryTest(org.apache.ignite.cache.store.jdbc.model.BinaryTest) PersonKey(org.apache.ignite.cache.store.jdbc.model.PersonKey) PersonComplexKey(org.apache.ignite.cache.store.jdbc.model.PersonComplexKey) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CI2(org.apache.ignite.internal.util.typedef.CI2) OrganizationKey(org.apache.ignite.cache.store.jdbc.model.OrganizationKey) BinaryObject(org.apache.ignite.binary.BinaryObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryTestKey(org.apache.ignite.cache.store.jdbc.model.BinaryTestKey) BinaryObject(org.apache.ignite.binary.BinaryObject) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Person(org.apache.ignite.cache.store.jdbc.model.Person) Test(org.junit.Test) GridAbstractCacheStoreSelfTest(org.apache.ignite.testframework.junits.cache.GridAbstractCacheStoreSelfTest) BinaryTest(org.apache.ignite.cache.store.jdbc.model.BinaryTest)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 BinaryTest (org.apache.ignite.cache.store.jdbc.model.BinaryTest)1 BinaryTestKey (org.apache.ignite.cache.store.jdbc.model.BinaryTestKey)1 Organization (org.apache.ignite.cache.store.jdbc.model.Organization)1 OrganizationKey (org.apache.ignite.cache.store.jdbc.model.OrganizationKey)1 Person (org.apache.ignite.cache.store.jdbc.model.Person)1 PersonComplexKey (org.apache.ignite.cache.store.jdbc.model.PersonComplexKey)1 PersonKey (org.apache.ignite.cache.store.jdbc.model.PersonKey)1 CI2 (org.apache.ignite.internal.util.typedef.CI2)1 GridAbstractCacheStoreSelfTest (org.apache.ignite.testframework.junits.cache.GridAbstractCacheStoreSelfTest)1 Test (org.junit.Test)1