com.ebuddy.cassandra.dao.AbstractColumnFamilyTemplate Maven / Gradle / Ivy
/*
* Copyright 2013 eBuddy B.V.
*
* Licensed 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 com.ebuddy.cassandra.dao;
import javax.annotation.Nullable;
import com.ebuddy.cassandra.BatchContext;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
/**
* Abstract superclass for Cassandra Data Access templates.
*
* @param the type of row key
* @param the type of top column name (super column name for Super Column Families);
* use Void if this is not specific to a column family
* @param the type of column value;
* use Void if this is not specific to a column family
* @author Eric Zoerner [email protected]
*/
// Extends KeyspaceTemplate as an implementation convenience to inherit the implementations of begin and commit
// If in the future we add more operations to KeyspaceOperations, then we will need to split keyspace template
// so this class doesn't inherit methods only appropriate for a keyspace.
public abstract class AbstractColumnFamilyTemplate extends KeyspaceTemplate {
/**
* Used for queries where we just ask for all columns.
*/
protected static final int ALL = Integer.MAX_VALUE;
/**
* The serializer used for column values, or null if not specific to one column family..
*/
@Nullable
private final Serializer valueSerializer;
/**
* The serializer for a standard column name or a super-column name, or null if not specific to one column family.
*/
@Nullable
private final Serializer topSerializer;
/**
* The default column family for this DAO, or null if not specific to a single column family.
*/
@Nullable
private final String columnFamily;
/**
* Constructor.
*
* @param keyspace the Keyspace
* @param columnFamily the name of the column family or null if this is not specific to a column family.
* @param keySerializer the serializer for row keys
* @param topSerializer the serializer for the top columns (columns for a Column Family,
* superColumns for a Super Column Family).
* If null, then this instance is not specific to one Column Family.
*/
protected AbstractColumnFamilyTemplate(Keyspace keyspace,
@Nullable String columnFamily,
Serializer keySerializer,
@Nullable Serializer topSerializer,
@Nullable Serializer valueSerializer) {
super(keyspace, keySerializer);
this.topSerializer = topSerializer;
this.columnFamily = columnFamily;
this.valueSerializer = valueSerializer;
}
/**
* Remove the entire row.
* @param rowKey the row key
*/
@SuppressWarnings("UnusedDeclaration")
public final void removeRow(K rowKey) {
removeRow(rowKey, null);
}
/**
* Remove the entire row in the default column family.
* @param rowKey the row key
* @param batchContext optional BatchContext
*/
public final void removeRow(K rowKey,
@Nullable BatchContext batchContext) {
Mutator mutator = validateAndGetMutator(batchContext);
if (mutator == null) {
createMutator().delete(rowKey, columnFamily, null, null);
} else {
mutator.addDeletion(rowKey, columnFamily);
}
// we used to translate hector exceptions into spring exceptions here, but spring dependency was removed
}
protected final Mutator createMutator() {
return HFactory.createMutator(getKeyspace(), getKeySerializer());
}
protected final Serializer getValueSerializer() {
// the assumption is that if this method is called then null is not acceptable
if (valueSerializer == null) {
throw new IllegalStateException("value serializer is null");
}
return valueSerializer;
}
protected final Serializer getTopSerializer() {
// the assumption is that if this method is called then null is not acceptable
if (topSerializer == null) {
throw new IllegalStateException("top serializer is null");
}
return topSerializer;
}
@Nullable
public String getColumnFamily() {
return columnFamily;
}
}