Back to home page

Enduro/X

 
 

    


0001 /* mtest6.c - memory-mapped database tester/toy */
0002 /*
0003  * Copyright 2011-2020 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 /* Tests for DB splits and merges */
0016 #include <stdio.h>
0017 #include <stdlib.h>
0018 #include <string.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 char dkbuf[1024];
0031 
0032 Ensure(test_nstd_mtest6)
0033 {
0034     int i = 0, j = 0, rc;
0035     EDB_env *env;
0036     EDB_dbi dbi;
0037     EDB_val key, data, sdata;
0038     EDB_txn *txn;
0039     EDB_stat mst;
0040     EDB_cursor *cursor;
0041     int count;
0042     int *values;
0043     long kval;
0044     char *sval;
0045         char errdet[PATH_MAX];
0046 
0047     srand(time(NULL));
0048         
0049 
0050         E(ndrx_mdb_unlink("./testdb", errdet, sizeof(errdet), 
0051             LOG_FACILITY_UBF));
0052 
0053     E(edb_env_create(&env));
0054     E(edb_env_set_mapsize(env, 10485760));
0055     E(edb_env_set_maxdbs(env, 4));
0056     E(edb_env_open(env, "./testdb", EDB_FIXEDMAP|EDB_NOSYNC, 0664));
0057 
0058     E(edb_txn_begin(env, NULL, 0, &txn));
0059     E(edb_dbi_open(txn, "id6", EDB_CREATE|EDB_INTEGERKEY, &dbi));
0060     E(edb_cursor_open(txn, dbi, &cursor));
0061     E(edb_stat(txn, dbi, &mst));
0062 
0063     sval = calloc(1, mst.ms_psize / 4);
0064     key.mv_size = sizeof(long);
0065     key.mv_data = &kval;
0066     sdata.mv_size = mst.ms_psize / 4 - 30;
0067     sdata.mv_data = sval;
0068 
0069     fprintf(stderr, "Adding 12 values, should yield 3 splits\n");
0070     for (i=0;i<12;i++) {
0071         kval = i*5;
0072         sprintf(sval, "%08lx", kval);
0073         data = sdata;
0074         (void)RES(EDB_KEYEXIST, edb_cursor_put(cursor, &key, &data, EDB_NOOVERWRITE));
0075     }
0076     fprintf(stderr, "Adding 12 more values, should yield 3 splits\n");
0077     for (i=0;i<12;i++) {
0078         kval = i*5+4;
0079         sprintf(sval, "%08lx", kval);
0080         data = sdata;
0081         (void)RES(EDB_KEYEXIST, edb_cursor_put(cursor, &key, &data, EDB_NOOVERWRITE));
0082     }
0083     fprintf(stderr, "Adding 12 more values, should yield 3 splits\n");
0084     for (i=0;i<12;i++) {
0085         kval = i*5+1;
0086         sprintf(sval, "%08lx", kval);
0087         data = sdata;
0088         (void)RES(EDB_KEYEXIST, edb_cursor_put(cursor, &key, &data, EDB_NOOVERWRITE));
0089     }
0090     E(edb_cursor_get(cursor, &key, &data, EDB_FIRST));
0091 
0092     do {
0093         printf("key: %.*s, data: %.*s\n",
0094             (int) key.mv_size,  (char *) key.mv_data,
0095             (int) data.mv_size, (char *) data.mv_data);
0096     } while ((rc = edb_cursor_get(cursor, &key, &data, EDB_NEXT)) == 0);
0097     CHECK(rc == EDB_NOTFOUND, "edb_cursor_get");
0098     edb_cursor_close(cursor);
0099     edb_txn_commit(txn);
0100 
0101     edb_env_close(env);
0102 
0103     return;
0104 }
0105 
0106 
0107 /**
0108  * LMDB/EDB tests
0109  * @return
0110  */
0111 TestSuite *ubf_nstd_mtest6(void)
0112 {
0113     TestSuite *suite = create_test_suite();
0114 
0115     add_test(suite, test_nstd_mtest6);
0116             
0117     return suite;
0118 }
0119