Back to index

openldap  2.4.31
mtest3.c
Go to the documentation of this file.
00001 /* mtest3.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 sorted duplicate DBs */
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 int main(int argc,char * argv[])
00024 {
00025        int i = 0, j = 0, rc;
00026        MDB_env *env;
00027        MDB_dbi dbi;
00028        MDB_val key, data;
00029        MDB_txn *txn;
00030        MDB_stat mst;
00031        MDB_cursor *cursor;
00032        int count;
00033        int *values;
00034        char sval[32];
00035        char kval[sizeof(int)];
00036 
00037        srandom(time(NULL));
00038 
00039        memset(sval, 0, sizeof(sval));
00040 
00041        count = (random()%384) + 64;
00042        values = (int *)malloc(count*sizeof(int));
00043 
00044        for(i = 0;i<count;i++) {
00045               values[i] = random()%1024;
00046        }
00047 
00048        rc = mdb_env_create(&env);
00049        rc = mdb_env_set_mapsize(env, 10485760);
00050        rc = mdb_env_set_maxdbs(env, 4);
00051        rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
00052        rc = mdb_txn_begin(env, NULL, 0, &txn);
00053        rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi);
00054 
00055        key.mv_size = sizeof(int);
00056        key.mv_data = kval;
00057        data.mv_size = sizeof(sval);
00058        data.mv_data = sval;
00059 
00060        printf("Adding %d values\n", count);
00061        for (i=0;i<count;i++) {
00062               if (!(i & 0x0f))
00063                      sprintf(kval, "%03x", values[i]);
00064               sprintf(sval, "%03x %d foo bar", values[i], values[i]);
00065               rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
00066               if (rc) j++;
00067        }
00068        if (j) printf("%d duplicates skipped\n", j);
00069        rc = mdb_txn_commit(txn);
00070        rc = mdb_env_stat(env, &mst);
00071 
00072        rc = mdb_txn_begin(env, NULL, 1, &txn);
00073        rc = mdb_cursor_open(txn, dbi, &cursor);
00074        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00075               printf("key: %p %.*s, data: %p %.*s\n",
00076                      key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00077                      data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00078        }
00079        mdb_cursor_close(cursor);
00080        mdb_txn_abort(txn);
00081 
00082        j=0;
00083 
00084        for (i= count - 1; i > -1; i-= (random()%5)) {
00085               j++;
00086               txn=NULL;
00087               rc = mdb_txn_begin(env, NULL, 0, &txn);
00088               sprintf(kval, "%03x", values[i & ~0x0f]);
00089               sprintf(sval, "%03x %d foo bar", values[i], values[i]);
00090               key.mv_size = sizeof(int);
00091               key.mv_data = kval;
00092               data.mv_size = sizeof(sval);
00093               data.mv_data = sval;
00094               rc = mdb_del(txn, dbi, &key, &data);
00095               if (rc) {
00096                      j--;
00097                      mdb_txn_abort(txn);
00098               } else {
00099                      rc = mdb_txn_commit(txn);
00100               }
00101        }
00102        free(values);
00103        printf("Deleted %d values\n", j);
00104 
00105        rc = mdb_env_stat(env, &mst);
00106        rc = mdb_txn_begin(env, NULL, 1, &txn);
00107        rc = mdb_cursor_open(txn, dbi, &cursor);
00108        printf("Cursor next\n");
00109        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00110               printf("key: %.*s, data: %.*s\n",
00111                      (int) key.mv_size,  (char *) key.mv_data,
00112                      (int) data.mv_size, (char *) data.mv_data);
00113        }
00114        printf("Cursor prev\n");
00115        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
00116               printf("key: %.*s, data: %.*s\n",
00117                      (int) key.mv_size,  (char *) key.mv_data,
00118                      (int) data.mv_size, (char *) data.mv_data);
00119        }
00120        mdb_cursor_close(cursor);
00121        mdb_close(env, dbi);
00122 
00123        mdb_txn_abort(txn);
00124        mdb_env_close(env);
00125 
00126        return 0;
00127 }