public string DropColumnFamily(Server server, string columnFamily) { _cachedKeyspaceDescription = null; using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port))) return(session.GetClient().system_drop_column_family(columnFamily)); }
public string AddColumnFamily(Server server, CfDef definition) { _cachedKeyspaceDescription = null; using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port))) return session.GetClient().system_add_column_family(definition); }
/// <summary> /// Execute the column family operation against the connection to the server. /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="action"></param> /// <param name="throwOnError"></param> /// <returns></returns> public TResult ExecuteOperation <TResult>(Operation <TResult> action, bool?throwOnError = null) { if (WasDisposed) { throw new ObjectDisposedException(GetType().FullName); } var localSession = _session == null; var session = _session; if (session == null) { session = new CassandraSession(ConnectionBuilder); } action.Context = this; try { var result = session.ExecuteOperation(action, throwOnError ?? ThrowErrors); LastError = session.LastError; return(result); } finally { if (localSession && session != null) { session.Dispose(); } } }
public void ResetDatabase() { using (var session = new CassandraSession(ConnectionBuilder)) using (var db = new CassandraContext(session)) { db.ThrowErrors = true; db.TryDropKeyspace(Keyspace); var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema { Name = Keyspace }, db); db.Keyspace = keyspace; keyspace.TryCreateSelf(); db.ExecuteNonQuery(@" CREATE TABLE users ( id int , name ascii, email ascii, age int, PRIMARY KEY(id, email) );"); ResetUsersFamily(db); } }
public string UpdateColumnFamily(Server server, CfDef definition) { _cachedKeyspaceDescription = null; using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port))) return(session.GetClient().system_update_column_family(definition)); }
public void ResetDatabase() { using (var session = new CassandraSession(ConnectionBuilder)) using (var db = new CassandraContext(session)) { db.ThrowErrors = true; db.TryDropKeyspace(Keyspace); var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema { Name = Keyspace }, db); db.Keyspace = keyspace; keyspace.TryCreateSelf(); keyspace.TryCreateColumnFamily <AsciiType>("Standard"); keyspace.TryCreateColumnFamily <AsciiType, AsciiType>("Super"); keyspace.TryCreateColumnFamily <AsciiType>("StandardAsciiType"); keyspace.TryCreateColumnFamily <BytesType>("StandardBytesType"); keyspace.TryCreateColumnFamily <IntegerType>("StandardIntegerType"); keyspace.TryCreateColumnFamily <LexicalUUIDType>("StandardLexicalUUIDType"); keyspace.TryCreateColumnFamily <LongType>("StandardLongType"); keyspace.TryCreateColumnFamily <TimeUUIDType>("StandardTimeUUIDType"); keyspace.TryCreateColumnFamily <UTF8Type>("StandardUTF8Type"); keyspace.TryCreateColumnFamily <UUIDType>("StandardUUIDType"); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardCompositeType", ColumnNameType = CassandraType.CompositeType(new[] { CassandraType.AsciiType, CassandraType.DoubleType }) }); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardDynamicCompositeType", ColumnNameType = CassandraType.DynamicCompositeType(new Dictionary <char, CassandraType> { { 'a', CassandraType.AsciiType }, { 'd', CassandraType.DoubleType } }) }); db.ExecuteNonQuery(string.Format(@" CREATE TABLE {0} ( id int , name ascii, email ascii, age int, PRIMARY KEY(id) );", toLowerIf(toLower, "Users"))); //db.ExecuteNonQuery(@"CREATE INDEX User_Age ON users (Age);"); db.Keyspace.ClearCachedKeyspaceSchema(); var family = db.GetColumnFamily <AsciiType>("Standard"); var superFamily = db.GetColumnFamily <AsciiType, AsciiType>("Super"); var userFamily = db.GetColumnFamily(toLowerIf(toLower, "Users")); ResetFamily(family); ResetSuperFamily(superFamily); ResetUsersFamily(userFamily); } }
public static string DescribePartitioner(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) { string response = session.GetClient(setKeyspace: false).describe_partitioner(); return(response); } }
public static Dictionary <string, List <string> > DescribeSchemaVersions(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) { var response = session.GetClient(setKeyspace: false).describe_schema_versions(); return(response); } }
public KsDef Describe(Server server) { if (_cachedKeyspaceDescription == null) { _cachedKeyspaceDescription = CassandraSession.GetKeyspace(server, KeyspaceName); } return(_cachedKeyspaceDescription); }
public void ResetDatabase() { using (var session = new CassandraSession(ConnectionBuilder)) using (var db = new CassandraContext(session)) { db.ThrowErrors = true; db.TryDropKeyspace(Keyspace); var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema { Name = Keyspace }, db); db.Keyspace = keyspace; keyspace.TryCreateSelf(); keyspace.TryCreateColumnFamily<AsciiType>("Standard"); keyspace.TryCreateColumnFamily<AsciiType, AsciiType>("Super"); keyspace.TryCreateColumnFamily<AsciiType>("StandardAsciiType"); keyspace.TryCreateColumnFamily<BytesType>("StandardBytesType"); keyspace.TryCreateColumnFamily<IntegerType>("StandardIntegerType"); keyspace.TryCreateColumnFamily<LexicalUUIDType>("StandardLexicalUUIDType"); keyspace.TryCreateColumnFamily<LongType>("StandardLongType"); keyspace.TryCreateColumnFamily<TimeUUIDType>("StandardTimeUUIDType"); keyspace.TryCreateColumnFamily<UTF8Type>("StandardUTF8Type"); keyspace.TryCreateColumnFamily<UUIDType>("StandardUUIDType"); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardCompositeType", ColumnNameType = CassandraType.CompositeType(new[] { CassandraType.AsciiType, CassandraType.DoubleType }) }); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardDynamicCompositeType", ColumnNameType = CassandraType.DynamicCompositeType(new Dictionary<char, CassandraType> { { 'a', CassandraType.AsciiType }, { 'd', CassandraType.DoubleType } }) }); db.ExecuteNonQuery(string.Format(@" CREATE TABLE {0} ( id int , name ascii, email ascii, age int, PRIMARY KEY(id) );", toLowerIf(toLower, "Users"))); //db.ExecuteNonQuery(@"CREATE INDEX User_Age ON users (Age);"); db.Keyspace.ClearCachedKeyspaceSchema(); var family = db.GetColumnFamily<AsciiType>("Standard"); var superFamily = db.GetColumnFamily<AsciiType, AsciiType>("Super"); var userFamily = db.GetColumnFamily(toLowerIf(toLower, "Users")); ResetFamily(family); ResetSuperFamily(superFamily); ResetUsersFamily(userFamily); } }
/// <summary> /// The dispose. /// </summary> /// <param name="disposing"> /// The disposing. /// </param> protected virtual void Dispose(bool disposing) { if (!WasDisposed && !_isOutsideSession && disposing && _session != null) { _session.Dispose(); _session = null; } WasDisposed = true; }
public IEnumerable<CassandraTokenRange> DescribeRing(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port))) { var tokenRanges = session.GetClient(setKeyspace: false).describe_ring(KeyspaceName); foreach (var tokenRange in tokenRanges) yield return new CassandraTokenRange(tokenRange.Start_token, tokenRange.End_token, tokenRange.Endpoints); } }
public void ResetDatabase() { using (var session = new CassandraSession(ConnectionBuilder)) using (var db = new CassandraContext(session)) { db.ThrowErrors = true; try { db.DropKeyspace(Keyspace); } catch (Exception exc) { Console.WriteLine(exc); } var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema { Name = Keyspace }, db); db.Keyspace = keyspace; keyspace.TryCreateSelf(); keyspace.TryCreateColumnFamily<AsciiType>("Standard"); keyspace.TryCreateColumnFamily<AsciiType, AsciiType>("Super"); keyspace.TryCreateColumnFamily<AsciiType>("StandardAsciiType"); keyspace.TryCreateColumnFamily<BytesType>("StandardBytesType"); keyspace.TryCreateColumnFamily<IntegerType>("StandardIntegerType"); keyspace.TryCreateColumnFamily<LexicalUUIDType>("StandardLexicalUUIDType"); keyspace.TryCreateColumnFamily<LongType>("StandardLongType"); keyspace.TryCreateColumnFamily<TimeUUIDType>("StandardTimeUUIDType"); keyspace.TryCreateColumnFamily<UTF8Type>("StandardUTF8Type"); keyspace.TryCreateColumnFamily<UUIDType>("StandardUUIDType"); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardCompositeType", ColumnNameType = CassandraType.CompositeType(new[] { CassandraType.AsciiType, CassandraType.DoubleType }) }); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardDynamicCompositeType", ColumnNameType = CassandraType.DynamicCompositeType(new Dictionary<char, CassandraType> { { 'a', CassandraType.AsciiType }, { 'd', CassandraType.DoubleType } }) }); db.ExecuteNonQuery(@" CREATE COLUMNFAMILY Users ( KEY int PRIMARY KEY, Name ascii, Email ascii, Age int );"); db.ExecuteNonQuery(@"CREATE INDEX User_Age ON Users (Age);"); db.Keyspace.ClearCachedKeyspaceSchema(); var family = db.GetColumnFamily<AsciiType>("Standard"); var superFamily = db.GetColumnFamily<AsciiType, AsciiType>("Super"); var userFamily = db.GetColumnFamily("Users"); ResetFamily(family); ResetSuperFamily(superFamily); ResetUsersFamily(userFamily); } }
public IEnumerable <CassandraTokenRange> DescribeRing(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port))) { var tokenRanges = session.GetClient(setKeyspace: false).describe_ring(KeyspaceName); foreach (var tokenRange in tokenRanges) { yield return(new CassandraTokenRange(tokenRange.Start_token, tokenRange.End_token, tokenRange.Endpoints)); } } }
public static IEnumerable <CassandraKeyspace> DescribeKeyspaces(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) { IEnumerable <KsDef> keyspaces = session.GetClient(setKeyspace: false).describe_keyspaces(); foreach (var keyspace in keyspaces) { yield return(new CassandraKeyspace(keyspace)); } } }
public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write) { if (Current != null) { throw new CassandraException("Cannot create a new session while there is one already active."); } ConnectionProvider = connectionProvider; ReadConsistency = read; WriteConsistency = write; Keyspace = new CassandraKeyspace(connectionProvider.Builder.Keyspace); Current = this; }
public void ResetDatabase() { using (var session = new CassandraSession(ConnectionBuilder)) using (var db = new CassandraContext(session)) { db.ThrowErrors = true; db.TryDropKeyspace(Keyspace); var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema { Name = Keyspace }, db); db.Keyspace = keyspace; keyspace.TryCreateSelf(); } }
public void TryCreateSelf(Server server) { if (CassandraSession.KeyspaceExists(server, KeyspaceName)) { Debug.WriteLine(KeyspaceName + " already exists", "keyspace"); return; } string result = CassandraSession.AddKeyspace(server, new KsDef { Name = KeyspaceName, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Replication_factor = 1, Cf_defs = new List <CfDef>(0) }); Debug.WriteLine(result, "keyspace setup"); }
/// <summary> /// Execute the column family operation against the connection to the server. /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="action"></param> /// <param name="throwOnError"></param> /// <returns></returns> public TResult ExecuteOperation <TResult>(Operation <TResult> action, bool?throwOnError = null) { if (!throwOnError.HasValue) { throwOnError = ThrowErrors; } CassandraSession _localSession = null; if (CassandraSession.Current == null) { _localSession = new CassandraSession(); } try { LastError = null; TResult result; bool success = action.TryExecute(out result); if (!success) { LastError = action.Error; } if (!success && (throwOnError ?? ThrowErrors)) { throw action.Error; } return(result); } finally { if (_localSession != null) { _localSession.Dispose(); } } }
public static string UpdateKeyspace(Server server, KsDef definition) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) return session.GetClient(setKeyspace: false).system_update_keyspace(definition); }
public static KsDef GetKeyspace(Server server, string keyspace) { using (var session = new CassandraSession(new ConnectionBuilder(keyspace, server.Host, server.Port))) return session.GetClient().describe_keyspace(keyspace); }
public static string DropKeyspace(Server server, string keyspace) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) return session.GetClient(setKeyspace: false).system_drop_keyspace(keyspace); }
public static string DescribeVersion(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) { string response = session.GetClient(setKeyspace: false).describe_version(); return response; } }
public static Dictionary<string, List<string>> DescribeSchemaVersions(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) { var response = session.GetClient(setKeyspace: false).describe_schema_versions(); return response; } }
public static IEnumerable<CassandraKeyspace> DescribeKeyspaces(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) { IEnumerable<KsDef> keyspaces = session.GetClient(setKeyspace: false).describe_keyspaces(); foreach (var keyspace in keyspaces) yield return new CassandraKeyspace(keyspace); } }
public void ResetDatabase() { using (var session = new CassandraSession(ConnectionBuilder)) using (var db = new CassandraContext(session)) { db.ThrowErrors = true; db.TryDropKeyspace(Keyspace); var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema { Name = Keyspace }, db); db.Keyspace = keyspace; keyspace.TryCreateSelf(); keyspace.TryCreateColumnFamily <AsciiType>("Standard"); keyspace.TryCreateColumnFamily <AsciiType, AsciiType>("Super"); keyspace.TryCreateColumnFamily <AsciiType>("StandardAsciiType"); keyspace.TryCreateColumnFamily <BytesType>("StandardBytesType"); keyspace.TryCreateColumnFamily <IntegerType>("StandardIntegerType"); keyspace.TryCreateColumnFamily <LexicalUUIDType>("StandardLexicalUUIDType"); keyspace.TryCreateColumnFamily <LongType>("StandardLongType"); keyspace.TryCreateColumnFamily <TimeUUIDType>("StandardTimeUUIDType"); keyspace.TryCreateColumnFamily <UTF8Type>("StandardUTF8Type"); keyspace.TryCreateColumnFamily <UUIDType>("StandardUUIDType"); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema() { FamilyName = "Counters", ColumnNameType = CassandraType.AsciiType, DefaultColumnValueType = CassandraType.CounterColumnType }); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardDecimalType", ColumnNameType = CassandraType.DecimalType }); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardCompositeType", ColumnNameType = CassandraType.CompositeType(new[] { CassandraType.AsciiType, CassandraType.DoubleType }) }); keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema { FamilyName = "StandardDynamicCompositeType", ColumnNameType = CassandraType.DynamicCompositeType(new Dictionary <char, CassandraType> { { 'a', CassandraType.AsciiType }, { 'd', CassandraType.DoubleType } }) }); db.ExecuteNonQuery(@" CREATE COLUMNFAMILY Users ( Id int PRIMARY KEY, Name ascii, Email ascii, Age int );", CqlVersion.Cql); db.ExecuteNonQuery(@"CREATE INDEX User_Age ON Users (Age);", CqlVersion.Cql); db.Keyspace.ClearCachedKeyspaceSchema(); var family = db.GetColumnFamily <AsciiType>("Standard"); var superFamily = db.GetColumnFamily <AsciiType, AsciiType>("Super"); var userFamily = db.GetColumnFamily("Users"); var counterFamily = db.GetColumnFamily("Counters"); ResetFamily(family); ResetSuperFamily(superFamily); ResetUsersFamily(userFamily); ResetCounterColumnFamily(counterFamily); } }
/// <summary> /// /// </summary> /// <param name="session"></param> public CassandraContext(CassandraSession session) : this(session.ConnectionBuilder) { _session = session; _isOutsideSession = true; }
public static IEnumerable<CassandraKeyspace> DescribeKeyspaces(Server server) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) { IEnumerable<KsDef> keyspaces = session.GetClient(setKeyspace: false).describe_keyspaces(); return keyspaces.Select(keyspace => new CassandraKeyspace(keyspace)).ToList(); } }
public _CassandraSetup() { var keyspaceName = "Testing"; var server = new Server("localhost"); if (!CassandraSession.KeyspaceExists(server, keyspaceName)) { CassandraSession.AddKeyspace(server, new KsDef { Name = keyspaceName, Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List <CfDef>() }); } var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Standard")) { keyspace.AddColumnFamily(server, new CfDef { Name = "Standard", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardAsciiType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardAsciiType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardBytesType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardBytesType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "BytesType", Comment = "Used for testing BytesType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardIntegerType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardIntegerType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "IntegerType", Comment = "Used for testing IntegerType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardLexicalUUIDType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLexicalUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LexicalUUIDType", Comment = "Used for testing LexicalUUIDType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardLongType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLongType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LongType", Comment = "Used for testing LongType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardTimeUUIDType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardTimeUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "TimeUUIDType", Comment = "Used for testing TimeUUIDType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardUTF8Type")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardUTF8Type", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "UTF8Type", Comment = "Used for testing UTF8Type family." }); } if (!keyspace.ColumnFamilyExists(server, "Super")) { keyspace.AddColumnFamily(server, new CfDef { Name = "Super", Keyspace = "Testing", Column_type = "Super", Comparator_type = "AsciiType", Subcomparator_type = "AsciiType", Comment = "Used for testing Super family." }); } DB = new CassandraContext(keyspaceName, server); DB.ThrowErrors = true; Family = DB.GetColumnFamily <AsciiType>("Standard"); SuperFamily = DB.GetColumnFamily <AsciiType, AsciiType>("Super"); Family.RemoveAllRows(); SuperFamily.RemoveAllRows(); Family.InsertColumn(TestKey1, "Test1", Math.PI); Family.InsertColumn(TestKey1, "Test2", Math.PI); Family.InsertColumn(TestKey1, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test3", Math.PI); Family.InsertColumn(TestKey2, "Test1", Math.PI); Family.InsertColumn(TestKey2, "Test2", Math.PI); Family.InsertColumn(TestKey2, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test3", Math.PI); }
public string DropColumnFamily(Server server, string columnFamily) { _cachedKeyspaceDescription = null; using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port))) return session.GetClient().system_drop_column_family(columnFamily); }
public static KsDef GetKeyspace(Server server, string keyspace) { using (var session = new CassandraSession(new ConnectionBuilder(keyspace, server.Host, server.Port))) return(session.GetClient().describe_keyspace(keyspace)); }
public static string DropKeyspace(Server server, string keyspace) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) return(session.GetClient(setKeyspace: false).system_drop_keyspace(keyspace)); }
public static string UpdateKeyspace(Server server, KsDef definition) { using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port))) return(session.GetClient(setKeyspace: false).system_update_keyspace(definition)); }