Back to index

openldap  2.4.31
mtest5.c
Go to the documentation of this file.
00001 /* mtest5.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 using cursor_put */
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        rc = mdb_cursor_open(txn, dbi, &cursor);
00055 
00056        key.mv_size = sizeof(int);
00057        key.mv_data = kval;
00058        data.mv_size = sizeof(sval);
00059        data.mv_data = sval;
00060 
00061        printf("Adding %d values\n", count);
00062        for (i=0;i<count;i++) {
00063               if (!(i & 0x0f))
00064                      sprintf(kval, "%03x", values[i]);
00065               sprintf(sval, "%03x %d foo bar", values[i], values[i]);
00066               rc = mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA);
00067               if (rc) j++;
00068        }
00069        if (j) printf("%d duplicates skipped\n", j);
00070        mdb_cursor_close(cursor);
00071        rc = mdb_txn_commit(txn);
00072        rc = mdb_env_stat(env, &mst);
00073 
00074        rc = mdb_txn_begin(env, NULL, 1, &txn);
00075        rc = mdb_cursor_open(txn, dbi, &cursor);
00076        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00077               printf("key: %p %.*s, data: %p %.*s\n",
00078                      key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00079                      data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00080        }
00081        mdb_cursor_close(cursor);
00082        mdb_txn_abort(txn);
00083 
00084        j=0;
00085 
00086        for (i= count - 1; i > -1; i-= (random()%5)) {
00087               j++;
00088               txn=NULL;
00089               rc = mdb_txn_begin(env, NULL, 0, &txn);
00090               sprintf(kval, "%03x", values[i & ~0x0f]);
00091               sprintf(sval, "%03x %d foo bar", values[i], values[i]);
00092               key.mv_size = sizeof(int);
00093               key.mv_data = kval;
00094               data.mv_size = sizeof(sval);
00095               data.mv_data = sval;
00096               rc = mdb_del(txn, dbi, &key, &data);
00097               if (rc) {
00098                      j--;
00099                      mdb_txn_abort(txn);
00100               } else {
00101                      rc = mdb_txn_commit(txn);
00102               }
00103        }
00104        free(values);
00105        printf("Deleted %d values\n", j);
00106 
00107        rc = mdb_env_stat(env, &mst);
00108        rc = mdb_txn_begin(env, NULL, 1, &txn);
00109        rc = mdb_cursor_open(txn, dbi, &cursor);
00110        printf("Cursor next\n");
00111        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00112               printf("key: %.*s, data: %.*s\n",
00113                      (int) key.mv_size,  (char *) key.mv_data,
00114                      (int) data.mv_size, (char *) data.mv_data);
00115        }
00116        printf("Cursor prev\n");
00117        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
00118               printf("key: %.*s, data: %.*s\n",
00119                      (int) key.mv_size,  (char *) key.mv_data,
00120                      (int) data.mv_size, (char *) data.mv_data);
00121        }
00122        mdb_cursor_close(cursor);
00123        mdb_close(env, dbi);
00124 
00125        mdb_txn_abort(txn);
00126        mdb_env_close(env);
00127 
00128        return 0;
00129 }