int ht_insert_host_agent (TCADB * adb, int data_nkey, int agent_nkey) { GSLList *list, *match; int sp = 0; if (adb == NULL) return (EINVAL); if ((list = tcadbget (adb, &data_nkey, sizeof (int), &sp)) != NULL) { if ((match = list_find (list, find_host_agent_in_list, &agent_nkey))) goto out; list = list_insert_prepend (list, int2ptr (agent_nkey)); } else { list = list_create (int2ptr (agent_nkey)); } if (!tcadbput (adb, &data_nkey, sizeof (int), list, sizeof (GSLList))) LOG_DEBUG (("Unable to tcadbput\n")); out: free (list); return 0; }
int ht_insert_hit (TCADB * adb, int data_nkey, int uniq_nkey, int root_nkey) { int sp = 0; GDataMap *map; if (adb == NULL) return (EINVAL); if ((map = tcadbget (adb, &data_nkey, sizeof (int), &sp)) != NULL) { map->data++; } else { map = xcalloc (1, sizeof (GDataMap)); map->data = 1; map->root = root_nkey; map->uniq = uniq_nkey; } if (!tcadbput (adb, &data_nkey, sizeof (int), map, sizeof (GDataMap))) LOG_DEBUG (("Unable to tcadbput\n")); if (map) free (map); return 0; }
int ht_insert_uniqmap (TCADB * adb, char *uniq_key) { void *value_ptr; int nkey = 0, size = 0; if ((adb == NULL) || (uniq_key == NULL)) return (EINVAL); if ((value_ptr = tcadbget2 (adb, uniq_key)) != NULL) { free (value_ptr); return 0; } size = get_ht_size (adb); /* the auto increment value starts at SIZE (hash table) + 1 */ nkey = size > 0 ? size + 1 : 1; if (!tcadbput (adb, uniq_key, strlen (uniq_key), &nkey, sizeof (int))) LOG_DEBUG (("Unable to tcadbput\n")); free (uniq_key); return nkey; }
static gearman_return_t _libtokyocabinet_add(gearman_server_st *server, void *context, const void *unique, size_t unique_size, const void *function_name, size_t function_name_size, const void *data, size_t data_size, gearman_job_priority_t priority) { gearman_queue_libtokyocabinet_st *queue= (gearman_queue_libtokyocabinet_st *)context; bool rc; TCXSTR *key; TCXSTR *job_data; gearman_log_debug(server->gearman, "libtokyocabinet add: %.*s", (uint32_t)unique_size, (char *)unique); char key_str[GEARMAN_QUEUE_TOKYOCABINET_MAX_KEY_LEN]; size_t key_length= (size_t)snprintf(key_str, GEARMAN_QUEUE_TOKYOCABINET_MAX_KEY_LEN, "%.*s-%.*s", (int)function_name_size, (const char *)function_name, (int)unique_size, (const char *)unique); key= tcxstrnew(); tcxstrcat(key, key_str, (int)key_length); gearman_log_debug(server->gearman, "libtokyocabinet key: %.*s", (int)key_length, key_str); job_data= tcxstrnew(); tcxstrcat(job_data, (const char *)function_name, (int)function_name_size); tcxstrcat(job_data, "\0", 1); tcxstrcat(job_data, (const char *)unique, (int)unique_size); tcxstrcat(job_data, "\0", 1); switch (priority) { case GEARMAN_JOB_PRIORITY_HIGH: case GEARMAN_JOB_PRIORITY_MAX: tcxstrcat2(job_data,"0"); break; case GEARMAN_JOB_PRIORITY_LOW: tcxstrcat2(job_data,"2"); break; case GEARMAN_JOB_PRIORITY_NORMAL: default: tcxstrcat2(job_data,"1"); } tcxstrcat(job_data, (const char *)data, (int)data_size); rc= tcadbput(queue->db, tcxstrptr(key), tcxstrsize(key), tcxstrptr(job_data), tcxstrsize(job_data)); tcxstrdel(key); tcxstrdel(job_data); if (!rc) return GEARMAN_QUEUE_ERROR; return GEARMAN_SUCCESS; }
int ht_insert_str_from_int_key (TCADB * adb, int nkey, const char *value) { if (adb == NULL) return (EINVAL); tcadbput (adb, &nkey, sizeof (int), value, strlen (value)); return 0; }
int ht_insert_str_from_int_key (TCADB * adb, int nkey, const char *value) { if (adb == NULL) return (EINVAL); if (!tcadbput (adb, &nkey, sizeof (int), value, strlen (value))) LOG_DEBUG (("Unable to tcadbput\n")); return 0; }
int ht_insert_nkey_nval (TCADB * adb, int nkey, int nval) { int sp = 0; if (adb == NULL) return (EINVAL); if (tcadbget (adb, &nkey, sizeof (int), &sp) != NULL) return 1; tcadbput (adb, &nkey, sizeof (int), &nval, sizeof (int)); return 0; }
int ht_insert_nkey_nval (TCADB * adb, int nkey, int nval) { int sp = 0; if (adb == NULL) return (EINVAL); if (tcadbget (adb, &nkey, sizeof (int), &sp) != NULL) return 1; if (!tcadbput (adb, &nkey, sizeof (int), &nval, sizeof (int))) LOG_DEBUG (("Unable to tcadbput\n")); return 0; }
static void put_test(void *db, int num, int vsiz, unsigned int seed) { TCADB *adb = db; struct keygen keygen; char *value = xmalloc(vsiz); int i; keygen_init(&keygen, seed); for (i = 0; i < num; i++) { const char *key = keygen_next_key(&keygen); tcadbput(adb, key, strlen(key), value, vsiz); } free(value); }
int ht_max_u64_from_int_key (TCADB * adb, int data_nkey, uint64_t newval) { int sp = 0; void *value_ptr; uint64_t curval = 0; if (adb == NULL) return (EINVAL); if ((value_ptr = tcadbget (adb, &data_nkey, sizeof (data_nkey), &sp)) != NULL) { curval = (*(uint64_t *) value_ptr); free (value_ptr); } if (curval < newval) tcadbput (adb, &data_nkey, sizeof (data_nkey), &newval, sizeof (uint64_t)); return 0; }
static int ht_inc_u64_from_key (TCADB * adb, const void *key, size_t ksize, uint64_t inc) { int sp = 0; void *value_ptr; uint64_t add_value; if (adb == NULL) return (EINVAL); if ((value_ptr = tcadbget (adb, key, ksize, &sp)) != NULL) { add_value = (*(uint64_t *) value_ptr) + inc; free (value_ptr); } else { add_value = 0 + inc; } tcadbput (adb, key, ksize, &add_value, sizeof (uint64_t)); return 0; }
/* thread the write function */ static void *threadwrite(void *targ){ TCADB *adb = ((TARGWRITE *)targ)->adb; int rnum = ((TARGWRITE *)targ)->rnum; int id = ((TARGWRITE *)targ)->id; bool err = false; int base = id * rnum; for(int i = 1; i <= rnum; i++){ char buf[RECBUFSIZ]; int len = sprintf(buf, "%08d", base + i + 1); if(!tcadbput(adb, buf, len, buf, len)){ eprint(adb, __LINE__, "tcadbput"); err = true; break; } if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } return err ? "error" : NULL; }
static int ht_inc_int_from_key (TCADB * adb, const void *key, size_t ksize, int inc) { int sp = 0; void *value_ptr; int add_value; if (adb == NULL) return (EINVAL); if ((value_ptr = tcadbget (adb, key, ksize, &sp)) != NULL) { add_value = (*(int *) value_ptr) + inc; free (value_ptr); } else { add_value = 0 + inc; } if (!tcadbput (adb, key, ksize, &add_value, sizeof (int))) LOG_DEBUG (("Unable to tcadbput\n")); return 0; }
int ht_insert_keymap (TCADB * adb, const char *value) { void *value_ptr; int nkey = 0, size = 0, ret = 0; if ((adb == NULL) || (value == NULL)) return (EINVAL); if ((value_ptr = tcadbget2 (adb, value)) != NULL) { ret = (*(int *) value_ptr); free (value_ptr); return ret; } size = get_ht_size (adb); /* the auto increment value starts at SIZE (hash table) + 1 */ nkey = size > 0 ? size + 1 : 1; tcadbput (adb, value, strlen (value), &nkey, sizeof (int)); return nkey; }
/* perform put command */ static int procput(const char *name, const char *kbuf, int ksiz, const char *vbuf, int vsiz, int dmode){ TCADB *adb = tcadbnew(); ADBSKEL skel; if(*name == '@'){ setskeltran(&skel); if(!tcadbsetskel(adb, &skel)){ printerr(adb); skel.del(skel.opq); tcadbdel(adb); return 1; } name++; } else if(*name == '%'){ if(!tcadbsetskelmulti(adb, 8)){ printerr(adb); tcadbdel(adb); return 1; } name++; } if(!tcadbopen(adb, name)){ printerr(adb); tcadbdel(adb); return 1; } bool err = false; switch(dmode){ case -1: if(!tcadbputkeep(adb, kbuf, ksiz, vbuf, vsiz)){ printerr(adb); err = true; } break; case 1: if(!tcadbputcat(adb, kbuf, ksiz, vbuf, vsiz)){ printerr(adb); err = true; } break; case 10: if(tcadbaddint(adb, kbuf, ksiz, tcatoi(vbuf)) == INT_MIN){ printerr(adb); err = true; } break; case 11: if(isnan(tcadbadddouble(adb, kbuf, ksiz, tcatof(vbuf)))){ printerr(adb); err = true; } break; default: if(!tcadbput(adb, kbuf, ksiz, vbuf, vsiz)){ printerr(adb); err = true; } break; } if(!tcadbclose(adb)){ if(!err) printerr(adb); err = true; } tcadbdel(adb); return err ? 1 : 0; }