author | Tero Marttila <terom@fixme.fi> |
Sat, 30 Aug 2008 19:13:15 +0300 | |
changeset 49 | 10c7dce1a043 |
parent 37 | f0188b445c84 |
permissions | -rw-r--r-- |
31 | 1 |
#include <stdlib.h> |
2 |
#include <string.h> |
|
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 | 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 | 9 |
#include "engine.h" |
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 | 12 |
void _cache_req_free (struct cache_req *req) { |
33 | 13 |
assert(req->op == NULL); |
14 |
||
31 | 15 |
free(req->key_copy.buf); |
16 |
free(req); |
|
17 |
} |
|
18 |
||
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 | 31 |
// copy the key |
32 |
if (key->length == 0) |
|
33 |
req->key_copy.length = strlen(key->buf) + 1; |
|
34 |
else |
|
35 |
req->key_copy.length = key->length; |
|
36 |
||
37 |
if (req->key_copy.length == 0) |
|
38 |
ERROR("zero-length key"); |
|
39 |
||
40 |
if ((req->key_copy.buf = malloc(req->key_copy.length)) == NULL) |
|
41 |
ERROR("malloc"); |
|
42 |
||
43 |
memcpy(req->key_copy.buf, key->buf, req->key_copy.length); |
|
44 |
||
45 |
req->key = &req->key_copy; |
|
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 | 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 | 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 | 62 |
// engine may forget to update the op_ptr |
63 |
assert(req->op); |
|
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 | 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 | 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 | 73 |
// we are not associated with any op |
74 |
req->op = NULL; |
|
75 |
||
31 | 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 | 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 | 81 |
/*int _cache_req_notify (struct cache_req *req, enum cache_req_event event) { */ |
82 |
int _cache_req_notify (struct cache_req *req) { |
|
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 | 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 | 112 |
case OP_STATE_WRITE: |
113 |
if (_cache_req_notify(req, req->is_write ? CACHE_EVENT_BEGIN_WRITE : CACHE_EVENT_BEGIN_READ)) |
|
114 |
goto error; |
|
115 |
||
116 |
break; |
|
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 | 128 |
*/ |
129 |
||
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 | 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 | 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 | 153 |
case OP_STATE_READ: |
154 |
return CACHE_STATE_READ; |
|
155 |
||
31 | 156 |
case OP_STATE_WRITE: |
157 |
return req->is_write ? CACHE_STATE_WRITE : CACHE_STATE_READ; |
|
33 | 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 | 168 |
const struct cache_key *cache_req_key (struct cache_req *req) { |
169 |
return req->key; |
|
170 |
} |
|
171 |
||
33 | 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 | 174 |
ERROR("req is not readable"); |
175 |
||
176 |
if (cache_op_available(req->op, size, offset)) |
|
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 | 180 |
*available = (*offset - req->read_offset); |
181 |
||
182 |
return 0; |
|
183 |
||
184 |
error: |
|
185 |
return -1; |
|
186 |
} |
|
187 |
||
31 | 188 |
int cache_req_begin_write(struct cache_req *req, size_t hint) { |
189 |
if (req->op->state != OP_STATE_MISS || !req->is_write) |
|
190 |
ERROR("req not in pre-write mode"); |
|
191 |
||
192 |
return cache_op_begin_write(req->op, hint); |
|
193 |
||
194 |
error: |
|
195 |
return -1; |
|
196 |
} |
|
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 | 219 |
int cache_req_push (struct cache_req *req, int fd, size_t *size) { |
220 |
if (req->op->state != OP_STATE_WRITE || !req->is_write) |
|
221 |
ERROR("req not in write mode"); |
|
222 |
||
223 |
return cache_op_push(req->op, fd, size); |
|
224 |
||
225 |
error: |
|
226 |
return -1; |
|
227 |
} |
|
228 |
||
37 | 229 |
int cache_req_pull (struct cache_req *req, int fd, size_t *size) { |
230 |
if (req->op->state != OP_STATE_READ && req->op->state != OP_STATE_WRITE) |
|
231 |
ERROR("req is not readable"); |
|
232 |
||
233 |
return cache_op_pull(req->op, fd, &req->read_offset, size); |
|
234 |
||
235 |
error: |
|
236 |
return -1; |
|
237 |
} |
|
238 |
||
33 | 239 |
int cache_req_done (struct cache_req *req) { |
240 |
if (req->op->state != OP_STATE_WRITE || !req->is_write) |
|
241 |
ERROR("req not in write mode"); |
|
242 |
||
243 |
return cache_op_done(req->op); |
|
244 |
||
245 |
error: |
|
246 |
return -1; |
|
247 |
} |
|
248 |
||
249 |
void cache_req_release (struct cache_req *req) { |
|
250 |
// unconditional deregister |
|
251 |
cache_op_deregister(req->op, req); |
|
252 |
||
253 |
// we are not associated with any op anymore |
|
254 |
req->op = NULL; |
|
255 |
||
256 |
// free |
|
257 |
_cache_req_free(req); |
|
258 |
} |
|
259 |