summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/scr037/SecondaryHashDatabaseTest.cs
blob: 839e8c9616717ecb1da816c5f2b9b77e231f1bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2009 Oracle.  All rights reserved.
 *
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml;
using NUnit.Framework;
using BerkeleyDB;

namespace CsharpAPITest
{
	[TestFixture]
	public class SecondaryHashDatabaseTest
	{
		private string testFixtureHome;
		private string testFixtureName;
		private string testName;
		private string testHome;

		[TestFixtureSetUp]
		public void RunBeforeTests()
		{
			testFixtureName = "SecondaryHashDatabaseTest";
			testFixtureHome = "./TestOut/" + testFixtureName;

			Configuration.ClearDir(testFixtureHome);
		}

		[Test]
		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();
		}

		public uint HashFunction(byte[] data)
		{
			data[0] = 0;
			return BitConverter.ToUInt32(data, 0);
		}

		[Test]
		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();
		}

		[Test]
		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 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();
		}


		/*
		 * Tests to all Open() share the same configuration in 
		 * AllTestData.xml.
		 */
		[Test]
		public void TestOpen()
		{
			testName = "TestOpen";
			testHome = testFixtureHome + "/" + testName;
			string dbFileName = testHome + "/" + testName + ".db";
			string dbSecFileName = testHome + "/" +
			    testName + "_sec.db";

			Configuration.ClearDir(testHome);

			OpenSecHashDB(testFixtureName, "TestOpen",
			    dbFileName, dbSecFileName, false);
		}

		[Test]
		public void TestOpenWithDBName()
		{
			testName = "TestOpenWithDBName";
			testHome = testFixtureHome + "/" + testName;
			string dbFileName = testHome + "/" + testName + ".db";
			string dbSecFileName = testHome + "/" +
			    testName + "_sec.db";

			Configuration.ClearDir(testHome);

			OpenSecHashDB(testFixtureName, "TestOpen",
			    dbFileName, dbSecFileName, true);
		}

		public void OpenSecHashDB(string className,
		    string funName, string dbFileName, string dbSecFileName,
		    bool ifDBName)
		{
			XmlElement xmlElem = Configuration.TestSetUp(
			    className, funName);

			// Open a primary recno database.
			HashDatabaseConfig primaryDBConfig =
			    new HashDatabaseConfig();
			primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
			HashDatabase primaryDB;

			/*
			 * If secondary database name is given, the primary 
			 * database is also opened with database name.
			 */
			if (ifDBName == false)
				primaryDB = HashDatabase.Open(dbFileName,
				    primaryDBConfig);
			else
				primaryDB = HashDatabase.Open(dbFileName,
				    "primary", primaryDBConfig);

			try
			{
				// Open a new secondary database.
				SecondaryHashDatabaseConfig secHashDBConfig =
				    new SecondaryHashDatabaseConfig(
				    primaryDB, null);
				SecondaryHashDatabaseConfigTest.Config(
				    xmlElem, ref secHashDBConfig, false);
				secHashDBConfig.Creation =
				    CreatePolicy.IF_NEEDED;

				SecondaryHashDatabase secHashDB;
				if (ifDBName == false)
					secHashDB = SecondaryHashDatabase.Open(
					    dbSecFileName, secHashDBConfig);
				else
					secHashDB = SecondaryHashDatabase.Open(
					    dbSecFileName, "secondary",
					    secHashDBConfig);

				// Close the secondary database.
				secHashDB.Close();

				// Open the existing secondary database.
				SecondaryDatabaseConfig secDBConfig =
				    new SecondaryDatabaseConfig(
				    primaryDB, null);

				SecondaryDatabase secDB;
				if (ifDBName == false)
					secDB = SecondaryHashDatabase.Open(
					    dbSecFileName, secDBConfig);
				else
					secDB = SecondaryHashDatabase.Open(
					    dbSecFileName, "secondary", secDBConfig);

				// Close secondary database.
				secDB.Close();
			}
			catch (DatabaseException)
			{
				throw new TestException();
			}
			finally
			{
				// Close primary database.
				primaryDB.Close();
			}
		}

		[Test]
		public void TestOpenWithinTxn()
		{
			testName = "TestOpenWithinTxn";
			testHome = testFixtureHome + "/" + testName;
			string dbFileName = testName + ".db";
			string dbSecFileName = testName + "_sec.db";

			Configuration.ClearDir(testHome);

			OpenSecHashDBWithinTxn(testFixtureName,
			    "TestOpen", testHome, dbFileName,
			    dbSecFileName, false);
		}

		[Test]
		public void TestOpenDBNameWithinTxn()
		{
			testName = "TestOpenDBNameWithinTxn";
			testHome = testFixtureHome + "/" + testName;
			string dbFileName = testName + ".db";
			string dbSecFileName = testName + "_sec.db";

			Configuration.ClearDir(testHome);

			OpenSecHashDBWithinTxn(testFixtureName,
			    "TestOpen", testHome, dbFileName,
			    dbSecFileName, true);
		}

		public void OpenSecHashDBWithinTxn(string className,
		    string funName, string home, string dbFileName,
		    string dbSecFileName, bool ifDbName)
		{
			XmlElement xmlElem = Configuration.TestSetUp(
			    className, funName);

			// Open an environment.
			DatabaseEnvironmentConfig envConfig =
			    new DatabaseEnvironmentConfig();
			envConfig.Create = true;
			envConfig.UseTxns = true;
			envConfig.UseMPool = true;
			envConfig.UseLogging = true;
			DatabaseEnvironment env = DatabaseEnvironment.Open(
			    home, envConfig);

			// Open a primary hash database.
			Transaction openDBTxn = env.BeginTransaction();
			HashDatabaseConfig dbConfig =
			    new HashDatabaseConfig();
			dbConfig.Creation = CreatePolicy.IF_NEEDED;
			dbConfig.Env = env;
			HashDatabase db = HashDatabase.Open(
			    dbFileName, dbConfig, openDBTxn);
			openDBTxn.Commit();

			// Open a secondary hash database.
			Transaction openSecTxn = env.BeginTransaction();
			SecondaryHashDatabaseConfig secDBConfig =
			    new SecondaryHashDatabaseConfig(db,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen));
			SecondaryHashDatabaseConfigTest.Config(xmlElem,
			    ref secDBConfig, false);
			secDBConfig.HashFunction = null;
			secDBConfig.Env = env;
			SecondaryHashDatabase secDB;
			if (ifDbName == false)
				secDB = SecondaryHashDatabase.Open(
				    dbSecFileName, secDBConfig, openSecTxn);
			else
				secDB = SecondaryHashDatabase.Open(
				    dbSecFileName, "secondary", secDBConfig,
				    openSecTxn);
			openSecTxn.Commit();

			// Confirm its flags configured in secDBConfig.
			Confirm(xmlElem, secDB, true);
			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 = SecondaryHashDatabase.Open(
				    dbSecFileName, secConfig, secTxn);
			else
				secExDB = SecondaryHashDatabase.Open(
				    dbSecFileName, "secondary", secConfig,
				    secTxn);
			secExDB.Close();
			secTxn.Commit();

			db.Close();
			env.Close();
		}

		public DatabaseEntry SecondaryKeyGen(
		    DatabaseEntry key, DatabaseEntry data)
		{
			DatabaseEntry dbtGen;
			dbtGen = new DatabaseEntry(data.Data);
			return dbtGen;
		}

		public int SecondaryEntryComparison(
		    DatabaseEntry dbt1, DatabaseEntry dbt2)
		{
			int a, b;
			a = BitConverter.ToInt32(dbt1.Data, 0);
			b = BitConverter.ToInt32(dbt2.Data, 0);
			return a - b;
		}

		public static void Confirm(XmlElement xmlElem,
		    SecondaryHashDatabase secDB, bool compulsory)
		{
			Configuration.ConfirmUint(xmlElem, "FillFactor",
			    secDB.FillFactor, compulsory);
			Configuration.ConfirmUint(xmlElem, "NumElements",
			    secDB.TableSize * secDB.FillFactor, compulsory);
		}
	}
}