summaryrefslogtreecommitdiff
path: root/dbm/tests/lots.c
blob: 20b86c59433aca503c7fa10fde0272a65e867ed3 (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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/* use sequental numbers printed to strings
 * to store lots and lots of entries in the
 * database.
 *
 * Start with 100 entries, put them and then
 * read them out.  Then delete the first
 * half and verify that all of the first half
 * is gone and then verify that the second
 * half is still there.
 * Then add the first half back and verify
 * again.  Then delete the middle third
 * and verify again.
 * Then increase the size by 1000 and do
 * the whole add delete thing again.
 *
 * The data for each object is the number string translated
 * to hex and replicated a random number of times.  The
 * number of times that the data is replicated is the first
 * int32 in the data.
 */

#include <stdio.h>

#include <stdlib.h>
#ifdef STDC_HEADERS
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <string.h>
#include <assert.h>
#include "mcom_db.h"

DB *database=0;
int MsgPriority=5;

#if defined(_WINDOWS) && !defined(WIN32)
#define int32 long
#define uint32 unsigned long
#else
#define int32 int
#define uint32 unsigned int
#endif

typedef enum {
USE_LARGE_KEY,
USE_SMALL_KEY
} key_type_enum;

#define TraceMe(priority, msg) 		\
	do {							\
		if(priority <= MsgPriority)	\
		  {							\
			ReportStatus msg;		\
		  }							\
	} while(0)

int
ReportStatus(char *string, ...)
{
    va_list args;

#ifdef STDC_HEADERS
    va_start(args, string);
#else
    va_start(args);
#endif
    vfprintf(stderr, string, args);
    va_end(args);

	fprintf (stderr, "\n");

	return(0);
}

int
ReportError(char *string, ...)
{
    va_list args;

#ifdef STDC_HEADERS
    va_start(args, string);
#else
    va_start(args);
#endif
	fprintf (stderr, "\n	");
    vfprintf(stderr, string, args);
	fprintf (stderr, "\n");
    va_end(args);

	return(0);
}

DBT * MakeLargeKey(int32 num)
{
	int32 low_bits;
	static DBT rv;
	static char *string_rv=0;
	int rep_char;
	size_t size;

	if(string_rv)
		free(string_rv);

	/* generate a really large text key derived from
	 * an int32
	 */
	low_bits = (num % 10000) + 1;

	/* get the repeat char from the low 26 */
	rep_char = (char) ((low_bits % 26) + 'a');

	/* malloc a string low_bits wide */
	size = low_bits*sizeof(char);
	string_rv = (char *)malloc(size);

	memset(string_rv, rep_char, size);

	rv.data = string_rv;
	rv.size = size;

	return(&rv);
}

DBT * MakeSmallKey(int32 num)
{
	static DBT rv;
	static char data_string[64];

	rv.data = data_string;

	sprintf(data_string, "%ld", (long)num);
	rv.size = strlen(data_string);

	return(&rv);

}

DBT * GenKey(int32 num, key_type_enum key_type)
{
	DBT *key;

	switch(key_type)
	  {
		case USE_LARGE_KEY:
			key = MakeLargeKey(num);
			break;
		case USE_SMALL_KEY:
			key = MakeSmallKey(num);
			break;
		default:
			abort();
			break;
	  }

	return(key);
}

int
SeqDatabase()
{
	int status;
	DBT key, data;

	ReportStatus("SEQuencing through database...");

	/* seq throught the whole database */
    if(!(status = (*database->seq)(database, &key, &data, R_FIRST)))
	  {
        while(!(status = (database->seq) (database, &key, &data, R_NEXT)));
			; /* null body */
	  }

	if(status < 0)
		ReportError("Error seq'ing database");

	return(status);
}

int 
VerifyData(DBT *data, int32 num, key_type_enum key_type)
{
	int32 count, compare_num;
	size_t size;
	int32 *int32_array;

	/* The first int32 is count 
	 * The other n entries should
	 * all equal num
	 */
	if(data->size < sizeof(int32))
	  {
		ReportError("Data size corrupted");
		return -1;
	  }

	memcpy(&count, data->data, sizeof(int32));

	size = sizeof(int32)*(count+1);

	if(size != data->size)
	  {
		ReportError("Data size corrupted");
		return -1;
	  }

	int32_array = (int32*)data->data;

	for(;count > 0; count--)
	  {
		memcpy(&compare_num, &int32_array[count], sizeof(int32));

		if(compare_num != num)
	      {
		    ReportError("Data corrupted");
		    return -1;
	      }
	  }

	return(0);
}


/* verify that a range of number strings exist
 * or don't exist. And that the data is valid
 */
#define SHOULD_EXIST 1
#define SHOULD_NOT_EXIST 0
int
VerifyRange(int32 low, int32 high, int32 should_exist, key_type_enum key_type)
{
	DBT *key, data;
	int32 num;
	int status;

	TraceMe(1, ("Verifying: %ld to %ld, using %s keys", 
		    low, high, key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));

	for(num = low; num <= high; num++)
	  {

		key = GenKey(num, key_type);

		status = (*database->get)(database, key, &data, 0);

		if(status == 0)
		  {
			/* got the item */
			if(!should_exist)
			  {
				ReportError("Item exists but shouldn't: %ld", num);
			  }
			else
			  {
			    /* else verify the data */
			    VerifyData(&data, num, key_type);
			  }
		  }
		else if(status > 0)
		  {
			/* item not found */
			if(should_exist)
			  {
				ReportError("Item not found but should be: %ld", num);
			  }
		  }
		else
		  {
			/* database error */
			ReportError("Database error");
			return(-1);
		  }
			
	  }

	TraceMe(1, ("Correctly verified: %ld to %ld", low, high));

	return(0);

}

DBT *
GenData(int32 num)
{
	int32 n;
	static DBT *data=0;
	int32 *int32_array;
	size_t size;

	if(!data)
	  {
		data = (DBT*)malloc(sizeof(DBT));
		data->size = 0;
		data->data = 0;
	  }
	else if(data->data)
	  {
		free(data->data);
	  }

	n = rand();

	n = n % 512;  /* bound to a 2K size */

	
	size = sizeof(int32)*(n+1);
	int32_array = (int32 *) malloc(size);

	memcpy(&int32_array[0], &n, sizeof(int32));

	for(; n > 0; n--)
	  {
		memcpy(&int32_array[n], &num, sizeof(int32));
	  }

	data->data = (void*)int32_array;
	data->size = size;

	return(data);
}

#define ADD_RANGE 1
#define DELETE_RANGE 2

int
AddOrDelRange(int32 low, int32 high, int action, key_type_enum key_type)
{
	DBT *key, *data;
#if 0 /* only do this if your really analy checking the puts */
	DBT tmp_data;
#endif 
	int32 num;
	int status;

	if(action != ADD_RANGE && action != DELETE_RANGE)
		assert(0);

	if(action == ADD_RANGE)
	  {
		TraceMe(1, ("Adding: %ld to %ld: %s keys", low, high,
		    	key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
	  }
	else
	  {
		TraceMe(1, ("Deleting: %ld to %ld: %s keys", low, high,
		    	key_type == USE_SMALL_KEY ? "SMALL" : "LARGE"));
	  }

	for(num = low; num <= high; num++)
	  {

		key = GenKey(num, key_type);

		if(action == ADD_RANGE)
		  {
			data = GenData(num);
			status = (*database->put)(database, key, data, 0);
		  }
		else
		  {
			status = (*database->del)(database, key, 0);
		  }

		if(status < 0)
		  {
			ReportError("Database error %s item: %ld",
							action == ADD_RANGE ? "ADDING" : "DELETING", 
							num);
		  }
		else if(status > 0)
		  {
			ReportError("Could not %s item: %ld", 
							action == ADD_RANGE ? "ADD" : "DELETE", 
							num);
		  }
		else if(action == ADD_RANGE)
		  {
#define SYNC_EVERY_TIME
#ifdef SYNC_EVERY_TIME
			status = (*database->sync)(database, 0);
			if(status != 0)
				ReportError("Database error syncing after add");
#endif

#if 0 /* only do this if your really analy checking the puts */
	 
			/* make sure we can still get it
			 */
			status = (*database->get)(database, key, &tmp_data, 0);

			if(status != 0)
			  {
				ReportError("Database error checking item just added: %d",
							num);
			  }
			else
			  {
				/* now verify that none of the ones we already
				 * put in have disappeared
				 */
				VerifyRange(low, num, SHOULD_EXIST, key_type);
			  }
#endif
			
		  }
	  }


	if(action == ADD_RANGE)
	  {
		TraceMe(1, ("Successfully added: %ld to %ld", low, high));
	  }
	else
	  {
		TraceMe(1, ("Successfully deleted: %ld to %ld", low, high));
	  }

	return(0);
}

int
TestRange(int32 low, int32 range, key_type_enum key_type)
{
	int status; int32 low_of_range1, high_of_range1; int32 low_of_range2, high_of_range2;
	int32 low_of_range3, high_of_range3;

	status = AddOrDelRange(low, low+range, ADD_RANGE, key_type);
	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);

	TraceMe(1, ("Finished with sub test 1"));

	SeqDatabase();

	low_of_range1 = low;
	high_of_range1 = low+(range/2);
	low_of_range2 = high_of_range1+1;
	high_of_range2 = low+range;
	status = AddOrDelRange(low_of_range1, high_of_range1, DELETE_RANGE, key_type);
	status = VerifyRange(low_of_range1, high_of_range1, SHOULD_NOT_EXIST, key_type);
	status = VerifyRange(low_of_range2, low_of_range2, SHOULD_EXIST, key_type);

	TraceMe(1, ("Finished with sub test 2"));

	SeqDatabase();

	status = AddOrDelRange(low_of_range1, high_of_range1, ADD_RANGE, key_type);
	/* the whole thing should exist now */
	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);

	TraceMe(1, ("Finished with sub test 3"));

	SeqDatabase();

	status = AddOrDelRange(low_of_range2, high_of_range2, DELETE_RANGE, key_type);
	status = VerifyRange(low_of_range1, high_of_range1, SHOULD_EXIST, key_type);
	status = VerifyRange(low_of_range2, high_of_range2, SHOULD_NOT_EXIST, key_type);

	TraceMe(1, ("Finished with sub test 4"));

	SeqDatabase();

	status = AddOrDelRange(low_of_range2, high_of_range2, ADD_RANGE, key_type);
	/* the whole thing should exist now */
	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);

	TraceMe(1, ("Finished with sub test 5"));

	SeqDatabase();

	low_of_range1 = low;
	high_of_range1 = low+(range/3);
	low_of_range2 = high_of_range1+1;
	high_of_range2 = high_of_range1+(range/3);
	low_of_range3 = high_of_range2+1;
	high_of_range3 = low+range;
	/* delete range 2 */
	status = AddOrDelRange(low_of_range2, high_of_range2, DELETE_RANGE, key_type);
	status = VerifyRange(low_of_range1, high_of_range1, SHOULD_EXIST, key_type);
	status = VerifyRange(low_of_range2, low_of_range2, SHOULD_NOT_EXIST, key_type);
	status = VerifyRange(low_of_range3, low_of_range2, SHOULD_EXIST, key_type);

	TraceMe(1, ("Finished with sub test 6"));

	SeqDatabase();

	status = AddOrDelRange(low_of_range2, high_of_range2, ADD_RANGE, key_type);
	/* the whole thing should exist now */
	status = VerifyRange(low, low+range, SHOULD_EXIST, key_type);

	TraceMe(1, ("Finished with sub test 7"));

	return(0);
}

#define START_RANGE 109876
int
main(int argc, char **argv)
{
	int32 i, j=0;
    int quick_exit = 0;
    int large_keys = 0;
    HASHINFO hash_info = {
        16*1024,
        0,
        0,
        0,
        0,
        0};


    if(argc > 1)
      {
        while(argc > 1)
	  {
            if(!strcmp(argv[argc-1], "-quick"))
                quick_exit = 1;
            else if(!strcmp(argv[argc-1], "-large"))
			  {
                large_keys = 1;
			  }
            argc--;
          }
      }

	database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, &hash_info);

	if(!database)
	  {
		ReportError("Could not open database");
#ifdef unix
		perror("");
#endif
		exit(1);
	  }

	if(quick_exit)
	  {
		if(large_keys)
			TestRange(START_RANGE, 200, USE_LARGE_KEY);
		else
			TestRange(START_RANGE, 200, USE_SMALL_KEY);

		(*database->sync)(database, 0);
		(*database->close)(database);
        exit(0);
	  }

	for(i=100; i < 10000000; i+=200)
	  {
		if(1 || j)
		  {
			TestRange(START_RANGE, i, USE_LARGE_KEY);
			j = 0;
		  }
		else
		  {
			TestRange(START_RANGE, i, USE_SMALL_KEY);
			j = 1;
		  }

		if(1 == rand() % 3)
		  {
			(*database->sync)(database, 0);
		  }
		
		if(1 == rand() % 3)
	 	  {
			/* close and reopen */
			(*database->close)(database);
			database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, 0);
			if(!database)
	  		{
				ReportError("Could not reopen database");
#ifdef unix
				perror("");
#endif
				exit(1);
	  		}
	 	  }
		else
		  {
			/* reopen database without closeing the other */
			database = dbopen("test.db", O_RDWR | O_CREAT, 0644, DB_HASH, 0);
			if(!database)
	  		{
				ReportError("Could not reopen database "
							"after not closing the other");
#ifdef unix
				perror("");
#endif
				exit(1);
	  		}
	 	  }
	  }

	return(0);
}