public void storeSnapshot(InputStream snapshot) { byte[] streamReference = "EteTest".getBytes(); TodoSchemaTableFactory tableFactory = TodoSchemaTableFactory.of(Namespace.DEFAULT_NAMESPACE); SnapshotsStreamStore streamStore = SnapshotsStreamStore.of(transactionManager, tableFactory); log.info("Storing stream..."); Pair<Long, Sha256Hash> storedStream = streamStore.storeStream(snapshot); Long newStreamId = storedStream.getLhSide(); log.info("Stored stream with ID {}", newStreamId); transactionManager.runTaskWithRetry(transaction -> { // Load previous stream, and unmark it as used LatestSnapshotTable.LatestSnapshotRow row = LatestSnapshotTable.LatestSnapshotRow.of(0L); LatestSnapshotTable latestSnapshotTable = tableFactory.getLatestSnapshotTable(transaction); Optional<LatestSnapshotTable.LatestSnapshotRowResult> maybeRow = latestSnapshotTable.getRow(row); maybeRow.ifPresent(latestSnapshot -> { Long latestStreamId = maybeRow.get().getStreamId(); log.info("Marking stream {}, ref {}, as unused", latestStreamId, PtBytes.toString(streamReference)); Map<Long, byte[]> theMap = ImmutableMap.of(latestStreamId, streamReference); streamStore.unmarkStreamsAsUsed(transaction, theMap); }); streamStore.markStreamAsUsed(transaction, newStreamId, streamReference); log.info("Marked stream {} as used with reference {}", newStreamId, PtBytes.toString(streamReference)); // Record the latest snapshot latestSnapshotTable.putStreamId(row, newStreamId); return null; }); }
@Override public List<LatestSnapshotNamedColumnValue<?>> getRowColumns(LatestSnapshotRow row, ColumnSelection columns) { byte[] bytes = row.persistToBytes(); RowResult<byte[]> rowResult = t.getRows(tableRef, ImmutableSet.of(bytes), columns).get(bytes); if (rowResult == null) { return ImmutableList.of(); } else { List<LatestSnapshotNamedColumnValue<?>> ret = Lists.newArrayListWithCapacity(rowResult.getColumns().size()); for (Entry<byte[], byte[]> e : rowResult.getColumns().entrySet()) { ret.add(shortNameToHydrator.get(PtBytes.toString(e.getKey())).hydrateFromBytes(e.getValue())); } return ret; } }
@Override public List<LatestSnapshotNamedColumnValue<?>> getRowColumns(LatestSnapshotRow row, ColumnSelection columns) { byte[] bytes = row.persistToBytes(); RowResult<byte[]> rowResult = t.getRows(tableRef, ImmutableSet.of(bytes), columns).get(bytes); if (rowResult == null) { return ImmutableList.of(); } else { List<LatestSnapshotNamedColumnValue<?>> ret = Lists.newArrayListWithCapacity(rowResult.getColumns().size()); for (Entry<byte[], byte[]> e : rowResult.getColumns().entrySet()) { ret.add(shortNameToHydrator.get(PtBytes.toString(e.getKey())).hydrateFromBytes(e.getValue())); } return ret; } }
public Map<LatestSnapshotRow, Long> getStreamIds(Collection<LatestSnapshotRow> rows) { Map<Cell, LatestSnapshotRow> cells = Maps.newHashMapWithExpectedSize(rows.size()); for (LatestSnapshotRow row : rows) { cells.put(Cell.create(row.persistToBytes(), PtBytes.toCachedBytes("i")), row); } Map<Cell, byte[]> results = t.get(tableRef, cells.keySet()); Map<LatestSnapshotRow, Long> ret = Maps.newHashMapWithExpectedSize(results.size()); for (Entry<Cell, byte[]> e : results.entrySet()) { Long val = StreamId.BYTES_HYDRATOR.hydrateFromBytes(e.getValue()).getValue(); ret.put(cells.get(e.getKey()), val); } return ret; }
public static LatestSnapshotRow of(long key) { return new LatestSnapshotRow(key); }
@Override public LatestSnapshotRow hydrateFromBytes(byte[] __input) { int __index = 0; Long key = Long.MIN_VALUE ^ PtBytes.toLong(__input, __index); __index += 8; return new LatestSnapshotRow(key); } };
@Override public LatestSnapshotRow apply(Long row) { return LatestSnapshotRow.of(row); } };
public Optional<LatestSnapshotRowResult> getRow(LatestSnapshotRow row, ColumnSelection columns) { byte[] bytes = row.persistToBytes(); RowResult<byte[]> rowResult = t.getRows(tableRef, ImmutableSet.of(bytes), columns).get(bytes); if (rowResult == null) { return Optional.empty(); } else { return Optional.of(LatestSnapshotRowResult.of(rowResult)); } }
public static LatestSnapshotRow of(long key) { return new LatestSnapshotRow(key); }
public Map<LatestSnapshotRow, Long> getStreamIds(Collection<LatestSnapshotRow> rows) { Map<Cell, LatestSnapshotRow> cells = Maps.newHashMapWithExpectedSize(rows.size()); for (LatestSnapshotRow row : rows) { cells.put(Cell.create(row.persistToBytes(), PtBytes.toCachedBytes("i")), row); } Map<Cell, byte[]> results = t.get(tableRef, cells.keySet()); Map<LatestSnapshotRow, Long> ret = Maps.newHashMapWithExpectedSize(results.size()); for (Entry<Cell, byte[]> e : results.entrySet()) { Long val = StreamId.BYTES_HYDRATOR.hydrateFromBytes(e.getValue()).getValue(); ret.put(cells.get(e.getKey()), val); } return ret; }
public Optional<LatestSnapshotRowResult> getRow(LatestSnapshotRow row, ColumnSelection columns) { byte[] bytes = row.persistToBytes(); RowResult<byte[]> rowResult = t.getRows(tableRef, ImmutableSet.of(bytes), columns).get(bytes); if (rowResult == null) { return Optional.empty(); } else { return Optional.of(LatestSnapshotRowResult.of(rowResult)); } }
@Override public LatestSnapshotRow apply(Long row) { return LatestSnapshotRow.of(row); } };
@Override public LatestSnapshotRow hydrateFromBytes(byte[] __input) { int __index = 0; Long key = Long.MIN_VALUE ^ PtBytes.toLong(__input, __index); __index += 8; return new LatestSnapshotRow(key); } };