diff --git a/common/consistenthash/consistenthash.go b/common/consistenthash/consistenthash.go index dde99a9..240612f 100644 --- a/common/consistenthash/consistenthash.go +++ b/common/consistenthash/consistenthash.go @@ -21,7 +21,27 @@ func (r *Ring) search(key string) int { ///////////////////////// // YOUR CODE GOES HERE // ///////////////////////// - + + h := hashId(key) + l := r.Nodes.Len() + if h > r.Nodes[l - 1].HashId && h <= r.Nodes[0].HashId { + + return 0 + + } + + for i, node := range r.Nodes { + + if i != l - 1 { + + if h > node.HashId && h <= r.Nodes[i + 1].HashId { + return i + 1 + } + + } + + } + return 0 } diff --git a/dynamo/cache.go b/dynamo/cache.go index 0483a39..009ad23 100644 --- a/dynamo/cache.go +++ b/dynamo/cache.go @@ -12,6 +12,7 @@ import ( // Cache is the struct that handle all the data storage for the dynamo server. type Cache struct { data map[string]string + timestamps map[string]int64 sync.Mutex } @@ -20,7 +21,8 @@ func NewCache() *Cache { var s Cache s.data = make(map[string]string) - + s.timestamps = make(map[string]int64) + return &s } @@ -29,7 +31,7 @@ func NewCache() *Cache { func (cache *Cache) Get(key string) (value string, timestamp int64) { cache.Lock() value = cache.data[key] - timestamp = 0 + timestamp = cache.timestamps[key] cache.Unlock() log.Printf("[CACHE] Getting Key '%v' with Value '%v' @ timestamp '%v'\n", key, value, timestamp) @@ -43,6 +45,7 @@ func (cache *Cache) Put(key string, value string, timestamp int64) { cache.Lock() cache.data[key] = value + cache.timestamps[key] = timestamp cache.Unlock() return diff --git a/dynamo/coordinator.go b/dynamo/coordinator.go index 408cc9e..4f89ac1 100644 --- a/dynamo/coordinator.go +++ b/dynamo/coordinator.go @@ -193,6 +193,22 @@ func aggregateVotes(votes []*vote) (result string) { ///////////////////////// // YOUR CODE GOES HERE // ///////////////////////// - result = votes[0].value + + var maxTimestamp int64 + maxTimestamp = 0 + n := 0 + + for i, vote := range votes { + + if(vote.timestamp > maxTimestamp){ + + maxTimestamp = vote.timestamp + n = i + + } + + } + + result = votes[n].value return }