private void Config(HeapDatabaseConfig cfg) { base.Config(cfg); /* * Database.Config calls set_flags, but that doesn't get the Heap * specific flags. No harm in calling it again. */ db.set_flags(cfg.flags); if (cfg.BlobDir != null && cfg.Env == null) { db.set_blob_dir(cfg.BlobDir); } if (cfg.blobThresholdIsSet) { db.set_blob_threshold(cfg.BlobThreshold, 0); } if (cfg.maxSizeIsSet) { db.set_heapsize(cfg.MaxSizeGBytes, cfg.MaxSizeBytes); } if (cfg.regionszIsSet) { db.set_heap_regionsize(cfg.RegionSize); } }
public void GetCursur(string dbFileName, bool ifConfig) { HeapDatabaseConfig dbConfig = new HeapDatabaseConfig(); dbConfig.Creation = CreatePolicy.IF_NEEDED; HeapDatabase db = HeapDatabase.Open(dbFileName, dbConfig); HeapRecordId rid = db.Append( new DatabaseEntry(BitConverter.GetBytes((int)1))); Cursor cursor; CursorConfig cursorConfig = new CursorConfig(); cursorConfig.Priority = CachePriority.HIGH; if (ifConfig == false) cursor = db.Cursor(); else cursor = db.Cursor(cursorConfig); cursor.Add(new KeyValuePair<DatabaseEntry, DatabaseEntry>( new DatabaseEntry(rid.toArray()), new DatabaseEntry(BitConverter.GetBytes((int)2)))); Cursor dupCursor = cursor.Duplicate(false); Assert.IsNull(dupCursor.Current.Key); if (ifConfig) Assert.AreEqual(CachePriority.HIGH, dupCursor.Priority); dupCursor.Close(); cursor.Close(); db.Close(); }
public static void Confirm(XmlElement xmlElement, HeapDatabaseConfig heapDBConfig, bool compulsory) { DatabaseConfig dbConfig = heapDBConfig; Confirm(xmlElement, dbConfig, compulsory); // Confirm Heap database specific configuration Configuration.ConfirmCreatePolicy(xmlElement, "Creation", heapDBConfig.Creation, compulsory); }
public static void Config(XmlElement xmlElement, ref HeapDatabaseConfig heapDBConfig, bool compulsory) { DatabaseConfig dbConfig = heapDBConfig; Config(xmlElement, ref dbConfig, compulsory); // Configure specific fields/properties of Heap database Configuration.ConfigCreatePolicy(xmlElement, "Creation", ref heapDBConfig.Creation, compulsory); }
/// <summary> /// Instantiate a new HeapDatabase object and open the database /// represented by <paramref name="Filename"/>. /// </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 HeapDatabase Open( string Filename, HeapDatabaseConfig cfg, Transaction txn) { HeapDatabase ret = new HeapDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, null, DBTYPE.DB_HEAP, cfg.openFlags, 0); ret.isOpen = true; return(ret); }
public new void TestConfigWithoutEnv() { testName = "TestConfigWithoutEnv"; SetUpTest(false); XmlElement xmlElem = Configuration.TestSetUp( testFixtureName, testName); HeapDatabaseConfig heapDBConfig = new HeapDatabaseConfig(); Config(xmlElem, ref heapDBConfig, true); Confirm(xmlElem, heapDBConfig, true); }
private void Config(HeapDatabaseConfig cfg) { base.Config(cfg); /* * Database.Config calls set_flags, but that doesn't get the Heap * specific flags. No harm in calling it again. */ db.set_flags(cfg.flags); if (cfg.maxSizeIsSet) { db.set_heapsize(cfg.MaxSizeGBytes, cfg.MaxSizeBytes); } }
private void Config(HeapDatabaseConfig cfg) { base.Config(cfg); /* * Database.Config calls set_flags, but that does not get the Heap * specific flags. No harm in calling it again. */ db.set_flags(cfg.flags); if (cfg.BlobDir != null && cfg.Env == null) db.set_blob_dir(cfg.BlobDir); if (cfg.blobThresholdIsSet) db.set_blob_threshold(cfg.BlobThreshold, 0); if (cfg.maxSizeIsSet) db.set_heapsize(cfg.MaxSizeGBytes, cfg.MaxSizeBytes); if (cfg.regionszIsSet) db.set_heap_regionsize(cfg.RegionSize); }
public void TestOpenExistingHeapDB() { testName = "TestOpenExistingHeapDB"; SetUpTest(true); string heapDBFileName = testHome + "/" + testName + ".db"; HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); heapConfig.Creation = CreatePolicy.ALWAYS; HeapDatabase heapDB = HeapDatabase.Open( heapDBFileName, heapConfig); heapDB.Close(); DatabaseConfig dbConfig = new DatabaseConfig(); Database db = Database.Open(heapDBFileName, dbConfig); Assert.AreEqual(db.Type, DatabaseType.HEAP); db.Close(); }
public void TestHeapRegionsize() { /* * Check that with a very small region size, the number of regions * grows quickly. */ testName = "TestHeapRegionsize"; SetUpTest(true); Random r = new Random(); byte[] buf = new byte[1024]; r.NextBytes(buf); HeapDatabaseConfig cfg = new HeapDatabaseConfig(); cfg.Creation = CreatePolicy.ALWAYS; cfg.PageSize = 512; cfg.RegionSize = 4; HeapDatabase db = HeapDatabase.Open( testHome + "/" + testName + ".db", cfg); DatabaseEntry dbt = new DatabaseEntry(buf); for (int i = 0; i < 10; i++) db.Append(dbt); Assert.AreEqual(db.RegionSize, 4); HeapStats stats = db.Stats(); db.Close(); Assert.AreEqual(stats.RegionSize, 4); Assert.Greater(stats.nRegions, 1); }
public void TestExists() { testName = "TestExists"; SetUpTest(true); DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseLocking = true; envConfig.UseLogging = true; envConfig.UseMPool = true; envConfig.UseTxns = true; DatabaseEnvironment env = DatabaseEnvironment.Open( testHome, envConfig); HeapDatabase db; try { Transaction openTxn = env.BeginTransaction(); try { HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); heapConfig.Creation = CreatePolicy.IF_NEEDED; heapConfig.Env = env; db = HeapDatabase.Open(testName + ".db", heapConfig, openTxn); openTxn.Commit(); } catch (DatabaseException e) { openTxn.Abort(); throw e; } Transaction cursorTxn = env.BeginTransaction(); Cursor cursor; HeapRecordId rid; try { /* * Put a record into heap database with * cursor and abort the operation. */ rid = db.Append(new DatabaseEntry( ASCIIEncoding.ASCII.GetBytes("foo")), cursorTxn); cursor = db.Cursor(cursorTxn); KeyValuePair<DatabaseEntry, DatabaseEntry> pair; pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>( new DatabaseEntry(rid.toArray()), new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("bar"))); cursor.Add(pair); cursor.Close(); Assert.IsTrue(db.Exists( new DatabaseEntry(rid.toArray()), cursorTxn)); cursorTxn.Abort(); } catch (DatabaseException e) { cursorTxn.Abort(); db.Close(); throw e; } Transaction delTxn = env.BeginTransaction(); try { /* * The put operation is aborted in the heap * database so querying if the record still exists * throws KeyEmptyException. */ Assert.IsFalse(db.Exists( new DatabaseEntry(rid.toArray()), delTxn)); delTxn.Commit(); } catch (DatabaseException e) { delTxn.Abort(); throw e; } finally { db.Close(); } } finally { env.Close(); } }
public void TestMessageFile() { testName = "TestMessageFile"; SetUpTest(true); // Configure and open an environment. DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseMPool = true; DatabaseEnvironment env = DatabaseEnvironment.Open( testHome, envConfig); // Configure and open a database. HeapDatabaseConfig DBConfig = new HeapDatabaseConfig(); DBConfig.Env = env; DBConfig.Creation = CreatePolicy.IF_NEEDED; string DBFileName = testName + ".db"; HeapDatabase db = HeapDatabase.Open(DBFileName, DBConfig); // Confirm message file does not exist. string messageFile = testHome + "/" + "msgfile"; Assert.AreEqual(false, File.Exists(messageFile)); // Call set_msgfile() of db. db.Msgfile = messageFile; // Print db statistic to message file. db.PrintStats(true); // Confirm message file exists now. Assert.AreEqual(true, File.Exists(messageFile)); db.Msgfile = ""; string line = null; // Read the third line of message file. System.IO.StreamReader file = new System.IO.StreamReader(@"" + messageFile); line = file.ReadLine(); line = file.ReadLine(); line = file.ReadLine(); // Confirm the message file is not empty. Assert.AreEqual(line, "DB handle information:"); file.Close(); // Close database and environment. db.Close(); env.Close(); }
private void Config(HeapDatabaseConfig cfg) { base.Config(cfg); /* * Database.Config calls set_flags, but that doesn't get the Heap * specific flags. No harm in calling it again. */ db.set_flags(cfg.flags); if (cfg.maxSizeIsSet) db.set_heapsize(cfg.MaxSizeGBytes, cfg.MaxSizeBytes); }
public void TestPutHeap() { KeyValuePair<DatabaseEntry, DatabaseEntry> pair; testName = "TestPutHeap"; SetUpTest(true); string heapDBFileName = testHome + "/" + testName + ".db"; HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); heapConfig.Creation = CreatePolicy.ALWAYS; using (HeapDatabase heapDB = HeapDatabase.Open( heapDBFileName, heapConfig)) { DatabaseEntry data = new DatabaseEntry( BitConverter.GetBytes((int)1)); HeapRecordId rid = heapDB.Append(data); DatabaseEntry key = new DatabaseEntry(rid.toArray()); data = new DatabaseEntry(BitConverter.GetBytes((int)2)); heapDB.Put(key, data); pair = heapDB.GetBoth(key, data); } }
/* * Test the blob database with or without environment. * 1. Config and open the environment; * 2. Verify the environment blob configs; * 3. Config and open the database; * 4. Verify the database blob configs; * 5. Insert and verify some blob data by database methods; * 6. Verify the stats; * 7. Close all handles. * If "blobdbt" is true, set the data DatabaseEntry.Blob as * true, otherwise make the data DatabaseEntry reach the blob * threshold in size. */ void TestBlobHeapDatabase(uint env_threshold, string env_blobdir, uint db_threshold, string db_blobdir, bool blobdbt) { if (env_threshold == 0 && db_threshold == 0) return; testName = "TestBlob"; SetUpTest(true); string heapDBName = testHome + "/" + testName + ".db"; HeapDatabaseConfig cfg = new HeapDatabaseConfig(); cfg.Creation = CreatePolicy.ALWAYS; string blrootdir = "__db_bl"; // Open the environment and verify the blob configs. if (env_threshold > 0) { DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.AutoCommit = true; envConfig.Create = true; envConfig.UseMPool = true; envConfig.UseLogging = true; envConfig.UseTxns = true; envConfig.UseLocking = true; envConfig.BlobThreshold = env_threshold; if (env_blobdir != null) { envConfig.BlobDir = env_blobdir; blrootdir = env_blobdir; } DatabaseEnvironment env = DatabaseEnvironment.Open( testHome, envConfig); if (env_blobdir == null) Assert.IsNull(env.BlobDir); else Assert.AreEqual(0, env.BlobDir.CompareTo(env_blobdir)); Assert.AreEqual(env_threshold, env.BlobThreshold); cfg.Env = env; heapDBName = testName + ".db"; } // Open the database and verify the blob configs. if (db_threshold > 0) cfg.BlobThreshold = db_threshold; if (db_blobdir != null) { cfg.BlobDir = db_blobdir; /* * The blob directory setting in the database * is effective only when it is opened without * an environment. */ if (cfg.Env == null) blrootdir = db_blobdir; } HeapDatabase db = HeapDatabase.Open(heapDBName, cfg); Assert.AreEqual( db_threshold > 0 ? db_threshold : env_threshold, db.BlobThreshold); if (db_blobdir == null && cfg.Env == null) Assert.IsNull(db.BlobDir); else Assert.AreEqual(0, db.BlobDir.CompareTo(blrootdir)); // Insert and verify some blob data by database methods. string[] records = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; DatabaseEntry kdbt = new DatabaseEntry(); DatabaseEntry ddbt = new DatabaseEntry(); byte[] ddata; string str; KeyValuePair<DatabaseEntry, DatabaseEntry> pair; ddbt.Blob = blobdbt; Assert.AreEqual(blobdbt, ddbt.Blob); for (int i = 0; i < records.Length; i++) { str = records[i]; if (!blobdbt) { for (int j = 0; j < db_threshold; j++) str = str + records[i]; } ddata = Encoding.ASCII.GetBytes(str); ddbt.Data = ddata; kdbt = new DatabaseEntry((db.Append(ddbt)).toArray()) ; try { pair = db.Get(kdbt); } catch (DatabaseException) { db.Close(); if (cfg.Env != null) cfg.Env.Close(); throw new TestException(); } Assert.AreEqual(ddata, pair.Value.Data); } /* * Verify the blob files are created in the expected location. * This part of test is disabled since BTreeDatabase.BlobSubDir * is not exposed to users. */ //if (cfg.Env != null) // blrootdir = testHome + "/" + blrootdir; //string blobdir = blrootdir + "/" + db.BlobSubDir; //Assert.AreEqual(records.Length, // Directory.GetFiles(blobdir, "__db.bl*").Length); //Assert.AreEqual(1, // Directory.GetFiles(blobdir, "__db_blob_meta.db").Length); // Verify the stats. HeapStats st = db.Stats(); Assert.AreEqual(records.Length, st.nBlobRecords); // Close all handles. db.Close(); if (cfg.Env != null) cfg.Env.Close(); /* * Remove the default blob directory * when it is not under the test home. */ if (db_blobdir == null && cfg.Env == null) Directory.Delete("__db_bl", true); }
public void TestOpenNewHeapDB() { testName = "TestOpenNewHeapDB"; SetUpTest(true); string heapDBFileName = testHome + "/" + testName + ".db"; // Configure all fields/properties in heap database. XmlElement xmlElem = Configuration.TestSetUp( testFixtureName, testName); HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); HeapDatabaseConfigTest.Config(xmlElem, ref heapConfig, true); heapConfig.Feedback = new DatabaseFeedbackDelegate(DbFeedback); // Open the heap database with above configuration. HeapDatabase heapDB = HeapDatabase.Open( heapDBFileName, heapConfig); // Check the fields/properties in opened heap database. Confirm(xmlElem, heapDB, true); heapDB.Close(); }
public void ConfigCase1(HeapDatabaseConfig dbConfig) { dbConfig.Creation = CreatePolicy.IF_NEEDED; dbConfig.PageSize = 4096; }
/// <summary> /// Instantiate a new HeapDatabase object and open the database /// represented by <paramref name="Filename"/>. /// </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 /// 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="cfg">The database's configuration</param> /// <returns>A new, open database object</returns> public static HeapDatabase Open( string Filename, HeapDatabaseConfig cfg) { return(Open(Filename, cfg, null)); }
public void TestStats() { testName = "TestStats"; SetUpTest(true); string dbFileName = testHome + "/" + testName + ".db"; HeapDatabaseConfig dbConfig = new HeapDatabaseConfig(); ConfigCase1(dbConfig); HeapDatabase db = HeapDatabase.Open(dbFileName, dbConfig); HeapStats stats = db.Stats(); ConfirmStatsPart1Case1(stats); db.PrintFastStats(true); // Put 500 records into the database. PutRecordCase1(db, null); stats = db.Stats(); ConfirmStatsPart2Case1(stats); db.PrintFastStats(); db.Close(); }
public void StatsInTxn(string home, string name, bool ifIsolation) { DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); EnvConfigCase1(envConfig); DatabaseEnvironment env = DatabaseEnvironment.Open( home, envConfig); Transaction openTxn = env.BeginTransaction(); HeapDatabaseConfig dbConfig = new HeapDatabaseConfig(); ConfigCase1(dbConfig); dbConfig.Env = env; HeapDatabase db = HeapDatabase.Open(name + ".db", dbConfig, openTxn); openTxn.Commit(); Transaction statsTxn = env.BeginTransaction(); HeapStats stats; if (ifIsolation == false) stats = db.Stats(statsTxn); else stats = db.Stats(statsTxn, Isolation.DEGREE_ONE); ConfirmStatsPart1Case1(stats); db.PrintStats(true); // Put 500 records into the database. PutRecordCase1(db, statsTxn); if (ifIsolation == false) stats = db.Stats(statsTxn); else stats = db.Stats(statsTxn, Isolation.DEGREE_TWO); ConfirmStatsPart2Case1(stats); db.PrintStats(); statsTxn.Commit(); db.Close(); env.Close(); }
/// <summary> /// Instantiate a new HeapDatabase object and open the database /// represented by <paramref name="Filename"/>. /// </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 HeapDatabase Open( string Filename, HeapDatabaseConfig cfg, Transaction txn) { HeapDatabase ret = new HeapDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, null, DBTYPE.DB_HEAP, cfg.openFlags, 0); ret.isOpen = true; return ret; }
public void TestAppendWithoutTxn() { testName = "TestAppendWithoutTxn"; SetUpTest(true); string heapDBFileName = testHome + "/" + testName + ".db"; HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); heapConfig.Creation = CreatePolicy.ALWAYS; HeapDatabase heapDB = HeapDatabase.Open( heapDBFileName, heapConfig); byte[] byteArr = new byte[4]; byteArr = BitConverter.GetBytes((int)1); DatabaseEntry data = new DatabaseEntry(byteArr); HeapRecordId rid = heapDB.Append(data); // Confirm that the record lives on a page larger than 0. Assert.AreNotEqual(0, rid.PageNumber); // Confirm that the record exists in the database. DatabaseEntry key = new DatabaseEntry(rid.toArray()); Assert.IsTrue(heapDB.Exists(key)); heapDB.Close(); }
/// <summary> /// Instantiate a new HeapDatabase object and open the database /// represented by <paramref name="Filename"/>. /// </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 /// 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="cfg">The database's configuration</param> /// <returns>A new, open database object</returns> public static HeapDatabase Open( string Filename, HeapDatabaseConfig cfg) { return Open(Filename, cfg, null); }
public void TestAppendWithTxn() { testName = "TestAppendWithTxn"; SetUpTest(true); string heapDBFileName = testHome + "/" + testName + ".db"; string heapDBName = Path.GetFileNameWithoutExtension(heapDBFileName); DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseTxns = true; envConfig.UseMPool = true; DatabaseEnvironment env = DatabaseEnvironment.Open( testHome, envConfig); Transaction txn = env.BeginTransaction(); HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); heapConfig.Creation = CreatePolicy.ALWAYS; heapConfig.Env = env; /* If environmnet home is set, the file name in * Open() is the relative path. */ HeapDatabase heapDB = HeapDatabase.Open( heapDBName, heapConfig, txn); DatabaseEntry data; int i = 1000; try { while (i > 0) { data = new DatabaseEntry( BitConverter.GetBytes(i)); heapDB.Append(data, txn); i--; } txn.Commit(); } catch { txn.Abort(); } finally { heapDB.Close(); env.Close(); } }