Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #130 from mrvisser/cql3-query-consistency

Add query-level tunable consistency
  • Loading branch information...
commit a8cfdacedaa7588a79e4b03e3f3ff7bc316cb6ae 2 parents 4c68934 + afc85fa
@devdazed devdazed authored
View
24 lib/connection.js
@@ -187,7 +187,8 @@ var Connection = function(options){
this.cqlVersion = options.cqlVersion;
/**
- * Consistency level for cql queries
+ * Default consistency level for cql queries. Can be overriden by the
+ * options in each cql query
*/
this.consistencylevel = options.consistencylevel || DEFAULT_CONSISTENCYLEVEL;
@@ -426,7 +427,7 @@ Connection.prototype.cql = function(cmd, args, options, callback){
callback = NOOP;
}
- var cql, escaped = [], self = this;
+ var cql, consistency_level, escaped = [], self = this;
if(args){
cql = new Buffer(formatCQL(cmd, args));
@@ -434,6 +435,9 @@ Connection.prototype.cql = function(cmd, args, options, callback){
cql = new Buffer(cmd);
}
+ // allow the query options to override the default consistency
+ consistency_level = options.consistencyLevel || self.consistencylevel;
+
function onReturn(err, res){
if (err){
callback(err);
@@ -457,19 +461,19 @@ Connection.prototype.cql = function(cmd, args, options, callback){
if(v===3)
if(options.gzip === true){
zlib.deflate(cql, function(err, cqlz){
- self.execute('execute_cql3_query', cqlz, ttype.Compression.GZIP, self.consistencylevel, onReturn);
+ self.execute('execute_cql3_query', cqlz, ttype.Compression.GZIP, consistency_level, onReturn);
});
} else {
- self.execute('execute_cql3_query', cql, ttype.Compression.NONE, self.consistencylevel, onReturn);
+ self.execute('execute_cql3_query', cql, ttype.Compression.NONE, consistency_level, onReturn);
}
else
if(options.gzip === true){
- zlib.deflate(cql, function(err, cqlz){
- self.execute('execute_cql_query', cqlz, ttype.Compression.GZIP, onReturn);
- });
- } else {
- self.execute('execute_cql_query', cql, ttype.Compression.NONE, onReturn);
- }
+ zlib.deflate(cql, function(err, cqlz){
+ self.execute('execute_cql_query', cqlz, ttype.Compression.GZIP, onReturn);
+ });
+ } else {
+ self.execute('execute_cql_query', cql, ttype.Compression.NONE, onReturn);
+ }
}
View
17 test/cql3.js
@@ -7,6 +7,19 @@ var poolConfig = require('./helpers/connection'),
// CQL3 introduces 4 different types of ColumnFamilies, see:
// https://issues.apache.org/jira/secure/attachment/12511286/create_cf_syntaxes.txt
+function testCqlFail() {
+ var args = Array.prototype.slice.call(arguments);
+
+ return function(test, assert){
+ args.push(function(err, res){
+ assert.isDefined(err);
+ test.finish();
+ });
+
+ conn.cql.apply(conn, args);
+ };
+}
+
function testCql(){
var args = Array.prototype.slice.call(arguments);
var tests = args.pop();
@@ -114,6 +127,10 @@ module.exports = {
assert.ok(res[0].get('foo').value === 'bar');
}),
+ // using consistency quorum should fail if query-level consistency is working, as we test against a single node
+ // with RF=3
+ 'test cql static CF select with consistency': testCqlFail(config['static_select#cql'], {'consistencyLevel': 2}),
+
'test cql static CF select *':testCql(config['static_select*#cql'], function(test, assert, err, res){
assert.ok(res.length === 1);
assert.ok(res[0] instanceof Helenus.Row);
View
8 test/helpers/cql3.json
@@ -1,8 +1,8 @@
{
- "create_ks#cql" : "CREATE KEYSPACE helenus_cql3_test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };",
- "create_ks#cql_v1" : "CREATE KEYSPACE helenus_cql3_test WITH strategy_class=SimpleStrategy AND strategy_options:replication_factor=1",
- "use#cql" : "USE helenus_cql3_test",
- "drop_ks#cql" : "DROP KEYSPACE helenus_cql3_test",
+ "create_ks#cql" : "CREATE KEYSPACE helenus_cql3_test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };",
+ "create_ks#cql_v1" : "CREATE KEYSPACE helenus_cql3_test WITH strategy_class=SimpleStrategy AND strategy_options:replication_factor=3",
+ "use#cql" : "USE helenus_cql3_test",
+ "drop_ks#cql" : "DROP KEYSPACE helenus_cql3_test",
"static_create_cf#cql" : "CREATE COLUMNFAMILY cql_test (id text, foo text, PRIMARY KEY (id))",
"static_create_cnt_cf#cql" : "CREATE TABLE cql_cnt_test (key varchar PRIMARY KEY, cnt counter)",
View
22 test/thrift.js
@@ -49,15 +49,15 @@ module.exports = {
// Add error handler to avoid uncaught exception.
badConn.on('error', function (err) { assert.isDefined(err); });
badConn.connect(function(err, keyspace) {
- assert.isDefined(err);
- badConn.createKeyspace(config.keyspace, function(err){
+ assert.isDefined(err);
+ badConn.createKeyspace(config.keyspace, function(err){
+ assert.isDefined(err);
+ badConn.dropKeyspace(config.keyspace, function(err){
assert.isDefined(err);
- badConn.dropKeyspace(config.keyspace, function(err){
- assert.isDefined(err);
- badConn.close();
- test.finish();
- });
- });
+ badConn.close();
+ test.finish();
+ });
+ });
});
},
@@ -705,8 +705,8 @@ module.exports = {
cf_standard.get(config.standard_row_key, function(err, row){
assert.ifError(err);
assert.ok(row.count === 3);
+ test.finish();
});
- test.finish();
});
},
@@ -760,8 +760,8 @@ module.exports = {
cf_standard.get(key, function(err, row){
assert.ifError(err);
assert.ok(row.count === 0);
+ test.finish();
});
- test.finish();
});
},
@@ -771,8 +771,8 @@ module.exports = {
cf_standard.get(config.standard_row_key, function(err, row){
assert.ifError(err);
assert.ok(row.count === 0);
+ test.finish();
});
- test.finish();
});
},
Please sign in to comment.
Something went wrong with that request. Please try again.