private void OpenDB() { try { db = clients[client_index].GetDatabase(dbname); } catch (SDBPException) { db = clients[client_index].CreateDatabase(dbname); } db = Utils.GetOrCreateEmptyDatabase(clients[client_index], dbname); try { indices = db.GetTable("indices"); } catch (SDBPException) { indices = db.CreateTable("indices"); } userIDs = indices.GetSequence("userIDs"); try { table = db.GetTable(tablename); } catch (SDBPException) { table = db.CreateTable(tablename); } try { tableByNick = db.GetTable(tablename + "ByNick"); } catch (SDBPException) { tableByNick = db.CreateTable(tablename + "ByNick"); } try { tableByBirth = db.GetTable(tablename + "ByBirth"); } catch (SDBPException) { tableByBirth = db.CreateTable(tablename + "ByBirth"); } try { tableByLastLogin = db.GetTable(tablename + "ByLastLogin"); } catch (SDBPException) { tableByLastLogin = db.CreateTable(tablename + "ByLastLogin"); } }
public static bool DBCompare(Database DB1, Database DB2) { bool res = true; Table cmp_tbl; byte[] cmp_val; // DB1 -> DB2 // call the function vice versa too, to be sure in result List <Table> tables = DB1.GetTables(); foreach (Table tbl in tables) { if (null == (cmp_tbl = DB2.GetTable(tbl.Name))) { Console.WriteLine(tbl.Name + " table is not in database " + DB2.Name); res = false; } else { Console.WriteLine("Comparing table " + tbl.Name); // check for table data foreach (KeyValuePair <byte[], byte[]> kv in tbl.GetKeyValueIterator(new ByteRangeParams())) { cmp_val = cmp_tbl.Get(kv.Key); if (null == cmp_val) { Console.WriteLine("Key \"" + kv.Key.ToString() + "\" is not in table " + tbl.Name); res = false; continue; } if (!Utils.ByteArraysEqual(kv.Value, cmp_val)) { Console.WriteLine("Values for key \"" + kv.Key.ToString() + "\" doesn't match:"); Console.WriteLine(kv.Value.ToString()); Console.WriteLine(cmp_val.ToString()); res = false; continue; } } } } return(res); }
public static Table GetOrCreateTableAndDatabase(Client client, string dbName, string tableName) { Database db = null; try { db = client.GetDatabase(dbName); } catch (SDBPException e) { if (e.Status == Status.SDBP_BADSCHEMA) { db = client.CreateDatabase(dbName); } } if (db == null) { return(null); } Table table = null; try { table = db.GetTable(tableName); } catch (SDBPException e) { if (e.Status == Status.SDBP_BADSCHEMA) { table = db.CreateTable(tableName); } } if (table == null) { table = db.CreateTable(tableName); } return(table); }
public static void ThreadFunc() { //Client.SetTrace(true); //Client.SetLogFile("d:/out.txt"); //string[] controllers = { "127.0.0.1:7080" }; string[] controllers = { "192.168.137.110:7080", "192.168.137.111:7080", "192.168.137.112:7080" }; Client client = new Client(controllers); Quorum quorum = client.GetQuorum("test"); Database db = client.GetDatabase("test"); Table test = db.GetTable("test"); Table indices = db.GetTable("indices"); Sequence IDs = indices.GetSequence("IDs"); //System.Console.WriteLine("Thread.CurrentThread.ManagedThreadId = {0}", Thread.CurrentThread.ManagedThreadId); System.Random random = new System.Random(Thread.CurrentThread.ManagedThreadId); var value = ""; while (value.Length < 10 * 1000) { value += "" + random.Next(); } //using (client.Transaction(quorum, "foo" + random.Next(100))) //{ // System.Console.WriteLine("Transaction started."); // for (var j = 0; j < 10*1000; j++) // table.Set("" + j, value); // client.CommitTransaction(); // System.Console.WriteLine("Transaction finished."); //} //return; ulong ID; //for (var i = 0; i < 1000; i++) while (true) { if (random.Next(3) == 0) { try { System.Console.WriteLine("Batch started."); for (var j = 0; j < 10; j++) { if (random.Next(2) == 0) { test.Set("" + random.Next(1000 * 1000), value); } else { test.Delete("" + random.Next(1000 * 1000)); } } //for (var j = 0; j < 10 * 1000; j++) // ID = IDs.GetNext; client.Submit(); System.Console.WriteLine("Batch finished."); } catch (SDBPException) { System.Console.WriteLine("Batch failed."); } continue; } //if (random.Next(2) == 0) //{ // try // { // System.Console.WriteLine("List started."); // var count = 0; // foreach (KeyValuePair<string, string> kv in test.GetKeyValueIterator(new StringRangeParams().Prefix("1"))) // { // count++; // //System.Console.WriteLine(kv.Key + " => " + kv.Value); // if (count == 10) // break; // } // System.Console.WriteLine("List finished."); // } // catch (SDBPException) // { // System.Console.WriteLine("List failed."); // // why does this happen // } // continue; //} while (true) { try { using (client.Transaction(quorum, "foo" + random.Next(100))) { System.Console.WriteLine("Transaction started."); for (var j = 0; j < 10; j++) { if (random.Next(2) == 0) { test.Set("" + random.Next(1000 * 1000), value); } else { test.Delete("" + random.Next(1000 * 1000)); } } //for (var j = 0; j < 1000; j++) // ID = IDs.GetNext; if (random.Next(2) == 0) { client.CommitTransaction(); } else { client.RollbackTransaction(); } System.Console.WriteLine("Transaction finished."); break; } } catch (SDBPException) { //System.Console.WriteLine("Exception."); System.Threading.Thread.Sleep(1); } } } }