New LINQ Style

In new style, ToCassandraByte method is unnecessary. And you can use ">=", "<=" instead of "GreaterThanOrEqual", "LessThanOrEqual", "Between" methods.

// ver 0.6
from x in context.ColumnList
where x.ColumnFamily == "Product" &&
      x.Key == "key1" &&
      x.SuperColumn == "sc1".ToCassandraByte() &&
      x.Column.GreaterThanOrEqual(1)
select x;

// ver 0.7
from x in context.ColumnList
where x.ColumnFamily == "Product" &&
      x.Key == "key1" &&
      x.SuperColumn == "sc1" &&
      x.Column >= 1
select x;

This new style is realized by CassandraBinary type. CassandraBinary is wrapper of byte[]. And it overload "==", "!=", ">=", "<=", ">", "<", "implicit(byte[])". CassandraEntity class use CassandraBinary in ver 0.7.

// ver 0.6
class CassandraEntity
{
    		public string Key { get; set; }
		public byte[] SuperColumn { get; set; }
		public byte[] Column { get; set; }
}

// ver 0.7
class CassandraEntity
{
    		public CassandraBinary Key { get; set; }
		public CassandraBinary SuperColumn { get; set; }
		public CassandraBinary Column { get; set; }
		public Dictionary<object, CassandraBinary> Index { get; set; }    // for Secondary Index
}
You can use 0.6 style in the future. So you don't have to rewrite LINQ.

Secondary Index

Cassandraemon ver 0.7 support Secondary Index. You can write IndexExpression as Index["ColumnName"] == "col". Support operator is "==", ">=", "<=", ">", "<", but "==" operator is required at least one.

from x in context.ColumnList
where x.ColumnFamily == "Product" &&
      x.Index["ColumnName1"] == "col1" &&
      x.Index["ColumnName2"] >= 1 &&
      x.Index["ColumnName3"] < DateTime.Now &&
      x.Column.In("ColumnName1", "ColumnName2")
select x;

You want to specify IndexClause.start_key and IndexClause.count, then write Key >= "key" and Take(count).

var query = from x in context.ColumnList
            where x.ColumnFamily == "Product" &&
                  x.Key >= 1 &&
                  x.Index["ColumnName1"] == "col1" &&
                  x.Column.In("ColumnName1", "ColumnName2")
            select x;

foreach(var entity in query.Take(10))
{
    Console.WriteLine(entity.Key.ToUTF8());
}

! If you want to use Secondary Index, you must set up index column by live schema update.

Live Schema Update

CassandraContext class implement some method for live schema update.

class CassandraContext
{
	public string SystemAddKeyspace(KsDef ksDef)
	public string SystemUpdateKeyspace(KsDef ksDef)
	public string SystemDropKeyspace(string keyspace)
	public string SystemAddColumnFamily(CfDef cfDef)
	public string SystemUpdateColumnFamily(CfDef cfDef)
	public string SystemDropColumnFamily(string columnFamily)
	public void Truncate(string columnFamily)
}

TTL

You want to set TTL to one column. Then all you need to do is set Apache.Cassandra.Column.Ttl. But you want to set TTL to multi column in bulk, then you can use Cassandraemon extension method.

Column.SetTtl (int ttl)
Column.SetNameValueTtl (object name, object value, int ttl)
Column.SetNameValueTimestampTtl (object name, object value, long unixTime, int ttl)

List<Column>.Add (object name, object value, int ttl)
List<Column>.Add (object name, object value, long unixTime, int ttl)
List<Column>.SetTtl (int ttl)    // set ttl all column

SuperColumn.SetColumn (object name, object value, int ttl)
SuperColumn.SetColumn (object name, object value, long unixTime, int ttl)
SuperColumn.AddColumn (object name, object value, int ttl)
SuperColumn.AddColumn (object name, object value, long unixTime, int ttl)
SuperColumn.SetTtl (int ttl)    // set ttl all column

List<SuperColumn>.SetTtl (int ttl)    // set ttl all column

Support multiget_count

Cassandraemon ver 0.7 support multiget_count. If you want to use it, then you specify multiple keys by "In" method and use CountColumn<TKey> generic method. TKey is type of Cassandra key value. CountColumn<TKey> method return Dictionary<TKey, int>.

var query = from x in context.ColumnList
            where x.ColumnFamily == "Product" &&
                  x.Key.In("key1", "key2") &&
                  x.Column >= 1
            select x;

Dictionary<string, int> d = query.CountColumn<string>();

Last edited Apr 2, 2011 at 6:09 PM by sabro, version 3

Comments

No comments yet.