MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / asyncDelete

Method asyncDelete

src/lazyfree.cpp:144–197  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

142 * will be reclaimed in a different bio.c thread. */
143#define LAZYFREE_THRESHOLD 64
144bool redisDbPersistentData::asyncDelete(robj *key) {
145 /* If the value is composed of a few allocations, to free in a lazy way
146 * is actually just slower... So under a certain limit we just free
147 * the object synchronously. */
148
149 if (m_spstorage != nullptr)
150 return syncDelete(key); // async delte never makes sense with a storage provider
151
152 dictEntry *de = dictUnlink(m_pdict,ptrFromObj(key));
153 if (de) {
154 if (m_pdbSnapshot != nullptr && m_pdbSnapshot->find_cached_threadsafe(szFromObj(key)) != nullptr)
155 {
156 uint64_t hash = dictGetHash(m_pdict, szFromObj(key));
157 dictAdd(m_pdictTombstone, sdsdup((sds)dictGetKey(de)), (void*)hash);
158 }
159
160 robj *val = (robj*)dictGetVal(de);
161 if (val->FExpires())
162 {
163 /* Deleting an entry from the expires dict will not free the sds of
164 * the key, because it is shared with the main dictionary. */
165 removeExpire(key,dict_iter(m_pdict, de));
166 }
167
168 /* Tells the module that the key has been unlinked from the database. */
169 moduleNotifyKeyUnlink(key,val);
170
171 size_t free_effort = lazyfreeGetFreeEffort(key,val);
172
173 /* If releasing the object is too much work, do it in the background
174 * by adding the object to the lazy free list.
175 * Note that if the object is shared, to reclaim it now it is not
176 * possible. This rarely happens, however sometimes the implementation
177 * of parts of the Redis core may call incrRefCount() to protect
178 * objects, and then call dbDelete(). In this case we'll fall
179 * through and reach the dictFreeUnlinkedEntry() call, that will be
180 * equivalent to just calling decrRefCount(). */
181 if (free_effort > LAZYFREE_THRESHOLD && val->getrefcount(std::memory_order_relaxed) == 1) {
182 atomicIncr(lazyfree_objects,1);
183 bioCreateLazyFreeJob(lazyfreeFreeObject,1, val);
184 dictSetVal(m_pdict,de,NULL);
185 }
186 }
187
188 /* Release the key-val pair, or just the key if we set the val
189 * field to NULL in order to lazy free it later. */
190 if (de) {
191 dictFreeUnlinkedEntry(m_pdict,de);
192 if (g_pserver->cluster_enabled) slotToKeyDel(szFromObj(key));
193 return true;
194 } else {
195 return false;
196 }
197}
198
199int dbAsyncDelete(redisDb *db, robj *key) {
200 return db->asyncDelete(key);

Callers 1

dbAsyncDeleteFunction · 0.80

Calls 15

dictUnlinkFunction · 0.85
ptrFromObjFunction · 0.85
szFromObjFunction · 0.85
dictGetHashFunction · 0.85
sdsdupFunction · 0.85
removeExpireFunction · 0.85
dict_iterClass · 0.85
moduleNotifyKeyUnlinkFunction · 0.85
lazyfreeGetFreeEffortFunction · 0.85
bioCreateLazyFreeJobFunction · 0.85
dictFreeUnlinkedEntryFunction · 0.85
slotToKeyDelFunction · 0.85

Tested by

no test coverage detected