Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added param string_rev for reverse order of keys with same size #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sophia/environment/se_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int se_document_createkey(sedocument *o)

/* set prefix */
if (o->prefix) {
if (db->scheme->scheme.keys[0]->type != SS_STRING)
if (db->scheme->scheme.keys[0]->type != SS_STRING && db->scheme->scheme.keys[0]->type != SS_STRINGREV)
return sr_error(&e->error, "%s", "prefix search is only "
"supported for a string key");
void *copy = ss_malloc(&e->a, o->prefix_size);
Expand Down
9 changes: 9 additions & 0 deletions sophia/format/sf_limit.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ sf_limitapply(sflimit *b, sfscheme *s, sfv *fields, ssorder order)
v->size = b->string_min_size;
}
break;
case SS_STRINGREV:
if (order == SS_LT || order == SS_LTE) {
v->pointer = b->string_min;
v->size = b->string_min_size;
} else {
v->pointer = b->string_max;
v->size = b->string_max_size;
}
break;
default: assert(0);
break;
}
Expand Down
19 changes: 19 additions & 0 deletions sophia/format/sf_scheme.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ sf_cmpstring(char *a, int asz, char *b, int bsz, void *arg ssunused)
return rc > 0 ? 1 : -1;
}

static inline sshot int
sf_cmpstring_reverse(char *a, int asz, char *b, int bsz, void *arg ssunused)
{
int size = (asz < bsz) ? asz : bsz;
int rc = memcmp(a, b, size);
if (ssunlikely(rc == 0)) {
if (sslikely(asz == bsz))
return 0;
//TODO reverse for compare with prefix?
return (asz < bsz) ? 1 : -1;
}
return rc > 0 ? -1 : 1;
}

static inline sshot int
sf_cmpu8(char *a, int asz ssunused, char *b, int bsz ssunused, void *arg ssunused)
{
Expand Down Expand Up @@ -197,6 +211,11 @@ sf_schemeset(sfscheme *s, sffield *f, char *opt)
f->fixed_size = 0;
f->cmp = sf_cmpstring;
} else
if (strcmp(opt, "string_rev") == 0) {
f->type = SS_STRINGREV;
f->fixed_size = 0;
f->cmp = sf_cmpstring_reverse;
} else
if (strcmp(opt, "u8") == 0) {
f->type = SS_U8;
f->fixed_size = sizeof(uint8_t);
Expand Down
2 changes: 2 additions & 0 deletions sophia/std/ss_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
typedef enum {
SS_UNDEF,
SS_STRING,
SS_STRINGREV,
SS_STRINGPTR,
SS_U8,
SS_U8REV,
Expand All @@ -31,6 +32,7 @@ ss_typeof(sstype type) {
switch (type) {
case SS_UNDEF: return "undef";
case SS_STRING: return "string";
case SS_STRINGREV: return "stringrev";
case SS_STRINGPTR: return "stringptr";
case SS_U8: return "u8";
case SS_U8REV: return "u8rev";
Expand Down