Skip to content

Commit

Permalink
entrycmp_jump_fnv1a: sort keys as numbers if possible
Browse files Browse the repository at this point in the history
Ensure that we treat numbers the way we document it.
Fixes issue #310.
  • Loading branch information
grobian committed Sep 14, 2017
1 parent a29ce6e commit 1c50590
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions consistent-hash.c
Expand Up @@ -169,6 +169,9 @@ entrycmp_jump_fnv1a(const void *l, const void *r)

char *si_l = server_instance(ch_l->server);
char *si_r = server_instance(ch_r->server);
char *endp;
long val_l;
long val_r;

/* order such that servers with explicit instance come first
* (sorted), all servers without instance follow in order of their
Expand All @@ -179,6 +182,16 @@ entrycmp_jump_fnv1a(const void *l, const void *r)
return 1;
if (si_r == NULL)
return -1;

/* at this point we have two instances, try and see if both are
* numbers, if so, compare as such, else fall back to string
* comparison */
val_l = strtol(si_l, &endp, 10);
if (*endp == '\0') {
val_r = strtol(si_r, &endp, 10);
if (*endp == '\0')
return (int)(val_l - val_r);
}
return strcmp(si_l, si_r);
}

Expand Down

0 comments on commit 1c50590

Please sign in to comment.