Back to index

openldap  2.4.31
mtest6.c
Go to the documentation of this file.
00001 /* mtest6.c - memory-mapped database tester/toy */
00002 /*
00003  * Copyright 2011 Howard Chu, Symas Corp.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted only as authorized by the OpenLDAP
00008  * Public License.
00009  *
00010  * A copy of this license is available in the file LICENSE in the
00011  * top-level directory of the distribution or, alternatively, at
00012  * <http://www.OpenLDAP.org/license.html>.
00013  */
00014 
00015 /* Tests for DB splits and merges */
00016 #define _XOPEN_SOURCE 500          /* srandom(), random() */
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #include <time.h>
00021 #include "mdb.h"
00022 
00023 char dkbuf[1024];
00024 
00025 int main(int argc,char * argv[])
00026 {
00027        int i = 0, j = 0, rc;
00028        MDB_env *env;
00029        MDB_dbi dbi;
00030        MDB_val key, data;
00031        MDB_txn *txn;
00032        MDB_stat mst;
00033        MDB_cursor *cursor;
00034        int count;
00035        int *values;
00036        long kval;
00037        char *sval;
00038 
00039        srandom(time(NULL));
00040 
00041        rc = mdb_env_create(&env);
00042        rc = mdb_env_set_mapsize(env, 10485760);
00043        rc = mdb_env_set_maxdbs(env, 4);
00044        rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
00045        rc = mdb_txn_begin(env, NULL, 0, &txn);
00046        rc = mdb_open(txn, "id2", MDB_CREATE|MDB_INTEGERKEY, &dbi);
00047        rc = mdb_cursor_open(txn, dbi, &cursor);
00048        rc = mdb_stat(txn, dbi, &mst);
00049 
00050        sval = calloc(1, mst.ms_psize / 4);
00051        key.mv_size = sizeof(long);
00052        key.mv_data = &kval;
00053        data.mv_size = mst.ms_psize / 4 - 30;
00054        data.mv_data = sval;
00055 
00056        printf("Adding 12 values, should yield 3 splits\n");
00057        for (i=0;i<12;i++) {
00058               kval = i*5;
00059               sprintf(sval, "%08x", kval);
00060               rc = mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE);
00061        }
00062        printf("Adding 12 more values, should yield 3 splits\n");
00063        for (i=0;i<12;i++) {
00064               kval = i*5+4;
00065               sprintf(sval, "%08x", kval);
00066               rc = mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE);
00067        }
00068        printf("Adding 12 more values, should yield 3 splits\n");
00069        for (i=0;i<12;i++) {
00070               kval = i*5+1;
00071               sprintf(sval, "%08x", kval);
00072               rc = mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE);
00073        }
00074        rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
00075 
00076        do {
00077               printf("key: %p %s, data: %p %.*s\n",
00078                      key.mv_data,  mdb_dkey(&key, dkbuf),
00079                      data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00080        } while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0);
00081        mdb_cursor_close(cursor);
00082        mdb_txn_commit(txn);
00083 
00084 #if 0
00085        j=0;
00086 
00087        for (i= count - 1; i > -1; i-= (random()%5)) {
00088               j++;
00089               txn=NULL;
00090               rc = mdb_txn_begin(env, NULL, 0, &txn);
00091               sprintf(kval, "%03x", values[i & ~0x0f]);
00092               sprintf(sval, "%03x %d foo bar", values[i], values[i]);
00093               key.mv_size = sizeof(int);
00094               key.mv_data = kval;
00095               data.mv_size = sizeof(sval);
00096               data.mv_data = sval;
00097               rc = mdb_del(txn, dbi, &key, &data);
00098               if (rc) {
00099                      j--;
00100                      mdb_txn_abort(txn);
00101               } else {
00102                      rc = mdb_txn_commit(txn);
00103               }
00104        }
00105        free(values);
00106        printf("Deleted %d values\n", j);
00107 
00108        rc = mdb_env_stat(env, &mst);
00109        rc = mdb_txn_begin(env, NULL, 1, &txn);
00110        rc = mdb_cursor_open(txn, dbi, &cursor);
00111        printf("Cursor next\n");
00112        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00113               printf("key: %.*s, data: %.*s\n",
00114                      (int) key.mv_size,  (char *) key.mv_data,
00115                      (int) data.mv_size, (char *) data.mv_data);
00116        }
00117        printf("Cursor prev\n");
00118        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
00119               printf("key: %.*s, data: %.*s\n",
00120                      (int) key.mv_size,  (char *) key.mv_data,
00121                      (int) data.mv_size, (char *) data.mv_data);
00122        }
00123        mdb_cursor_close(cursor);
00124        mdb_close(txn, dbi);
00125 
00126        mdb_txn_abort(txn);
00127 #endif
00128        mdb_env_close(env);
00129 
00130        return 0;
00131 }