Skip to content

Commit

Permalink
Implemented a bucket/paritition distribution function in Request clas…
Browse files Browse the repository at this point in the history
…s that hashes the underlying key id over a provided modulus. Currently uses the SHA1 algorithm.
  • Loading branch information
danielrichardrushton committed Feb 12, 2018
1 parent 31e67c0 commit 5493754
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ CXXFLAGS := -std=c++14 -Wall -g -pedantic-errors -Werror -O3 \
# have the library and headers installed.
REPLAY ?= no

LDFLAGS :=
LDFLAGS := -lcrypto -lssl -lpython2.7

ifeq ($(REPLAY),yes)
LDFLAGS += -lmemcached
LDFLAGS += -lmemcached
else
CXXFLAGS += -DNOREPLAY
endif
Expand All @@ -25,7 +25,7 @@ all: lsm-sim
$(CXX) $(CXXFLAGS) -c $<

lsm-sim: $(OBJS)
$(CXX) $(LDFLAGS) -o $@ $^ -lpython2.7
$(CXX) $(LDFLAGS) -o $@ $^

clean:
-rm lsm-sim src/*.o
Expand Down
26 changes: 26 additions & 0 deletions src/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "common.h"
#include "request.h"

#include "openssl/sha.h"

Request::Request(const std::string& s)
: time{}
, key_sz{}
Expand Down Expand Up @@ -70,3 +72,27 @@ bool Request::operator<(const Request& other)
{
return time < other.time;
}

size_t Request::hash_key(const size_t modulus) const
{
union Int_to_unsigned_char
{
uint32_t int_value;
unsigned char char_value[sizeof(uint32_t)];
};

union Unsigned_char_to_size_t
{
unsigned char char_value[SHA_DIGEST_LENGTH]; // 20 bytes.
size_t size_t_value; // 8 bytes on x86.
};

Int_to_unsigned_char input;
input.int_value = this->kid;
Unsigned_char_to_size_t output;
SHA_CTX context;
SHA1_Init(&context);
SHA1_Update(&context, input.char_value, sizeof(uint32_t));
SHA1_Final(output.char_value, &context);
return output.size_t_value % modulus;
}
8 changes: 8 additions & 0 deletions src/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ struct Request {
int32_t get_frag() const;
bool operator<(const Request& other);

// Given a modulus, computes the SHA hash of the key_id 'kid' that belongs to
// this 'Request' and returns the hash mod the provided modules. This is
// intended for uniformly distributing requests across buckets, partitions,
// etc.
//
// @param modulus - number of buckets or partitions to mod the hash by.
size_t hash_key(const size_t modulus) const;

enum req_type { GET = 1
, SET = 2
, DEL = 3
Expand Down

0 comments on commit 5493754

Please sign in to comment.