/* * Get a document from the database based with the given document's ID. */ public Document SearchInDatabaseById(long id) { DatabaseEntry key = new DatabaseEntry(BitConverter.GetBytes(id)); try { return(new Document(hashDatabase.Get(key).Value.Data)); } catch (NotFoundException) { return(null); } }
public long SearchInDatabaseByUrl(string url) { DatabaseEntry key = new DatabaseEntry(Encoding.UTF8.GetBytes(url)); try { return(BitConverter.ToInt64(urlDatabase.Get(key).Value.Data)); } catch (NotFoundException) { return(0); } }
/// <summary> /// Gets the index payload for a given atom. /// </summary> /// <param name="atom">The internal representation belonging to the atom.</param> /// <returns> /// The index payload for the given atom. /// </returns> public IndexPayload GetPayload(long atom) { var key = new DatabaseEntry(Util.Encoding.DbEncode(atom)); if (m_db.Exists(key)) { var kvPair = m_db.Get(key); return(new IndexPayload(kvPair.Value.Data)); } else { throw new ArgumentOutOfRangeException("atom", "No payload for given atom!"); } }
/// <summary> /// Gets the external (or friendly) string representation belonging to a given integer /// used as internal representation by the database. /// </summary> /// <param name="value">The internal representation.</param> /// <returns> /// A string belonging to the given internal representation. /// </returns> public string GetExternalRepresentation(long value) { var key = new DatabaseEntry(Util.Encoding.DbEncode(value)); // // if this particular internal representation does not already exist in the dictionary // we throw an argument exception. if (m_dbLong2Str.Exists(key)) { var kvPair = m_dbLong2Str.Get(key); return(Util.Encoding.DbDecodeString(kvPair.Value.Data)); } else { throw new ArgumentOutOfRangeException("value", "No such internal representation!"); } }
/// <summary> /// Gets the internal representation used by the database for a given string value. /// </summary> /// <param name="value">The string value.</param> /// <returns> /// An integer which is the internal representation for the given string. /// </returns> public long GetInternalRepresentation(string value) { var key = new DatabaseEntry(Util.Encoding.DbEncode(value)); // // if this string already exists in the dictionary we simply retrieve the internal // representation that belongs to it. if not, we assign an internal representation to // it on the fly and insert it into the dictionary. whatever case we are in is // indistinguishable to the caller. if (m_dbStr2Long.Exists(key)) { var kvPair = m_dbStr2Long.Get(key); return(Util.Encoding.DbDecodeInt64(kvPair.Value.Data)); } else { var next = m_next++; Insert(next, value); return(next); } }
/* * Test the external file database with or without environment. * 1. Config and open the environment; * 2. Verify the environment external file configs; * 3. Config and open the database; * 4. Verify the database external file configs; * 5. Insert and verify some external file data by database methods; * 6. Insert some external file data by cursor, update it and verify * the update by database stream and cursor; * 7. Verify the stats; * 8. Close all handles. * If "blobdbt" is true, set the data DatabaseEntry.ExternalFile as * true, otherwise make the data DatabaseEntry reach the external file * threshold in size. */ void TestBlobHashDatabase(uint env_threshold, string env_blobdir, uint db_threshold, string db_blobdir, bool blobdbt) { if (env_threshold == 0 && db_threshold == 0) { return; } string hashDBName = testHome + "/" + testName + ".db"; Configuration.ClearDir(testHome); HashDatabaseConfig cfg = new HashDatabaseConfig(); cfg.Creation = CreatePolicy.ALWAYS; string blrootdir = "__db_bl"; // Open the environment and verify the external file config. 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.ExternalFileThreshold = env_threshold; if (env_blobdir != null) { envConfig.ExternalFileDir = env_blobdir; blrootdir = env_blobdir; } DatabaseEnvironment env = DatabaseEnvironment.Open( testHome, envConfig); if (env_blobdir == null) { Assert.IsNull(env.ExternalFileDir); } else { Assert.AreEqual(0, env.ExternalFileDir.CompareTo(env_blobdir)); } Assert.AreEqual(env_threshold, env.ExternalFileThreshold); cfg.Env = env; hashDBName = testName + ".db"; } // Open the database and verify the external file config. if (db_threshold > 0) { cfg.ExternalFileThreshold = db_threshold; } if (db_blobdir != null) { cfg.ExternalFileDir = db_blobdir; /* * The external file directory setting in the database * is effective only when it is opened without * an environment. */ if (cfg.Env == null) { blrootdir = db_blobdir; } } HashDatabase db = HashDatabase.Open(hashDBName, cfg); Assert.AreEqual( db_threshold > 0 ? db_threshold : env_threshold, db.ExternalFileThreshold); if (db_blobdir == null && cfg.Env == null) { Assert.IsNull(db.ExternalFileDir); } else { Assert.AreEqual(0, db.ExternalFileDir.CompareTo(blrootdir)); } // Insert and verify some external file 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[] kdata, ddata; string str; KeyValuePair <DatabaseEntry, DatabaseEntry> pair; ddbt.ExternalFile = blobdbt; Assert.AreEqual(blobdbt, ddbt.ExternalFile); for (int i = 0; i < records.Length; i++) { kdata = BitConverter.GetBytes(i); str = records[i]; if (!blobdbt) { for (int j = 0; j < db_threshold; j++) { str = str + records[i]; } } ddata = Encoding.ASCII.GetBytes(str); kdbt.Data = kdata; ddbt.Data = ddata; db.Put(kdbt, ddbt); 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); } /* * Insert some external file data by cursor, update it and * verify the update by database stream. */ kdata = BitConverter.GetBytes(records.Length); ddata = Encoding.ASCII.GetBytes("abc"); kdbt.Data = kdata; ddbt.Data = ddata; ddbt.ExternalFile = true; Assert.IsTrue(ddbt.ExternalFile); pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(kdbt, ddbt); CursorConfig dbcConfig = new CursorConfig(); Transaction txn = null; if (cfg.Env != null) { txn = cfg.Env.BeginTransaction(); } HashCursor cursor = db.Cursor(dbcConfig, txn); cursor.Add(pair); DatabaseStreamConfig dbsc = new DatabaseStreamConfig(); dbsc.SyncPerWrite = true; DatabaseStream dbs = cursor.DbStream(dbsc); Assert.AreNotEqual(null, dbs); Assert.IsFalse(dbs.GetConfig.ReadOnly); Assert.IsTrue(dbs.GetConfig.SyncPerWrite); Assert.AreEqual(3, dbs.Size()); DatabaseEntry sdbt = dbs.Read(0, 3); Assert.IsNotNull(sdbt); Assert.AreEqual(ddata, sdbt.Data); sdbt = new DatabaseEntry(Encoding.ASCII.GetBytes("defg")); Assert.IsTrue(dbs.Write(sdbt, 3)); Assert.AreEqual(7, dbs.Size()); sdbt = dbs.Read(0, 7); Assert.IsNotNull(sdbt); Assert.AreEqual(Encoding.ASCII.GetBytes("abcdefg"), sdbt.Data); dbs.Close(); /* * Verify the database stream can not write when it is * configured to be read-only. */ dbsc.ReadOnly = true; dbs = cursor.DbStream(dbsc); Assert.IsTrue(dbs.GetConfig.ReadOnly); try { Assert.IsFalse(dbs.Write(sdbt, 7)); throw new TestException(); } catch (DatabaseException) { } dbs.Close(); // Verify the update by cursor. Assert.IsTrue(cursor.Move(kdbt, true)); pair = cursor.Current; Assert.AreEqual(Encoding.ASCII.GetBytes("abcdefg"), pair.Value.Data); cursor.Close(); if (cfg.Env != null) { txn.Commit(); } /* * Verify the external 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 + 1, // Directory.GetFiles(blobdir, "__db.bl*").Length); //Assert.AreEqual(1, // Directory.GetFiles(blobdir, "__db_blob_meta.db").Length); // Verify the stats. HashStats st = db.Stats(); Assert.AreEqual(records.Length + 1, st.nExternalFiles); // Close all handles. db.Close(); if (cfg.Env != null) { cfg.Env.Close(); } /* * Remove the default external file directory * when it is not under the test home. */ if (db_blobdir == null && cfg.Env == null) { Directory.Delete("__db_bl", true); } }