BFS B+tree test: style cleanup.

This commit is contained in:
Axel Dörfler
2015-01-15 20:30:45 +01:00
parent f982c9ed88
commit 758cfd808a
@@ -1,21 +1,23 @@
/* test - BFS B+Tree torture test
**
** Initial version by Axel Dörfler, [email protected]
** This file may be used under the terms of the OpenBeOS License.
/*
* Copyright 2001-2015, Axel Dörfler, [email protected].
* This file may be used under the terms of the MIT License.
*/
#include "Volume.h"
#include "Inode.h"
#include "BPlusTree.h"
//! BFS B+Tree torture test
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <List.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "BPlusTree.h"
#include "Inode.h"
#include "Volume.h"
#define DEFAULT_ITERATIONS 10
@@ -122,11 +124,11 @@ dumpKey(void *key, int32 length)
printf("%g", *(double*)key);
break;
}
if ((gType == S_INT_INDEX || gType == S_UINT_INDEX || gType == S_FLOAT_INDEX)
&& length != 4)
if ((gType == S_INT_INDEX || gType == S_UINT_INDEX
|| gType == S_FLOAT_INDEX) && length != 4)
printf(" (wrong length %ld)", length);
else if ((gType == S_LONG_LONG_INDEX || gType == S_ULONG_LONG_INDEX || gType == S_DOUBLE_INDEX)
&& length != 8)
else if ((gType == S_LONG_LONG_INDEX || gType == S_ULONG_LONG_INDEX
|| gType == S_DOUBLE_INDEX) && length != 8)
printf(" (wrong length %ld)", length);
}
@@ -171,10 +173,7 @@ dumpKeys()
}
// #pragma mark -
//
// Functions to generate the keys in every available type
//
// #pragma mark - Key generation
void
@@ -267,7 +266,8 @@ createKeys()
if (gType == S_STR_INDEX) {
for (int32 i = 0; i < gNum; i++) {
char name[B_FILE_NAME_LENGTH];
int32 length,tries = 0;
int32 length;
int32 tries = 0;
bool last;
// create unique keys!
@@ -323,10 +323,7 @@ createKeys()
}
// #pragma mark -
//
// Tree validity checker
//
// #pragma mark - Validity checker
void
@@ -338,10 +335,12 @@ checkTreeContents(BPlusTree *tree)
TreeIterator iterator(tree);
char key[B_FILE_NAME_LENGTH];
uint16 length,duplicate;
uint16 length;
uint16 duplicate;
off_t value;
status_t status;
while ((status = iterator.GetNextEntry(key,&length,B_FILE_NAME_LENGTH,&value,&duplicate)) == B_OK) {
while ((status = iterator.GetNextEntry(key, &length, B_FILE_NAME_LENGTH,
&value, &duplicate)) == B_OK) {
if (value < 0 || value >= gNum) {
iterator.Dump();
printf("\ninvalid value %Ld in tree: ", value);
@@ -349,13 +348,16 @@ checkTreeContents(BPlusTree *tree)
}
if (gKeys[value].value != value) {
iterator.Dump();
printf("\nkey pointing to the wrong value %Ld (should be %Ld)\n",value,gKeys[value].value);
printf("\nkey pointing to the wrong value %Ld (should be %Ld)\n",
value, gKeys[value].value);
bailOutWithKey(key, length);
}
if (length != gKeys[value].length
|| memcmp(key, gKeys[value].data, length)) {
iterator.Dump();
printf("\nkeys don't match (key index = %Ld, %ld times in tree, %ld. occassion):\n\tfound: ",value,gKeys[value].in,gKeys[value].count + 1);
printf("\nkeys don't match (key index = %Ld, %ld times in tree, "
"%ld. occassion):\n\tfound: ", value, gKeys[value].in,
gKeys[value].count + 1);
dumpKey(key, length);
printf("\n\texpected: ");
dumpKey(gKeys[value].data, gKeys[value].length);
@@ -382,12 +384,12 @@ checkTreeContents(BPlusTree *tree)
}
/*! Simple test, just seeks down to every key - if it's in and couldn't
be found or it's not in and can be found, something must be wrong
*/
void
checkTreeIntegrity(BPlusTree* tree)
{
// simple test, just seeks down to every key - if it's in and couldn't
// be found or it's not in and can be found, something must be wrong
TreeIterator iterator(tree);
for (int32 i = 0; i < gNum; i++) {
status_t status = iterator.Find((uint8*)gKeys[i].data, gKeys[i].length);
@@ -422,10 +424,7 @@ checkTree(BPlusTree *tree)
}
// #pragma mark -
//
// The tree "torture" functions
//
// #pragma mark - "Torture" functions
void
@@ -433,14 +432,14 @@ addAllKeys(Transaction &transaction, BPlusTree *tree)
{
printf("*** Adding all keys to the tree...\n");
for (int32 i = 0; i < gNum; i++) {
status_t status = tree->Insert(transaction,(uint8 *)gKeys[i].data,gKeys[i].length,gKeys[i].value);
if (status < B_OK) {
status_t status = tree->Insert(transaction, (uint8*)gKeys[i].data,
gKeys[i].length, gKeys[i].value);
if (status != B_OK) {
printf("BPlusTree::Insert() returned: %s\n", strerror(status));
printf("key: ");
dumpKey(gKeys[i].data, gKeys[i].length);
putchar('\n');
}
else {
} else {
gKeys[i].in++;
gTreeCount++;
}
@@ -457,13 +456,12 @@ removeAllKeys(Transaction &transaction, BPlusTree *tree)
while (gKeys[i].in > 0) {
status_t status = tree->Remove(transaction, (uint8*)gKeys[i].data,
gKeys[i].length, gKeys[i].value);
if (status < B_OK) {
if (status != B_OK) {
printf("BPlusTree::Remove() returned: %s\n", strerror(status));
printf("key: ");
dumpKey(gKeys[i].data, gKeys[i].length);
putchar('\n');
}
else {
} else {
gKeys[i].in--;
gTreeCount--;
}
@@ -490,12 +488,15 @@ duplicateTest(Transaction &transaction,BPlusTree *tree)
int32 insertTotal = 0;
for (int32 i = 0; i < 8; i++) {
int32 insertCount = int32(1000.0 * rand() / RAND_MAX);
if (gVerbose)
printf("* insert %ld to %ld old entries...\n",insertCount,insertTotal + gKeys[index].in);
if (gVerbose) {
printf("* insert %ld to %ld old entries...\n", insertCount,
insertTotal + gKeys[index].in);
}
for (int32 j = 0; j < insertCount; j++) {
status = tree->Insert(transaction,(uint8 *)gKeys[index].data,gKeys[index].length,gKeys[index].value);
if (status < B_OK) {
status = tree->Insert(transaction, (uint8*)gKeys[index].data,
gKeys[index].length, gKeys[index].value);
if (status != B_OK) {
printf("BPlusTree::Insert() returned: %s\n", strerror(status));
bailOutWithKey(gKeys[index].data, gKeys[index].length);
}
@@ -515,12 +516,16 @@ duplicateTest(Transaction &transaction,BPlusTree *tree)
} else
count = insertTotal;
if (gVerbose)
printf("* remove %ld from %ld entries...\n",count,insertTotal + gKeys[index].in);
if (gVerbose) {
printf("* remove %ld from %ld entries...\n", count,
insertTotal + gKeys[index].in);
}
for (int32 j = 0; j < count; j++) {
status_t status = tree->Remove(transaction,(uint8 *)gKeys[index].data,gKeys[index].length,gKeys[index].value);
if (status < B_OK) {
status_t status = tree->Remove(transaction,
(uint8*)gKeys[index].data, gKeys[index].length,
gKeys[index].value);
if (status != B_OK) {
printf("BPlusTree::Remove() returned: %s\n", strerror(status));
bailOutWithKey(gKeys[index].data, gKeys[index].length);
}
@@ -541,18 +546,22 @@ duplicateTest(Transaction &transaction,BPlusTree *tree)
void
addRandomSet(Transaction& transaction, BPlusTree* tree, int32 num)
{
printf("*** Add random set to tree (%ld to %ld old entries)...\n",num,gTreeCount);
printf("*** Add random set to tree (%ld to %ld old entries)...\n",
num, gTreeCount);
for (int32 i = 0; i < num; i++) {
int32 index = int32(1.0 * gNum * rand() / RAND_MAX);
if (index == gNum)
index = gNum - 1;
if (gVerbose)
printf("adding key %ld (%ld times in the tree)\n",index,gKeys[index].in);
if (gVerbose) {
printf("adding key %ld (%ld times in the tree)\n", index,
gKeys[index].in);
}
status_t status = tree->Insert(transaction,(uint8 *)gKeys[index].data,gKeys[index].length,gKeys[index].value);
if (status < B_OK) {
status_t status = tree->Insert(transaction, (uint8*)gKeys[index].data,
gKeys[index].length, gKeys[index].value);
if (status != B_OK) {
printf("BPlusTree::Insert() returned: %s\n", strerror(status));
bailOutWithKey(gKeys[index].data, gKeys[index].length);
}
@@ -570,7 +579,8 @@ addRandomSet(Transaction &transaction,BPlusTree *tree,int32 num)
void
removeRandomSet(Transaction& transaction, BPlusTree* tree, int32 num)
{
printf("*** Remove random set from tree (%ld from %ld entries)...\n",num,gTreeCount);
printf("*** Remove random set from tree (%ld from %ld entries)...\n",
num, gTreeCount);
int32 tries = 500;
@@ -589,11 +599,14 @@ removeRandomSet(Transaction &transaction,BPlusTree *tree,int32 num)
continue;
}
if (gVerbose)
printf("removing key %ld (%ld times in the tree)\n",index,gKeys[index].in);
if (gVerbose) {
printf("removing key %ld (%ld times in the tree)\n", index,
gKeys[index].in);
}
status_t status = tree->Remove(transaction,(uint8 *)gKeys[index].data,gKeys[index].length,gKeys[index].value);
if (status < B_OK) {
status_t status = tree->Remove(transaction, (uint8*)gKeys[index].data,
gKeys[index].length, gKeys[index].value);
if (status != B_OK) {
printf("BPlusTree::Remove() returned: %s\n", strerror(status));
bailOutWithKey(gKeys[index].data, gKeys[index].length);
}
@@ -616,7 +629,8 @@ usage(char *program)
{
if (strrchr(program, '/'))
program = strrchr(program, '/') + 1;
fprintf(stderr,"usage: %s [-veh] [-t type] [-n keys] [-i iterations] [-h times] [-r seed]\n"
fprintf(stderr, "usage: %s [-veh] [-t type] [-n keys] [-i iterations] "
"[-h times] [-r seed]\n"
"BFS B+Tree torture test\n"
"\t-t\ttype is one of string, int32, uint32, int64, uint64, float,\n"
"\t\tor double; defaults to string.\n"
@@ -625,7 +639,8 @@ usage(char *program)
"\t-i\titerations is the number of the test cycles, defaults to %d.\n"
"\t-r\tthe seed for the random function, defaults to %ld.\n"
"\t-h\tremoves the keys and start over again for x times.\n"
"\t-e\texcessive validity tests: tree contents will be tested after every operation\n"
"\t-e\texcessive validity tests: tree contents will be tested after "
"every operation\n"
"\t-v\tfor verbose output.\n",
program, DEFAULT_NUM_KEYS, DEFAULT_ITERATIONS, gSeed);
exit(0);
@@ -637,18 +652,14 @@ main(int argc,char **argv)
{
char* program = argv[0];
while (*++argv)
{
while (*++argv) {
char* arg = *argv;
if (*arg == '-')
{
if (*arg == '-') {
if (arg[1] == '-')
usage(program);
while (*++arg && isalpha(*arg))
{
switch (*arg)
{
while (*++arg && isalpha(*arg)) {
switch (*arg) {
case 'v':
gVerbose = true;
break;
@@ -712,8 +723,7 @@ main(int argc,char **argv)
break;
}
}
}
else
} else
break;
}
@@ -729,18 +739,18 @@ main(int argc,char **argv)
init_cache(gVolume->Device(), gVolume->BlockSize());
//
// Create the tree, the keys, and add all keys to the tree initially
//
BPlusTree tree(transaction, &inode);
status_t status;
if ((status = tree.InitCheck()) < B_OK) {
status_t status = tree.InitCheck();
if (status != B_OK) {
fprintf(stderr, "creating tree failed: %s\n", strerror(status));
bailOut();
}
printf("*** Creating %ld keys...\n", gNum);
if ((status = createKeys()) < B_OK) {
status = createKeys();
if (status != B_OK) {
fprintf(stderr, "creating keys failed: %s\n", strerror(status));
bailOut();
}
@@ -751,15 +761,16 @@ main(int argc,char **argv)
for (int32 j = 0; j < gHard; j++) {
addAllKeys(transaction, &tree);
//
// Run the tests (they will exit the app, if an error occurs)
//
for (int32 i = 0; i < gIterations; i++) {
printf("---------- Test iteration %ld ---------------------------------\n",i+1);
printf("---------- Test iteration %ld --------------------------\n",
i + 1);
addRandomSet(transaction,&tree,int32(1.0 * gNum * rand() / RAND_MAX));
removeRandomSet(transaction,&tree,int32(1.0 * gNum * rand() / RAND_MAX));
addRandomSet(transaction, &tree,
int32(1.0 * gNum * rand() / RAND_MAX));
removeRandomSet(transaction, &tree,
int32(1.0 * gNum * rand() / RAND_MAX));
duplicateTest(transaction, &tree);
}