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

Function setTypeAdd

src/t_set.cpp:52–89  ·  view source on GitHub ↗

Add the specified value into a set. * * If the value was already member of the set, nothing is done and 0 is * returned, otherwise the new element is added and 1 is returned. */

Source from the content-addressed store, hash-verified

50 * If the value was already member of the set, nothing is done and 0 is
51 * returned, otherwise the new element is added and 1 is returned. */
52int setTypeAdd(robj *subject, const char *value) {
53 long long llval;
54 if (subject->encoding == OBJ_ENCODING_HT) {
55 dict *ht = (dict*)subject->m_ptr;
56 dictEntry *de = dictAddRaw(ht,(char*)value,NULL);
57 if (de) {
58 dictSetKey(ht,de,sdsdup(value));
59 dictSetVal(ht,de,NULL);
60 return 1;
61 }
62 } else if (subject->encoding == OBJ_ENCODING_INTSET) {
63 if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) {
64 uint8_t success = 0;
65 subject->m_ptr = intsetAdd((intset*)subject->m_ptr,llval,&success);
66 if (success) {
67 /* Convert to regular set when the intset contains
68 * too many entries. */
69 size_t max_entries = g_pserver->set_max_intset_entries;
70 /* limit to 1G entries due to intset internals. */
71 if (max_entries >= 1<<30) max_entries = 1<<30;
72 if (intsetLen((intset*)subject->m_ptr) > max_entries)
73 setTypeConvert(subject,OBJ_ENCODING_HT);
74 return 1;
75 }
76 } else {
77 /* Failed to get integer from object, convert to regular set. */
78 setTypeConvert(subject,OBJ_ENCODING_HT);
79
80 /* The set *was* an intset and this value is not integer
81 * encodable, so dictAdd should always work. */
82 serverAssert(dictAdd((dict*)subject->m_ptr,sdsdup(value),NULL) == DICT_OK);
83 return 1;
84 }
85 } else {
86 serverPanic("Unknown set encoding");
87 }
88 return 0;
89}
90
91int setTypeRemove(robj *setobj, const char *value) {
92 long long llval;

Callers 6

setTypeDupFunction · 0.85
saddCommandFunction · 0.85
smoveCommandFunction · 0.85
spopWithCountCommandFunction · 0.85
sinterGenericCommandFunction · 0.85
sunionDiffGenericCommandFunction · 0.85

Calls 7

dictAddRawFunction · 0.85
sdsdupFunction · 0.85
intsetAddFunction · 0.85
intsetLenFunction · 0.85
setTypeConvertFunction · 0.85
dictAddFunction · 0.70

Tested by

no test coverage detected