Created
October 14, 2011 13:21
-
-
Save caryfitzhugh/1287079 to your computer and use it in GitHub Desktop.
Pseudo Code For Redis Weighted Sorts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (fieldlen > 0 ) { | |
if (o->type != REDIS_HASH || fieldname.len < 1) return NULL; | |
// Here is my new code inserted | |
// There is probably a better way to test for the [].. | |
if (fieldname.buf[0] == '[' && fieldname.buf[fieldname.len] == ']') { | |
// First - do I need to do something with the refCount on the o above? | |
decrRefCount(o); | |
// Then I need to somehow create an robj with REDIS_ENCODING_INT | |
sum_of_weights = ??????(REDIS_ENCODING_INT); | |
long *score = o->ptr; | |
// Set it to 0 | |
*score = 0; | |
// I already have the code to parse the '+' symbols | |
// Just pretend I now have a string for the fieldname, and a scalar value to multiply it by | |
char* ptr_in_fieldname_buf; // The name of the field in the hash | |
float scalar_for_fieldname; // The amount to scale the field value by | |
// HERE IS THE BIG QUESTION? | |
initStaticStringObject(.........); | |
field = hashTypeGet(o, field); | |
if (field.enc_type == INT) | |
*score += field.value * scalar; | |
else | |
*score += atoi(field.value) * scalar; | |
// Now I have my sum of all the field weights, so I put that back into o so it gets returned. | |
o = sum_of_weights; | |
} else { | |
// The current hash field code | |
initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(struct sdshdr))); | |
o = hashTypeGetObject(o, &fieldobj); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment