Skip to content

Commit

Permalink
update readme and fix casts in erlang driver
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismoos committed Jun 3, 2011
1 parent 6f721cf commit 05bf040
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -22,9 +22,13 @@ limitations under the License.

**hash_ring** exposes a simple API to create/remove rings, add/remove nodes, and locate the node for a key on the ring.

A ring is created by using the *hash_ring_create()* function. There is one parameter, *numReplicas*, which determines how many times a node is placed on the ring. This parameter gives you flexibility on how consistent the ring is. Larger values will even out the node distribution on the ring. 128 is a sensible default for most users.
A ring is created by using the *hash_ring_create()* function.

hash_ring_t *ring = hash_ring_create(128);
*numReplicas*, which determines how many times a node is placed on the ring. This parameter gives you flexibility on how consistent the ring is. Larger values will even out the node distribution on the ring. 128 is a sensible default for most users.

*hash_fn* is the hash function to use. Currently *HASH_FUNCTION_MD5* and *HASH_FUNCTION_SHA1* are supported. In tests MD5 is on average about 25% faster.

hash_ring_t *ring = hash_ring_create(128, HASH_FUNCTION_SHA1);

Next you can add some nodes to the ring. For example, if you have a cluster of Redis servers, you might do the following:

Expand Down Expand Up @@ -69,7 +73,7 @@ This will produce a shared library in **build/libhashring.so**.

The following code is from the tests, which hashes some known keys and ensures that they are located on the ring properly.

hash_ring_t *ring = hash_ring_create(8);
hash_ring_t *ring = hash_ring_create(8, HASH_FUNCTION_SHA1);
char *slotA = "slotA";
char *slotB = "slotB";

Expand Down
20 changes: 10 additions & 10 deletions lib/erl/src/hash_ring_drv.c
Expand Up @@ -131,28 +131,28 @@ static void hash_ring_drv_output(ErlDrvData handle, char *buff, int bufflen)
}
}
else if(bufflen >= 9 && buff[0] == COMMAND_ADD_NODE) {
uint32_t index = readUint32(&buff[1]);
uint32_t nodeLen = readUint32(&buff[5]);
uint32_t index = readUint32((unsigned char*)&buff[1]);
uint32_t nodeLen = readUint32((unsigned char*)&buff[5]);
if((bufflen - 9) == nodeLen && d->numRings > index && d->ring_usage[index] == 1) {
hash_ring_add_node(d->rings[index], &buff[9], nodeLen);
hash_ring_add_node(d->rings[index], (unsigned char*)&buff[9], nodeLen);
res = RETURN_OK;
}
}
else if(bufflen >= 9 && buff[0] == COMMAND_REMOVE_NODE) {
uint32_t index = readUint32(&buff[1]);
uint32_t nodeLen = readUint32(&buff[5]);
uint32_t index = readUint32((unsigned char*)&buff[1]);
uint32_t nodeLen = readUint32((unsigned char*)&buff[5]);
if((bufflen - 9) == nodeLen && d->numRings > index && d->ring_usage[index] == 1) {
hash_ring_remove_node(d->rings[index], &buff[9], nodeLen);
hash_ring_remove_node(d->rings[index], (unsigned char*)&buff[9], nodeLen);
res = RETURN_OK;
}
}
else if(bufflen >= 9 && buff[0] == COMMAND_FIND_NODE) {
uint32_t index = readUint32(&buff[1]);
uint32_t keyLen = readUint32(&buff[5]);
uint32_t index = readUint32((unsigned char*)&buff[1]);
uint32_t keyLen = readUint32((unsigned char*)&buff[5]);
if((bufflen - 9) == keyLen && d->numRings > index && d->ring_usage[index] == 1) {
hash_ring_node_t *node = hash_ring_find_node(d->rings[index], &buff[9], keyLen);
hash_ring_node_t *node = hash_ring_find_node(d->rings[index], (unsigned char*)&buff[9], keyLen);
if(node != NULL) {
driver_output(d->port, node->name, node->nameLen);
driver_output(d->port, (char*)node->name, node->nameLen);
return;
}
}
Expand Down

0 comments on commit 05bf040

Please sign in to comment.