Пример #1
0
void		radix_from_file(t_rdx **root, char *file)
{
	int		fd;
	char	*line;
	size_t	ct;

	if ((fd = open(file, O_RDONLY)) == -1)
		return ;
	ct = 0;
	while (gnl(fd, &line))
	{
		radix_insert(root, line);
		if (line)
			free(line);
		ct++;
	}
	if (line)
	{
		ct++;
		radix_insert(root, line);
		free(line);
	}
	close(fd);
}
Пример #2
0
void radix_insert(char *key, struct node *p)
{
    int numberOfMatchingCharacters = getNumberOfMatchingCharacters(key, p);
    if (p->is_root || numberOfMatchingCharacters == 0 || (numberOfMatchingCharacters < strlen(key) && numberOfMatchingCharacters >= strlen(p->key)))
    {
        int flag = 0;
        char *newText = (char*)malloc(sizeof(char)*(strlen(key) - numberOfMatchingCharacters + 1));
        strcpy(newText, key + numberOfMatchingCharacters);
        int i;
        for (i = 0; i < p->child_num; i++)
        {
            if (p->child[i] == NULL && p->is_root)
            {
                struct node *tmp = (struct node*)malloc(sizeof(struct node));
                strcpy(tmp->key, newText);
                tmp->child_num = 0;
                tmp->is_real = 1;
                tmp->parent = p;
                tmp->child = (struct node**)malloc(sizeof(struct node));

                p->child_num++;
                p->child = realloc(p->child, sizeof(struct node));
                p->child[p->child_num - 1] = tmp;

            }
            if (p->child[i]->key[0] == newText[0])
            {
                flag = 1;
                radix_insert(newText, p->child[i]);
            }
        }

        if (0 == flag)
        {
            struct node *rn = (struct node*)malloc(sizeof(struct node));

            strcpy(rn->key, newText);
            rn->child_num = 0;
            rn->parent = p;
            rn->is_real = 1;
            rn->child = (struct node**)malloc(sizeof(struct node));

            p->child_num++;
            p->child = realloc(p->child, sizeof(struct node));

            p->child[p->child_num - 1] = rn;
        }
        FREE(newText);
        newText = NULL;
    }

    else if (numberOfMatchingCharacters == strlen(key) && numberOfMatchingCharacters == strlen(p->key))
    {
        if (1 == p->is_real)
            fprintf(stdout,"Duplicate key!\n");
        p->is_real = 1;
    }
    else if (numberOfMatchingCharacters > 0 && numberOfMatchingCharacters < strlen(p->key))
    {

        struct node *n1 = (struct node*)malloc(sizeof(struct node));
        char *newKey1 = (char*)malloc(sizeof(char)*(strlen(p->key) - numberOfMatchingCharacters + 1));
        strcpy(newKey1, p->key + numberOfMatchingCharacters);
        strcpy(n1->key, newKey1);
        FREE(newKey1);
        newKey1 = NULL;

        n1->value_head = p->value_head;
        n1->value_num = p->value_num;
        p->value_num = 0;
        p->value_head = NULL;

        n1->is_real = p->is_real;
        n1->child_num = p->child_num;
        int i;

        n1->child = (struct node**)malloc(sizeof(struct node) * n1->child_num);

        for (i = 0; i < n1->child_num; i++)
        {
            n1->child[i] = p->child[i];
            p->child[i]->parent = n1;
        }
        n1->parent = p;
        if (1 == n1->child_num && 0 == n1->is_real)
            mergeNodes(n1, n1->child[0]);
        char *newKey2 = (char*)malloc(sizeof(char)* (numberOfMatchingCharacters + 1));
        strncpy(newKey2, key, numberOfMatchingCharacters);
        newKey2[numberOfMatchingCharacters] = '\0';

        strcpy(p->key, newKey2);
        FREE(newKey2);
        newKey2 = NULL;
        p->is_real = 0;
        p->child[p->child_num] = (struct node*)malloc(sizeof(struct node));
        p->child[0] = n1;
        p->child_num = 1;

        if(numberOfMatchingCharacters < strlen(key))
        {
            struct node *n2 = (struct node*)malloc(sizeof(struct node));
            char *newKey3 = (char*)malloc(sizeof(char)*(strlen(key) - numberOfMatchingCharacters + 1));
            strcpy(newKey3, key + numberOfMatchingCharacters);

            strcpy(n2->key,newKey3);
            n2->is_real = 1;
            n2->parent = p;
            n2->child_num = 0;
            n2->child = (struct node**)malloc(sizeof(struct node));
            p->child_num++;
            p->child = realloc(p->child, sizeof(struct node));
            p->child[1] = n2;

        }
        else
        {
            p->is_real = 1;
        }

    }
    else
    {
        struct node *n = (struct node*)malloc(sizeof(struct node));

        char *newKey4 = (char*)malloc(sizeof(char)*(strlen(p->key) - numberOfMatchingCharacters + 1));
        strcpy(newKey4, p->key + numberOfMatchingCharacters);

        strcpy(n->key, newKey4);

        n->value_head = p->value_head;
        n->value_num = p->value_num;
        p->value_num = 0;
        p->value_head = NULL;

        FREE(newKey4);
        newKey4 = NULL;
        n->parent = p;
        n->child_num = p->child_num;
        int i;
        for (i = 0; i < p->child_num; i++)
        {
            if (!n->child[i])
                n->child[i] = (struct node*)malloc(sizeof(struct node));
            n->child[i] = p->child[i];
        }
        n->is_real = p->is_real;

        strcpy(p->key, key);
        p->is_real = 1;
        p->child_num++;
        p->child = realloc(p->child, sizeof(struct node));

        p->child[p->child_num] = n;
    }
}
Пример #3
0
/* 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);
//---------------------------------------------------------------------------------------------------
}