summaryrefslogtreecommitdiff
path: root/db-4.8.30/test/scr037/LogCursorTest.cs
blob: 32a793a32ca376b77c13c2ca6eb1865a21313ee6 (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
/*-
 * 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.Xml;
using NUnit.Framework;
using BerkeleyDB;

namespace CsharpAPITest
{
	[TestFixture]
	public class LogCursorTest
	{
		private string testFixtureHome;
		private string testFixtureName;
		private string testName;
		private string testHome;
		DatabaseEnvironment env;

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

			/*
			 * Delete existing test ouput directory and files specified 
			 * for the current test fixture and then create a new one.
			 */
			Configuration.ClearDir(testFixtureHome);
		}

		[Test]
		public void TestClose()
		{
			testName = "TestClose";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;	
			LogCursor logCursor;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);
			
			// Get log cursor to read/write log.
			logCursor = env.GetLogCursor();

			// Close the log cursor and env.
			logCursor.Close();
			db.Close();
			env.Close();
		}

		[Test]
		public void TesCurrentLSN()
		{
			testName = "TesCurrentLSN";
			testHome = testFixtureHome + "/" + testName;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			/*
			 * Move the cursor to the beginning of the #1 log file.
			 * Get the current LSN and confirm that is the position
			 * the cursor is moved to.
			 */
			LSN lsn = new LSN(1, 0);
			logCursor.Move(lsn);
			Assert.AreEqual(lsn.LogFileNumber,
			    logCursor.CurrentLSN.LogFileNumber);
			Assert.AreEqual(lsn.Offset, logCursor.CurrentLSN.Offset);

			// Close all.
			logCursor.Close();
			db.Close();
			env.Close();
		}

		[Test]
		public void TestCurrentRecord()
		{
			testName = "TestCurrentRecord";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			/*
			 * Move the cursor to the beginning of the #1 log file.
			 * Get the current LSN and confirm that is the position
			 * the cursor is moved to.
			 */
			LSN lsn = new LSN(1, 0);
			logCursor.Move(lsn);
			Assert.IsNotNull(logCursor.CurrentRecord.Data);

			// Close all.
			logCursor.Close();
			db.Close();
			env.Close();
		}

		[Test]
		public void TestMove()
		{
			testName = "TestMove";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			// Move the cursor to specified location in log files.
			LSN lsn = new LSN(1, 0);
			Assert.IsTrue(logCursor.Move(lsn));

			// Close all.
			logCursor.Close();
			db.Close();
			env.Close();
		}

		/*
		[Test]
		public void TestMoveFirst()
		{
			testName = "TestMoveFirst";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			// Move to the first LSN in log file.
			Assert.IsTrue(logCursor.MoveFirst());
			
			// Confirm offset of the fist LSN should be 0.
			Assert.AreEqual(0, logCursor.CurrentLSN.Offset);

			// Close all.
			logCursor.Close();
			db.Close();
			env.Close();
		}
		*/

		[Test]
		public void TestMoveLast()
		{
			testName = "TestMoveLast";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			// Move to the last LSN in log file.
			Assert.IsTrue(logCursor.MoveLast());

			// The offset of last LSN shouldn't be 0.
			Assert.AreNotEqual(0, logCursor.CurrentLSN.Offset);

			// Close all.
			logCursor.Close();
			db.Close();
			env.Close();
		}

		[Test]
		public void TestMoveNext()
		{
			testName = "TestMoveNext";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			logCursor.MoveLast();
			DatabaseEntry curRec = logCursor.CurrentRecord;
			for (int i = 0; i < 1000; i++)
				db.Append(new DatabaseEntry(
				    ASCIIEncoding.ASCII.GetBytes("new data")));

			Assert.IsTrue(logCursor.MoveNext());

			logCursor.MoveNext();

			// Close the log cursor.
			logCursor.Close();
			db.Close();
			env.Close();
		}


		[Test]
		public void TestMovePrev()
		{
			testName = "TestMovePrev";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;
			LSN lsn;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			// Get the last two LSN in log file.
			logCursor.MoveLast();
			Assert.IsTrue(logCursor.MovePrev());

			// Close all.
			logCursor.Close();
			db.Close();
			env.Close();
		}

		[Test]
		public void TestRefresh()
		{
			testName = "TestRefresh";
			testHome = testFixtureHome + "/" + testName;
			DatabaseEnvironment env;
			LSN lsn;
			RecnoDatabase db;

			Logging(testHome, testName, out env, out db);

			// Get log cursor to read/write log. 
			LogCursor logCursor = env.GetLogCursor();

			// Move the cursor to the last record. 
			logCursor.MoveLast();
			DatabaseEntry curRec = logCursor.CurrentRecord;

			// Put some new records into database.
			for (int i = 0; i < 10; i++)
				db.Append(new DatabaseEntry(
				    ASCIIEncoding.ASCII.GetBytes("new data")));

			// Get the current record that cursor points to.
			logCursor.Refresh();

			// It shouldn't be changed.
			Assert.AreEqual(curRec.Data, 
			    logCursor.CurrentRecord.Data);

			// Close all.
			logCursor.Close();
			db.Close();
			env.Close();
		}

		/*
		 * Open environment, database and write data into database. 
		 * Generated log files are put under testHome.
		 */
		public void Logging(string home, string dbName,
		    out DatabaseEnvironment env, out RecnoDatabase recnoDB)
		{
			string dbFileName = dbName + ".db";

			Configuration.ClearDir(home);

			// Open environment with logging subsystem.
			DatabaseEnvironmentConfig envConfig =
			    new DatabaseEnvironmentConfig();
			envConfig.Create = true;
			envConfig.UseLogging = true;
			envConfig.LogSystemCfg = new LogConfig();
			envConfig.LogSystemCfg.FileMode = 755;
			envConfig.LogSystemCfg.ZeroOnCreate = true;
			envConfig.UseMPool = true;
			env = DatabaseEnvironment.Open(home, envConfig);

			/*
			 * Open recno database, write 100000 records into 
			 * the database and close it.
			 */
			RecnoDatabaseConfig recnoConfig =
			    new RecnoDatabaseConfig();
			recnoConfig.Creation = CreatePolicy.IF_NEEDED;
			recnoConfig.Env = env;
			// The db needs mpool to open.
			recnoConfig.NoMMap = false;
			recnoDB = RecnoDatabase.Open(dbFileName,
			    recnoConfig);
			for (int i = 0; i < 1000; i++)
				recnoDB.Append(new DatabaseEntry(
				    ASCIIEncoding.ASCII.GetBytes("key")));
		}
	}
}