use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class GridCacheBinaryObjectsAbstractMultiThreadedSelfTest method testGetPut.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("BusyWait")
public void testGetPut() throws Exception {
final AtomicBoolean flag = new AtomicBoolean();
final LongAdder cnt = new LongAdder();
IgniteInternalFuture<?> f = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int threadId = idxGen.getAndIncrement() % 2;
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (!flag.get()) {
IgniteCache<Object, Object> c = jcache(rnd.nextInt(gridCount()));
switch(threadId) {
case 0:
// Put/get/remove binary -> binary.
c.put(new TestObject(rnd.nextInt(10000)), new TestObject(rnd.nextInt(10000)));
IgniteCache<Object, Object> p2 = ((IgniteCacheProxy<Object, Object>) c).keepBinary();
BinaryObject v = (BinaryObject) p2.get(new TestObject(rnd.nextInt(10000)));
if (v != null)
v.deserialize();
c.remove(new TestObject(rnd.nextInt(10000)));
break;
case 1:
// Put/get int -> binary.
c.put(rnd.nextInt(10000), new TestObject(rnd.nextInt(10000)));
IgniteCache<Integer, BinaryObject> p4 = ((IgniteCacheProxy<Object, Object>) c).keepBinary();
BinaryObject v1 = p4.get(rnd.nextInt(10000));
if (v1 != null)
v1.deserialize();
p4.remove(rnd.nextInt(10000));
break;
default:
assert false;
}
cnt.add(3);
}
return null;
}
}, THREAD_CNT);
for (int i = 0; i < 30 && !f.isDone(); i++) Thread.sleep(1000);
flag.set(true);
f.get();
info("Operations in 30 sec: " + cnt.sum());
}
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 IgniteCacheBinaryEntryProcessorSelfTest method checkInvokeBinaryObject.
/**
* @param cacheMode Cache mode to test.
* @param atomicityMode Atomicity mode to test.
* @throws Exception
*/
private void checkInvokeBinaryObject(CacheMode cacheMode, CacheAtomicityMode atomicityMode) throws Exception {
Ignite client = ignite(SRV_CNT);
IgniteCache<Integer, TestValue> clientCache = client.createCache(cacheConfiguration(cacheMode, atomicityMode));
try {
IgniteBinary binary = client.binary();
for (int i = 0; i < 100; i++) {
clientCache.put(i, new TestValue(i, "value-" + i));
BinaryObjectBuilder bldr = binary.builder("NoClass");
bldr.setField("val", i);
bldr.setField("strVal", "value-" + i);
clientCache.withKeepBinary().put(-(i + 1), bldr.build());
}
IgniteCache<Integer, BinaryObject> binaryClientCache = clientCache.withKeepBinary();
for (int i = 0; i < 100; i++) {
binaryClientCache.invoke(i, new TestEntryProcessor());
binaryClientCache.invoke(-(i + 1), new TestEntryProcessor());
}
for (int g = 0; g < NODES; g++) {
IgniteCache<Integer, TestValue> nodeCache = ignite(g).cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, BinaryObject> nodeBinaryCache = nodeCache.withKeepBinary();
for (int i = 0; i < 100; i++) {
TestValue updated = nodeCache.get(i);
assertEquals((Integer) (i + 1), updated.value());
assertEquals("updated-" + i, updated.stringValue());
BinaryObject updatedBinary = nodeBinaryCache.get(i);
assertEquals(new Integer(i + 1), updatedBinary.field("val"));
assertEquals("updated-" + i, updatedBinary.field("strVal"));
updatedBinary = nodeBinaryCache.get(-(i + 1));
assertEquals(new Integer(i + 1), updatedBinary.field("val"));
assertEquals("updated-" + i, updatedBinary.field("strVal"));
}
}
} finally {
client.destroyCache(DEFAULT_CACHE_NAME);
}
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class UpdatePlan method processRowForUpdate.
/**
* Convert a row into value.
*
* @param row Row to process.
* @throws IgniteCheckedException if failed.
* @return Tuple contains: [key, old value, new value]
*/
public T3<Object, Object, Object> processRowForUpdate(List<?> row) throws IgniteCheckedException {
GridH2RowDescriptor rowDesc = tbl.rowDescriptor();
GridQueryTypeDescriptor desc = rowDesc.type();
GridCacheContext cctx = rowDesc.context();
boolean hasNewVal = (valColIdx != -1);
boolean hasProps = !hasNewVal || colNames.length > 1;
Object key = row.get(0);
Object oldVal = row.get(1);
if (cctx.binaryMarshaller() && !(oldVal instanceof BinaryObject))
oldVal = cctx.grid().binary().toBinary(oldVal);
Object newVal;
Map<String, Object> newColVals = new HashMap<>();
for (int i = 0; i < colNames.length; i++) {
if (hasNewVal && i == valColIdx - 2)
continue;
GridQueryProperty prop = tbl.rowDescriptor().type().property(colNames[i]);
assert prop != null : "Unknown property: " + colNames[i];
newColVals.put(colNames[i], DmlUtils.convert(row.get(i + 2), rowDesc, prop.type(), colTypes[i]));
}
newVal = valSupplier.apply(row);
if (newVal == null)
throw new IgniteSQLException("New value for UPDATE must not be null", IgniteQueryErrorCode.NULL_VALUE);
// Skip key and value - that's why we start off with 3rd column
for (int i = 0; i < tbl.getColumns().length - DEFAULT_COLUMNS_COUNT; i++) {
Column c = tbl.getColumn(i + DEFAULT_COLUMNS_COUNT);
if (rowDesc.isKeyValueOrVersionColumn(c.getColumnId()))
continue;
GridQueryProperty prop = desc.property(c.getName());
if (prop.key())
// Don't get values of key's columns - we won't use them anyway
continue;
boolean hasNewColVal = newColVals.containsKey(c.getName());
if (!hasNewColVal)
continue;
Object colVal = newColVals.get(c.getName());
// UPDATE currently does not allow to modify key or its fields, so we must be safe to pass null as key.
rowDesc.setColumnValue(null, newVal, colVal, i);
}
if (cctx.binaryMarshaller() && hasProps) {
assert newVal instanceof BinaryObjectBuilder;
newVal = ((BinaryObjectBuilder) newVal).build();
}
desc.validateKeyAndValue(key, newVal);
return new T3<>(key, oldVal, newVal);
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class UpdatePlanBuilder method createSupplier.
/**
* Detect appropriate method of instantiating key or value (take from param, create binary builder,
* invoke default ctor, or allocate).
*
* @param cctx Cache context.
* @param desc Table descriptor.
* @param colIdx Column index if key or value is present in columns list, {@code -1} if it's not.
* @param hasProps Whether column list affects individual properties of key or value.
* @param key Whether supplier should be created for key or for value.
* @return Closure returning key or value.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings({ "ConstantConditions", "unchecked" })
private static KeyValueSupplier createSupplier(final GridCacheContext<?, ?> cctx, GridQueryTypeDescriptor desc, final int colIdx, boolean hasProps, final boolean key, boolean forUpdate) throws IgniteCheckedException {
final String typeName = key ? desc.keyTypeName() : desc.valueTypeName();
// Try to find class for the key locally.
final Class<?> cls = key ? U.firstNotNull(U.classForName(desc.keyTypeName(), null), desc.keyClass()) : desc.valueClass();
boolean isSqlType = QueryUtils.isSqlType(cls);
// If we don't need to construct anything from scratch, just return value from given list.
if (isSqlType || !hasProps) {
if (colIdx != -1)
return new PlainValueSupplier(colIdx);
else if (isSqlType)
// Non constructable keys and values (SQL types) must be present in the query explicitly.
throw new IgniteCheckedException((key ? "Key" : "Value") + " is missing from query");
}
if (cctx.binaryMarshaller()) {
if (colIdx != -1) {
// If we have key or value explicitly present in query, create new builder upon them...
return new KeyValueSupplier() {
/**
* {@inheritDoc}
*/
@Override
public Object apply(List<?> arg) throws IgniteCheckedException {
Object obj = arg.get(colIdx);
if (obj == null)
return null;
BinaryObject bin = cctx.grid().binary().toBinary(obj);
BinaryObjectBuilder builder = cctx.grid().binary().builder(bin);
cctx.prepareAffinityField(builder);
return builder;
}
};
} else {
// ...and if we don't, just create a new builder.
return new KeyValueSupplier() {
/**
* {@inheritDoc}
*/
@Override
public Object apply(List<?> arg) throws IgniteCheckedException {
BinaryObjectBuilder builder = cctx.grid().binary().builder(typeName);
cctx.prepareAffinityField(builder);
return builder;
}
};
}
} else {
if (colIdx != -1) {
if (forUpdate && colIdx == 1) {
// so we have to clone it. And on UPDATE we don't expect any key supplier.
assert !key;
return new KeyValueSupplier() {
/**
* {@inheritDoc}
*/
@Override
public Object apply(List<?> arg) throws IgniteCheckedException {
byte[] oldPropBytes = cctx.marshaller().marshal(arg.get(1));
// colVal is another object now, we can mutate it
return cctx.marshaller().unmarshal(oldPropBytes, U.resolveClassLoader(cctx.gridConfig()));
}
};
} else
// We either are not updating, or the new value is given explicitly, no cloning needed.
return new PlainValueSupplier(colIdx);
}
Constructor<?> ctor;
try {
ctor = cls.getDeclaredConstructor();
ctor.setAccessible(true);
} catch (NoSuchMethodException | SecurityException ignored) {
ctor = null;
}
if (ctor != null) {
final Constructor<?> ctor0 = ctor;
// Use default ctor, if it's present...
return new KeyValueSupplier() {
/**
* {@inheritDoc}
*/
@Override
public Object apply(List<?> arg) throws IgniteCheckedException {
try {
return ctor0.newInstance();
} catch (Exception e) {
if (S.INCLUDE_SENSITIVE)
throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + " [type=" + typeName + ']', e);
else
throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + '.', e);
}
}
};
} else {
// ...or allocate new instance with unsafe, if it's not
return new KeyValueSupplier() {
/**
* {@inheritDoc}
*/
@Override
public Object apply(List<?> arg) throws IgniteCheckedException {
try {
return GridUnsafe.allocateInstance(cls);
} catch (InstantiationException e) {
if (S.INCLUDE_SENSITIVE)
throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + " [type=" + typeName + ']', e);
else
throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + '.', e);
}
}
};
}
}
}
Aggregations