use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class TupleMarshallerVarlenOnlyBenchmark method init.
/**
* Setup.
*/
@Setup
public void init() {
final long seed = System.currentTimeMillis();
final boolean useString = "string".equals(type);
rnd = new Random(seed);
schema = new SchemaDescriptor(42, new Column[] { new Column("key", INT64, false, (Supplier<Object> & Serializable) () -> 0L) }, IntStream.range(0, fieldsCount).boxed().map(i -> new Column("col" + i, useString ? STRING : BYTES, nullable)).toArray(Column[]::new));
marshaller = new TupleMarshallerImpl(new SchemaRegistryImpl(v -> null, () -> INITIAL_SCHEMA_VERSION) {
@Override
public SchemaDescriptor schema() {
return schema;
}
@Override
public SchemaDescriptor schema(int ver) {
return schema;
}
@Override
public int lastSchemaVersion() {
return schema.version();
}
});
if (useString) {
final byte[] data = new byte[dataSize / fieldsCount];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (rnd.nextInt() & 0x7F);
}
// Latin1 string.
val = new String(data, StandardCharsets.ISO_8859_1);
} else {
rnd.nextBytes((byte[]) (val = new byte[dataSize / fieldsCount]));
}
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class Example method useCase8.
/**
* Use case 8: Here we show how to use mapper to represent the same data in different ways.
*/
@Disabled
@ParameterizedTest
@MethodSource("tableFactory")
public void useCase8(Table t) {
new SchemaDescriptor(1, new Column[] { new Column("colId", NativeTypes.INT64, false) }, new Column[] { new Column("colData", NativeTypes.BYTES, true) });
// Arbitrary user type.
class UserObject {
double salary;
}
// Domain class, which fields mapped to table columns.
class Employee {
UserObject fieldData;
}
// Domain class, which fields mapped to table columns.
class Employee2 {
byte[] fieldData;
}
Mapper.builder(Employee.class).map("fieldData.salary", "colSalary").build();
// Actually, any bi-directional converter can be here instead.
// Marshaller is a special case of "UserObject <--> byte[]" converter, just for example.
// here, create some marshaller for UserObject.class.
TypeConverter<UserObject, byte[]> marsh = null;
// One-column only supported first-citizen types.
Mapper.of(Long.class);
Mapper.of(byte[].class);
// Automatically maps object fields to columns with same names.
Mapper.of(UserObject.class);
// LongMapper -> long - is it possible?
// Shortcut (supported one-column key and value).
Mapper.of(Long.class, "colId");
// Shortcut (key and value represented by byte array)
// Does one-column record make sense ??? either one-column table ???
Mapper.of(UserObject.class, "colData");
// Keys, Values, and Records
Mapper.builder(Employee.class).map("fieldData", "colData").map("fieldData2", "colData1").build();
Mapper.builder(Employee.class).map("fieldData", "colData").build();
Mapper.builder(Employee.class).map("fieldData", "colData", "fieldData2", "colData1").build();
// Shortcuts (supported keys and values and records).
Mapper.of(Employee.class, "fieldData", "colData");
Mapper.of(Employee.class, "fieldData", "colData", "fieldData1", "colData1");
// Shortcut (supported one-column key and value) with additional transformation.
Mapper.of(UserObject.class, "data", marsh);
// (supported one-column key and value and records) with additional transformation.
Mapper.builder(Employee.class).convert("colData", marsh).map("fieldData", "colData").build();
// OR another way to do the same
Mapper.builder(Employee.class).map("fieldData", "colData", marsh).build();
// Next views shows different approaches to map user objects to columns.
KeyValueView<Long, Employee> v1 = t.keyValueView(Mapper.of(Long.class), Mapper.of(Employee.class, "fieldData", "colData"));
KeyValueView<Long, Employee2> v2 = t.keyValueView(Mapper.of(Long.class), Mapper.builder(Employee2.class).convert("colData", marsh).map("fieldData", "colData").build());
KeyValueView<Long, Employee2> v3 = t.keyValueView(Mapper.of(Long.class, "colId"), Mapper.builder(Employee2.class).map("fieldData", "colData", marsh).build());
KeyValueView<Long, UserObject> v4 = t.keyValueView(Mapper.of(Long.class, "colId"), Mapper.of(UserObject.class, "colData", marsh));
KeyValueView<Long, byte[]> v5 = t.keyValueView(Mapper.of(Long.class, "colId"), Mapper.of(byte[].class, "colData"));
// The values in next operations are equivalent, and lead to the same row value part content.
v1.put(null, 1L, new Employee());
v2.put(null, 2L, new Employee2());
v3.put(null, 3L, new Employee2());
v4.put(null, 4L, new UserObject());
v5.put(null, 5L, new byte[] {/* serialized UserObject bytes */
});
// Shortcut with classes for simple use-case
KeyValueView<Long, String> v6 = t.keyValueView(Long.class, String.class);
// Shortcut with classes for widely used case
KeyValueView<Long, UserObject> v7 = t.keyValueView(Long.class, // obj.salary -> colSalary
UserObject.class);
// do the same as
KeyValueView<Long, UserObject> v8 = t.keyValueView(Mapper.of(Long.class), // obj.salary -> colSalary
Mapper.builder(UserObject.class).automap().build());
KeyValueView<Long, UserObject> v9 = t.keyValueView(Mapper.of(Long.class), // UserObject -> byte[] -> colData
Mapper.of(UserObject.class, "colData", marsh));
// Get operations return the same result for all keys for each of row.
// for 1 in 1..5
// v1.get(iL) == v1.get(1L);
// ============================ GET ===============================================
new SchemaDescriptor(1, new Column[] { new Column("colId", NativeTypes.INT64, false) }, new Column[] { new Column("colData", NativeTypes.BYTES, true), new Column("colSalary", NativeTypes.BYTES, true) });
// indistinguishable absent value and null column
UserObject obj = v4.get(null, 1L);
// Optional way
// Optional<UserObject> optionalObj = v4.get(1L); // abuse of Optional type
// NullableValue way
NullableValue<UserObject> nullableValue = v4.getNullable(null, 1L);
// what if user uses this syntax for nullable column?
UserObject userObject = v4.get(null, 1L);
// 1. Exception always
// 2. Exception if column value is null (use getNullable)
// Get or default
String str = v6.getOrDefault(null, 1L, "default");
// ============================ PUT ===============================================
v4.put(null, 1L, null);
v4.remove(null, 1L, null);
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class KeyValueBinaryViewOperationsTest method getAndPut.
@Test
public void getAndPut() {
SchemaDescriptor schema = schemaDescriptor();
KeyValueView<Tuple, Tuple> tbl = createTable(schema).keyValueView();
final Tuple key = Tuple.create().set("id", 1L);
final Tuple val = Tuple.create().set("val", 11L);
final Tuple val2 = Tuple.create().set("val", 22L);
final Tuple val3 = Tuple.create().set("val", 33L);
assertNull(tbl.get(null, key));
// Insert new tuple.
assertNull(tbl.getAndPut(null, key, val));
assertEqualsValues(schema, val, tbl.get(null, key));
assertEqualsValues(schema, val, tbl.get(null, Tuple.create().set("id", 1L)));
assertEqualsValues(schema, val, tbl.getAndPut(null, key, val2));
assertEqualsValues(schema, val2, tbl.getAndPut(null, key, Tuple.create().set("val", 33L)));
assertEqualsValues(schema, val3, tbl.get(null, key));
assertNull(tbl.get(null, Tuple.create().set("id", 2L)));
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class KeyValueBinaryViewOperationsTest method removeExact.
@Test
public void removeExact() {
SchemaDescriptor schema = schemaDescriptor();
final KeyValueView<Tuple, Tuple> tbl = createTable(schema).keyValueView();
final Tuple key = Tuple.create().set("id", 1L);
final Tuple key2 = Tuple.create().set("id", 2L);
final Tuple val = Tuple.create().set("val", 11L);
final Tuple val2 = Tuple.create().set("val", 22L);
// Put KV pair.
tbl.put(null, key, val);
assertEqualsValues(schema, val, tbl.get(null, key));
// Fails to delete KV pair with unexpected value.
assertFalse(tbl.remove(null, key, val2));
assertEqualsValues(schema, val, tbl.get(null, key));
// Delete KV pair with expected value.
assertTrue(tbl.remove(null, key, val));
assertNull(tbl.get(null, key));
// Once again.
assertFalse(tbl.remove(null, key, val));
assertNull(tbl.get(null, key));
// Try to remove non-existed key.
assertThrows(Exception.class, () -> tbl.remove(null, key, null));
assertNull(tbl.get(null, key));
// Put KV pair.
tbl.put(null, key, val2);
assertEqualsValues(schema, val2, tbl.get(null, key));
// Check null value ignored.
assertThrows(Exception.class, () -> tbl.remove(null, key, null));
assertEqualsValues(schema, val2, tbl.get(null, key));
// Delete KV pair with expected value.
assertTrue(tbl.remove(null, key, val2));
assertNull(tbl.get(null, key));
assertFalse(tbl.remove(null, key2, val2));
assertNull(tbl.get(null, key2));
}
use of org.apache.ignite.internal.schema.SchemaDescriptor in project ignite-3 by apache.
the class KeyValueBinaryViewOperationsTest method getAll.
@Test
public void getAll() {
SchemaDescriptor schema = schemaDescriptor();
KeyValueView<Tuple, Tuple> tbl = createTable(schema).keyValueView();
Tuple key1 = Tuple.create().set("id", 1L);
Tuple key2 = Tuple.create().set("id", 2L);
Tuple key3 = Tuple.create().set("id", 3L);
tbl.putAll(null, Map.of(key1, Tuple.create().set("val", 11L), key3, Tuple.create().set("val", 33L)));
Map<Tuple, Tuple> res = tbl.getAll(null, List.of(key1, key2, key3));
assertEquals(2, res.size());
assertEquals(Tuple.create().set("val", 11L), res.get(key1));
assertEquals(Tuple.create().set("val", 33L), res.get(key3));
assertNull(res.get(key2));
}
Aggregations