Skip to content

Commit

Permalink
Working add but still with some hard codes
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardcapriolo committed Jun 14, 2014
1 parent 0347127 commit ae52774
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
52 changes: 47 additions & 5 deletions src/main/java/io/teknek/intravert/action/impl/UpsertAction.java
@@ -1,14 +1,22 @@
package io.teknek.intravert.action.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.cassandra.db.ConsistencyLevel;
import org.apache.cassandra.db.IMutation;
import org.apache.cassandra.db.RowMutation;
import org.apache.cassandra.db.filter.QueryPath;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.exceptions.OverloadedException;
import org.apache.cassandra.exceptions.UnavailableException;
import org.apache.cassandra.exceptions.WriteTimeoutException;
import org.apache.cassandra.service.StorageProxy;
import org.apache.cassandra.utils.ByteBufferUtil;

import io.teknek.intravert.action.Action;
import io.teknek.intravert.action.filter.Filter;
Expand All @@ -18,22 +26,56 @@
import io.teknek.intravert.model.Response;
import io.teknek.intravert.service.ApplicationContext;
import io.teknek.intravert.service.RequestContext;
import io.teknek.intravert.util.ResponseUtil;
import io.teknek.intravert.util.TypeUtil;

public class UpsertAction implements Action {

@Override
public void doAction(Operation operation, Response response, RequestContext request,
ApplicationContext application) {

String keyspace = (String) operation.getArguments().get("keyspace");
String columnFamily = (String) operation.getArguments().get("columnFamily");
Object rowkey = TypeUtil.convert(operation.getArguments().get("rowkey"));
Object column = TypeUtil.convert(operation.getArguments().get("column"));
Object value = TypeUtil.convert(operation.getArguments().get("value"));

Map m = new HashMap();
m.put(Constants.RESULT, Constants.OK);
response.getResults().put(operation.getId(), Arrays.asList(m));
List<IMutation> changes = new ArrayList<>();
RowMutation rm = new RowMutation(keyspace, ByteBufferUtil.bytes((String)rowkey));
QueryPath qp = new QueryPath(columnFamily, null, ByteBufferUtil.bytes((String) column));
rm.add(qp, ByteBufferUtil.bytes((String) value), System.nanoTime());
changes.add(rm);
try {
StorageProxy.mutate(changes, ConsistencyLevel.QUORUM);
} catch (WriteTimeoutException | UnavailableException | OverloadedException e) {
throw new RuntimeException(e);
}
response.getResults().put(operation.getId(), ResponseUtil.getDefaultHappy());

}

}

/*
* List<IMutation> changes = new ArrayList<>();
{
RowMutation rm = new RowMutation(IV_KEYSPACE, ByteBufferUtil.bytes(name));
QueryPath qp = new QueryPath(FILTER_CF, null, ByteBufferUtil.bytes("spec"));
rm.add(qp, ByteBufferUtil.bytes(n.getSpec().toString()), System.nanoTime());
changes.add(rm);
}
{
RowMutation rm = new RowMutation(IV_KEYSPACE, ByteBufferUtil.bytes(name));
QueryPath qp = new QueryPath(FILTER_CF, null, ByteBufferUtil.bytes("theClass"));
String cs = n.getTheClass() == null ? "" : n.getTheClass();
rm.add(qp, ByteBufferUtil.bytes(cs), System.nanoTime());
changes.add(rm);
}
{
RowMutation rm = new RowMutation(IV_KEYSPACE, ByteBufferUtil.bytes(name));
QueryPath qp = new QueryPath(FILTER_CF, null, ByteBufferUtil.bytes("script"));
String cs = n.getScript() == null ? "" : n.getScript();
rm.add(qp, ByteBufferUtil.bytes(cs), System.nanoTime());
changes.add(rm);
}
StorageProxy.mutate(changes, ConsistencyLevel.QUORUM);
*/
4 changes: 3 additions & 1 deletion src/test/java/io/teknek/intravert/daemon/UpsertTest.java
Expand Up @@ -48,7 +48,9 @@ public void createApplicationFilter() throws JsonGenerationException, JsonMappin
.withId("3").withType(ActionFactory.UPSERT).withArguments(
new ImmutableMap.Builder<String, Object>().put("rowkey", "ecapriolo")
.put("column","firstname")
.put("value","edward").build()));
.put("value","edward")
.put("keyspace","example")
.put("columnFamily", "upsert").build()));
Client cl = new Client();
Response response = cl.post("http://127.0.0.1:7654", request);
List<Map> results = (List<Map>) response.getResults().get("1");
Expand Down

0 comments on commit ae52774

Please sign in to comment.