use of org.apache.ignite.cache.store.jdbc.model.PersonKey in project ignite by apache.
the class CacheJdbcStoreAbstractMultithreadedSelfTest method testMultithreadedPut.
/**
* @throws Exception If failed.
*/
@Test
public void testMultithreadedPut() throws Exception {
IgniteInternalFuture<?> fut1 = runMultiThreadedAsync(new Callable<Object>() {
private final Random rnd = new Random();
@Nullable
@Override
public Object call() throws Exception {
for (int i = 0; i < TX_CNT; i++) {
IgniteCache<Object, Object> cache = jcache();
int id = rnd.nextInt(1000);
if (rnd.nextBoolean())
cache.put(new OrganizationKey(id), new Organization(id, "Name" + id, "City" + id));
else
cache.put(new PersonKey(id), new Person(id, rnd.nextInt(), new Date(System.currentTimeMillis()), "Name" + id, 1, Gender.random()));
}
return null;
}
}, 4, "put");
IgniteInternalFuture<?> fut2 = runMultiThreadedAsync(new Callable<Object>() {
private final Random rnd = new Random();
@Nullable
@Override
public Object call() throws Exception {
for (int i = 0; i < TX_CNT; i++) {
IgniteCache<Object, Object> cache = jcache();
int id = rnd.nextInt(1000);
if (rnd.nextBoolean())
cache.putIfAbsent(new OrganizationKey(id), new Organization(id, "Name" + id, "City" + id));
else
cache.putIfAbsent(new PersonKey(id), new Person(id, rnd.nextInt(), new Date(System.currentTimeMillis()), "Name" + id, i, Gender.random()));
}
return null;
}
}, 8, "putIfAbsent");
fut1.get();
fut2.get();
}
use of org.apache.ignite.cache.store.jdbc.model.PersonKey in project ignite by apache.
the class CacheJdbcStoreAbstractMultithreadedSelfTest method testMultithreadedPutAll.
/**
* @throws Exception If failed.
*/
@Test
public void testMultithreadedPutAll() throws Exception {
multithreaded(new Callable<Object>() {
private final Random rnd = new Random();
@Nullable
@Override
public Object call() throws Exception {
for (int i = 0; i < TX_CNT; i++) {
int cnt = rnd.nextInt(BATCH_CNT);
List<Integer> ids = new ArrayList<>(cnt);
for (int j = 0; j < cnt; j++) {
int id = rnd.nextInt(5000);
if (!ids.contains(id))
ids.add(id);
}
Collections.sort(ids);
Map<Object, Object> map = U.newLinkedHashMap(cnt);
for (Integer id : ids) {
if (rnd.nextBoolean())
map.put(new OrganizationKey(id), new Organization(id, "Name" + id, "City" + id));
else
map.put(new PersonKey(id), new Person(id, rnd.nextInt(), new Date(System.currentTimeMillis()), "Name" + id, 1, Gender.random()));
}
IgniteCache<Object, Object> cache = jcache();
cache.putAll(map);
}
return null;
}
}, 8, "putAll");
}
use of org.apache.ignite.cache.store.jdbc.model.PersonKey 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());
}
use of org.apache.ignite.cache.store.jdbc.model.PersonKey in project ignite by apache.
the class CacheJdbcPojoStoreAbstractSelfTest method checkPutRemove.
/**
* Check put in cache and store it in db.
*
* @throws Exception If failed.
*/
private void checkPutRemove() throws Exception {
boolean binaryMarshaller = marshaller() instanceof BinaryMarshaller || marshaller() == null;
IgniteCache<Object, Person> c1 = grid().cache(CACHE_NAME);
Connection conn = getConnection();
try {
PreparedStatement stmt = conn.prepareStatement("SELECT ID, ORG_ID, BIRTHDAY, NAME, GENDER FROM PERSON WHERE ID = ?");
stmt.setInt(1, -1);
ResultSet rs = stmt.executeQuery();
assertFalse("Unexpected non empty result set", rs.next());
U.closeQuiet(rs);
Date testDate = Date.valueOf("2001-05-05");
Gender testGender = Gender.random();
Person val = new Person(-1, -2, testDate, "Person-to-test-put-insert", 999, testGender);
Object key = builtinKeys ? Integer.valueOf(-1) : new PersonKey(-1);
// Test put-insert.
c1.put(key, val);
rs = stmt.executeQuery();
assertTrue("Unexpected empty result set", rs.next());
assertEquals(-1, rs.getInt(1));
assertEquals(-2, rs.getInt(2));
assertEquals(testDate, rs.getDate(3));
assertEquals("Person-to-test-put-insert", rs.getString(4));
assertEquals(testGender.toString(), binaryMarshaller ? Gender.values()[rs.getInt(5)].toString() : rs.getString(5));
assertFalse("Unexpected more data in result set", rs.next());
U.closeQuiet(rs);
// Test put-update.
testDate = Date.valueOf("2016-04-04");
c1.put(key, new Person(-1, -3, testDate, "Person-to-test-put-update", 999, testGender));
rs = stmt.executeQuery();
assertTrue("Unexpected empty result set", rs.next());
assertEquals(-1, rs.getInt(1));
assertEquals(-3, rs.getInt(2));
assertEquals(testDate, rs.getDate(3));
assertEquals("Person-to-test-put-update", rs.getString(4));
assertEquals(testGender.toString(), binaryMarshaller ? Gender.values()[rs.getInt(5)].toString() : rs.getString(5));
assertFalse("Unexpected more data in result set", rs.next());
// Test remove.
c1.remove(key);
rs = stmt.executeQuery();
assertFalse("Unexpected non-empty result set", rs.next());
U.closeQuiet(rs);
} finally {
U.closeQuiet(conn);
}
}
Aggregations