summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/scr037/SecondaryDatabaseTest.cs
blob: 40f54fe2e77e91510d2f7ff466636024d394e38b (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*-
 * 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 SecondaryDatabaseTest
	{
		private string testFixtureHome;
		private string testFixtureName;
		private string testName;
		private string testHome;

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

			Configuration.ClearDir(testFixtureHome);
		}

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

			Configuration.ClearDir(testHome);

			// Open primary database.
			BTreeDatabaseConfig primaryDBConfig =
			    new BTreeDatabaseConfig();
			primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
			BTreeDatabase primaryDB =
			    BTreeDatabase.Open(dbFileName, primaryDBConfig);

			// Open secondary database.
			SecondaryBTreeDatabaseConfig secDBConfig =
			    new SecondaryBTreeDatabaseConfig(primaryDB,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen));
			SecondaryBTreeDatabase secDB =
			    SecondaryBTreeDatabase.Open(dbFileName,
			    secDBConfig);

			primaryDB.Put(new DatabaseEntry(
			    BitConverter.GetBytes((int)1)),
			    new DatabaseEntry(BitConverter.GetBytes((int)11)));

			KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
			pair = secDB.Get(new DatabaseEntry(
			    BitConverter.GetBytes((int)11)));
			Assert.IsNotNull(pair.Value);

			// Close secondary database.
			secDB.Close();

			// Close primary database.
			primaryDB.Close();
		}

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

			Configuration.ClearDir(testHome);

			// Open primary database.
			BTreeDatabaseConfig primaryDBConfig =
			    new BTreeDatabaseConfig();
			primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
			BTreeDatabase primaryDB =
			    BTreeDatabase.Open(dbFileName, primaryDBConfig);

			// Open secondary database.
			SecondaryBTreeDatabaseConfig secDBConfig =
			    new SecondaryBTreeDatabaseConfig(primaryDB,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen));
			SecondaryBTreeDatabase secDB =
			    SecondaryBTreeDatabase.Open(dbFileName,
			    secDBConfig);

			primaryDB.Put(new DatabaseEntry(
			    BitConverter.GetBytes((int)1)),
			    new DatabaseEntry(BitConverter.GetBytes((int)11)));


			SecondaryCursor cursor = secDB.SecondaryCursor();
			cursor.Move(new DatabaseEntry(
			    BitConverter.GetBytes((int)11)), true);
			Assert.AreEqual(BitConverter.GetBytes((int)11),
			    cursor.Current.Key.Data);

			// Close the cursor.
			cursor.Close();

			// Close secondary database.
			secDB.Close();

			// Close primary database.
			primaryDB.Close();
		}

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

			Configuration.ClearDir(testHome);

			BTreeDatabase db;
			SecondaryBTreeDatabase secDB;
			OpenPrimaryAndSecondaryDB(dbFileName, out db, out secDB);

			for (int i = 0; i < 10; i++)
				db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
				    new DatabaseEntry(BitConverter.GetBytes((int)i)));

			CursorConfig cursorConfig = new CursorConfig();
			cursorConfig.WriteCursor = false;
			SecondaryCursor cursor =
			    secDB.SecondaryCursor(cursorConfig);

			cursor.Move(new DatabaseEntry(
			    BitConverter.GetBytes((int)5)), true);

			Assert.AreEqual(1, cursor.Count());

			// Close the cursor.
			cursor.Close();

			// Close secondary database.
			secDB.Close();

			// Close primary database.
			db.Close();
		}

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

			Configuration.ClearDir(testHome);

			GetSecondaryCursurWithTxn(testHome, testName, false);
		}

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

			Configuration.ClearDir(testHome);
			GetSecondaryCursurWithTxn(testHome, testName, true);
		}

		public void GetSecondaryCursurWithTxn(string home,
		    string name, bool ifCfg)
		{
			string dbFileName = name + ".db";
			SecondaryCursor cursor;

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


			// Open primary/secondary database.
			Transaction txn = env.BeginTransaction();
			BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
			dbConfig.Creation = CreatePolicy.IF_NEEDED;
			dbConfig.Env = env;
			BTreeDatabase db = BTreeDatabase.Open(dbFileName,
			    dbConfig, txn);

			SecondaryBTreeDatabaseConfig secDBConfig = new
			    SecondaryBTreeDatabaseConfig(db,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen));
			secDBConfig.Env = env;
			SecondaryBTreeDatabase secDB =
			    SecondaryBTreeDatabase.Open(dbFileName,
			    secDBConfig, txn);

			for (int i = 0; i < 10; i++)
				db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
				    new DatabaseEntry(BitConverter.GetBytes((int)i)), txn);


			// Create secondary cursor.
			if (ifCfg == false)
				secDB.SecondaryCursor(txn);
			else if (ifCfg == true)
			{
				CursorConfig cursorConfig = new CursorConfig();
				cursorConfig.WriteCursor = false;
				cursor = secDB.SecondaryCursor(cursorConfig, txn);
				cursor.Close();
			}

			secDB.Close();
			db.Close();
			txn.Commit();
			env.Close();
		}

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

			Configuration.ClearDir(testHome);

			OpenSecQueueDB(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);
			OpenSecQueueDB(dbFileName, dbSecFileName, true);
		}

		public void OpenSecQueueDB(string dbFileName, 
		    string dbSecFileName, bool ifDBName)
		{
			// Open a primary btree database.
			BTreeDatabaseConfig primaryDBConfig =
			    new BTreeDatabaseConfig();
			primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
			BTreeDatabase primaryDB;

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

			try
			{
				// Open a new secondary database.
				SecondaryBTreeDatabaseConfig secBTDBConfig =
				    new SecondaryBTreeDatabaseConfig(
				    primaryDB, null);
				secBTDBConfig.Creation =
				    CreatePolicy.IF_NEEDED;

				SecondaryBTreeDatabase secBTDB;
				if (ifDBName == false)
					secBTDB = SecondaryBTreeDatabase.Open(
					    dbSecFileName, secBTDBConfig);
				else
					secBTDB = SecondaryBTreeDatabase.Open(
					    dbSecFileName, "secondary",
					    secBTDBConfig);

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

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

				SecondaryDatabase secDB;
				if (ifDBName == false)
					secDB = SecondaryBTreeDatabase.Open(
					    dbSecFileName, secDBConfig);
				else
					secDB = SecondaryBTreeDatabase.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);

			OpenSecQueueDBWithinTxn(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);

			OpenSecQueueDBWithinTxn(testHome, dbFileName,
			    dbSecFileName, true);
		}

		public void OpenSecQueueDBWithinTxn(string home,
		    string dbFileName, string dbSecFileName, bool ifDbName)
		{
			// 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 btree database.
			Transaction openDBTxn = env.BeginTransaction();
			BTreeDatabaseConfig dbConfig =
			    new BTreeDatabaseConfig();
			dbConfig.Creation = CreatePolicy.IF_NEEDED;
			dbConfig.Env = env;
			BTreeDatabase db = BTreeDatabase.Open(
			    dbFileName, dbConfig, openDBTxn);
			openDBTxn.Commit();

			// Open a secondary btree database.
			Transaction openSecTxn = env.BeginTransaction();
			SecondaryBTreeDatabaseConfig secDBConfig =
			    new SecondaryBTreeDatabaseConfig(db,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen));
			secDBConfig.Env = env;
			secDBConfig.Creation = CreatePolicy.IF_NEEDED;

			SecondaryBTreeDatabase secDB;
			if (ifDbName == false)
				secDB = SecondaryBTreeDatabase.Open(
				    dbSecFileName, secDBConfig, openSecTxn);
			else
				secDB = SecondaryBTreeDatabase.Open(
				    dbSecFileName, "secondary", secDBConfig,
				    openSecTxn);
			openSecTxn.Commit();
			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 = SecondaryBTreeDatabase.Open(
				    dbSecFileName, secConfig, secTxn);
			else
				secExDB = SecondaryBTreeDatabase.Open(
				    dbSecFileName, "secondary", secConfig,
				    secTxn);
			secExDB.Close();
			secTxn.Commit();

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

		public void OpenPrimaryAndSecondaryDB(string dbFileName,
		    out BTreeDatabase primaryDB,
		    out SecondaryBTreeDatabase secDB)
		{
			// Open primary database.
			BTreeDatabaseConfig primaryDBConfig =
			    new BTreeDatabaseConfig();
			primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
			primaryDB =
			    BTreeDatabase.Open(dbFileName, primaryDBConfig);

			// Open secondary database.
			SecondaryBTreeDatabaseConfig secDBConfig =
			    new SecondaryBTreeDatabaseConfig(primaryDB,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen));
			secDB = SecondaryBTreeDatabase.Open(dbFileName,
			    secDBConfig);
		}

		[Test, ExpectedException(typeof(ExpectedTestException))]
		public void TestBadSecondaryException()
		{
			testName = "TestBadSecondaryException";
			testHome = testFixtureHome + "/" + testName;
			string dbFileName = testHome + "/" + testName + ".db";
			string secDBFileName = testHome + "/" +
			    testName + "_sec.db";

			Configuration.ClearDir(testHome);

			// Open primary database.
			BTreeDatabaseConfig btreeDBConfig =
			    new BTreeDatabaseConfig();
			btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
			BTreeDatabase btreeDB =
			    BTreeDatabase.Open(dbFileName, btreeDBConfig);

			// Open secondary database.
			SecondaryBTreeDatabaseConfig secBtDbConfig =
			    new SecondaryBTreeDatabaseConfig(btreeDB,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen));
			secBtDbConfig.Creation = CreatePolicy.IF_NEEDED;
			SecondaryBTreeDatabase secBtDb =
			    SecondaryBTreeDatabase.Open(secDBFileName,
			    secBtDbConfig);

			// Put some data into primary database.
			for (int i = 0; i < 10; i++)
				btreeDB.Put(new DatabaseEntry(
				    BitConverter.GetBytes(i)),
				    new DatabaseEntry(BitConverter.GetBytes(i)));

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

			// Delete record(5, 5) in primary database.
			btreeDB.Delete(new DatabaseEntry(
			    BitConverter.GetBytes((int)5)));

			// Reopen the secondary database.
			SecondaryDatabase secDB = SecondaryDatabase.Open(
			    secDBFileName,
			    new SecondaryDatabaseConfig(btreeDB,
			    new SecondaryKeyGenDelegate(SecondaryKeyGen)));

			/*
			 * Getting record(5, 5) by secondary database should 
			 * throw BadSecondaryException since it has been
			 * deleted in the primary database.
			 */
			try
			{
				secDB.Exists(new DatabaseEntry(
				    BitConverter.GetBytes((int)5)));
			}
			catch (BadSecondaryException)
			{
				throw new ExpectedTestException();
			}
			finally
			{
				secDB.Close();
				btreeDB.Close();
			}
		}

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

	}

}