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

Function findBigKeys

src/redis-cli-cpphelper.cpp:804–937  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

802}
803
804void findBigKeys(int memkeys, unsigned memkeys_samples) {
805 unsigned long long sampled = 0, total_keys, totlen=0, *sizes=NULL, it=0;
806 redisReply *reply, *keys;
807 unsigned int arrsize=0, i;
808 dictIterator *di;
809 dictEntry *de;
810 typeinfo **types = NULL;
811 double pct;
812
813 dict *types_dict = dictCreate(&typeinfoDictType, NULL);
814 typeinfo_add(types_dict, "string", &type_string);
815 typeinfo_add(types_dict, "list", &type_list);
816 typeinfo_add(types_dict, "set", &type_set);
817 typeinfo_add(types_dict, "hash", &type_hash);
818 typeinfo_add(types_dict, "zset", &type_zset);
819 typeinfo_add(types_dict, "stream", &type_stream);
820
821 /* Total keys pre scanning */
822 total_keys = getDbSize();
823
824 /* Status message */
825 printf("\n# Scanning the entire keyspace to find biggest keys as well as\n");
826 printf("# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec\n");
827 printf("# per 100 SCAN commands (not usually needed).\n\n");
828
829 /* SCAN loop */
830 do {
831 /* Calculate approximate percentage completion */
832 pct = 100 * (double)sampled/total_keys;
833
834 /* Grab some keys and point to the keys array */
835 reply = sendScan(&it);
836 keys = reply->element[1];
837
838 /* Reallocate our type and size array if we need to */
839 if(keys->elements > arrsize) {
840 types = (typeinfo**)zrealloc(types, sizeof(typeinfo*)*keys->elements);
841 sizes = (unsigned long long*)zrealloc(sizes, sizeof(unsigned long long)*keys->elements);
842
843 if(!types || !sizes) {
844 fprintf(stderr, "Failed to allocate storage for keys!\n");
845 exit(1);
846 }
847
848 arrsize = keys->elements;
849 }
850
851 /* Retrieve types and then sizes */
852 getKeyTypes(types_dict, keys, types);
853 getKeySizes(keys, types, sizes, memkeys, memkeys_samples);
854
855 /* Now update our stats */
856 for(i=0;i<keys->elements;i++) {
857 typeinfo *type = types[i];
858 /* Skip keys that disappeared between SCAN and TYPE */
859 if(!type)
860 continue;
861

Callers 1

mainFunction · 0.85

Calls 15

typeinfo_addFunction · 0.85
getDbSizeFunction · 0.85
sendScanFunction · 0.85
zreallocFunction · 0.85
getKeyTypesFunction · 0.85
getKeySizesFunction · 0.85
sdsfreeFunction · 0.85
sdscatreprFunction · 0.85
sdsemptyFunction · 0.85
freeReplyObjectFunction · 0.85
zfreeFunction · 0.85
dictCreateFunction · 0.70

Tested by

no test coverage detected