+/* Emit the commands needed to rebuild a set object.
+ * The function returns 0 on error, 1 on success. */
+int rewriteSetObject(rio *r, robj *key, robj *o) {
+ long long count = 0, items = setTypeSize(o);
+
+ if (o->encoding == REDIS_ENCODING_INTSET) {
+ int ii = 0;
+ int64_t llval;
+
+ while(intsetGet(o->ptr,ii++,&llval)) {
+ if (count == 0) {
+ int cmd_items = (items > REDIS_AOFREWRITE_ITEMS_PER_CMD) ?
+ REDIS_AOFREWRITE_ITEMS_PER_CMD : items;
+
+ if (rioWriteBulkCount(r,'*',2+cmd_items) == 0) return 0;
+ if (rioWriteBulkString(r,"SADD",4) == 0) return 0;
+ if (rioWriteBulkObject(r,key) == 0) return 0;
+ }
+ if (rioWriteBulkLongLong(r,llval) == 0) return 0;
+ if (++count == REDIS_AOFREWRITE_ITEMS_PER_CMD) count = 0;
+ items--;
+ }
+ } else if (o->encoding == REDIS_ENCODING_HT) {
+ dictIterator *di = dictGetIterator(o->ptr);
+ dictEntry *de;
+
+ while((de = dictNext(di)) != NULL) {
+ robj *eleobj = dictGetKey(de);
+ if (count == 0) {
+ int cmd_items = (items > REDIS_AOFREWRITE_ITEMS_PER_CMD) ?
+ REDIS_AOFREWRITE_ITEMS_PER_CMD : items;
+
+ if (rioWriteBulkCount(r,'*',2+cmd_items) == 0) return 0;
+ if (rioWriteBulkString(r,"SADD",4) == 0) return 0;
+ if (rioWriteBulkObject(r,key) == 0) return 0;
+ }
+ if (rioWriteBulkObject(r,eleobj) == 0) return 0;
+ if (++count == REDIS_AOFREWRITE_ITEMS_PER_CMD) count = 0;
+ items--;
+ }
+ dictReleaseIterator(di);
+ } else {
+ redisPanic("Unknown set encoding");
+ }
+ return 1;
+}
+