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

Commit

Permalink
added support for specified column values in a column family
Browse files Browse the repository at this point in the history
  • Loading branch information
devdazed committed Jan 12, 2012
1 parent c50b794 commit ed43bc6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
14 changes: 12 additions & 2 deletions lib/column_family.js
Expand Up @@ -43,6 +43,15 @@ var ColumnFamily = function(keyspace, definition){
this.columnMarshaller = new Marshal(definition.comparator_type);
this.valueMarshaller = new Marshal(definition.default_validation_class);
this.keyMarshaller = new Marshal(definition.key_validation_class);
this.columns = {};

if(definition.column_metadata && Array.isArray(definition.column_metadata)){
var i = 0, len = definition.column_metadata.length, col;
for(; i < len; i += 1){
col = definition.column_metadata[i];
this.columns[col.name] = new Marshal(col.validation_class);
}
}
};
//util.inherits(ColumnFamily, ttype.CfDef);

Expand Down Expand Up @@ -78,11 +87,12 @@ ColumnFamily.prototype.insert = function(key, values, options, callback){
value = '';
}

var col = new Column(prop, value, ts, options.ttl);
var col = new Column(prop, value, ts, options.ttl),
valueMarshaller = this.columns[prop] || this.valueMarshaller;

mutations.push(new ttype.Mutation({
column_or_supercolumn: new ttype.ColumnOrSuperColumn({
column: col.toThrift(this.columnMarshaller, this.valueMarshaller)
column: col.toThrift(this.columnMarshaller, valueMarshaller)
})
}));
}
Expand Down
55 changes: 51 additions & 4 deletions lib/keyspace.js
Expand Up @@ -9,7 +9,7 @@ var ColumnFamily = require('./column_family'),
* @constant
*/
var NOOP = function(){};

/**
* Creates an instance of a keyspace
* @constructor
Expand Down Expand Up @@ -95,7 +95,36 @@ Keyspace.prototype.describe = function(callback){
/**
* Creates a column family with options
* @param {String} name The name of the column family to create
* @param {Object} options The options for the columns family
* @param {Object} options The options for the columns family, options are:
* <ul>
* <li>column_type: Can be "Standard" or "Super" Defaults to "Standard" </li>
* <li>comparator_type: The default comparator type</li>
* <li>subcomparator_type: The default subcomparator type</li>
* <li>comment: A comment for the cf</li>
* <li>read_repair_chance: </li>
* <li>column_metadata: </li>
* <li>gc_grace_seconds: </li>
* <li>default_validation_class: </li>
* <li>min_compaction_threshold: </li>
* <li>max_compaction_threshold: </li>
* <li>replicate_on_write: </li>
* <li>merge_shards_chance: </li>
* <li>key_validation_class: </li>
* <li>key_alias: </li>
* <li>compaction_strategy: </li>
* <li>compaction_strategy_options: </li>
* <li>compression_options: </li>
* <li>bloom_filter_fp_chance: </li>
* <li>columns: Columns is an array of column options each element in the array is an object with these options:
* <ul>
* <li>name: *REQUIRED* The name of the column</li>
* <li>validation_class: *REQUIRED* The validation class. Defaults to BytesType</li>
* <li>index_type: The type of index</li>
* <li>index_name: The name of the index</li>
* <li>index_options: The options for the index, </li>
* </ul>
* </li>
* </ul>
* @param {Function} callback The callback to invoke once the column family has been created
*/
Keyspace.prototype.createColumnFamily = function(name, options, callback){
Expand All @@ -106,7 +135,25 @@ Keyspace.prototype.createColumnFamily = function(name, options, callback){

callback = callback || NOOP;
options = options || {};


var meta;
if(options.columns && Array.isArray(options.columns)){
var i = 0, len = options.columns.length, col;
meta = [];

for(; i < len; i += 1){
col = options.columns[i];

meta.push(new ttypes.ColumnDef({
name: col.name,
validation_class: col.validation_class,
index_type: col.index_type,
index_name: col.index_name,
index_options: col.index_options
}));
}
}

var cfdef = new ttypes.CfDef({
keyspace: this.name,
name: name,
Expand All @@ -115,7 +162,7 @@ Keyspace.prototype.createColumnFamily = function(name, options, callback){
subcomparator_type: options.subcomparator_type,
comment: options.comment,
read_repair_chance: options.read_repair_chance || 1,
column_metadata: options.column_metadata,
column_metadata: meta,
gc_grace_seconds: options.gc_grace_seconds,
default_validation_class: options.default_validation_class,
min_compaction_threshold: options.min_compaction_threshold,
Expand Down
20 changes: 15 additions & 5 deletions lib/row.js
Expand Up @@ -52,18 +52,28 @@ util.inherits(Row, Array);
* @param {ColumnFamily} cf The column family creating the row
*/
Row.fromThrift = function(key, columns, cf){
var data = { columns: [], key:key },
var data = { columns: [], key:key },
schema = {}, i = 0, len = columns.length;

//TODO: Implement counter and super columns
for(; i < len; i += 1){
data.columns.push( columns[i].column );
}
schema.value_types = {}; //TODO:implement this

schema.value_types = {};
schema.default_value_type = cf.definition.default_validation_class;
schema.default_name_type = cf.definition.comparator_type;


if(cf.definition.column_metadata && Array.isArray(cf.definition.column_metadata)){
i = 0; len = cf.definition.column_metadata.length;
var item;

for(; i < len; i += 1){
item = cf.definition.column_metadata[i];
schema.value_types[item.name] = item.validation_class;
}
}

return new Row(data, schema);
};

Expand Down

0 comments on commit ed43bc6

Please sign in to comment.