extern CAMLprim value kc_remove(value caml_db, value key) { CAMLparam2(caml_db, key); KCDB* db = get_db(caml_db); if (! kcdbremove(db, String_val(key), caml_string_length(key) )) { if (kcdbecode(db) != KCENOREC) { RAISE(kcdbemsg(db)); } } CAMLreturn(Val_unit); }
static nxweb_result delete_on_request( nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) { nxweb_set_response_content_type(resp, "text/plain"); nxweb_parse_request_parameters(req, 0); const char *name_space = nx_simple_map_get_nocase(req->parameters, "namespace"); char * url = malloc(strlen(req->path_info)+1); if (!url) { nxweb_send_http_error(resp, 500, "Please retry"); resp->keep_alive=0; return NXWEB_ERROR; } strcpy(url, req->path_info); url[strlen(req->path_info)] = '\0'; nxweb_url_decode(url, 0); char *fname = url, *temp; while (temp = strstr(fname, "/")) {fname = temp + 1;} char *strip_args = strstr(fname, "?"); int fname_len = strip_args?strip_args-fname:strlen(fname); char fname_with_space[1024]= {0}; if (strlen(url) > sizeof(fname_with_space)-1) { nxweb_send_http_error(resp, 414, "Request URI Too Long"); resp->keep_alive=0; free(url); return NXWEB_MISS; } if (name_space) {sprintf( fname_with_space, "%s:/", name_space);} else {strcpy(fname_with_space, ":/");} strncat(fname_with_space, fname, fname_len); kcdbremove(g_kcdb, fname_with_space, strlen( fname_with_space)); nxweb_response_printf(resp, "OK\n"); free(url); return NXWEB_OK; }
Datum kc_delete(PG_FUNCTION_ARGS) { char *map_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); char *start_time = text_to_cstring(PG_GETARG_TEXT_PP(1)); ArrayType *rids = PG_GETARG_ARRAYTYPE_P(2); int i; Datum *rid_datums; bool *rid_nulls; int rid_count; char *next_rid; KCDB *main_db; char *vbuf; size_t vsiz; int64_t num_keys_to_run; int64_t num_keys_deleted; char **keys_to_use; Cloudflare__ZoneTimeBucket *msg_new; // Open our DB. main_db = kcdbnew(); if (!open_db (main_db, map_name, start_time)) { PG_RETURN_INT64(0); } kcdbbegintran (main_db, 0); // Now run over the array. deconstruct_array(rids, TEXTOID, -1, false, 'i', &rid_datums, &rid_nulls, &rid_count); if (ARR_HASNULL(rids)) { ereport(ERROR, (errcode(ERRCODE_ARRAY_ELEMENT_ERROR), errmsg("cannot work with arrays containing NULLs"))); } keys_to_use = (char **)palloc(KC_MAX_ENTRIES_PER_RID * sizeof(char)); num_keys_deleted = 0; char prefixes_to_use[rid_count][KC_MAX_RID]; for (i = 0; i < rid_count; i++) { next_rid = TextDatumGetCString(rid_datums[i]); snprintf(prefixes_to_use[i], KC_MAX_RID, "%s%s", next_rid, CF_LABEL_SEP); num_keys_to_run = kcdbmatchprefix (main_db, prefixes_to_use[i], keys_to_use, KC_MAX_ENTRIES_PER_RID); if (num_keys_to_run != -1) { num_keys_deleted += num_keys_to_run; int next_key; for (next_key=0; next_key < num_keys_to_run; next_key++) { vbuf = kcdbget(main_db, keys_to_use[next_key], strlen(keys_to_use[next_key]), &vsiz); if (vbuf) { msg_new = cloudflare__zone_time_bucket__unpack(NULL, vsiz, (const uint8_t *)vbuf); if (msg_new == NULL) { // Something failed ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("error unpacking incoming message"))); } else { if (msg_new->kv_map_file) { unlink(msg_new->kv_map_file); } kcdbremove (main_db, keys_to_use[next_key], strlen(keys_to_use[next_key])); } cloudflare__zone_time_bucket__free_unpacked(msg_new, NULL); kcfree(vbuf); kcfree(keys_to_use[next_key]); } else { ereport(NOTICE, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("get error on %s -- %s", keys_to_use[next_key], kcecodename(kcdbecode(main_db))))); } } } else { ereport(NOTICE, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("prefix error on %s -- %s", prefixes_to_use[i], kcecodename(kcdbecode(main_db))))); } } pfree(keys_to_use); // Done! kcdbendtran (main_db, 1); if (!kcdbclose(main_db)) { ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("Error Closeing db: \"%s\"", kcecodename(kcdbecode(main_db))))); } PG_RETURN_INT64(num_keys_deleted); }
/* perform order command */ static int32_t procorder(const char* path, int64_t rnum, int32_t rnd, int32_t etc, int32_t tran, int32_t oflags) { KCDB* db; KCCUR* cur, *paracur; int32_t err; char kbuf[RECBUFSIZ], *vbuf, wbuf[RECBUFSIZ], *corepath, *copypath, *snappath; size_t ksiz, vsiz, psiz; int32_t wsiz; int64_t i, cnt; double stime, etime; VISARG visarg; oprintf("<In-order Test>\n path=%s rnum=%ld rnd=%d etc=%d tran=%d oflags=%d\n\n", path, (long)rnum, rnd, etc, tran, oflags); err = FALSE; db = kcdbnew(); oprintf("opening the database:\n"); stime = kctime(); if (!kcdbopen(db, path, KCOWRITER | KCOCREATE | KCOTRUNCATE | oflags)) { dberrprint(db, __LINE__, "kcdbopen"); err = TRUE; } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); oprintf("setting records:\n"); stime = kctime(); for (i = 1; !err && i <= rnum; i++) { if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i)); if (!kcdbset(db, kbuf, ksiz, kbuf, ksiz)) { dberrprint(db, __LINE__, "kcdbset"); err = TRUE; } if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (rnum > 250 && i % (rnum / 250) == 0) { oputchar('.'); if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i); } } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); if (etc) { oprintf("adding records:\n"); stime = kctime(); for (i = 1; !err && i <= rnum; i++) { if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i)); if (!kcdbadd(db, kbuf, ksiz, kbuf, ksiz) && kcdbecode(db) != KCEDUPREC) { dberrprint(db, __LINE__, "kcdbadd"); err = TRUE; } if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (rnum > 250 && i % (rnum / 250) == 0) { oputchar('.'); if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i); } } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); } if (etc) { oprintf("appending records:\n"); stime = kctime(); for (i = 1; !err && i <= rnum; i++) { if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i)); if (!kcdbappend(db, kbuf, ksiz, kbuf, ksiz)) { dberrprint(db, __LINE__, "kcdbadd"); err = TRUE; } if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (rnum > 250 && i % (rnum / 250) == 0) { oputchar('.'); if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i); } } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); } oprintf("getting records:\n"); stime = kctime(); for (i = 1; !err && i <= rnum; i++) { if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i)); vbuf = kcdbget(db, kbuf, ksiz, &vsiz); if (vbuf) { if (vsiz < ksiz || memcmp(vbuf, kbuf, ksiz)) { dberrprint(db, __LINE__, "kcdbget"); err = TRUE; } kcfree(vbuf); } else if (!rnd || kcdbecode(db) != KCENOREC) { dberrprint(db, __LINE__, "kcdbget"); err = TRUE; } if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (rnum > 250 && i % (rnum / 250) == 0) { oputchar('.'); if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i); } } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); if (etc) { oprintf("getting records with a buffer:\n"); stime = kctime(); for (i = 1; !err && i <= rnum; i++) { if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i)); wsiz = kcdbgetbuf(db, kbuf, ksiz, wbuf, sizeof(wbuf)); if (wsiz >= 0) { if (wsiz < (int32_t)ksiz || memcmp(wbuf, kbuf, ksiz)) { dberrprint(db, __LINE__, "kcdbgetbuf"); err = TRUE; } } else if (!rnd || kcdbecode(db) != KCENOREC) { dberrprint(db, __LINE__, "kcdbgetbuf"); err = TRUE; } if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (rnum > 250 && i % (rnum / 250) == 0) { oputchar('.'); if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i); } } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); } if (etc) { oprintf("traversing the database by the inner iterator:\n"); stime = kctime(); cnt = kcdbcount(db); visarg.rnum = rnum; visarg.rnd = rnd; visarg.cnt = 0; memset(visarg.rbuf, '+', sizeof(visarg.rbuf)); if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } if (!kcdbiterate(db, visitfull, &visarg, TRUE)) { dberrprint(db, __LINE__, "kcdbiterate"); err = TRUE; } if (rnd) oprintf(" (end)\n"); if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (visarg.cnt != cnt) { dberrprint(db, __LINE__, "kcdbiterate"); err = TRUE; } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); } if (etc) { oprintf("traversing the database by the outer cursor:\n"); stime = kctime(); cnt = kcdbcount(db); visarg.rnum = rnum; visarg.rnd = rnd; visarg.cnt = 0; if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } cur = kcdbcursor(db); if (!kccurjump(cur) && kccurecode(cur) != KCENOREC) { dberrprint(db, __LINE__, "kccurjump"); err = TRUE; } paracur = kcdbcursor(db); while (!err && kccuraccept(cur, &visitfull, &visarg, TRUE, !rnd)) { if (rnd) { ksiz = sprintf(kbuf, "%08ld", (long)myrand(rnum)); switch (myrand(3)) { case 0: { if (!kcdbremove(db, kbuf, ksiz) && kcdbecode(db) != KCENOREC) { dberrprint(db, __LINE__, "kcdbremove"); err = TRUE; } break; } case 1: { if (!kccurjumpkey(paracur, kbuf, ksiz) && kccurecode(paracur) != KCENOREC) { dberrprint(db, __LINE__, "kccurjump"); err = TRUE; } break; } default: { if (!kccurstep(cur) && kccurecode(cur) != KCENOREC) { dberrprint(db, __LINE__, "kccurstep"); err = TRUE; } break; } } } } oprintf(" (end)\n"); kccurdel(paracur); kccurdel(cur); if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (!rnd && visarg.cnt != cnt) { dberrprint(db, __LINE__, "kccuraccept"); err = TRUE; } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); } if (etc) { oprintf("synchronizing the database:\n"); stime = kctime(); if (!kcdbsync(db, FALSE, NULL, NULL)) { dberrprint(db, __LINE__, "kcdbsync"); err = TRUE; } if (!kcdboccupy(db, FALSE, NULL, NULL)) { dberrprint(db, __LINE__, "kcdboccupy"); err = TRUE; } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); } if (etc) { corepath = kcdbpath(db); psiz = strlen(corepath); if (strstr(corepath, ".kch") || strstr(corepath, ".kct")) { copypath = kcmalloc(psiz + 256); sprintf(copypath, "%s.tmp", corepath); snappath = kcmalloc(psiz + 256); sprintf(snappath, "%s.kcss", corepath); } else { copypath = kcmalloc(256); sprintf(copypath, "kclangctest.tmp"); snappath = kcmalloc(256); sprintf(snappath, "kclangctest.kcss"); } oprintf("copying the database file:\n"); stime = kctime(); if (!kcdbcopy(db, copypath)) { dberrprint(db, __LINE__, "kcdbcopy"); err = TRUE; } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); remove(copypath); oprintf("dumping records into snapshot:\n"); stime = kctime(); if (!kcdbdumpsnap(db, snappath)) { dberrprint(db, __LINE__, "kcdbdumpsnap"); err = TRUE; } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); oprintf("loading records into snapshot:\n"); stime = kctime(); cnt = kcdbcount(db); if (rnd && myrand(2) == 0 && !kcdbclear(db)) { dberrprint(db, __LINE__, "kcdbclear"); err = TRUE; } if (!kcdbloadsnap(db, snappath) || kcdbcount(db) != cnt) { dberrprint(db, __LINE__, "kcdbloadsnap"); err = TRUE; } etime = kctime(); dbmetaprint(db, FALSE); oprintf("time: %.3f\n", etime - stime); remove(snappath); kcfree(copypath); kcfree(snappath); kcfree(corepath); } oprintf("removing records:\n"); stime = kctime(); for (i = 1; !err && i <= rnum; i++) { if (tran && !kcdbbegintran(db, FALSE)) { dberrprint(db, __LINE__, "kcdbbegintran"); err = TRUE; } ksiz = sprintf(kbuf, "%08ld", (long)(rnd ? myrand(rnum) + 1 : i)); if (!kcdbremove(db, kbuf, ksiz) && ((!rnd && !etc) || kcdbecode(db) != KCENOREC)) { dberrprint(db, __LINE__, "kcdbremove"); err = TRUE; } if (tran && !kcdbendtran(db, TRUE)) { dberrprint(db, __LINE__, "kcdbendtran"); err = TRUE; } if (rnum > 250 && i % (rnum / 250) == 0) { oputchar('.'); if (i == rnum || i % (rnum / 10) == 0) oprintf(" (%08ld)\n", (long)i); } } etime = kctime(); dbmetaprint(db, TRUE); oprintf("time: %.3f\n", etime - stime); oprintf("closing the database:\n"); stime = kctime(); if (!kcdbclose(db)) { dberrprint(db, __LINE__, "kcdbclose"); err = TRUE; } etime = kctime(); oprintf("time: %.3f\n", etime - stime); kcdbdel(db); oprintf("%s\n\n", err ? "error" : "ok"); return err ? 1 : 0; }