Example #1
0
        public void TestPutNoDuplicate()
        {
            testName = "TestPutNoDuplicate";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig hashConfig =
                new HashDatabaseConfig();

            hashConfig.Creation   = CreatePolicy.ALWAYS;
            hashConfig.Duplicates = DuplicatesPolicy.SORTED;
            hashConfig.TableSize  = 20;
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            DatabaseEntry key, data;

            for (int i = 1; i <= 10; i++)
            {
                key  = new DatabaseEntry(BitConverter.GetBytes(i));
                data = new DatabaseEntry(BitConverter.GetBytes(i));
                hashDB.PutNoDuplicate(key, data);
            }

            Assert.IsTrue(hashDB.Exists(
                              new DatabaseEntry(BitConverter.GetBytes((int)5))));

            hashDB.Close();
        }
        public void TestDuplicates()
        {
            testName = "TestDuplicates";
            SetUpTest(true);
            string dbFileName    = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" + testName
                                   + "_sec.db";

            // Open a primary hash database.
            HashDatabaseConfig dbConfig =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.ALWAYS;
            HashDatabase db = HashDatabase.Open(
                dbFileName, dbConfig);

            // Open a secondary hash database.
            SecondaryHashDatabaseConfig secConfig =
                new SecondaryHashDatabaseConfig(null, null);

            secConfig.Primary    = db;
            secConfig.Duplicates = DuplicatesPolicy.SORTED;
            secConfig.Creation   = CreatePolicy.IF_NEEDED;
            SecondaryHashDatabase secDB =
                SecondaryHashDatabase.Open(
                    dbSecFileName, secConfig);

            // Confirm the duplicate in opened secondary database.
            Assert.AreEqual(DuplicatesPolicy.SORTED,
                            secDB.Duplicates);

            secDB.Close();
            db.Close();
        }
Example #3
0
        public void TestStats()
        {
            testName = "TestStats";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            ConfigCase1(dbConfig);
            HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);

            HashStats stats     = db.Stats();
            HashStats fastStats = db.FastStats();

            ConfirmStatsPart1Case1(stats);
            ConfirmStatsPart1Case1(fastStats);

            // Put 100 records into the database.
            PutRecordCase1(db, null);

            stats = db.Stats();
            ConfirmStatsPart2Case1(stats);

            // Delete some data to get some free pages.
            byte[] bigArray = new byte[262144];
            db.Delete(new DatabaseEntry(bigArray));
            stats = db.Stats();
            ConfirmStatsPart3Case1(stats);

            db.Close();
        }
Example #4
0
        public void TestHashComparison()
        {
            testName = "TestHashComparison";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            dbConfig.Creation       = CreatePolicy.IF_NEEDED;
            dbConfig.HashComparison = new EntryComparisonDelegate(EntryComparison);
            HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);
            int          ret;

            /*
             * Comparison gets the value that lowest byte of the
             * former dbt minus that of the latter one.
             */
            ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(2)),
                             new DatabaseEntry(BitConverter.GetBytes(2)));
            Assert.AreEqual(0, ret);

            ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(256)),
                             new DatabaseEntry(BitConverter.GetBytes(1)));
            Assert.Greater(0, ret);

            db.Close();
        }
        public static void Config(XmlElement xmlElement,
                                  ref HashDatabaseConfig hashDBConfig, bool compulsory)
        {
            uint           fillFactor  = new uint();
            uint           numElements = new uint();
            DatabaseConfig dbConfig    = hashDBConfig;

            Config(xmlElement, ref dbConfig, compulsory);

            // Configure specific fields/properties of Hash db
            Configuration.ConfigCreatePolicy(xmlElement,
                                             "Creation", ref hashDBConfig.Creation,
                                             compulsory);
            Configuration.ConfigDuplicatesPolicy(xmlElement,
                                                 "Duplicates", ref hashDBConfig.Duplicates,
                                                 compulsory);
            if (Configuration.ConfigUint(xmlElement, "FillFactor",
                                         ref fillFactor, compulsory))
            {
                hashDBConfig.FillFactor = fillFactor;
            }
            if (Configuration.ConfigUint(xmlElement, "NumElements",
                                         ref numElements, compulsory))
            {
                hashDBConfig.TableSize = numElements;
            }
        }
Example #6
0
        public void TestPutNoDuplicateWithUnsortedDuplicate()
        {
            testName = "TestPutNoDuplicateWithUnsortedDuplicate";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig hashConfig = new HashDatabaseConfig();

            hashConfig.Creation    = CreatePolicy.ALWAYS;
            hashConfig.Duplicates  = DuplicatesPolicy.UNSORTED;
            hashConfig.ErrorPrefix = testName;

            HashDatabase  hashDB = HashDatabase.Open(dbFileName, hashConfig);
            DatabaseEntry key, data;

            key  = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));

            try
            {
                hashDB.PutNoDuplicate(key, data);
            }
            catch (DatabaseException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                hashDB.Close();
            }
        }
Example #7
0
        /*
         * Configure and open hash databases for inverted index.
         */
        public InvertedIndex(DocumentsCatalogue documentsCatalogue, DatabaseEnvironment env)
        {
            this.documentsCatalogue = documentsCatalogue;
            this.env = env.env;
            /* Configure the database. */
            var hashDatabaseConfig = new HashDatabaseConfig()
            {
                Duplicates   = DuplicatesPolicy.NONE,
                Creation     = CreatePolicy.IF_NEEDED,
                FreeThreaded = true,
                CacheSize    = new CacheInfo(1, 0, 128),
                // Env = env.env,
            };

            /* Create the database if does not already exist and open the database file. */
            try
            {
                hashDatabase    = HashDatabase.Open("inverted_index.db", hashDatabaseConfig);
                maxFreqDatabase = HashDatabase.Open("max_freq.db", hashDatabaseConfig);
                //Console.WriteLine("{0} open.", dbFileName);
            }
            catch (Exception e)
            {
                // Console.WriteLine("Error opening {0}.", dbFileName);
                Console.WriteLine(e.Message);
                return;
            }
        }
Example #8
0
        protected HatUserFactory()
        {
            string path;

            try{
                path = ConfigurationSettings.AppSettings["db_path"];
            } catch (Exception ex) {
                throw new System.IO.FileNotFoundException("db_path setting not found", ex);
            }

            try{
                var cfg = new HashDatabaseConfig();

                cfg.Duplicates  = DuplicatesPolicy.SORTED;
                cfg.ErrorPrefix = "HatUserFactoryError_";
                cfg.Creation    = CreatePolicy.IF_NEEDED;
                cfg.CacheSize   = new CacheInfo(0, 64 * 1024, 1);
                cfg.PageSize    = 8 * 1024;

                db = HashDatabase.Open(path, this.GetType().Name, cfg);
            } catch (DatabaseException ex) {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Example #9
0
        public void TestKeyExistException()
        {
            testName = "TestKeyExistException";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig hashConfig = new HashDatabaseConfig();

            hashConfig.Creation   = CreatePolicy.ALWAYS;
            hashConfig.Duplicates = DuplicatesPolicy.SORTED;
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            // Put the same record into db twice.
            DatabaseEntry key, data;

            key  = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
            try
            {
                hashDB.PutNoDuplicate(key, data);
                hashDB.PutNoDuplicate(key, data);
            }
            catch (KeyExistException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                hashDB.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.
            HashDatabaseConfig hashDBConfig =
                new HashDatabaseConfig();

            hashDBConfig.Creation = CreatePolicy.IF_NEEDED;
            HashDatabase hashDB = HashDatabase.Open(
                dbFileName, hashDBConfig);

            SecondaryHashDatabaseConfig secDBConfig =
                new SecondaryHashDatabaseConfig(hashDB, null);

            Config(xmlElem, ref secDBConfig, true);
            Confirm(xmlElem, secDBConfig, true);

            // Close the primary btree database.
            hashDB.Close();
        }
 public void ConfigCase1(HashDatabaseConfig dbConfig)
 {
     dbConfig.Creation   = CreatePolicy.IF_NEEDED;
     dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
     dbConfig.FillFactor = 10;
     dbConfig.TableSize  = 20;
     dbConfig.PageSize   = 4096;
 }
Example #12
0
        override public void TestConfigWithoutEnv()
        {
            testName = "TestConfigWithoutEnv";
            XmlElement xmlElem = Configuration.TestSetUp(
                testFixtureName, testName);
            HashDatabaseConfig hashConfig = new HashDatabaseConfig();

            Config(xmlElem, ref hashConfig, true);
            Confirm(xmlElem, hashConfig, true);
        }
Example #13
0
        public void GetHashDBAndCursor(string home, string name,
                                       out HashDatabase db, out HashCursor cursor)
        {
            string             dbFileName = home + "/" + name + ".db";
            HashDatabaseConfig dbConfig   = new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            db     = HashDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();
        }
        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.
            HashDatabaseConfig DBConfig =
                new HashDatabaseConfig();

            DBConfig.Env      = env;
            DBConfig.Creation = CreatePolicy.IF_NEEDED;

            string       DBFileName = testName + ".db";
            HashDatabase db         = HashDatabase.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();
        }
Example #15
0
        public void TestInsertToLoc()
        {
            HashDatabase       db;
            HashDatabaseConfig dbConfig;
            HashCursor         cursor;
            DatabaseEntry      data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
            string dbFileName;

            testName   = "TestInsertToLoc";
            testHome   = testFixtureHome + "/" + testName;
            dbFileName = testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);

            // Open database and cursor.
            dbConfig          = new HashDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;

            /*
             * The database should be set to be unsorted to
             * insert before/after a certain record.
             */
            dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
            db     = HashDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();

            // Add record("key", "data") into database.
            AddOneByCursor(cursor);

            /*
             * Insert the new record("key","data1") after the
             * record("key", "data").
             */
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1"));
            cursor.Insert(data, Cursor.InsertLocation.AFTER);

            /*
             * Move the cursor to the record("key", "data") and
             * confirm that the next record is the one just inserted.
             */
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
            Assert.IsTrue(cursor.Move(pair, true));
            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"),
                            cursor.Current.Key.Data);
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"),
                            cursor.Current.Value.Data);

            cursor.Close();
            db.Close();
        }
Example #16
0
        static void Main(string[] args)
        {
            try {
                var cfg = new HashDatabaseConfig();
                cfg.Duplicates = DuplicatesPolicy.UNSORTED;
                cfg.Creation   = CreatePolicy.IF_NEEDED;
                cfg.CacheSize  = new CacheInfo(0, 64 * 1024, 1);
                cfg.PageSize   = 8 * 1024;

                Database db = HashDatabase.Open("d:\\test.db", "hat_db", cfg);

                Console.WriteLine("db opened");

                var key  = new DatabaseEntry();
                var data = new DatabaseEntry();

                key.Data  = System.Text.Encoding.ASCII.GetBytes("key1");
                data.Data = System.Text.Encoding.ASCII.GetBytes("val1");

                try {
                    db.Put(key, data);
                    db.Put(key, data);
                }
                catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                }


                using (var dbc = db.Cursor()) {
                    System.Text.ASCIIEncoding decode = new ASCIIEncoding();

                    /* 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}",
                                          decode.GetString(p.Key.Data), decode.GetString(p.Value.Data));
                    }
                }

                db.Close();
                Console.WriteLine("db closed");
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }
        public void TestOpenNewHashDB()
        {
            testName = "TestOpenNewHashDB";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            XmlElement         xmlElem    = Configuration.TestSetUp(testFixtureName, testName);
            HashDatabaseConfig hashConfig = new HashDatabaseConfig();

            HashDatabaseConfigTest.Config(xmlElem, ref hashConfig, true);
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            Confirm(xmlElem, hashDB, true);
            hashDB.Close();
        }
Example #18
0
        public void TestAddUnique()
        {
            HashDatabase db;
            HashCursor   cursor;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

            testName = "TestAddUnique";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            // Open a database and cursor.
            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;

            /*
             * To put no duplicate data, the database should be
             * set to be sorted.
             */
            dbConfig.Duplicates = DuplicatesPolicy.SORTED;
            db = HashDatabase.Open(
                testHome + "/" + testName + ".db", dbConfig);
            cursor = db.Cursor();

            // Add record("key", "data") into database.
            AddOneByCursor(cursor);

            /*
             * Fail to add duplicate record("key","data").
             */
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
            try
            {
                cursor.AddUnique(pair);
            }
            catch (KeyExistException)
            {
            }
            finally
            {
                cursor.Close();
                db.Close();
            }
        }
        public void TestHashFunction()
        {
            testName = "TestHashFunction";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName    = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" +
                                   testName + "_sec.db";

            Configuration.ClearDir(testHome);

            // Open a primary hash database.
            HashDatabaseConfig dbConfig =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            HashDatabase hashDB = HashDatabase.Open(
                dbFileName, dbConfig);

            /*
             * Define hash function and open a secondary
             * hash database.
             */
            SecondaryHashDatabaseConfig secDBConfig =
                new SecondaryHashDatabaseConfig(hashDB, null);

            secDBConfig.HashFunction =
                new HashFunctionDelegate(HashFunction);
            secDBConfig.Creation = CreatePolicy.IF_NEEDED;
            SecondaryHashDatabase secDB =
                SecondaryHashDatabase.Open(dbSecFileName,
                                           secDBConfig);

            /*
             * Confirm the hash function defined in the configuration.
             * Call the hash function and the one from secondary
             * database. If they return the same value, then the hash
             * function is configured successfully.
             */
            uint data = secDB.HashFunction(BitConverter.GetBytes(1));

            Assert.AreEqual(0, data);

            // Close all.
            secDB.Close();
            hashDB.Close();
        }
Example #20
0
        public void TestPutNoDuplicateWithTxn()
        {
            testName = "TestPutNoDuplicateWithTxn";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create     = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool   = true;
            envConfig.UseTxns    = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            // Open a hash database within a transaction.
            Transaction        txn      = env.BeginTransaction();
            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.IF_NEEDED;
            dbConfig.Duplicates = DuplicatesPolicy.SORTED;
            dbConfig.Env        = env;
            HashDatabase db = HashDatabase.Open(testName + ".db", dbConfig, txn);

            DatabaseEntry dbt = new DatabaseEntry(BitConverter.GetBytes((int)100));

            db.PutNoDuplicate(dbt, dbt, txn);
            try
            {
                db.PutNoDuplicate(dbt, dbt, txn);
            }
            catch (KeyExistException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                // Close all.
                db.Close();
                txn.Commit();
                env.Close();
            }
        }
        public static void Confirm(XmlElement xmlElement,
                                   HashDatabaseConfig hashDBConfig, bool compulsory)
        {
            DatabaseConfig dbConfig = hashDBConfig;

            Confirm(xmlElement, dbConfig, compulsory);

            // Confirm Hash database specific configuration.
            Configuration.ConfirmCreatePolicy(xmlElement,
                                              "Creation", hashDBConfig.Creation, compulsory);
            Configuration.ConfirmDuplicatesPolicy(xmlElement,
                                                  "Duplicates", hashDBConfig.Duplicates, compulsory);
            Configuration.ConfirmUint(xmlElement, "FillFactor",
                                      hashDBConfig.FillFactor, compulsory);
            Configuration.ConfirmUint(xmlElement, "NumElements",
                                      hashDBConfig.TableSize, compulsory);
        }
        public void TestHashFunction()
        {
            testName = "TestHashFunction";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            dbConfig.Creation     = CreatePolicy.IF_NEEDED;
            dbConfig.HashFunction = new HashFunctionDelegate(HashFunction);
            HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);

            // Hash function will change the lowest byte to 0;
            uint data = db.HashFunction(BitConverter.GetBytes(1));

            Assert.AreEqual(0, data);
            db.Close();
        }
        public void TestCompare()
        {
            testName = "TestCompare";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName    = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" + testName +
                                   "_sec.db";

            Configuration.ClearDir(testHome);

            // Open a primary hash database.
            HashDatabaseConfig dbConfig =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.ALWAYS;
            HashDatabase db = HashDatabase.Open(
                dbFileName, dbConfig);

            // Open a secondary hash database.
            SecondaryHashDatabaseConfig secConfig =
                new SecondaryHashDatabaseConfig(null, null);

            secConfig.Creation = CreatePolicy.IF_NEEDED;
            secConfig.Primary  = db;
            secConfig.Compare  =
                new EntryComparisonDelegate(SecondaryEntryComparison);
            SecondaryHashDatabase secDB =
                SecondaryHashDatabase.Open(dbSecFileName, secConfig);

            /*
             * Get the compare function set in the configuration
             * and run it in a comparison to see if it is alright.
             */
            DatabaseEntry dbt1, dbt2;

            dbt1 = new DatabaseEntry(
                BitConverter.GetBytes((int)257));
            dbt2 = new DatabaseEntry(
                BitConverter.GetBytes((int)255));
            Assert.Less(0, secDB.Compare(dbt1, dbt2));

            secDB.Close();
            db.Close();
        }
Example #24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AtomDictionary"/> class.
        /// </summary>
        /// <param name="databaseName">Name of the TripleT database this dictionary belongs to.</param>
        public AtomDictionary(string databaseName)
        {
            //
            // names for each of the files involved

            var nameStr2Long = String.Format("{0}.dict.str.dat", databaseName);
            var nameLong2Str = String.Format("{0}.dict.int.dat", databaseName);

            m_fileNextValue = String.Format("{0}.dict.dat", databaseName);

            //
            // we manually keep a file containing the value for our auto-incrementing index integer
            // assigned to new string values inserted into the dictionary. BerkeleyDB does not
            // support such a feature...

            if (File.Exists(m_fileNextValue))
            {
                using (var sr = new BinaryReader(File.Open(m_fileNextValue, FileMode.Open, FileAccess.Read, FileShare.Read))) {
                    m_next = sr.ReadInt64();
                }
            }
            else
            {
                m_next = 1;
            }

            //
            // configuration for the dictionary databases. memory allocated for the caches is
            // hardcoded here.

            var config = new HashDatabaseConfig();

            config.Duplicates = DuplicatesPolicy.NONE;
            config.CacheSize  = new CacheInfo(0, 256 * 1024 * 1024, 4);
            config.PageSize   = 512;
            config.Creation   = CreatePolicy.IF_NEEDED;

            //
            // opening the databases...

            m_dbStr2Long = HashDatabase.Open(nameStr2Long, config);
            m_dbLong2Str = HashDatabase.Open(nameLong2Str, config);
        }
        protected Repository(string databaseName, uint tableSize, ILoggerFactory loggerService)
        {
            logger = loggerService.CreateLogger(databaseName);
            path   = Environment.GetEnvironmentVariable("DATA_DIR");
            var cfg = new HashDatabaseConfig
            {
                Creation      = CreatePolicy.IF_NEEDED,
                CacheSize     = new CacheInfo(1, 0, 1),
                ErrorFeedback = (prefix, message) =>
                {
                    logger.LogCritical($"{prefix}: {message}");
                },
                ErrorPrefix = databaseName,
                Duplicates  = DuplicatesPolicy.UNSORTED,
                TableSize   = tableSize
            };

            db = HashDatabase.Open(Path.Combine(path, databaseName + ".db"), cfg);
        }
        public void TestOpenExistingHashDB()
        {
            testName = "TestOpenExistingHashDB";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig hashConfig =
                new HashDatabaseConfig();

            hashConfig.Creation = CreatePolicy.ALWAYS;
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            hashDB.Close();

            DatabaseConfig dbConfig = new DatabaseConfig();
            Database       db       = Database.Open(dbFileName, dbConfig);

            Assert.AreEqual(db.Type, DatabaseType.HASH);
            db.Close();
        }
        /*
         * Configure and open hash database for documents.
         * Each document has a unique ID and is stored in the database with hash key the document's ID and value the document.
         */
        public DocumentsCatalogue(DatabaseEnvironment env)
        {
            /* Configure the database. */
            var hashDatabaseConfig = new HashDatabaseConfig
            {
                Duplicates   = DuplicatesPolicy.NONE,
                Creation     = CreatePolicy.IF_NEEDED,
                FreeThreaded = true,
            };

            /* Create the database if does not already exist and open the database file. */
            try
            {
                hashDatabase = HashDatabase.Open("documents_catalogue.db", hashDatabaseConfig);
                urlDatabase  = HashDatabase.Open("visited_urls.db", hashDatabaseConfig);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }
Example #28
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Index"/> class.
        /// </summary>
        /// <param name="databaseName">Name of the database this index belongs to.</param>
        /// <param name="pBucket">The p bucket belonging to this database.</param>
        /// <param name="oBucket">The o bucket belonging to this database.</param>
        /// <param name="sBucket">The s bucket belonging to this database.</param>
        public Index(string databaseName, Bucket sBucket, Bucket pBucket, Bucket oBucket)
        {
            var nameDb = String.Format("{0}.index.dat", databaseName);

            m_sBucket = sBucket;
            m_pBucket = pBucket;
            m_oBucket = oBucket;

            //
            // the index is stored in a BerkeleyDB hash database. configuration for this database
            // is hardcoded here.

            var config = new HashDatabaseConfig();

            config.Duplicates = DuplicatesPolicy.NONE;
            config.CacheSize  = new CacheInfo(0, 256 * 1024 * 1024, 4);
            config.PageSize   = 512;
            config.Creation   = CreatePolicy.IF_NEEDED;

            //
            // open the index database

            m_db = HashDatabase.Open(nameDb, config);
        }
        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();
            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            ConfigCase1(dbConfig);
            dbConfig.Env = env;
            HashDatabase db = HashDatabase.Open(name + ".db", dbConfig, openTxn);

            openTxn.Commit();

            Transaction statsTxn = env.BeginTransaction();
            HashStats   stats;
            HashStats   fastStats;

            if (ifIsolation == false)
            {
                stats     = db.Stats(statsTxn);
                fastStats = db.Stats(statsTxn);
            }
            else
            {
                stats     = db.Stats(statsTxn, Isolation.DEGREE_ONE);
                fastStats = db.Stats(statsTxn, Isolation.DEGREE_ONE);
            }

            ConfirmStatsPart1Case1(stats);

            // Put 100 records into the database.
            PutRecordCase1(db, statsTxn);

            if (ifIsolation == false)
            {
                stats = db.Stats(statsTxn);
            }
            else
            {
                stats = db.Stats(statsTxn, Isolation.DEGREE_TWO);
            }
            ConfirmStatsPart2Case1(stats);

            // Delete some data to get some free pages.
            byte[] bigArray = new byte[262144];
            db.Delete(new DatabaseEntry(bigArray), statsTxn);
            if (ifIsolation == false)
            {
                stats = db.Stats(statsTxn);
            }
            else
            {
                stats = db.Stats(statsTxn, Isolation.DEGREE_THREE);
            }
            ConfirmStatsPart3Case1(stats);

            statsTxn.Commit();
            db.Close();
            env.Close();
        }
        public void TestCompactWithoutTxn()
        {
            int i, nRecs;

            nRecs    = 10000;
            testName = "TestCompactWithoutTxn";
            SetUpTest(true);
            string hashDBFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig hashDBConfig = new HashDatabaseConfig();

            hashDBConfig.Creation = CreatePolicy.ALWAYS;
            // The minimum page size
            hashDBConfig.PageSize       = 512;
            hashDBConfig.HashComparison =
                new EntryComparisonDelegate(dbIntCompare);
            using (HashDatabase hashDB = HashDatabase.Open(
                       hashDBFileName, hashDBConfig)) {
                DatabaseEntry key;
                DatabaseEntry data;

                // Fill the database with entries from 0 to 9999
                for (i = 0; i < nRecs; i++)
                {
                    key  = new DatabaseEntry(BitConverter.GetBytes(i));
                    data = new DatabaseEntry(BitConverter.GetBytes(i));
                    hashDB.Put(key, data);
                }

                /*
                 * Delete entries below 500, between 3000 and
                 * 5000 and above 7000
                 */
                for (i = 0; i < nRecs; i++)
                {
                    if (i < 500 || i > 7000 || (i < 5000 && i > 3000))
                    {
                        key = new DatabaseEntry(BitConverter.GetBytes(i));
                        hashDB.Delete(key);
                    }
                }

                hashDB.Sync();
                long fileSize = new FileInfo(hashDBFileName).Length;

                // Compact database
                CompactConfig cCfg = new CompactConfig();
                cCfg.FillPercentage = 30;
                cCfg.Pages          = 10;
                cCfg.Timeout        = 1000;
                cCfg.TruncatePages  = true;
                cCfg.start          = new DatabaseEntry(BitConverter.GetBytes(1));
                cCfg.stop           = new DatabaseEntry(BitConverter.GetBytes(7000));
                CompactData compactData = hashDB.Compact(cCfg);
                Assert.IsFalse((compactData.Deadlocks == 0) &&
                               (compactData.Levels == 0) &&
                               (compactData.PagesExamined == 0) &&
                               (compactData.PagesFreed == 0) &&
                               (compactData.PagesTruncated == 0));

                hashDB.Sync();
                long compactedFileSize = new FileInfo(hashDBFileName).Length;
                Assert.Less(compactedFileSize, fileSize);
            }
        }