예제 #1
0
void testDictErrorHandling() {
    struct dict *d = NULL;
    int e = dict_set(d, "hallo", "1");
    assert(e == -1);

    e = dict_del(d, "hallo");
    assert(e == -1);

    e = dict_has(d, "halo");
    assert(e == 0);

    const char *item = dict_get(d, "hallo");
    assert(item == NULL);


    d = dict_create();
    dict_set(d, "hallo", "1");
    dict_set(d, "hallo2", "1");
    dict_set(d, "hallo3", "2");

    e = dict_del(d, "halo");
    assert(e == -1);

    item = dict_get(d, "halo");
    assert(item == NULL);

    dict_free(d);
}
예제 #2
0
파일: rpcd.c 프로젝트: gozfree/src
void rpcd_connect_del(struct rpcd *rpcd, int fd, char *uuid)
{
    char key[9];
    snprintf(key, sizeof(key), "%08x", fd);
    dict_del(rpcd->dict_fd2rpc, key);
    dict_del(rpcd->dict_uuid2fd, uuid);
}
예제 #3
0
void testDict() {
    struct dict *d = dict_create(NULL);
    dict_set(d, "name", "drmaa2");
    dict_set(d, "language", "c");
    dict_set(d, "version", "2");
    dict_set(d, "age", "2 weeks");

    assert(dict_has(d, "language") != 0);

    dict_del(d, "age");
    dict_del(d, "language");

    assert(dict_has(d, "version") != 0);

    dict_del(d, "name");
    dict_set(d, "version", "3");
    const char *v = dict_get(d, "version");
    assert(strcmp(v, "3") == 0);

    dict_del(d, "version");
    dict_set(d, "new_version", "4");
    assert(dict_has(d, "version") == 0);
    assert(dict_has(d, "new_version") != 0);

    dict_free(d);
}
예제 #4
0
파일: poly.c 프로젝트: ggjjlldd/swftools
char gfxpoly_check(gfxpoly_t*poly)
{
    dict_t*d = dict_new2(&point_type);
    int s,t;
    gfxpolystroke_t*stroke = poly->strokes;
    for(;stroke;stroke=stroke->next) {
	for(s=0;s<stroke->num_points;s++) {
	    point_t p = stroke->points[s];
	    int num = (s>=1 && s<stroke->num_points-1)?2:1; // mid points are two points (start+end)
	    if(!dict_contains(d, &p)) {
		dict_put(d, &p, (void*)(ptroff_t)num);
	    } else {
		int count = (ptroff_t)dict_lookup(d, &p);
		dict_del(d, &p);
		count+=num;
		dict_put(d, &p, (void*)(ptroff_t)count);
	    }
	}
    }
    DICT_ITERATE_ITEMS(d, point_t*, p, void*, c) {
        int count = (ptroff_t)c;
        if(count&1) {
            fprintf(stderr, "Point (%f,%f) occurs %d times\n", p->x*poly->gridsize, p->y*poly->gridsize, count);
            dict_destroy(d);
            return 0;
        }
    }
예제 #5
0
int     dict_cache_delete(DICT_CACHE *cp, const char *cache_key)
{
    const char *myname = "dict_cache_delete";
    int     del_res;
    DICT   *db = cp->db;

    /*
     * Delete the entry, unless we would delete the current first/next entry.
     * In that case, schedule the "current" entry for delete-behind to avoid
     * mis-behavior by some databases.
     */
    if (DC_MATCH_SAVED_CURRENT_KEY(cp, cache_key)) {
	DC_SCHEDULE_FOR_DELETE_BEHIND(cp);
	if (cp->user_flags & DICT_CACHE_FLAG_VERBOSE)
	    msg_info("%s: key=%s (current entry - schedule for delete-behind)",
		     myname, cache_key);
	DICT_ERR_VAL_RETURN(cp, DICT_ERR_NONE, DICT_STAT_SUCCESS);
    } else {
	del_res = dict_del(db, cache_key);
	if (del_res != 0)
	    msg_rate_delay(&cp->del_log_stamp, cp->log_delay, msg_warn,
		  "%s: could not delete entry for %s", cp->name, cache_key);
	if (cp->user_flags & DICT_CACHE_FLAG_VERBOSE)
	    msg_info("%s: key=%s (%s)", myname, cache_key,
		     del_res == 0 ? "found" :
		     db->error ? "error" : "not found");
	DICT_ERR_VAL_RETURN(cp, db->error, del_res);
    }
}
예제 #6
0
bool fts_del(fts_t *fts, robj *title) {
    fts_doc_t *doc = dict_del(fts->docs, title->ptr);

    if (!doc) return false;
    fts_index_del(fts, doc);
    fts_doc_free(doc);
    return true;
}
예제 #7
0
파일: exports.c 프로젝트: gluster/glusterfs
/**
 * __exp_dict_free_walk -- Delete each item in the dict
 *
 * @dict : Dict to free elements from
 * @key  : Key in the dict we are on
 * @val  : Value associated with that dict
 * @tmp  : Not used
 *
 * Passed as a function pointer to dict_foreach()
 *
 * Not for external use.
 */
static int
__exp_dict_free_walk(dict_t *dict, char *key, data_t *val, void *tmp)
{
    if (val) {
        GF_REF_PUT((struct export_item *)val->data);
        val->data = NULL;
        dict_del(dict, key);
    }
    return 0;
}
예제 #8
0
static int postmap_deletes(VSTREAM *in, char **maps, const int map_count,
			           int dict_flags)
{
    int     found = 0;
    VSTRING *keybuf = vstring_alloc(100);
    DICT  **dicts;
    const char *map_name;
    int     n;
    int     open_flags;

    /*
     * Sanity check.
     */
    if (map_count <= 0)
	msg_panic("postmap_deletes: bad map count");

    /*
     * Open maps ahead of time.
     */
    dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count);
    for (n = 0; n < map_count; n++) {
	map_name = split_at(maps[n], ':');
	if (map_name && strcmp(maps[n], DICT_TYPE_PROXY) == 0)
	    open_flags = O_RDWR | O_CREAT;	/* XXX */
	else
	    open_flags = O_RDWR;
	dicts[n] = (map_name != 0 ?
		    dict_open3(maps[n], map_name, open_flags, dict_flags) :
		  dict_open3(var_db_type, maps[n], open_flags, dict_flags));
    }

    /*
     * Perform all requests.
     */
    while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) {
	for (n = 0; n < map_count; n++) {
	    found |= (dict_del(dicts[n], STR(keybuf)) == 0);
	    if (dicts[n]->error)
		msg_fatal("table %s:%s: delete error: %m",
			  dicts[n]->type, dicts[n]->name);
	}
    }

    /*
     * Cleanup.
     */
    for (n = 0; n < map_count; n++)
	if (dicts[n])
	    dict_close(dicts[n]);
    myfree((void *) dicts);
    vstring_free(keybuf);

    return (found);
}
예제 #9
0
파일: types.c 프로젝트: 8l/awl
void awlenv_del(awlenv* e) {
    e->references--;

    if (e->references <= 0 && !e->top_level) {
        if (e->parent && e->parent->references >= 1) {
            awlenv_del(e->parent);
        }

        dict_del(e->internal_dict);
        free(e);
    }
}
예제 #10
0
/**
 * auth_cache_lookup -- Lookup an item from the cache
 *
 * @cache: cache to lookup from
 * @fh   : FH to use in lookup
 * @host_addr: Address to use in lookup
 * @timestamp: The timestamp to set when lookup succeeds
 * @can_write: Is the host authorized to write to the filehandle?
 *
 * If the current time - entry time of the cache entry > ttl_sec,
 * we remove the element from the dict and return ENTRY_EXPIRED.
 *
 * @return: ENTRY_EXPIRED if entry expired
 *          ENTRY_NOT_FOUND if entry not found in dict
 *          0 if found
 */
enum auth_cache_lookup_results
auth_cache_lookup (struct auth_cache *cache, struct nfs3_fh *fh,
                   const char *host_addr, time_t *timestamp,
                   gf_boolean_t *can_write)
{
        char                    *hashkey    = NULL;
        data_t                  *entry_data = NULL;
        struct auth_cache_entry *lookup_res = NULL;
        int                      ret        = ENTRY_NOT_FOUND;

        GF_VALIDATE_OR_GOTO (GF_NFS, cache, out);
        GF_VALIDATE_OR_GOTO (GF_NFS, cache->cache_dict, out);
        GF_VALIDATE_OR_GOTO (GF_NFS, fh, out);
        GF_VALIDATE_OR_GOTO (GF_NFS, host_addr, out);
        GF_VALIDATE_OR_GOTO (GF_NFS, timestamp, out);
        GF_VALIDATE_OR_GOTO (GF_NFS, can_write, out);

        hashkey = make_hashkey (fh, host_addr);
        if (!hashkey) {
                ret = -ENOMEM;
                goto out;
        }

        entry_data = dict_get (cache->cache_dict, hashkey);
        if (!entry_data) {
                gf_msg_debug (GF_NFS, 0, "could not find entry for %s",
                              host_addr);
                goto out;
        }

        lookup_res = (struct auth_cache_entry *)(entry_data->data);

        if ((time (NULL) - lookup_res->timestamp) > cache->ttl_sec) {
                gf_msg_debug (GF_NFS, 0, "entry for host %s has expired",
                              host_addr);
                GF_FREE (lookup_res);
                entry_data->data = NULL;
                /* Remove from the cache */
                dict_del (cache->cache_dict, hashkey);

                ret = ENTRY_EXPIRED;
                goto out;
        }

        *timestamp = lookup_res->timestamp;
        *can_write = lookup_res->item->opts->rw;

        ret = ENTRY_FOUND;
out:
        GF_FREE (hashkey);

        return ret;
}
예제 #11
0
파일: types.c 프로젝트: 8l/awl
void awlval_del(awlval* v) {
    switch (v->type) {
        case AWLVAL_INT:
            break;

        case AWLVAL_FLOAT:
            break;

        case AWLVAL_BUILTIN:
            free(v->builtin_name);
            break;

        case AWLVAL_FN:
        case AWLVAL_MACRO:
            awlenv_del(v->env);
            awlval_del(v->formals);
            awlval_del(v->body);
            break;

        case AWLVAL_ERR:
            free(v->err);
            break;

        case AWLVAL_SYM:
        case AWLVAL_QSYM:
            free(v->sym);
            break;

        case AWLVAL_STR:
            free(v->str);
            break;

        case AWLVAL_BOOL:
            break;

        case AWLVAL_DICT:
            dict_del(v->d);
            break;

        case AWLVAL_EEXPR:
        case AWLVAL_SEXPR:
        case AWLVAL_QEXPR:
        case AWLVAL_CEXPR:
            for (int i = 0; i < v->count; i++) {
                awlval_del(v->cell[i]);
            }
            free(v->cell);
            break;
    }

    free(v);
}
예제 #12
0
void testBasicDict() {
    struct dict *d = dict_create();

    dict_set(d, "name", "drmaa2");
    dict_del(d, "name");
    dict_set(d, "name", "drmaa2");
    assert(dict_has(d, "name") != 0);
    assert(dict_has(d, "language") == 0);

    const char *v = dict_get(d, "name");
    assert(strcmp(v, "drmaa2") == 0);

    dict_set(d, "language", "c");
    assert(dict_has(d, "language") != 0);

    dict_del(d, "name");
    assert(dict_has(d, "name") == 0);
    v = dict_get(d, "language");
    assert(strcmp(v, "c") == 0);

    dict_free(d);
}
예제 #13
0
void dictExample() {
	dict_t *d = (dict_t *)malloc(sizeof(dict_t));
	char *var; 
	
	
	char *key1 = strToHeap("key1");
	char *key2 = strToHeap("key2");
	char *key3 = strToHeap("key3");
	
	char *value1 = strToHeap("var1");
	char *value2 = strToHeap("var2");
	char *value3 = strToHeap("var3");
	
	dict_init(d); 
	dict_set(d, key1, value1); 
	dict_set(d, key2, value2); 
	dict_set(d, key3, value3); 
	
	dict_get(d, "key1", (void **)&var); 
	printf("key1=>%s\n", var);
	dict_get(d, "key2", (void **)&var); 
	printf("key1=>%s\n", var);
	dict_get(d, "key3", (void **)&var); 
	printf("key1=>%s\n", var);
	
	printf("dict size:%d\n", dict_size(d));
	
	
	
	if (1 == dict_del(d, key3)) {
		key3 = NULL;
		value3 = NULL;
		printf("del key3 done\n");
	}
	
	char **ks = (char **)malloc(dict_size(d)*sizeof(char*));
	int i; 
	dict_keys(d, ks); 
	for(i = 0; i < dict_size(d); i++)
		printf("%s ",*(ks+i) );
	printf("\n");
	
	char *k, *v; 
	while(dict_iter(d, &k, (void **)&v))
		printf("%s = >%s\n", k, v);
	
	dict_reset(d); 
	
	dict_destory(d); 
	free(d); 
}
예제 #14
0
파일: dict.c 프로젝트: whilke/ColdC
void func_dict_del(void) {
    cData * args;

    if (!func_init_2(&args, DICT, 0))
	return;

    if (!dict_contains(args[0].u.dict, &args[1])) {
	cthrow(keynf_id, "Key (%D) is not in the dictionary.", &args[1]);
    } else {
	anticipate_assignment();
	args[0].u.dict = dict_del(args[0].u.dict, &args[1]);
	pop(1);
    }
}
예제 #15
0
파일: ops.c 프로젝트: xupingmao/minipy
void obj_del(Object self, Object k) {
    switch(TM_TYPE(self)) {
        case TYPE_DICT:{
            dict_del(GET_DICT(self), k);
            break;
        }
        case TYPE_LIST:{
            list_del(GET_LIST(self), k);
            break;
        }
        default:
            tm_raise("obj_del: not supported type %s", tm_type(self.type));
    }
}
예제 #16
0
//handler
static int ev_handler(struct mg_connection * conn, enum mg_event ev) {
	struct kvbox * box = 0;
	char key[KEY_MAX_LEN + 1];
	char value[VALUE_MAX_LEN + 1];
	//
	switch(ev) {
		case MG_AUTH:
			return MG_TRUE;
		case MG_REQUEST:
			mg_printf_data(conn, "URI -> %s, QUERY -> %s\n", conn->uri, conn->query_string);
			if(strcmp(conn->uri, "/add.do") == 0) {
				mg_get_var(conn, "key", key, sizeof(key));
				mg_get_var(conn, "value", value, sizeof(value));
				mg_printf_data(conn, "ADD: %s -> %s, %zu\n", key, value, dbox->box_size);
				//
				pthread_mutex_lock(&(dbox->mutex));
				box = dict_add(dbox, key, value);
				pthread_mutex_unlock(&(dbox->mutex));
				//
				if(box) {
					mg_printf_data(conn, "%zu ADD OK\n", dbox->box_size);
				} else {
					mg_printf_data(conn, "%zu ADD FAIL\n", dbox->box_size);
				}
			} else if(strcmp(conn->uri, "/del.do") == 0) {
				mg_get_var(conn, "key", key, sizeof(key));
				//
				pthread_mutex_lock(&(dbox->mutex));
				dict_del(dbox, key);
				pthread_mutex_unlock(&(dbox->mutex));
				//
				mg_printf_data(conn, "DEL %zu %s\n", dbox->box_size, key);
			} else if(strcmp(conn->uri, "/seek.do") == 0) {
				mg_get_var(conn, "key", key, sizeof(key));
				//
				box = dict_seek(dbox, key);
				if(box) {
					mg_printf_data(conn, "SEEK: %s -> %s\n", key, box->value);
				} else {
					mg_printf_data(conn, "SEEK: %s ->\n", key);
				}
			}
			return MG_TRUE;
		default:
			return MG_FALSE;
	}
	//
	return 0;
}
예제 #17
0
bool fts_add(fts_t *fts, robj *title, robj *doc) {
    if (dict_contains(fts->docs, title->ptr)) fts_del(fts, title);

    fts_doc_t *fd = fts_doc_create(title, doc);
    if (!dict_set(fts->docs, title->ptr, fd)) {
        fts_doc_free(fd);
        return false;
    }

    /* update the index for this doc */
    if (!fts_index_add(fts, fd)) {
        fts_doc_free(dict_del(fts->docs, title->ptr));
        return false;
    }
    return true;
}
예제 #18
0
파일: proxymap.c 프로젝트: KKcorps/postfix
static void proxymap_delete_service(VSTREAM *client_stream)
{
    int     request_flags;
    DICT   *dict;
    int     dict_status;
    int     reply_status;

    /*
     * Process the request.
     *
     * XXX We don't close maps, so we must turn on synchronous update to ensure
     * that the on-disk data is in a consistent state between updates.
     */
    if (attr_scan(client_stream, ATTR_FLAG_STRICT,
                  ATTR_TYPE_STR, MAIL_ATTR_TABLE, request_map,
                  ATTR_TYPE_INT, MAIL_ATTR_FLAGS, &request_flags,
                  ATTR_TYPE_STR, MAIL_ATTR_KEY, request_key,
                  ATTR_TYPE_END) != 3) {
        reply_status = PROXY_STAT_BAD;
    } else if (proxy_writer == 0) {
        msg_warn("refusing %s delete request on non-%s service",
                 STR(request_map), MAIL_SERVICE_PROXYWRITE);
        reply_status = PROXY_STAT_DENY;
    } else if ((dict = proxy_map_find(STR(request_map), request_flags,
                                      &reply_status)) == 0) {
        /* void */ ;
    } else {
        dict->flags = ((dict->flags & ~DICT_FLAG_RQST_MASK)
                       | (request_flags & DICT_FLAG_RQST_MASK)
                       | DICT_FLAG_SYNC_UPDATE);
        dict_status = dict_del(dict, STR(request_key));
        if (dict_status == 0) {
            reply_status = PROXY_STAT_OK;
        } else if (dict->error == 0) {
            reply_status = PROXY_STAT_NOKEY;
        } else {
            reply_status = PROXY_STAT_RETRY;
        }
    }

    /*
     * Respond to the client.
     */
    attr_print(client_stream, ATTR_FLAG_NONE,
               ATTR_TYPE_INT, MAIL_ATTR_STATUS, reply_status,
               ATTR_TYPE_END);
}
예제 #19
0
static int
dict_key_remove_namespace(dict_t *dict, char *key, data_t *value, void *data)
{
  /*
    char buffer[3*value->len+1];
    int index = 0;
    for (index = 0; index < value->len; index++)
    sprintf(buffer+3*index, " %02x", value->data[index]);
  */
        xlator_t *this = (xlator_t *) data;
        if (strncmp(key, "user.", 5) == 0) {
                dict_set (dict, key + 5, value);
                gf_log (this->name, GF_LOG_DEBUG,
                        "remove_namespace_dict: %s -> %s ", key, key + 5);
                dict_del (dict, key);
        }
        return 0;
}
예제 #20
0
static int pop_async_cmd(struct ipc *ipc, uint32_t func_id)
{
    int ret = 0;
    char *pair = NULL;
    char cmd_str[11];//0xFFFFFFFF
    snprintf(cmd_str, sizeof(cmd_str), "0x%08X", func_id);
    pair = dict_get(ipc->async_cmd_list, cmd_str, cmd_str);
    if (pair) {
        if (-1 == dict_del(ipc->async_cmd_list, cmd_str)) {
            ret = -1;
        } else {
            ret = 0;
        }
    } else {
        ret = -1;
    }
    return ret;
}
예제 #21
0
static int postmap_delete(const char *map_type, const char *map_name,
			          const char *key, int dict_flags)
{
    DICT   *dict;
    int     status;
    int     open_flags;

    if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
	open_flags = O_RDWR | O_CREAT;		/* XXX */
    else
	open_flags = O_RDWR;
    dict = dict_open3(map_type, map_name, open_flags, dict_flags);
    status = dict_del(dict, key);
    if (dict->error)
	msg_fatal("table %s:%s: delete error: %m", dict->type, dict->name);
    dict_close(dict);
    return (status == 0);
}
예제 #22
0
파일: exports.c 프로젝트: gluster/glusterfs
/**
 * _exp_file_dict_destroy -- Delete each item in the dict
 *
 * @dict : Dict to free elements from
 * @key  : Key in the dict we are on
 * @val  : Value associated with that dict
 * @tmp  : Not used
 *
 * Not for external use.
 */
static int
_exp_file_dict_destroy(dict_t *dict, char *key, data_t *val, void *tmp)
{
    struct export_dir *dir = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, dict, out);

    if (val) {
        dir = (struct export_dir *)val->data;

        if (dir) {
            _export_dir_deinit(dir);
            val->data = NULL;
        }
        dict_del(dict, key);
    }

out:
    return 0;
}
예제 #23
0
void finish_file()
{
    if(!state || state->level!=1) {
        syntaxerror("unexpected end of file in pass %d", as3_pass);
    }
    
    if(as3_pass==2) {
        dict_del(global->file2token2info, current_filename);
        code_t*header = method_header(state->method);
        //if(global->init->method->body->code || global->init->traits) {
        if(global->init) {
            code_t*c = wrap_function(header, 0, global->init->method->body->code);
            global->init->method->body->code = abc_returnvoid(c);
            free(state->method);state->method=0;
        }
    }

    //free(state->package);state->package=0; // used in registry
    state_destroy(state);state=0;
}
예제 #24
0
파일: math.c 프로젝트: hpersh/ool2
void
math_module_fini(void)
{
  unsigned i;

  for (i = 0; i < ARRAY_SIZE(math_init_inst_method_tbl); ++i) {
    obj_t dict = CLASS(*math_init_inst_method_tbl[i].cl)->inst_methods;
    obj_t sel  = *math_init_inst_method_tbl[i].sel;
    obj_t p, q;

    if (p = dict_at(dict, sel)) {
      q = CDR(p);
      if (is_kind_of(q, consts.cl.code_method)
	  && CODE_METHOD(q)->func == math_init_inst_method_tbl[i].func
	  ) {
	dict_del(dict, sel);
      }
    }
  }
}
예제 #25
0
int     smtp_sasl_auth_cache_find(SMTP_SASL_AUTH_CACHE *auth_cache,
				          const SMTP_SESSION *session)
{
    char   *key;
    const char *entry;
    int     valid = 0;

    key = smtp_sasl_auth_cache_make_key(session->host, session->sasl_username);
    if ((entry = dict_get(auth_cache->dict, key)) != 0)
	if ((valid = smtp_sasl_auth_cache_valid_value(auth_cache, entry,
						session->sasl_passwd)) == 0)
	    /* Remove expired, password changed, or malformed cache entry. */
	    if (dict_del(auth_cache->dict, key) != 0)
		msg_warn("SASL auth failure map %s: entry not deleted: %s",
			 auth_cache->dict->name, key);
    if (auth_cache->dict->error)
	msg_warn("SASL auth failure map %s: lookup failed for %s",
		 auth_cache->dict->name, key);
    myfree(key);
    return (valid);
}
예제 #26
0
int     dict_cache_delete(DICT_CACHE *cp, const char *cache_key)
{
    const char *myname = "dict_cache_delete";
    int     zero_means_found;

    /*
     * Delete the entry, unless we would delete the current first/next entry.
     * In that case, schedule the "current" entry for delete-behind to avoid
     * mis-behavior by some databases.
     */
    if (DC_MATCH_SAVED_CURRENT_KEY(cp, cache_key)) {
	DC_SCHEDULE_FOR_DELETE_BEHIND(cp);
	if (cp->user_flags & DICT_CACHE_FLAG_VERBOSE)
	    msg_info("%s: key=%s (current entry - schedule for delete-behind)",
		     myname, cache_key);
	zero_means_found = 0;
    } else {
	zero_means_found = dict_del(cp->db, cache_key);
	if (cp->user_flags & DICT_CACHE_FLAG_VERBOSE)
	    msg_info("%s: key=%s (%s)", myname, cache_key,
		     zero_means_found == 0 ? "found" : "not found");
    }
    return (zero_means_found);
}
예제 #27
0
static void
test_dict(void) {
    DictObject *mp = dict_cnew(10000, 0, 0, 0, 0, 0, 0, 0);
    //DictObject *mp = dict_new();
    char keybuf[100];
    size_t valuebuf[] = { 1 };
    dict_add(mp, "a", valuebuf);
    dict_del(mp, "a");
    dict_add(mp, "b", valuebuf);
    dict_del(mp, "b");
    dict_add(mp, "c", valuebuf);
    dict_del(mp, "c");
    dict_add(mp, "d", valuebuf);
    dict_del(mp, "d");
    dict_add(mp, "e", valuebuf);
    dict_del(mp, "e");
    dict_add(mp, "xffff", valuebuf);
    *valuebuf = 123456789;
    dict_set(mp, "test", valuebuf);
    DictObject *copy = dict_copy(mp);
    dict_print_by_value_desc(copy);
    fprintf(stdout, "===above is copy===\n");
    dict_free(copy);
    *valuebuf = 1;
    size_t *vp;
//    while (fscanf(stdin, "%s", keybuf) == 1) {
//        vp = dict_get(mp, keybuf);
//        if (vp)
//            *vp += 1;
//        else
//            dict_add(mp, keybuf, valuebuf);
//    }
//    this is another faster version
    while (fscanf(stdin, "%s", keybuf) == 1) {
        vp = dict_fget(mp, keybuf);
        *vp += 1;
    }
    *valuebuf = 123456789;
    dict_set(mp, "the", valuebuf);
    *valuebuf = 145678999;
    dict_set(mp, "xxx", valuebuf);
    dict_del(mp, "xxx");

    DictObject *mp2 = dict_new();
    *valuebuf = 99999999;
    dict_set(mp2, "xiangnan", valuebuf);
    *valuebuf = 89999999;
    dict_add(mp2, "laoma", valuebuf);
    dict_update(mp, mp2);
    dict_free(mp2);
    //dict_print_by_value_desc(mp);
    dict_print(mp);
    void *key, *value;
    IterObject *dio = iter(mp);
    printf("\ntest iterw...\n");
    while(iterw(dio, &key)) {
        value = dict_get(mp, key);
        fprintf(stdout, "%s\t%u\n", (char*)key, *(size_t*)value);
    }
    *valuebuf = 9888888;
    dict_set(mp, "emacs", valuebuf);
    iterf(dio);
    printf("\nnow test dict_iterkv...\n");
    while(dict_iterkv(dio, &key, &value)) {
        fprintf(stdout, "%s\t%u\n", (char*)key, *(size_t*)value);
    }
    free(dio);
    dict_clear(mp);
    dict_clear(mp); //just for test
    dict_free(mp);
}
예제 #28
0
void    dict_test(int argc, char **argv)
{
    VSTRING *keybuf = vstring_alloc(1);
    VSTRING *inbuf = vstring_alloc(1);
    DICT   *dict;
    char   *dict_name;
    int     open_flags;
    char   *bufp;
    char   *cmd;
    const char *key;
    const char *value;
    int     ch;
    int     dict_flags = 0;
    int     n;
    int     rc;

#define USAGE	"verbose|del key|get key|put key=value|first|next|masks|flags"

    signal(SIGPIPE, SIG_IGN);

    msg_vstream_init(argv[0], VSTREAM_ERR);
    while ((ch = GETOPT(argc, argv, "v")) > 0) {
	switch (ch) {
	default:
	    usage(argv[0]);
	case 'v':
	    msg_verbose++;
	    break;
	}
    }
    optind = OPTIND;
    if (argc - optind < 2)
	usage(argv[0]);
    if (strcasecmp(argv[optind + 1], "create") == 0)
	open_flags = O_CREAT | O_RDWR | O_TRUNC;
    else if (strcasecmp(argv[optind + 1], "write") == 0)
	open_flags = O_RDWR;
    else if (strcasecmp(argv[optind + 1], "read") == 0)
	open_flags = O_RDONLY;
    else
	msg_fatal("unknown access mode: %s", argv[2]);
    for (n = 2; argv[optind + n]; n++)
	dict_flags |= dict_flags_mask(argv[optind + 2]);
    if ((dict_flags & DICT_FLAG_OPEN_LOCK) == 0)
	dict_flags |= DICT_FLAG_LOCK;
    if ((dict_flags & (DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_IGNORE)) == 0)
	dict_flags |= DICT_FLAG_DUP_REPLACE;
    vstream_fflush(VSTREAM_OUT);
    dict_name = argv[optind];
    dict_allow_surrogate = 1;
    dict = dict_open(dict_name, open_flags, dict_flags);
    dict_register(dict_name, dict);
    while (vstring_fgets_nonl(inbuf, VSTREAM_IN)) {
	bufp = vstring_str(inbuf);
	if (!isatty(0)) {
	    vstream_printf("> %s\n", bufp);
	    vstream_fflush(VSTREAM_OUT);
	}
	if (*bufp == '#')
	    continue;
	if ((cmd = mystrtok(&bufp, " ")) == 0) {
	    vstream_printf("usage: %s\n", USAGE);
	    vstream_fflush(VSTREAM_OUT);
	    continue;
	}
	if (dict_changed_name())
	    msg_warn("dictionary has changed");
	key = *bufp ? vstring_str(unescape(keybuf, mystrtok(&bufp, " ="))) : 0;
	value = mystrtok(&bufp, " =");
	if (strcmp(cmd, "verbose") == 0 && !key) {
	    msg_verbose++;
	} else if (strcmp(cmd, "del") == 0 && key && !value) {
	    if ((rc = dict_del(dict, key)) > 0)
		vstream_printf("%s: not found\n", key);
	    else if (rc < 0)
		vstream_printf("%s: error\n", key);
	    else
		vstream_printf("%s: deleted\n", key);
	} else if (strcmp(cmd, "get") == 0 && key && !value) {
	    if ((value = dict_get(dict, key)) == 0) {
		vstream_printf("%s: %s\n", key, dict->error ?
			       "error" : "not found");
	    } else {
		vstream_printf("%s=%s\n", key, value);
	    }
	} else if (strcmp(cmd, "put") == 0 && key && value) {
	    if (dict_put(dict, key, value) != 0)
		vstream_printf("%s: %s\n", key, dict->error ?
			       "error" : "not updated");
	    else
		vstream_printf("%s=%s\n", key, value);
	} else if (strcmp(cmd, "first") == 0 && !key && !value) {
	    if (dict_seq(dict, DICT_SEQ_FUN_FIRST, &key, &value) == 0)
		vstream_printf("%s=%s\n", key, value);
	    else
		vstream_printf("%s\n", dict->error ?
			       "error" : "not found");
	} else if (strcmp(cmd, "next") == 0 && !key && !value) {
	    if (dict_seq(dict, DICT_SEQ_FUN_NEXT, &key, &value) == 0)
		vstream_printf("%s=%s\n", key, value);
	    else
		vstream_printf("%s\n", dict->error ?
			       "error" : "not found");
	} else if (strcmp(cmd, "flags") == 0 && !key && !value) {
	    vstream_printf("dict flags %s\n",
			   dict_flags_str(dict->flags));
	} else if (strcmp(cmd, "masks") == 0 && !key && !value) {
	    vstream_printf("DICT_FLAG_IMPL_MASK %s\n",
			   dict_flags_str(DICT_FLAG_IMPL_MASK));
	    vstream_printf("DICT_FLAG_PARANOID %s\n",
			   dict_flags_str(DICT_FLAG_PARANOID));
	    vstream_printf("DICT_FLAG_RQST_MASK %s\n",
			   dict_flags_str(DICT_FLAG_RQST_MASK));
	    vstream_printf("DICT_FLAG_INST_MASK %s\n",
			   dict_flags_str(DICT_FLAG_INST_MASK));
	} else {
	    vstream_printf("usage: %s\n", USAGE);
	}
	vstream_fflush(VSTREAM_OUT);
    }
    vstring_free(keybuf);
    vstring_free(inbuf);
    dict_close(dict);
}
예제 #29
0
int     main(int argc, char **argv)
{
    VSTRING *keybuf = vstring_alloc(1);
    VSTRING *inbuf = vstring_alloc(1);
    DICT   *dict;
    char   *dict_name;
    int     open_flags;
    char   *bufp;
    char   *cmd;
    const char *key;
    const char *value;
    int     ch;
    int     dict_flags = DICT_FLAG_LOCK | DICT_FLAG_DUP_REPLACE;
    int     n;

    signal(SIGPIPE, SIG_IGN);

    msg_vstream_init(argv[0], VSTREAM_ERR);
    while ((ch = GETOPT(argc, argv, "v")) > 0) {
	switch (ch) {
	default:
	    usage(argv[0]);
	case 'v':
	    msg_verbose++;
	    break;
	}
    }
    optind = OPTIND;
    if (argc - optind < 2)
	usage(argv[0]);
    if (strcasecmp(argv[optind + 1], "create") == 0)
	open_flags = O_CREAT | O_RDWR | O_TRUNC;
    else if (strcasecmp(argv[optind + 1], "write") == 0)
	open_flags = O_RDWR;
    else if (strcasecmp(argv[optind + 1], "read") == 0)
	open_flags = O_RDONLY;
    else
	msg_fatal("unknown access mode: %s", argv[2]);
    for (n = 2; argv[optind + n]; n++) {
	if (strcasecmp(argv[optind + 2], "fold") == 0)
	    dict_flags |= DICT_FLAG_FOLD_ANY;
	else if (strcasecmp(argv[optind + 2], "sync") == 0)
	    dict_flags |= DICT_FLAG_SYNC_UPDATE;
	else
	    usage(argv[0]);
    }
    dict_name = argv[optind];
    dict = dict_open(dict_name, open_flags, dict_flags);
    dict_register(dict_name, dict);
    while (vstring_fgets_nonl(inbuf, VSTREAM_IN)) {
	bufp = vstring_str(inbuf);
	if (!isatty(0)) {
	    vstream_printf("> %s\n", bufp);
	    vstream_fflush(VSTREAM_OUT);
	}
	if (*bufp == '#')
	    continue;
	if ((cmd = mystrtok(&bufp, " ")) == 0) {
	    vstream_printf("usage: del key|get key|put key=value|first|next\n");
	    vstream_fflush(VSTREAM_OUT);
	    continue;
	}
	if (dict_changed_name())
	    msg_warn("dictionary has changed");
	key = *bufp ? vstring_str(unescape(keybuf, mystrtok(&bufp, " ="))) : 0;
	value = mystrtok(&bufp, " =");
	if (strcmp(cmd, "del") == 0 && key && !value) {
	    if (dict_del(dict, key))
		vstream_printf("%s: not found\n", key);
	    else
		vstream_printf("%s: deleted\n", key);
	} else if (strcmp(cmd, "get") == 0 && key && !value) {
	    if ((value = dict_get(dict, key)) == 0) {
		vstream_printf("%s: %s\n", key,
			       dict_errno == DICT_ERR_RETRY ?
			       "soft error" : "not found");
	    } else {
		vstream_printf("%s=%s\n", key, value);
	    }
	} else if (strcmp(cmd, "put") == 0 && key && value) {
	    dict_put(dict, key, value);
	    vstream_printf("%s=%s\n", key, value);
	} else if (strcmp(cmd, "first") == 0 && !key && !value) {
	    if (dict_seq(dict, DICT_SEQ_FUN_FIRST, &key, &value) == 0)
		vstream_printf("%s=%s\n", key, value);
	    else
		vstream_printf("%s\n",
			       dict_errno == DICT_ERR_RETRY ?
			       "soft error" : "not found");
	} else if (strcmp(cmd, "next") == 0 && !key && !value) {
	    if (dict_seq(dict, DICT_SEQ_FUN_NEXT, &key, &value) == 0)
		vstream_printf("%s=%s\n", key, value);
	    else
		vstream_printf("%s\n",
			       dict_errno == DICT_ERR_RETRY ?
			       "soft error" : "not found");
	} else {
	    vstream_printf("usage: del key|get key|put key=value|first|next\n");
	}
	vstream_fflush(VSTREAM_OUT);
    }
    vstring_free(keybuf);
    vstring_free(inbuf);
    dict_close(dict);
    return (0);
}
예제 #30
0
int     dict_cache_sequence(DICT_CACHE *cp, int first_next,
			            const char **cache_key,
			            const char **cache_val)
{
    const char *myname = "dict_cache_sequence";
    int     seq_res;
    const char *raw_cache_key;
    const char *raw_cache_val;
    char   *previous_curr_key;
    char   *previous_curr_val;
    DICT   *db = cp->db;

    /*
     * Find the first or next database entry. Hide the record with the cache
     * cleanup completion time stamp.
     */
    seq_res = dict_seq(db, first_next, &raw_cache_key, &raw_cache_val);
    if (seq_res == 0
	&& strcmp(raw_cache_key, DC_LAST_CACHE_CLEANUP_COMPLETED) == 0)
	seq_res =
	    dict_seq(db, DICT_SEQ_FUN_NEXT, &raw_cache_key, &raw_cache_val);
    if (cp->user_flags & DICT_CACHE_FLAG_VERBOSE)
	msg_info("%s: key=%s value=%s", myname,
		 seq_res == 0 ? raw_cache_key : db->error ?
		 "(error)" : "(not found)",
		 seq_res == 0 ? raw_cache_val : db->error ?
		 "(error)" : "(not found)");
    if (db->error)
	msg_rate_delay(&cp->seq_log_stamp, cp->log_delay, msg_warn,
		       "%s: sequence error", cp->name);

    /*
     * Save the current cache_key and cache_val before they are clobbered by
     * our own delete operation below. This also prevents surprises when the
     * application accesses the database after this function returns.
     * 
     * We also use the saved cache_key to protect the current entry against
     * application delete requests.
     */
    previous_curr_key = cp->saved_curr_key;
    previous_curr_val = cp->saved_curr_val;
    if (seq_res == 0) {
	cp->saved_curr_key = mystrdup(raw_cache_key);
	cp->saved_curr_val = mystrdup(raw_cache_val);
    } else {
	cp->saved_curr_key = 0;
	cp->saved_curr_val = 0;
    }

    /*
     * Delete behind.
     */
    if (db->error == 0 && DC_IS_SCHEDULED_FOR_DELETE_BEHIND(cp)) {
	DC_CANCEL_DELETE_BEHIND(cp);
	if (cp->user_flags & DICT_CACHE_FLAG_VERBOSE)
	    msg_info("%s: delete-behind key=%s value=%s",
		     myname, previous_curr_key, previous_curr_val);
	if (dict_del(db, previous_curr_key) != 0)
	    msg_rate_delay(&cp->del_log_stamp, cp->log_delay, msg_warn,
			   "%s: could not delete entry for %s",
			   cp->name, previous_curr_key);
    }

    /*
     * Clean up previous iteration key and value.
     */
    if (previous_curr_key)
	myfree(previous_curr_key);
    if (previous_curr_val)
	myfree(previous_curr_val);

    /*
     * Return the result.
     */
    *cache_key = (cp)->saved_curr_key;
    *cache_val = (cp)->saved_curr_val;
    DICT_ERR_VAL_RETURN(cp, db->error, seq_res);
}