/* Open (or reopen) the underlying sophia database iff no cursors
 * are still in use.
 */
static PyObject *
sophia_db_open(SophiaDB *db, PyObject *args)
{
    char *path;
    
    if (!PyArg_ParseTuple(args, "s:open", &path))
        return NULL;
    
    if (db->db) {
        int status = sophia_db_close_internal(db);
        if (status == 0) {
            db->close_me = 1;
            Py_RETURN_FALSE;
        }
        else if (status == -1)
            return NULL;
    }
    
    if (sp_ctl(db->env, SPDIR, SPO_CREAT | SPO_RDWR, path) == -1 ||
        (!db->cmp_fun && sp_ctl(db->env, SPCMP, sophia_compare_default, NULL) == -1))
        return PyErr_NoMemory();

    db->db = sp_open(db->env);
    if (!db->db) {
        PyErr_SetString(SophiaError, sp_error(db->env));
        return NULL;
    }
    
    db->cursors = 0;
    db->close_me = 0;
    
    Py_RETURN_TRUE;
}
Exemple #2
0
static void
meta_db(void)
{
	void *env = sp_env();
	t( env != NULL );
	void *c = sp_ctl(env);
	t( c != NULL );
	t( sp_set(c, "sophia.path", cx->suite->sophiadir) == 0 );
	t( sp_set(c, "scheduler.threads", "0") == 0 );
	t( sp_set(c, "db", "test") == 0 );
	t( sp_set(c, "db.test.sync", "0") == 0 );
	t( sp_open(env) == 0 );

	void *db = sp_get(c, "db.test");
	t( db != NULL );

	void *dbctl = sp_ctl(db);
	t( dbctl != NULL );

	void *o = sp_get(dbctl, "name");
	t( o != NULL );
	t( strcmp(sp_get(o, "value", NULL), "test") == 0 );
	sp_destroy(o);

	o = sp_get(dbctl, "id");
	t( o != NULL );
	t( strcmp(sp_get(o, "value", NULL), "1") == 0 );
	sp_destroy(o);

	t( sp_destroy(env) == 0 );
}
Exemple #3
0
todo_db_t *
todo_db_new (char *path) {
  int rc;
  void *env;
  todo_db_t *db = malloc(sizeof(todo_db_t));

  // bail if failed to allocate memoery
  if (NULL == db) return NULL;

  env = sp_env();

  // error in memoery allocation
  if (NULL == env) return NULL;

  // create if not exists in read-write mode
  rc = sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, path);

  // turn on garbage collection
  sp_ctl(env, SPGC, 1);

  if (-1 == rc) {
    todo_ferror("%s\n", sp_error(env));
    sp_destroy(env);
    return NULL;
  }

  db->env = env;
  db->path = path;

  return db;
}
Exemple #4
0
static void
single_process_2(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 );
	t( sp_ctl(env, SPCMP, cmp, NULL) == 0 );
	void *db = sp_open(env);
	t( db != NULL );
	void *env2 = sp_env();
	t( env != NULL );
	t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep2) == 0 );
	t( sp_ctl(env2, SPCMP, cmp, NULL) == 0 );
	void *db2 = sp_open(env2);
	t( db2 != NULL );
	uint32_t k = 1;
	t( sp_set(db2, &k, sizeof(k), &k, sizeof(k)) == 0 );
	size_t vsize = 0;
	void *vp = NULL;
	t( sp_get(db2, &k, sizeof(k), &vp, &vsize) == 1 );
	t( vsize == sizeof(k) );
	t( *(uint32_t*)vp == k );
	free(vp);
	k = 3;
	t( sp_set(db, &k, sizeof(k), &k, sizeof(k)) == 0 );
	t( sp_get(db, &k, sizeof(k), &vp, &vsize) == 1 );
	t( vsize == sizeof(k) );
	t( *(uint32_t*)vp == k );
	free(vp);
	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	t( sp_destroy(db2) == 0 );
	t( sp_destroy(env2) == 0 );
	t( rmrf(dbrep1) == 0 );
	t( rmrf(dbrep2) == 0 );
}
Exemple #5
0
static void
multi_process(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 );
	void *db = sp_open(env);
	t( db != NULL );
	int pid = fork();
	t( pid != -1 );
	if (pid == 0) {
		/* new process */
		void *env2 = sp_env();
		t( env2 != NULL );
		t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 );
		void *db2 = sp_open(env2);
		t( db2 == NULL );
		t( sp_destroy(env2) == 0 );
		/* valgrind: parent db and env are unfreed here, and that
		 *           is correct otherwise destroy would
		 *           corrupt the database.
		*/
		exit(0);
	} else {
		int status = 0;
		t( waitpid(pid, &status, 0) == pid );
		t( status == 0 );
	}
	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	rmrf(dbrep1);
}
/* Attach a python comparison function to the database instance.
 * Passing `None` resets the comparison function to the original
 * default one.
 */
static PyObject *
sophia_db_set_cmp_fun(SophiaDB *db, PyObject *fun)
{
    int rv;
    
    if (fun == Py_None) {
        if (!db->cmp_fun)
            return 0;
        Py_DECREF(db->cmp_fun);
        db->cmp_fun = NULL;
        rv = sp_ctl(db->env, SPCMP, sophia_compare_default, NULL);
    }
    else if (PyCallable_Check(fun)) {
        if (db->cmp_fun)
            Py_DECREF(db->cmp_fun);
        Py_INCREF(fun);
        db->cmp_fun = fun;
        rv = sp_ctl(db->env, SPCMP, sophia_compare_custom, fun);
    }
    else {
        PyErr_SetString(PyExc_TypeError, "expected either a callable or None");
        return NULL;
    }
    
    if (rv == -1) {
        PyErr_SetString(SophiaError, sp_error(db->env));
        return NULL;
    }
    Py_RETURN_NONE;
}
static PyObject *
sophia_db_set_option(SophiaDB *db, PyObject *args)
{
    int rv, option;
    PyObject *pvalue, *pvalue2 = NULL;
    
    if (!PyArg_ParseTuple(args, "iO|O:setopt", &option, &pvalue, &pvalue2))
        return NULL;
    
    if (option == SPCMP) {
        return sophia_db_set_cmp_fun(db, pvalue);
    }
    else if (option == SPPAGE || option == SPMERGEWM) {
    
        uint32_t value;
        if (pylong_to_uint32_t(pvalue, &value) == -1)
            return NULL;
        rv = sp_ctl(db->env, option, value);
    }
    else if (option == SPGC || option == SPMERGE) {
    
        int value = PyObject_IsTrue(pvalue);
        if (value == -1)
            return NULL;
        rv = sp_ctl(db->env, option, value);
    }
    else if (option == SPGCF) {
    
        double value;
        if (pyfloat_to_double(pvalue, &value) == -1)
            return NULL;
        rv = sp_ctl(db->env, option, value);
    }
    else if (option == SPGROW) {

        if (pvalue2 == NULL) {
            PyErr_SetString(PyExc_ValueError, "expected a third argument");
            return NULL;
        }
        
        uint32_t new_size;
        double resize;
        
        if (pylong_to_uint32_t(pvalue, &new_size) == -1 ||
            pyfloat_to_double(pvalue2, &resize) == -1)
            return NULL;
        rv = sp_ctl(db->env, option, new_size, resize);
    }
    else {
        PyErr_SetString(PyExc_ValueError, "unknown option");
        return NULL;
    }
    
    if (rv == -1) {
        PyErr_SetString(SophiaError, sp_error(db->env));
        return NULL;
    }
    Py_RETURN_NONE;
}
Exemple #8
0
static inline void
st_phase_commit(stc *cx)
{
    switch (cx->phase_scene) {
    case 0:
        break;
    case 1:
        t( sp_set(sp_ctl(cx->env), "db.test.branch") == 0 );
        break;
    case 2:
        t( sp_set(sp_ctl(cx->env), "db.test.branch") == 0 );
        t( sp_set(sp_ctl(cx->env), "db.test.compact") == 0 );
        break;
    case 3:
        t( sp_set(sp_ctl(cx->env), "log.rotate") == 0 );
        t( sp_set(sp_ctl(cx->env), "log.gc") == 0 );
        break;
    case 4:
        if (cx->phase == 0) {
            t( sp_set(sp_ctl(cx->env), "db.test.branch") == 0 );
            t( sp_set(sp_ctl(cx->env), "log.rotate") == 0 );
            t( sp_set(sp_ctl(cx->env), "log.gc") == 0 );
            cx->phase = 1;
        } else if (cx->phase == 1) {
            t( sp_set(sp_ctl(cx->env), "db.test.compact") == 0 );
            cx->phase = 0;
        }
        break;
    default:
        t(0);
    }
}
Exemple #9
0
static void
limit_value(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 );
	t( sp_ctl(env, SPCMP, cmp, NULL) == 0 );
	void *db = sp_open(env);
	t( db != NULL );
	char buf[1];
	t( sp_set(db, buf, sizeof(buf), buf, UINT32_MAX + 1ULL) == -1 );
	t( sp_error(db) != NULL );
	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	rmrf(dbrep);
}
Exemple #10
0
static void
mt_quota_checkpoint(stc *cx)
{
	cx->env = sp_env();
	t( cx->env != NULL );
	void *c = sp_ctl(cx->env);
	t( c != NULL );
	t( sp_set(c, "sophia.path", cx->suite->sophiadir) == 0 );
	t( sp_set(c, "memory.limit", "524288") == 0 ); /* 512k */
	/* use default settings */
	t( sp_set(c, "scheduler.threads", "5") == 0 );
	t( sp_set(c, "log.path", cx->suite->logdir) == 0 );
	t( sp_set(c, "log.sync", "0") == 0 );
	t( sp_set(c, "log.rotate_sync", "0") == 0 );
	t( sp_set(c, "db", "test") == 0 );
	t( sp_set(c, "db.test.path", cx->suite->dir) == 0 );
	t( sp_set(c, "db.test.sync", "0") == 0 );
	t( sp_set(c, "db.test.index.cmp", "u32", NULL) == 0 );
	cx->db = sp_get(c, "db.test");
	t( cx->db != NULL );
	t( sp_open(cx->env) == 0 );
	char value[1000];
	memset(value, 0, sizeof(value));
	int i = 0;
	while (i < 20000) { /* ~ 20Mb */
		void *o = sp_object(cx->db);
		assert(o != NULL);
		sp_set(o, "key", &i, sizeof(i));
		sp_set(o, "value", value, sizeof(value));
		int rc = sp_set(cx->db, o);
		t( rc == 0 );
		i++;
	}
	t( sp_destroy(cx->env) == 0 );
}
Exemple #11
0
void
st_scene_create(stscene *g, stc *cx)
{
    printf(".create");
    fflush(NULL);
    t( cx->db  == NULL );
    t( cx->env == NULL );
    cx->env = sp_env();
    t( cx->env != NULL );
    void *c = sp_ctl(cx->env);
    t( c != NULL );
    t( sp_set(c, "sham.path", cx->suite->shamdir) == 0 );
    t( sp_set(c, "scheduler.threads", "0") == 0 );
    t( sp_set(c, "compaction.page_checksum", "1") == 0 );
    t( sp_set(c, "log.enable", "1") == 0 );
    t( sp_set(c, "log.path", cx->suite->logdir) == 0 );
    t( sp_set(c, "log.sync", "0") == 0 );
    t( sp_set(c, "log.rotate_sync", "0") == 0 );
    t( sp_set(c, "db", "test") == 0 );
    t( sp_set(c, "db.test.path", cx->suite->dir) == 0 );
    t( sp_set(c, "db.test.format", "kv") == 0 );
    t( sp_set(c, "db.test.sync", "0") == 0 );
    t( sp_set(c, "db.test.compression", "none") == 0 );
    t( sp_set(c, "db.test.index.cmp", "u32", NULL) == 0 );
    cx->db = sp_get(c, "db.test");
    t( cx->db != NULL );
}
Exemple #12
0
static void
gh_29(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 );
	void *db = sp_open(env);
	t( db != NULL );
	int i;
	for (i = 1; i <= 10; i++)  {
		char key[4];
		int len = snprintf(key, sizeof(key), "%d", i);
		t( sp_set(db, key, len, key, len) == 0 );
	}
	for (i = 1; i <= 10; i++) {
		char key[4];
		int len = snprintf(key, sizeof(key), "%d", i);
		size_t vsize = 0;
		void *vp = NULL;
		t( sp_get(db, key, len, &vp, &vsize) == 1 );
		t( vsize == len );
		t( memcmp(key, vp, len) == 0 );
		free(vp);
	}
	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	t( rmrf(dbrep) == 0 );
}
Exemple #13
0
static void
gh_5(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 );
	void *db = sp_open(env);
	t( db != NULL );

	char k1[] = "key-1";
	char v1[] = "val-1";
	t( sp_set(db, k1, sizeof(k1), v1, sizeof(v1)) == 0);

	char k2[] = "key-10";
	char v2[] = "val-10";
	t( sp_set(db, k2, sizeof(k2), v2, sizeof(v2)) == 0);

	size_t ressize = 0;
	void *res = NULL;
	t( sp_get(db, k1, sizeof(k1), &res, &ressize) == 1 );

	t( ressize == sizeof(v1) );
	t( strcmp(v1, res) == 0 );

	free(res);

	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	t( rmrf(dbrep) == 0 );
}
Exemple #14
0
static void
multi_process_2(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 );
	t( sp_ctl(env, SPCMP, cmp, NULL) == 0 );
	void *db = sp_open(env);
	t( db != NULL );
	void *env2 = sp_env();
	t( env != NULL );
	t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep2) == 0 );
	t( sp_ctl(env2, SPCMP, cmp, NULL) == 0 );
	uint32_t k = 1;
	size_t vsize = 0;
	void *vp = NULL;
	int pid = fork();
	t( pid != -1 );
	if (pid == 0) {
		void *db2 = sp_open(env2);
		t( db2 != NULL );
		t( sp_set(db2, &k, sizeof(k), &k, sizeof(k)) == 0 );
		t( sp_get(db2, &k, sizeof(k), &vp, &vsize) == 1 );
		t( vsize == sizeof(k) );
		t( *(uint32_t*)vp == k );
		free(vp);
		t( sp_destroy(db2) == 0 );
		t( sp_destroy(env2) == 0 );
		t( rmrf(dbrep2) == 0 );
		/* valgrind: parent db and env are unfreed here, and that
		 *           is correct otherwise destroy would
		 *           corrupt the database.
		*/
		exit(0);
	}
	k = 3;
	t( sp_set(db, &k, sizeof(k), &k, sizeof(k)) == 0 );
	t( sp_get(db, &k, sizeof(k), &vp, &vsize) == 1 );
	t( vsize == sizeof(k) );
	t( *(uint32_t*)vp == k );
	free(vp);
	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	t( rmrf(dbrep1) == 0 );
	int status = 0;
	t( waitpid(pid, &status, 0) == pid );
	t( status == 0 );
}
Exemple #15
0
static void
single_process(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 );
	void *db = sp_open(env);
	t( db != NULL );
	void *env2 = sp_env();
	t( env2 != NULL );
	t( sp_ctl(env2, SPDIR, SPO_CREAT|SPO_RDWR, dbrep1) == 0 );
	void *db2 = sp_open(env2);
	t( db2 == NULL );
	t( sp_destroy(env2) == 0 );
	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	rmrf(dbrep1);
}
Exemple #16
0
void st_scene_multithread_1(stscene *g, stc *cx)
{
    printf(".multithread");
    fflush(NULL);
    void *c = sp_ctl(cx->env);
    t( c != NULL );
    t( sp_set(c, "scheduler.threads", "1") == 0 );
}
Exemple #17
0
static void
gh_37(void)
{
	const int size = 999;
	const int watermark = 3996;
	const int pagesize = 1022;

	char data[size];
	memset(data, 0, sizeof(data));

	void *env = sp_env();
	int rc = sp_ctl(env, SPDIR, SPO_CREAT | SPO_RDWR, dbrep);
	t(rc == 0);
	if (rc == -1)

	rc = sp_ctl(env, SPMERGE, 0);
	t(rc == 0);

	rc = sp_ctl(env, SPMERGEWM, watermark);
	if (rc == -1)
	t(rc == 0);

	sp_ctl(env, SPPAGE, pagesize);
	t(rc == 0);

	void *db = sp_open(env);
	t(db != NULL);

	int byteCount = 0;
	int i;
	for (i = 0; i < 100000; ++i) {
		rc = sp_set(db, &i, sizeof(int), data, size);
		t(rc == 0);
	}

	byteCount += size;
	if (i != 0 && i % watermark == 0) {
		rc = sp_ctl(db, SPMERGEFORCE);
		t(rc == 0);
	}

	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	t( rmrf(dbrep) == 0 );
}
Exemple #18
0
static void
error_nonfatal(void) {
	void *env = sp_env();
	t( env != NULL );
	t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 );
	t( sp_ctl(env, SPCMP, cmp, NULL) == 0 );
	void *db = sp_open(env);
	t( db != NULL );
	uint32_t k = 1, v = 1;
	t( sp_set(db, &k, sizeof(k), &v, sizeof(v)) == 0);
	t( sp_set(db, &k, UINT16_MAX + 1 , &v, sizeof(v)) == -1);
	t( sp_error(db) != NULL );
	t( sp_set(db, &k, sizeof(k), &v, sizeof(v)) == 0);
	t( sp_error(db) == NULL );
	t( sp_destroy(db) == 0 );
	t( sp_destroy(env) == 0 );
	t( rmrf(dbrep) == 0 );
}
Exemple #19
0
static void
mt_multi_stmt(stc *cx)
{
	cx->env = sp_env();
	t( cx->env != NULL );
	void *c = sp_ctl(cx->env);
	t( c != NULL );
	t( sp_set(c, "sophia.path", cx->suite->sophiadir) == 0 );
	t( sp_set(c, "scheduler.threads", "3") == 0 );
	t( sp_set(c, "log.path", cx->suite->logdir) == 0 );
	t( sp_set(c, "log.sync", "0") == 0 );
	t( sp_set(c, "log.rotate_sync", "0") == 0 );
	t( sp_set(c, "db", "test") == 0 );
	t( sp_set(c, "db.test.path", cx->suite->dir) == 0 );
	t( sp_set(c, "db.test.sync", "0") == 0 );
	cx->db = sp_get(c, "db.test");
	t( cx->db != NULL );
	t( sp_open(cx->env) == 0 );

	ssa a;
	ss_aopen(&a, &ss_stda);
	srscheme cmp;
	sr_schemeinit(&cmp);
	srkey *part = sr_schemeadd(&cmp, &a);
	t( sr_keysetname(part, &a, "key") == 0 );
	t( sr_keyset(part, &a, "u32") == 0 );
	ssinjection ij;
	memset(&ij, 0, sizeof(ij));
	srerror error;
	sr_errorinit(&error);
	srseq seq;
	sr_seqinit(&seq);
	sscrcf crc = ss_crc32c_function();
	sr r;
	sr_init(&r, &error, &a, &seq, SF_KV, SF_SRAW, &cmp, &ij, crc, NULL);

	soworkers w;
	so_workersinit(&w);
	void *ptr[2] = { cx->env, cx->db };
	t( so_workersnew(&w, &r, 5, multi_stmt_thread, (void*)ptr) == 0 );
	t( so_workersshutdown(&w, &r) == 0 );
	sr_schemefree(&cmp, &a);

	void *o = sp_get(c, "db.test.index.count");
	t( o != NULL );
	t( strcmp( sp_get(o, "value", NULL), "100000") == 0 );
	sp_destroy(o);

	t( sp_destroy(cx->env) == 0 );
}
Exemple #20
0
int
main(int argc, char * argv[])
{
	if (argc != 3)
		usage(argv[0]);

	int n = atol(argv[2]);
	srand(701888);

	env = sp_env();
	sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, "./sophia_bench_data");
	db = sp_open(env);
	if (db == NULL) {
		printf("open: %s\n", sp_error(env));
		return 1;
	}
	
	int rc = 0;
	unsigned long long start = now();
	
	switch (benchof(argv[1]))
	{
	case SETSEQ: rc = setseq(n);
		break;
	case SETRAND: rc = setrand(n);
		break;
	case GETSEQ:rc = getseq(n);
		break;
	case GETRAND: rc = getrand(n);
		break;
	case RANGEFWD: rc = rangefwd(n);
		break;
	case UNKNOWN:
		printf("unknown operation\n");
	}

	if (rc == 0) {
		unsigned long long diff = now() - start;
		float rps = n / (diff / 1000.0);
		printf("%d rps\n", (int)rps);
	}
	if (keytraversed > 0 && keytraversed != n)
		printf("(%d keys traversed)\n", keytraversed);

	sp_destroy(db);
	sp_destroy(env);
	return rc;
}
Exemple #21
0
jlong JNICALL Java_eu_unicredit_sophia_SophiaInterface_sp_1ctl
  (JNIEnv * env, jobject obj, jlongArray args)
{
    //devo verificare la trasformazione da array a vararg se si può fare meglio...
    jlong* array = (*env)->GetLongArrayElements(env, args,JNI_FALSE);
    jint size = (*env)->GetArrayLength(env, args);
    void* ret = NULL;
    switch(size) {
      case 0:
        printf("Not supported\n");
        break;
      case 1:
        ret = sp_ctl((void*)array[0]);
        break;
      default:
        printf("Not supported\n");
        break;
    };
    return (jlong)ret;
}
Exemple #22
0
static void
mt_quota_age(stc *cx)
{
	cx->env = sp_env();
	t( cx->env != NULL );
	void *c = sp_ctl(cx->env);
	t( c != NULL );
	t( sp_set(c, "sophia.path", cx->suite->sophiadir) == 0 );
	t( sp_set(c, "memory.limit", "524288") == 0 ); /* 512k */
	/* 100Mb branch_wm to avoid branching */
	t( sp_set(c, "compaction.80.mode", "3") == 0 );
	t( sp_set(c, "compaction.80.branch_wm", "104857600") == 0 );
	t( sp_set(c, "compaction.80.branch_age_period", "1") == 0 );
	t( sp_set(c, "compaction.80.branch_age", "1") == 0 );
	t( sp_set(c, "compaction.80.branch_prio", "3") == 0 );
	t( sp_set(c, "compaction.80.branch_age_wm", "500000") == 0 );
	t( sp_set(c, "scheduler.threads", "5") == 0 );
	t( sp_set(c, "log.path", cx->suite->logdir) == 0 );
	t( sp_set(c, "log.sync", "0") == 0 );
	t( sp_set(c, "log.rotate_sync", "0") == 0 );
	t( sp_set(c, "db", "test") == 0 );
	t( sp_set(c, "db.test.path", cx->suite->dir) == 0 );
	t( sp_set(c, "db.test.sync", "0") == 0 );
	t( sp_set(c, "db.test.index.cmp", "u32", NULL) == 0 );
	cx->db = sp_get(c, "db.test");
	t( cx->db != NULL );
	t( sp_open(cx->env) == 0 );
	char value[1000];
	memset(value, 0, sizeof(value));
	int i = 0;
	while (i < 20000) { /* ~ 20Mb */
		void *o = sp_object(cx->db);
		assert(o != NULL);
		sp_set(o, "key", &i, sizeof(i));
		sp_set(o, "value", value, sizeof(value));
		int rc = sp_set(cx->db, o);
		t( rc == 0 );
		i++;
	}
	t( sp_destroy(cx->env) == 0 );
}
Exemple #23
0
    BackendImp (int keyBytes,
             Parameters const& keyValues,
             Scheduler& scheduler)
        : m_keyBytes (keyBytes)
        , m_scheduler (scheduler)
        , m_batch (*this, scheduler)
        , m_name (keyValues ["path"].toStdString ())
        , m_env (nullptr)
        , m_db (nullptr)
    {
        if (m_name.empty())
            Throw (std::runtime_error ("Missing path in Sophia backend"));

        m_env = sp_env ();

        if (m_env != nullptr)
        {
            sp_ctl (m_env, SPDIR, SPO_RDWR | SPO_CREAT, m_name.c_str());
            m_db = sp_open (m_env);
        }
    }
Exemple #24
0
sophiadb_t* db_init(void * conf) {
	
	conf_t * pconf = (conf_t *)conf;
	printf("size of datatype = %d\n",pconf->max_num);

	sophiadb_t* db = (sophiadb_t*) calloc(pconf->max_num, sizeof(sophiadb_t));	
	if (!db) {
		printf("error alloc size=%lu\n", sizeof(sophiadb_t));
		exit(1);
	}
	
	assert(db);	

	datatype_t * p = (datatype_t *)pconf->list_datatypes;	
	
	spcmpf fcmp ;

	while(p) {
		fcmp = cmp0;

		if (p->number <= 0) {
			printf("error config parametr: number=%d \n", p->number );
			free(db);
			return NULL;
		}

		db[p->number-1].type = p->type;
		db[p->number-1].datadir = p->datadir;

		switch (p->type) {
			case SPHDB_INT :
				db[p->number-1].datalen = sizeof(int32_t);
				fcmp = cmp32;
				break;
		
			case SPHDB_LONG : 
				db[p->number-1].datalen = sizeof(int64_t);
				fcmp = cmp64;
				break;

			case SPHDB_STRING :	
				db[p->number-1].datalen = SPHDB_STRLEN;
				break;

			default:
				printf("error: undefined config datatype\n");
				free(db);
				exit(1);
		}

	printf("fcmp: fcmp=%x\n", fcmp);

		db[p->number-1].env = sp_env();
		if (db[p->number-1].env == NULL) {
			/* memory allocation error */
			printf("env error\n");
			free(db);
			exit(1);
		}

		int rc = sp_ctl(db[p->number-1].env, SPDIR, SPO_CREAT|SPO_RDWR, p->datadir);
		if (rc == -1) {
			/* memory allocation error */
			printf("error: %s\n", sp_error(db[p->number-1].env));
			sp_destroy(db[p->number-1].env);
			free(db);
			exit(1);
		}

		if (fcmp != cmp0) {	
			printf("%s type=%d %x\n", p->datadir, p->type , fcmp);
			rc = sp_ctl(db[p->number-1].env, SPCMP, fcmp, NULL); 
			if (rc == -1) {
				/* memory allocation error */
				printf("error: %s\n", sp_error(db[p->number-1].env));
				sp_destroy(db[p->number-1].env);
				free(db);
				exit(1);
			}
		}

		db[p->number-1].db = sp_open(db[p->number-1].env);
		if (db[p->number-1].db == NULL) {
		    printf("sp_open: %s\n", sp_error(db[p->number-1].env));
		    sp_destroy(db[p->number-1].env);
		    exit(1);
		}

	 	p = p->next;
	}


	printf("db is open %s\n", pconf->datadir );


	return db;
}
Exemple #25
0
void
st_scene_branch_wm(stscene *g, stc *cx)
{
    void *c = sp_ctl(cx->env);
    t( sp_set(c, "compaction.0.branch_wm", "1") == 0 );
}
Exemple #26
0
 * Copyright (c) Dmitry Simonenko
 * BSD License
*/

#include <libss.h>
#include <libsf.h>
#include <libss.h>
#include <libst.h>
#include <sophia.h>

static void
branch_loggc(stc *cx ssunused)
{
	void *env = sp_env();
	t( env != NULL );
	void *c = sp_ctl(env);
	t( c != NULL );
	t( sp_set(c, "sophia.path", cx->suite->sophiadir) == 0 );
	t( sp_set(c, "scheduler.threads", "0") == 0 );
	t( sp_set(c, "log.path", cx->suite->logdir) == 0 );
	t( sp_set(c, "log.sync", "0") == 0 );
	t( sp_set(c, "log.rotate_sync", "0") == 0 );
	t( sp_set(c, "db", "test") == 0 );
	t( sp_set(c, "db.test.path", cx->suite->dir) == 0 );
	t( sp_set(c, "db.test.sync", "0") == 0 );
	t( sp_set(c, "db.test.index.cmp", "u32", NULL) == 0 );
	t( sp_set(c, "compaction.0.branch_wm", "1") == 0 );
	void *db = sp_get(c, "db.test");
	t( db != NULL );
	t( sp_open(env) == 0 );
static void
recover_loop(stc *cx)
{
	int seedprev = -1;
	int seed = 3424118;
	int seedorigin = seed;
	int run = 10;
	int count = 1040;

	while (run >= 0) {
		cx->env = sp_env();
		t( cx->env != NULL );
		void *c = sp_ctl(cx->env);
		t( c != NULL );
		t( sp_set(c, "sham.path", cx->suite->shamdir) == 0 );
		t( sp_set(c, "scheduler.threads", "0") == 0 );
		t( sp_set(c, "compaction.0.branch_wm", "1") == 0 );
		t( sp_set(c, "log.path", cx->suite->logdir) == 0 );
		t( sp_set(c, "log.sync", "0") == 0 );
		t( sp_set(c, "log.rotate_sync", "0") == 0 );
		t( sp_set(c, "db", "test") == 0 );
		t( sp_set(c, "db.test.path", cx->suite->dir) == 0 );
		t( sp_set(c, "db.test.sync", "0") == 0 );
		t( sp_set(c, "db.test.index.cmp", "u32", NULL) == 0 );
		cx->db = sp_get(c, "db.test");
		t( cx->db != NULL );
		t( sp_open(cx->env) == 0 );
	
		int i = 0;
		if (seedprev != -1) {
			srand(seedprev);
			while (i < count) {
				int k = rand();
				void *o = sp_object(cx->db);
				t( sp_set(o, "key", &k, sizeof(k)) == 0 );
				o = sp_get(cx->db, o);
				t( o != NULL );
				st_transaction(cx);
				t( *(int*)sp_get(o, "value", NULL) == k );
				sp_destroy(o);
				i++;
			}
			t( sp_set(c, "db.test.branch") == 0 );
		}

		srand(seed);
		i = 0;
		while (i < count) {
			int k = rand();
			void *o = sp_object(cx->db);
			t( o != NULL );
			t( sp_set(o, "key", &k, sizeof(k)) == 0 );
			t( sp_set(o, "value", &k, sizeof(k)) == 0 );
			t( sp_set(cx->db, o) == 0 );
			st_transaction(cx);
			i++;
		}
		t( sp_destroy(cx->env) == 0 );

		seedprev = seed;
		seed = time(NULL);
		run--;
	}

	cx->env = sp_env();
	t( cx->env != NULL );
	void *c = sp_ctl(cx->env);
	t( c != NULL );
	t( sp_set(c, "sham.path", cx->suite->shamdir) == 0 );
	t( sp_set(c, "scheduler.threads", "0") == 0 );
	t( sp_set(c, "compaction.0.branch_wm", "1") == 0 );
	t( sp_set(c, "log.path", cx->suite->logdir) == 0 );
	t( sp_set(c, "log.sync", "0") == 0 );
	t( sp_set(c, "log.rotate_sync", "0") == 0 );
	t( sp_set(c, "db", "test") == 0 );
	t( sp_set(c, "db.test.path", cx->suite->dir) == 0 );
	t( sp_set(c, "db.test.sync", "0") == 0 );
	t( sp_set(c, "db.test.index.cmp", "u32", NULL) == 0 );
	cx->db = sp_get(c, "db.test");
	t( cx->db != NULL );
	t( sp_open(cx->env) == 0 );

	srand(seedorigin);
	int i = 0;
	while (i < count) {
		int k = rand();
		void *o = sp_object(cx->db);
		t( sp_set(o, "key", &k, sizeof(k)) == 0 );
		o = sp_get(cx->db, o);
		t( o != NULL );
		t( *(int*)sp_get(o, "value", NULL) == k );
		sp_destroy(o);
		i++;
	}
	t( sp_destroy(cx->env) == 0 );
	cx->env = NULL;
	cx->db  = NULL;
}