use of org.apache.ignite.cache.store.jdbc.model.Organization 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.Organization 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.Organization in project ignite by apache.
the class CacheJdbcPojoStoreTest method testWriteRetry.
/**
* @throws Exception If failed.
*/
@Test
public void testWriteRetry() throws Exception {
CacheJdbcPojoStore<Object, Object> store = store();
// Special dialect that will skip updates, to test write retry.
store.setDialect(new H2Dialect() {
/**
* {@inheritDoc}
*/
@Override
public boolean hasMerge() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public String updateQuery(String tblName, Collection<String> keyCols, Iterable<String> valCols) {
return super.updateQuery(tblName, keyCols, valCols) + " AND 1 = 0";
}
});
inject(store);
Connection conn = store.openConnection(false);
PreparedStatement orgStmt = conn.prepareStatement("INSERT INTO Organization(id, name, city) VALUES (?, ?, ?)");
orgStmt.setInt(1, 1);
orgStmt.setString(2, "name" + 1);
orgStmt.setString(3, "city" + 1);
orgStmt.executeUpdate();
U.closeQuiet(orgStmt);
conn.commit();
OrganizationKey k1 = new OrganizationKey(1);
Organization v1 = new Organization(1, "Name1", "City1");
ses.newSession(null);
try {
store.write(new CacheEntryImpl<>(wrap(k1), wrap(v1)));
fail("CacheWriterException wasn't thrown.");
} catch (CacheWriterException e) {
if (!e.getMessage().startsWith("Failed insert entry in database, violate a unique index or primary key") || e.getSuppressed().length != 2)
throw e;
}
}
use of org.apache.ignite.cache.store.jdbc.model.Organization 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());
}
Aggregations