Exemplo n.º 1
0
/**
 * @brief Store an entire hashmap.
 * @param d Disk to store \p map on.
 * @param key Key to store \p map under.
 * @param map Hashmap to store.
 * @return An error code.
 */
int disk_store_hm(struct disk *d, char *key, struct hashmap *map)
{
	struct hashmap_node *node;
	char *data, *msg, *query;
	struct string *s;
	struct hashmap_iterator *it;
	int rc;

	it = hashmap_new_iterator(map);
	for(node = hashmap_iterator_next(it); node;
			node = hashmap_iterator_next(it)) {
		s = container_of(node, struct string, node);
		string_get(s, &data);
		xfiredb_sprintf(&query, DISK_STORE_QUERY, key, node->key, "hashmap", data);
		rc = sqlite3_exec(d->handle, query, &dummy_hook, d, &msg);

		if(rc != SQLITE_OK)
			fprintf(stderr, "Disk store failed: %s\n", msg);

		sqlite3_free(msg);
		xfiredb_free(query);
		xfiredb_free(data);
	}

	return -XFIREDB_OK;
}
void TestHashmaplinked_IterateHandlesCollisions(
    CuTest * tc
)
{
    hashmap_t *hm, *hm2;

    hashmap_iterator_t iter;

    void *key;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);
    hm2 = hashmap_new(__uint_hash, __uint_compare, 4);

    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 91);
    hashmap_put(hm, (void *) 9, (void *) 90);

    hashmap_put(hm2, (void *) 1, (void *) 92);
    hashmap_put(hm2, (void *) 5, (void *) 91);
    hashmap_put(hm2, (void *) 9, (void *) 90);

    hashmap_iterator(hm, &iter);

    /*  remove every key we iterate on */
    while ((key = hashmap_iterator_next(hm, &iter)))
    {
        CuAssertTrue(tc, NULL != hashmap_remove(hm2, key));
    }

    /*  check if the hashmap is empty */
    CuAssertTrue(tc, 0 == hashmap_count(hm2));
    hashmap_freeall(hm);
}
/**
 * Compute the range's length if it was a milestone
 * @param df the dest file whose queue is to be scanned
 * @param tlen the length of the underlying text
 * @return true if it worked else 0
 */
static int compute_range_lengths( dest_file *df, int tlen )
{
    int res = 1;
    hashmap *map = hashmap_create();
    if ( map != NULL )
    {
        range *r = df->queue;
        while ( r != NULL )
        {
            range *s = hashmap_get( map, range_get_name(r) );
            if ( s != NULL )
                range_set_len( s, range_get_start(r)-range_get_start(s) );
            hashmap_put( map, range_get_name(r), r );
            r = range_get_next( r );
        }
        hashmap_iterator *iter = hashmap_iterator_create( map );
        if ( iter != NULL )
        {
            while ( hashmap_iterator_has_next(iter) )
            {
                char *key = hashmap_iterator_next(iter);
                range *t = hashmap_get( map, key );
                range_set_len( t, tlen-range_get_start(t) );
            }
            hashmap_iterator_dispose( iter );
        }
        else
            res = 0;
        hashmap_dispose( map );
    }
    else
        res = 0;
    return res;
}
Exemplo n.º 4
0
static void __peer_release(peer_t* p)
{
    hashmap_iterator_t iter;
    piece_t* pce;

    for (hashmap_iterator(p->have_pieces, &iter);
        (pce = hashmap_iterator_next(p->have_pieces, &iter));)
    {
        hashmap_remove(p->have_pieces, p);
    }
}
Exemplo n.º 5
0
int bt_rarestfirst_selector_poll_best_piece(
    void *r,
    const void *peer
)
{
    rarestfirst_t *rf = r;
    heap_t *hp;
    peer_t *pr;
    void *p;
    int piece_idx;
    hashmap_iterator_t iter;
    piece_t *pce;

    if (!(pr = hashmap_get(rf->peers, peer)))
    {
        return -1;
    }

    /*  if we have no pieces, fail */
    if (0 == hashmap_count(pr->have_pieces))
    {
        return -1;
    }

    hp = heap_new(__cmp_piece, rf);

    /*  add to priority queue */
    for (hashmap_iterator(rf->pieces, &iter);
        (p = hashmap_iterator_next(rf->pieces, &iter));)
    {
        /* only add if peer has it */
        if (hashmap_get(pr->have_pieces, p))
        {
            pce = hashmap_get(rf->pieces, p);
            heap_offer(hp, pce);
        }
    }

    /*  queue best from priority queue */
    if ((pce = heap_poll(hp)))
    {
        piece_idx = pce->idx;
        hashmap_remove(rf->pieces, (void *) (long) pce->idx);
        hashmap_put(rf->pieces_polled, (void *) (long) pce->idx, pce);
    }
    else
    {
        piece_idx = -1;
    }

    heap_free(hp);
    return piece_idx;
}
void TestHashmaplinked_IterateAndRemoveDoesntBreakIteration(
    CuTest * tc
)
{
    hashmap_t *hm;
    hashmap_t *hm2;
    hashmap_iterator_t iter;
    void *key;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);
    hm2 = hashmap_new(__uint_hash, __uint_compare, 11);

    hashmap_put(hm, (void *) 50, (void *) 92);
    hashmap_put(hm, (void *) 49, (void *) 91);
    hashmap_put(hm, (void *) 48, (void *) 90);
    hashmap_put(hm, (void *) 47, (void *) 89);
    hashmap_put(hm, (void *) 46, (void *) 88);
    hashmap_put(hm, (void *) 45, (void *) 87);
    /*  the following 3 collide: */
    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 91);
    hashmap_put(hm, (void *) 9, (void *) 90);

    hashmap_put(hm2, (void *) 50, (void *) 92);
    hashmap_put(hm2, (void *) 49, (void *) 91);
    hashmap_put(hm2, (void *) 48, (void *) 90);
    hashmap_put(hm2, (void *) 47, (void *) 89);
    hashmap_put(hm2, (void *) 46, (void *) 88);
    hashmap_put(hm2, (void *) 45, (void *) 87);
    /*  the following 3 collide: */
    hashmap_put(hm2, (void *) 1, (void *) 92);
    hashmap_put(hm2, (void *) 5, (void *) 91);
    hashmap_put(hm2, (void *) 9, (void *) 90);

    hashmap_iterator(hm, &iter);

    /*  remove every key we iterate on */
    while ((key = hashmap_iterator_next(hm, &iter)))
    {
        CuAssertTrue(tc, NULL != hashmap_remove(hm2, key));
        hashmap_remove(hm,key);
    }

    /*  check if the hashmap is empty */
    CuAssertTrue(tc, 0 == hashmap_count(hm2));
    hashmap_freeall(hm);
    hashmap_freeall(hm2);
}
Exemplo n.º 7
0
/**
 * @return peer that corresponds to conn_ctx, otherwise NULL */
void *bt_peermanager_conn_ctx_to_peer(void * pm, void* conn_ctx)
{
    bt_peermanager_t *me = pm;
    hashmap_iterator_t iter;

    /* find peerconn that has this conn_ctx */
    for (hashmap_iterator(me->peers,&iter);
         hashmap_iterator_has_next(me->peers,&iter);)
    {
        bt_peer_t* peer;

        peer = hashmap_iterator_next(me->peers,&iter);
        if (peer->conn_ctx == conn_ctx)
            return peer;
    }

    return NULL;
}
Exemplo n.º 8
0
void* bt_peermanager_get_peer_from_pc(void* pm, const void* pc)
{
    bt_peermanager_t *me = pm;
    hashmap_iterator_t iter;

    /* find peerconn that has this ip and port */
    for (hashmap_iterator(me->peers,&iter);
         hashmap_iterator_has_next(me->peers,&iter);)
    {
        bt_peer_t* p = hashmap_iterator_next(me->peers,&iter);

        if (p->pc == pc)
        {
            return p;
        }
    }
    return NULL;
}
Exemplo n.º 9
0
void bt_peermanager_forall(
        void* pm,
        void* caller,
        void* udata,
        void (*run)(void* caller, void* peer, void* udata))
{
    bt_peermanager_t *me = pm;
    hashmap_iterator_t iter;

    for (hashmap_iterator(me->peers,&iter);
         hashmap_iterator_has_next(me->peers,&iter);)
    {
        bt_peer_t* peer;

        peer = hashmap_iterator_next(me->peers,&iter);
        run(caller,peer,udata);
    }
}
Exemplo n.º 10
0
/**
 * @return 1 if the peer is within the manager */
int bt_peermanager_contains(void *pm, const char *ip, const int port)
{
    bt_peermanager_t *me = pm;
    hashmap_iterator_t iter;

    /* find peerconn that has this ip and port */
    for (hashmap_iterator(me->peers,&iter);
         hashmap_iterator_has_next(me->peers,&iter);)
    {
        bt_peer_t* peer;

        peer = hashmap_iterator_next(me->peers,&iter);
        if (!strcmp(peer->ip, ip) && peer->port == port)
        {
            return 1;
        }
    }
    return 0;
}
Exemplo n.º 11
0
void bt_rarestfirst_selector_free(
    void *r
)
{
    rarestfirst_t *rf = r;
    hashmap_iterator_t iter;
    peer_t* p;

    for (hashmap_iterator(rf->pieces, &iter);
        (p = hashmap_iterator_next(rf->pieces, &iter));)
    {
        hashmap_remove(rf->pieces, p);
        free(p);
    }

    hashmap_free(rf->peers);
    hashmap_free(rf->pieces);
    hashmap_free(rf->pieces_polled);
    free(rf);
}
Exemplo n.º 12
0
/**
 * Print out details of any unfreed blocks or report that all blocks were freed
 */
void memory_print()
{
    if ( allocations != NULL )
    {
        int unfreed = 0;
        hashmap_iterator *iter = hashmap_iterator_create( allocations );
        while ( hashmap_iterator_has_next(iter) )
        {
            UChar *key = hashmap_iterator_next( iter );
            UChar *value = hashmap_get( allocations, key );
            debug_report("unfreed block %s at %s \n", key, value );
            unfreed++;
        }
        if ( unfreed == 0 )
            debug_report("all blocks were freed\n");
        hashmap_dispose( allocations );
        free( strings );
        strings = NULL;
    }
}
Exemplo n.º 13
0
iterator_t *hashmap_iterator_filter(hashmap_t * map, char *keyStartsWith) 
{
    iterator_t *it = (iterator_t *)malloc(sizeof(iterator_t));
    int first_bucket_idx = 0;
    
    it->map = map;
    it->cur_bucket_idx = first_bucket_idx;
    it->current = NULL;
    it->filter = keyStartsWith;
    
    it = it_nxt(it);
    
    if(it == NULL)
        return NULL;
    
    if(!startsWith(it->filter, hashmap_iterator_key(it)))
        it = hashmap_iterator_next(it);
    
    return it;
}
Exemplo n.º 14
0
void* hashmap_value_iterator_next(id self) {
    hashmap_entry ent = (hashmap_entry)hashmap_iterator_next(self);
    return ent->value;
}
Exemplo n.º 15
0
void* hashmap_key_iterator_next(id self) {
    hashmap_entry ent = (hashmap_entry)hashmap_iterator_next(self);
    return ent->key;
}