Example #1
0
        public void BigInsertTest(int RowsNo = 5000)
        {
            string keyspaceName = "keyspace" + Guid.NewGuid().ToString("N").ToLower();
            Randomm rndm = new Randomm(DateTime.Now.Millisecond);
            QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE KEYSPACE {0}
             WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }};"
                , keyspaceName));

            string tableName = "table" + Guid.NewGuid().ToString("N").ToLower();
            try
            {
                QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}(
             tweet_id uuid,
             author text,
             body text,
             isok boolean,
             fval float,
             dval double,
             PRIMARY KEY(tweet_id))", tableName));
            }
            catch (AlreadyExistsException)
            {
            }

            StringBuilder longQ = new StringBuilder();
            longQ.AppendLine("BEGIN BATCH ");

            for (int i = 0; i < RowsNo; i++)
            {
                longQ.AppendFormat(@"INSERT INTO {0} (
             tweet_id,
             author,
             isok,
             body,
             fval,
             dval)
            VALUES ({1},'test{2}',{3},'body{2}',{4},{5});", tableName, Guid.NewGuid().ToString(), i, i % 2 == 0 ? "false" : "true", rndm.NextSingle(), rndm.NextDouble());
            }
            longQ.AppendLine("APPLY BATCH;");
            QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting...");
            QueryTools.ExecuteSyncQuery(Session, string.Format(@"SELECT * from {0};", tableName));
            QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"DROP TABLE {0};", tableName));

            QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"DROP KEYSPACE {0};", keyspaceName));
        }
Example #2
0
 internal static object RandomVal(Type tp)
 {
     Randomm rndm = new Randomm();
     if (tp != null)
         return rndm.GetType().GetMethod("Next" + tp.Name).Invoke(rndm, new object[] { });
     else
         return "";
 }
        public void CreateKeyspaceWithPropertiesTest(string strategy_class)
        {
            CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
            try
            {
                Session = CCMCluster.Session;
                Cluster = CCMCluster.Cluster;

                Randomm rndm = new Randomm(DateTime.Now.Millisecond);
                bool durable_writes = rndm.NextBoolean();

                int? replication_factor = null;
                int? data_centers_count = null;
                Dictionary<string, int> datacenters_replication_factors = null;

                if (strategy_class == ReplicationStrategies.SimpleStrategy)
                {
                    replication_factor = rndm.Next(1, 21);
                    Session.CreateKeyspaceIfNotExists(Keyspace,ReplicationStrategies.CreateSimpleStrategyReplicationProperty((int)replication_factor), durable_writes);
                    Session.ChangeKeyspace(Keyspace);
                }
                else
                    if (strategy_class == ReplicationStrategies.NetworkTopologyStrategy)
                    {
                        data_centers_count = rndm.Next(1, 11);
                        datacenters_replication_factors = new Dictionary<string, int>((int)data_centers_count);
                        for (int i = 0; i < data_centers_count; i++)
                            datacenters_replication_factors.Add("dc" + i.ToString(), rndm.Next(1, 21));
                        Session.CreateKeyspaceIfNotExists(Keyspace, ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(datacenters_replication_factors), durable_writes);
                    }

                KeyspaceMetadata ksmd = Cluster.Metadata.GetKeyspace(Keyspace);
                Assert.Equal(strategy_class, ksmd.StrategyClass);
                Assert.Equal(durable_writes, ksmd.DurableWrites);
                if (replication_factor != null)
                    Assert.Equal(replication_factor, ksmd.Replication["replication_factor"]);
                if (datacenters_replication_factors != null)
                    Assert.True(datacenters_replication_factors.SequenceEqual(ksmd.Replication));
            }
            finally
            {
                CCMCluster.Discard();
            }
        }
        public void checkPureMetadata(string TableName = null, string KeyspaceName = null, TableOptions tableOptions = null)
        {
            Dictionary<string, ColumnTypeCode> columns = new Dictionary
                               <string, ColumnTypeCode>()
                {
                    {"q0uuid", ColumnTypeCode.Uuid},
                    {"q1timestamp", ColumnTypeCode.Timestamp},
                    {"q2double", ColumnTypeCode.Double},
                    {"q3int32", ColumnTypeCode.Int},
                    {"q4int64", ColumnTypeCode.Bigint},
                    {"q5float", ColumnTypeCode.Float},
                    {"q6inet", ColumnTypeCode.Inet},
                    {"q7boolean", ColumnTypeCode.Boolean},
                    {"q8inet", ColumnTypeCode.Inet},
                    {"q9blob", ColumnTypeCode.Blob},
            #if NET_40_OR_GREATER
                         {"q10varint", ColumnTypeCode.Varint},
                         {"q11decimal", ColumnTypeCode.Decimal},
            #endif
                    {"q12list", ColumnTypeCode.List},
                    {"q13set", ColumnTypeCode.Set},
                    {"q14map", ColumnTypeCode.Map}
                    //{"q12counter", Metadata.ColumnTypeCode.Counter}, A table that contains a counter can only contain counters
                };

            string tablename = TableName ?? "table" + Guid.NewGuid().ToString("N");
            StringBuilder sb = new StringBuilder(@"CREATE TABLE " + tablename + " (");
            Randomm urndm = new Randomm(DateTimeOffset.Now.Millisecond);

            foreach (var col in columns)
                sb.Append(col.Key + " " + col.Value.ToString() +
                          (((col.Value == ColumnTypeCode.List) ||
                            (col.Value == ColumnTypeCode.Set) ||
                            (col.Value == ColumnTypeCode.Map))
                               ? "<int" + (col.Value == ColumnTypeCode.Map ? ",varchar>" : ">")
                               : "") + ", ");

            sb.Append("PRIMARY KEY(");
            int rowKeys = urndm.Next(1, columns.Count - 3);

            for (int i = 0; i < rowKeys; i++)
                sb.Append(columns.Keys.Where(key => key.StartsWith("q" + i.ToString())).First() +
                          ((i == rowKeys - 1) ? "" : ", "));
            var opt = tableOptions != null ? " WITH " + tableOptions.ToString() : "";
            sb.Append("))" + opt + ";");

            Session.Cluster.WaitForSchemaAgreement(
                QueryTools.ExecuteSyncNonQuery(Session, sb.ToString())

            );

            var table = this.Cluster.Metadata.GetTable(KeyspaceName ?? Keyspace, tablename);
            foreach (var metaCol in table.TableColumns)
            {
                Assert.True(columns.Keys.Contains(metaCol.Name));
                Assert.True(metaCol.TypeCode ==
                            columns.Where(tpc => tpc.Key == metaCol.Name).First().Value);
                Assert.True(metaCol.Table == tablename);
                Assert.True(metaCol.Keyspace == (KeyspaceName ?? Keyspace));
            }
            if (tableOptions != null)
                Assert.True(tableOptions.Equals(table.Options));
        }
Example #5
0
        public void ErrorInjectionInParallelInsertTest()
        {
            string keyspaceName = "keyspace" + Guid.NewGuid().ToString("N").ToLower();
                Session.Cluster.WaitForSchemaAgreement(
                    Session.Execute(
                string.Format(@"CREATE KEYSPACE {0}
             WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }};"
                    , keyspaceName)).QueriedHost);
            Session.ChangeKeyspace(keyspaceName);

            string tableName = "table" + Guid.NewGuid().ToString("N").ToLower();
            try
            {
                Session.Cluster.WaitForSchemaAgreement(
                    Session.Execute(string.Format(@"CREATE TABLE {0}(
             tweet_id uuid,
             author text,
             body text,
             isok boolean,
             PRIMARY KEY(tweet_id))", tableName)).QueriedHost);
            }
            catch (AlreadyExistsException)
            {
            }
            Randomm rndm = new Randomm();
            int RowsNo = 300;
            bool[] ar = new bool[RowsNo];
            List<Thread> threads = new List<Thread>();
            object monit = new object();
            int readyCnt = 0;

            Thread errorInjector = new Thread(() =>
            {
                lock (monit)
                {
                    readyCnt++;
                    Monitor.Wait(monit);
                }
                Thread.Sleep(5);
                Console.Write("#");
                Session.SimulateSingleConnectionDown();

                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(1);
                    Console.Write("#");
                    Session.SimulateSingleConnectionDown();
                }
            });

            Console.WriteLine();
            Console.WriteLine("Preparing...");

            for (int idx = 0; idx < RowsNo; idx++)
            {

                var i = idx;
                threads.Add(new Thread(() =>
                {
                    try
                    {
                        Console.Write("+");
                        lock (monit)
                        {
                            readyCnt++;
                            Monitor.Wait(monit);
                        }

                        Session.Execute(string.Format(@"INSERT INTO {0} (
             tweet_id,
             author,
             isok,
             body)
            VALUES ({1},'test{2}',{3},'body{2}');", tableName, Guid.NewGuid().ToString(), i, i % 2 == 0 ? "false" : "true")
                       );
                        ar[i] = true;
                        Thread.MemoryBarrier();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                        ar[i] = true;
                        Thread.MemoryBarrier();
                    }

                }));
            }

            for (int idx = 0; idx < RowsNo; idx++)
            {
                threads[idx].Start();
            }
            errorInjector.Start();

            lock (monit)
            {
                while (true)
                {
                    if (readyCnt < RowsNo + (1))
                    {
                        Monitor.Exit(monit);
                        Thread.Sleep(100);
                        Monitor.Enter(monit);
                    }
                    else
                    {
                        Monitor.PulseAll(monit);
                        break;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Start!");

            HashSet<int> done = new HashSet<int>();
            while (done.Count < RowsNo)
            {
                for (int i = 0; i < RowsNo; i++)
                {
                    Thread.MemoryBarrier();
                    if (!done.Contains(i) && ar[i])
                    {
                        done.Add(i);
                        Console.Write("-");
                    }
                }
            }

            for (int idx = 0; idx < RowsNo; idx++)
            {
                threads[idx].Join();
            }

            errorInjector.Join();

            Console.WriteLine();
            Console.WriteLine("Inserted... now we are checking the count");

            using (var ret = Session.Execute(string.Format(@"SELECT * from {0} LIMIT {1};", tableName, RowsNo + 100)))
            {
                Assert.Equal(RowsNo, ret.RowsCount);
            }

            try
            {
                Session.Execute(string.Format(@"DROP TABLE {0};", tableName));
            }
            catch { }

            try
            {
                Session.Execute(string.Format(@"DROP KEYSPACE {0};", keyspaceName));
            }
            catch { }
        }
Example #6
0
        public void checkPureMetadata(string TableName = null, string KeyspaceName = null, TableOptions tableOptions = null)
        {
            Dictionary <string, ColumnTypeCode> columns = new Dictionary
                                                          <string, ColumnTypeCode>()
            {
                { "q0uuid", ColumnTypeCode.Uuid },
                { "q1timestamp", ColumnTypeCode.Timestamp },
                { "q2double", ColumnTypeCode.Double },
                { "q3int32", ColumnTypeCode.Int },
                { "q4int64", ColumnTypeCode.Bigint },
                { "q5float", ColumnTypeCode.Float },
                { "q6inet", ColumnTypeCode.Inet },
                { "q7boolean", ColumnTypeCode.Boolean },
                { "q8inet", ColumnTypeCode.Inet },
                { "q9blob", ColumnTypeCode.Blob },
#if NET_40_OR_GREATER
                { "q10varint", ColumnTypeCode.Varint },
                { "q11decimal", ColumnTypeCode.Decimal },
#endif
                { "q12list", ColumnTypeCode.List },
                { "q13set", ColumnTypeCode.Set },
                { "q14map", ColumnTypeCode.Map }
                //{"q12counter", Metadata.ColumnTypeCode.Counter}, A table that contains a counter can only contain counters
            };

            string        tablename = TableName ?? "table" + Guid.NewGuid().ToString("N");
            StringBuilder sb        = new StringBuilder(@"CREATE TABLE " + tablename + " (");
            Randomm       urndm     = new Randomm(DateTimeOffset.Now.Millisecond);

            foreach (var col in columns)
            {
                sb.Append(col.Key + " " + col.Value.ToString() +
                          (((col.Value == ColumnTypeCode.List) ||
                            (col.Value == ColumnTypeCode.Set) ||
                            (col.Value == ColumnTypeCode.Map))
                               ? "<int" + (col.Value == ColumnTypeCode.Map ? ",varchar>" : ">")
                               : "") + ", ");
            }

            sb.Append("PRIMARY KEY(");
            int rowKeys = urndm.Next(1, columns.Count - 3);

            for (int i = 0; i < rowKeys; i++)
            {
                sb.Append(columns.Keys.Where(key => key.StartsWith("q" + i.ToString())).First() +
                          ((i == rowKeys - 1) ? "" : ", "));
            }
            var opt = tableOptions != null ? " WITH " + tableOptions.ToString() : "";

            sb.Append("))" + opt + ";");

            Session.Cluster.WaitForSchemaAgreement(
                QueryTools.ExecuteSyncNonQuery(Session, sb.ToString())

                );

            var table = this.Cluster.Metadata.GetTable(KeyspaceName ?? Keyspace, tablename);

            foreach (var metaCol in table.TableColumns)
            {
                Assert.True(columns.Keys.Contains(metaCol.Name));
                Assert.True(metaCol.TypeCode ==
                            columns.Where(tpc => tpc.Key == metaCol.Name).First().Value);
                Assert.True(metaCol.Table == tablename);
                Assert.True(metaCol.Keyspace == (KeyspaceName ?? Keyspace));
            }
            if (tableOptions != null)
            {
                Assert.True(tableOptions.Equals(table.Options));
            }
        }
Example #7
0
        public void parallelInsertTest()
        {
            string keyspaceName = "keyspace" + Guid.NewGuid().ToString("N").ToLower();

            Session.Execute(
            string.Format(@"CREATE KEYSPACE {0}
             WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }};"
                , keyspaceName));

            Session.ChangeKeyspace(keyspaceName);

            string tableName = "table" + Guid.NewGuid().ToString("N").ToLower();
            try
            {
                Session.Execute(string.Format(@"CREATE TABLE {0}(
             tweet_id uuid,
             author text,
             body text,
             isok boolean,
             PRIMARY KEY(tweet_id))", tableName));
            }
            catch (AlreadyExistsException)
            {
            }
            Randomm rndm = new Randomm();
            int RowsNo = 300;
            IAsyncResult[] ar = new IAsyncResult[RowsNo];
            List<Thread> threads = new List<Thread>();
            object monit = new object();
            int readyCnt = 0;
            Console.WriteLine();
            Console.WriteLine("Preparing...");

            for (int idx = 0; idx < RowsNo; idx++)
            {

                var i = idx;
                threads.Add(new Thread(() =>
                {
                    try
                    {
                        Console.Write("+");
                        lock (monit)
                        {
                            readyCnt++;
                            Monitor.Wait(monit);
                        }

                        ar[i] = Session.BeginExecute(string.Format(@"INSERT INTO {0} (
             tweet_id,
             author,
             isok,
             body)
            VALUES ({1},'test{2}',{3},'body{2}');", tableName, Guid.NewGuid().ToString(), i, i % 2 == 0 ? "false" : "true")
                       , ConsistencyLevel.Default, null, null);
                        Thread.MemoryBarrier();
                    }
                    catch
                    {
                        Console.Write("@");
                    }

                }));
            }

            for (int idx = 0; idx < RowsNo; idx++)
            {
                threads[idx].Start();
            }

            lock (monit)
            {
                while (true)
                {
                    if (readyCnt < RowsNo)
                    {
                        Monitor.Exit(monit);
                        Thread.Sleep(100);
                        Monitor.Enter(monit);
                    }
                    else
                    {
                        Monitor.PulseAll(monit);
                        break;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Start!");

            HashSet<int> done = new HashSet<int>();
            while (done.Count < RowsNo)
            {
                for (int i = 0; i < RowsNo; i++)
                {
                    Thread.MemoryBarrier();
                    if (!done.Contains(i) && ar[i] != null)
                    {
                        if (ar[i].AsyncWaitHandle.WaitOne(10))
                        {
                            try
                            {
                                Session.EndExecute(ar[i]);
                            }
                            catch
                            {
                                Console.Write("!");
                            }
                            done.Add(i);
                            Console.Write("-");
                        }
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Inserted... now we are checking the count");

            using (var ret = Session.Execute(string.Format(@"SELECT * from {0} LIMIT {1};", tableName, RowsNo + 100)))
            {
                Assert.Equal(RowsNo, ret.RowsCount);
            }

            Session.Execute(string.Format(@"DROP TABLE {0};", tableName));

            Session.Execute(string.Format(@"DROP KEYSPACE {0};", keyspaceName));

            for (int idx = 0; idx < RowsNo; idx++)
            {
                threads[idx].Join();
            }
        }
Example #8
0
        public void checkingOrderOfCollection(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null, string pendingMode = "")
        {
            string cassandraDataTypeName    = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed);
            string cassandraKeyDataTypeName = "";

            string openBracket  = CassandraCollectionType == "list" ? "[" : "{";
            string closeBracket = CassandraCollectionType == "list" ? "]" : "}";
            string mapSyntax    = "";

            string randomKeyValue = string.Empty;

            if (TypeOfKeyForMap != null)
            {
                cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap);
                mapSyntax = cassandraKeyDataTypeName + ",";

                if (TypeOfKeyForMap == typeof(DateTimeOffset))
                {
                    randomKeyValue = (string)(Randomm.RandomVal(typeof(DateTimeOffset)).GetType().GetMethod("ToString", new Type[] { typeof(string) }).Invoke(Randomm.RandomVal(typeof(DateTimeOffset)), new object[1] {
                        "yyyy-MM-dd H:mm:sszz00"
                    }) + "' : '");
                }
                else
                {
                    randomKeyValue = Randomm.RandomVal(TypeOfDataToBeInputed) + "' : '";
                }
            }


            string tableName = "table" + Guid.NewGuid().ToString("N");

            try
            {
                Session.Cluster.WaitForSchemaAgreement(
                    QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}(
         tweet_id uuid PRIMARY KEY,
         some_collection {1}<{2}{3}>
         );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName)));
            }
            catch (AlreadyExistsException)
            {
            }
            Guid tweet_id = Guid.NewGuid();

            StringBuilder longQ = new StringBuilder();

            longQ.AppendLine("BEGIN BATCH ");

            int          CollectionElementsNo = 100;
            List <Int32> orderedAsInputed     = new List <Int32>(CollectionElementsNo);

            string inputSide = "some_collection + {1}";

            if (CassandraCollectionType == "list" && pendingMode == "prepending")
            {
                inputSide = "{1} + some_collection";
            }

            for (int i = 0; i < CollectionElementsNo; i++)
            {
                var data = i * (i % 2);
                longQ.AppendFormat(@"UPDATE {0} SET some_collection = " + inputSide + " WHERE tweet_id = {2};"
                                   , tableName, openBracket + randomKeyValue + data + closeBracket, tweet_id.ToString());
                orderedAsInputed.Add(data);
            }

            longQ.AppendLine("APPLY BATCH;");
            QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting...");

            if (CassandraCollectionType == "set")
            {
                orderedAsInputed.Sort();
                orderedAsInputed.RemoveRange(0, orderedAsInputed.LastIndexOf(0));
            }
            else if (CassandraCollectionType == "list" && pendingMode == "prepending")
            {
                orderedAsInputed.Reverse();
            }

            using (var rs = Session.Execute(string.Format("SELECT * FROM {0};", tableName), ConsistencyLevel.Default))
            {
                int ind = 0;
                foreach (var row in rs.GetRows())
                {
                    foreach (var value in row[1] as System.Collections.IEnumerable)
                    {
                        Assert.True(orderedAsInputed[ind] == (int)value);
                        ind++;
                    }
                }
            }

            QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName));
            QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName));
        }
Example #9
0
        public void insertingSingleCollectionPrepared(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null)
        {
            string cassandraDataTypeName    = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed);
            string cassandraKeyDataTypeName = "";
            string mapSyntax = "";

            object valueCollection = null;

            int Cnt = 10;

            if (CassandraCollectionType == "list" || CassandraCollectionType == "set")
            {
                var openType = CassandraCollectionType == "list"? typeof(List <>) : typeof(HashSet <>);
                var listType = openType.MakeGenericType(TypeOfDataToBeInputed);
                valueCollection = Activator.CreateInstance(listType);
                var addM = listType.GetMethod("Add");
                for (int i = 0; i < Cnt; i++)
                {
                    var randomValue = Randomm.RandomVal(TypeOfDataToBeInputed);
                    addM.Invoke(valueCollection, new object[] { randomValue });
                }
            }
            else if (CassandraCollectionType == "map")
            {
                cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap);
                mapSyntax = cassandraKeyDataTypeName + ",";

                var openType = typeof(SortedDictionary <,>);
                var dicType  = openType.MakeGenericType(TypeOfKeyForMap, TypeOfDataToBeInputed);
                valueCollection = Activator.CreateInstance(dicType);
                var addM = dicType.GetMethod("Add");
                for (int i = 0; i < Cnt; i++)
                {
RETRY:
                    try
                    {
                        var randomKey   = Randomm.RandomVal(TypeOfKeyForMap);
                        var randomValue = Randomm.RandomVal(TypeOfDataToBeInputed);
                        addM.Invoke(valueCollection, new object[] { randomKey, randomValue });
                    }
                    catch
                    {
                        goto RETRY;
                    }
                }
            }

            string tableName = "table" + Guid.NewGuid().ToString("N").ToLower();

            try
            {
                Session.Cluster.WaitForSchemaAgreement(
                    QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}(
         tweet_id uuid PRIMARY KEY,
         some_collection {1}<{2}{3}>
         );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName))
                    );
            }
            catch (AlreadyExistsException)
            {
            }

            Guid tweet_id   = Guid.NewGuid();
            var  prepInsert = QueryTools.PrepareQuery(Session, string.Format("INSERT INTO {0}(tweet_id,some_collection) VALUES (?, ?);", tableName));

            Session.Execute(prepInsert.Bind(tweet_id, valueCollection).SetConsistencyLevel(ConsistencyLevel.Quorum));
            QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName));
            QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName));
        }
Example #10
0
        public void insertingSingleCollection(string CassandraCollectionType, Type TypeOfDataToBeInputed, Type TypeOfKeyForMap = null)
        {
            string cassandraDataTypeName    = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfDataToBeInputed);
            string cassandraKeyDataTypeName = "";

            string openBracket  = CassandraCollectionType == "list" ? "[" : "{";
            string closeBracket = CassandraCollectionType == "list" ? "]" : "}";
            string mapSyntax    = "";

            var randomValue = Randomm.RandomVal(TypeOfDataToBeInputed);

            if (TypeOfDataToBeInputed == typeof(string))
            {
                randomValue = "'" + randomValue.ToString().Replace("'", "''") + "'";
            }

            string randomKeyValue = string.Empty;

            if (TypeOfKeyForMap != null)
            {
                cassandraKeyDataTypeName = QueryTools.convertTypeNameToCassandraEquivalent(TypeOfKeyForMap);
                mapSyntax = cassandraKeyDataTypeName + ",";

                if (TypeOfKeyForMap == typeof(DateTimeOffset))
                {
                    randomKeyValue = "'" + (string)(Randomm.RandomVal(typeof(DateTimeOffset)).GetType().GetMethod("ToString", new Type[] { typeof(string) }).Invoke(Randomm.RandomVal(typeof(DateTimeOffset)), new object[1] {
                        "yyyy-MM-dd H:mm:sszz00"
                    }) + "'");
                }
                else if (TypeOfKeyForMap == typeof(string))
                {
                    randomKeyValue = "'" + Randomm.RandomVal(TypeOfDataToBeInputed).ToString().Replace("'", "''") + "'";
                }
                else
                {
                    randomKeyValue = Randomm.RandomVal(TypeOfDataToBeInputed).ToString();
                }
            }

            string tableName = "table" + Guid.NewGuid().ToString("N").ToLower();

            try
            {
                Session.Cluster.WaitForSchemaAgreement(
                    QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}(
         tweet_id uuid PRIMARY KEY,
         some_collection {1}<{2}{3}>
         );", tableName, CassandraCollectionType, mapSyntax, cassandraDataTypeName))
                    );
            }
            catch (AlreadyExistsException)
            {
            }

            Guid tweet_id = Guid.NewGuid();


            QueryTools.ExecuteSyncNonQuery(Session, string.Format("INSERT INTO {0}(tweet_id,some_collection) VALUES ({1}, {2});", tableName, tweet_id.ToString(), openBracket + randomKeyValue + (string.IsNullOrEmpty(randomKeyValue) ? "" : " : ") + randomValue + closeBracket));

            StringBuilder longQ = new StringBuilder();

            longQ.AppendLine("BEGIN BATCH ");

            int CollectionElementsNo = 100;
            var rval = Randomm.RandomVal(TypeOfDataToBeInputed);

            for (int i = 0; i < CollectionElementsNo; i++)
            {
                var val = rval;
                if (TypeOfDataToBeInputed == typeof(string))
                {
                    val = "'" + val.ToString().Replace("'", "''") + "'";
                }

                longQ.AppendFormat(@"UPDATE {0} SET some_collection = some_collection + {1} WHERE tweet_id = {2};"
                                   , tableName, openBracket + randomKeyValue + (string.IsNullOrEmpty(randomKeyValue) ? "" : " : ") + val + closeBracket, tweet_id.ToString());
            }
            longQ.AppendLine("APPLY BATCH;");
            QueryTools.ExecuteSyncNonQuery(Session, longQ.ToString(), "Inserting...");

            QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName));
            QueryTools.ExecuteSyncNonQuery(Session, string.Format("DROP TABLE {0};", tableName));
        }
Example #11
0
        public void massivePreparedStatementTest()
        {
            string tableName = "table" + Guid.NewGuid().ToString("N");

            try
            {
                Session.WaitForSchemaAgreement(
                    QueryTools.ExecuteSyncNonQuery(Session, string.Format(@"CREATE TABLE {0}(
         tweet_id uuid PRIMARY KEY,
         numb1 double,
         numb2 int
         );", tableName))
                    );
            }
            catch (AlreadyExistsException)
            {
            }
            int numberOfPrepares = 100;

            List <object[]>          values   = new List <object[]>(numberOfPrepares);
            List <PreparedStatement> prepares = new List <PreparedStatement>();

            Parallel.For(0, numberOfPrepares, i =>
            {
                var prep = QueryTools.PrepareQuery(Session, string.Format("INSERT INTO {0}(tweet_id, numb1, numb2) VALUES ({1}, ?, ?);", tableName, Guid.NewGuid()));

                lock (prepares)
                    prepares.Add(prep);
            });

            Parallel.ForEach(prepares, prep =>
            {
                QueryTools.ExecutePreparedQuery(this.Session, prep, new object[] { (double)Randomm.RandomVal(typeof(double)), (int)Randomm.RandomVal(typeof(int)) });
            });

            QueryTools.ExecuteSyncQuery(Session, string.Format("SELECT * FROM {0};", tableName));
        }
Example #12
0
        public void parallelInsertTest()
        {
            string keyspaceName = "keyspace" + Guid.NewGuid().ToString("N").ToLower();

            Session.Cluster.WaitForSchemaAgreement(
                Session.Execute(
                    string.Format(@"CREATE KEYSPACE {0} 
         WITH replication = {{ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }};"
                                  , keyspaceName)).QueriedHost);

            Session.ChangeKeyspace(keyspaceName);

            string tableName = "table" + Guid.NewGuid().ToString("N").ToLower();

            try
            {
                Session.Cluster.WaitForSchemaAgreement(
                    Session.Execute(string.Format(@"CREATE TABLE {0}(
         tweet_id uuid,
         author text,
         body text,
         isok boolean,
         PRIMARY KEY(tweet_id))", tableName)).QueriedHost);
            }
            catch (AlreadyExistsException)
            {
            }
            Randomm rndm   = new Randomm();
            int     RowsNo = 300;

            IAsyncResult[] ar       = new IAsyncResult[RowsNo];
            List <Thread>  threads  = new List <Thread>();
            object         monit    = new object();
            int            readyCnt = 0;

            Console.WriteLine();
            Console.WriteLine("Preparing...");

            for (int idx = 0; idx < RowsNo; idx++)
            {
                var i = idx;
                threads.Add(new Thread(() =>
                {
                    try
                    {
                        Console.Write("+");
                        lock (monit)
                        {
                            readyCnt++;
                            Monitor.Wait(monit);
                        }

                        ar[i] = Session.BeginExecute(string.Format(@"INSERT INTO {0} (
         tweet_id,
         author,
         isok,
         body)
VALUES ({1},'test{2}',{3},'body{2}');", tableName, Guid.NewGuid().ToString(), i, i % 2 == 0 ? "false" : "true")
                                                     , ConsistencyLevel.Default, null, null);
                        Thread.MemoryBarrier();
                    }
                    catch
                    {
                        Console.Write("@");
                    }
                }));
            }

            for (int idx = 0; idx < RowsNo; idx++)
            {
                threads[idx].Start();
            }

            lock (monit)
            {
                while (true)
                {
                    if (readyCnt < RowsNo)
                    {
                        Monitor.Exit(monit);
                        Thread.Sleep(100);
                        Monitor.Enter(monit);
                    }
                    else
                    {
                        Monitor.PulseAll(monit);
                        break;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Start!");

            HashSet <int> done = new HashSet <int>();

            while (done.Count < RowsNo)
            {
                for (int i = 0; i < RowsNo; i++)
                {
                    Thread.MemoryBarrier();
                    if (!done.Contains(i) && ar[i] != null)
                    {
                        if (ar[i].AsyncWaitHandle.WaitOne(10))
                        {
                            try
                            {
                                Session.EndExecute(ar[i]);
                            }
                            catch
                            {
                                Console.Write("!");
                            }
                            done.Add(i);
                            Console.Write("-");
                        }
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Inserted... now we are checking the count");

            using (var ret = Session.Execute(string.Format(@"SELECT * from {0} LIMIT {1};", tableName, RowsNo + 100)))
            {
                Assert.Equal(RowsNo, ret.RowsCount);
            }

            Session.Execute(string.Format(@"DROP TABLE {0};", tableName));

            Session.Execute(string.Format(@"DROP KEYSPACE {0};", keyspaceName));

            for (int idx = 0; idx < RowsNo; idx++)
            {
                threads[idx].Join();
            }
        }