Search in sources :

Example 26 with BinaryRow

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

the class RecordViewImpl method unmarshal.

/**
 * Unmarshal records.
 *
 * @param rows Row collection.
 * @return Records collection.
 */
@NotNull
public Collection<R> unmarshal(Collection<BinaryRow> rows) {
    if (rows.isEmpty()) {
        return Collections.emptyList();
    }
    final RecordMarshaller<R> marsh = marshaller(schemaReg.lastSchemaVersion());
    List<R> recs = new ArrayList<>(rows.size());
    try {
        for (Row row : schemaReg.resolve(rows)) {
            if (row != null) {
                recs.add(marsh.unmarshal(row));
            }
        }
        return recs;
    } catch (MarshallerException e) {
        throw new IgniteException(e);
    }
}
Also used : MarshallerException(org.apache.ignite.internal.schema.marshaller.MarshallerException) IgniteException(org.apache.ignite.lang.IgniteException) ArrayList(java.util.ArrayList) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with BinaryRow

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

the class CommandUtils method rowsToBytes.

/**
 * Writes a list of rows to byte array.
 *
 * @param rows     Collection of rows.
 * @return         Rows data.
 */
public static byte[] rowsToBytes(Collection<BinaryRow> rows) {
    if (rows == null || rows.isEmpty()) {
        return null;
    }
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        for (BinaryRow row : rows) {
            if (row == null) {
                baos.write(intToBytes(0));
            } else {
                byte[] bytes = rowToBytes(row);
                baos.write(intToBytes(bytes.length));
                baos.write(bytes);
            }
        }
        baos.flush();
        return baos.toByteArray();
    } catch (IOException e) {
        LOG.error("Could not write rows to stream [rows=" + rows.size() + ']', e);
        throw new IgniteInternalException(e);
    }
}
Also used : IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) IOException(java.io.IOException)

Example 28 with BinaryRow

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

the class PartitionListener method handleScanInitCommand.

/**
 * Handler for the {@link ScanInitCommand}.
 *
 * @param clo Command closure.
 * @param cmd Command.
 */
private void handleScanInitCommand(CommandClosure<ScanInitCommand> clo, ScanInitCommand cmd) {
    IgniteUuid cursorId = cmd.scanId();
    try {
        Cursor<BinaryRow> cursor = storage.scan(key -> true);
        cursors.put(cursorId, new CursorMeta(cursor, cmd.requesterNodeId(), new AtomicInteger(0)));
    } catch (StorageException e) {
        clo.result(e);
    }
    clo.result(null);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteUuid(org.apache.ignite.lang.IgniteUuid) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) StorageException(org.apache.ignite.internal.storage.StorageException)

Example 29 with BinaryRow

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

the class VersionedRowStore method scan.

/**
 * Executes a scan.
 *
 * @param pred The predicate.
 * @return The cursor.
 */
public Cursor<BinaryRow> scan(Predicate<SearchRow> pred) {
    Cursor<DataRow> delegate = storage.scan(pred);
    // TODO asch add tx support IGNITE-15087.
    return new Cursor<BinaryRow>() {

        @Nullable
        private BinaryRow cur = null;

        @Override
        public void close() throws Exception {
            delegate.close();
        }

        @NotNull
        @Override
        public Iterator<BinaryRow> iterator() {
            return this;
        }

        @Override
        public boolean hasNext() {
            if (cur != null) {
                return true;
            }
            if (delegate.hasNext()) {
                DataRow row = delegate.next();
                cur = versionedRow(row, null).getFirst();
                // Skip tombstones.
                return cur != null ? true : hasNext();
            }
            return false;
        }

        @Override
        public BinaryRow next() {
            BinaryRow next = cur;
            cur = null;
            assert next != null;
            return next;
        }
    };
}
Also used : BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Cursor(org.apache.ignite.internal.util.Cursor) DataRow(org.apache.ignite.internal.storage.DataRow) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow)

Example 30 with BinaryRow

use of org.apache.ignite.internal.schema.BinaryRow 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

BinaryRow (org.apache.ignite.internal.schema.BinaryRow)57 Row (org.apache.ignite.internal.schema.row.Row)34 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)19 MethodSource (org.junit.jupiter.params.provider.MethodSource)19 Column (org.apache.ignite.internal.schema.Column)18 ArrayList (java.util.ArrayList)13 NotNull (org.jetbrains.annotations.NotNull)11 IgniteException (org.apache.ignite.lang.IgniteException)10 MarshallerException (org.apache.ignite.internal.schema.marshaller.MarshallerException)9 CountDownLatch (java.util.concurrent.CountDownLatch)6 Subscription (java.util.concurrent.Flow.Subscription)6 Test (org.junit.jupiter.api.Test)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 TestObjectWithAllTypes (org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes)4 HashMap (java.util.HashMap)3 UUID (java.util.UUID)3 Flow (java.util.concurrent.Flow)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3