This is related to consistent hashing and while I conceptually understand what I need to do, I'm having a hard time translating this into code.
I'm trying to divide a given keyspace (say, 128 bits) into equal sized partitions. I want the upper bound (highest key) of each partition.
Basically, how would I complete this?
#define KEYSPACE_BYTE_SIZE 16
#define KEYSPACE_BIT_SIZE (KEYSPACE_BYTE_SIZE * 8)
typedef struct _key
{
char byte[KEYSPACE_BYTE_SIZE];
} key;
key * partition_keyspace( int num_partitions )
{
key * partitions = malloc( sizeof(key) * num_partitions );
// ...
}
Edit:
I suppose another way of saying this is:
for (i = 0; i < num_partitions; i++)
{
partitions[i] = ((2 ^ KEYSPACE_BIT_SIZE) / num_partitions) * i;
}
Of course the problem is 2 ^ 128 is a very large number and can't be contained in any single integer variable in C with which to do the math (hence the char[16] struct).
I really don't want to use a large number library (or any library) for this.
Edit:
Although, in actuality the numbers I'm looking for is:
for (i = 0; i < num_partitions; i++)
{
partitions[i] = (((2 ^ KEYSPACE_BIT_SIZE) / num_partitions) * (i + 1)) - 1;
}