Create object for defining our table schema and provide utilities for generating CQL to create or ALTER the table schema.
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/a62fb0a0 Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/a62fb0a0 Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/a62fb0a0 Branch: refs/heads/datastax-cass-driver Commit: a62fb0a0c95ece13c84a563e58802230bdcff1cb Parents: 01c4970 Author: Michael Russo <[email protected]> Authored: Tue Feb 9 17:14:38 2016 -0800 Committer: Michael Russo <[email protected]> Committed: Tue Feb 9 17:14:38 2016 -0800 ---------------------------------------------------------------------- stack/corepersistence/common/pom.xml | 11 -- .../persistence/core/datastax/CQLUtils.java | 77 ++++++++++- .../core/datastax/TableDefinition.java | 129 +++++++++++++++++++ .../core/migration/schema/Migration.java | 2 + .../persistence/core/datastax/CQLUtilsTest.java | 73 +++++++++++ .../core/datastax/DatastaxClusterTest.java | 4 +- 6 files changed, 281 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/a62fb0a0/stack/corepersistence/common/pom.xml ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/pom.xml b/stack/corepersistence/common/pom.xml index 6b617cf..983c665 100644 --- a/stack/corepersistence/common/pom.xml +++ b/stack/corepersistence/common/pom.xml @@ -196,17 +196,6 @@ </exclusion> </exclusions> </dependency> - <dependency> - <groupId>com.datastax.cassandra</groupId> - <artifactId>cassandra-driver-mapping</artifactId> - <version>${datastax.version}</version> - <exclusions> - <exclusion> - <groupId>com.datastax.cassandra</groupId> - <artifactId>cassandra-driver-core</artifactId> - </exclusion> - </exclusions> - </dependency> <!-- LZ4 compression used for Datastax Java Driver: https://datastax.github.io/java-driver/2.1.7/features/compression/ --> http://git-wip-us.apache.org/repos/asf/usergrid/blob/a62fb0a0/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/CQLUtils.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/CQLUtils.java b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/CQLUtils.java index b663934..0a7408a 100644 --- a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/CQLUtils.java +++ b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/CQLUtils.java @@ -20,12 +20,35 @@ package org.apache.usergrid.persistence.core.datastax; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.usergrid.persistence.core.util.StringUtils; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.StringJoiner; public class CQLUtils { + + enum ACTION { + CREATE, UPDATE + } + + static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS"; + static String ALTER_TABLE = "ALTER TABLE"; + static String WITH ="WITH"; + static String AND = "AND"; + static String EQUAL = "="; + static String COMPRESSION = "compression"; + static String COMPACTION = "compaction"; + static String GC_GRACE_SECONDS = "gc_grace_seconds"; + static String PRIMARY_KEY = "PRIMARY KEY"; + static String COMPACT_STORAGE = "COMPACT STORAGE"; + static String CLUSTERING_ORDER_BY = "CLUSTERING ORDER BY"; + + private final static ObjectMapper mapper = new ObjectMapper(); + + public static String getFormattedReplication(String strategy, String strategyOptions) throws JsonProcessingException { Map<String, String> replicationSettings = new HashMap<>(); @@ -35,13 +58,63 @@ public class CQLUtils { String[] splitOptions = option.split(":"); replicationSettings.put(splitOptions[0], splitOptions[1]); } - ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(replicationSettings).replace("\"", "'"); } - public static void createColumnFamily(){ + public static String getMapAsCQLString(Map<String, Object> map) throws JsonProcessingException { + + return mapper.writeValueAsString(map).replace("\"", "'"); + } + + + public static String getTableCQL(TableDefinition tableDefinition, ACTION tableAction) throws Exception { + + StringJoiner cql = new StringJoiner(" "); + + if ( tableAction.equals(ACTION.CREATE) ){ + cql.add(CREATE_TABLE); + } else if ( tableAction.equals(ACTION.UPDATE) ){ + cql.add(ALTER_TABLE); + }else{ + throw new Exception("Invalid Action specified. Must of of type CQLUtils.Action"); + } + + cql.add( "\""+tableDefinition.getTableName()+"\"" ); + + + StringJoiner columnsString = new StringJoiner(","); + Map<String, String> columns = tableDefinition.getColumns(); + columns.forEach( (key, value) -> columnsString.add(key+" "+value)); + columnsString.add(PRIMARY_KEY +" ( "+StringUtils.join(tableDefinition.getPrimaryKeys(), ",") + " )"); + + StringJoiner orderingString = new StringJoiner(" "); + Map<String, String> ordering = tableDefinition.getClusteringOrder(); + ordering.forEach( (key, value) -> orderingString.add(key+" "+value)); + + if ( tableAction.equals(ACTION.CREATE) ){ + cql.add("(").add(columnsString.toString()).add(")") + .add(WITH) + .add(CLUSTERING_ORDER_BY).add("(").add(orderingString.toString()).add(")") + .add(AND) + .add(COMPACT_STORAGE) + .add(AND); + + } else if ( tableAction.equals(ACTION.UPDATE) ){ + cql.add(WITH); + + } + + + cql.add(COMPACTION).add(EQUAL).add( getMapAsCQLString( tableDefinition.getCompaction() ) ) + .add(AND) + .add(COMPRESSION).add(EQUAL).add( getMapAsCQLString( tableDefinition.getCompression() ) ) + .add(AND) + .add(GC_GRACE_SECONDS).add(EQUAL).add( tableDefinition.getGcGraceSeconds() ); + + + return cql.toString(); } http://git-wip-us.apache.org/repos/asf/usergrid/blob/a62fb0a0/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/TableDefinition.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/TableDefinition.java b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/TableDefinition.java new file mode 100644 index 0000000..58d43a5 --- /dev/null +++ b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/datastax/TableDefinition.java @@ -0,0 +1,129 @@ +/* + * + * * Licensed to the Apache Software Foundation (ASF) under one + * * or more contributor license agreements. See the NOTICE file + * * distributed with this work for additional information + * * regarding copyright ownership. The ASF licenses this file + * * to you under the Apache License, Version 2.0 (the + * * "License"); you may not use this file except in compliance + * * with the License. You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, + * * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * * KIND, either express or implied. See the License for the + * * specific language governing permissions and limitations + * * under the License. + * + */ + +package org.apache.usergrid.persistence.core.datastax; + + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public class TableDefinition { + + + public enum CacheOption { + + ALL( "ALL" ), + KEYS( "KEYS_ONLY" ), + ROWS( "ROWS_ONLY" ), + NONE( "NONE" ); + + private String value; + + + CacheOption( String value ) { + this.value = value; + } + + + public String getValue() { + return value; + } + } + + + private final String tableName; + private final Collection<String> primaryKeys; + private final Map<String, String> columns; + private final CacheOption cacheOption; + private final Map<String, Object> compaction; + private final String bloomFilterChance; + private final String readRepairChance; + private final Map<String, Object> compression; + private final String gcGraceSeconds; + private final Map<String, String> clusteringOrder; + + public TableDefinition( final String tableName, final Collection<String> primaryKeys, + final Map<String, String> columns, final CacheOption cacheOption, + final Map<String, String> clusteringOrder){ + + this.tableName = tableName; + this.primaryKeys = primaryKeys; + this.columns = columns; + this.cacheOption = cacheOption; + this.clusteringOrder = clusteringOrder; + + + // this are default settings always used + this.compaction = new HashMap<>(1); + compaction.put( "class", "LeveledCompactionStrategy" ); + this.bloomFilterChance = "0.1d"; + this.readRepairChance = "0.1d"; + this.compression = new HashMap<>(1); + compression.put("sstable_compression", "LZ4Compressor"); + this.gcGraceSeconds = "864000"; + + + + } + + public String getTableName() { + return tableName; + } + + public Collection<String> getPrimaryKeys() { + return primaryKeys; + } + + public Map<String, String> getColumns() { + return columns; + } + + public CacheOption getCacheOption() { + return cacheOption; + } + + public Map<String, Object> getCompaction() { + return compaction; + } + + public String getBloomFilterChance() { + return bloomFilterChance; + } + + public String getReadRepairChance() { + return readRepairChance; + } + + public Map<String, Object> getCompression() { + return compression; + } + + public String getGcGraceSeconds() { + return gcGraceSeconds; + } + + public Map<String, String> getClusteringOrder() { + return clusteringOrder; + } + + +} http://git-wip-us.apache.org/repos/asf/usergrid/blob/a62fb0a0/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/Migration.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/Migration.java b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/Migration.java index 9938b88..3896df2 100644 --- a/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/Migration.java +++ b/stack/corepersistence/common/src/main/java/org/apache/usergrid/persistence/core/migration/schema/Migration.java @@ -33,4 +33,6 @@ public interface Migration { * Get the column families required for this implementation. If one does not exist it will be created. */ Collection<MultiTenantColumnFamilyDefinition> getColumnFamilies(); + + //Collection<String> getTables(); } http://git-wip-us.apache.org/repos/asf/usergrid/blob/a62fb0a0/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/CQLUtilsTest.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/CQLUtilsTest.java b/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/CQLUtilsTest.java new file mode 100644 index 0000000..8ddfa3f --- /dev/null +++ b/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/CQLUtilsTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.usergrid.persistence.core.datastax; + + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertTrue; + +public class CQLUtilsTest { + + private static final Logger logger = LoggerFactory.getLogger( CQLUtilsTest.class ); + + @Test + public void testTableCQL() throws Exception { + + + Map<String, String> columns = new HashMap<>(); + columns.put("key", "blob"); + columns.put("column1", "text"); + columns.put("value", "blob"); + + List<String> primaryKeys = new ArrayList<>(); + primaryKeys.add("key"); + primaryKeys.add("column1"); + + Map<String, String> clusteringOrder = new HashMap<>(); + clusteringOrder.put("column1", "DESC"); + + + + TableDefinition table1 = new TableDefinition( + "table1", + primaryKeys, + columns, + TableDefinition.CacheOption.KEYS, + clusteringOrder + ); + + String createCQL = CQLUtils.getTableCQL(table1, CQLUtils.ACTION.CREATE); + String updateCQL = CQLUtils.getTableCQL(table1, CQLUtils.ACTION.UPDATE); + + assertTrue( createCQL.contains( CQLUtils.CREATE_TABLE ) && !createCQL.contains( CQLUtils.ALTER_TABLE ) ); + assertTrue( updateCQL.contains( CQLUtils.ALTER_TABLE ) && !updateCQL.contains( CQLUtils.CREATE_TABLE ) ); + //logger.info("CREATE: {}", createCQL); + //logger.info("UPDATE: {}", updateCQL); + + } + +} http://git-wip-us.apache.org/repos/asf/usergrid/blob/a62fb0a0/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/DatastaxClusterTest.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/DatastaxClusterTest.java b/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/DatastaxClusterTest.java index 477a205..5da23ce 100644 --- a/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/DatastaxClusterTest.java +++ b/stack/corepersistence/common/src/test/java/org/apache/usergrid/persistence/core/datastax/DatastaxClusterTest.java @@ -1,5 +1,3 @@ -package org.apache.usergrid.persistence.core.datastax; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,6 +16,8 @@ package org.apache.usergrid.persistence.core.datastax; * specific language governing permissions and limitations * under the License. */ +package org.apache.usergrid.persistence.core.datastax; + public class DatastaxClusterTest { //TODO
