# HG changeset patch # User Tero Marttila # Date 1218144020 -10800 # Node ID 1b09dad6757e1afaf8f55e73e7eba418bdb896b3 # Parent 12d5361e747242c985af6cc12f00623886e3e98a remove unnecessary files and fix compilation of the half-completed 'done' action diff -r 12d5361e7472 -r 1b09dad6757e cache/engines/fs.h --- a/cache/engines/fs.h Fri Aug 08 00:15:29 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -#ifndef CACHE_ENGINE_FS_H -#define CACHE_ENGINE_FS_H - -#include "../engine.h" - -struct cache_engine_fs { - struct cache_engine base; - - // custom stuff - const char *cache_dir; -}; - -#endif /* CACHE_ENGINE_FS_H */ - diff -r 12d5361e7472 -r 1b09dad6757e cache/op.h --- a/cache/op.h Fri Aug 08 00:15:29 2008 +0300 +++ b/cache/op.h Fri Aug 08 00:20:20 2008 +0300 @@ -11,6 +11,8 @@ OP_STATE_HIT, OP_STATE_WRITE, + + OP_STATE_DONE, }; struct cache_op { diff -r 12d5361e7472 -r 1b09dad6757e cache/proto1/cache.c --- a/cache/proto1/cache.c Fri Aug 08 00:15:29 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - - -#include "cache.h" -#include "common.h" - -static int _cache_open_file (int *fd, const char *cache_path, const char *prefix, unsigned int id) { - char path[PATH_MAX]; - - if (snprintf(path, PATH_MAX, "%s/%s_%u", cache_path, prefix, id) >= PATH_MAX) - ERROR("path too long: %s/%s_%u", cache_path, prefix, id); - - if ((*fd = open(path, O_CREAT | O_RDWR, 0644)) == -1) - PERROR("open: %s", path); - - return 0; - -error: - *fd = -1; - - return -1; -} - -void cache_close (struct cache *ctx) { - if (ctx->index.data) - munmap(ctx->index.data, ctx->index.size); - - if (ctx->index.fd != -1) - close(ctx->index.fd); - - for (int id = 0; id < CACHE_NEST_COUNT; id++) - if (ctx->nests[id].fd != -1) - close(ctx->nests[id].fd); -} - -int cache_open (struct cache *ctx, const char *cache_path) { - // zero all state - memset(ctx, 0, sizeof(ctx)); - ctx->index.fd = -1; - for (int id = 0; id < CACHE_NEST_COUNT; id++) ctx->nests[id].fd = -1; - - // get system page size - if ((sys_pagesize = sysconf(_SC_PAGESIZE)) == -1) - PERROR("sysconf(_SC_PAGESIZE)"); - - // open the index - if (_cache_open_file(&ctx->index.fd, cache_path, "idx", 0)) - ERROR("open index"); - - // open the nests - for (int id = 0; id < CACHE_NEST_COUNT; id++) { - if (_cache_open_file(&ctx->nests[id].fd, cache_path, "nest", id)) - ERROR("open nest %d", id); - } - - // init the index - if (_cache_index_init(&ctx->index)) - goto error; - - return 0; - -error: - cache_close(ctx); - - return -1; -} - diff -r 12d5361e7472 -r 1b09dad6757e cache/proto1/cache.h --- a/cache/proto1/cache.h Fri Aug 08 00:15:29 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -#ifndef CACHE_H -#define CACHE_H - -#include - -/* - * Configuration - */ - -#define CACHE_NEST_COUNT 16 -#define CACHE_KEY_SIZE 8 - -struct cache { - struct cache_index { - int fd; - struct cache_index_file *data; - off_t size; - - } index; - - struct cache_nest { - int fd; - struct cache_nest_header *nest; - off_t size; - - struct cache_op *w_op; - - } nests[CACHE_NEST_COUNT]; - - struct cache_op *wop_queue; - struct cache_op *rop_list; -}; - -// system page size, initialized by cache_open -static long sys_pagesize; - -// cache.c -void cache_close (struct cache *ctx); -int cache_open (struct cache *ctx, const char *cache_path); - -// cache_index.c -int _cache_index_init (struct cache_index *ctx); -int _cache_index_find (struct cache_index *ctx, const u_int8_t key[CACHE_KEY_SIZE], u_int8_t *nest, u_int32_t *offset, u_int16_t *length); -int _cache_index_insert (struct cache_index *ctx, const u_int8_t key[CACHE_KEY_SIZE], const u_int8_t nest, const u_int32_t offset, const u_int16_t length); - -#endif /* CACHE_H */ diff -r 12d5361e7472 -r 1b09dad6757e cache/proto1/cache_index.c --- a/cache/proto1/cache_index.c Fri Aug 08 00:15:29 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include -#include - -#include "cache.h" -#include "common.h" - - -/* - * On-disk structs - */ - -#pragma pack(push) -#pragma pack(1) - -struct cache_index_data_node { - u_int8_t key[CACHE_KEY_SIZE]; - u_int8_t nest; - u_int32_t offset; - u_int16_t length; -}; - -struct cache_index_file { - u_int16_t entry_count; - - struct cache_index_data_node entries[]; -}; - -#pragma pack(pop) - -static int _cache_index_grow (struct cache_index *ctx) { - off_t old_size; - - // remember the old size - old_size = ctx->size; - - // calc the new size - ctx->size += sys_pagesize; - - // grow the underlying file - if (ftruncate(ctx->fd, ctx->size)) - PERROR("ftruncate"); - - // mmap/mremap - if ((ctx->data = mremap(ctx->data, old_size, ctx->size, MREMAP_MAYMOVE)) == MAP_FAILED) - PERROR("mremap"); - - return 0; - -error: - return -1; -} - -int _cache_index_init (struct cache_index *ctx) { - struct stat stat_info; - - // figure out initial index size - if (fstat(ctx->fd, &stat_info)) - PERROR("fstat"); - - // file size - ctx->size = stat_info.st_size; - - if (ctx->size == 0) { - // init it to one page - ctx->size = sys_pagesize; - - // grow the underlying file - if (ftruncate(ctx->fd, ctx->size)) - PERROR("ftruncate"); - - // the file now contains all zeros... - } - - // initial mmap - if ((ctx->data = mmap(NULL, ctx->size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ctx->fd, 0)) == MAP_FAILED) - PERROR("mmap"); - - return 0; - -error: - return -1; -} - -int _cache_index_find (struct cache_index *ctx, const u_int8_t key[CACHE_KEY_SIZE], struct cache_nest **nest, u_int32_t *offset, u_int16_t *length) { - int idx = -1; - - for (int i = 0; i < ctx->data->entry_count; i++) { - if (memcmp(ctx->data->entries[i].key, key, CACHE_KEY_SIZE) == 0) { - idx = i; - break; - } - } - - if (idx == -1) { - return -1; - - } else { - *nest = &ctx->entries[ctx->data->entries[idx].nest]; - *offset = ctx->data->entries[idx].offset; - *length = ctx->data->entries[idx].length; - - return 0; - } -} - -int _cache_index_insert (struct cache_index *ctx, const u_int8_t key[CACHE_KEY_SIZE], const u_int8_t nest, const u_int32_t offset, const u_int16_t length) { - // check that this entry will fit into the cache - if ((ctx->data->entry_count + 1) * sizeof(struct cache_index_data_node) + sizeof(struct cache_index_file) < ctx->size) { - // grow the index table - if (_cache_index_grow(ctx)) - goto error; - - } - - // the struct that we're using - struct cache_index_data_node *info = &ctx->data->entries[ctx->data->entry_count++]; - - // copy the info into the file - memcpy(info->key, key, CACHE_KEY_SIZE); - info->nest = nest; - info->offset = offset; - info->length = length; - - return 0; - -error: - return -1; - -} - diff -r 12d5361e7472 -r 1b09dad6757e cache/proto1/cache_nest.c --- a/cache/proto1/cache_nest.c Fri Aug 08 00:15:29 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -/* - * On-disk structs - */ - -#pragma pack(push) -#pragma pack(1) - -struct cache_nest_header { - u_int32_t next_offset; -}; - -int _cache_nest_assign_op (struct cache *ctx, struct cache_op *op_info, struct cache_nest **nest_info) { - for (int i = 0; i < CACHE_NEST_COUNT; i++) { - if (ctx->nests[i].op == NULL) { - // found an unused nest - ctx->nests[i].op = op_info; - *nest_info = ctx->nests[i].op; - - return 0; - } - } - - // no unused nests - return -1; -} - diff -r 12d5361e7472 -r 1b09dad6757e cache/proto1/cache_op.c --- a/cache/proto1/cache_op.c Fri Aug 08 00:15:29 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ - -#include "cache.h" -#include "common.h" - -struct cache_op { - u_int8_t key[CACHE_KEY]; - - struct cache_nest *nest; - - u_int8_t obj_nest; - u_int32_t obj_offset; - u_int16_t obj_length: - - void *mmap_addr; - - // for linked lists - struct cache_op *next; -}; - -int _cache_op_get (struct cache_op **op_info, struct cache *cache_info, u_int8_t key[CACHE_KEY_SIZE]) { - struct cache_op *op; - - // first, search the read queue - for (op = cache_info->rop_list; op; op = op->next) { - if (memcmp(op->key, key, CACHE_KEY_SIZE)) - break; - } - - if (!op) { - // look for in-progress write ops - for (int i = 0; i < CACHE_NEST_COUNT; i++) { - op = ctx->nests[i].op; - - if (op && memcmp(op->key, key, CACHE_KEY_SIZE)) - break; - } - - if (!op) { - // look for queued write ops - for (op = cache_info->wop_list; op; op = op->next) { - if (memcmp(op->key, key, CACHE_KEY_SIZE)) - break; - } - } - - if (!op) { - // alloc a new op struct - op = calloc(1, sizeof(*op)); - - // look it up in the index - if (_cache_index_find(cache_info->index, key, &op->nest, &op->obj_offset, &op->obj_length) == 0) { - // found it in some nest - - // XXX: enter read list and start read - - } else { - // not in the cache, try and find an unused nest to write it - if (_cache_nest_assign_op(cache_info, op, &op->nest) == 0) { - // excellent, we can start writing directly - - // XXX: start writing - - } else { - // enter the wait queue... - - // XXX: enter write queue - } - } - } else { - // XXX: start reading - } -} - diff -r 12d5361e7472 -r 1b09dad6757e cache/proto1/cache_req.c --- a/cache/proto1/cache_req.c Fri Aug 08 00:15:29 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ - -#include "cache.h" -#include "common.h" - -enum cache_req_mode { - REQ_GET, - REQ_PUT, -}; - -struct cache_req { - struct cache_op *op; - - off_t offset; - -}; - -int cache_req_start (struct cache_req *ctx, struct cache *cache_info, u_int8_t key[CACHE_KEY_SIZE]) { - // clear state - memset(ctx, 0, sizeof(*ctx)); - - // get the op - if (_cache_op_get(&ctx->op, cache_info, key)) - goto error; - - -} - -int cache_req_read_fd (struct cache_req *ctx, int fd) { - return cache_op_read_fd(ctx->op, fd, ctx->offset); -} - -int cache_req_write_fd (struct cache_req *ctx, int fd) { - return cache_op_write_fd(ctx->op, fd, ctx->offset); -} -