PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/SN-NG4.4/db4/test/scr037/HashDatabaseTest.cs

https://gitlab.com/OpenSourceMirror/sourcenav
C# | 466 lines | 380 code | 67 blank | 19 comment | 13 complexity | 3b81acd0a689c69df879b929a11120bd MD5 | raw file
  1. /*-
  2. * See the file LICENSE for redistribution information.
  3. *
  4. * Copyright (c) 2009 Oracle. All rights reserved.
  5. *
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using System.Xml;
  12. using NUnit.Framework;
  13. using BerkeleyDB;
  14. namespace CsharpAPITest
  15. {
  16. [TestFixture]
  17. public class HashDatabaseTest
  18. {
  19. private string testFixtureHome;
  20. private string testFixtureName;
  21. private string testName;
  22. private string testHome;
  23. [TestFixtureSetUp]
  24. public void RunBeforeTests()
  25. {
  26. testFixtureName = "HashDatabaseTest";
  27. testFixtureHome = "./TestOut/" + testFixtureName;
  28. Configuration.ClearDir(testFixtureHome);
  29. }
  30. [Test]
  31. public void TestHashComparison()
  32. {
  33. testName = "TestHashComparison";
  34. testHome = testFixtureHome + "/" + testName;
  35. string dbFileName = testHome + "/" + testName + ".db";
  36. Configuration.ClearDir(testHome);
  37. HashDatabaseConfig dbConfig = new HashDatabaseConfig();
  38. dbConfig.Creation = CreatePolicy.IF_NEEDED;
  39. dbConfig.HashComparison = new EntryComparisonDelegate(EntryComparison);
  40. HashDatabase db = HashDatabase.Open(dbFileName,dbConfig);
  41. int ret;
  42. /*
  43. * Comparison gets the value that lowest byte of the
  44. * former dbt minus that of the latter one.
  45. */
  46. ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(2)),
  47. new DatabaseEntry(BitConverter.GetBytes(2)));
  48. Assert.AreEqual(0, ret);
  49. ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(256)),
  50. new DatabaseEntry(BitConverter.GetBytes(1)));
  51. Assert.Greater(0, ret);
  52. db.Close();
  53. }
  54. public int EntryComparison(DatabaseEntry dbt1,
  55. DatabaseEntry dbt2)
  56. {
  57. return dbt1.Data[0] - dbt2.Data[0];
  58. }
  59. [Test]
  60. public void TestHashFunction()
  61. {
  62. testName = "TestHashFunction";
  63. testHome = testFixtureHome + "/" + testName;
  64. string dbFileName = testHome + "/" + testName + ".db";
  65. Configuration.ClearDir(testHome);
  66. HashDatabaseConfig dbConfig = new HashDatabaseConfig();
  67. dbConfig.Creation = CreatePolicy.IF_NEEDED;
  68. dbConfig.HashFunction = new HashFunctionDelegate(HashFunction);
  69. HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);
  70. // Hash function will change the lowest byte to 0;
  71. uint data = db.HashFunction(BitConverter.GetBytes(1));
  72. Assert.AreEqual(0, data);
  73. db.Close();
  74. }
  75. public uint HashFunction(byte[] data)
  76. {
  77. data[0] = 0;
  78. return BitConverter.ToUInt32(data, 0);
  79. }
  80. [Test]
  81. public void TestOpenExistingHashDB()
  82. {
  83. testName = "TestOpenExistingHashDB";
  84. testHome = testFixtureHome + "/" + testName;
  85. string dbFileName = testHome + "/" + testName + ".db";
  86. Configuration.ClearDir(testHome);
  87. HashDatabaseConfig hashConfig =
  88. new HashDatabaseConfig();
  89. hashConfig.Creation = CreatePolicy.ALWAYS;
  90. HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);
  91. hashDB.Close();
  92. DatabaseConfig dbConfig = new DatabaseConfig();
  93. Database db = Database.Open(dbFileName, dbConfig);
  94. Assert.AreEqual(db.Type, DatabaseType.HASH);
  95. db.Close();
  96. }
  97. [Test]
  98. public void TestOpenNewHashDB()
  99. {
  100. testName = "TestOpenNewHashDB";
  101. testHome = testFixtureHome + "/" + testName;
  102. string dbFileName = testHome + "/" + testName + ".db";
  103. Configuration.ClearDir(testHome);
  104. XmlElement xmlElem = Configuration.TestSetUp(testFixtureName, testName);
  105. HashDatabaseConfig hashConfig = new HashDatabaseConfig();
  106. HashDatabaseConfigTest.Config(xmlElem, ref hashConfig, true);
  107. HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);
  108. Confirm(xmlElem, hashDB, true);
  109. hashDB.Close();
  110. }
  111. [Test, ExpectedException(typeof(ExpectedTestException))]
  112. public void TestPutNoDuplicateWithUnsortedDuplicate()
  113. {
  114. testName = "TestPutNoDuplicateWithUnsortedDuplicate";
  115. testHome = testFixtureHome + "/" + testName;
  116. string dbFileName = testHome + "/" + testName + ".db";
  117. Configuration.ClearDir(testHome);
  118. HashDatabaseConfig hashConfig = new HashDatabaseConfig();
  119. hashConfig.Creation = CreatePolicy.ALWAYS;
  120. hashConfig.Duplicates = DuplicatesPolicy.UNSORTED;
  121. hashConfig.ErrorPrefix = testName;
  122. HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);
  123. DatabaseEntry key, data;
  124. key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
  125. data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
  126. try
  127. {
  128. hashDB.PutNoDuplicate(key, data);
  129. }
  130. catch (DatabaseException)
  131. {
  132. throw new ExpectedTestException();
  133. }
  134. finally
  135. {
  136. hashDB.Close();
  137. }
  138. }
  139. [Test, ExpectedException(typeof(ExpectedTestException))]
  140. public void TestKeyExistException()
  141. {
  142. testName = "TestKeyExistException";
  143. testHome = testFixtureHome + "/" + testName;
  144. string dbFileName = testHome + "/" + testName + ".db";
  145. Configuration.ClearDir(testHome);
  146. HashDatabaseConfig hashConfig = new HashDatabaseConfig();
  147. hashConfig.Creation = CreatePolicy.ALWAYS;
  148. hashConfig.Duplicates = DuplicatesPolicy.SORTED;
  149. HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);
  150. // Put the same record into db twice.
  151. DatabaseEntry key, data;
  152. key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
  153. data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
  154. try
  155. {
  156. hashDB.PutNoDuplicate(key, data);
  157. hashDB.PutNoDuplicate(key, data);
  158. }
  159. catch (KeyExistException)
  160. {
  161. throw new ExpectedTestException();
  162. }
  163. finally
  164. {
  165. hashDB.Close();
  166. }
  167. }
  168. [Test]
  169. public void TestPutNoDuplicate()
  170. {
  171. testName = "TestPutNoDuplicate";
  172. testHome = testFixtureHome + "/" + testName;
  173. string dbFileName = testHome + "/" + testName + ".db";
  174. Configuration.ClearDir(testHome);
  175. HashDatabaseConfig hashConfig =
  176. new HashDatabaseConfig();
  177. hashConfig.Creation = CreatePolicy.ALWAYS;
  178. hashConfig.Duplicates = DuplicatesPolicy.SORTED;
  179. hashConfig.TableSize = 20;
  180. HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);
  181. DatabaseEntry key, data;
  182. for (int i = 1; i <= 10; i++)
  183. {
  184. key = new DatabaseEntry(BitConverter.GetBytes(i));
  185. data = new DatabaseEntry(BitConverter.GetBytes(i));
  186. hashDB.PutNoDuplicate(key, data);
  187. }
  188. Assert.IsTrue(hashDB.Exists(
  189. new DatabaseEntry(BitConverter.GetBytes((int)5))));
  190. hashDB.Close();
  191. }
  192. [Test, ExpectedException(typeof(ExpectedTestException))]
  193. public void TestPutNoDuplicateWithTxn()
  194. {
  195. testName = "TestPutNoDuplicateWithTxn";
  196. testHome = testFixtureHome + "/" + testName;
  197. Configuration.ClearDir(testHome);
  198. // Open an environment.
  199. DatabaseEnvironmentConfig envConfig =
  200. new DatabaseEnvironmentConfig();
  201. envConfig.Create = true;
  202. envConfig.UseLogging = true;
  203. envConfig.UseMPool = true;
  204. envConfig.UseTxns = true;
  205. DatabaseEnvironment env = DatabaseEnvironment.Open(
  206. testHome, envConfig);
  207. // Open a hash database within a transaction.
  208. Transaction txn = env.BeginTransaction();
  209. HashDatabaseConfig dbConfig = new HashDatabaseConfig();
  210. dbConfig.Creation = CreatePolicy.IF_NEEDED;
  211. dbConfig.Duplicates = DuplicatesPolicy.SORTED;
  212. dbConfig.Env = env;
  213. HashDatabase db = HashDatabase.Open(testName + ".db", dbConfig, txn);
  214. DatabaseEntry dbt = new DatabaseEntry(BitConverter.GetBytes((int)100));
  215. db.PutNoDuplicate(dbt, dbt, txn);
  216. try
  217. {
  218. db.PutNoDuplicate(dbt, dbt, txn);
  219. }
  220. catch (KeyExistException)
  221. {
  222. throw new ExpectedTestException();
  223. }
  224. finally
  225. {
  226. // Close all.
  227. db.Close();
  228. txn.Commit();
  229. env.Close();
  230. }
  231. }
  232. [Test]
  233. public void TestStats()
  234. {
  235. testName = "TestStats";
  236. testHome = testFixtureHome + "/" + testName;
  237. string dbFileName = testHome + "/" + testName + ".db";
  238. Configuration.ClearDir(testHome);
  239. HashDatabaseConfig dbConfig = new HashDatabaseConfig();
  240. ConfigCase1(dbConfig);
  241. HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);
  242. HashStats stats = db.Stats();
  243. HashStats fastStats = db.FastStats();
  244. ConfirmStatsPart1Case1(stats);
  245. ConfirmStatsPart1Case1(fastStats);
  246. // Put 100 records into the database.
  247. PutRecordCase1(db, null);
  248. stats = db.Stats();
  249. ConfirmStatsPart2Case1(stats);
  250. // Delete some data to get some free pages.
  251. byte[] bigArray = new byte[262144];
  252. db.Delete(new DatabaseEntry(bigArray));
  253. stats = db.Stats();
  254. ConfirmStatsPart3Case1(stats);
  255. db.Close();
  256. }
  257. [Test]
  258. public void TestStatsInTxn()
  259. {
  260. testName = "TestStatsInTxn";
  261. testHome = testFixtureHome + "/" + testName;
  262. Configuration.ClearDir(testHome);
  263. StatsInTxn(testHome, testName, false);
  264. }
  265. [Test]
  266. public void TestStatsWithIsolation()
  267. {
  268. testName = "TestStatsWithIsolation";
  269. testHome = testFixtureHome + "/" + testName;
  270. Configuration.ClearDir(testHome);
  271. StatsInTxn(testHome, testName, true);
  272. }
  273. public void StatsInTxn(string home, string name, bool ifIsolation)
  274. {
  275. DatabaseEnvironmentConfig envConfig =
  276. new DatabaseEnvironmentConfig();
  277. EnvConfigCase1(envConfig);
  278. DatabaseEnvironment env = DatabaseEnvironment.Open(home, envConfig);
  279. Transaction openTxn = env.BeginTransaction();
  280. HashDatabaseConfig dbConfig = new HashDatabaseConfig();
  281. ConfigCase1(dbConfig);
  282. dbConfig.Env = env;
  283. HashDatabase db = HashDatabase.Open(name + ".db", dbConfig, openTxn);
  284. openTxn.Commit();
  285. Transaction statsTxn = env.BeginTransaction();
  286. HashStats stats;
  287. HashStats fastStats;
  288. if (ifIsolation == false)
  289. {
  290. stats = db.Stats(statsTxn);
  291. fastStats = db.Stats(statsTxn);
  292. }
  293. else
  294. {
  295. stats = db.Stats(statsTxn, Isolation.DEGREE_ONE);
  296. fastStats = db.Stats(statsTxn, Isolation.DEGREE_ONE);
  297. }
  298. ConfirmStatsPart1Case1(stats);
  299. // Put 100 records into the database.
  300. PutRecordCase1(db, statsTxn);
  301. if (ifIsolation == false)
  302. stats = db.Stats(statsTxn);
  303. else
  304. stats = db.Stats(statsTxn, Isolation.DEGREE_TWO);
  305. ConfirmStatsPart2Case1(stats);
  306. // Delete some data to get some free pages.
  307. byte[] bigArray = new byte[262144];
  308. db.Delete(new DatabaseEntry(bigArray), statsTxn);
  309. if (ifIsolation == false)
  310. stats = db.Stats(statsTxn);
  311. else
  312. stats = db.Stats(statsTxn, Isolation.DEGREE_THREE);
  313. ConfirmStatsPart3Case1(stats);
  314. statsTxn.Commit();
  315. db.Close();
  316. env.Close();
  317. }
  318. public void EnvConfigCase1(DatabaseEnvironmentConfig cfg)
  319. {
  320. cfg.Create = true;
  321. cfg.UseTxns = true;
  322. cfg.UseMPool = true;
  323. cfg.UseLogging = true;
  324. }
  325. public void ConfigCase1(HashDatabaseConfig dbConfig)
  326. {
  327. dbConfig.Creation = CreatePolicy.IF_NEEDED;
  328. dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
  329. dbConfig.FillFactor = 10;
  330. dbConfig.TableSize = 20;
  331. dbConfig.PageSize = 4096;
  332. }
  333. public void PutRecordCase1(HashDatabase db, Transaction txn)
  334. {
  335. byte[] bigArray = new byte[262144];
  336. for (int i = 0; i < 50; i++)
  337. {
  338. if (txn == null)
  339. db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
  340. new DatabaseEntry(BitConverter.GetBytes(i)));
  341. else
  342. db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
  343. new DatabaseEntry(BitConverter.GetBytes(i)), txn);
  344. }
  345. for (int i = 50; i < 100; i++)
  346. {
  347. if (txn == null)
  348. db.Put(new DatabaseEntry(bigArray),
  349. new DatabaseEntry(bigArray));
  350. else
  351. db.Put(new DatabaseEntry(bigArray),
  352. new DatabaseEntry(bigArray), txn);
  353. }
  354. }
  355. public void ConfirmStatsPart1Case1(HashStats stats)
  356. {
  357. Assert.AreEqual(10, stats.FillFactor);
  358. Assert.AreEqual(4096, stats.PageSize);
  359. Assert.AreNotEqual(0, stats.Version);
  360. }
  361. public void ConfirmStatsPart2Case1(HashStats stats)
  362. {
  363. Assert.AreNotEqual(0, stats.BigPages);
  364. Assert.AreNotEqual(0, stats.BigPagesFreeBytes);
  365. Assert.AreNotEqual(0, stats.BucketPagesFreeBytes);
  366. Assert.AreNotEqual(0, stats.DuplicatePages);
  367. Assert.AreNotEqual(0, stats.DuplicatePagesFreeBytes);
  368. Assert.AreNotEqual(0, stats.MagicNumber);
  369. Assert.AreNotEqual(0, stats.MetadataFlags);
  370. Assert.AreEqual(100, stats.nData);
  371. Assert.AreNotEqual(0, stats.nHashBuckets);
  372. Assert.AreEqual(51, stats.nKeys);
  373. Assert.AreNotEqual(0, stats.nPages);
  374. Assert.AreEqual(0, stats.OverflowPages);
  375. Assert.AreEqual(0, stats.OverflowPagesFreeBytes);
  376. }
  377. public void ConfirmStatsPart3Case1(HashStats stats)
  378. {
  379. Assert.AreNotEqual(0, stats.FreePages);
  380. }
  381. public static void Confirm(XmlElement xmlElem,
  382. HashDatabase hashDB, bool compulsory)
  383. {
  384. DatabaseTest.Confirm(xmlElem, hashDB, compulsory);
  385. Configuration.ConfirmCreatePolicy(xmlElem,
  386. "Creation", hashDB.Creation, compulsory);
  387. Configuration.ConfirmDuplicatesPolicy(xmlElem,
  388. "Duplicates", hashDB.Duplicates, compulsory);
  389. Configuration.ConfirmUint(xmlElem,
  390. "FillFactor", hashDB.FillFactor, compulsory);
  391. Configuration.ConfirmUint(xmlElem,
  392. "NumElements", hashDB.TableSize * hashDB.FillFactor,
  393. compulsory);
  394. Assert.AreEqual(DatabaseType.HASH, hashDB.Type);
  395. string type = hashDB.Type.ToString();
  396. Assert.IsNotNull(type);
  397. }
  398. }
  399. }