cache/req.c
author Tero Marttila <terom@fixme.fi>
Sat, 30 Aug 2008 19:13:15 +0300
changeset 49 10c7dce1a043
parent 37 f0188b445c84
permissions -rw-r--r--
autogenerate the memcache_test help output, and pipeline memcache requests
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     1
#include <stdlib.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     2
#include <string.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     3
#include <assert.h>
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
     4
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
     5
#include "../cache.h"
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     6
#include "cache.h"
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
     7
#include "req.h"
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
     8
#include "op.h"
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     9
#include "engine.h"
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    10
#include "../common.h"
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    11
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    12
void _cache_req_free (struct cache_req *req) {
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    13
    assert(req->op == NULL);
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    14
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    15
    free(req->key_copy.buf);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    16
    free(req);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    17
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    18
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    19
struct cache_req *cache_req (struct cache *cache, const struct cache_key *key, cache_callback cb_func, void *cb_data) {
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    20
    struct cache_req *req = NULL;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    21
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    22
    // calloc the req info
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    23
    if ((req = calloc(1, sizeof(*req))) == NULL)
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    24
        ERROR("calloc");
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    25
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    26
    // set up basic state
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    27
    req->cb_func = cb_func;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    28
    req->cb_data = cb_data;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    29
    req->is_write = 0;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    30
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    31
    // copy the key
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    32
    if (key->length == 0)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    33
        req->key_copy.length = strlen(key->buf) + 1;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    34
    else
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    35
        req->key_copy.length = key->length;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    36
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    37
    if (req->key_copy.length == 0)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    38
        ERROR("zero-length key");
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    39
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    40
    if ((req->key_copy.buf = malloc(req->key_copy.length)) == NULL)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    41
        ERROR("malloc");
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    42
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    43
    memcpy(req->key_copy.buf, key->buf, req->key_copy.length);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    44
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    45
    req->key = &req->key_copy;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    46
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    47
    // look for an existing cache_op for this key
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    48
    if ((req->op = cache_op_find(cache, req->key)) == NULL) {
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    49
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    50
        // none available, start a new cache op
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    51
        if (cache->engine->fn_op_start(cache, &req->op, req->key))
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    52
            goto error;
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
    53
        
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
    54
        // since we were the one that created it, we take care of writing it if needed
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    55
        req->is_write = 1;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    56
        
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    57
    } else {
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    58
        // we found an existing cache_op, we can just use that
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    59
        
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    60
    }
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    61
    
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    62
    // engine may forget to update the op_ptr
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    63
    assert(req->op);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    64
    
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    65
    // register
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    66
    if (cache_op_register(req->op, req))
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    67
        goto error;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    68
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    69
    // hurray, we now have an active cache_op \o/
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    70
    return req;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    71
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    72
error:
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    73
    // we are not associated with any op
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    74
    req->op = NULL;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    75
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    76
    _cache_req_free(req);
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    77
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    78
    return NULL;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    79
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    80
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    81
/*int _cache_req_notify (struct cache_req *req, enum cache_req_event event) { */
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    82
int _cache_req_notify (struct cache_req *req) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    83
    if (req->cb_func(req, req->cb_data)) {
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    84
        // XXX: handle errors
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    85
        assert(0);
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    86
    }
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    87
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    88
    return 0;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    89
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    90
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    91
int cache_req_notify (struct cache_req *req) {
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    92
/*
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    93
    switch (req->op->state) {
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    94
        case OP_STATE_INVALID:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    95
        case OP_STATE_LOOKUP:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    96
            assert(0);
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    97
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    98
            break;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    99
        
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   100
        case OP_STATE_HIT:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   101
            if (_cache_req_notify(req, CACHE_EVENT_HIT))
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   102
                goto error;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   103
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   104
            break;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   105
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   106
        case OP_STATE_MISS:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   107
            if (_cache_req_notify(req, CACHE_EVENT_MISS))
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   108
                goto error;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   109
            
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   110
            break;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   111
        
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   112
        case OP_STATE_WRITE:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   113
            if (_cache_req_notify(req, req->is_write ? CACHE_EVENT_BEGIN_WRITE : CACHE_EVENT_BEGIN_READ))
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   114
                goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   115
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   116
            break;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   117
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   118
        default:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   119
            assert(0);
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   120
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   121
            break;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   122
    }
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   123
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   124
    return 0;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   125
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   126
error:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   127
    return -1;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   128
*/
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   129
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   130
    return _cache_req_notify(req);
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   131
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   132
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   133
enum cache_req_state cache_req_state (struct cache_req *req) {
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   134
     switch (req->op->state) {
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   135
        case OP_STATE_INVALID:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   136
            return CACHE_STATE_INVALID;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   137
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   138
        case OP_STATE_LOOKUP:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   139
            return CACHE_STATE_LOOKUP;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   140
        
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   141
        case OP_STATE_HIT:
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   142
            return CACHE_STATE_HIT;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   143
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   144
        case OP_STATE_MISS:
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   145
            return req->is_write ? CACHE_STATE_MISS : CACHE_STATE_OPEN_READ;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   146
        
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   147
        case OP_STATE_OPEN_READ:
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   148
            return CACHE_STATE_OPEN_READ;
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   149
        
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   150
        case OP_STATE_OPEN_WRITE:
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   151
            return req->is_write ? CACHE_STATE_OPEN_WRITE : CACHE_STATE_READ;
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   152
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   153
        case OP_STATE_READ:
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   154
            return CACHE_STATE_READ;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   155
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   156
        case OP_STATE_WRITE:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   157
            return req->is_write ? CACHE_STATE_WRITE : CACHE_STATE_READ;
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   158
        
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   159
        case OP_STATE_ERROR:
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   160
            // XXX: error handling
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   161
            assert(0);
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   162
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   163
        default:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   164
            assert(0);
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   165
    }
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   166
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   167
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   168
const struct cache_key *cache_req_key (struct cache_req *req) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   169
    return req->key;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   170
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   171
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   172
int cache_req_available (struct cache_req *req, size_t *size, size_t *offset, size_t *available) {
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   173
    if (req->op->state == OP_STATE_INVALID || req->op->state == OP_STATE_LOOKUP || req->op->state == OP_STATE_ERROR)
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   174
        ERROR("req is not readable");
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   175
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   176
    if (cache_op_available(req->op, size, offset))
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   177
        goto error;
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   178
    
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   179
    // this is based on our own read offset
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   180
    *available = (*offset - req->read_offset);
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   181
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   182
    return 0;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   183
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   184
error:
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   185
    return -1;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   186
}
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   187
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   188
int cache_req_begin_write(struct cache_req *req, size_t hint) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   189
    if (req->op->state != OP_STATE_MISS || !req->is_write)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   190
        ERROR("req not in pre-write mode");
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   191
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   192
    return cache_op_begin_write(req->op, hint);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   193
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   194
error:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   195
    return -1;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   196
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   197
36
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   198
int cache_req_begin_read (struct cache_req *req) {
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   199
    if (req->op->state == OP_STATE_HIT) {
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   200
        // needs to be done
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   201
        return cache_op_begin_read(req->op);
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   202
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   203
    } else if (req->op->state == OP_STATE_OPEN_READ || req->op->state == OP_STATE_READ) {
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   204
        WARNING("req is already in OP_STATE_READ/OP_STATE_OPEN_READ");
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   205
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   206
    } else if (req->op->state == OP_STATE_OPEN_WRITE || req->op->state == OP_STATE_WRITE) {
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   207
        WARNING("req is already in OP_STATE_WRITE/OP_STATE_OPEN_WRITE");
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   208
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   209
    } else {
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   210
        ERROR("req is not readable");
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   211
    }
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   212
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   213
    return 0;
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   214
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   215
error:
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   216
    return -1;
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   217
}
b4023990811e rename/clean up states slightly and add lots of documentation
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
   218
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   219
int cache_req_push (struct cache_req *req, int fd, size_t *size) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   220
    if (req->op->state != OP_STATE_WRITE || !req->is_write)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   221
        ERROR("req not in write mode");
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   222
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   223
    return cache_op_push(req->op, fd, size); 
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   224
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   225
error:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   226
    return -1;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   227
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   228
37
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   229
int cache_req_pull (struct cache_req *req, int fd, size_t *size) {
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   230
    if (req->op->state != OP_STATE_READ && req->op->state != OP_STATE_WRITE)
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   231
        ERROR("req is not readable");
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   232
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   233
    return cache_op_pull(req->op, fd, &req->read_offset, size);
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   234
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   235
error:
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   236
    return -1;
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   237
}
f0188b445c84 read/pull support
Tero Marttila <terom@fixme.fi>
parents: 36
diff changeset
   238
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   239
int cache_req_done (struct cache_req *req) {
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   240
    if (req->op->state != OP_STATE_WRITE || !req->is_write)
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   241
        ERROR("req not in write mode");
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   242
    
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   243
    return cache_op_done(req->op); 
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   244
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   245
error:
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   246
    return -1;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   247
}
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   248
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   249
void cache_req_release (struct cache_req *req) {
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   250
    // unconditional deregister
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   251
    cache_op_deregister(req->op, req);
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   252
    
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   253
    // we are not associated with any op anymore
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   254
    req->op = NULL;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   255
    
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   256
    // free
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   257
    _cache_req_free(req);
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   258
}
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   259