1 #ifndef ITEMS_H
2 #define ITEMS_H
3 
4 /*
5  * You should not try to aquire any of the item locks before calling these
6  * functions.
7  */
8 typedef struct _hash_item {
9     struct _hash_item *next;
10     struct _hash_item *prev;
11     struct _hash_item *h_next; /* hash chain next */
12     rel_time_t time;  /* least recent access */
13     rel_time_t exptime; /**< When the item will expire (relative to process
14                          * startup) */
15     uint32_t nbytes; /**< The total size of the data (in bytes) */
16     uint32_t flags; /**< Flags associated with the item (in network byte order)*/
17     uint16_t nkey; /**< The total length of the key (in bytes) */
18     uint16_t iflag; /**< Intermal flags. lower 8 bit is reserved for the core
19                      * server, the upper 8 bits is reserved for engine
20                      * implementation. */
21     unsigned short refcount;
22     uint8_t slabs_clsid;/* which slab class we're in */
23 } hash_item;
24 
25 typedef struct {
26     unsigned int evicted;
27     unsigned int evicted_nonzero;
28     rel_time_t evicted_time;
29     unsigned int outofmemory;
30     unsigned int tailrepairs;
31     unsigned int reclaimed;
32 } itemstats_t;
33 
34 struct items {
35    hash_item *heads[POWER_LARGEST];
36    hash_item *tails[POWER_LARGEST];
37    itemstats_t itemstats[POWER_LARGEST];
38    unsigned int sizes[POWER_LARGEST];
39 };
40 
41 
42 /**
43  * Allocate and initialize a new item structure
44  * @param engine handle to the storage engine
45  * @param key the key for the new item
46  * @param nkey the number of bytes in the key
47  * @param flags the flags in the new item
48  * @param exptime when the object should expire
49  * @param nbytes the number of bytes in the body for the item
50  * @return a pointer to an item on success NULL otherwise
51  */
52 hash_item *item_alloc(struct default_engine *engine,
53                       const void *key, size_t nkey, int flags,
54                       rel_time_t exptime, int nbytes, const void *cookie);
55 
56 /**
57  * Get an item from the cache
58  *
59  * @param engine handle to the storage engine
60  * @param key the key for the item to get
61  * @param nkey the number of bytes in the key
62  * @return pointer to the item if it exists or NULL otherwise
63  */
64 hash_item *item_get(struct default_engine *engine,
65                     const void *key, const size_t nkey);
66 
67 /**
68  * Reset the item statistics
69  * @param engine handle to the storage engine
70  */
71 void item_stats_reset(struct default_engine *engine);
72 
73 /**
74  * Get item statitistics
75  * @param engine handle to the storage engine
76  * @param add_stat callback provided by the core used to
77  *                 push statistics into the response
78  * @param cookie cookie provided by the core to identify the client
79  */
80 void item_stats(struct default_engine *engine,
81                 ADD_STAT add_stat,
82                 const void *cookie);
83 
84 /**
85  * Get detaild item statitistics
86  * @param engine handle to the storage engine
87  * @param add_stat callback provided by the core used to
88  *                 push statistics into the response
89  * @param cookie cookie provided by the core to identify the client
90  */
91 void item_stats_sizes(struct default_engine *engine,
92                       ADD_STAT add_stat, const void *cookie);
93 
94 /**
95  * Dump items from the cache
96  * @param engine handle to the storage engine
97  * @param slabs_clsid the slab class to get items from
98  * @param limit the maximum number of items to receive
99  * @param bytes the number of bytes in the return message (OUT)
100  * @return pointer to a string containint the data
101  *
102  * @todo we need to rewrite this to use callbacks!!!! currently disabled
103  */
104 char *item_cachedump(struct default_engine *engine,
105                      const unsigned int slabs_clsid,
106                      const unsigned int limit,
107                      unsigned int *bytes);
108 
109 /**
110  * Flush expired items from the cache
111  * @param engine handle to the storage engine
112  * @param when when the items should be flushed
113  */
114 void  item_flush_expired(struct default_engine *engine, time_t when);
115 
116 /**
117  * Release our reference to the current item
118  * @param engine handle to the storage engine
119  * @param it the item to release
120  */
121 void item_release(struct default_engine *engine, hash_item *it);
122 
123 /**
124  * Unlink the item from the hash table (make it inaccessible)
125  * @param engine handle to the storage engine
126  * @param it the item to unlink
127  */
128 void item_unlink(struct default_engine *engine, hash_item *it);
129 
130 /**
131  * Set the expiration time for an object
132  * @param engine handle to the storage engine
133  * @param key the key to set
134  * @param nkey the number of characters in key..
135  * @param exptime the expiration time
136  * @return The (updated) item if it exists
137  */
138 hash_item *touch_item(struct default_engine *engine,
139                       const void *key,
140                       uint16_t nkey,
141                       uint32_t exptime);
142 
143 /**
144  * Store an item in the cache
145  * @param engine handle to the storage engine
146  * @param item the item to store
147  * @param cas the cas value (OUT)
148  * @param operation what kind of store operation is this (ADD/SET etc)
149  * @return ENGINE_SUCCESS on success
150  *
151  * @todo should we refactor this into hash_item ** and remove the cas
152  *       there so that we can get it from the item instead?
153  */
154 ENGINE_ERROR_CODE store_item(struct default_engine *engine,
155                              hash_item *item,
156                              uint64_t *cas,
157                              ENGINE_STORE_OPERATION operation,
158                              const void *cookie);
159 
160 ENGINE_ERROR_CODE arithmetic(struct default_engine *engine,
161                              const void* cookie,
162                              const void* key,
163                              const int nkey,
164                              const bool increment,
165                              const bool create,
166                              const uint64_t delta,
167                              const uint64_t initial,
168                              const rel_time_t exptime,
169                              uint64_t *cas,
170                              uint64_t *result);
171 
172 
173 /**
174  * Start the item scrubber
175  * @param engine handle to the storage engine
176  */
177 bool item_start_scrub(struct default_engine *engine);
178 
179 /**
180  * The tap walker to walk the hashtables
181  */
182 tap_event_t item_tap_walker(ENGINE_HANDLE* handle,
183                             const void *cookie, item **itm,
184                             void **es, uint16_t *nes, uint8_t *ttl,
185                             uint16_t *flags, uint32_t *seqno,
186                             uint16_t *vbucket);
187 
188 bool initialize_item_tap_walker(struct default_engine *engine,
189                                 const void* cookie);
190 
191 
192 #endif
193