Back to index

openldap  2.4.31
mtest.c
Go to the documentation of this file.
00001 /* mtest.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 #define _XOPEN_SOURCE 500          /* srandom(), random() */
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <time.h>
00018 #include "mdb.h"
00019 
00020 int main(int argc,char * argv[])
00021 {
00022        int i = 0, j = 0, rc;
00023        MDB_env *env;
00024        MDB_dbi dbi;
00025        MDB_val key, data;
00026        MDB_txn *txn;
00027        MDB_stat mst;
00028        MDB_cursor *cursor;
00029        int count;
00030        int *values;
00031        char sval[32];
00032 
00033        srandom(time(NULL));
00034 
00035            count = (random()%384) + 64;
00036            values = (int *)malloc(count*sizeof(int));
00037 
00038            for(i = 0;i<count;i++) {
00039                      values[i] = random()%1024;
00040            }
00041     
00042               rc = mdb_env_create(&env);
00043               rc = mdb_env_set_mapsize(env, 10485760);
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, NULL, 0, &dbi);
00047    
00048               key.mv_size = sizeof(int);
00049               key.mv_data = sval;
00050               data.mv_size = sizeof(sval);
00051               data.mv_data = sval;
00052 
00053               printf("Adding %d values\n", count);
00054            for (i=0;i<count;i++) { 
00055                      sprintf(sval, "%03x %d foo bar", values[i], values[i]);
00056                      rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
00057                      if (rc) j++;
00058            }
00059               if (j) printf("%d duplicates skipped\n", j);
00060               rc = mdb_txn_commit(txn);
00061               rc = mdb_env_stat(env, &mst);
00062 
00063               rc = mdb_txn_begin(env, NULL, 1, &txn);
00064               rc = mdb_cursor_open(txn, dbi, &cursor);
00065               while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00066                      printf("key: %p %.*s, data: %p %.*s\n",
00067                             key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00068                             data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00069               }
00070               mdb_cursor_close(cursor);
00071               mdb_txn_abort(txn);
00072 
00073               j=0;
00074               key.mv_data = sval;
00075            for (i= count - 1; i > -1; i-= (random()%5)) {      
00076                      j++;
00077                      txn=NULL;
00078                      rc = mdb_txn_begin(env, NULL, 0, &txn);
00079                      sprintf(sval, "%03x ", values[i]);
00080                      rc = mdb_del(txn, dbi, &key, NULL);
00081                      if (rc) {
00082                             j--;
00083                             mdb_txn_abort(txn);
00084                      } else {
00085                             rc = mdb_txn_commit(txn);
00086                      }
00087            }
00088            free(values);
00089               printf("Deleted %d values\n", j);
00090 
00091               rc = mdb_env_stat(env, &mst);
00092               rc = mdb_txn_begin(env, NULL, 1, &txn);
00093               rc = mdb_cursor_open(txn, dbi, &cursor);
00094               printf("Cursor next\n");
00095               while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00096                      printf("key: %.*s, data: %.*s\n",
00097                             (int) key.mv_size,  (char *) key.mv_data,
00098                             (int) data.mv_size, (char *) data.mv_data);
00099               }
00100               printf("Cursor prev\n");
00101               while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
00102                      printf("key: %.*s, data: %.*s\n",
00103                             (int) key.mv_size,  (char *) key.mv_data,
00104                             (int) data.mv_size, (char *) data.mv_data);
00105               }
00106 #if 0
00107               /* write ops aren't coordinated with cursors,
00108                * this stuff all breaks
00109                */
00110               printf("Deleting with cursor\n");
00111               rc = mdb_txn_begin(env, NULL, 0, &txn);
00112               rc = mdb_cursor_open(db, txn, &cur2);
00113               for (i=0; i<50; i++) {
00114                      rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT);
00115                      printf("key: %p %.*s, data: %p %.*s\n",
00116                             key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00117                             data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00118                      rc = mdb_del(db, txn, &key, NULL);
00119               }
00120 
00121               printf("Restarting cursor in txn\n");
00122               rc = mdb_cursor_get(cur2, &key, &data, MDB_FIRST);
00123               printf("key: %p %.*s, data: %p %.*s\n",
00124                      key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00125                      data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00126               for (i=0; i<32; i++) {
00127                      rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT);
00128                      printf("key: %p %.*s, data: %p %.*s\n",
00129                             key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00130                             data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00131               }
00132               mdb_cursor_close(cur2);
00133               rc = mdb_txn_commit(txn);
00134               mdb_cursor_close(cursor);
00135               printf("Restarting cursor outside txn\n");
00136               rc = mdb_cursor_open(db, NULL, &cursor);
00137               rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
00138               printf("key: %p %.*s, data: %p %.*s\n",
00139                      key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00140                      data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00141               for (i=0; i<32; i++) {
00142                      rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT);
00143                      printf("key: %p %.*s, data: %p %.*s\n",
00144                             key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00145                             data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00146               }
00147 #endif
00148               mdb_cursor_close(cursor);
00149               mdb_close(env, dbi);
00150 
00151               mdb_txn_abort(txn);
00152               mdb_env_close(env);
00153 
00154        return 0;
00155 }