struct node *radix_match(char *prefix, struct node *p, struct node *head) { int numberOfMatchingCharacters = getNumberOfMatchingCharacters(prefix, p); if (numberOfMatchingCharacters == strlen(prefix) && numberOfMatchingCharacters == strlen(p->key)) { return p; } else if (p->is_root) { int flag = 0, i = 0; for (i = 0; i < p->child_num; i++) { if (getNumberOfMatchingCharacters(prefix, p->child[i]) != 0) flag = 1; } if (0 == flag) { return p; } } if (numberOfMatchingCharacters < strlen(prefix) && numberOfMatchingCharacters >= strlen(p->key)) { char *newText = (char*)malloc(sizeof(char)*(strlen(prefix) - numberOfMatchingCharacters + 1)); strcpy(newText, prefix + numberOfMatchingCharacters); int i; for (i = 0; i < p->child_num; i++) { if (p->child[i]->key[0] == newText[0]) { struct node *temp = (struct node*)malloc(sizeof(struct node)); temp = radix_match(newText, p->child[i], head); FREE(newText); newText = NULL; return temp; } } FREE(newText); newText = NULL; return p; } if (p->parent != NULL) return p->parent; else return head; }
void insert_value(char* key, void *value, struct node *p) { struct node *node_temp = radix_match(key, p, p); if (node_temp != NULL) { struct value_struct *q = (struct value_struct*)malloc(sizeof(struct value_struct)); q->value = value; if (0 == node_temp->value_num) node_temp->value_head = q; else { struct value_struct *value_temp = node_temp->value_head; int i = 0; for (i = 0; i < node_temp->value_num - 1; i++) value_temp = value_temp->next; value_temp->next = q; } node_temp->value_num++; } }
void radix_delete(char *key, struct node *p) { struct node *temp = radix_match(key, p, p); if (temp != NULL) visit(key, temp); }
/* FIXME */ void s_command(client c) { dstr pkey, skey; int i; dlist_t dlist; struct kvd *kvd; if (dstr_length(c->argv[0]) == 1) { add_reply_error(c, "index can't be empty\r\n"); return; } pkey = dstr_new(c->argv[0] + 1); skey = dstr_new(pkey); //------------------------------------------------------------------------ dstr pre = dstr_new(pkey); dstr rest = dstr_new(""); //------------------------------------------------------------------------ if (c->argc > 1) for (i = 1; i < c->argc; ++i) { skey = dstr_cat(skey, ","); skey = dstr_cat(skey, c->argv[i]); //------------------------------------------------------------------------ rest = dstr_cat(rest,c->argv[i]); //------------------------------------------------------------------------ } /* table_lock(cache); if ((dlist = table_get_value(cache, pkey))) { dlist_iter_t iter = dlist_iter_new(dlist, DLIST_START_HEAD); dlist_node_t node; */ /* FIXME: still has room to improve */ /* while ((node = dlist_next(iter))) { kvd = (struct kvd *)dlist_node_value(node); if (dstr_length(kvd->key) >= dstr_length(skey) && !memcmp(kvd->key, skey, dstr_length(skey))) { dstr *fields = NULL; int nfield = 0; dstr res, contracts = NULL; fields = dstr_split_len(kvd->key, dstr_length(kvd->key), ",", 1, &nfield); res = dstr_new(fields[0]); if (nfield > 1) { contracts = dstr_new(fields[1]); for (i = 2; i < nfield; ++i) { contracts = dstr_cat(contracts, ","); contracts = dstr_cat(contracts, fields[i]); } } dstr_free_tokens(fields, nfield); fields = NULL, nfield = 0; fields = dstr_split_len(kvd->u.value, dstr_length(kvd->u.value), ",", 1, &nfield); res = dstr_cat(res, ","); res = dstr_cat(res, fields[0]); if (contracts) { res = dstr_cat(res, ","); res = dstr_cat(res, contracts); } for (i = 1; i < nfield; ++i) { res = dstr_cat(res, ","); res = dstr_cat(res, fields[i]); } res = dstr_cat(res, "\r\n"); pthread_spin_lock(&c->lock); if (!(c->flags & CLIENT_CLOSE_ASAP)) { if (net_try_write(c->fd, res, dstr_length(res), 10, NET_NONBLOCK) == -1) { xcb_log(XCB_LOG_WARNING, "Writing to client '%p': %s", c, strerror(errno)); if (++c->eagcount >= 3) { client_free_async(c); pthread_spin_unlock(&c->lock); dstr_free(contracts); dstr_free(res); dstr_free_tokens(fields, nfield); table_unlock(cache); dstr_free(skey); dstr_free(pkey); return; } } else if (c->eagcount) c->eagcount = 0; } pthread_spin_unlock(&c->lock); dstr_free(contracts); dstr_free(res); dstr_free_tokens(fields, nfield); } } dlist_iter_free(&iter); } table_unlock(cache); */ //--------------------------------------------------------------------------------------------------- /* table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey)) == NULL) { if (NEW(kvd)) { kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); dlist_insert_tail(kvd->u.dlist, c); dlist = dlist_new(cmpkvd, kdfree); dlist_insert_sort(dlist, kvd); table_insert(subscribers, pkey, dlist); } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); dstr_free(pkey); } } else { if (NEW(kvd)) { dlist_node_t node; kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); if ((node = dlist_find(dlist, kvd)) == NULL) { dlist_insert_tail(kvd->u.dlist, c); dlist_insert_sort(dlist, kvd); } else { kdfree(kvd); kvd = (struct kvd *)dlist_node_value(node); if (dlist_find(kvd->u.dlist, c) == NULL) dlist_insert_tail(kvd->u.dlist, c); } } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); } dstr_free(pkey); } table_rwlock_unlock(subscribers); */ //-------------------------------------------------------------------------------------------------------------------- // table_rwlock_wrlock(subscribers_radix); radix_block_insert(pre, ",", subscribers_radix->head); radix_insert(rest, radix_match(pre, subscribers_radix->head, subscribers_radix->head)); pre = dstr_cat(pre,rest); insert_value(pre, c, subscribers_radix->head); dstr_free(pre); dstr_free(rest); // table_rwlock_unlock(subscribers_radix); //--------------------------------------------------------------------------------------------------- }