This repository has been archived by the owner on May 25, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connections now are object oriented, and basic keyspace and column fa…
…mily support is being provided, see sandbox for example
- Loading branch information
Nick Berardi
committed
Apr 18, 2010
1 parent
089c51c
commit 1adec35
Showing
15 changed files
with
834 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Apache.Cassandra; | ||
|
||
namespace FluentCassandra | ||
{ | ||
public class CassandraColumnFamily | ||
{ | ||
private CassandraKeyspace _keyspace; | ||
private IConnection _connection; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="keyspace"></param> | ||
/// <param name="connection"></param> | ||
public CassandraColumnFamily(CassandraKeyspace keyspace, IConnection connection) | ||
{ | ||
_keyspace = keyspace; | ||
_connection = connection; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <returns></returns> | ||
protected Cassandra.Client GetClient() | ||
{ | ||
return _connection.Client; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="record"></param> | ||
public void Insert(FluentColumnFamily record) | ||
{ | ||
Insert(record.ColumnFamily, record.Key, record); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="columnFamily"></param> | ||
/// <param name="key"></param> | ||
/// <param name="record"></param> | ||
public void Insert(string columnFamily, string key, FluentColumnFamily record) | ||
{ | ||
var utf8 = Encoding.UTF8; | ||
|
||
foreach (var col in record) | ||
{ | ||
var path = new ColumnPath { | ||
Column_family = columnFamily, | ||
Column = col.NameBytes | ||
}; | ||
|
||
GetClient().insert( | ||
_keyspace.KeyspaceName, | ||
key, | ||
path, | ||
col.ValueBytes, | ||
col.Timestamp.Ticks, | ||
ConsistencyLevel.ONE | ||
); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="record"></param> | ||
/// <param name="columnName"></param> | ||
public void Remove(FluentColumnFamily record, string columnName = null) | ||
{ | ||
Remove(record.ColumnFamily, record.Key, columnName); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="columnFamily"></param> | ||
/// <param name="key"></param> | ||
/// <param name="columnName"></param> | ||
public void Remove(string columnFamily, string key, string columnName = null) | ||
{ | ||
var utf8 = Encoding.UTF8; | ||
var path = new ColumnPath { | ||
Column_family = columnFamily | ||
}; | ||
|
||
if (!String.IsNullOrWhiteSpace(columnName)) | ||
path.Column = FluentColumn<string>.GetBytes(columnName); | ||
|
||
GetClient().remove( | ||
_keyspace.KeyspaceName, | ||
key, | ||
path, | ||
DateTimeOffset.UtcNow.Ticks, | ||
ConsistencyLevel.ONE | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace FluentCassandra | ||
{ | ||
public class CassandraContext : IDisposable | ||
{ | ||
private readonly ConnectionBuilder _connectionBuilder; | ||
private readonly IConnectionProvider _connectionProvider; | ||
private readonly IConnection _connection; | ||
private readonly CassandraKeyspace _keyspace; | ||
private bool _disposed; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="keyspace"></param> | ||
/// <param name="host"></param> | ||
/// <param name="port"></param> | ||
/// <param name="timeout"></param> | ||
/// <param name="provider"></param> | ||
public CassandraContext(string keyspace, string host, int port = 9160, int timeout = 0, string provider = "Normal") | ||
: this(new ConnectionBuilder(keyspace, host, port, timeout, provider)) { } | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="connectionString"></param> | ||
public CassandraContext(string connectionString) | ||
: this(new ConnectionBuilder(connectionString)) { } | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="connectionBuilder"></param> | ||
public CassandraContext(ConnectionBuilder connectionBuilder) | ||
{ | ||
_connectionBuilder = connectionBuilder; | ||
_connectionProvider = connectionBuilder.Provider; | ||
_connection = _connectionProvider.Open(); | ||
_keyspace = new CassandraKeyspace(_connectionBuilder.Keyspace, _connection); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the database. | ||
/// </summary> | ||
public CassandraKeyspace Keyspace | ||
{ | ||
get { return this._keyspace; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets ConnectionProvider. | ||
/// </summary> | ||
public IConnectionProvider ConnectionProvider | ||
{ | ||
get { return this._connectionProvider; } | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public IConnection Connection | ||
{ | ||
get { return this._connection; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets a typed column family. | ||
/// </summary> | ||
/// <typeparam name="T">Type of column family.</typeparam> | ||
/// <returns></returns> | ||
public CassandraColumnFamily<T> GetColumnFamily<T>() | ||
{ | ||
return _keyspace.GetColumnFamily<T>(); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
} | ||
|
||
/// <summary> | ||
/// The dispose. | ||
/// </summary> | ||
/// <param name="disposing"> | ||
/// The disposing. | ||
/// </param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!this._disposed && disposing && this._connection != null) | ||
this._connectionProvider.Close(this._connection); | ||
|
||
this._disposed = true; | ||
} | ||
|
||
/// <summary> | ||
/// Finalizes an instance of the <see cref="Mongo"/> class. | ||
/// </summary> | ||
~CassandraContext() | ||
{ | ||
this.Dispose(false); | ||
} | ||
} | ||
} |
Oops, something went wrong.