Search in sources :

Example 16 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerRepositoryExample method runExample.

public void runExample() {
    createTablesIfNotExists();
    this.traderRepository.deleteAll();
    this.tradeRepository.deleteAll();
    this.traderRepository.save(new Trader("demo_trader1", "John", "Doe"));
    this.traderRepository.save(new Trader("demo_trader2", "Mary", "Jane"));
    this.traderRepository.save(new Trader("demo_trader3", "Scott", "Smith"));
    this.tradeRepository.save(new Trade("1", "BUY", 100.0, 50.0, "STOCK1", "demo_trader1", Arrays.asList(99.0, 101.00)));
    this.tradeRepository.save(new Trade("2", "BUY", 105.0, 60.0, "STOCK2", "demo_trader1", Arrays.asList(99.0, 101.00)));
    this.tradeRepository.save(new Trade("3", "BUY", 100.0, 50.0, "STOCK1", "demo_trader1", Arrays.asList(99.0, 101.00)));
    this.tradeRepository.save(new Trade("1", "BUY", 100.0, 70.0, "STOCK2", "demo_trader2", Arrays.asList(99.0, 101.00)));
    this.tradeRepository.save(new Trade("2", "BUY", 103.0, 50.0, "STOCK1", "demo_trader2", Arrays.asList(99.0, 101.00)));
    this.tradeRepository.save(new Trade("3", "SELL", 100.0, 52.0, "STOCK2", "demo_trader2", Arrays.asList(99.0, 101.00)));
    this.tradeRepository.save(new Trade("1", "SELL", 98.0, 50.0, "STOCK1", "demo_trader3", Arrays.asList(99.0, 101.00)));
    this.tradeRepository.save(new Trade("2", "SELL", 110.0, 50.0, "STOCK2", "demo_trader3", Arrays.asList(99.0, 101.00)));
    LOGGER.info("The table for trades has been cleared and " + this.tradeRepository.count() + " new trades have been inserted:");
    Iterable<Trade> allTrades = this.tradeRepository.findAll();
    LOGGER.info("All trades:");
    for (Trade t : allTrades) {
        LOGGER.info(t);
    }
    LOGGER.info("These are the Cloud Spanner primary keys for the trades:");
    for (Trade t : allTrades) {
        Key key = this.spannerSchemaUtils.getKey(t);
        LOGGER.info(key);
    }
    LOGGER.info("There are " + this.tradeRepository.countByAction("BUY") + " BUY trades:");
    for (Trade t : this.tradeRepository.findByAction("BUY")) {
        LOGGER.info(t);
    }
    LOGGER.info("A query method can retrieve a single entity:");
    LOGGER.info(this.tradeRepository.getAnyOneTrade());
    LOGGER.info("A query method can also select properties in entities:");
    this.tradeRepository.getTradeIds("BUY").stream().forEach((x) -> LOGGER.info(x));
    LOGGER.info("Try http://localhost:8080/trades in the browser to see all trades.");
}
Also used : Key(com.google.cloud.spanner.Key)

Example 17 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method existsByIdTest.

@Test
public void existsByIdTest() {
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenReturn(true);
    when(this.readContext.read(any(), any(), any(), any())).thenReturn(results);
    when(this.databaseClient.singleUse(any())).thenReturn(this.readContext);
    Key key = Key.of("key");
    KeySet keySet = KeySet.singleKey(key);
    assertThat(this.spannerTemplate.existsById(TestEntity.class, key)).isTrue();
    verify(this.databaseClient, times(1)).singleUse();
    verify(this.readContext, times(1)).read(eq("custom_test_table"), eq(keySet), eq(Collections.singleton("id")));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 18 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method deleteByKeyTest.

@Test
public void deleteByKeyTest() {
    Key key = Key.of("key");
    Mutation mutation = Mutation.delete("custom_test_table", key);
    KeySet keys = KeySet.newBuilder().addKey(key).build();
    List<Mutation> mutations = Collections.singletonList(mutation);
    when(this.mutationFactory.delete(eq(TestEntity.class), same(key))).thenReturn(mutation);
    verifyBeforeAndAfterEvents(new BeforeDeleteEvent(mutations, null, keys, TestEntity.class), new AfterDeleteEvent(mutations, null, keys, TestEntity.class), () -> this.spannerTemplate.delete(TestEntity.class, key), x -> x.verify(this.databaseClient, times(1)).write(eq(Collections.singletonList(mutation))));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) AfterDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterDeleteEvent) Mutation(com.google.cloud.spanner.Mutation) BeforeDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 19 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityWriterTests method writeValidColumnToKey.

@Test
public void writeValidColumnToKey() {
    Key key = this.spannerEntityWriter.convertToKey(true);
    assertThat(key).isEqualTo(Key.of(true));
}
Also used : Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 20 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerCompositeKeyProperty method getId.

Key getId(Object entity) {
    PersistentPropertyAccessor accessor = getOwner().getPropertyAccessor(entity);
    List keyParts = new ArrayList();
    for (SpannerPersistentProperty spannerPersistentProperty : this.primaryKeyColumns) {
        Object value = accessor.getProperty(spannerPersistentProperty);
        if (spannerPersistentProperty.isEmbedded()) {
            Key embeddedKeyParts = this.spannerPersistentEntity.getSpannerMappingContext().getPersistentEntity(spannerPersistentProperty.getType()).getIdProperty().getId(value);
            for (Object keyPart : embeddedKeyParts.getParts()) {
                keyParts.add(keyPart);
            }
        } else if (spannerPersistentProperty.getAnnotatedColumnItemType() == null || value == null) {
            keyParts.add(value);
        } else {
            keyParts.add(this.spannerPersistentEntity.getSpannerEntityProcessor().getSpannerWriteConverter().convert(value, SpannerTypeMapper.getSimpleJavaClassFor(spannerPersistentProperty.getAnnotatedColumnItemType())));
        }
    }
    return this.spannerPersistentEntity.getSpannerEntityProcessor().convertToKey(keyParts);
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.cloud.spanner.Key)

Aggregations

Key (com.google.cloud.spanner.Key)26 Test (org.junit.Test)16 KeySet (com.google.cloud.spanner.KeySet)8 PrimaryKey (org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey)7 ArrayList (java.util.ArrayList)4 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)4 Mutation (com.google.cloud.spanner.Mutation)3 ResultSet (com.google.cloud.spanner.ResultSet)3 SpannerOperations (org.springframework.cloud.gcp.data.spanner.core.SpannerOperations)3 SpannerPageableQueryOptions (org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions)3 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)3 Sort (org.springframework.data.domain.Sort)3 PersistentProperty (org.springframework.data.mapping.PersistentProperty)3 Statement (com.google.cloud.spanner.Statement)2 Struct (com.google.cloud.spanner.Struct)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 Pageable (org.springframework.data.domain.Pageable)2 ApiFuture (com.google.api.core.ApiFuture)1 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)1