use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class BinaryTreeSelfTest method checkTreeSet.
/**
* Check {@code TreeSet} data structure.
*
* @param useBinary Whether to go through binary mode.
* @param useComparator Whether comparator should be used.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private void checkTreeSet(boolean useBinary, boolean useComparator) throws Exception {
// Populate set.
TreeSet<TestKey> set;
if (useComparator) {
set = new TreeSet<>(new TestKeyComparator());
for (int i = 0; i < SIZE; i++) set.add(key(false, i));
} else {
set = new TreeSet<>();
for (int i = 0; i < SIZE; i++) set.add(key(true, i));
}
// Put and get value from cache.
cache().put(KEY, set);
TreeSet<TestKey> resSet;
if (useBinary) {
BinaryObject resMapBinary = (BinaryObject) cache().withKeepBinary().get(KEY);
resSet = resMapBinary.deserialize();
} else
resSet = (TreeSet<TestKey>) cache().get(KEY);
// Ensure content is correct.
if (useComparator)
assert resSet.comparator() instanceof TestKeyComparator;
else
assertNull(resSet.comparator());
assertEquals(set, resSet);
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class GridCacheUtilsSelfTest method testCacheKeyValidation.
/**
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public void testCacheKeyValidation() throws IgniteCheckedException {
CU.validateCacheKey("key");
CU.validateCacheKey(1);
CU.validateCacheKey(1L);
CU.validateCacheKey(1.0);
CU.validateCacheKey(new ExtendsClassWithEqualsAndHashCode());
CU.validateCacheKey(new ExtendsClassWithEqualsAndHashCode2());
assertThrowsForInvalidKey(new NoEqualsAndHashCode());
assertThrowsForInvalidKey(new NoEquals());
assertThrowsForInvalidKey(new NoHashCode());
assertThrowsForInvalidKey(new WrongEquals());
BinaryObjectBuilderImpl binBuilder = new BinaryObjectBuilderImpl(binaryContext(), EqualsAndHashCode.class.getName());
BinaryObject binObj = binBuilder.build();
CU.validateCacheKey(binObj);
BinaryObjectBuilderImpl binBuilder2 = new BinaryObjectBuilderImpl((BinaryObjectImpl) binObj);
CU.validateCacheKey(binBuilder2.build());
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class GridCacheStoreManagerDeserializationTest method _testBinaryStream.
/**
* TODO GG-11148.
*
* Check whether binary objects are stored without unmarshalling via stream API.
*
* @throws Exception If failed.
*/
public void _testBinaryStream() throws Exception {
final Ignite grid = startGrid("binaryGrid");
final IgniteCache<BinaryObject, BinaryObject> cache = grid.createCache(CACHE_NAME).withKeepBinary();
final BinaryObject key = streamBinaryData(grid);
assert cache.containsKey(key);
assert store.map.containsKey(key);
cache.destroy();
cache.close();
assert store.map.containsKey(key);
final IgniteCache<BinaryObject, BinaryObject> cache2 = grid.createCache(CACHE_NAME).withKeepBinary();
final BinaryObject loaded = cache2.get(key);
assertSame(loaded, key);
assertTrue(store.map.containsKey(key));
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class GridIndexingSpiAbstractSelfTest method testSpi.
/**
* @throws Exception If failed.
*/
public void testSpi() throws Exception {
IgniteH2Indexing spi = getIndexing();
IgniteCache<Integer, BinaryObject> cacheA = ignite0.createCache(cacheACfg());
IgniteCache<Integer, BinaryObject> cacheB = ignite0.createCache(cacheBCfg());
assertFalse(spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), "select * from A.A", null, Collections.emptySet(), typeAA.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select * from A.B", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select * from B.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select * from B.A, A.B, A.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
try {
spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select aa.*, ab.*, ba.* from A.A aa, A.B ab, B.A ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext();
fail("Enumerations of aliases in select block must be prohibited");
} catch (IgniteCheckedException ignored) {
// all fine
}
assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select ab.* from A.B ab", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select ba.* from B.A as ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
cacheA.put(1, aa("A", 1, "Vasya", 10).build());
cacheA.put(1, ab(1, "Vasya", 20, "Some text about Vasya goes here.").build());
cacheB.put(1, ba(2, "Petya", 25, true).build());
cacheB.put(1, ba(2, "Kolya", 25, true).build());
cacheA.put(2, aa("A", 2, "Valera", 19).build());
cacheA.put(3, aa("A", 3, "Borya", 18).build());
cacheA.put(4, ab(4, "Vitalya", 20, "Very Good guy").build());
// Query data.
Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), "from a order by age", null, Collections.emptySet(), typeAA.name(), null, null);
assertTrue(res.hasNext());
assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), "select aa.* from a aa order by aa.age", null, Collections.emptySet(), typeAA.name(), null, null);
assertTrue(res.hasNext());
assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "from b order by name", null, Collections.emptySet(), typeAB.name(), null, null);
assertTrue(res.hasNext());
assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select bb.* from b as bb order by bb.name", null, Collections.emptySet(), typeAB.name(), null, null);
assertTrue(res.hasNext());
assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "from a", null, Collections.emptySet(), typeBA.name(), null, null);
assertTrue(res.hasNext());
assertEquals(ba(2, "Kolya", 25, true).build(), value(res.next()));
assertFalse(res.hasNext());
// Text queries
Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> txtRes = spi.queryLocalText(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "good", typeAB.name(), null);
assertTrue(txtRes.hasNext());
assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(txtRes.next()));
assertFalse(txtRes.hasNext());
// Fields query
GridQueryFieldsResult fieldsRes = spi.queryLocalSqlFields(spi.schema("A"), "select a.a.name n1, a.a.age a1, b.a.name n2, " + "b.a.age a2 from a.a, b.a where a.a.id = b.a.id ", Collections.emptySet(), null, false, 0, null);
String[] aliases = { "N1", "A1", "N2", "A2" };
Object[] vals = { "Valera", 19, "Kolya", 25 };
IgniteSpiCloseableIterator<List<?>> it = fieldsRes.iterator();
assertTrue(it.hasNext());
List<?> fields = it.next();
assertEquals(4, fields.size());
int i = 0;
for (Object f : fields) {
assertEquals(aliases[i], fieldsRes.metaData().get(i).fieldName());
assertEquals(vals[i++], f);
}
assertFalse(it.hasNext());
// Remove
cacheA.remove(2);
cacheB.remove(1);
}
use of org.apache.ignite.binary.BinaryObject in project YCSB by brianfrankcooper.
the class IgniteClient method insert.
/**
* Insert a record in the database. Any field/value pairs in the specified
* values HashMap will be written into the record with the specified record
* key.
*
* @param table The name of the table
* @param key The record key of the record to insert.
* @param values A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error
*/
@Override
public Status insert(String table, String key, Map<String, ByteIterator> values) {
try {
BinaryObjectBuilder bob = cluster.binary().builder("CustomType");
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
bob.setField(entry.getKey(), entry.getValue().toString());
if (debug) {
log.info(entry.getKey() + ":" + entry.getValue());
}
}
BinaryObject bo = bob.build();
if (table.equals(DEFAULT_CACHE_NAME)) {
cache.put(key, bo);
} else {
throw new UnsupportedOperationException("Unexpected table name: " + table);
}
return Status.OK;
} catch (Exception e) {
log.error(String.format("Error inserting key: %s", key), e);
return Status.ERROR;
}
}
Aggregations