Skip to content

Commit

Permalink
Merge branch 'master' of github.com:zznate/intravert-ug
Browse files Browse the repository at this point in the history
  • Loading branch information
zznate committed Apr 5, 2013
2 parents 6b86c96 + 9bf5e37 commit 618284c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -139,7 +139,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/usergrid/vx/experimental/CompositeTool.java
Expand Up @@ -19,6 +19,8 @@
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.utils.ByteBufferUtil;

public class CompositeTool {
Expand Down Expand Up @@ -78,4 +80,13 @@ public static List<byte[]> readComposite(byte[] column) {
}
return result;
}

public static void prettyPrintComposite(byte [] column, List<AbstractType> columnType){
List<byte[]> parts = readComposite(column);
for (int i =0;i<parts.size();i++){
try {
System.out.println( columnType.get(i).getString(ByteBuffer.wrap(parts.get(i))) );
} catch (Exception ex){}
}
}
}
Expand Up @@ -323,7 +323,7 @@ public void createAndListKeyspaces() throws Exception {
.putObject("opsRes", new JsonObject()
.putString("0", "OK")
.putString("1", "OK")
.putArray("2", new JsonArray((List) asList("ks2", "myks", "ks1"))))
.putArray("2", new JsonArray((List) asList("myks", "ks1","ks2"))))
.toString();

assertJSONEquals("Failed to get keyspaces", expectedResponse, actualResponse);
Expand Down
139 changes: 139 additions & 0 deletions src/test/java/org/usergrid/vx/experimental/ThriftITest.java
@@ -1,8 +1,27 @@
package org.usergrid.vx.experimental;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;

import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.locator.SimpleStrategy;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.CfDef;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.KsDef;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.usergrid.vx.client.thrift.FramedConnWrapper;
Expand All @@ -22,4 +41,124 @@ public void aThriftSanityTest() throws Exception {
Assert.assertTrue( c.describe_keyspaces().size() > 1 );
wrap.close();
}

@Test
@RequiresColumnFamily(ksName = "myks", cfName = "mycf")
public void aChangingAComparator() throws Exception {

FramedConnWrapper wrap = new FramedConnWrapper("localhost",9160);
wrap.open();
Cassandra.Client c = wrap.getClient();
KsDef k = new KsDef();
k.setName("composites");
Map m = new HashMap();
m.put("replication_factor","1");
k.strategy_options = m;
k.setStrategy_class(SimpleStrategy.class.getName());
k.cf_defs = new ArrayList<CfDef>();
c.system_add_keyspace(k);

c.set_keyspace("composites");
CfDef d = new CfDef();
d.setKeyspace("composites");
d.setName("atest");
d.setComparator_type("CompositeType(UTF8Type,UTF8Type)");
d.setDefault_validation_class("UTF8Type");
c.system_add_column_family(d);
Assert.assertTrue( c.describe_keyspaces().size() > 1 );
//eds friend bob is a good friend
List<byte[]> parts = new ArrayList<byte[]>();
parts.add("friend".getBytes() );
parts.add("bob".getBytes());
ColumnParent cp = new ColumnParent();
cp.setColumn_family("atest");
Column col = new Column();
col.setName( CompositeTool.makeComposite(parts) );
col.setTimestamp(System.nanoTime());
col.setValue("good".getBytes());
c.insert(ByteBufferUtil.bytes("ed"), cp, col, ConsistencyLevel.ONE);


//eds friend ted is a bad friend
List<byte[]> parts2 = new ArrayList<byte[]>();
parts2.add("friend".getBytes() );
parts2.add("ted".getBytes());


Column col2 = new Column();
col2.setName( CompositeTool.makeComposite(parts2) );
col2.setTimestamp(System.nanoTime());
col2.setValue("bad".getBytes());
c.insert(ByteBufferUtil.bytes("ed"), cp, col2, ConsistencyLevel.ONE);

List<byte[]> parts3 = new ArrayList<byte[]>();
parts3.add("friend".getBytes() );
int [] sep = new int[] { 0 , 0 };

SlicePredicate sp = new SlicePredicate();
SliceRange sr = new SliceRange();
sr.setStart( ByteBuffer.wrap(CompositeTool.makeComposite(parts3, sep) ) );
sr.setFinish(new byte[0]);
sp.setSlice_range(sr);

List<ColumnOrSuperColumn> res = c.get_slice(ByteBufferUtil.bytes("ed"), cp, sp, ConsistencyLevel.ONE);
Assert.assertEquals(2, res.size());


//------------

d.setComparator_type("CompositeType(UTF8Type,UTF8Type,UTF8Type)");
c.system_update_column_family(d);

List<byte[]> parts4 = new ArrayList<byte[]>();
parts4.add("friend".getBytes() );
parts4.add("ted".getBytes());
parts4.add("yokers".getBytes());


Column col4 = new Column();
col4.setName( CompositeTool.makeComposite(parts4) );
col4.setTimestamp(System.nanoTime());
col4.setValue("good".getBytes());
c.insert(ByteBufferUtil.bytes("ed"), cp, col4, ConsistencyLevel.ONE);


@SuppressWarnings("rawtypes")
List<AbstractType> t = new ArrayList<AbstractType>();
t.add(UTF8Type.instance);
t.add(UTF8Type.instance);
t.add(UTF8Type.instance);

res = c.get_slice(ByteBufferUtil.bytes("ed"), cp, sp, ConsistencyLevel.ONE);
for (int i =0;i< res.size();i++){

CompositeTool.prettyPrintComposite(res.get(i).column.getName(), t);
}
Assert.assertEquals(3, res.size());


//so there is a column in there with 2 parts...can delete it?
ColumnPath path = new ColumnPath();
path.setColumn(CompositeTool.makeComposite(parts2));
path.setColumn_family("atest");
c.remove(ByteBufferUtil.bytes("ed"), path, System.nanoTime(), ConsistencyLevel.ONE);


//canwewrite it back?
col2.setTimestamp(System.nanoTime());
c.insert(ByteBufferUtil.bytes("ed"), cp, col2, ConsistencyLevel.ONE);

//can you dig (slice) it!
SlicePredicate sp2 = new SlicePredicate();
SliceRange sr2 = new SliceRange();
sr2.setStart(CompositeTool.makeComposite(parts2));
sr2.setFinish( new byte[0]);
sr2.setCount(100);
sp2.setSlice_range(sr2);
List<ColumnOrSuperColumn>res3 = c.get_slice(ByteBufferUtil.bytes("ed"), cp, sp2, ConsistencyLevel.ONE);
CompositeTool.prettyPrintComposite(res3.get(0).column.getName(), t);
Assert.assertEquals(2, res3.size() );
wrap.close();
}

}

0 comments on commit 618284c

Please sign in to comment.