public static void Config(XmlElement xmlElement, ref SecondaryBTreeDatabaseConfig secBtreeDBConfig, bool compulsory) { uint minKeysPerPage = new uint(); SecondaryDatabaseConfig secDBConfig = secBtreeDBConfig; SecondaryDatabaseConfigTest.Config(xmlElement, ref secDBConfig, compulsory); // Configure specific fields/properties of Btree db Configuration.ConfigCreatePolicy(xmlElement, "Creation", ref secBtreeDBConfig.Creation, compulsory); Configuration.ConfigDuplicatesPolicy(xmlElement, "Duplicates", ref secBtreeDBConfig.Duplicates, compulsory); if (Configuration.ConfigUint(xmlElement, "MinKeysPerPage", ref minKeysPerPage, compulsory)) secBtreeDBConfig.MinKeysPerPage = minKeysPerPage; Configuration.ConfigBool(xmlElement, "NoReverseSplitting", ref secBtreeDBConfig.NoReverseSplitting, compulsory); Configuration.ConfigBool(xmlElement, "UseRecordNumbers", ref secBtreeDBConfig.UseRecordNumbers, compulsory); }
/// <summary> /// Instantiate a new SecondaryBTreeDatabase object, open the /// database represented by <paramref name="Filename"/> and associate /// the database with the /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>. /// </summary> /// <remarks> /// <para> /// If both <paramref name="Filename"/> and /// <paramref name="DatabaseName"/> are null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. If /// <paramref name="Filename"/> is null and /// <paramref name="DatabaseName"/> is non-null, the database can be /// opened by other threads of control and be replicated to client /// sites in any replication group. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. The /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="DatabaseName"> /// This parameter allows applications to have multiple databases in a /// single file. Although no DatabaseName needs to be specified, it is /// an error to attempt to open a second database in a file that was not /// initially created using a database name. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryBTreeDatabase Open( string Filename, string DatabaseName, SecondaryBTreeDatabaseConfig cfg, Transaction txn) { SecondaryBTreeDatabase ret = new SecondaryBTreeDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, DatabaseName, cfg.DbType.getDBTYPE(), cfg.openFlags, 0); ret.isOpen = true; ret.doAssocRef = new BDB_AssociateDelegate( SecondaryDatabase.doAssociate); cfg.Primary.db.associate(Transaction.getDB_TXN(txn), ret.db, ret.doAssocRef, cfg.assocFlags); if (cfg.ForeignKeyDatabase != null) { if (cfg.OnForeignKeyDelete == ForeignKeyDeleteAction.NULLIFY) { ret.doNullifyRef = new BDB_AssociateForeignDelegate(doNullify); } else { ret.doNullifyRef = null; } cfg.ForeignKeyDatabase.db.associate_foreign( ret.db, ret.doNullifyRef, cfg.foreignFlags); } return(ret); }
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)); }
private void Config(SecondaryBTreeDatabaseConfig cfg) { base.Config((SecondaryDatabaseConfig)cfg); db.set_flags(cfg.flags); if (cfg.Compare != null) Compare = cfg.Compare; if (cfg.PrefixCompare != null) PrefixCompare = cfg.PrefixCompare; if (cfg.DuplicateCompare != null) DupCompare = cfg.DuplicateCompare; if (cfg.minkeysIsSet) db.set_bt_minkey(cfg.MinKeysPerPage); }
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(); }
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(); }
private void Config(SecondaryBTreeDatabaseConfig cfg) { base.Config((SecondaryDatabaseConfig)cfg); db.set_flags(cfg.flags); if (cfg.Compare != null) { Compare = cfg.Compare; } if (cfg.PrefixCompare != null) { PrefixCompare = cfg.PrefixCompare; } if (cfg.DuplicateCompare != null) { DupCompare = cfg.DuplicateCompare; } if (cfg.minkeysIsSet) { db.set_bt_minkey(cfg.MinKeysPerPage); } }
public static void Confirm(XmlElement xmlElement, SecondaryBTreeDatabaseConfig secBtreeDBConfig, bool compulsory) { SecondaryDatabaseConfig secDBConfig = secBtreeDBConfig; SecondaryDatabaseConfigTest.Confirm(xmlElement, secDBConfig, compulsory); // Confirm secondary btree database specific configuration. Configuration.ConfirmCreatePolicy(xmlElement, "Creation", secBtreeDBConfig.Creation, compulsory); Configuration.ConfirmDuplicatesPolicy(xmlElement, "Duplicates", secBtreeDBConfig.Duplicates, compulsory); Configuration.ConfirmUint(xmlElement, "MinKeysPerPage", secBtreeDBConfig.MinKeysPerPage, compulsory); Configuration.ConfirmBool(xmlElement, "NoReverseSplitting", secBtreeDBConfig.NoReverseSplitting, compulsory); Configuration.ConfirmBool(xmlElement, "UseRecordNumbers", secBtreeDBConfig.UseRecordNumbers, compulsory); }
public void OpenSecDB(string dbFileName, string dbSecFileName, out BTreeDatabase db, out SecondaryBTreeDatabase secDB) { // Open a primary database. BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; 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.SORTED; secDB = SecondaryBTreeDatabase.Open(dbSecFileName, secConfig); }
public void OpenSecDBInTxn(string home, string dbFileName, string dbSecFileName, out DatabaseEnvironment env, out BTreeDatabase db, out SecondaryBTreeDatabase secDB) { // Open environment. DatabaseEnvironmentConfig envCfg = new DatabaseEnvironmentConfig(); envCfg.Create = true; envCfg.UseLocking = true; envCfg.UseLogging = true; envCfg.UseMPool = true; envCfg.UseTxns = true; env = DatabaseEnvironment.Open( home, envCfg); // Open primary and secondary database in a transaction. Transaction openTxn = env.BeginTransaction(); BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.Env = env; dbConfig.PageSize = 4096; dbConfig.Duplicates = DuplicatesPolicy.NONE; dbConfig.ReadUncommitted = true; db = BTreeDatabase.Open(dbFileName, dbConfig, openTxn); openTxn.Commit(); openTxn = env.BeginTransaction(); SecondaryBTreeDatabaseConfig secConfig = new SecondaryBTreeDatabaseConfig(db, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secConfig.Creation = CreatePolicy.IF_NEEDED; secConfig.Duplicates = DuplicatesPolicy.SORTED; secConfig.Env = env; secConfig.ReadUncommitted = true; secDB = SecondaryBTreeDatabase.Open(dbSecFileName, secConfig, openTxn); openTxn.Commit(); }
public void TestDuplicates() { testName = "TestDuplicates"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; string dbSecFileName = testHome + "/" + testName + "_sec.db"; // Open a primary btree database. BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase btreeDB = BTreeDatabase.Open( dbFileName, btreeDBConfig); // Open a secondary btree database. SecondaryBTreeDatabaseConfig secBtreeDBConfig = new SecondaryBTreeDatabaseConfig(btreeDB, null); secBtreeDBConfig.Creation = CreatePolicy.IF_NEEDED; secBtreeDBConfig.Duplicates = DuplicatesPolicy.SORTED; SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open(dbSecFileName, secBtreeDBConfig); Assert.AreEqual(DuplicatesPolicy.SORTED, secDB.Duplicates); secDB.Close(); btreeDB.Close(); }
/// <summary> /// Instantiate a new SecondaryBTreeDatabase object, open the /// database represented by <paramref name="Filename"/> and associate /// the database with the /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>. /// </summary> /// <remarks> /// <para> /// If <paramref name="Filename"/> is null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. /// </para> /// <para> /// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="cfg">The database's configuration</param> /// <returns>A new, open database object</returns> public static SecondaryBTreeDatabase Open( string Filename, SecondaryBTreeDatabaseConfig cfg) { return(Open(Filename, null, cfg, null)); }
/// <summary> /// Instantiate a new SecondaryBTreeDatabase object, open the /// database represented by <paramref name="Filename"/> and associate /// the database with the /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>. /// </summary> /// <remarks> /// <para> /// If <paramref name="Filename"/> is null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. The /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryBTreeDatabase Open(string Filename, SecondaryBTreeDatabaseConfig cfg, Transaction txn) { return(Open(Filename, null, cfg, txn)); }
public void TestDuplicates() { testName = "TestDuplicates"; testHome = testFixtureHome + "/" + testName; string dbFileName = testHome + "/" + testName + ".db"; string dbSecFileName = testHome + "/" + testName + "_sec.db"; Configuration.ClearDir(testHome); // Open a primary btree database. BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase btreeDB = BTreeDatabase.Open( dbFileName, btreeDBConfig); // Open a secondary btree database. SecondaryBTreeDatabaseConfig secBtreeDBConfig = new SecondaryBTreeDatabaseConfig(btreeDB, null); secBtreeDBConfig.Creation = CreatePolicy.IF_NEEDED; secBtreeDBConfig.Duplicates = DuplicatesPolicy.SORTED; SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open(dbSecFileName, secBtreeDBConfig); secDB.Close(); btreeDB.Close(); }
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 TestConfig() { testName = "TestConfig"; testHome = testFixtureHome + "/" + testName; string dbFileName = testHome + "/" + testName + ".db"; Configuration.ClearDir(testHome); XmlElement xmlElem = Configuration.TestSetUp( testFixtureName, testName); // Open a primary btree database. BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase btreeDB = BTreeDatabase.Open( dbFileName, btreeDBConfig); SecondaryBTreeDatabaseConfig secDBConfig = new SecondaryBTreeDatabaseConfig(btreeDB, null); Config(xmlElem, ref secDBConfig, true); Confirm(xmlElem, secDBConfig, true); // Close the primary btree database. btreeDB.Close(); }
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 TestDupCompare() { testName = "TestDupCompare"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); cfg.Creation = CreatePolicy.ALWAYS; BTreeDatabase pDb = BTreeDatabase.Open( dbFileName, "p", cfg); SecondaryBTreeDatabaseConfig sCfg = new SecondaryBTreeDatabaseConfig(pDb, new SecondaryKeyGenDelegate(SecondaryKeyGen)); sCfg.Creation = CreatePolicy.IF_NEEDED; DatabaseEntry dbt1, dbt2; dbt1 = new DatabaseEntry( BitConverter.GetBytes((Int32)1)); dbt2 = new DatabaseEntry( BitConverter.GetBytes((Int64)4294967297)); // Do not set the duplicate comparison delegate. using (SecondaryBTreeDatabase sDb = SecondaryBTreeDatabase.Open(dbFileName, "s1", sCfg)) { try { int ret = sDb.DupCompare(dbt1, dbt2); throw new TestException(); } catch (NullReferenceException) { } } sCfg.DuplicateCompare = new EntryComparisonDelegate( SecondaryEntryComparison); using (SecondaryBTreeDatabase sDb = SecondaryBTreeDatabase.Open(dbFileName, "s2", sCfg)) { /* * Get the dup compare function set in the * configuration and run it in a comparison to * see if it is alright. */ int ret = 1 - BitConverter.ToInt32( BitConverter.GetBytes((Int64)4294967297), 0); Assert.AreEqual(ret, sDb.DupCompare(dbt1, dbt2)); } pDb.Close(); }
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 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 TestOpen() { testName = "TestOpen"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; string dbSecFileName = testHome + "/" + testName + "_sec.db"; XmlElement xmlElem = Configuration.TestSetUp( testFixtureName, testName); // Open a primary btree database. BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; BTreeDatabase btreeDB = BTreeDatabase.Open( dbFileName, btreeDBConfig); // Open a secondary btree database. SecondaryBTreeDatabaseConfig secDBConfig = new SecondaryBTreeDatabaseConfig(btreeDB, null); SecondaryBTreeDatabaseConfigTest.Config(xmlElem, ref secDBConfig, true); SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open(dbSecFileName, secDBConfig); // Confirm its flags configured in secDBConfig. Confirm(xmlElem, secDB, true); secDB.Close(); btreeDB.Close(); }
public void TestPrefixCompare() { 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.Compare = new EntryComparisonDelegate( SecondaryEntryComparison); secBtreeDBConfig.PrefixCompare = new EntryPrefixComparisonDelegate( SecondaryPrefixEntryComparison); secBtreeDBConfig.KeyGen = new SecondaryKeyGenDelegate( SecondaryKeyGen); SecondaryBTreeDatabase secDB = SecondaryBTreeDatabase.Open( dbFileName, secBtreeDBConfig); /* * Get the prefix compare function set in the * configuration and run it in a comparison to * see if it is alright. */ EntryPrefixComparisonDelegate cmp = secDB.PrefixCompare; DatabaseEntry dbt1, dbt2; dbt1 = new DatabaseEntry( BitConverter.GetBytes((Int32)1)); dbt2 = new DatabaseEntry( BitConverter.GetBytes((Int64)4294967297)); Assert.AreEqual(5, cmp(dbt1, dbt2)); secDB.Close(); btreeDB.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 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 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(); } }
/// <summary> /// Instantiate a new SecondaryBTreeDatabase object, open the /// database represented by <paramref name="Filename"/> and associate /// the database with the /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>. /// </summary> /// <remarks> /// <para> /// If both <paramref name="Filename"/> and /// <paramref name="DatabaseName"/> are null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. If /// <paramref name="Filename"/> is null and /// <paramref name="DatabaseName"/> is non-null, the database can be /// opened by other threads of control and will be replicated to client /// sites in any replication group. /// </para> /// <para> /// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// will be implicitly transaction protected. Note that transactionally /// protected operations on a datbase object requires the object itself /// be transactionally protected during its open. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file that will be used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="DatabaseName"> /// This parameter allows applications to have multiple databases in a /// single file. Although no DatabaseName needs to be specified, it is /// an error to attempt to open a second database in a file that was not /// initially created using a database name. /// </param> /// <param name="cfg">The database's configuration</param> /// <returns>A new, open database object</returns> public static SecondaryBTreeDatabase Open(string Filename, string DatabaseName, SecondaryBTreeDatabaseConfig cfg) { return Open(Filename, DatabaseName, cfg, null); }
public void TestKeyGen() { testName = "TestKeyGen"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; // 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(); }
/// <summary> /// Instantiate a new SecondaryBTreeDatabase object, open the /// database represented by <paramref name="Filename"/> and associate /// the database with the /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>. /// </summary> /// <remarks> /// <para> /// If <paramref name="Filename"/> is null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will /// be implicitly transaction protected. Note that transactionally /// protected operations on a datbase object requires the object itself /// be transactionally protected during its open. Also note that the /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file that will be used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryBTreeDatabase Open(string Filename, SecondaryBTreeDatabaseConfig cfg, Transaction txn) { return Open(Filename, null, cfg, txn); }
public void TestSecondaryCursor() { testName = "TestSecondaryCursor"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; // 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(); }
/// <summary> /// Instantiate a new SecondaryBTreeDatabase object, open the /// database represented by <paramref name="Filename"/> and associate /// the database with the /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>. /// </summary> /// <remarks> /// <para> /// If both <paramref name="Filename"/> and /// <paramref name="DatabaseName"/> are null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. If /// <paramref name="Filename"/> is null and /// <paramref name="DatabaseName"/> is non-null, the database can be /// opened by other threads of control and will be replicated to client /// sites in any replication group. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will /// be implicitly transaction protected. Note that transactionally /// protected operations on a datbase object requires the object itself /// be transactionally protected during its open. Also note that the /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file that will be used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="DatabaseName"> /// This parameter allows applications to have multiple databases in a /// single file. Although no DatabaseName needs to be specified, it is /// an error to attempt to open a second database in a file that was not /// initially created using a database name. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryBTreeDatabase Open( string Filename, string DatabaseName, SecondaryBTreeDatabaseConfig cfg, Transaction txn) { SecondaryBTreeDatabase ret = new SecondaryBTreeDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, DatabaseName, cfg.DbType.getDBTYPE(), cfg.openFlags, 0); ret.isOpen = true; ret.doAssocRef = new BDB_AssociateDelegate( SecondaryDatabase.doAssociate); cfg.Primary.db.associate(Transaction.getDB_TXN(txn), ret.db, ret.doAssocRef, cfg.assocFlags); if (cfg.ForeignKeyDatabase != null) { if (cfg.OnForeignKeyDelete == ForeignKeyDeleteAction.NULLIFY) ret.doNullifyRef = new BDB_AssociateForeignDelegate(doNullify); else ret.doNullifyRef = null; cfg.ForeignKeyDatabase.db.associate_foreign( ret.db, ret.doNullifyRef, cfg.foreignFlags); } return ret; }
public void OpenPrimaryAndSecondaryDB(string dbFileName, out BTreeDatabase primaryDB, out SecondaryBTreeDatabase secDB) { // Open primary database. BTreeDatabaseConfig primaryDBConfig = new BTreeDatabaseConfig(); primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; primaryDB = BTreeDatabase.Open(dbFileName, primaryDBConfig); // Open secondary database. SecondaryBTreeDatabaseConfig secDBConfig = new SecondaryBTreeDatabaseConfig(primaryDB, new SecondaryKeyGenDelegate(SecondaryKeyGen)); secDB = SecondaryBTreeDatabase.Open(dbFileName, secDBConfig); }
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(); }