use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.
the class KvMarshallerImpl method collectObjectStats.
/**
* Reads object fields and gather statistic.
*
* @throws MarshallerException If failed to read object content.
*/
private ObjectStatistic collectObjectStats(Columns cols, Marshaller marsh, Object obj) throws MarshallerException {
if (obj == null || !cols.hasVarlengthColumns()) {
return ObjectStatistic.ZERO_VARLEN_STATISTICS;
}
int cnt = 0;
int size = 0;
for (int i = cols.firstVarlengthColumn(); i < cols.length(); i++) {
final Object val = marsh.value(obj, i);
final NativeType colType = cols.column(i).type();
if (val == null || colType.spec().fixedLength()) {
continue;
}
size += getValueSize(val, colType);
cnt++;
}
return new ObjectStatistic(cnt, size);
}
use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.
the class InteropOperationsTest method validateTuple.
/**
* Test specified tuple.
*
* @param id Expected tuple id.
* @param t Tuple to test.
* @param nulls If {@code true} - nullable fields will be filled.
*/
private void validateTuple(int id, Tuple t, boolean nulls) {
long actualId = t.longValue("id");
assertEquals(id, actualId);
Tuple expected = createTuple(id, nulls);
expected.set("id", (long) id);
for (Column col : SCHEMA.valueColumns().columns()) {
if (!nulls && col.nullable()) {
continue;
}
String colName = col.name();
NativeType type = col.type();
if (NativeTypes.INT8.equals(type)) {
assertEquals(expected.byteValue(colName), t.byteValue(colName));
} else if (NativeTypes.INT16.equals(type)) {
assertEquals(expected.shortValue(colName), t.shortValue(colName));
} else if (NativeTypes.INT32.equals(type)) {
assertEquals(expected.intValue(colName), t.intValue(colName));
} else if (NativeTypes.INT64.equals(type)) {
assertEquals(expected.longValue(colName), t.longValue(colName));
} else if (NativeTypes.FLOAT.equals(type)) {
assertEquals(expected.floatValue(colName), t.floatValue(colName));
} else if (NativeTypes.DOUBLE.equals(type)) {
assertEquals(expected.doubleValue(colName), t.doubleValue(colName));
} else if (NativeTypes.BYTES.equals(type)) {
assertArrayEquals((byte[]) expected.value(colName), (byte[]) t.value(colName));
} else if (NativeTypes.STRING.equals(type)) {
assertEquals(expected.stringValue(colName), t.stringValue(colName));
} else if (NativeTypes.UUID.equals(type)) {
assertEquals(expected.uuidValue(colName), t.uuidValue(colName));
} else if (NativeTypes.DATE.equals(type)) {
assertEquals(expected.dateValue(colName), t.dateValue(colName));
} else if (NativeTypes.time().equals(type)) {
assertEquals(expected.timeValue(colName), t.timeValue(colName));
} else if (NativeTypes.datetime().equals(type)) {
assertEquals(expected.datetimeValue(colName), t.datetimeValue(colName));
} else if (NativeTypes.timestamp().equals(type)) {
assertEquals(expected.timestampValue(colName), expected.timestampValue(colName));
} else if (NativeTypes.numberOf(2).equals(type)) {
assertEquals((BigInteger) expected.value(colName), t.value(colName));
} else if (NativeTypes.decimalOf(5, 2).equals(type)) {
assertEquals((BigDecimal) expected.value(colName), t.value(colName));
} else if (NativeTypes.bitmaskOf(8).equals(type)) {
assertEquals(expected.bitmaskValue(colName), t.bitmaskValue(colName));
} else {
fail("Unable to validate value of type " + type);
}
}
assertTrue(!nulls ^ expected.equals(t), "nulls = " + nulls + ", id = " + id);
}
use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.
the class InteropOperationsTest method createTuple.
/**
* Create tuple with specified id and nulls fields filled.
*
* @param id Id.
* @param nulls If {@code true} - nullable fields will be filled.
* @return Tuple with all requested fields.
*/
private Tuple createTuple(int id, boolean nulls) {
Tuple res = Tuple.create();
for (Column col : SCHEMA.valueColumns().columns()) {
if (!nulls && col.nullable()) {
continue;
}
String colName = col.name();
NativeType type = col.type();
if (NativeTypes.INT8.equals(type)) {
res.set(colName, (byte) id);
} else if (NativeTypes.INT16.equals(type)) {
res.set(colName, (short) id);
} else if (NativeTypes.INT32.equals(type)) {
res.set(colName, id);
} else if (NativeTypes.INT64.equals(type)) {
res.set(colName, (long) id);
} else if (NativeTypes.FLOAT.equals(type)) {
res.set(colName, (float) id);
} else if (NativeTypes.DOUBLE.equals(type)) {
res.set(colName, (double) id);
} else if (NativeTypes.BYTES.equals(type)) {
res.set(colName, String.valueOf(id).getBytes(StandardCharsets.UTF_8));
} else if (NativeTypes.STRING.equals(type)) {
res.set(colName, String.valueOf(id));
} else if (NativeTypes.UUID.equals(type)) {
res.set(colName, new UUID(0L, (long) id));
} else if (NativeTypes.DATE.equals(type)) {
res.set(colName, LocalDate.ofYearDay(2021, id));
} else if (NativeTypes.time().equals(type)) {
res.set(colName, LocalTime.ofSecondOfDay(id));
} else if (NativeTypes.datetime().equals(type)) {
res.set(colName, LocalDateTime.ofEpochSecond(id, 0, ZoneOffset.UTC));
} else if (NativeTypes.timestamp().equals(type)) {
res.set(colName, Instant.ofEpochSecond(id));
} else if (NativeTypes.numberOf(2).equals(type)) {
res.set(colName, BigInteger.valueOf(id));
} else if (NativeTypes.decimalOf(5, 2).equals(type)) {
res.set(colName, BigDecimal.valueOf(id * 100).movePointLeft(2));
} else if (NativeTypes.bitmaskOf(8).equals(type)) {
BitSet bitSet = new BitSet();
bitSet.set(id);
res.set(colName, bitSet);
} else {
fail("Unable to fullfill value of type " + type);
}
}
return res;
}
use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.
the class KeyValueViewOperationsSimpleSchemaTest method putGetAllTypes.
@Test
public void putGetAllTypes() {
Random rnd = new Random();
Long key = 42L;
List<NativeType> allTypes = List.of(NativeTypes.INT8, NativeTypes.INT16, NativeTypes.INT32, NativeTypes.INT64, NativeTypes.FLOAT, NativeTypes.DOUBLE, NativeTypes.DATE, NativeTypes.UUID, NativeTypes.numberOf(20), NativeTypes.decimalOf(25, 5), NativeTypes.bitmaskOf(22), NativeTypes.time(), NativeTypes.datetime(), NativeTypes.timestamp(), NativeTypes.BYTES, NativeTypes.STRING);
// Validate all types are tested.
assertEquals(Set.of(NativeTypeSpec.values()), allTypes.stream().map(NativeType::spec).collect(Collectors.toSet()));
for (NativeType type : allTypes) {
final Object val = SchemaTestUtils.generateRandomValue(rnd, type);
assertFalse(type.mismatch(NativeTypes.fromObject(val)));
KeyValueViewImpl<Long, Object> kvView = kvViewForValueType(NativeTypes.fromObject(val), (Class<Object>) val.getClass());
kvView.put(null, key, val);
if (val instanceof byte[]) {
assertArrayEquals((byte[]) val, (byte[]) kvView.get(null, key));
} else {
assertEquals(val, kvView.get(null, key));
}
}
}
use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.
the class SchemaSerializerImpl method readColumn.
/**
* Reads column from byte buffer.
*
* @param buf Byte buffer.
* @return Column.
*/
private Column readColumn(ByteBuffer buf) {
int schemaIdx = buf.getInt();
int columnOrder = buf.getInt();
boolean nullable = buf.get() == 1;
String name = readString(buf);
NativeType nativeType = fromByteBuffer(buf);
Object object = readDefaultValue(buf, nativeType);
return new Column(columnOrder, name, nativeType, nullable, () -> object).copy(schemaIdx);
}
Aggregations