Search in sources :

Example 1 with ResultImpl

use of com.scalar.db.common.ResultImpl in project scalardb by scalar-labs.

the class CrudHandlerTest method scan_CalledAfterDeleteUnderRealSnapshot_ShouldReturnResultsWithoutDeletedRecord.

@Test
public void scan_CalledAfterDeleteUnderRealSnapshot_ShouldReturnResultsWithoutDeletedRecord() throws ExecutionException, CrudException {
    // Arrange
    Scan scan = prepareScan();
    result = prepareResult(TransactionState.COMMITTED);
    ImmutableMap<String, Column<?>> columns = ImmutableMap.<String, Column<?>>builder().put(ANY_NAME_1, TextColumn.of(ANY_NAME_1, ANY_TEXT_1)).put(ANY_NAME_2, TextColumn.of(ANY_NAME_2, ANY_TEXT_3)).put(Attribute.ID, ScalarDbUtils.toColumn(Attribute.toIdValue(ANY_ID_2))).put(Attribute.STATE, ScalarDbUtils.toColumn(Attribute.toStateValue(TransactionState.COMMITTED))).put(Attribute.VERSION, ScalarDbUtils.toColumn(Attribute.toVersionValue(2))).put(Attribute.BEFORE_ID, ScalarDbUtils.toColumn(Attribute.toBeforeIdValue(ANY_ID_1))).put(Attribute.BEFORE_STATE, ScalarDbUtils.toColumn(Attribute.toBeforeStateValue(TransactionState.COMMITTED))).put(Attribute.BEFORE_VERSION, ScalarDbUtils.toColumn(Attribute.toBeforeVersionValue(1))).build();
    Result result2 = new ResultImpl(columns, TABLE_METADATA);
    Map<Snapshot.Key, Optional<TransactionResult>> readSet = new HashMap<>();
    Map<Snapshot.Key, Delete> deleteSet = new HashMap<>();
    snapshot = new Snapshot(ANY_TX_ID, Isolation.SNAPSHOT, null, tableMetadataManager, parallelExecutor, readSet, new HashMap<>(), new HashMap<>(), deleteSet);
    handler = new CrudHandler(storage, snapshot, tableMetadataManager);
    when(scanner.iterator()).thenReturn(Arrays.asList(result, result2).iterator());
    when(storage.scan(scan)).thenReturn(scanner);
    Delete delete = new Delete(new Key(ANY_NAME_1, ANY_TEXT_1), new Key(ANY_NAME_2, ANY_TEXT_3)).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
    // Act
    handler.delete(delete);
    List<Result> results = handler.scan(scan);
    // Assert
    assertThat(results.size()).isEqualTo(1);
    assertThat(results.get(0)).isEqualTo(new FilteredResult(result, Collections.emptyList(), TABLE_METADATA));
    // check the delete set
    assertThat(deleteSet.size()).isEqualTo(1);
    assertThat(deleteSet).containsKey(new Snapshot.Key(delete));
    // check if the scanned data is inserted correctly in the read set
    assertThat(readSet.size()).isEqualTo(2);
    Snapshot.Key key1 = new Snapshot.Key(scan, result);
    assertThat(readSet.get(key1).isPresent()).isTrue();
    assertThat(readSet.get(key1).get()).isEqualTo(new TransactionResult(result));
    Snapshot.Key key2 = new Snapshot.Key(scan, result2);
    assertThat(readSet.get(key2).isPresent()).isTrue();
    assertThat(readSet.get(key2).get()).isEqualTo(new TransactionResult(result2));
}
Also used : Delete(com.scalar.db.api.Delete) Optional(java.util.Optional) HashMap(java.util.HashMap) ResultImpl(com.scalar.db.common.ResultImpl) Result(com.scalar.db.api.Result) TextColumn(com.scalar.db.io.TextColumn) Column(com.scalar.db.io.Column) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 2 with ResultImpl

use of com.scalar.db.common.ResultImpl in project scalardb by scalar-labs.

the class FilteredResultTest method setUp.

@BeforeEach
public void setUp() {
    // Arrange
    Map<String, Column<?>> columns = ImmutableMap.<String, Column<?>>builder().put(ACCOUNT_ID, ScalarDbUtils.toColumn(ACCOUNT_ID_VALUE)).put(ACCOUNT_TYPE, ScalarDbUtils.toColumn(ACCOUNT_TYPE_VALUE)).put(BALANCE, ScalarDbUtils.toColumn(BALANCE_VALUE)).put(Attribute.ID, ScalarDbUtils.toColumn(ID_VALUE)).put(Attribute.STATE, ScalarDbUtils.toColumn(STATE_VALUE)).put(Attribute.VERSION, ScalarDbUtils.toColumn(VERSION_VALUE)).put(Attribute.PREPARED_AT, ScalarDbUtils.toColumn(PREPARED_AT_VALUE)).put(Attribute.COMMITTED_AT, ScalarDbUtils.toColumn(COMMITTED_AT_VALUE)).put(Attribute.BEFORE_PREFIX + BALANCE, ScalarDbUtils.toColumn(BEFORE_BALANCE_VALUE)).put(Attribute.BEFORE_ID, ScalarDbUtils.toColumn(BEFORE_ID_VALUE)).put(Attribute.BEFORE_STATE, ScalarDbUtils.toColumn(BEFORE_STATE_VALUE)).put(Attribute.BEFORE_VERSION, ScalarDbUtils.toColumn(BEFORE_VERSION_VALUE)).put(Attribute.BEFORE_PREPARED_AT, ScalarDbUtils.toColumn(BEFORE_PREPARED_AT_VALUE)).put(Attribute.BEFORE_COMMITTED_AT, ScalarDbUtils.toColumn(BEFORE_COMMITTED_AT_VALUE)).build();
    result = new ResultImpl(columns, TABLE_METADATA);
}
Also used : Column(com.scalar.db.io.Column) ResultImpl(com.scalar.db.common.ResultImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ResultImpl

use of com.scalar.db.common.ResultImpl in project scalardb by scalar-labs.

the class FilteredResultTest method equals_DifferentResiltGiven_WithoutProjections_ShouldReturnFalse.

@Test
public void equals_DifferentResiltGiven_WithoutProjections_ShouldReturnFalse() {
    // Arrange
    FilteredResult filteredResult = new FilteredResult(result, Collections.emptyList(), TABLE_METADATA);
    FilteredResult anotherFilterResult = new FilteredResult(new ResultImpl(Collections.emptyMap(), TABLE_METADATA), Collections.emptyList(), TABLE_METADATA);
    // Act
    boolean isEqual = filteredResult.equals(anotherFilterResult);
    // Assert
    assertThat(isEqual).isFalse();
}
Also used : ResultImpl(com.scalar.db.common.ResultImpl) Test(org.junit.jupiter.api.Test)

Example 4 with ResultImpl

use of com.scalar.db.common.ResultImpl in project scalardb by scalar-labs.

the class FilteredResultTest method equals_ResultImplWithDifferentValuesGiven_WithProjections_ShouldReturnTrue.

@Test
public void equals_ResultImplWithDifferentValuesGiven_WithProjections_ShouldReturnTrue() {
    // Arrange
    Result filteredResult = new FilteredResult(result, Collections.singletonList(BALANCE), TABLE_METADATA);
    Result anotherResult = new ResultImpl(ImmutableMap.of(BALANCE, ScalarDbUtils.toColumn(BALANCE_VALUE)), TABLE_METADATA);
    // Act
    boolean isEqual = filteredResult.equals(anotherResult);
    // Assert
    assertThat(isEqual).isTrue();
}
Also used : ResultImpl(com.scalar.db.common.ResultImpl) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 5 with ResultImpl

use of com.scalar.db.common.ResultImpl in project scalardb by scalar-labs.

the class SnapshotTest method toSerializableWithExtraRead_MultipleScansInScanSetExist_ShouldProcessWithoutExceptions.

@Test
public void toSerializableWithExtraRead_MultipleScansInScanSetExist_ShouldProcessWithoutExceptions() throws ExecutionException {
    // Arrange
    snapshot = prepareSnapshot(Isolation.SERIALIZABLE, SerializableStrategy.EXTRA_READ);
    Scan scan1 = new Scan(new Key(ANY_NAME_1, ANY_TEXT_1)).withStart(new Key(ANY_NAME_2, ANY_TEXT_2)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
    Scan scan2 = new Scan(new Key(ANY_NAME_1, ANY_TEXT_2)).withStart(new Key(ANY_NAME_2, ANY_TEXT_1)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
    Result result1 = new TransactionResult(new ResultImpl(ImmutableMap.of(ANY_NAME_1, TextColumn.of(ANY_NAME_1, ANY_TEXT_1), ANY_NAME_2, TextColumn.of(ANY_NAME_2, ANY_TEXT_2), Attribute.ID, ScalarDbUtils.toColumn(Attribute.toIdValue("id1")), Attribute.VERSION, ScalarDbUtils.toColumn(Attribute.toVersionValue(ANY_VERSION))), TABLE_METADATA));
    Result result2 = new TransactionResult(new ResultImpl(ImmutableMap.of(ANY_NAME_1, TextColumn.of(ANY_NAME_1, ANY_TEXT_2), ANY_NAME_2, TextColumn.of(ANY_NAME_2, ANY_TEXT_1), Attribute.ID, ScalarDbUtils.toColumn(Attribute.toIdValue("id2")), Attribute.VERSION, ScalarDbUtils.toColumn(Attribute.toVersionValue(ANY_VERSION))), TABLE_METADATA));
    Snapshot.Key key1 = new Snapshot.Key(scan1, result1);
    Snapshot.Key key2 = new Snapshot.Key(scan2, result2);
    snapshot.put(scan1, Collections.singletonList(key1));
    snapshot.put(scan2, Collections.singletonList(key2));
    snapshot.put(key1, Optional.of(new TransactionResult(result1)));
    snapshot.put(key2, Optional.of(new TransactionResult(result2)));
    DistributedStorage storage = mock(DistributedStorage.class);
    Scanner scanner1 = mock(Scanner.class);
    when(scanner1.iterator()).thenReturn(Collections.singletonList(result1).iterator());
    Scan scan1WithProjections = new Scan(new Key(ANY_NAME_1, ANY_TEXT_1)).withStart(new Key(ANY_NAME_2, ANY_TEXT_2)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME).withProjections(Arrays.asList(Attribute.ID, Attribute.VERSION, ANY_NAME_1, ANY_NAME_2));
    when(storage.scan(scan1WithProjections)).thenReturn(scanner1);
    Scanner scanner2 = mock(Scanner.class);
    when(scanner2.iterator()).thenReturn(Collections.singletonList(result2).iterator());
    Scan scan2WithProjections = new Scan(new Key(ANY_NAME_1, ANY_TEXT_2)).withStart(new Key(ANY_NAME_2, ANY_TEXT_1)).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME).withProjections(Arrays.asList(Attribute.ID, Attribute.VERSION, ANY_NAME_1, ANY_NAME_2));
    when(storage.scan(scan2WithProjections)).thenReturn(scanner2);
    // Act Assert
    assertThatCode(() -> snapshot.toSerializableWithExtraRead(storage)).doesNotThrowAnyException();
}
Also used : Scanner(com.scalar.db.api.Scanner) DistributedStorage(com.scalar.db.api.DistributedStorage) Scan(com.scalar.db.api.Scan) ResultImpl(com.scalar.db.common.ResultImpl) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Aggregations

ResultImpl (com.scalar.db.common.ResultImpl)10 Test (org.junit.jupiter.api.Test)9 Result (com.scalar.db.api.Result)8 Column (com.scalar.db.io.Column)5 Key (com.scalar.db.io.Key)4 TextColumn (com.scalar.db.io.TextColumn)4 IntColumn (com.scalar.db.io.IntColumn)3 Put (com.scalar.db.api.Put)2 Scan (com.scalar.db.api.Scan)2 Delete (com.scalar.db.api.Delete)1 DistributedStorage (com.scalar.db.api.DistributedStorage)1 Scanner (com.scalar.db.api.Scanner)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1