Search in sources :

Example 16 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project aroma-data-operations by RedRoma.

the class CassandraApplicationRepository method createStatementToSave.

private Statement createStatementToSave(Application app) {
    BatchStatement batch = new BatchStatement();
    // UUIDs
    UUID appId = UUID.fromString(app.applicationId);
    UUID iconId = null;
    UUID orgId = null;
    // Enums
    String tier = null;
    String programmingLanguage = null;
    if (app.tier != null) {
        tier = app.tier.toString();
    }
    if (app.programmingLanguage != null) {
        programmingLanguage = app.programmingLanguage.toString();
    }
    if (app.isSetApplicationIconMediaId()) {
        iconId = UUID.fromString(app.applicationIconMediaId);
    }
    if (app.isSetOrganizationId()) {
        orgId = UUID.fromString(app.organizationId);
    }
    Set<UUID> owners = Sets.nullToEmpty(app.owners).stream().map(UUID::fromString).collect(Collectors.toSet());
    Statement insertIntoMainTable = QueryBuilder.insertInto(TABLE_NAME).value(APP_ID, appId).value(APP_NAME, app.name).value(APP_DESCRIPTION, app.applicationDescription).value(ICON_MEDIA_ID, iconId).value(ORG_ID, orgId).value(OWNERS, owners).value(PROGRAMMING_LANGUAGE, programmingLanguage).value(TIME_PROVISIONED, app.timeOfProvisioning).value(TIME_OF_TOKEN_EXPIRATION, app.timeOfTokenExpiration).value(TIER, tier);
    batch.add(insertIntoMainTable);
    // Save into the "Recents Table"
    Long timeToLive = DEFAULT_RECENT_DURATION.getSeconds();
    Statement insertIntoRecentlyCreated = QueryBuilder.insertInto(TABLE_NAME_RECENTLY_CREATED).value(APP_ID, appId).value(APP_NAME, app.name).value(APP_DESCRIPTION, app.applicationDescription).value(ICON_MEDIA_ID, iconId).value(ORG_ID, orgId).value(OWNERS, owners).value(PROGRAMMING_LANGUAGE, programmingLanguage).value(TIME_PROVISIONED, app.timeOfProvisioning).value(TIME_OF_TOKEN_EXPIRATION, app.timeOfTokenExpiration).value(TIER, tier).using(ttl(timeToLive.intValue()));
    batch.add(insertIntoRecentlyCreated);
    return batch;
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) StringAssertions.nonEmptyString(tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString) UUID(java.util.UUID)

Example 17 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project aroma-data-operations by RedRoma.

the class CassandraFollowerRepository method createStatementToSaveFollowing.

private Statement createStatementToSaveFollowing(User user, Application app) {
    UUID userId = UUID.fromString(user.userId);
    UUID appId = UUID.fromString(app.applicationId);
    BatchStatement batch = new BatchStatement();
    Statement insertIntoAppFollowersTable = QueryBuilder.insertInto(Follow.TABLE_NAME_APP_FOLLOWERS).value(APP_ID, appId).value(USER_ID, userId).value(APP_NAME, app.name).value(USER_FIRST_NAME, user.firstName).value(TIME_OF_FOLLOW, Instant.now().toEpochMilli());
    batch.add(insertIntoAppFollowersTable);
    Statement insertIntoUserFollowingsTable = QueryBuilder.insertInto(Follow.TABLE_NAME_USER_FOLLOWING).value(APP_ID, appId).value(USER_ID, userId).value(APP_NAME, app.name).value(USER_FIRST_NAME, user.firstName).value(TIME_OF_FOLLOW, Instant.now().toEpochMilli());
    batch.add(insertIntoUserFollowingsTable);
    return batch;
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) UUID(java.util.UUID)

Example 18 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project aroma-data-operations by RedRoma.

the class CassandraFollowerRepository method createDeleteStatementFor.

private Statement createDeleteStatementFor(String userId, String applicationId) {
    UUID appUuid = UUID.fromString(applicationId);
    UUID userUuid = UUID.fromString(userId);
    BatchStatement batch = new BatchStatement();
    Statement deleteFromAppFollowersTable = QueryBuilder.delete().all().from(Follow.TABLE_NAME_APP_FOLLOWERS).where(eq(APP_ID, appUuid)).and(eq(USER_ID, userUuid));
    batch.add(deleteFromAppFollowersTable);
    Statement deleteFromUserFollowingsTable = QueryBuilder.delete().all().from(Follow.TABLE_NAME_USER_FOLLOWING).where(eq(APP_ID, appUuid)).and(eq(USER_ID, userUuid));
    batch.add(deleteFromUserFollowingsTable);
    return batch;
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) UUID(java.util.UUID)

Example 19 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project aroma-data-operations by RedRoma.

the class CassandraTokenRepository method createStatementToDeleteToken.

private Statement createStatementToDeleteToken(String tokenId) throws TException {
    UUID tokenUuid = UUID.fromString(tokenId);
    // Need to get Token first
    AuthenticationToken token = this.getToken(tokenId);
    UUID ownerUuid = UUID.fromString(token.ownerId);
    BatchStatement batch = new BatchStatement();
    Statement deleteFromMainTable = QueryBuilder.delete().all().from(Tokens.TABLE_NAME).where(eq(TOKEN_ID, tokenUuid));
    batch.add(deleteFromMainTable);
    Statement deleteFromOwnersTable = QueryBuilder.delete().all().from(Tokens.TABLE_NAME_BY_OWNER).where(eq(OWNER_ID, ownerUuid));
    batch.add(deleteFromOwnersTable);
    return batch;
}
Also used : AuthenticationToken(tech.aroma.thrift.authentication.AuthenticationToken) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) StringAssertions.validUUID(tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID) UUID(java.util.UUID)

Example 20 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project aroma-data-operations by RedRoma.

the class CassandraTokenRepository method createStatementToInsert.

private Statement createStatementToInsert(AuthenticationToken token) throws InvalidArgumentException {
    // UUIDs
    UUID tokenId = UUID.fromString(token.tokenId);
    UUID ownerId = UUID.fromString(token.ownerId);
    UUID orgId = null;
    // Enums
    String tokenType = null;
    if (token.tokenType != null) {
        tokenType = token.tokenType.toString();
    }
    long timeToLive = TimeFunctions.toSeconds(DEFAULT_TOKEN_LIFETIME);
    if (!isNullOrEmpty(token.organizationId)) {
        checkThat(token.organizationId).usingMessage("token organizationId must be a UUID type").throwing(InvalidArgumentException.class).is(validUUID());
        orgId = UUID.fromString(token.organizationId);
    }
    BatchStatement batch = new BatchStatement();
    Statement insertIntoMainTable = QueryBuilder.insertInto(Tokens.TABLE_NAME).value(TOKEN_ID, tokenId).value(OWNER_ID, ownerId).value(ORG_ID, orgId).value(OWNER_NAME, token.ownerName).value(TIME_OF_EXPIRATION, token.timeOfExpiration).value(TIME_OF_CREATION, token.timeOfCreation).value(TOKEN_TYPE, tokenType).using(ttl((int) timeToLive));
    batch.add(insertIntoMainTable);
    Statement insertIntoOwnersTable = QueryBuilder.insertInto(Tokens.TABLE_NAME_BY_OWNER).value(OWNER_ID, ownerId).value(TOKEN_ID, tokenId).value(ORG_ID, orgId).value(OWNER_NAME, token.ownerName).value(TIME_OF_EXPIRATION, token.timeOfExpiration).value(TIME_OF_CREATION, token.timeOfCreation).value(TOKEN_TYPE, tokenType).using(ttl((int) timeToLive));
    batch.add(insertIntoOwnersTable);
    return batch;
}
Also used : InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) StringAssertions.nonEmptyString(tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString) StringAssertions.validUUID(tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID) UUID(java.util.UUID)

Aggregations

BatchStatement (com.datastax.driver.core.BatchStatement)22 Statement (com.datastax.driver.core.Statement)11 UUID (java.util.UUID)7 PreparedStatement (com.datastax.driver.core.PreparedStatement)5 Session (com.datastax.driver.core.Session)4 Test (org.junit.Test)4 ResultSet (com.datastax.driver.core.ResultSet)3 StringAssertions.validUUID (tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID)3 BoundStatement (com.datastax.driver.core.BoundStatement)2 StringAssertions.nonEmptyString (tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString)2 Type (com.datastax.driver.core.BatchStatement.Type)1 Cluster (com.datastax.driver.core.Cluster)1 Builder (com.datastax.driver.core.Cluster.Builder)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 HostDistance (com.datastax.driver.core.HostDistance)1 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 ProtocolVersion (com.datastax.driver.core.ProtocolVersion)1 SimpleStatement (com.datastax.driver.core.SimpleStatement)1