Search in sources :

Example 1 with DataRow

use of org.apache.ignite.internal.storage.DataRow in project ignite-3 by apache.

the class ItInternalTableScanTest method setUp.

/**
 * Prepare test environment.
 * <ol>
 * <li>Start network node.</li>
 * <li>Start raft server.</li>
 * <li>Prepare partitioned raft group.</li>
 * <li>Prepare partitioned raft group service.</li>
 * <li>Prepare internal table as a test object.</li>
 * </ol>
 *
 * @throws Exception If any.
 */
@BeforeEach
public void setUp(TestInfo testInfo) throws Exception {
    NetworkAddress nodeNetworkAddress = new NetworkAddress("localhost", 20_000);
    network = ClusterServiceTestUtils.clusterService(testInfo, 20_000, new StaticNodeFinder(List.of(nodeNetworkAddress)), NETWORK_FACTORY);
    network.start();
    raftSrv = new RaftServerImpl(network, FACTORY);
    raftSrv.start();
    String grpName = "test_part_grp";
    List<Peer> conf = List.of(new Peer(nodeNetworkAddress));
    mockStorage = mock(PartitionStorage.class);
    txManager = new TxManagerImpl(network, new HeapLockManager());
    txManager.start();
    UUID tblId = UUID.randomUUID();
    raftSrv.startRaftGroup(grpName, new PartitionListener(tblId, new VersionedRowStore(mockStorage, txManager) {

        @Override
        protected Pair<BinaryRow, BinaryRow> versionedRow(@Nullable DataRow row, Timestamp timestamp) {
            // Return as is.
            return new Pair<>(new ByteBufferRow(row.valueBytes()), null);
        }
    }), conf);
    executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
    RaftGroupService raftGrpSvc = RaftGroupServiceImpl.start(RAFT_GRP_ID, network, FACTORY, 10_000, conf, true, 200, executor).get(3, TimeUnit.SECONDS);
    internalTbl = new InternalTableImpl(TEST_TABLE_NAME, tblId, Int2ObjectMaps.singleton(0, raftGrpSvc), 1, NetworkAddress::toString, txManager, mock(TableStorage.class));
}
Also used : VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) Peer(org.apache.ignite.raft.client.Peer) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) Timestamp(org.apache.ignite.internal.tx.Timestamp) RaftServerImpl(org.apache.ignite.internal.raft.server.impl.RaftServerImpl) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) NetworkAddress(org.apache.ignite.network.NetworkAddress) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) UUID(java.util.UUID) Nullable(org.jetbrains.annotations.Nullable) PartitionStorage(org.apache.ignite.internal.storage.PartitionStorage) Pair(org.apache.ignite.internal.util.Pair) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with DataRow

use of org.apache.ignite.internal.storage.DataRow in project ignite-3 by apache.

the class RocksDbPartitionStorage method invoke.

/**
 * {@inheritDoc}
 */
@Nullable
@Override
public <T> T invoke(SearchRow key, InvokeClosure<T> clo) throws StorageException {
    try {
        byte[] partitionKey = partitionKey(key);
        byte[] existingDataBytes = data.get(partitionKey);
        clo.call(existingDataBytes == null ? null : new DelegatingDataRow(key, existingDataBytes));
        switch(clo.operationType()) {
            case WRITE:
                DataRow newRow = clo.newRow();
                assert newRow != null;
                byte[] value = newRow.valueBytes();
                assert value != null;
                data.put(partitionKey, value);
                break;
            case REMOVE:
                data.delete(partitionKey);
                break;
            case NOOP:
                break;
            default:
                throw new UnsupportedOperationException(String.valueOf(clo.operationType()));
        }
        return clo.result();
    } catch (RocksDBException e) {
        throw new StorageException("Failed to access data in the storage", e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) StorageException(org.apache.ignite.internal.storage.StorageException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DataRow

use of org.apache.ignite.internal.storage.DataRow in project ignite-3 by apache.

the class RocksDbPartitionStorage method insertAll.

/**
 * {@inheritDoc}
 */
@Override
public Collection<DataRow> insertAll(List<? extends DataRow> rows) throws StorageException {
    List<DataRow> cantInsert = new ArrayList<>();
    try (var batch = new WriteBatch();
        var opts = new WriteOptions()) {
        for (DataRow row : rows) {
            byte[] partitionKey = partitionKey(row);
            if (data.get(partitionKey) == null) {
                byte[] value = row.valueBytes();
                assert value != null;
                data.put(batch, partitionKey, value);
            } else {
                cantInsert.add(row);
            }
        }
        db.write(opts, batch);
    } catch (RocksDBException e) {
        throw new StorageException("Filed to write data to the storage", e);
    }
    return cantInsert;
}
Also used : WriteOptions(org.rocksdb.WriteOptions) RocksDBException(org.rocksdb.RocksDBException) ArrayList(java.util.ArrayList) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) WriteBatch(org.rocksdb.WriteBatch) StorageException(org.apache.ignite.internal.storage.StorageException)

Example 4 with DataRow

use of org.apache.ignite.internal.storage.DataRow in project ignite-3 by apache.

the class RocksDbPartitionStorage method writeAll.

/**
 * {@inheritDoc}
 */
@Override
public void writeAll(List<? extends DataRow> rows) throws StorageException {
    try (WriteBatch batch = new WriteBatch();
        WriteOptions opts = new WriteOptions()) {
        for (DataRow row : rows) {
            byte[] value = row.valueBytes();
            assert value != null;
            data.put(batch, partitionKey(row), value);
        }
        db.write(opts, batch);
    } catch (RocksDBException e) {
        throw new StorageException("Filed to write data to the storage", e);
    }
}
Also used : WriteOptions(org.rocksdb.WriteOptions) RocksDBException(org.rocksdb.RocksDBException) WriteBatch(org.rocksdb.WriteBatch) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) StorageException(org.apache.ignite.internal.storage.StorageException)

Example 5 with DataRow

use of org.apache.ignite.internal.storage.DataRow 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)

Aggregations

DataRow (org.apache.ignite.internal.storage.DataRow)10 DelegatingDataRow (org.apache.ignite.internal.storage.basic.DelegatingDataRow)7 SimpleDataRow (org.apache.ignite.internal.storage.basic.SimpleDataRow)6 StorageException (org.apache.ignite.internal.storage.StorageException)5 RocksDBException (org.rocksdb.RocksDBException)5 ArrayList (java.util.ArrayList)4 Nullable (org.jetbrains.annotations.Nullable)3 WriteBatch (org.rocksdb.WriteBatch)3 WriteOptions (org.rocksdb.WriteOptions)3 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)2 ByteArray (org.apache.ignite.lang.ByteArray)2 UUID (java.util.UUID)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 RaftServerImpl (org.apache.ignite.internal.raft.server.impl.RaftServerImpl)1 ByteBufferRow (org.apache.ignite.internal.schema.ByteBufferRow)1 PartitionStorage (org.apache.ignite.internal.storage.PartitionStorage)1 BinarySearchRow (org.apache.ignite.internal.storage.basic.BinarySearchRow)1 PartitionListener (org.apache.ignite.internal.table.distributed.raft.PartitionListener)1 InternalTableImpl (org.apache.ignite.internal.table.distributed.storage.InternalTableImpl)1 VersionedRowStore (org.apache.ignite.internal.table.distributed.storage.VersionedRowStore)1