public void DeleteUser(TestUser user) { table.Delete(user.info.id); tableByNick.Delete(user.info.Nick + "|" + user.info.id.ToString()); tableByBirth.Delete(user.info.DateOfBirth + "|" + user.info.id.ToString()); tableByLastLogin.Delete(user.info.LastLogin + "|" + user.info.id.ToString()); }
public void SetUser(TestUser user) { bool update = false; // read old data for indexes TestUser oldVersion = GetUser(user.info.id); if (oldVersion != null) { update = true; } // last login will be last set :) user.info.LastLogin = System.DateTime.Now.ToShortDateString(); // set user row table.Set(System.Text.Encoding.UTF8.GetBytes(user.info.id), Utils.JsonSerialize(user.info)); if (update) { // set nick index if (oldVersion.info.Nick == user.info.Nick) { // just set tableByNick.Set(System.Text.Encoding.UTF8.GetBytes(user.info.Nick + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); } else { // delete old tableByNick.Delete(oldVersion.info.Nick + "|" + user.info.id.ToString()); // set new tableByNick.Set(System.Text.Encoding.UTF8.GetBytes(user.info.Nick + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); } // set dateofbirth index if (oldVersion.info.DateOfBirth == user.info.DateOfBirth) { // just set tableByBirth.Set(System.Text.Encoding.UTF8.GetBytes(user.info.DateOfBirth + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); } else { // delete old tableByBirth.Delete(oldVersion.info.DateOfBirth + "|" + user.info.id.ToString()); // set new tableByBirth.Set(System.Text.Encoding.UTF8.GetBytes(user.info.DateOfBirth + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); } // set lastlogin index if (oldVersion.info.LastLogin == user.info.LastLogin) { // just set tableByLastLogin.Set(System.Text.Encoding.UTF8.GetBytes(user.info.LastLogin + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); } else { // delete old tableByLastLogin.Delete(oldVersion.info.LastLogin + "|" + user.info.id.ToString()); // set new tableByLastLogin.Set(System.Text.Encoding.UTF8.GetBytes(user.info.LastLogin + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); } } else { tableByNick.Set(System.Text.Encoding.UTF8.GetBytes(user.info.Nick + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); tableByBirth.Set(System.Text.Encoding.UTF8.GetBytes(user.info.DateOfBirth + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); tableByLastLogin.Set(System.Text.Encoding.UTF8.GetBytes(user.info.LastLogin + "|" + user.info.id.ToString()), Utils.JsonSerialize(user.info)); } }
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); } } } }