Merged up with build.
[m6w6/libmemcached] / example / storage.c
1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 #include "config.h"
3 #include <stdlib.h>
4 #include <inttypes.h>
5 #include <time.h>
6 #include <stdbool.h>
7 #include <string.h>
8 #include "storage.h"
9
10 struct list_entry {
11 struct item item;
12 struct list_entry *next;
13 struct list_entry *prev;
14 };
15
16 static struct list_entry *root;
17 static uint64_t cas;
18
19 bool initialize_storage(void)
20 {
21 return true;
22 }
23
24 void shutdown_storage(void)
25 {
26 /* Do nothing */
27 }
28
29 void put_item(struct item* item)
30 {
31 struct list_entry* entry= (void*)item;
32
33 update_cas(item);
34
35 if (root == NULL)
36 {
37 entry->next= entry->prev= entry;
38 }
39 else
40 {
41 entry->prev= root->prev;
42 entry->next= root;
43 entry->prev->next= entry;
44 entry->next->prev= entry;
45 }
46
47 root= entry;
48 }
49
50 struct item* get_item(const void* key, size_t nkey)
51 {
52 struct list_entry *walker= root;
53
54 if (root == NULL)
55 {
56 return NULL;
57 }
58
59 do
60 {
61 if (((struct item*)walker)->nkey == nkey &&
62 memcmp(((struct item*)walker)->key, key, nkey) == 0)
63 {
64 return (struct item*)walker;
65 }
66 walker= walker->next;
67 } while (walker != root);
68
69 return NULL;
70 }
71
72 struct item* create_item(const void* key, size_t nkey, const void* data,
73 size_t size, uint32_t flags, time_t exp)
74 {
75 struct item* ret= calloc(1, sizeof(struct list_entry));
76
77 if (ret != NULL)
78 {
79 ret->key= malloc(nkey);
80 if (size > 0)
81 {
82 ret->data= malloc(size);
83 }
84
85 if (ret->key == NULL || (size > 0 && ret->data == NULL))
86 {
87 free(ret->key);
88 free(ret->data);
89 free(ret);
90 return NULL;
91 }
92
93 memcpy(ret->key, key, nkey);
94 if (data != NULL)
95 {
96 memcpy(ret->data, data, size);
97 }
98
99 ret->nkey= nkey;
100 ret->size= size;
101 ret->flags= flags;
102 ret->exp= exp;
103 }
104
105 return ret;
106 }
107
108 bool delete_item(const void* key, size_t nkey)
109 {
110 struct item* item= get_item(key, nkey);
111 bool ret= false;
112
113 if (item)
114 {
115 /* remove from linked list */
116 struct list_entry *entry= (void*)item;
117
118 if (entry->next == entry)
119 {
120 /* Only one object in the list */
121 root= NULL;
122 }
123 else
124 {
125 /* ensure that we don't loose track of the root, and this will
126 * change the start position for the next search ;-) */
127 root= entry->next;
128 entry->prev->next= entry->next;
129 entry->next->prev= entry->prev;
130 }
131
132 free(item->key);
133 free(item->data);
134 free(item);
135 ret= true;
136 }
137
138 return ret;
139 }
140
141 void flush(uint32_t when)
142 {
143 /* FIXME */
144 (void)when;
145 /* remove the complete linked list */
146 if (root == NULL)
147 {
148 return;
149 }
150
151 root->prev->next= NULL;
152 while (root != NULL)
153 {
154 struct item* tmp= (void*)root;
155 root= root->next;
156
157 free(tmp->key);
158 free(tmp->data);
159 free(tmp);
160 }
161 }
162
163 void update_cas(struct item* item)
164 {
165 item->cas= ++cas;
166 }
167
168 void release_item(struct item* item __attribute__((unused)))
169 {
170 /* EMPTY */
171 }