static void batch_delete_commit(void) { void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_setstring(env, "log.path", st_r.conf->log_dir, 0) == 0 ); t( sp_setstring(env, "db", "test", 0) == 0 ); t( sp_setstring(env, "db.test.path", st_r.conf->db_dir, 0) == 0 ); t( sp_setstring(env, "db.test.index.key", "u32", 0) == 0 ); t( sp_setint(env, "db.test.sync", 0) == 0 ); void *db = sp_getobject(env, "db.test"); t( db != NULL ); t( sp_open(env) == 0 ); void *batch = sp_batch(db); t( batch != NULL ); int key = 123; void *o = sp_object(db); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_delete(batch, o) == 0 ); t( sp_commit(batch) == 0 ); t( sp_destroy(env) == 0 ); }
static void compact_delete1(void) { void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_setint(env, "compaction.0.branch_wm", 1) == 0 ); t( sp_setstring(env, "log.path", st_r.conf->log_dir, 0) == 0 ); t( sp_setstring(env, "db", "test", 0) == 0 ); t( sp_setstring(env, "db.test.path", st_r.conf->db_dir, 0) == 0 ); t( sp_setstring(env, "db.test.scheme", "key", 0) == 0 ); t( sp_setstring(env, "db.test.scheme.key", "u32,key", 0) == 0 ); t( sp_setstring(env, "db.test.scheme", "value", 0) == 0 ); t( sp_setint(env, "db.test.sync", 0) == 0 ); void *db = sp_getobject(env, "db.test"); t( db != NULL ); t( sp_open(env) == 0 ); int key = 0; while (key < 20) { void *o = sp_document(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_setstring(o, "value", &key, sizeof(key)) == 0 ); t( sp_set(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); key = 0; while (key < 20) { void *o = sp_document(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_delete(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); void *o = sp_document(db); t( o != NULL ); void *cur = sp_cursor(env); t( o != NULL ); int i = 0; while ((o = sp_get(cur, o))) { t( *(int*)sp_getstring(o, "key", NULL) == i ); i++; } t( i == 0 ); t( sp_destroy(env) == 0 ); }
static void github_120(void) { /* open or create environment and database */ void *env = sp_env(); sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0); sp_setstring(env, "db", "test", 0); void *db = sp_getobject(env, "db.test"); int rc = sp_open(env); if (rc == -1) goto error; /* set */ void *o = sp_document(db); sp_setstring(o, "key", "hello", 0); sp_setstring(o, "value", "world", 0); rc = sp_set(db, o); if (rc == -1) goto error; /* get */ o = sp_document(db); sp_setstring(o, "key", "hello", 0); o = sp_get(db, o); if (o) { /* ensure key and value are correct */ int size; char *ptr = sp_getstring(o, "key", &size); t( size == 5 ); t( strncmp(ptr, "hello", 5) == 0 ); ptr = sp_getstring(o, "value", &size); t( size == 5 ); t( strncmp(ptr, "world", 5) == 0 ); sp_destroy(o); } /* delete */ o = sp_document(db); sp_setstring(o, "key", "hello", 0); rc = sp_delete(db, o); if (rc == -1) goto error; /* finish work */ sp_destroy(env); return; error:; int size; char *error = sp_getstring(env, "sophia.error", &size); printf("error: %s\n", error); free(error); sp_destroy(env); }
static void mt_set_delete_get(void) { void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0) == 0 ); t( sp_setint(env, "scheduler.threads", 5) == 0 ); t( sp_setstring(env, "log.path", st_r.conf->log_dir, 0) == 0 ); t( sp_open(env) == 0 ); t( sp_setstring(env, "db", "test", 0) == 0 ); t( sp_setstring(env, "db.test.path", st_r.conf->db_dir, 0) == 0 ); t( sp_setstring(env, "db.test.index.key", "u32", 0) == 0 ); t( sp_setint(env, "db.test.sync", 0) == 0 ); void *db = sp_getobject(env, "db.test"); t( db != NULL ); t( sp_open(db) == 0 ); char value[100]; memset(value, 0, sizeof(value)); uint32_t n = 700000; uint32_t i, k; srand(82351); for (i = 0; i < n; i++) { k = rand(); *(uint32_t*)value = k; void *o = sp_object(db); t( o != NULL ); t( sp_setstring(o, "key", &k, sizeof(k)) == 0 ); t( sp_setstring(o, "value", value, sizeof(value)) == 0 ); t( sp_set(db, o) == 0 ); print_current(i); } srand(82351); for (i = 0; i < n; i++) { k = rand(); *(uint32_t*)value = k; void *o = sp_object(db); t( o != NULL ); t( sp_setstring(o, "key", &k, sizeof(k)) == 0 ); t( sp_setstring(o, "value", value, sizeof(value)) == 0 ); t( sp_delete(db, o) == 0 ); print_current(i); } srand(82351); for (i = 0; i < n; i++) { k = rand(); void *o = sp_object(db); t( o != NULL ); t( sp_setstring(o, "key", &k, sizeof(k)) == 0 ); o = sp_get(db, o); t( o == NULL ); print_current(i); } t( sp_destroy(env) == 0 ); }
PUBLIC void test_sp_discard() /* This should be the final test because it can mess up the test directory * structure severely. */ { errstat err; bool ok; capset tmp_capset, disc_capset; (void)strcpy(testname, "test_sp_discard()"); if (verbose) printf("\n---- %s ----\n", testname); /* Discard an empty directory with minimal rights. */ /* Create a new directory. */ err = sp_create(SP_DEFAULT, def_colnames, &tmp_capset); if (test_good(err, "1a, sp_create()")) { /* Append and look-up to get a restricted capset. */ err = sp_append(SP_DEFAULT, aux_name, &tmp_capset, 1, rgt_masks[DEL]); if (ok = test_good(err, "1a, sp_append()")) { err = sp_lookup(SP_DEFAULT, aux_name, &disc_capset); if (ok = test_good(err, "1a, sp_lookup()")) { /* Try to discard the restricted empty capset. */ err = sp_discard(&disc_capset); ok = test_good(err, "1a"); /* Free the space for the capset. */ cs_free(&disc_capset); } /* Delete the entry for the original capset. */ err = sp_delete(SP_DEFAULT, aux_name); (void)test_good(err, "1a, sp_delete()"); } if (!ok) { /* Destroy the directory using the unrestricted capset. */ err = sp_discard(&tmp_capset); (void)test_good(err, "1a, sp_discard()"); } cs_free(&tmp_capset); } } /* test_sp_discard() */
void st_scene_truncate(stscene *g, stc *cx) { printf(".truncate"); fflush(NULL); void *o = sp_object(cx->db); t( o != NULL ); void *c = sp_cursor(cx->db, o); t( c != NULL ); while ((o = sp_get(c))) { void *k = sp_object(cx->db); t( k != NULL ); int keysize; void *key = sp_get(o, "key", &keysize); sp_set(k, "key", key, keysize); t( sp_delete(cx->db, k) == 0 ); } t( sp_destroy(c) == 0 ); }
static void compact_delete_node0(void) { void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_setint(env, "compaction.0.branch_wm", 1) == 0 ); t( sp_setstring(env, "log.path", st_r.conf->log_dir, 0) == 0 ); t( sp_setstring(env, "db", "test", 0) == 0 ); t( sp_setstring(env, "db.test.path", st_r.conf->db_dir, 0) == 0 ); t( sp_setstring(env, "db.test.index.key", "u32", 0) == 0 ); t( sp_setint(env, "db.test.sync", 0) == 0 ); void *db = sp_getobject(env, "db.test"); t( db != NULL ); t( sp_open(env) == 0 ); int key = 0; while (key < 20) { void *o = sp_object(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_setstring(o, "value", &key, sizeof(key)) == 0 ); t( sp_set(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); key = 0; while (key < 20) { void *o = sp_object(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_delete(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); t( sp_destroy(env) == 0 ); }
static PyObject * sophia_db_delete(SophiaDB *db, PyObject *args) { char *key; PyObject *pkey; Py_ssize_t ksize; ensure_is_opened(db, NULL); if (!PyArg_UnpackTuple(args, "delete", 1, 1, &pkey) || PyBytes_AsStringAndSize(pkey, &key, &ksize) == -1) return NULL; if (sp_delete(db->db, key, (size_t)ksize) == -1) { PyErr_SetString(SophiaError, sp_error(db->db)); return NULL; } Py_RETURN_NONE; }
void symbol_free_db_mem(const void *s, int slen, struct sptree *spt) { register spkey_t key; register struct syment *se, *pe; struct spblk *spl; if (s == NULL || spt == NULL) return; key = crc32n(s, slen); /* Ok, time for the hard work. Lets see if we have this key in the symtab splay tree (we can't use cache here!) */ pe = NULL; spl = sp_lookup(key, spt); if (spl != NULL) { /* Got it ! Now see that we really have it, and not only have a hash collision */ se = (struct syment *)spl->data; do { if (se->namelen == slen && memcmp(se->name, s, slen) == 0) { /* Really found it! */ if (pe != NULL) pe->next = se->next; else spl->data = (void*) se->next; hfree(se); break; } pe = se; se = se->next; } while (se != NULL); } if (spl != NULL && spl->data == NULL) sp_delete(spl, spt); }
int todo_db_rm_each (todo_db_t *db, int (*fn)(char *key, char *value, todo_db_t *db)) { int rc = -1; int i = 0, l = 0; void *c = sp_cursor(db->dbh, SPGT, NULL, 0); const char *ckey; const char *cvalue; char *keys[512]; size_t sizes[512] = { }; size_t size; while (sp_fetch(c)) { ckey = sp_key(c); cvalue = sp_value(c); size = sp_keysize(c); rc = fn(strdup((char *) ckey), strdup((char *)cvalue), db); if (0 == rc) { keys[i++] = (char *)ckey; sizes[i] = size; if (-1 == rc) { return rc; } } } sp_destroy(c); l = i; i = 0; for (; i < l; ++i) { rc = sp_delete(db->dbh, (const char *)keys[i], strlen((const char*) keys[i])); if (-1 == rc) { return rc; } } return rc; }
jint JNICALL Java_eu_unicredit_sophia_SophiaInterface_sp_1delete (JNIEnv * env, jobject obj, jlongArray args) { jlong* array = (*env)->GetLongArrayElements(env, args,JNI_FALSE); jint size = (*env)->GetArrayLength(env, args); int ret = -1; switch(size) { case 0: printf("Not supported\n"); break; case 1: printf("Not supported\n"); break; case 2: ret = sp_delete((void*)array[0], (void*)array[1]); break; default: printf("Not supported\n"); break; }; return (jint)ret; }
int main(int argc, char *argv[]) { (void)argc; (void)argv; /* * Do set, get, delete operations. (see transaction.c) */ /* open or create environment and database */ void *env = sp_env(); sp_setstring(env, "sophia.path", "_test", 0); sp_setstring(env, "db", "test", 0); void *db = sp_getobject(env, "db.test"); int rc = sp_open(env); if (rc == -1) goto error; /* set */ uint32_t key = 1; void *o = sp_object(db); sp_setstring(o, "key", &key, sizeof(key)); sp_setstring(o, "value", &key, sizeof(key)); rc = sp_set(db, o); if (rc == -1) goto error; /* get */ o = sp_object(db); sp_setstring(o, "key", &key, sizeof(key)); o = sp_get(db, o); if (o) { /* ensure key and value are correct */ int size; char *ptr = sp_getstring(o, "key", &size); assert(size == sizeof(uint32_t)); assert(*(uint32_t*)ptr == key); ptr = sp_getstring(o, "value", &size); assert(size == sizeof(uint32_t)); assert(*(uint32_t*)ptr == key); sp_destroy(o); } /* delete */ o = sp_object(db); sp_setstring(o, "key", &key, sizeof(key)); rc = sp_delete(db, o); if (rc == -1) goto error; /* finish work */ sp_destroy(env); return 0; error:; int size; char *error = sp_getstring(env, "sophia.error", &size); printf("error: %s\n", error); free(error); sp_destroy(env); return 1; }
static void jsonout_export(void) { unsigned int entries = 0; unsigned int exported = 0; struct spblk *x, *nextx; struct sptree *sp; struct cache_ent *e; char *json = NULL; int got_pos; char tbuf[TBUF_LEN]; time_t now; time(&now); time_jsonais(&now, tbuf, TBUF_LEN); /* fill in initial json */ json = str_append(json, "{\n" "\t\"protocol\": \"jsonais\",\n" "\t\"encodetime\": \"%s\",\n" "\t\"groups\": [\n" /* start of groups */ "\t\t{\n" /* start of group */ "\t\t\t\"path\": [ { \"name\": \"%s\" } ],\n" "\t\t\t\"msgs\": [\n", tbuf, mycall ); /* get the current position cache */ sp = cache_rotate(); /* expire old entries */ for (x = sp_fhead(sp); x != NULL; x = nextx) { entries++; nextx = sp_fnext(x); e = (struct cache_ent *)x->data; got_pos = ((e->lat > 0.0001 || e->lat < -0.0001) && (e->lon > 0.0001 || e->lon < -0.0001)); if ((e->mmsi) && (got_pos) ) { hlog(LOG_DEBUG, "jsonout: exporting MMSI %d position", e->mmsi); time_jsonais(&e->received_pos, tbuf, TBUF_LEN); json = str_append(json, "%s{\"msgtype\": 3, \"mmsi\": %d, \"rxtime\": \"%s\"", (exported == 0) ? "" : ",\n", e->mmsi, tbuf ); json = str_append(json, ", \"lat\": %.7f, \"lon\": %.7f", e->lat, e->lon ); if (e->course >= 0) json = str_append(json, ", \"course\": %.1f", e->course); if (e->hdg >= 0) json = str_append(json, ", \"heading\": %d", e->hdg); if (e->sog >= 0) json = str_append(json, ", \"speed\": %.1f", e->sog); if (e->navstat >= 0) json = str_append(json, ", \"status\": %d", e->navstat); json = str_append(json, "}"); exported++; } if ((e->mmsi) && (e->name) ) { hlog(LOG_DEBUG, "jsonout: exporting MMSI %d data", e->mmsi); time_jsonais(&e->received_data, tbuf, TBUF_LEN); json = str_append(json, "%s{\"msgtype\": 5, \"mmsi\": %d, \"rxtime\": \"%s\"", (exported == 0) ? "" : ",\n", e->mmsi, tbuf ); if (e->imo >= 0) json = str_append(json, ", \"imo\": %d", e->imo); if (e->shiptype >= 0) json = str_append(json, ", \"shiptype\": %d", e->shiptype); if (e->callsign) json = str_append(json, ", \"callsign\": \"%s\"", e->callsign); if (e->name) json = str_append(json, ", \"shipname\": \"%s\"", e->name); if (e->destination) json = str_append(json, ", \"destination\": \"%s\"", e->destination); if (e->A >= 0 && e->B >= 0) { json = str_append(json, ", \"length\": %d", e->A + e->B); json = str_append(json, ", \"ref_front\": %d", e->A); } if (e->draught >= 0) json = str_append(json, ", \"draught\": %.1f", e->draught); if (e->C >= 0 && e->D >= 0) { json = str_append(json, ", \"width\": %d", e->C + e->D); json = str_append(json, ", \"ref_left\": %d", e->C); } json = str_append(json, "}"); exported++; } if (e->persons_on_board >= 0) { hlog(LOG_DEBUG, "jsonout: exporting MMSI %d persons_on_board %d", e->mmsi, e->persons_on_board); time_jsonais(&e->received_persons_on_board, tbuf, TBUF_LEN); json = str_append(json, "%s{\"msgtype\": 8, \"mmsi\": %d, \"persons_on_board\": %d, \"rxtime\": \"%s\"}", (exported == 0) ? "" : ",\n", e->mmsi, e->persons_on_board, tbuf ); exported++; } cache_free_entry(e); sp_delete(x, sp); } json = str_append(json, "\n\n" "\t\t\t]\n" /* end of message array */ "\t\t}\n" /* end of the message group */ "\t]\n" /* end of groups */ "}\n" /* end of the whole json blob */ ); /* clean up */ if (sp) { sp_null(sp); hfree(sp); } hlog(LOG_DEBUG, "jsonout: %s", json); if (exported) { /* if we have some entries, send them out */ jsonout_post_all(json); } hfree(json); }
static void compact_delete_node1(void) { void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_setint(env, "compaction.0.branch_wm", 1) == 0 ); t( sp_setint(env, "compaction.node_size", 524288 /* 512K */) == 0 ); t( sp_setstring(env, "log.path", st_r.conf->log_dir, 0) == 0 ); t( sp_setstring(env, "db", "test", 0) == 0 ); t( sp_setstring(env, "db.test.path", st_r.conf->db_dir, 0) == 0 ); t( sp_setstring(env, "db.test.index.key", "u32", 0) == 0 ); t( sp_setint(env, "db.test.sync", 0) == 0 ); void *db = sp_getobject(env, "db.test"); t( db != NULL ); t( sp_open(env) == 0 ); int key = 0; char value[100]; memset(value, 0, sizeof(value)); while (key < 13000) { void *o = sp_object(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_setstring(o, "value", value, sizeof(value)) == 0 ); t( sp_set(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); t( sp_getint(env, "db.test.index.node_count") == 2 ); key = 0; while (key < 5511 ) { void *o = sp_object(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_delete(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); t( sp_getint(env, "db.test.index.node_count") == 1 ); void *o = sp_object(db); void *cur = sp_cursor(db, o); while ((o = sp_get(cur, NULL))) { t( sp_delete(db, o) == 0 ); key++; sp_destroy(o); } sp_destroy(cur); t( key == 13000 ); t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); t( sp_getint(env, "db.test.index.node_count") == 1 ); t( sp_destroy(env) == 0 ); }
static void compact_delete_node1(void) { void *env = sp_env(); t( env != NULL ); t( sp_setstring(env, "sophia.path", st_r.conf->sophia_dir, 0) == 0 ); t( sp_setint(env, "scheduler.threads", 0) == 0 ); t( sp_setint(env, "compaction.0.branch_wm", 1) == 0 ); t( sp_setstring(env, "log.path", st_r.conf->log_dir, 0) == 0 ); t( sp_setstring(env, "db", "test", 0) == 0 ); t( sp_setstring(env, "db.test.path", st_r.conf->db_dir, 0) == 0 ); t( sp_setstring(env, "db.test.scheme", "key", 0) == 0 ); t( sp_setstring(env, "db.test.scheme.key", "u32,key", 0) == 0 ); t( sp_setstring(env, "db.test.scheme", "value", 0) == 0 ); t( sp_setint(env, "db.test.node_size", 716800/* 700K */) == 0 ); t( sp_setint(env, "db.test.sync", 0) == 0 ); void *db = sp_getobject(env, "db.test"); t( db != NULL ); t( sp_open(env) == 0 ); int key = 0; char value[100]; memset(value, 0, sizeof(value)); while (key < 13000) { void *o = sp_document(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_setstring(o, "value", value, sizeof(value)) == 0 ); t( sp_set(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); t( sp_getint(env, "db.test.index.node_count") == 2 ); key = 0; while (key < 6511 ) { void *o = sp_document(db); t( o != NULL ); t( sp_setstring(o, "key", &key, sizeof(key)) == 0 ); t( sp_delete(db, o) == 0 ); key++; } t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); t( sp_getint(env, "db.test.index.node_count") == 1 ); void *o = sp_document(db); void *cur = sp_cursor(env); while ((o = sp_get(cur, o))) { int keysize; void *keyptr = sp_getstring(o, "key", &keysize); t( *(uint32_t*)keyptr == key ); void *ko = sp_document(db); t( ko != NULL ); t( sp_setstring(ko, "key", keyptr, keysize) == 0 ); t( sp_delete(db, ko) == 0 ); key++; } sp_destroy(cur); t( key == 13000 ); t( sp_setint(env, "db.test.branch", 0) == 0 ); t( sp_setint(env, "db.test.compact", 0) == 0 ); t( sp_getint(env, "db.test.index.node_count") == 1 ); t( sp_destroy(env) == 0 ); }
#include <libsv.h> #include <libsd.h> #include <libst.h> static inline void set(void *dest, uint32_t id, uint32_t value) { void *o = st_document(id, value); t( sp_set(dest, o) == 0 ); } static inline void delete(void *dest, uint32_t id) { void *o = st_document(id, id); t( sp_delete(dest, o) == 0 ); } static inline void get(void *dest, uint32_t id, int value_to_check) { void *o = st_document(id, id); o = sp_get(dest, o); if (o == NULL) { t( value_to_check == -1 ); return; } st_document_is(o, id, value_to_check); sp_destroy(o); }