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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2009 Oracle. All rights reserved.
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using NUnit.Framework;
using BerkeleyDB;
namespace CsharpAPITest
{
[TestFixture]
public class RecnoDatabaseTest : DatabaseTest
{
private string testFixtureHome;
private string testFixtureName;
private string testName;
private string testHome;
[TestFixtureSetUp]
public void RunBeforeTests()
{
testFixtureName = "RecnoDatabaseTest";
testFixtureHome = "./TestOut/" + testFixtureName;
Configuration.ClearDir(testFixtureHome);
}
[Test]
public void TestOpenExistingRecnoDB()
{
testName = "TestOpenExistingRecnoDB";
testHome = testFixtureHome + "/" + testName;
string recnoDBFileName = testHome + "/" +
testName + ".db";
Configuration.ClearDir(testHome);
RecnoDatabaseConfig recConfig =
new RecnoDatabaseConfig();
recConfig.Creation = CreatePolicy.ALWAYS;
RecnoDatabase recDB = RecnoDatabase.Open(
recnoDBFileName, recConfig);
recDB.Close();
RecnoDatabaseConfig dbConfig = new RecnoDatabaseConfig();
string backingFile = testHome + "/backingFile";
File.Copy(recnoDBFileName, backingFile);
dbConfig.BackingFile = backingFile;
RecnoDatabase db = RecnoDatabase.Open(recnoDBFileName, dbConfig);
Assert.AreEqual(db.Type, DatabaseType.RECNO);
db.Close();
}
[Test]
public void TestOpenNewRecnoDB()
{
RecnoDatabase recnoDB;
RecnoDatabaseConfig recnoConfig;
testName = "TestOpenNewRecnoDB";
testHome = testFixtureHome + "/" + testName;
string recnoDBFileName = testHome + "/" +
testName + ".db";
Configuration.ClearDir(testHome);
XmlElement xmlElem = Configuration.TestSetUp(
testFixtureName, testName);
recnoConfig = new RecnoDatabaseConfig();
RecnoDatabaseConfigTest.Config(xmlElem,
ref recnoConfig, true);
recnoDB = RecnoDatabase.Open(recnoDBFileName,
recnoConfig);
Confirm(xmlElem, recnoDB, true);
recnoDB.Close();
}
[Test]
public void TestAppendWithoutTxn()
{
testName = "TestAppendWithoutTxn";
testHome = testFixtureHome + "/" + testName;
string recnoDBFileName = testHome + "/" +
testName + ".db";
Configuration.ClearDir(testHome);
RecnoDatabaseConfig recnoConfig =
new RecnoDatabaseConfig();
recnoConfig.Creation = CreatePolicy.ALWAYS;
RecnoDatabase recnoDB = RecnoDatabase.Open(
recnoDBFileName, recnoConfig);
DatabaseEntry data = new DatabaseEntry(
ASCIIEncoding.ASCII.GetBytes("data"));
uint num = recnoDB.Append(data);
DatabaseEntry key = new DatabaseEntry(
BitConverter.GetBytes(num));
Assert.IsTrue(recnoDB.Exists(key));
KeyValuePair<DatabaseEntry, DatabaseEntry> record =
recnoDB.Get(key);
Assert.IsTrue(data.Data.Length ==
record.Value.Data.Length);
for (int i = 0; i < data.Data.Length; i++)
Assert.IsTrue(data.Data[i] ==
record.Value.Data[i]);
recnoDB.Close();
}
[Test]
public void TestCompact()
{
testName = "TestCompact";
testHome = testFixtureHome + "/" + testName;
string recnoDBFileName = testHome + "/" +
testName + ".db";
Configuration.ClearDir(testHome);
RecnoDatabaseConfig recnoConfig =
new RecnoDatabaseConfig();
recnoConfig.Creation = CreatePolicy.ALWAYS;
recnoConfig.Length = 512;
DatabaseEntry key, data;
RecnoDatabase recnoDB;
using (recnoDB = RecnoDatabase.Open(
recnoDBFileName, recnoConfig))
{
for (int i = 1; i <= 5000; i++)
{
data = new DatabaseEntry(
BitConverter.GetBytes(i));
recnoDB.Append(data);
}
for (int i = 1; i <= 5000; i++)
{
if (i > 500 && (i % 5 != 0))
{
key = new DatabaseEntry(
BitConverter.GetBytes(i));
recnoDB.Delete(key);
}
}
int startInt = 1;
int stopInt = 2500;
DatabaseEntry start, stop;
start = new DatabaseEntry(
BitConverter.GetBytes(startInt));
stop = new DatabaseEntry(
BitConverter.GetBytes(stopInt));
Assert.IsTrue(recnoDB.Exists(start));
Assert.IsTrue(recnoDB.Exists(stop));
CompactConfig cCfg = new CompactConfig();
cCfg.start = start;
cCfg.stop = stop;
cCfg.FillPercentage = 30;
cCfg.Pages = 1;
cCfg.returnEnd = true;
cCfg.Timeout = 5000;
cCfg.TruncatePages = true;
CompactData compactData = recnoDB.Compact(cCfg);
Assert.IsNotNull(compactData.End);
Assert.AreNotEqual(0, compactData.PagesExamined);
}
}
[Test]
public void TestStats()
{
testName = "TestStats";
testHome = testFixtureHome + "/" + testName;
string dbFileName = testHome + "/" +
testName + ".db";
Configuration.ClearDir(testHome);
RecnoDatabaseConfig dbConfig =
new RecnoDatabaseConfig();
ConfigCase1(dbConfig);
RecnoDatabase db = RecnoDatabase.Open(dbFileName,
dbConfig);
RecnoStats stats = db.Stats();
ConfirmStatsPart1Case1(stats);
// Put 1000 records into the database.
PutRecordCase1(db, null);
stats = db.Stats();
ConfirmStatsPart2Case1(stats);
// Delete 500 records.
for (int i = 250; i <= 750; i++)
db.Delete(new DatabaseEntry(BitConverter.GetBytes(i)));
stats = db.Stats();
ConfirmStatsPart3Case1(stats);
db.Close();
}
[Test]
public void TestStatsInTxn()
{
testName = "TestStatsInTxn";
testHome = testFixtureHome + "/" + testName;
Configuration.ClearDir(testHome);
StatsInTxn(testHome, testName, false);
}
[Test]
public void TestStatsWithIsolation()
{
testName = "TestStatsWithIsolation";
testHome = testFixtureHome + "/" + testName;
Configuration.ClearDir(testHome);
StatsInTxn(testHome, testName, true);
}
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();
RecnoDatabaseConfig dbConfig =
new RecnoDatabaseConfig();
ConfigCase1(dbConfig);
dbConfig.Env = env;
RecnoDatabase db = RecnoDatabase.Open(name + ".db",
dbConfig, openTxn);
openTxn.Commit();
Transaction statsTxn = env.BeginTransaction();
RecnoStats stats;
RecnoStats fastStats;
if (ifIsolation == false)
{
stats = db.Stats(statsTxn);
fastStats = db.FastStats(statsTxn);
}
else
{
stats = db.Stats(statsTxn, Isolation.DEGREE_ONE);
fastStats = db.FastStats(statsTxn,
Isolation.DEGREE_ONE);
}
ConfirmStatsPart1Case1(stats);
// Put 1000 records into the database.
PutRecordCase1(db, statsTxn);
if (ifIsolation == false)
{
stats = db.Stats(statsTxn);
fastStats = db.FastStats(statsTxn);
}
else
{
stats = db.Stats(statsTxn, Isolation.DEGREE_TWO);
fastStats = db.FastStats(statsTxn,
Isolation.DEGREE_TWO);
}
ConfirmStatsPart2Case1(stats);
// Delete 500 records.
for (int i = 250; i <= 750; i++)
db.Delete(new DatabaseEntry(BitConverter.GetBytes(i)),
statsTxn);
if (ifIsolation == false)
{
stats = db.Stats(statsTxn);
fastStats = db.FastStats(statsTxn);
}
else
{
stats = db.Stats(statsTxn, Isolation.DEGREE_THREE);
fastStats = db.FastStats(statsTxn,
Isolation.DEGREE_THREE);
}
ConfirmStatsPart3Case1(stats);
statsTxn.Commit();
db.Close();
env.Close();
}
public void EnvConfigCase1(DatabaseEnvironmentConfig cfg)
{
cfg.Create = true;
cfg.UseTxns = true;
cfg.UseMPool = true;
cfg.UseLogging = true;
}
public void ConfigCase1(RecnoDatabaseConfig dbConfig)
{
dbConfig.Creation = CreatePolicy.IF_NEEDED;
dbConfig.PageSize = 4096;
dbConfig.Length = 4000;
dbConfig.PadByte = 256;
}
public void PutRecordCase1(RecnoDatabase db, Transaction txn)
{
for (int i = 1; i <= 1000; i++)
{
if (txn == null)
db.Put(new DatabaseEntry(
BitConverter.GetBytes(i)),
new DatabaseEntry(BitConverter.GetBytes(i)));
else
db.Put(new DatabaseEntry(
BitConverter.GetBytes(i)),
new DatabaseEntry(
BitConverter.GetBytes(i)), txn);
}
}
public void ConfirmStatsPart1Case1(RecnoStats stats)
{
Assert.AreEqual(1, stats.EmptyPages);
Assert.AreEqual(1, stats.Levels);
Assert.AreNotEqual(0, stats.MagicNumber);
Assert.AreEqual(10, stats.MetadataFlags);
Assert.AreEqual(2, stats.MinKey);
Assert.AreEqual(2, stats.nPages);
Assert.AreEqual(4096, stats.PageSize);
Assert.AreEqual(4000, stats.RecordLength);
Assert.AreEqual(256, stats.RecordPadByte);
Assert.AreEqual(9, stats.Version);
}
public void ConfirmStatsPart2Case1(RecnoStats stats)
{
Assert.AreEqual(0, stats.DuplicatePages);
Assert.AreEqual(0, stats.DuplicatePagesFreeBytes);
Assert.AreNotEqual(0, stats.InternalPages);
Assert.AreNotEqual(0, stats.InternalPagesFreeBytes);
Assert.AreNotEqual(0, stats.LeafPages);
Assert.AreNotEqual(0, stats.LeafPagesFreeBytes);
Assert.AreEqual(1000, stats.nData);
Assert.AreEqual(1000, stats.nKeys);
Assert.AreNotEqual(0, stats.OverflowPages);
Assert.AreNotEqual(0, stats.OverflowPagesFreeBytes);
}
public void ConfirmStatsPart3Case1(RecnoStats stats)
{
Assert.AreNotEqual(0, stats.FreePages);
}
[Test]
public void TestTruncateUnusedPages()
{
testName = "TestTruncateUnusedPages";
testHome = testFixtureHome + "/" + testName;
Configuration.ClearDir(testHome);
DatabaseEnvironmentConfig envConfig =
new DatabaseEnvironmentConfig();
envConfig.Create = true;
envConfig.UseCDB = true;
envConfig.UseMPool = true;
DatabaseEnvironment env = DatabaseEnvironment.Open(
testHome, envConfig);
RecnoDatabaseConfig dbConfig =
new RecnoDatabaseConfig();
dbConfig.Creation = CreatePolicy.IF_NEEDED;
dbConfig.Env = env;
dbConfig.PageSize = 512;
RecnoDatabase db = RecnoDatabase.Open(
testName + ".db", dbConfig);
ModifyRecordsInDB(db, null);
Assert.Less(0, db.TruncateUnusedPages());
db.Close();
env.Close();
}
[Test]
public void TestTruncateUnusedPagesInTxn()
{
testName = "TestTruncateUnusedPagesInTxn";
testHome = testFixtureHome + "/" + testName;
Configuration.ClearDir(testHome);
DatabaseEnvironmentConfig envConfig =
new DatabaseEnvironmentConfig();
envConfig.Create = true;
envConfig.UseLogging = true;
envConfig.UseMPool = true;
envConfig.UseTxns = true;
DatabaseEnvironment env = DatabaseEnvironment.Open(
testHome, envConfig);
Transaction openTxn = env.BeginTransaction();
RecnoDatabaseConfig dbConfig =
new RecnoDatabaseConfig();
dbConfig.Creation = CreatePolicy.IF_NEEDED;
dbConfig.Env = env;
dbConfig.PageSize = 512;
RecnoDatabase db = RecnoDatabase.Open(
testName + ".db", dbConfig, openTxn);
openTxn.Commit();
Transaction modifyTxn = env.BeginTransaction();
ModifyRecordsInDB(db, modifyTxn);
Assert.Less(0, db.TruncateUnusedPages(modifyTxn));
modifyTxn.Commit();
db.Close();
env.Close();
}
public void ModifyRecordsInDB(RecnoDatabase db,
Transaction txn)
{
uint[] recnos = new uint[100];
if (txn == null)
{
// Add a lot of records into database.
for (int i = 0; i < 100; i++)
recnos[i] = db.Append(new DatabaseEntry(
new byte[10240]));
// Remove some records from database.
for (int i = 30; i < 100; i++)
db.Delete(new DatabaseEntry(
BitConverter.GetBytes(recnos[i])));
}
else
{
// Add a lot of records into database in txn.
for (int i = 0; i < 100; i++)
recnos[i] = db.Append(new DatabaseEntry(
new byte[10240]), txn);
// Remove some records from database in txn.
for (int i = 30; i < 100; i++)
db.Delete(new DatabaseEntry(
BitConverter.GetBytes(recnos[i])), txn);
}
}
public static void Confirm(XmlElement xmlElem,
RecnoDatabase recnoDB, bool compulsory)
{
DatabaseTest.Confirm(xmlElem, recnoDB, compulsory);
// Confirm recno database specific field/property
Configuration.ConfirmInt(xmlElem, "Delimiter",
recnoDB.RecordDelimiter, compulsory);
Configuration.ConfirmUint(xmlElem, "Length",
recnoDB.RecordLength, compulsory);
Configuration.ConfirmInt(xmlElem, "PadByte",
recnoDB.RecordPad, compulsory);
Configuration.ConfirmBool(xmlElem, "Renumber",
recnoDB.Renumber, compulsory);
Configuration.ConfirmBool(xmlElem, "Snapshot",
recnoDB.Snapshot, compulsory);
Assert.AreEqual(DatabaseType.RECNO, recnoDB.Type);
string type = recnoDB.Type.ToString();
Assert.IsNotNull(type);
}
}
}
|