Search in sources :

Example 6 with ByteBufferRow

use of org.apache.ignite.internal.schema.ByteBufferRow in project ignite-3 by apache.

the class CommandUtils method readRows.

/**
 * Reads the keys from a byte array.
 *
 * @param bytes    Byte array.
 * @param consumer Consumer for binary row.
 */
public static void readRows(byte[] bytes, Consumer<BinaryRow> consumer) {
    if (bytes == null || bytes.length == 0) {
        return;
    }
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
        byte[] lenBytes = new byte[4];
        byte[] rowBytes;
        int read;
        while ((read = bais.read(lenBytes)) != -1) {
            assert read == 4;
            int len = bytesToInt(lenBytes);
            if (len == 0) {
                consumer.accept(null);
                continue;
            }
            rowBytes = new byte[len];
            read = bais.read(rowBytes);
            assert read == len;
            consumer.accept(new ByteBufferRow(rowBytes));
        }
    } catch (IOException e) {
        LOG.error("Could not read rows from stream.", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) IOException(java.io.IOException)

Example 7 with ByteBufferRow

use of org.apache.ignite.internal.schema.ByteBufferRow in project ignite-3 by apache.

the class MutableRowTupleAdapterTest method testVariousColumnTypes.

@Test
public void testVariousColumnTypes() throws TupleMarshallerException {
    Random rnd = new Random();
    TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(fullSchema));
    Tuple tuple = Tuple.create().set("valByteCol", (byte) 1).set("valShortCol", (short) 2).set("valIntCol", 3).set("valLongCol", 4L).set("valFloatCol", 0.055f).set("valDoubleCol", 0.066d).set("keyUuidCol", UUID.randomUUID()).set("valDateCol", LocalDate.now()).set("valDateTimeCol", truncatedLocalDateTimeNow()).set("valTimeCol", truncatedLocalTimeNow()).set("valTimeStampCol", truncatedInstantNow()).set("valBitmask1Col", randomBitSet(rnd, 12)).set("valBytesCol", IgniteTestUtils.randomBytes(rnd, 13)).set("valStringCol", IgniteTestUtils.randomString(rnd, 14)).set("valNumberCol", BigInteger.valueOf(rnd.nextLong())).set("valDecimalCol", BigDecimal.valueOf(rnd.nextLong(), 5));
    Tuple rowTuple = TableRow.tuple(new Row(fullSchema, new ByteBufferRow(marshaller.marshal(tuple).bytes())));
    assertEquals(tuple, rowTuple);
    // Force row to tuple conversion.
    rowTuple.set("foo", "bar");
    // Force row to tuple conversion.
    tuple.set("foo", "bar");
    assertEquals(tuple, rowTuple);
}
Also used : Random(java.util.Random) TupleMarshaller(org.apache.ignite.internal.schema.marshaller.TupleMarshaller) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 8 with ByteBufferRow

use of org.apache.ignite.internal.schema.ByteBufferRow in project ignite-3 by apache.

the class MutableRowTupleAdapterTest method testRowTupleSchemaAwareness.

@Test
public void testRowTupleSchemaAwareness() throws TupleMarshallerException {
    TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(schema));
    Row row = new Row(schema, new ByteBufferRow(marshaller.marshal(Tuple.create().set("id", 1L).set("name", "Shirt")).bytes()));
    Tuple tuple = TableRow.tuple(row);
    Tuple key = TableRow.keyTuple(row);
    final Tuple val = TableRow.valueTuple(row);
    assertTrue(tuple instanceof SchemaAware);
    assertNotNull(((SchemaAware) tuple).schema());
    assertNotNull(((SchemaAware) key).schema());
    assertNotNull(((SchemaAware) val).schema());
    tuple.set("name", "noname");
    assertNull(((SchemaAware) tuple).schema());
    assertNotNull(((SchemaAware) key).schema());
    assertNotNull(((SchemaAware) val).schema());
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) TupleMarshaller(org.apache.ignite.internal.schema.marshaller.TupleMarshaller) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 9 with ByteBufferRow

use of org.apache.ignite.internal.schema.ByteBufferRow in project ignite-3 by apache.

the class MutableRowTupleAdapterTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Random rnd = new Random();
    Tuple tup1 = Tuple.create().set("valByteCol", (byte) 1).set("valShortCol", (short) 2).set("valIntCol", 3).set("valLongCol", 4L).set("valFloatCol", 0.055f).set("valDoubleCol", 0.066d).set("keyUuidCol", UUID.randomUUID()).set("valDateCol", LocalDate.now()).set("valDateTimeCol", truncatedLocalDateTimeNow()).set("valTimeCol", truncatedLocalTimeNow()).set("valTimeStampCol", truncatedInstantNow()).set("valBitmask1Col", randomBitSet(rnd, 12)).set("valBytesCol", IgniteTestUtils.randomBytes(rnd, 13)).set("valStringCol", IgniteTestUtils.randomString(rnd, 14)).set("valNumberCol", BigInteger.valueOf(rnd.nextLong())).set("valDecimalCol", BigDecimal.valueOf(rnd.nextLong(), 5));
    TupleMarshaller marshaller = new TupleMarshallerImpl(new DummySchemaManagerImpl(fullSchema));
    Row row = new Row(fullSchema, new ByteBufferRow(marshaller.marshal(tup1).bytes()));
    Tuple tup2 = deserializeTuple(serializeTuple(TableRow.tuple(row)));
    assertEquals(tup1, tup2);
    assertEquals(tup2, tup1);
}
Also used : Random(java.util.Random) TupleMarshaller(org.apache.ignite.internal.schema.marshaller.TupleMarshaller) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) TupleMarshallerImpl(org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) Tuple(org.apache.ignite.table.Tuple) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) Test(org.junit.jupiter.api.Test)

Example 10 with ByteBufferRow

use of org.apache.ignite.internal.schema.ByteBufferRow in project ignite-3 by apache.

the class VersionedRowStore method unpack.

/**
 * Unpacks a raw value into (cur, old, ts) triplet. TODO asch IGNITE-15934 not very efficient.
 *
 * @param row The row.
 * @return The value.
 */
private static Value unpack(@Nullable DataRow row) {
    if (row == null) {
        return new Value(null, null, null);
    }
    ByteBuffer buf = row.value();
    BinaryRow newVal = null;
    BinaryRow oldVal = null;
    int l1 = buf.asIntBuffer().get();
    int pos = 4;
    buf.position(pos);
    if (l1 != 0) {
        // TODO asch IGNITE-15934 get rid of copying
        byte[] tmp = new byte[l1];
        buf.get(tmp);
        newVal = new ByteBufferRow(tmp);
        pos += l1;
    }
    buf.position(pos);
    int l2 = buf.asIntBuffer().get();
    pos += 4;
    buf.position(pos);
    if (l2 != 0) {
        // TODO asch get rid of copying
        byte[] tmp = new byte[l2];
        buf.get(tmp);
        oldVal = new ByteBufferRow(tmp);
        pos += l2;
    }
    buf.position(pos);
    long ts = buf.getLong();
    long nodeId = buf.getLong();
    return new Value(newVal, oldVal, new Timestamp(ts, nodeId));
}
Also used : ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) ByteBuffer(java.nio.ByteBuffer) Timestamp(org.apache.ignite.internal.tx.Timestamp)

Aggregations

ByteBufferRow (org.apache.ignite.internal.schema.ByteBufferRow)22 Row (org.apache.ignite.internal.schema.row.Row)14 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)10 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)10 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)10 Tuple (org.apache.ignite.table.Tuple)10 Test (org.junit.jupiter.api.Test)10 RowAssembler (org.apache.ignite.internal.schema.row.RowAssembler)7 Random (java.util.Random)5 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)4 Column (org.apache.ignite.internal.schema.Column)3 SchemaAware (org.apache.ignite.internal.schema.SchemaAware)3 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)3 NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)2 Timestamp (org.apache.ignite.internal.tx.Timestamp)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ByteBuffer (java.nio.ByteBuffer)1