Github user djkevincr commented on a diff in the pull request: https://github.com/apache/gora/pull/110#discussion_r124050129 --- Diff: gora-cassandra-cql/src/main/java/org/apache/gora/cassandra/store/CassandraStore.java --- @@ -0,0 +1,788 @@ +/* + * 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.gora.cassandra.store; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.ConsistencyLevel; +import com.datastax.driver.core.HostDistance; +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.PoolingOptions; +import com.datastax.driver.core.ProtocolOptions; +import com.datastax.driver.core.ProtocolVersion; +import com.datastax.driver.core.QueryOptions; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.SocketOptions; +import com.datastax.driver.core.TableMetadata; +import com.datastax.driver.core.TypeCodec; +import com.datastax.driver.core.policies.ConstantReconnectionPolicy; +import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; +import com.datastax.driver.core.policies.DefaultRetryPolicy; +import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy; +import com.datastax.driver.core.policies.ExponentialReconnectionPolicy; +import com.datastax.driver.core.policies.FallthroughRetryPolicy; +import com.datastax.driver.core.policies.LatencyAwarePolicy; +import com.datastax.driver.core.policies.LoggingRetryPolicy; +import com.datastax.driver.core.policies.RoundRobinPolicy; +import com.datastax.driver.core.policies.TokenAwarePolicy; +import com.datastax.driver.mapping.Mapper; +import com.datastax.driver.mapping.MappingManager; +import org.apache.gora.cassandra.bean.CassandraKey; +import org.apache.gora.cassandra.bean.ClusterKeyField; +import org.apache.gora.cassandra.bean.Field; +import org.apache.gora.cassandra.bean.KeySpace; +import org.apache.gora.cassandra.bean.PartitionKeyField; +import org.apache.gora.persistency.BeanFactory; +import org.apache.gora.persistency.Persistent; +import org.apache.gora.query.PartitionQuery; +import org.apache.gora.query.Query; +import org.apache.gora.query.Result; +import org.apache.gora.store.DataStore; +import org.apache.gora.store.DataStoreFactory; +import org.jdom.Attribute; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Properties; + +/** + * Implementation of Cassandra Store. + * + * @param <K> key class + * @param <T> persistent class + */ +public class CassandraStore<K, T extends Persistent> implements DataStore<K, T> { + + private static final String DEFAULT_MAPPING_FILE = "gora-cassandra-mapping.xml"; + + public static final Logger LOG = LoggerFactory.getLogger(CassandraStore.class); + + private BeanFactory<K, T> beanFactory; + + private Cluster cluster; + + private Class keyClass; + + private Class persistentClass; + + private CassandraMapping mapping; + + private boolean isUseNativeSerialization; + + private Mapper<T> mapper; + + private Session session; + + public CassandraStore() { + super(); + } + + /** + * In initializing the cassandra datastore, read the mapping file, creates the basic connection to cassandra cluster, + * according to the gora properties + * + * @param keyClass key class + * @param persistentClass persistent class + * @param properties properties + */ + public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) { + LOG.debug("Initializing Cassandra store"); + try { + this.keyClass = keyClass; + this.persistentClass = persistentClass; + String mappingFile = DataStoreFactory.getMappingFile(properties, this, DEFAULT_MAPPING_FILE); + List<String> codecs = readCustomCodec(properties); + mapping = readMapping(mappingFile); + isUseNativeSerialization = Boolean.parseBoolean(properties.getProperty(CassandraStoreParameters.USE_CASSANDRA_NATIVE_SERIALIZATION)); + Cluster.Builder builder = Cluster.builder(); + builder = populateSettings(builder, properties); + this.cluster = builder.build(); + if (codecs != null) { + registerCustomCodecs(codecs); + } + this.session = this.cluster.connect(); + if (isUseNativeSerialization) { + this.createSchema(); + MappingManager mappingManager = new MappingManager(session); + mapper = mappingManager.mapper(persistentClass); + } + + } catch (Exception e) { + LOG.error("Error while initializing Cassandra store: {}", + new Object[]{e.getMessage()}); + throw new RuntimeException(e); + } + } + + private void registerCustomCodecs(List<String> codecs) throws Exception { + for (String codec : codecs) { + this.cluster.getConfiguration().getCodecRegistry().register((TypeCodec<?>) Class.forName(codec).newInstance()); + } + } + + /** + * In this method we reads the mapping file and creates the Cassandra Mapping. + * + * @param filename mapping file name + * @return @{@link CassandraMapping} + * @throws IOException + */ + private CassandraMapping readMapping(String filename) throws IOException { + CassandraMapping map = new CassandraMapping(); --- End diff -- It is lot more clean to move this code to separate mapping builder class.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---