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. */
| 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. */ |
| 52 | int 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 | |
| 91 | int setTypeRemove(robj *setobj, const char *value) { |
| 92 | long long llval; |
no test coverage detected