Back to index

openldap  2.4.31
mtest2.c
Go to the documentation of this file.
00001 /* mtest2.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 /* Just like mtest.c, but using a subDB instead of the main DB */
00016 
00017 #define _XOPEN_SOURCE 500          /* srandom(), random() */
00018 #include <stdio.h>
00019 #include <stdlib.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 
00036        srandom(time(NULL));
00037 
00038        count = (random()%384) + 64;
00039        values = (int *)malloc(count*sizeof(int));
00040 
00041        for(i = 0;i<count;i++) {
00042               values[i] = random()%1024;
00043        }
00044 
00045        rc = mdb_env_create(&env);
00046        rc = mdb_env_set_mapsize(env, 10485760);
00047        rc = mdb_env_set_maxdbs(env, 4);
00048        rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
00049        rc = mdb_txn_begin(env, NULL, 0, &txn);
00050        rc = mdb_open(txn, "id1", MDB_CREATE, &dbi);
00051    
00052        key.mv_size = sizeof(int);
00053        key.mv_data = sval;
00054        data.mv_size = sizeof(sval);
00055        data.mv_data = sval;
00056 
00057        printf("Adding %d values\n", count);
00058        for (i=0;i<count;i++) {     
00059               sprintf(sval, "%03x %d foo bar", values[i], values[i]);
00060               rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
00061               if (rc) j++;
00062        }
00063        if (j) printf("%d duplicates skipped\n", j);
00064        rc = mdb_txn_commit(txn);
00065        rc = mdb_env_stat(env, &mst);
00066 
00067        rc = mdb_txn_begin(env, NULL, 1, &txn);
00068        rc = mdb_cursor_open(txn, dbi, &cursor);
00069        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00070               printf("key: %p %.*s, data: %p %.*s\n",
00071                      key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00072                      data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00073        }
00074        mdb_cursor_close(cursor);
00075        mdb_txn_abort(txn);
00076 
00077        j=0;
00078        key.mv_data = sval;
00079        for (i= count - 1; i > -1; i-= (random()%5)) {   
00080               j++;
00081               txn=NULL;
00082               rc = mdb_txn_begin(env, NULL, 0, &txn);
00083               sprintf(sval, "%03x ", values[i]);
00084               rc = mdb_del(txn, dbi, &key, NULL);
00085               if (rc) {
00086                      j--;
00087                      mdb_txn_abort(txn);
00088               } else {
00089                      rc = mdb_txn_commit(txn);
00090               }
00091        }
00092        free(values);
00093        printf("Deleted %d values\n", j);
00094 
00095        rc = mdb_env_stat(env, &mst);
00096        rc = mdb_txn_begin(env, NULL, 1, &txn);
00097        rc = mdb_cursor_open(txn, dbi, &cursor);
00098        printf("Cursor next\n");
00099        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00100               printf("key: %.*s, data: %.*s\n",
00101                      (int) key.mv_size,  (char *) key.mv_data,
00102                      (int) data.mv_size, (char *) data.mv_data);
00103        }
00104        printf("Cursor prev\n");
00105        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
00106               printf("key: %.*s, data: %.*s\n",
00107                      (int) key.mv_size,  (char *) key.mv_data,
00108                      (int) data.mv_size, (char *) data.mv_data);
00109        }
00110        mdb_cursor_close(cursor);
00111        mdb_close(env, dbi);
00112 
00113        mdb_txn_abort(txn);
00114        mdb_env_close(env);
00115 
00116        return 0;
00117 }