struct peer_cache *cache_rank (const struct peer_cache *c, ranking_function rank, const struct nodeID *target, const void *target_meta) { struct peer_cache *res; int i,j,pos; res = cache_init(c->cache_size, c->metadata_size, c->max_timestamp); if (res == NULL) { return res; } for (i = 0; i < c->current_size; i++) { if (!target || !nodeid_equal(c->entries[i].id,target)) { pos = 0; for (j=0; j<res->current_size;j++) { if (((rank != NULL) && rank(target_meta, c->metadata+(c->metadata_size * i), res->metadata+(res->metadata_size * j)) == 2) || ((rank == NULL) && res->entries[j].timestamp < c->entries[i].timestamp)) { pos++; } } if (c->metadata_size) { memmove(res->metadata + (pos + 1) * res->metadata_size, res->metadata + pos * res->metadata_size, (res->current_size - pos) * res->metadata_size); memcpy(res->metadata + pos * res->metadata_size, c->metadata+(c->metadata_size * i), res->metadata_size); } for (j = res->current_size; j > pos; j--) { res->entries[j] = res->entries[j - 1]; } res->entries[pos].id = nodeid_dup(c->entries[i].id); res->entries[pos].timestamp = c->entries[i].timestamp; res->current_size++; } } return res; }
void cache_check(const struct peer_cache *c) { int i, j; for (i = 0; i < c->current_size; i++) { for (j = i + 1; j < c->current_size; j++) { assert(!nodeid_equal(c->entries[i].id, c->entries[j].id)); } } }
struct cloud_helper_context* get_cloud_helper_for(const struct nodeID *local){ struct cloud_helper_context *ctx; struct ctx_map_entry *entry; int i; ctx = NULL; for (i=0; i<fifo_queue_size(ctx_map); i++) { entry = fifo_queue_get(ctx_map, i); if (nodeid_equal(entry->node, local)) ctx = entry->cloud_ctx; } return ctx; }
int cache_pos(const struct peer_cache *c, const struct nodeID *n) { int i; for (i = 0; i < c->current_size; i++) { if (nodeid_equal(c->entries[i].id, n)) { return i; } } return -1; }
int cache_metadata_update(struct peer_cache *c, const struct nodeID *p, const void *meta, int meta_size) { int i; if (!meta_size || meta_size != c->metadata_size) { return -3; } for (i = 0; i < c->current_size; i++) { if (nodeid_equal(c->entries[i].id, p)) { memcpy(c->metadata + i * meta_size, meta, meta_size); return 1; } } return 0; }
static int cache_insert(struct peer_cache *c, struct cache_entry *e, const void *meta) { int i, position; if (c->current_size == c->cache_size) { return -2; } position = 0; for (i = 0; i < c->current_size; i++) { assert(e->id); assert(c->entries[i].id); if (c->entries[i].timestamp <= e->timestamp) { position = i + 1; } if (nodeid_equal(e->id, c->entries[i].id)) { if (c->entries[i].timestamp > e->timestamp) { assert(i >= position); nodeid_free(c->entries[i].id); c->entries[i] = *e; memcpy(c->metadata + i * c->metadata_size, meta, c->metadata_size); if (position != i) { memmove(c->entries + position + 1, c->entries + position, sizeof(struct cache_entry) * (i - position)); memmove(c->metadata + (position + 1) * c->metadata_size, c->metadata + position * c->metadata_size, (i -position) * c->metadata_size); } return position; } return -1; } } if (position != c->current_size) { memmove(c->entries + position + 1, c->entries + position, sizeof(struct cache_entry) * (c->current_size - position)); memmove(c->metadata + (position + 1) * c->metadata_size, c->metadata + position * c->metadata_size, (c->current_size - position) * c->metadata_size); } c->current_size++; c->entries[position] = *e; memcpy(c->metadata + position * c->metadata_size, meta, c->metadata_size); return position; }
int cache_add_ranked(struct peer_cache *c, struct nodeID *neighbour, const void *meta, int meta_size, ranking_function f, const void *tmeta) { int i, pos = 0; if (meta_size && meta_size != c->metadata_size) { return -3; } for (i = 0; i < c->current_size; i++) { if (nodeid_equal(c->entries[i].id, neighbour)) { if (f != NULL) { cache_del(c,neighbour); if (i == c->current_size) break; } else { cache_metadata_update(c,neighbour,meta,meta_size); return -1; } } if ((f != NULL) && f(tmeta, meta, c->metadata+(c->metadata_size * i)) == 2) { pos++; } } if (c->current_size == c->cache_size) { return -2; } if (c->metadata_size) { memmove(c->metadata + (pos + 1) * c->metadata_size, c->metadata + pos * c->metadata_size, (c->current_size - pos) * c->metadata_size); if (meta_size) { memcpy(c->metadata + pos * c->metadata_size, meta, meta_size); } else { memset(c->metadata + pos * c->metadata_size, 0, c->metadata_size); } } for (i = c->current_size; i > pos; i--) { c->entries[i] = c->entries[i - 1]; } c->entries[pos].id = nodeid_dup(neighbour); c->entries[pos].timestamp = 1; c->current_size++; return c->current_size; }
static int add_context(const struct nodeID *local, struct cloud_helper_context *ctx) { int i; struct ctx_map_entry *entry; /* Checks whether the queue is already initialized */ if (ctx_map == NULL) { ctx_map = fifo_queue_create(CLOUD_HELPER_INITAIL_INSTANCES); if (!ctx_map) { return 1; } } /* Check if the node is already present in the ctx_map */ for (i=0; i<fifo_queue_size(ctx_map); i++) { entry = fifo_queue_get(ctx_map, i); if (nodeid_equal(entry->node, local)) { return 1; } } /* Add the new entry to the ctx_map */ entry = malloc(sizeof(struct ctx_map_entry)); if (!entry) { return 1; } entry->node = local; entry->cloud_ctx = ctx; if (fifo_queue_add(ctx_map, entry) != 0) { free (entry); return 1; } return 0; }
int cache_del(struct peer_cache *c, const struct nodeID *neighbour) { int i; int found = 0; for (i = 0; i < c->current_size; i++) { if (nodeid_equal(c->entries[i].id, neighbour)) { nodeid_free(c->entries[i].id); c->current_size--; found = 1; if (c->metadata_size && (i < c->current_size)) { memmove(c->metadata + c->metadata_size * i, c->metadata + c->metadata_size * (i + 1), c->metadata_size * (c->current_size - i)); } } if (found && (i < c->current_size)) { c->entries[i] = c->entries[i + 1]; } } return c->current_size; }