Back to index

openldap  2.4.31
mtest4.c
Go to the documentation of this file.
00001 /* mtest4.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 with fixed-size keys */
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[8];
00035        char kval[sizeof(int)];
00036 
00037        memset(sval, 0, sizeof(sval));
00038 
00039        count = 510;
00040        values = (int *)malloc(count*sizeof(int));
00041 
00042        for(i = 0;i<count;i++) {
00043               values[i] = i*5;
00044        }
00045 
00046        rc = mdb_env_create(&env);
00047        rc = mdb_env_set_mapsize(env, 10485760);
00048        rc = mdb_env_set_maxdbs(env, 4);
00049        rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
00050        rc = mdb_txn_begin(env, NULL, 0, &txn);
00051        rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT|MDB_DUPFIXED, &dbi);
00052 
00053        key.mv_size = sizeof(int);
00054        key.mv_data = kval;
00055        data.mv_size = sizeof(sval);
00056        data.mv_data = sval;
00057 
00058        printf("Adding %d values\n", count);
00059        strcpy(kval, "001");
00060        for (i=0;i<count;i++) {
00061               sprintf(sval, "%07x", values[i]);
00062               rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
00063               if (rc) j++;
00064        }
00065        if (j) printf("%d duplicates skipped\n", j);
00066        rc = mdb_txn_commit(txn);
00067        rc = mdb_env_stat(env, &mst);
00068 
00069        /* there should be one full page of dups now.
00070         */
00071        rc = mdb_txn_begin(env, NULL, 1, &txn);
00072        rc = mdb_cursor_open(txn, dbi, &cursor);
00073        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00074               printf("key: %p %.*s, data: %p %.*s\n",
00075                      key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
00076                      data.mv_data, (int) data.mv_size, (char *) data.mv_data);
00077        }
00078        mdb_cursor_close(cursor);
00079        mdb_txn_abort(txn);
00080 
00081        /* test all 3 branches of split code:
00082         * 1: new key in lower half
00083         * 2: new key at split point
00084         * 3: new key in upper half
00085         */
00086 
00087        key.mv_size = sizeof(int);
00088        key.mv_data = kval;
00089        data.mv_size = sizeof(sval);
00090        data.mv_data = sval;
00091 
00092        sprintf(sval, "%07x", values[3]+1);
00093        rc = mdb_txn_begin(env, NULL, 0, &txn);
00094        rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
00095        mdb_txn_abort(txn);
00096 
00097        sprintf(sval, "%07x", values[255]+1);
00098        rc = mdb_txn_begin(env, NULL, 0, &txn);
00099        rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
00100        mdb_txn_abort(txn);
00101 
00102        sprintf(sval, "%07x", values[500]+1);
00103        rc = mdb_txn_begin(env, NULL, 0, &txn);
00104        rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
00105        rc = mdb_txn_commit(txn);
00106 
00107        /* Try MDB_NEXT_MULTIPLE */
00108        rc = mdb_txn_begin(env, NULL, 0, &txn);
00109        rc = mdb_cursor_open(txn, dbi, &cursor);
00110        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT_MULTIPLE)) == 0) {
00111               printf("key: %.*s, data: %.*s\n",
00112                      (int) key.mv_size,  (char *) key.mv_data,
00113                      (int) data.mv_size, (char *) data.mv_data);
00114        }
00115        mdb_cursor_close(cursor);
00116        mdb_txn_abort(txn);
00117        j=0;
00118 
00119        for (i= count - 1; i > -1; i-= (random()%3)) {
00120               j++;
00121               txn=NULL;
00122               rc = mdb_txn_begin(env, NULL, 0, &txn);
00123               sprintf(sval, "%07x", values[i]);
00124               key.mv_size = sizeof(int);
00125               key.mv_data = kval;
00126               data.mv_size = sizeof(sval);
00127               data.mv_data = sval;
00128               rc = mdb_del(txn, dbi, &key, &data);
00129               if (rc) {
00130                      j--;
00131                      mdb_txn_abort(txn);
00132               } else {
00133                      rc = mdb_txn_commit(txn);
00134               }
00135        }
00136        free(values);
00137        printf("Deleted %d values\n", j);
00138 
00139        rc = mdb_env_stat(env, &mst);
00140        rc = mdb_txn_begin(env, NULL, 1, &txn);
00141        rc = mdb_cursor_open(txn, dbi, &cursor);
00142        printf("Cursor next\n");
00143        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
00144               printf("key: %.*s, data: %.*s\n",
00145                      (int) key.mv_size,  (char *) key.mv_data,
00146                      (int) data.mv_size, (char *) data.mv_data);
00147        }
00148        printf("Cursor prev\n");
00149        while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
00150               printf("key: %.*s, data: %.*s\n",
00151                      (int) key.mv_size,  (char *) key.mv_data,
00152                      (int) data.mv_size, (char *) data.mv_data);
00153        }
00154        mdb_cursor_close(cursor);
00155        mdb_close(env, dbi);
00156 
00157        mdb_txn_abort(txn);
00158        mdb_env_close(env);
00159 
00160        return 0;
00161 }