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

Method random

src/db.cpp:3068–3089  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3066}
3067
3068dict_iter redisDbPersistentData::random()
3069{
3070 if (size() == 0)
3071 return dict_iter(nullptr);
3072 if (m_pdbSnapshot != nullptr && m_pdbSnapshot->size() > 0)
3073 {
3074 dict_iter iter(nullptr);
3075 double pctInSnapshot = (double)m_pdbSnapshot->size() / (size() + m_pdbSnapshot->size());
3076 double randval = (double)rand()/RAND_MAX;
3077 if (randval <= pctInSnapshot)
3078 {
3079 iter = m_pdbSnapshot->random_cache_threadsafe(); // BUG: RANDOM doesn't consider keys not in RAM
3080 ensure(iter.key());
3081 dictEntry *de = dictFind(m_pdict, iter.key());
3082 return dict_iter(m_pdict, de);
3083 }
3084 }
3085 dictEntry *de = dictGetRandomKey(m_pdict);
3086 if (de != nullptr)
3087 ensure((const char*)dictGetKey(de), &de);
3088 return dict_iter(m_pdict, de);
3089}
3090
3091size_t redisDbPersistentData::size(bool fCachedOnly) const
3092{

Callers 1

dbRandomKeyFunction · 0.80

Calls 6

dict_iterClass · 0.85
dictGetRandomKeyFunction · 0.85
dictFindFunction · 0.70
sizeMethod · 0.45
keyMethod · 0.45

Tested by

no test coverage detected