public void TestForeignKeyDelete(DatabaseType dbtype, ForeignKeyDeleteAction action) { SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; string fdbFileName = testHome + "/" + testName + "foreign.db"; string sdbFileName = testHome + "/" + testName + "sec.db"; Database primaryDB, fdb; SecondaryDatabase secDB; // Open primary database. if (dbtype == DatabaseType.BTREE) { BTreeDatabaseConfig btConfig = new BTreeDatabaseConfig(); btConfig.Creation = CreatePolicy.ALWAYS; primaryDB = BTreeDatabase.Open(dbFileName, btConfig); fdb = BTreeDatabase.Open(fdbFileName, btConfig); } else if (dbtype == DatabaseType.HASH) { HashDatabaseConfig hConfig = new HashDatabaseConfig(); hConfig.Creation = CreatePolicy.ALWAYS; primaryDB = HashDatabase.Open(dbFileName, hConfig); fdb = HashDatabase.Open(fdbFileName, hConfig); } else if (dbtype == DatabaseType.QUEUE) { QueueDatabaseConfig qConfig = new QueueDatabaseConfig(); qConfig.Creation = CreatePolicy.ALWAYS; qConfig.Length = 4; primaryDB = QueueDatabase.Open(dbFileName, qConfig); fdb = QueueDatabase.Open(fdbFileName, qConfig); } else if (dbtype == DatabaseType.RECNO) { RecnoDatabaseConfig rConfig = new RecnoDatabaseConfig(); rConfig.Creation = CreatePolicy.ALWAYS; primaryDB = RecnoDatabase.Open(dbFileName, rConfig); fdb = RecnoDatabase.Open(fdbFileName, rConfig); } else { throw new ArgumentException("Invalid DatabaseType"); } // Open secondary database. if (dbtype == DatabaseType.BTREE) { SecondaryBTreeDatabaseConfig secbtConfig = new SecondaryBTreeDatabaseConfig(primaryDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secbtConfig.Creation = CreatePolicy.ALWAYS; secbtConfig.Duplicates = DuplicatesPolicy.SORTED; if (action == ForeignKeyDeleteAction.NULLIFY) { secbtConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); } else { secbtConfig.SetForeignKeyConstraint(fdb, action); } secDB = SecondaryBTreeDatabase.Open(sdbFileName, secbtConfig); } else if (dbtype == DatabaseType.HASH) { SecondaryHashDatabaseConfig sechConfig = new SecondaryHashDatabaseConfig(primaryDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); sechConfig.Creation = CreatePolicy.ALWAYS; sechConfig.Duplicates = DuplicatesPolicy.SORTED; if (action == ForeignKeyDeleteAction.NULLIFY) { sechConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); } else { sechConfig.SetForeignKeyConstraint(fdb, action); } secDB = SecondaryHashDatabase.Open(sdbFileName, sechConfig); } else if (dbtype == DatabaseType.QUEUE) { SecondaryQueueDatabaseConfig secqConfig = new SecondaryQueueDatabaseConfig(primaryDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secqConfig.Creation = CreatePolicy.ALWAYS; secqConfig.Length = 4; if (action == ForeignKeyDeleteAction.NULLIFY) { secqConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); } else { secqConfig.SetForeignKeyConstraint(fdb, action); } secDB = SecondaryQueueDatabase.Open(sdbFileName, secqConfig); } else if (dbtype == DatabaseType.RECNO) { SecondaryRecnoDatabaseConfig secrConfig = new SecondaryRecnoDatabaseConfig(primaryDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secrConfig.Creation = CreatePolicy.ALWAYS; if (action == ForeignKeyDeleteAction.NULLIFY) { secrConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); } else { secrConfig.SetForeignKeyConstraint(fdb, action); } secDB = SecondaryRecnoDatabase.Open(sdbFileName, secrConfig); } else { throw new ArgumentException("Invalid DatabaseType"); } /* Use integer keys for Queue/Recno support. */ fdb.Put(new DatabaseEntry(BitConverter.GetBytes(100)), new DatabaseEntry(BitConverter.GetBytes(1001))); fdb.Put(new DatabaseEntry(BitConverter.GetBytes(200)), new DatabaseEntry(BitConverter.GetBytes(2002))); fdb.Put(new DatabaseEntry(BitConverter.GetBytes(300)), new DatabaseEntry(BitConverter.GetBytes(3003))); primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(1)), new DatabaseEntry(BitConverter.GetBytes(100))); primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(2)), new DatabaseEntry(BitConverter.GetBytes(200))); if (dbtype == DatabaseType.BTREE || dbtype == DatabaseType.HASH) { primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(3)), new DatabaseEntry(BitConverter.GetBytes(100))); } try { fdb.Delete(new DatabaseEntry(BitConverter.GetBytes(100))); } catch (ForeignConflictException) { Assert.AreEqual(action, ForeignKeyDeleteAction.ABORT); } if (action == ForeignKeyDeleteAction.ABORT) { Assert.IsTrue(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); Assert.IsTrue(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1)))); Assert.IsTrue(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); } else if (action == ForeignKeyDeleteAction.CASCADE) { try { Assert.IsFalse(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); } catch (KeyEmptyException) { Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); } try { Assert.IsFalse(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1)))); } catch (KeyEmptyException) { Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); } try { Assert.IsFalse(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); } catch (KeyEmptyException) { Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); } } else if (action == ForeignKeyDeleteAction.NULLIFY) { try { Assert.IsFalse(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); } catch (KeyEmptyException) { Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); } Assert.IsTrue(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1)))); try { Assert.IsFalse(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); } catch (KeyEmptyException) { Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); } } // Close secondary database. secDB.Close(); // Close primary database. primaryDB.Close(); // Close foreign database fdb.Close(); }
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.RepMgrLocalSite = new ReplicationHostAddress("127.0.0.1", ports[0]); cfg.RepSystemCfg.Priority = 100; cfg.RepSystemCfg.NSites = 2; 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); // Start a master site with replication manager. env.RepMgrStartMaster(3); // Open a btree database and write some data. BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.AutoCommit = true; dbConfig.Env = env; dbConfig.PageSize = 512; BTreeDatabase db = BTreeDatabase.Open(dbName, dbConfig); for (int i = 0; i < 5; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i))); } 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. for (int i = 10; i < 15; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(BitConverter.GetBytes(i))); } 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.LessOrEqual(0, repMgrStats.DroppedConnections); Assert.LessOrEqual(0, repMgrStats.DroppedMessages); Assert.LessOrEqual(0, repMgrStats.FailedConnections); Assert.LessOrEqual(0, repMgrStats.FailedMessages); 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 Client() { string home = testHome + "/Client"; Configuration.ClearDir(home); clientStartSignal.WaitOne(); Console.WriteLine("Client: Join the replication"); // Open a environment. 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.LockTimeout = 50000; cfg.RepSystemCfg = new ReplicationConfig(); cfg.RepSystemCfg.RepMgrLocalSite = new ReplicationHostAddress("127.0.0.1", ports[1]); cfg.RepSystemCfg.Priority = 10; cfg.RepSystemCfg.AddRemoteSite( new ReplicationHostAddress("127.0.0.1", ports[0]), false); cfg.RepSystemCfg.NSites = 2; cfg.EventNotify = new EventNotifyDelegate(stuffHappened); DatabaseEnvironment env = DatabaseEnvironment.Open( home, cfg); // Start a client site with replication manager. env.RepMgrStartClient(3, false); // Leave enough time to sync. Thread.Sleep(20000); // Open database. BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.NEVER; dbConfig.AutoCommit = true; dbConfig.Env = env; dbConfig.PageSize = 512; BTreeDatabase db = BTreeDatabase.Open("rep.db", dbConfig); // Write data into database. Console.WriteLine("Client: Start reading data #1."); for (int i = 0; i < 5; i++) { db.GetBoth(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry( BitConverter.GetBytes(i))); } // Leave sometime for client to read new data from master. Thread.Sleep(20000); /* * Read the data. All data exists in master site should * appear in the client site. */ Console.WriteLine("Client: Start reading data #2."); for (int i = 10; i < 15; i++) { db.GetBoth(new DatabaseEntry( BitConverter.GetBytes(i)), new DatabaseEntry( BitConverter.GetBytes(i))); } // Get the latest replication subsystem statistics. ReplicationStats repStats = env.ReplicationSystemStats(); Assert.IsTrue(repStats.ClientStartupComplete); Assert.AreEqual(1, repStats.DuplicateLogRecords); Assert.LessOrEqual(0, repStats.EnvID); Assert.LessOrEqual(0, repStats.NextPage); Assert.LessOrEqual(0, repStats.ReceivedPages); Assert.AreEqual(1, repStats.Status); // Close all. db.Close(false); env.LogFlush(); env.Close(); Console.WriteLine( "Client: All data is read. Leaving the replication"); // The master is closed after client's close. masterCloseSignal.Set(); }
private void DeleteMultipleAndMultipleKey(string dbFileName, string dbName, DatabaseType type, bool mulKey) { List <DatabaseEntry> kList = new List <DatabaseEntry>(); List <uint> rList = new List <uint>(); List <KeyValuePair <DatabaseEntry, DatabaseEntry> > pList = new List <KeyValuePair <DatabaseEntry, DatabaseEntry> >(); DatabaseEntry key; Database db; SecondaryDatabase secDb; Configuration.ClearDir(testHome); if (type == DatabaseType.BTREE) { BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; db = BTreeDatabase.Open( dbFileName, dbName, dbConfig); SecondaryBTreeDatabaseConfig secDbConfig = new SecondaryBTreeDatabaseConfig(db, null); secDbConfig.Creation = CreatePolicy.IF_NEEDED; secDbConfig.Duplicates = DuplicatesPolicy.SORTED; secDbConfig.KeyGen = new SecondaryKeyGenDelegate(SecondaryKeyGen); secDb = SecondaryBTreeDatabase.Open( dbFileName, dbName + "_sec", secDbConfig); } else if (type == DatabaseType.HASH) { HashDatabaseConfig dbConfig = new HashDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; db = HashDatabase.Open( dbFileName, dbName, dbConfig); SecondaryHashDatabaseConfig secDbConfig = new SecondaryHashDatabaseConfig(db, null); secDbConfig.Creation = CreatePolicy.IF_NEEDED; secDbConfig.Duplicates = DuplicatesPolicy.SORTED; secDbConfig.KeyGen = new SecondaryKeyGenDelegate(SecondaryKeyGen); secDb = SecondaryHashDatabase.Open( dbFileName, dbName + "_sec", secDbConfig); } else if (type == DatabaseType.QUEUE) { QueueDatabaseConfig dbConfig = new QueueDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Length = 4; db = QueueDatabase.Open(dbFileName, dbConfig); SecondaryQueueDatabaseConfig secDbConfig = new SecondaryQueueDatabaseConfig(db, null); secDbConfig.Creation = CreatePolicy.IF_NEEDED; secDbConfig.Length = 4; secDbConfig.KeyGen = new SecondaryKeyGenDelegate(SecondaryKeyGen); secDb = SecondaryQueueDatabase.Open( dbFileName + "_sec", secDbConfig); } else if (type == DatabaseType.RECNO) { RecnoDatabaseConfig dbConfig = new RecnoDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; db = RecnoDatabase.Open( dbFileName, dbName, dbConfig); SecondaryRecnoDatabaseConfig secDbConfig = new SecondaryRecnoDatabaseConfig(db, null); secDbConfig.Creation = CreatePolicy.IF_NEEDED; secDbConfig.KeyGen = new SecondaryKeyGenDelegate(SecondaryKeyGen); secDb = SecondaryRecnoDatabase.Open( dbFileName, dbName + "_sec", secDbConfig); } else { throw new TestException(); } for (uint i = 1; i <= 100; i++) { key = new DatabaseEntry( BitConverter.GetBytes(i)); if (i >= 50 && i < 60) { kList.Add(key); } else if (i > 80) { pList.Add(new KeyValuePair < DatabaseEntry, DatabaseEntry>( key, key)); } else if (type == DatabaseType.QUEUE || type == DatabaseType.RECNO) { rList.Add(i); } db.Put(key, key); } if (mulKey) { // Create bulk buffer for key/value pairs. MultipleKeyDatabaseEntry pBuff; if (type == DatabaseType.BTREE) { pBuff = new MultipleKeyDatabaseEntry( pList, false); } else if (type == DatabaseType.HASH) { pBuff = new MultipleKeyDatabaseEntry( pList, false); } else if (type == DatabaseType.QUEUE) { pBuff = new MultipleKeyDatabaseEntry( pList, true); } else { pBuff = new MultipleKeyDatabaseEntry( pList, true); } // Bulk delete with the key/value pair bulk buffer. secDb.Delete(pBuff); foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> pair in pList) { try { db.GetBoth(pair.Key, pair.Value); throw new TestException(); } catch (NotFoundException e1) { if (type == DatabaseType.QUEUE) { throw e1; } } catch (KeyEmptyException e2) { if (type == DatabaseType.BTREE || type == DatabaseType.HASH || type == DatabaseType.RECNO) { throw e2; } } } /* * Dump the database to verify that 80 records * remain after bulk delete. */ Assert.AreEqual(80, db.Truncate()); } else { // Create bulk buffer for key. MultipleDatabaseEntry kBuff; if (type == DatabaseType.BTREE) { kBuff = new MultipleDatabaseEntry( kList, false); } else if (type == DatabaseType.HASH) { kBuff = new MultipleDatabaseEntry( kList, false); } else if (type == DatabaseType.QUEUE) { kBuff = new MultipleDatabaseEntry( kList, true); } else { kBuff = new MultipleDatabaseEntry( kList, true); } /* * Bulk delete in secondary database with key * buffer. Primary records that the deleted * records in secondar database should be * deleted as well. */ secDb.Delete(kBuff); foreach (DatabaseEntry dbt in kList) { try { db.Get(dbt); throw new TestException(); } catch (NotFoundException e1) { if (type == DatabaseType.QUEUE || type == DatabaseType.RECNO) { throw e1; } } catch (KeyEmptyException e2) { if (type == DatabaseType.BTREE || type == DatabaseType.HASH) { throw e2; } } } /* * Bulk delete in secondary database with recno * based key buffer. */ if (type == DatabaseType.QUEUE || type == DatabaseType.RECNO) { MultipleDatabaseEntry rBuff = new MultipleDatabaseEntry(rList); secDb.Delete(rBuff); Assert.AreEqual(20, db.Truncate()); } } secDb.Close(); db.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(); }
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 OpenSecQueueDB(string dbFileName, string dbSecFileName, bool ifDBName) { // Open a primary btree database. BTreeDatabaseConfig primaryDBConfig = new BTreeDatabaseConfig(); primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase primaryDB; /* * If secondary database name is given, the primary * database is also opened with database name. */ if (ifDBName == false) { primaryDB = BTreeDatabase.Open(dbFileName, primaryDBConfig); } else { primaryDB = BTreeDatabase.Open(dbFileName, "primary", primaryDBConfig); } try { // Open a new secondary database. SecondaryBTreeDatabaseConfig secBTDBConfig = new SecondaryBTreeDatabaseConfig( primaryDB, null); secBTDBConfig.Creation = CreatePolicy.IF_NEEDED; SecondaryBTreeDatabase secBTDB; if (ifDBName == false) { secBTDB = SecondaryBTreeDatabase.Open( dbSecFileName, secBTDBConfig); } else { secBTDB = SecondaryBTreeDatabase.Open( dbSecFileName, "secondary", secBTDBConfig); } // Close the secondary database. secBTDB.Close(); // Open the existing secondary database. SecondaryDatabaseConfig secDBConfig = new SecondaryDatabaseConfig( primaryDB, null); SecondaryDatabase secDB; if (ifDBName == false) { secDB = SecondaryBTreeDatabase.Open( dbSecFileName, secDBConfig); } else { secDB = SecondaryBTreeDatabase.Open( dbSecFileName, "secondary", secDBConfig); } // Close secondary database. secDB.Close(); } catch (DatabaseException) { throw new TestException(); } finally { // Close primary database. primaryDB.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 TestDuplicateToDifferentPos() { BTreeDatabase db; BTreeDatabaseConfig dbConfig; BTreeCursor cursor, dupCursor; DatabaseEnvironment env; DatabaseEnvironmentConfig envConfig; DatabaseEntry key, data; KeyValuePair <DatabaseEntry, DatabaseEntry> pair; Transaction txn; testName = "TestDuplicateToDifferentPos"; SetUpTest(true); envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseMPool = true; envConfig.UseTxns = true; envConfig.NoMMap = false; env = DatabaseEnvironment.Open(testHome, envConfig); txn = env.BeginTransaction(); dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; db = BTreeDatabase.Open(testName + ".db", dbConfig, txn); key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")); data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")); pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(key, data); db.Put(key, data, txn); txn.Commit(); txn = env.BeginTransaction(); cursor = db.Cursor(txn); cursor.Move(key, true); //Duplicate a new cursor to the same position. dupCursor = cursor.Duplicate(false); /* * The duplicate cursor points to nothing so overwriting the * record is not allowed. */ try { dupCursor.Overwrite(new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("newdata"))); } catch (DatabaseException) { throw new ExpectedTestException(); } finally { dupCursor.Close(); cursor.Close(); txn.Commit(); db.Close(); env.Close(); } }
public void TestFullLogBufferException() { testName = "TestFullLogBufferException"; SetUpTest(true); // Open an environment and configured log subsystem. DatabaseEnvironmentConfig cfg = new DatabaseEnvironmentConfig(); cfg.Create = true; cfg.TxnNoSync = true; cfg.UseTxns = true; cfg.UseLocking = true; cfg.UseMPool = true; cfg.UseLogging = true; cfg.LogSystemCfg = new LogConfig(); cfg.LogSystemCfg.AutoRemove = false; cfg.LogSystemCfg.BufferSize = 409600; cfg.LogSystemCfg.MaxFileSize = 10480; cfg.LogSystemCfg.NoBuffer = false; cfg.LogSystemCfg.NoSync = true; cfg.LogSystemCfg.ZeroOnCreate = true; cfg.LogSystemCfg.InMemory = true; cfg.LogSystemCfg.LogExternalFileContent = true; DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); BTreeDatabase db; try { Transaction openTxn = env.BeginTransaction(); try { BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn); openTxn.Commit(); } catch (DatabaseException e) { openTxn.Abort(); throw e; } Transaction writeTxn = env.BeginTransaction(); try { /* * Writing 10 large records into in-memory logging * database should throw FullLogBufferException since * the amount of put data is larger than buffer size. */ byte[] byteArr = new byte[204800]; for (int i = 0; i < 10; i++) { db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), new DatabaseEntry(byteArr), writeTxn); } writeTxn.Commit(); } catch (Exception e) { writeTxn.Abort(); throw e; } finally { db.Close(true); } } catch (FullLogBufferException e) { Assert.AreEqual(ErrorCodes.DB_LOG_BUFFER_FULL, e.ErrorCode); throw new ExpectedTestException(); } finally { env.Close(); } }
public static int excs_btrec(string dbFileName, string dbName, string wordFile) { BTreeCursor dbc; BTreeDatabase btreeDB; BTreeDatabaseConfig btreeDBconfig; DatabaseEntry key, data; KeyValuePair <DatabaseEntry, DatabaseEntry> pair; StreamReader wordList; string buff; char[] rbuff; int i, j; uint cnt; string progName = "excs_btrec"; /* Check if word list exists. */ if (!File.Exists(wordFile)) { Console.WriteLine("Cannot find {0}/{1}.", Environment.CurrentDirectory, wordFile); return(1); } /* Optionally remove the previous database. */ if (File.Exists(dbFileName)) { while (true) { Console.Write ("Database already exists, delete or not (y/n)?"); buff = Console.ReadLine(); if (buff == "y" || buff == "n") { break; } } if (buff == "y") { File.Delete(dbFileName); Console.WriteLine("The existing {0} is deleted", dbName); } } /* Create and initialize database object, open the database. */ btreeDBconfig = new BTreeDatabaseConfig(); btreeDBconfig.Creation = CreatePolicy.IF_NEEDED; btreeDBconfig.ErrorPrefix = progName; btreeDBconfig.UseRecordNumbers = true; try { btreeDB = BTreeDatabase.Open(dbFileName, dbName, btreeDBconfig); } catch { Console.WriteLine("Database can not be opened"); return(1); } /* * Insert records into the database, where the key is the word * preceded by its record number, and the data is the same, * but in reverse order. */ key = new DatabaseEntry(); data = new DatabaseEntry(); wordList = new StreamReader(wordFile); for (cnt = 1; cnt <= 1000; cnt++) { /* Read one word from word list. */ try { buff = wordList.ReadLine(); } catch { Console.WriteLine("Error when reading word list"); btreeDB.Close(); wordList.Close(); return(1); } /* * Prefix the word with record number and store * them in key. */ try { buff = cnt.ToString("0000") + buff; } catch { Console.WriteLine( "Wrong format for record number."); btreeDB.Close(); wordList.Close(); return(1); } dbtFromString(key, buff); /* Reverse buff to rbuff and store them in data. */ rbuff = new char[buff.Length]; for (i = 0, j = buff.Length - 1; i < buff.Length; i++, j--) { rbuff[i] = buff[j]; } dbtFromString(data, new string(rbuff)); /* Put the key/data pair into database. */ try { btreeDB.PutNoOverwrite(key, data); } catch { Console.WriteLine("Put error with key::{0} and" + "data::{1}", buff, new string(rbuff)); btreeDB.Close(); wordList.Close(); return(1); } } /* Close the word list. */ wordList.Close(); /* Acquire a cursor for the database. */ dbc = btreeDB.Cursor(); /* * Input an available record number and get its * corresponding key/data pair in the database. */ while (true) { Console.WriteLine("Please input record number" + ", input 0 to stop"); while (true) { Console.WriteLine("recno #:\t"); buff = Console.ReadLine(); try { cnt = uint.Parse(buff); break; } catch { Console.WriteLine("Invalid Record Number. " + "Try Again."); } } if (cnt == 0) { break; } /* * Position the cursor at a key/data pair in the database * where the record number points to. */ if (!dbc.Move(cnt)) { Console.WriteLine("No record is found"); break; } /* Get the current key/data pair and display them.*/ pair = dbc.Current; Console.WriteLine("key::{0, 18:G} data::{1, 18:G}", strFromDBT(pair.Key), strFromDBT(pair.Value)); /* Check if it's the right record number. */ if (dbc.Recno() != cnt) { Console.WriteLine("The current record is not" + "the right one of the given record number."); btreeDB.Close(); dbc.Close(); return(1); } /* Get the next record. */ if (!dbc.MoveNext()) { Console.WriteLine( "Next record is not found"); break; } /* Get the current key/data pair and display them.*/ pair = dbc.Current; Console.WriteLine("key::{0, 18:G} data::{1, 18:G}\n", strFromDBT(pair.Key), strFromDBT(pair.Value)); } /* Close cursor and database. */ dbc.Close(); btreeDB.Close(); return(0); }
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.LogExternalFileContent = false; cfg.LogSystemCfg.MaxFileSize = 1048576; cfg.LogSystemCfg.NoBuffer = false; cfg.LogSystemCfg.NoSync = true; cfg.LogSystemCfg.RegionSize = 204800; cfg.LogSystemCfg.ZeroOnCreate = true; DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); LogStats stats = env.LoggingSystemStats(); env.Msgfile = testHome + "/" + testName + ".log"; 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.AreEqual(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(); }
private void Open() { Console.WriteLine("Opening environment and database"); // Set up the environment. DatabaseEnvironmentConfig envCfg = new DatabaseEnvironmentConfig(); envCfg.Create = true; envCfg.UseMPool = true; envCfg.UseLocking = true; envCfg.UseLogging = true; envCfg.UseTxns = true; // Allow multiple threads visit to the environment handle. envCfg.FreeThreaded = true; if (inMem) { envCfg.Private = true; } else { envCfg.RunRecovery = true; } /* * Indicate that we want db to internally perform * deadlock detection, aborting the transaction that * has performed the least amount of WriteData activity * in the event of a deadlock. */ envCfg.LockSystemCfg = new LockingConfig(); envCfg.LockSystemCfg.DeadlockResolution = DeadlockPolicy.MIN_WRITE; if (inMem) { // Specify in-memory logging. envCfg.LogSystemCfg = new LogConfig(); envCfg.LogSystemCfg.InMemory = true; /* * Specify the size of the in-memory log buffer * Must be large enough to handle the log data * created by the largest transaction. */ envCfg.LogSystemCfg.BufferSize = 10 * 1024 * 1024; /* * Specify the size of the in-memory cache, * large enough to avoid paging to disk. */ envCfg.MPoolSystemCfg = new MPoolConfig(); envCfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 10 * 1024 * 1024, 1); } // Set up the database. BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); dbCfg.AutoCommit = true; dbCfg.Creation = CreatePolicy.IF_NEEDED; dbCfg.Duplicates = DuplicatesPolicy.SORTED; dbCfg.FreeThreaded = true; dbCfg.ReadUncommitted = true; /* * Open the environment. Any errors will be caught * by the caller. */ env = DatabaseEnvironment.Open(home, envCfg); /* * Open the database. Do not provide a txn handle. This * Open is autocommitted because BTreeDatabaseConfig.AutoCommit * is true. */ dbCfg.Env = env; db = BTreeDatabase.Open(dbName, dbCfg); }
static void Main(string[] args) { BTreeDatabase btreeDB; BTreeDatabaseConfig btreeConfig; Cursor dbc; DatabaseEntry key; DatabaseEntry data; string buff, dbFileName, keyString; /* * excs_access is meant to be run from build_windows\AnyCPU, in either * the Debug or Release directory. The required core libraries, * however, are in either build_windows\Win32 or build_windows\x64, * depending upon the platform. That location needs to be added * to the PATH environment variable for the P/Invoke calls to work. */ try { String pwd = Environment.CurrentDirectory; pwd = Path.Combine(pwd, ".."); pwd = Path.Combine(pwd, ".."); if (IntPtr.Size == 4) { pwd = Path.Combine(pwd, "Win32"); } else { pwd = Path.Combine(pwd, "x64"); } #if DEBUG pwd = Path.Combine(pwd, "Debug"); #else pwd = Path.Combine(pwd, "Release"); #endif pwd += ";" + Environment.GetEnvironmentVariable("PATH"); Environment.SetEnvironmentVariable("PATH", pwd); } catch (Exception e) { Console.WriteLine( "Unable to set the PATH environment variable."); Console.WriteLine(e.Message); return; } try { dbFileName = "access.db"; if (args.Length > 0) { dbFileName = args[0]; } } catch { usage(); return; } /* Optiionally remove the existing database file. */ if (File.Exists(dbFileName)) { while (true) { Console.Write ("{0} already exists. Delete it? (y/n) ", dbFileName); buff = Console.ReadLine().ToLower(); if (buff == "y" || buff == "n") { break; } } if (buff == "y") { try { File.Delete(dbFileName); } catch { Console.WriteLine("Unable to delete {0}.", dbFileName); return; } } } /* Configure the database. */ btreeConfig = new BTreeDatabaseConfig(); btreeConfig.Duplicates = DuplicatesPolicy.SORTED; btreeConfig.ErrorPrefix = "excs_access"; btreeConfig.Creation = CreatePolicy.IF_NEEDED; btreeConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1); btreeConfig.PageSize = 8 * 1024; /* Create and open a new database in the file. */ try { btreeDB = BTreeDatabase.Open(dbFileName, btreeConfig); } catch (Exception e) { Console.WriteLine("Error opening {0}.", dbFileName); Console.WriteLine(e.Message); return; } /* Input key/data pairs and insert them into the database.*/ key = new DatabaseEntry(); data = new DatabaseEntry(); while (true) { Console.Write("key [blank line to quit] > "); keyString = Console.ReadLine(); if (keyString == "") { break; } dbtFromString(key, keyString); dbtFromString(data, reverse(keyString)); try { btreeDB.Put(key, data); } catch { return; } } /* Acquire a cursor for the database. */ using (dbc = btreeDB.Cursor()) { /* Walk through the database and print out key/data pairs. */ Console.WriteLine("All key : data pairs:"); foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> p in dbc) { Console.WriteLine("{0}::{1}", strFromDBT(p.Key), strFromDBT(p.Value)); } } Console.Write("Press any key to exit >"); Console.ReadKey(true); /* Close the cursor and database. */ btreeDB.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.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; Thread thread1 = new Thread(new ThreadStart(Write)); Thread thread2 = new Thread(new ThreadStart(Read)); thread1.Start(); Thread.Sleep(10); thread2.Start(); thread1.Join(); thread2.Join(); env.PrintLockingSystemStats(); stats = env.LockingSystemStats(); Assert.Less(0, stats.LastAllocatedLockerID); Assert.Less(0, stats.LockConflictsNoWait); Assert.LessOrEqual(0, stats.LockConflictsWait); Assert.LessOrEqual(0, stats.LockDeadlocks); 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.Less(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.Less(0, stats.TxnTimeouts); db.Close(); env.Close(); }
public MyDbs(string databaseHome) { vDbName = "vendordb.db"; iDbName = "inventorydb.db"; itemSDbName = "itemname.sdb"; if (databaseHome != null) { vDbName = databaseHome + "\\" + vDbName; iDbName = databaseHome + "\\" + iDbName; itemSDbName = databaseHome + "\\" + itemSDbName; } btreeConfig = new BTreeDatabaseConfig(); btreeConfig.Creation = CreatePolicy.IF_NEEDED; btreeConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1); btreeConfig.ErrorPrefix = "excs_getting_started"; btreeConfig.PageSize = 8 * 1024; /* Optionally remove existing database files. */ try { RemoveDbFile(vDbName); RemoveDbFile(iDbName); RemoveDbFile(itemSDbName); } catch (Exception e) { Console.WriteLine("Error deleting db."); Console.WriteLine(e.Message); throw e; } /* Create and open the Inventory and Vendor database files. */ try { vbtreeDB = BTreeDatabase.Open(vDbName, btreeConfig); } catch (Exception e) { Console.WriteLine("Error opening {0}.", vDbName); Console.WriteLine(e.Message); throw e; } try { ibtreeDB = BTreeDatabase.Open(iDbName, btreeConfig); } catch (Exception e) { Console.WriteLine("Error opening {0}.", iDbName); Console.WriteLine(e.Message); throw e; } /* * Open a secondary btree database associated with the * Inventory database. */ try { itemSecbtreeConfig = new SecondaryBTreeDatabaseConfig( ibtreeDB, new SecondaryKeyGenDelegate( CreateSecondaryKey)); itemSecbtreeConfig.Creation = CreatePolicy.IF_NEEDED; itemSecbtreeConfig.Duplicates = DuplicatesPolicy.UNSORTED; itemSecbtreeDB = SecondaryBTreeDatabase.Open( itemSDbName, itemSecbtreeConfig); } catch (Exception e) { Console.WriteLine("Error opening secondary {0}", itemSDbName); Console.WriteLine(e.Message); throw e; } }
/* Initialize environment and database (s) */ public void InitDbs() { /* Open transactional environment */ DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseMPool = true; envConfig.UseLocking = true; envConfig.UseLogging = true; envConfig.UseTxns = true; envConfig.LockSystemCfg = new LockingConfig(); envConfig.LockSystemCfg.MaxLocks = (this.dups == 0) ? (uint)this.num : (uint)(this.num * this.dups); envConfig.LockSystemCfg.MaxObjects = envConfig.LockSystemCfg.MaxLocks; if (this.cachesize != 0) { envConfig.MPoolSystemCfg = new MPoolConfig(); envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(0, this.cachesize, 1); } try { env = DatabaseEnvironment.Open(home, envConfig); } catch (Exception e) { Console.WriteLine(e.StackTrace); System.Environment.Exit(1); } Transaction txn = env.BeginTransaction(); try { /* Open primary db in transaction */ BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Env = env; dbConfig.Creation = CreatePolicy.IF_NEEDED; if (this.pagesize != 0) { dbConfig.PageSize = this.pagesize; } if (this.dups != 0) { dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; } pdb = BTreeDatabase.Open(dbFileName, pDbName, dbConfig, txn); /* Open secondary db in transaction */ if (this.secondary) { SecondaryBTreeDatabaseConfig sdbConfig = new SecondaryBTreeDatabaseConfig( pdb, new SecondaryKeyGenDelegate(SecondaryKeyGen)); sdbConfig.Creation = CreatePolicy.IF_NEEDED; if (this.pagesize != 0) { sdbConfig.PageSize = this.pagesize; } sdbConfig.Duplicates = DuplicatesPolicy.SORTED; sdbConfig.Env = env; sdb = SecondaryBTreeDatabase.Open( dbFileName, sDbName, sdbConfig, txn); } txn.Commit(); } catch (DatabaseException e1) { txn.Abort(); throw e1; } catch (FileNotFoundException e2) { txn.Abort(); throw e2; } }
public void TestDuplicate() { testName = "TestDuplicate"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; string dbSecFileName = testHome + "/" + testName + "_sec.db"; // 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 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 TestMoveJoinCursor() { testName = "TestMoveJoinCursor"; testHome = testFixtureHome + "/" + testName; string dbFileName = testHome + "/" + testName + ".db"; string secFileName1 = testHome + "/" + "sec_" + testName + "1.db"; string secFileName2 = testHome + "/" + "sec_" + testName + "2.db"; Configuration.ClearDir(testHome); // Open a primary database. BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); dbCfg.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase db = BTreeDatabase.Open(dbFileName, dbCfg); /* * Open a secondary database on high byte of * its data and another secondary database on * little byte of its data. */ SecondaryBTreeDatabase secDB1, secDB2; SecondaryCursor[] cursors = new SecondaryCursor[2]; byte[] byteValue = new byte[1]; byteValue[0] = 0; GetSecCursor(db, secFileName1, new SecondaryKeyGenDelegate(KeyGenOnBigByte), out secDB1, out cursors[0], false, new DatabaseEntry(byteValue)); byteValue[0] = 1; GetSecCursor(db, secFileName2, new SecondaryKeyGenDelegate(KeyGenOnLittleByte), out secDB2, out cursors[1], true, new DatabaseEntry(byteValue)); // Get join cursor. JoinCursor joinCursor = db.Join(cursors, true); /* * MoveNextJoinItem do not use data value found * in all of the cursor so the data in Current won't be * changed. */ Assert.IsTrue(joinCursor.MoveNextItem()); Assert.AreEqual(0, joinCursor.Current.Key.Data[ joinCursor.Current.Key.Data.Length - 1]); Assert.AreEqual(1, joinCursor.Current.Key.Data[0]); Assert.IsNull(joinCursor.Current.Value.Data); // Iterate on join cursor. foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> pair in joinCursor) { /* * Confirm that the key got by join cursor has 0 at * its highest byte and 1 at its lowest byte. */ Assert.AreEqual(0, pair.Key.Data[pair.Key.Data.Length - 1]); Assert.AreEqual(1, pair.Key.Data[0]); } Assert.IsFalse(joinCursor.MoveNext()); // Close all. joinCursor.Close(); cursors[0].Close(); cursors[1].Close(); secDB1.Close(); secDB2.Close(); db.Close(); }
public void OpenSecDBWithinTxn(string home, string dbFileName, string dbSecFileName, bool ifDbName) { // Open an environment. DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseTxns = true; envConfig.UseMPool = true; envConfig.UseLogging = true; envConfig.UseLocking = true; DatabaseEnvironment env = DatabaseEnvironment.Open( home, envConfig); // Open a primary btree database. Transaction openDBTxn = env.BeginTransaction(); BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; BTreeDatabase db = BTreeDatabase.Open( dbFileName, dbConfig, openDBTxn); // Open a secondary btree database. SecondaryBTreeDatabaseConfig secDBConfig = new SecondaryBTreeDatabaseConfig(db, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secDBConfig.Env = env; secDBConfig.Creation = CreatePolicy.IF_NEEDED; SecondaryBTreeDatabase secDB; if (ifDbName == false) { secDB = SecondaryBTreeDatabase.Open( dbSecFileName, secDBConfig, openDBTxn); } else { secDB = SecondaryBTreeDatabase.Open( dbSecFileName, "secondary", secDBConfig, openDBTxn); } openDBTxn.Commit(); secDB.Close(); // Open the existing secondary database. Transaction secTxn = env.BeginTransaction(); SecondaryDatabaseConfig secConfig = new SecondaryDatabaseConfig(db, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secConfig.Env = env; SecondaryDatabase secExDB; if (ifDbName == false) { secExDB = SecondaryBTreeDatabase.Open( dbSecFileName, secConfig, secTxn); } else { secExDB = SecondaryBTreeDatabase.Open( dbSecFileName, "secondary", secConfig, secTxn); } secExDB.Close(); secTxn.Commit(); db.Close(); env.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 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 TestReplicationView() { testName = "TestReplicationView"; SetUpTest(true); string masterHome = testHome + "\\Master"; Configuration.ClearDir(masterHome); string clientHome1 = testHome + "\\Client1"; Configuration.ClearDir(clientHome1); string clientHome2 = testHome + "\\Client2"; Configuration.ClearDir(clientHome2); ports.Clear(); AvailablePorts portGen = new AvailablePorts(); uint mPort = portGen.Current; portGen.MoveNext(); uint cPort1 = portGen.Current; portGen.MoveNext(); uint cPort2 = portGen.Current; /* Open environment with replication configuration. */ DatabaseEnvironmentConfig cfg = new DatabaseEnvironmentConfig(); cfg.Create = true; cfg.RunRecovery = true; cfg.UseLocking = true; cfg.UseLogging = true; cfg.UseMPool = true; cfg.UseReplication = true; cfg.FreeThreaded = true; cfg.UseTxns = true; cfg.EventNotify = new EventNotifyDelegate(stuffHappened); cfg.RepSystemCfg = new ReplicationConfig(); cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); cfg.RepSystemCfg.RepmgrSitesConfig[0].Host = "127.0.0.1"; cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = mPort; cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true; cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = true; cfg.RepSystemCfg.Priority = 100; /* Start up the master site. */ DatabaseEnvironment mEnv = DatabaseEnvironment.Open( masterHome, cfg); mEnv.DeadlockResolution = DeadlockPolicy.DEFAULT; mEnv.RepMgrStartMaster(2); /* Open the environment of the client 1 site. */ cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = cPort1; cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = false; cfg.RepSystemCfg.Priority = 10; cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); cfg.RepSystemCfg.RepmgrSitesConfig[1].Host = "127.0.0.1"; cfg.RepSystemCfg.RepmgrSitesConfig[1].Port = mPort; cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true; /* Set the site as a partial view. */ cfg.RepSystemCfg.ReplicationView = repView; DatabaseEnvironment cEnv1 = DatabaseEnvironment.Open( clientHome1, cfg); /* Open the environment of the client 2 site. */ cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = cPort2; cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = false; cfg.RepSystemCfg.Priority = 10; /* Set the site as a full view. */ cfg.RepSystemCfg.ReplicationView = null; DatabaseEnvironment cEnv2 = DatabaseEnvironment.Open( clientHome2, cfg); /* * Create two database files db1.db and db2.db * on the master. */ BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Env = mEnv; btreeDBConfig.Creation = CreatePolicy.ALWAYS; btreeDBConfig.AutoCommit = true; BTreeDatabase db1 = BTreeDatabase.Open("db1.db", btreeDBConfig); BTreeDatabase db2 = BTreeDatabase.Open("db2.db", btreeDBConfig); db1.Close(); db2.Close(); /* Start up the client sites. */ cEnv1.RepMgrStartClient(2, false); cEnv2.RepMgrStartClient(2, false); /* Wait for clients to start up */ int i = 0; while (!cEnv1.ReplicationSystemStats().ClientStartupComplete) { if (i < 20) { Thread.Sleep(1000); i++; } else { throw new TestException(); } } i = 0; while (!cEnv2.ReplicationSystemStats().ClientStartupComplete) { if (i < 20) { Thread.Sleep(1000); i++; } else { throw new TestException(); } } /* * Verify that the file db2.db is replicated to the * client 2 (full view), but not to the client 1 * (partial view), and the file db1.db is * replicated to both sites. */ btreeDBConfig.Env = cEnv1; btreeDBConfig.Creation = CreatePolicy.NEVER; db1 = BTreeDatabase.Open("db1.db", btreeDBConfig); try { db2 = BTreeDatabase.Open("db2.db", btreeDBConfig); throw new TestException(); } catch (DatabaseException e) { Assert.AreEqual(0, String.Compare( "No such file or directory", e.Message)); } db1.Close(); btreeDBConfig.Env = cEnv2; db1 = BTreeDatabase.Open("db1.db", btreeDBConfig); db2 = BTreeDatabase.Open("db2.db", btreeDBConfig); db1.Close(); db2.Close(); /* Get the replication manager statistic. */ RepMgrStats repMgrStats = mEnv.RepMgrSystemStats(); Assert.AreEqual(1, repMgrStats.ParticipantSites); Assert.AreEqual(3, repMgrStats.TotalSites); Assert.AreEqual(2, repMgrStats.ViewSites); /* * Verify the master is not a view locally * or from remote site. */ ReplicationStats repstats = mEnv.ReplicationSystemStats(); Assert.AreEqual(false, repstats.View); RepMgrSite[] rsite = cEnv1.RepMgrRemoteSites; Assert.AreEqual(2, rsite.Length); for (i = 0; i < rsite.Length; i++) { if (rsite[i].Address.Port == mPort) { break; } } Assert.Greater(rsite.Length, i); Assert.AreEqual(false, rsite[i].isView); /* * Verify the clients are views locally * and from remote site. */ rsite = mEnv.RepMgrRemoteSites; Assert.AreEqual(2, rsite.Length); Assert.AreEqual(true, rsite[0].isView); Assert.AreEqual(true, rsite[1].isView); repstats = cEnv1.ReplicationSystemStats(); Assert.AreEqual(true, repstats.View); repstats = cEnv2.ReplicationSystemStats(); Assert.AreEqual(true, repstats.View); cEnv2.Close(); cEnv1.Close(); mEnv.Close(); }