Merge pull request #140 from hussainnaqvee/patch-1
[awesomized/libmemcached] / example / storage.cc
1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 #include "mem_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= (struct list_entry*)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= (struct item*)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= (struct list_entry*)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 /* remove the complete linked list */
144 if (root == NULL)
145 {
146 return;
147 }
148
149 root->prev->next= NULL;
150 while (root != NULL)
151 {
152 struct item* tmp= (struct item*)root;
153 root= root->next;
154
155 free(tmp->key);
156 free(tmp->data);
157 free(tmp);
158 }
159 }
160
161 void update_cas(struct item* item)
162 {
163 item->cas= ++cas;
164 }
165
166 void release_item(struct item* /* item */)
167 {
168 }