public void WriteRecords(BTreeDatabase db) { /* * Write ten records into the database. The records * from 1st to 5th and 8th to 10th are unique in the * database. The data in the 6th and 7th records * are the same. */ for (int i = 0; i < 10; i++) { if (i == 5 || i == 6) { db.Put(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry( BitConverter.GetBytes((int)10))); } else { db.Put(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i))); } } }
public void WriteRecordsInTxn(BTreeDatabase db, DatabaseEnvironment env) { Transaction txn = env.BeginTransaction(); /* * Write ten records into the database. The records * from 1st to 5th and 8th to 10th are unique in the * database. The data in the 6th and 7th records * are the same. */ for (int i = 0; i < 10; i++) { if (i == 5 || i == 6) { db.Put(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry( BitConverter.GetBytes((int)10)), txn); } else { db.Put(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i)), txn); } } txn.Commit(); }
protected void AddToDb(string keyval, byte[] dataval) { var key = new DatabaseEntry(Encoding.UTF8.GetBytes(keyval)); var data = new DatabaseEntry(dataval); db.Put(key, data); }
public void MutexThread1() { TestMutex.Lock(); for (int i = 0; i < 100; i++) { TestDB.Put(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry(new byte[102400])); } TestMutex.Unlock(); }
public void GetSecondaryCursurWithTxn(string home, string name, bool ifCfg) { string dbFileName = name + ".db"; SecondaryCursor cursor; // Open env. DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseTxns = true; envConfig.UseMPool = true; DatabaseEnvironment env = DatabaseEnvironment.Open(home, envConfig); // Open primary/secondary database. Transaction txn = env.BeginTransaction(); BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; BTreeDatabase db = BTreeDatabase.Open(dbFileName, dbConfig, txn); SecondaryBTreeDatabaseConfig secDBConfig = new SecondaryBTreeDatabaseConfig(db, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secDBConfig.Env = env; SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open(dbFileName, secDBConfig, txn); for (int i = 0; i < 10; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes((int)i)), txn); } // Create secondary cursor. if (ifCfg == false) { secDB.SecondaryCursor(txn); } else if (ifCfg == true) { CursorConfig cursorConfig = new CursorConfig(); cursorConfig.WriteCursor = false; cursor = secDB.SecondaryCursor(cursorConfig, txn); cursor.Close(); } secDB.Close(); db.Close(); txn.Commit(); env.Close(); }
/// <summary> /// Default data processing entry point for <see cref="DataWriter"/>. /// </summary> /// <param name="timestamp">Timestamp of <paramref name="dataBlock"/>.</param> /// <param name="dataBlock">Points values read at current timestamp.</param> public void Write(DateTime timestamp, DataPoint[] dataBlock) { if (m_settings.WriteToOpenHistorian) // Write to openHistorian { foreach (DataPoint point in dataBlock) { m_historianKey.Timestamp = point.Timestamp; m_historianKey.PointID = point.PointID; m_historianValue.Value1 = point.Value; m_historianValue.Value3 = point.Flags; m_historianArchive.Write(m_historianKey, m_historianValue); } } if (m_settings.WriteToBerkeleyDB) // Write to Berkeley DB { //// Create a new pointList each time *slow //KeyValuePair<DatabaseEntry, DatabaseEntry>[] pointList = new KeyValuePair<DatabaseEntry, DatabaseEntry>[dataBlock.Length]; //Parallel.For(0, pointList.Length, (i) => pointList[i] = new KeyValuePair<DatabaseEntry, DatabaseEntry>( // new DatabaseEntry(BitConverter.GetBytes(timestamp.Ticks).Concat(BitConverter.GetBytes(dataBlock[i].PointID)).ToArray()), // new DatabaseEntry(BitConverter.GetBytes(dataBlock[i].Value)))); //using (MultipleKeyDatabaseEntry buffer = new MultipleKeyDatabaseEntry(pointList, false)) //{ // m_berkeleyDb.Put(buffer); //} //Parallel.For(0, pointList.Length, (i) => //{ // pointList[i].Key.Dispose(); // pointList[i].Value.Dispose(); //}); //// Single writes using the same key and value each time *medium speed //foreach (DataPoint point in dataBlock) //{ // m_berkeleyDbKey.Data = BitConverter.GetBytes(timestamp.Ticks).Concat(BitConverter.GetBytes(point.PointID)).ToArray(); // m_berkeleyDbValue.Data = BitConverter.GetBytes(point.Value).ToArray(); // m_berkeleyDb.Put(m_berkeleyDbKey, m_berkeleyDbValue); //} // Bulk writes using the same pointList *fastest method found so far* Parallel.For(0, dataBlock.Length, (i) => { m_berkeleyDbPointList[i].Key.Data = BitConverter.GetBytes(timestamp.Ticks).Concat(BitConverter.GetBytes(dataBlock[i].PointID)).ToArray(); m_berkeleyDbPointList[i].Value.Data = BitConverter.GetBytes(dataBlock[i].Value); }); using (MultipleKeyDatabaseEntry buffer = new MultipleKeyDatabaseEntry(m_berkeleyDbPointList.Take(dataBlock.Length), false)) { m_berkeleyDb.Put(buffer); } } }
public void WriteOneIntoBtreeDBWithTxn(BTreeDatabase db, Transaction txn) { DatabaseEntry key, data; key = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("key")); data = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("data")); db.Put(key, data, txn); }
public void TestCompare() { testName = "TestCompare"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; // Open a primary btree database. BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Creation = CreatePolicy.ALWAYS; BTreeDatabase btreeDB = BTreeDatabase.Open( dbFileName, btreeDBConfig); // Open a secondary btree database. SecondaryBTreeDatabaseConfig secBtreeDBConfig = new SecondaryBTreeDatabaseConfig(null, null); secBtreeDBConfig.Primary = btreeDB; secBtreeDBConfig.Compare = new EntryComparisonDelegate( SecondaryEntryComparison); secBtreeDBConfig.KeyGen = new SecondaryKeyGenDelegate(SecondaryKeyGen); SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open( dbFileName, secBtreeDBConfig); /* * Get the compare function set in the configuration * and run it in a comparison to see if it is alright. */ EntryComparisonDelegate cmp = secDB.Compare; DatabaseEntry dbt1, dbt2; dbt1 = new DatabaseEntry( BitConverter.GetBytes((int)257)); dbt2 = new DatabaseEntry( BitConverter.GetBytes((int)255)); Assert.Less(0, cmp(dbt1, dbt2)); for (int i = 0; i < 1000; i++) { btreeDB.Put(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry( BitConverter.GetBytes(i))); } secDB.Close(); btreeDB.Close(); }
public void Read() { Transaction txn = testLockStatsEnv.BeginTransaction(); for (int i = 0; i < 200; i++) { testLockStatsDb.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes( Configuration.RandomString(i))), txn); testLockStatsDb.Sync(); } txn.Commit(); }
/// <summary> /// Add records directly to the database. /// </summary> /// <param name="keyval">The key.</param> /// <param name="dataval">The data value.</param> protected void AddToDb(string keyval, byte[] dataval) { try { var key = new DatabaseEntry(Encoding.UTF8.GetBytes(keyval)); var data = new DatabaseEntry(dataval); db.Put(key, data); } catch (Exception ex) { Sync(); logger.LogError($"{DateTime.Now}\t{ex.GetType()} in {db.DatabaseName} AddToDb: {ex.Message}"); } }
public override void Write(int flowID, IEnumerable <KeyValuePair <long, Tick> > flow) { lock (SyncRoot) { foreach (var item in flow) { DatabaseEntry key = new DatabaseEntry(BitConverter.GetBytes(item.Key)); DatabaseEntry value = new DatabaseEntry(FromTick(item.Value)); database.Put(key, value); } database.Sync(); } }
public void TestJoin() { testName = "TestJoin"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; string secFileName1 = testHome + "/" + "sec_" + testName + "1.db"; string secFileName2 = testHome + "/" + "sec_" + testName + "2.db"; // Open a primary database. BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); dbCfg.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase db = BTreeDatabase.Open(dbFileName, dbCfg); for (int i = 0; i < 10; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i))); } /* * Open two databases, their secondary databases and * secondary cursors. */ SecondaryBTreeDatabase secDB1, secDB2; SecondaryCursor[] cursors = new SecondaryCursor[2]; GetSecCursor(db, secFileName1, new SecondaryKeyGenDelegate(KeyGenOnBigByte), out secDB1, out cursors[0], false, null); GetSecCursor(db, secFileName2, new SecondaryKeyGenDelegate(KeyGenOnLittleByte), out secDB2, out cursors[1], true, null); // Get join cursor. JoinCursor joinCursor = db.Join(cursors, true); Assert.IsNotNull(joinCursor.GetEnumerator().Current); // Close all. joinCursor.Dispose(); cursors[0].Close(); cursors[1].Close(); secDB1.Close(); secDB2.Close(); db.Close(); }
public void TestSecondaryCursor() { testName = "TestSecondaryCursor"; testHome = testFixtureHome + "/" + testName; string dbFileName = testHome + "/" + testName + ".db"; Configuration.ClearDir(testHome); // Open primary database. BTreeDatabaseConfig primaryDBConfig = new BTreeDatabaseConfig(); primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase primaryDB = BTreeDatabase.Open(dbFileName, primaryDBConfig); // Open secondary database. SecondaryBTreeDatabaseConfig secDBConfig = new SecondaryBTreeDatabaseConfig(primaryDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open(dbFileName, secDBConfig); primaryDB.Put(new DatabaseEntry( BitConverter.GetBytes((int)1)), new DatabaseEntry(BitConverter.GetBytes((int)11))); SecondaryCursor cursor = secDB.SecondaryCursor(); cursor.Move(new DatabaseEntry( BitConverter.GetBytes((int)11)), true); Assert.AreEqual(BitConverter.GetBytes((int)11), cursor.Current.Key.Data); // Close the cursor. cursor.Close(); // Close secondary database. secDB.Close(); // Close primary database. primaryDB.Close(); }
public void GenerateDeadlock() { Transaction txn = testLockStatsEnv.BeginTransaction(); try { testLockStatsDb.Put( new DatabaseEntry(BitConverter.GetBytes(100)), new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes( Configuration.RandomString(200))), txn); DeadlockDidPut = 1; testLockStatsDb.Get(new DatabaseEntry( BitConverter.GetBytes(10)), txn); } catch (DeadlockException) { } // Abort unconditionally - we don't care about the transaction txn.Abort(); }
public void TestInvalidToken() { DatabaseEntry key, data; DatabaseEnvironment master = SetUpEnv("./master", 100, 8000, true); BTreeDatabase db = Open(master, true); Transaction txn = master.BeginTransaction(); key = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("Key")); data = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("Data")); db.Put(key, data, txn); txn.Commit(); byte[] token = txn.CommitToken; token[19] += 123; Assert.Throws <DatabaseException>(delegate { master.IsTransactionApplied(token, 1000); }); db.Close(); master.Close(); }
public void GetSecCursor(BTreeDatabase db, string secFileName, SecondaryKeyGenDelegate keyGen, out SecondaryBTreeDatabase secDB, out SecondaryCursor cursor, bool ifCfg, DatabaseEntry data) { // Open secondary database. SecondaryBTreeDatabaseConfig secCfg = new SecondaryBTreeDatabaseConfig(db, keyGen); secCfg.Creation = CreatePolicy.IF_NEEDED; secCfg.Duplicates = DuplicatesPolicy.SORTED; secDB = SecondaryBTreeDatabase.Open(secFileName, secCfg); int[] intArray = new int[4]; intArray[0] = 0; intArray[1] = 1; intArray[2] = 2049; intArray[3] = 65537; for (int i = 0; i < 4; i++) { DatabaseEntry record = new DatabaseEntry( BitConverter.GetBytes(intArray[i])); db.Put(record, record); } // Get secondary cursor on the secondary database. if (ifCfg == false) { cursor = secDB.SecondaryCursor(); } else { cursor = secDB.SecondaryCursor(new CursorConfig()); } // Position the cursor. if (data != null) { Assert.IsTrue(cursor.Move(data, true)); } }
public void TestNestedTXN() { DatabaseEntry key, data; DatabaseEnvironment master = SetUpEnv("./master", 100, 8000, true); BTreeDatabase db = Open(master, true); TransactionConfig txnconfig = new TransactionConfig(); Transaction txn = master.BeginTransaction(); Transaction txn1 = master.BeginTransaction(txnconfig, txn); key = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("Key")); data = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("Data")); db.Put(key, data, txn1); txn1.Commit(); txn.Commit(); byte[] token; Assert.Throws <ArgumentException>(delegate { token = txn1.CommitToken; }); master.Close(); }
public void TestKeyGen() { testName = "TestKeyGen"; testHome = testFixtureHome + "/" + testName; string dbFileName = testHome + "/" + testName + ".db"; Configuration.ClearDir(testHome); // Open primary database. BTreeDatabaseConfig primaryDBConfig = new BTreeDatabaseConfig(); primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase primaryDB = BTreeDatabase.Open(dbFileName, primaryDBConfig); // Open secondary database. SecondaryBTreeDatabaseConfig secDBConfig = new SecondaryBTreeDatabaseConfig(primaryDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open(dbFileName, secDBConfig); primaryDB.Put(new DatabaseEntry( BitConverter.GetBytes((int)1)), new DatabaseEntry(BitConverter.GetBytes((int)11))); KeyValuePair <DatabaseEntry, DatabaseEntry> pair; pair = secDB.Get(new DatabaseEntry( BitConverter.GetBytes((int)11))); Assert.IsNotNull(pair.Value); // Close secondary database. secDB.Close(); // Close primary database. primaryDB.Close(); }
private void btn_WriteStringToBDB_Click(object sender, RoutedEventArgs e) { BTreeDatabaseConfig bTreeDatabaseConfig = new BTreeDatabaseConfig(); //文件不存在则创建 bTreeDatabaseConfig.Creation = CreatePolicy.IF_NEEDED; //页大小 bTreeDatabaseConfig.PageSize = 512; //缓存大小 bTreeDatabaseConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1); BTreeDatabase bTreeDatabase = BTreeDatabase.Open("demo.db", bTreeDatabaseConfig); string guid = this.tbox_Key.Text; string url = this.tbox_Value.Text; DatabaseEntry key = new DatabaseEntry(); key.Data = Encoding.ASCII.GetBytes(guid); DatabaseEntry value = new DatabaseEntry(); value.Data = Encoding.ASCII.GetBytes(url); bTreeDatabase.Put(key, value); EMessageBox.Show("写入成功"); bTreeDatabase.Close(); }
public void TestLoggingSystemStats() { testName = "TestLoggingSystemStats"; SetUpTest(true); string logDir = "./"; Directory.CreateDirectory(testHome + "/" + logDir); DatabaseEnvironmentConfig cfg = new DatabaseEnvironmentConfig(); cfg.Create = true; cfg.UseTxns = true; cfg.AutoCommit = true; cfg.UseLocking = true; cfg.UseMPool = true; cfg.UseLogging = true; cfg.MPoolSystemCfg = new MPoolConfig(); cfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 1048576, 1); cfg.LogSystemCfg = new LogConfig(); cfg.LogSystemCfg.AutoRemove = false; cfg.LogSystemCfg.BufferSize = 10240; cfg.LogSystemCfg.Dir = logDir; cfg.LogSystemCfg.FileMode = 755; cfg.LogSystemCfg.ForceSync = true; cfg.LogSystemCfg.InMemory = false; cfg.LogSystemCfg.LogBlobContent = false; cfg.LogSystemCfg.MaxFileSize = 1048576; cfg.LogSystemCfg.NoBuffer = false; cfg.LogSystemCfg.RegionSize = 204800; cfg.LogSystemCfg.ZeroOnCreate = true; DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); LogStats stats = env.LoggingSystemStats(); env.PrintLoggingSystemStats(); Assert.AreEqual(10240, stats.BufferSize); Assert.AreEqual(1, stats.CurrentFile); Assert.AreNotEqual(0, stats.CurrentOffset); Assert.AreEqual(0, stats.FileId); Assert.AreEqual(1048576, stats.FileSize); Assert.AreEqual(0, stats.InitFileId); Assert.AreNotEqual(0, stats.MagicNumber); Assert.AreEqual(0, stats.MaxFileId); Assert.AreNotEqual(0, stats.PermissionsMode); Assert.AreEqual(1, stats.Records); Assert.AreNotEqual(0, stats.RegionLockNoWait); Assert.LessOrEqual(204800, stats.RegionSize); Assert.AreNotEqual(0, stats.Version); Transaction openTxn = env.BeginTransaction(); BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; BTreeDatabase db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn); openTxn.Commit(); Transaction writeTxn = env.BeginTransaction(); byte[] byteArr = new byte[1024]; for (int i = 0; i < 1000; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(byteArr), writeTxn); } writeTxn.Commit(); stats = env.LoggingSystemStats(); Assert.AreNotEqual(0, stats.Bytes); Assert.AreNotEqual(0, stats.BytesSinceCheckpoint); Assert.AreNotEqual(0, stats.DiskFileNumber); Assert.AreNotEqual(0, stats.DiskOffset); Assert.AreNotEqual(0, stats.MaxCommitsPerFlush); Assert.AreNotEqual(0, stats.MBytes); Assert.AreNotEqual(0, stats.MBytesSinceCheckpoint); Assert.AreNotEqual(0, stats.MinCommitsPerFlush); Assert.AreNotEqual(0, stats.OverflowWrites); Assert.AreNotEqual(0, stats.Syncs); Assert.AreNotEqual(0, stats.Writes); Assert.AreEqual(0, stats.Reads); Assert.AreEqual(0, stats.RegionLockWait); stats = env.LoggingSystemStats(true); stats = env.LoggingSystemStats(); Assert.AreEqual(0, stats.Bytes); Assert.AreEqual(0, stats.BytesSinceCheckpoint); Assert.AreEqual(0, stats.MaxCommitsPerFlush); Assert.AreEqual(0, stats.MBytes); Assert.AreEqual(0, stats.MBytesSinceCheckpoint); Assert.AreEqual(0, stats.MinCommitsPerFlush); Assert.AreEqual(0, stats.OverflowWrites); Assert.AreEqual(0, stats.Syncs); Assert.AreEqual(0, stats.Writes); Assert.AreEqual(0, stats.Reads); env.PrintLoggingSystemStats(true, true); db.Close(); env.Close(); }
public void MoveWithRMW(string home, string name) { paramEnv = null; paramDB = null; // Open the environment. DatabaseEnvironmentConfig envCfg = new DatabaseEnvironmentConfig(); envCfg.Create = true; envCfg.FreeThreaded = true; envCfg.UseLocking = true; envCfg.UseLogging = true; envCfg.UseMPool = true; envCfg.UseTxns = true; paramEnv = DatabaseEnvironment.Open(home, envCfg); // Open database in transaction. Transaction openTxn = paramEnv.BeginTransaction(); BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); cfg.Creation = CreatePolicy.ALWAYS; cfg.Env = paramEnv; cfg.FreeThreaded = true; cfg.PageSize = 4096; // Use record number. cfg.UseRecordNumbers = true; paramDB = BTreeDatabase.Open(name + ".db", cfg, openTxn); openTxn.Commit(); /* * Put 10 different, 2 duplicate and another different * records into database. */ Transaction txn = paramEnv.BeginTransaction(); for (int i = 0; i < 13; i++) { DatabaseEntry key, data; if (i == 10 || i == 11) { key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")); data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")); } else { key = new DatabaseEntry(BitConverter.GetBytes(i)); data = new DatabaseEntry(BitConverter.GetBytes(i)); } paramDB.Put(key, data, txn); } txn.Commit(); // Get a event wait handle. signal = new EventWaitHandle(false, EventResetMode.ManualReset); /* * Start RdMfWt() in two threads. RdMfWt() reads * and writes data into database. */ Thread t1 = new Thread(new ThreadStart(RdMfWt)); Thread t2 = new Thread(new ThreadStart(RdMfWt)); t1.Start(); t2.Start(); // Give both threads time to read before signalling them to write. Thread.Sleep(1000); // Invoke the write operation in both threads. signal.Set(); // Return the number of deadlocks. while (t1.IsAlive || t2.IsAlive) { /* * Give both threads time to write before * counting the number of deadlocks. */ Thread.Sleep(1000); uint deadlocks = paramEnv.DetectDeadlocks(DeadlockPolicy.DEFAULT); // Confirm that there won't be any deadlock. Assert.AreEqual(0, deadlocks); } t1.Join(); t2.Join(); paramDB.Close(); paramEnv.Close(); }
public void Put(DatabaseEntry key, DatabaseEntry value) { db.Put(key, value); }
public void TestBadSecondaryException() { testName = "TestBadSecondaryException"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; string secDBFileName = testHome + "/" + testName + "_sec.db"; // Open primary database. BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase btreeDB = BTreeDatabase.Open(dbFileName, btreeDBConfig); // Open secondary database. SecondaryBTreeDatabaseConfig secBtDbConfig = new SecondaryBTreeDatabaseConfig(btreeDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secBtDbConfig.Creation = CreatePolicy.IF_NEEDED; SecondaryBTreeDatabase secBtDb = SecondaryBTreeDatabase.Open(secDBFileName, secBtDbConfig); // Put some data into primary database. for (int i = 0; i < 10; i++) { btreeDB.Put(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i))); } // Close the secondary database. secBtDb.Close(); // Delete record(5, 5) in primary database. btreeDB.Delete(new DatabaseEntry( BitConverter.GetBytes((int)5))); // Reopen the secondary database. SecondaryDatabase secDB = SecondaryDatabase.Open( secDBFileName, new SecondaryDatabaseConfig(btreeDB, new SecondaryKeyGenDelegate(SecondaryKeyGen))); /* * Getting record(5, 5) by secondary database should * throw BadSecondaryException since it has been * deleted in the primary database. */ try { secDB.Exists(new DatabaseEntry( BitConverter.GetBytes((int)5))); } catch (BadSecondaryException) { throw new ExpectedTestException(); } finally { secDB.Close(); btreeDB.Close(); } }
public void TestDuplicate() { testName = "TestDuplicate"; testHome = testFixtureHome + "/" + testName; string dbFileName = testHome + "/" + testName + ".db"; string dbSecFileName = testHome + "/" + testName + "_sec.db"; Configuration.ClearDir(testHome); // Open a primary database. BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase db = BTreeDatabase.Open( dbFileName, dbConfig); // Open a secondary database. SecondaryBTreeDatabaseConfig secConfig = new SecondaryBTreeDatabaseConfig(db, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secConfig.Creation = CreatePolicy.IF_NEEDED; secConfig.Duplicates = DuplicatesPolicy.UNSORTED; SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open(dbSecFileName, secConfig); // Put a pair of key and data into the database. DatabaseEntry key, data; key = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("key")); data = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("data")); db.Put(key, data); // Create a cursor. SecondaryCursor cursor = secDB.SecondaryCursor(); cursor.Move(key, true); // Duplicate the cursor. SecondaryCursor dupCursor; dupCursor = cursor.Duplicate(true); /* * Confirm that the duplicate cursor has the same * position as the original one. */ Assert.AreEqual(cursor.Current.Key, dupCursor.Current.Key); Assert.AreEqual(cursor.Current.Value, dupCursor.Current.Value); // Close the cursor and the duplicate cursor. dupCursor.Close(); cursor.Close(); // Close secondary and primary database. secDB.Close(); db.Close(); }
public void TestLockStats() { testName = "TestLockStats"; SetUpTest(true); // Configure locking subsystem. LockingConfig lkConfig = new LockingConfig(); lkConfig.MaxLockers = 60; lkConfig.MaxLocks = 50; lkConfig.MaxObjects = 70; lkConfig.Partitions = 20; lkConfig.DeadlockResolution = DeadlockPolicy.DEFAULT; // Configure and open environment. DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.FreeThreaded = true; envConfig.LockSystemCfg = lkConfig; envConfig.LockTimeout = 1000; envConfig.MPoolSystemCfg = new MPoolConfig(); envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(0, 104800, 1); envConfig.NoLocking = false; envConfig.TxnTimeout = 2000; envConfig.UseLocking = true; envConfig.UseMPool = true; envConfig.UseTxns = true; DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, envConfig); // Get and confirm locking subsystem statistics. LockStats stats = env.LockingSystemStats(); env.Msgfile = testHome + "/" + testName + ".log"; env.PrintLockingSystemStats(true, true); Assert.AreEqual(0, stats.AllocatedLockers); Assert.AreNotEqual(0, stats.AllocatedLocks); Assert.AreNotEqual(0, stats.AllocatedObjects); Assert.AreEqual(0, stats.InitLockers); Assert.AreNotEqual(0, stats.InitLocks); Assert.AreNotEqual(0, stats.InitObjects); Assert.AreEqual(0, stats.LastAllocatedLockerID); Assert.AreEqual(0, stats.LockConflictsNoWait); Assert.AreEqual(0, stats.LockConflictsWait); Assert.AreEqual(0, stats.LockDeadlocks); Assert.AreEqual(0, stats.LockDowngrades); Assert.AreEqual(0, stats.LockerNoWait); Assert.AreEqual(0, stats.Lockers); Assert.AreEqual(0, stats.LockerWait); Assert.AreEqual(9, stats.LockModes); Assert.AreEqual(0, stats.LockPuts); Assert.AreEqual(0, stats.LockRequests); Assert.AreEqual(0, stats.Locks); Assert.AreEqual(0, stats.LockSteals); Assert.AreEqual(1000, stats.LockTimeoutLength); Assert.AreEqual(0, stats.LockTimeouts); Assert.AreEqual(0, stats.LockUpgrades); Assert.AreEqual(0, stats.MaxBucketLength); Assert.AreEqual(0, stats.MaxLockers); Assert.AreEqual(60, stats.MaxLockersInTable); Assert.AreEqual(0, stats.MaxLocks); Assert.AreEqual(0, stats.MaxLocksInBucket); Assert.AreEqual(50, stats.MaxLocksInTable); Assert.AreEqual(0, stats.MaxLockSteals); Assert.AreEqual(0, stats.MaxObjects); Assert.AreEqual(0, stats.MaxObjectsInBucket); Assert.AreEqual(70, stats.MaxObjectsInTable); Assert.AreEqual(0, stats.MaxObjectSteals); Assert.AreEqual(0, stats.MaxPartitionLockNoWait); Assert.AreEqual(0, stats.MaxPartitionLockWait); Assert.AreNotEqual(0, stats.MaxUnusedID); Assert.AreEqual(20, stats.nPartitions); Assert.AreEqual(0, stats.ObjectNoWait); Assert.AreEqual(0, stats.Objects); Assert.AreEqual(0, stats.ObjectSteals); Assert.AreEqual(0, stats.ObjectWait); Assert.LessOrEqual(0, stats.PartitionLockNoWait); Assert.AreEqual(0, stats.PartitionLockWait); Assert.Less(0, stats.RegionNoWait); Assert.AreNotEqual(0, stats.RegionSize); Assert.AreEqual(0, stats.RegionWait); Assert.AreNotEqual(0, stats.TableSize); Assert.AreEqual(2000, stats.TxnTimeoutLength); Assert.AreEqual(0, stats.TxnTimeouts); env.PrintLockingSystemStats(); Transaction txn = env.BeginTransaction(); BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; dbConfig.FreeThreaded = true; BTreeDatabase db = BTreeDatabase.Open( testName + ".db", dbConfig, txn); txn.Commit(); testLockStatsEnv = env; testLockStatsDb = db; // Use some locks, to ensure the stats work when populated. txn = testLockStatsEnv.BeginTransaction(); for (int i = 0; i < 500; i++) { testLockStatsDb.Put( new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes( Configuration.RandomString(i))), txn); testLockStatsDb.Sync(); } txn.Commit(); env.PrintLockingSystemStats(); stats = env.LockingSystemStats(); Assert.Less(0, stats.LastAllocatedLockerID); Assert.Less(0, stats.LockDowngrades); Assert.LessOrEqual(0, stats.LockerNoWait); Assert.Less(0, stats.Lockers); Assert.LessOrEqual(0, stats.LockerWait); Assert.Less(0, stats.LockPuts); Assert.Less(0, stats.LockRequests); Assert.Less(0, stats.Locks); Assert.LessOrEqual(0, stats.LockSteals); Assert.LessOrEqual(0, stats.LockTimeouts); Assert.LessOrEqual(0, stats.LockUpgrades); Assert.Less(0, stats.MaxBucketLength); Assert.Less(0, stats.MaxLockers); Assert.Less(0, stats.MaxLocks); Assert.Less(0, stats.MaxLocksInBucket); Assert.LessOrEqual(0, stats.MaxLockSteals); Assert.Less(0, stats.MaxObjects); Assert.Less(0, stats.MaxObjectsInBucket); Assert.LessOrEqual(0, stats.MaxObjectSteals); Assert.LessOrEqual(0, stats.MaxPartitionLockNoWait); Assert.LessOrEqual(0, stats.MaxPartitionLockWait); Assert.Less(0, stats.MaxUnusedID); Assert.LessOrEqual(0, stats.ObjectNoWait); Assert.Less(0, stats.Objects); Assert.LessOrEqual(0, stats.ObjectSteals); Assert.LessOrEqual(0, stats.ObjectWait); Assert.Less(0, stats.PartitionLockNoWait); Assert.LessOrEqual(0, stats.PartitionLockWait); Assert.Less(0, stats.RegionNoWait); Assert.LessOrEqual(0, stats.RegionWait); Assert.LessOrEqual(0, stats.TxnTimeouts); // Force a deadlock to ensure the stats work. txn = testLockStatsEnv.BeginTransaction(); testLockStatsDb.Put(new DatabaseEntry(BitConverter.GetBytes(10)), new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes( Configuration.RandomString(200))), txn); Thread thread1 = new Thread(GenerateDeadlock); thread1.Start(); while (DeadlockDidPut == 0) { Thread.Sleep(10); } try { testLockStatsDb.Get(new DatabaseEntry( BitConverter.GetBytes(100)), txn); } catch (DeadlockException) { } // Abort unconditionally - we don't care about the transaction txn.Abort(); thread1.Join(); stats = env.LockingSystemStats(); Assert.Less(0, stats.LockConflictsNoWait); Assert.LessOrEqual(0, stats.LockConflictsWait); db.Close(); env.Close(); }
public int doloop() { BTreeDatabase db = null; for (;;) { if (db == null) { BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Env = dbenv.env; if (dbenv.IsMaster) { /* * Open database allowing create only if this is a master * database. A client database uses polling to attempt * to open the database, without creating it, until the * open succeeds. * * This polling logic for allowing create can be * simplified under some circumstances. For example, if * the application can be sure a database is already * there, it would never need to open it allowing create. */ dbConfig.Creation = CreatePolicy.IF_NEEDED; } dbConfig.AutoCommit = true; try { db = BTreeDatabase.Open(RepConfig.progname, dbConfig); } catch (DatabaseException) { Console.WriteLine("no stock database available yet."); if (db != null) { db.Close(true); db = null; } Thread.Sleep(RepConfig.SLEEPTIME); continue; } } /* Listen for input, and add it to the database. */ Console.Write("QUOTESERVER"); if (dbenv.IsMaster == false) { Console.Write("(read-only)"); } Console.Write("> "); string nextLine = null; try { nextLine = Console.ReadLine(); } catch (System.IO.IOException) { Console.WriteLine("Unable to get data"); break; } /* A blank line causes the DB to be dumped. */ string[] words = nextLine.Split(' '); if (words.Length == 0 || words.Length == 1 && words[0].Length == 0) { try { if (dbenv.InClientSync) { Console.WriteLine("Cannot read data during " + "client initialization - please try again."); } else { printStocks(db); } } catch (DeadlockException) { continue; } catch (DatabaseException e) { /* * This could be DB_REP_HANDLE_DEAD, which * should close the database and continue. */ Console.WriteLine("Got db exception reading replication" + "DB: " + e); Console.WriteLine("Expected if it was due to a dead " + "replication handle, otherwise an unexpected error."); db.Close(false); /* Close no sync. */ db = null; continue; } continue; } if (words.Length == 1 && (words[0].ToLower().Equals("quit") || words[0].ToLower().Equals("exit"))) { dbenv.AppFinished = true; break; } else if (words.Length != 2) { Console.WriteLine("Format: TICKER VALUE"); continue; } if (!dbenv.IsMaster) { Console.WriteLine("Can't update client"); continue; } DatabaseEntry key = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes(words[0])); DatabaseEntry data = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes(words[1])); db.Put(key, data); } if (db != null) { db.Close(true); } return(0); }
public void TestMultiKeyGen() { testName = "TestPrefixCompare"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; // Open a primary btree database. BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Creation = CreatePolicy.ALWAYS; BTreeDatabase btreeDB = BTreeDatabase.Open( dbFileName, btreeDBConfig); // Open a secondary btree database. SecondaryBTreeDatabaseConfig secBtreeDBConfig = new SecondaryBTreeDatabaseConfig(btreeDB, null); secBtreeDBConfig.Primary = btreeDB; secBtreeDBConfig.KeyGen = new SecondaryKeyGenDelegate( MultipleKeyGen); SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open( dbFileName, secBtreeDBConfig); /* Check that multiple secondary keys work */ DatabaseEntry key, data; String keyStr = "key"; key = new DatabaseEntry(); Configuration.dbtFromString(key, keyStr); data = new DatabaseEntry(); String[] dataStrs = { "abc", "def", "ghi", "jkl" }; String dataStr = String.Join(",", dataStrs); Configuration.dbtFromString(data, dataStr); btreeDB.Put(key, data); foreach (String skeyStr in dataStrs) { DatabaseEntry skey = new DatabaseEntry(); Configuration.dbtFromString(skey, skeyStr); Assert.IsTrue(secDB.Exists(skey)); } /* * Check that a single secondary key, returned in a * MultipleDatabaseEntry works. */ keyStr = "key2"; key = new DatabaseEntry(); Configuration.dbtFromString(key, keyStr); data = new DatabaseEntry(); dataStrs = new string[1] { "abcdefghijkl" }; dataStr = String.Join(",", dataStrs); Configuration.dbtFromString(data, dataStr); btreeDB.Put(key, data); // Check that secondary keys work foreach (String skeyStr in dataStrs) { DatabaseEntry skey = new DatabaseEntry(); Configuration.dbtFromString(skey, skeyStr); Assert.IsTrue(secDB.Exists(skey)); } }
public void Master() { string home = testHome + "/Master"; string dbName = "rep.db"; Configuration.ClearDir(home); /* * Configure and open environment with replication * application. */ DatabaseEnvironmentConfig cfg = new DatabaseEnvironmentConfig(); cfg.UseReplication = true; cfg.MPoolSystemCfg = new MPoolConfig(); cfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 20485760, 1); cfg.UseLocking = true; cfg.UseTxns = true; cfg.UseMPool = true; cfg.Create = true; cfg.UseLogging = true; cfg.RunRecovery = true; cfg.TxnNoSync = true; cfg.FreeThreaded = true; cfg.RepSystemCfg = new ReplicationConfig(); cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); cfg.RepSystemCfg.RepmgrSitesConfig[0].Host = "127.0.0.1"; cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = ports[0]; cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true; cfg.RepSystemCfg.Priority = 100; cfg.RepSystemCfg.BulkTransfer = true; cfg.RepSystemCfg.AckTimeout = 2000; cfg.RepSystemCfg.BulkTransfer = true; cfg.RepSystemCfg.CheckpointDelay = 1500; cfg.RepSystemCfg.Clockskew(102, 100); cfg.RepSystemCfg.ConnectionRetry = 10; cfg.RepSystemCfg.DelayClientSync = false; cfg.RepSystemCfg.ElectionRetry = 5; cfg.RepSystemCfg.ElectionTimeout = 3000; cfg.RepSystemCfg.FullElectionTimeout = 5000; cfg.RepSystemCfg.HeartbeatMonitor = 100; cfg.RepSystemCfg.HeartbeatSend = 10; cfg.RepSystemCfg.LeaseTimeout = 1300; cfg.RepSystemCfg.AutoInit = true; cfg.RepSystemCfg.NoBlocking = false; cfg.RepSystemCfg.RepMgrAckPolicy = AckPolicy.ALL_PEERS; cfg.RepSystemCfg.RetransmissionRequest(10, 100); cfg.RepSystemCfg.Strict2Site = true; cfg.RepSystemCfg.UseMasterLeases = false; cfg.EventNotify = new EventNotifyDelegate(stuffHappened); DatabaseEnvironment env = DatabaseEnvironment.Open( home, cfg); // Get initial replication stats. ReplicationStats repStats = env.ReplicationSystemStats(); env.PrintReplicationSystemStats(); Assert.AreEqual(100, repStats.EnvPriority); Assert.AreEqual(1, repStats.CurrentElectionGenerationNumber); Assert.AreEqual(0, repStats.CurrentGenerationNumber); Assert.AreEqual(0, repStats.AppliedTransactions); Assert.AreEqual(0, repStats.ElectionDataGeneration); // Start a master site with replication manager. env.RepMgrStartMaster(3); // Open a btree database and write some data. Transaction txn = env.BeginTransaction(); BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; dbConfig.PageSize = 512; BTreeDatabase db = BTreeDatabase.Open(dbName, dbConfig, txn); txn.Commit(); txn = env.BeginTransaction(); for (int i = 0; i < 5; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i)), txn); } txn.Commit(); Console.WriteLine( "Master: Finished initialization and data#1."); // Client site could enter now. clientStartSignal.Set(); Console.WriteLine( "Master: Wait for Client to join and get #1."); Console.WriteLine("..."); // Put some new data into master site. txn = env.BeginTransaction(); for (int i = 10; i < 15; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i)), txn); } txn.Commit(); Console.WriteLine( "Master: Write something new, data #2."); Console.WriteLine("Master: Wait for client to read #2..."); // Get the stats. repStats = env.ReplicationSystemStats(true); env.PrintReplicationSystemStats(); Assert.LessOrEqual(0, repStats.AppliedTransactions); Assert.LessOrEqual(0, repStats.AwaitedLSN.LogFileNumber); Assert.LessOrEqual(0, repStats.AwaitedLSN.Offset); Assert.LessOrEqual(0, repStats.AwaitedPage); Assert.LessOrEqual(0, repStats.BadGenerationMessages); Assert.LessOrEqual(0, repStats.BulkBufferFills); Assert.LessOrEqual(0, repStats.BulkBufferOverflows); Assert.LessOrEqual(0, repStats.BulkBufferTransfers); Assert.LessOrEqual(0, repStats.BulkRecordsStored); Assert.LessOrEqual(0, repStats.ClientServiceRequests); Assert.LessOrEqual(0, repStats.ClientServiceRequestsMissing); Assert.IsInstanceOf(typeof(bool), repStats.ClientStartupComplete); Assert.AreEqual(2, repStats.CurrentElectionGenerationNumber); Assert.AreEqual(1, repStats.CurrentGenerationNumber); Assert.LessOrEqual(0, repStats.CurrentQueuedLogRecords); Assert.LessOrEqual(0, repStats.CurrentWinner); Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.LogFileNumber); Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.Offset); Assert.LessOrEqual(0, repStats.DuplicateLogRecords); Assert.LessOrEqual(0, repStats.DuplicatePages); Assert.LessOrEqual(0, repStats.DupMasters); Assert.LessOrEqual(0, repStats.ElectionGenerationNumber); Assert.LessOrEqual(0, repStats.ElectionPriority); Assert.LessOrEqual(0, repStats.Elections); Assert.LessOrEqual(0, repStats.ElectionStatus); Assert.LessOrEqual(0, repStats.ElectionsWon); Assert.LessOrEqual(0, repStats.ElectionTiebreaker); Assert.LessOrEqual(0, repStats.ElectionTimeSec); Assert.LessOrEqual(0, repStats.ElectionTimeUSec); Assert.AreEqual(repStats.EnvID, repStats.MasterEnvID); Assert.LessOrEqual(0, repStats.EnvPriority); Assert.LessOrEqual(0, repStats.FailedMessageSends); Assert.LessOrEqual(0, repStats.ForcedRerequests); Assert.LessOrEqual(0, repStats.IgnoredMessages); Assert.LessOrEqual(0, repStats.MasterChanges); Assert.LessOrEqual(0, repStats.MasterEnvID); Assert.LessOrEqual(0, repStats.MaxLeaseSec); Assert.LessOrEqual(0, repStats.MaxLeaseUSec); Assert.LessOrEqual(0, repStats.MaxPermanentLSN.Offset); Assert.LessOrEqual(0, repStats.MaxQueuedLogRecords); Assert.LessOrEqual(0, repStats.MessagesSent); Assert.LessOrEqual(0, repStats.MissedLogRecords); Assert.LessOrEqual(0, repStats.MissedPages); Assert.LessOrEqual(0, repStats.NewSiteMessages); Assert.LessOrEqual(repStats.MaxPermanentLSN.LogFileNumber, repStats.NextLSN.LogFileNumber); if (repStats.MaxPermanentLSN.LogFileNumber == repStats.NextLSN.LogFileNumber) { Assert.Less(repStats.MaxPermanentLSN.Offset, repStats.NextLSN.Offset); } Assert.LessOrEqual(0, repStats.NextPage); Assert.LessOrEqual(0, repStats.Outdated); Assert.LessOrEqual(0, repStats.QueuedLogRecords); Assert.LessOrEqual(0, repStats.ReceivedLogRecords); Assert.LessOrEqual(0, repStats.ReceivedMessages); Assert.LessOrEqual(0, repStats.ReceivedPages); Assert.LessOrEqual(0, repStats.RegisteredSites); Assert.LessOrEqual(0, repStats.RegisteredSitesNeeded); Assert.LessOrEqual(0, repStats.Sites); Assert.LessOrEqual(0, repStats.StartSyncMessagesDelayed); Assert.AreEqual(2, repStats.Status); Assert.LessOrEqual(0, repStats.Throttled); Assert.LessOrEqual(0, repStats.Votes); // Get replication manager statistics. RepMgrStats repMgrStats = env.RepMgrSystemStats(true); Assert.AreEqual(0, repMgrStats.AutoTakeovers); Assert.LessOrEqual(0, repMgrStats.DroppedConnections); Assert.LessOrEqual(0, repMgrStats.DroppedMessages); Assert.LessOrEqual(0, repMgrStats.ElectionThreads); Assert.LessOrEqual(0, repMgrStats.FailedConnections); Assert.LessOrEqual(0, repMgrStats.FailedMessages); Assert.Less(0, repMgrStats.MaxElectionThreads); Assert.LessOrEqual(0, repMgrStats.QueuedMessages); // Print them out. env.PrintRepMgrSystemStats(); // Wait until client has finished reading. masterCloseSignal.WaitOne(); Console.WriteLine("Master: Leave as well."); // Close all. db.Close(false); env.LogFlush(); env.Close(); }
public void TestCommitSuccess() { testName = "TestCommitSuccess"; SetUpTest(true); string[] keys = { "key 1", "key 2", "key 3", "key 4", "key 5", "key 6", "key 7", "key 8","key 9", "key 10" }; DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, masterPort, true); DatabaseEnvironment client = SetUpEnv(testHome + "/client", 0, clientPort, false); master.RepMgrStartMaster(2); Thread.Sleep(2000); client.RepMgrStartClient(2); BTreeDatabase db1 = Open(master, true); BTreeDatabase db2 = null; for (; ;) { if (db2 == null) { try { db2 = Open(client, false); break; } catch (DatabaseException) { if (db2 != null) { db2.Close(true); db2 = null; } System.Threading.Thread.Sleep(1000); continue; } } } try { for (int i = 0; i < 3; i++) { Transaction txn = master.BeginTransaction(); // Get the key. DatabaseEntry key, data; key = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes(keys[i])); data = new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes(keys[i])); db1.Put(key, data, txn); txn.Commit(); byte[] token = txn.CommitToken; Assert.AreEqual(master.IsTransactionApplied(token, 5000), TransactionAppliedStatus.APPLIED); Assert.AreEqual(client.IsTransactionApplied(token, 200000), TransactionAppliedStatus.APPLIED); } } finally { db1.Close(); db2.Close(); master.Close(); client.Close(); } }
public void TestSnapshotIsolation() { BTreeDatabaseConfig dbConfig; DatabaseEntry key, data; DatabaseEnvironmentConfig envConfig; Thread readThread, updateThread; Transaction txn; updateTxn = null; readTxn = null; paramDB = null; paramEnv = null; testName = "TestSnapshotIsolation"; testHome = testFixtureHome + "/" + testName; Configuration.ClearDir(testHome); /* * Open environment with DB_MULTIVERSION * which is required by DB_TXN_SNAPSHOT. */ envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseMVCC = true; envConfig.UseTxns = true; envConfig.UseMPool = true; envConfig.UseLocking = true; envConfig.TxnTimeout = 1000; paramEnv = DatabaseEnvironment.Open( testHome, envConfig); paramEnv.DetectDeadlocks(DeadlockPolicy.YOUNGEST); /* * Open a transactional database and put 1000 records * into it within transaction. */ txn = paramEnv.BeginTransaction(); dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.UseMVCC = true; dbConfig.Env = paramEnv; paramDB = BTreeDatabase.Open( testName + ".db", dbConfig, txn); for (int i = 0; i < 256; i++) { key = new DatabaseEntry( BitConverter.GetBytes(i)); data = new DatabaseEntry( BitConverter.GetBytes(i)); paramDB.Put(key, data, txn); } txn.Commit(); /* * Begin two threads, read and update thread. * The update thread runs a update transaction * using full read/write locking. The read thread * set DB_TXN_SNAPSHOT on read-only cursor. */ readThread = new Thread(new ThreadStart(ReadTxn)); updateThread = new Thread(new ThreadStart(UpdateTxn)); updateThread.Start(); Thread.Sleep(1000); readThread.Start(); readThread.Join(); updateThread.Join(); // Commit transacion in both two threads. if (updateTxn != null) { updateTxn.Commit(); } if (readTxn != null) { readTxn.Commit(); } /* * Confirm that the overwrite operation works. */ ConfirmOverwrite(); paramDB.Close(); paramEnv.Close(); }