Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
fixes related to virtual keyspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
edanuff committed Jan 20, 2011
1 parent 21f30df commit 8c6bf4f
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

import me.prettyprint.hector.api.Serializer;


/**
* A base class for serializer implementations.
* Takes care of the default implementations of to/fromBytesList and to/fromBytesMap. Extenders of
* this class only need to implement the toBytes and fromBytes.
*
* A base class for serializer implementations. Takes care of the default
* implementations of to/fromBytesList and to/fromBytesMap. Extenders of this
* class only need to implement the toBytes and fromBytes.
*
* @author Ed Anuff
*
*
* @param <T>
*/
public abstract class AbstractSerializer<T> implements Serializer<T> {
Expand All @@ -33,39 +32,36 @@ public byte[] toBytes(T obj) {
bb.get(bytes, 0, bytes.length);
return bytes;
}
@Override

@Override
public T fromBytes(byte[] bytes) {
return fromByteBuffer(ByteBuffer.wrap(bytes));
}

/*
public ByteBuffer toByteBuffer(T obj) {
return ByteBuffer.wrap(toBytes(obj));
}

public ByteBuffer toByteBuffer(T obj, ByteBuffer byteBuffer, int offset, int length) {
byteBuffer.put(toBytes(obj), offset, length);
return byteBuffer;
}
*/
/*
* public ByteBuffer toByteBuffer(T obj) { return
* ByteBuffer.wrap(toBytes(obj)); }
*
* public ByteBuffer toByteBuffer(T obj, ByteBuffer byteBuffer, int offset,
* int length) { byteBuffer.put(toBytes(obj), offset, length); return
* byteBuffer; }
*/

@Override
public abstract T fromByteBuffer(ByteBuffer byteBuffer);

/*
public T fromByteBuffer(ByteBuffer byteBuffer) {
return fromBytes(byteBuffer.array());
}
public T fromByteBuffer(ByteBuffer byteBuffer, int offset, int length) {
return fromBytes(Arrays.copyOfRange(byteBuffer.array(), offset, length));
}
*/
* public T fromByteBuffer(ByteBuffer byteBuffer) { return
* fromBytes(byteBuffer.array()); }
*
* public T fromByteBuffer(ByteBuffer byteBuffer, int offset, int length) {
* return fromBytes(Arrays.copyOfRange(byteBuffer.array(), offset, length)); }
*/

@Override
public Set<ByteBuffer> toBytesSet(List<T> list) {
Set<ByteBuffer> bytesList = new HashSet<ByteBuffer>(computeInitialHashSize(list.size()));
Set<ByteBuffer> bytesList = new HashSet<ByteBuffer>(
computeInitialHashSize(list.size()));
for (T s : list) {
bytesList.add(toByteBuffer(s));
}
Expand Down Expand Up @@ -101,7 +97,8 @@ public List<T> fromBytesList(List<ByteBuffer> list) {

@Override
public <V> Map<ByteBuffer, V> toBytesMap(Map<T, V> map) {
Map<ByteBuffer, V> bytesMap = new LinkedHashMap<ByteBuffer, V>(computeInitialHashSize(map.size()));
Map<ByteBuffer, V> bytesMap = new LinkedHashMap<ByteBuffer, V>(
computeInitialHashSize(map.size()));
for (Entry<T, V> entry : map.entrySet()) {
bytesMap.put(toByteBuffer(entry.getKey()), entry.getValue());
}
Expand All @@ -110,14 +107,15 @@ public <V> Map<ByteBuffer, V> toBytesMap(Map<T, V> map) {

@Override
public <V> Map<T, V> fromBytesMap(Map<ByteBuffer, V> map) {
Map<T, V> objMap = new LinkedHashMap<T, V>(computeInitialHashSize(map.size()));
Map<T, V> objMap = new LinkedHashMap<T, V>(
computeInitialHashSize(map.size()));
for (Entry<ByteBuffer, V> entry : map.entrySet()) {
objMap.put(fromByteBuffer(entry.getKey()), entry.getValue());
}
return objMap;
}

private int computeInitialHashSize(int initialSize) {
return Double.valueOf(Math.floor(initialSize / 0.75)).intValue() + 1;
public int computeInitialHashSize(int initialSize) {
return Double.valueOf(Math.floor(initialSize / 0.75)).intValue() + 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package me.prettyprint.cassandra.serializers;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.exceptions.HectorSerializationException;
Expand Down Expand Up @@ -60,6 +65,33 @@ public S fromByteBuffer(ByteBuffer bytes) {
return s;
}

@Override
public List<S> fromBytesList(List<ByteBuffer> list) {
List<S> objList = new ArrayList<S>(list.size());
for (ByteBuffer s : list) {
try {
objList.add(fromByteBuffer(s));
} catch (HectorSerializationException e) {
// not a prefixed key, discard
}
}
return objList;
}

@Override
public <V> Map<S, V> fromBytesMap(Map<ByteBuffer, V> map) {
Map<S, V> objMap = new LinkedHashMap<S, V>(
computeInitialHashSize(map.size()));
for (Entry<ByteBuffer, V> entry : map.entrySet()) {
try {
objMap.put(fromByteBuffer(entry.getKey()), entry.getValue());
} catch (HectorSerializationException e) {
// not a prefixed key, discard
}
}
return objMap;
}

private static int compareByteArrays(byte[] bytes1, int offset1, int len1,
byte[] bytes2, int offset2, int len2) {
if (null == bytes1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import me.prettyprint.cassandra.serializers.PrefixedSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.ConsistencyLevelPolicy;
import me.prettyprint.hector.api.HConsistencyLevel;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.exceptions.HectorTransportException;
Expand Down Expand Up @@ -81,25 +80,13 @@ public void batchMutate(
super.batchMutate(ps.toBytesMap(mutationMap));
}

@Override
public void batchMutate(BatchMutation batchMutate) throws HectorException {

super.batchMutate(batchMutate);
}

@Override
public int getCount(ByteBuffer key, ColumnParent columnParent,
SlicePredicate predicate) throws HectorException {

return super.getCount(ps.toByteBuffer(key), columnParent, predicate);
}

@Override
public CassandraHost getCassandraHost() {

return super.getCassandraHost();
}

@Override
public Map<ByteBuffer, List<Column>> getRangeSlices(
ColumnParent columnParent, SlicePredicate predicate, KeyRange keyRange)
Expand Down Expand Up @@ -160,40 +147,43 @@ public Map<ByteBuffer, List<Column>> multigetSlice(List<ByteBuffer> keys,
ColumnParent columnParent, SlicePredicate predicate)
throws HectorException {

return super.multigetSlice(ps.toBytesList(keys), columnParent, predicate);
return ps.fromBytesMap(super.multigetSlice(ps.toBytesList(keys),
columnParent, predicate));
}

@Override
public Map<ByteBuffer, SuperColumn> multigetSuperColumn(
List<ByteBuffer> keys, ColumnPath columnPath) throws HectorException {

return super.multigetSuperColumn(ps.toBytesList(keys), columnPath);
return ps.fromBytesMap(super.multigetSuperColumn(ps.toBytesList(keys),
columnPath));
}

@Override
public Map<ByteBuffer, SuperColumn> multigetSuperColumn(
List<ByteBuffer> keys, ColumnPath columnPath, boolean reversed, int size)
throws HectorException {

return super.multigetSuperColumn(ps.toBytesList(keys), columnPath,
reversed, size);
return ps.fromBytesMap(super.multigetSuperColumn(ps.toBytesList(keys),
columnPath, reversed, size));
}

@Override
public Map<ByteBuffer, List<SuperColumn>> multigetSuperSlice(
List<ByteBuffer> keys, ColumnParent columnParent, SlicePredicate predicate)
throws HectorException {

return super.multigetSuperSlice(ps.toBytesList(keys), columnParent,
predicate);
return ps.fromBytesMap(super.multigetSuperSlice(ps.toBytesList(keys),
columnParent, predicate));
}

@Override
public Map<ByteBuffer, List<Column>> getIndexedSlices(
ColumnParent columnParent, IndexClause indexClause,
SlicePredicate predicate) throws HectorException {

return super.getIndexedSlices(columnParent, indexClause, predicate);
return ps.fromBytesMap(super.getIndexedSlices(columnParent, indexClause,
predicate));
}

@Override
Expand All @@ -207,8 +197,8 @@ public Map<ByteBuffer, Integer> multigetCount(List<ByteBuffer> keys,
ColumnParent columnParent, SlicePredicate slicePredicate)
throws HectorException {

return super.multigetCount(ps.toBytesList(keys), columnParent,
slicePredicate);
return ps.fromBytesMap(super.multigetCount(ps.toBytesList(keys),
columnParent, slicePredicate));
}

@Override
Expand All @@ -218,29 +208,11 @@ public void remove(ByteBuffer key, ColumnPath columnPath, long timestamp)
super.remove(ps.toByteBuffer(key), columnPath, timestamp);
}

@Override
public String getName() {

return super.getName();
}

@Override
public Column getColumn(ByteBuffer key, ColumnPath columnPath)
throws HectorException {

return super.getColumn(ps.toByteBuffer(key), columnPath);
}

@Override
public HConsistencyLevel getConsistencyLevel(OperationType operationType) {

return super.getConsistencyLevel(operationType);
}

@Override
public String toString() {

return super.toString();
}

}
115 changes: 115 additions & 0 deletions core/src/test/java/me/prettyprint/hector/api/KeyspaceCreationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package me.prettyprint.hector.api;

import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;

import java.util.ArrayList;
import java.util.List;

import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ThriftColumnDef;
import me.prettyprint.hector.api.ddl.ColumnDefinition;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;

import org.apache.cassandra.thrift.ColumnDef;
import org.apache.cassandra.thrift.IndexType;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.thrift.TException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KeyspaceCreationTest extends BaseEmbededServerSetupTest {

private static final Logger log = LoggerFactory
.getLogger(KeyspaceCreationTest.class);
private final static String KEYSPACE = "Keyspace2";
private static final StringSerializer se = new StringSerializer();
private Cluster cluster;

private static final int colCount = 6;

@Before
public void setupCase() {
cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170");
}

@After
public void teardownCase() {
cluster = null;
}

@Test
public void testCreateKeyspace() throws InvalidRequestException, TException {

List<ColumnFamilyDefinition> cf_defs = new ArrayList<ColumnFamilyDefinition>();

List<ColumnDef> columns = new ArrayList<ColumnDef>();
for (int i = 0; i < colCount; i++) {
String cName = "col" + i;
log.info("Creating column " + cName);
columns.add(newIndexedColumnDef(cName, "BytesType"));
}

List<ColumnDefinition> columnMetadata = ThriftColumnDef
.fromThriftList(columns);

ColumnFamilyDefinition cf_def = HFactory.createColumnFamilyDefinition(
KEYSPACE, "TEST_CF", ComparatorType.BYTESTYPE, columnMetadata);

cf_defs.add(cf_def);

makeKeyspace(KEYSPACE, cf_defs);

checkKeyspaces();

log.info("Done, all errors to console after this point likely due to shutdown");

}

public void checkKeyspaces() {

List<KeyspaceDefinition> ksDefs = null;
try {
ksDefs = cluster.describeKeyspaces();
} catch (Exception e) {
log.error("Unable to describe keyspaces", e);
}

if (ksDefs != null) {
for (KeyspaceDefinition ksDef : ksDefs) {
log.info(ksDef.getName().toString());
}
}

}

ColumnDef newIndexedColumnDef(String column_name, String comparer) {
ColumnDef cd = new ColumnDef(se.toByteBuffer(column_name), comparer);
cd.setIndex_name(column_name);
cd.setIndex_type(IndexType.KEYS);
return cd;
}

public void makeKeyspace(String keyspace, List<ColumnFamilyDefinition> cf_defs)
throws InvalidRequestException, TException {

log.info("Creating keyspace: " + keyspace);
try {
KeyspaceDefinition ks_def = HFactory.createKeyspaceDefinition(keyspace,
"org.apache.cassandra.locator.SimpleStrategy", 1, cf_defs);

cluster.addKeyspace(ks_def);

log.info("Created keyspace: " + keyspace);
} catch (Exception e) {
log.error("Unable to create keyspace " + keyspace, e);
}
}

}

0 comments on commit 8c6bf4f

Please sign in to comment.