Back to home page

Enduro/X

 
 

    


0001 /* mtest2.c - memory-mapped database tester/toy */
0002 /*
0003  * Copyright 2011-2017 Howard Chu, Symas Corp.
0004  * All rights reserved.
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted only as authorized by the OpenLDAP
0008  * Public License.
0009  *
0010  * A copy of this license is available in the file LICENSE in the
0011  * top-level directory of the distribution or, alternatively, at
0012  * <http://www.OpenLDAP.org/license.html>.
0013  */
0014 
0015 /* Just like mtest.c, but using a subDB instead of the main DB */
0016 
0017 #include <stdio.h>
0018 #include <stdlib.h>
0019 #include <time.h>
0020 #include <cgreen/cgreen.h>
0021 #include <ndebug.h>
0022 #include <edbutil.h>
0023 #include "exdb.h"
0024 
0025 #define E(expr) CHECK((rc = (expr)) == EDB_SUCCESS, #expr)
0026 #define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0))
0027 #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \
0028     "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, edb_strerror(rc)), abort()))
0029 
0030 Ensure(test_nstd_mtest2)
0031 {
0032     int i = 0, j = 0, rc;
0033     EDB_env *env;
0034     EDB_dbi dbi;
0035     EDB_val key, data;
0036     EDB_txn *txn;
0037     EDB_stat mst;
0038     EDB_cursor *cursor;
0039     int count;
0040     int *values;
0041     char sval[32] = "";
0042         char errdet[PATH_MAX];
0043         
0044     srand(time(NULL));
0045 
0046     count = (rand()%384) + 64;
0047     values = (int *)malloc(count*sizeof(int));
0048 
0049     for(i = 0;i<count;i++) {
0050         values[i] = rand()%1024;
0051     }
0052 
0053         E(ndrx_mdb_unlink("./testdb", errdet, sizeof(errdet), 
0054             LOG_FACILITY_UBF));
0055         
0056     E(edb_env_create(&env));
0057     E(edb_env_set_maxreaders(env, 1));
0058     E(edb_env_set_mapsize(env, 10485760));
0059     E(edb_env_set_maxdbs(env, 4));
0060     E(edb_env_open(env, "./testdb", EDB_FIXEDMAP|EDB_NOSYNC, 0664));
0061 
0062     E(edb_txn_begin(env, NULL, 0, &txn));
0063     E(edb_dbi_open(txn, "id1", EDB_CREATE, &dbi));
0064    
0065     key.mv_size = sizeof(int);
0066     key.mv_data = sval;
0067 
0068     fprintf(stderr, "Adding %d values\n", count);
0069     for (i=0;i<count;i++) { 
0070         sprintf(sval, "%03x %d foo bar", values[i], values[i]);
0071         data.mv_size = sizeof(sval);
0072         data.mv_data = sval;
0073         if (RES(EDB_KEYEXIST, edb_put(txn, dbi, &key, &data, EDB_NOOVERWRITE)))
0074             j++;
0075     }
0076     if (j) fprintf(stderr, "%d duplicates skipped\n", j);
0077     E(edb_txn_commit(txn));
0078     E(edb_env_stat(env, &mst));
0079 
0080     E(edb_txn_begin(env, NULL, EDB_RDONLY, &txn));
0081     E(edb_cursor_open(txn, dbi, &cursor));
0082     while ((rc = edb_cursor_get(cursor, &key, &data, EDB_NEXT)) == 0) {
0083         fprintf(stderr, "key: %p %.*s, data: %p %.*s\n",
0084             key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
0085             data.mv_data, (int) data.mv_size, (char *) data.mv_data);
0086     }
0087     CHECK(rc == EDB_NOTFOUND, "edb_cursor_get");
0088     edb_cursor_close(cursor);
0089     edb_txn_abort(txn);
0090 
0091     j=0;
0092     key.mv_data = sval;
0093     for (i= count - 1; i > -1; i-= (rand()%5)) {
0094         j++;
0095         txn=NULL;
0096         E(edb_txn_begin(env, NULL, 0, &txn));
0097         sprintf(sval, "%03x ", values[i]);
0098         if (RES(EDB_NOTFOUND, edb_del(txn, dbi, &key, NULL))) {
0099             j--;
0100             edb_txn_abort(txn);
0101         } else {
0102             E(edb_txn_commit(txn));
0103         }
0104     }
0105     free(values);
0106     fprintf(stderr, "Deleted %d values\n", j);
0107 
0108     E(edb_env_stat(env, &mst));
0109     E(edb_txn_begin(env, NULL, EDB_RDONLY, &txn));
0110     E(edb_cursor_open(txn, dbi, &cursor));
0111     fprintf(stderr, "Cursor next\n");
0112     while ((rc = edb_cursor_get(cursor, &key, &data, EDB_NEXT)) == 0) {
0113         fprintf(stderr, "key: %.*s, data: %.*s\n",
0114             (int) key.mv_size,  (char *) key.mv_data,
0115             (int) data.mv_size, (char *) data.mv_data);
0116     }
0117     CHECK(rc == EDB_NOTFOUND, "edb_cursor_get");
0118     fprintf(stderr, "Cursor prev\n");
0119     while ((rc = edb_cursor_get(cursor, &key, &data, EDB_PREV)) == 0) {
0120         fprintf(stderr, "key: %.*s, data: %.*s\n",
0121             (int) key.mv_size,  (char *) key.mv_data,
0122             (int) data.mv_size, (char *) data.mv_data);
0123     }
0124     CHECK(rc == EDB_NOTFOUND, "edb_cursor_get");
0125     edb_cursor_close(cursor);
0126     edb_txn_abort(txn);
0127 
0128     edb_dbi_close(env, dbi);
0129     edb_env_close(env);
0130     return;
0131 }
0132 
0133 /**
0134  * LMDB/EDB tests
0135  * @return
0136  */
0137 TestSuite *ubf_nstd_mtest2(void)
0138 {
0139     TestSuite *suite = create_test_suite();
0140 
0141     add_test(suite, test_nstd_mtest2);
0142             
0143     return suite;
0144 }