From 6c092fe01646808a7902f423a664f1d27b738eac Mon Sep 17 00:00:00 2001 From: Ju Arai Date: Thu, 13 Oct 2016 09:18:58 -0300 Subject: [PATCH 1/2] Solicitacao de code review --- common/consistenthash/consistenthash.go | 13 ++++++++----- dynamo/cache.go | 15 +++++++-------- dynamo/coordinator.go | 13 ++++++++++--- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/common/consistenthash/consistenthash.go b/common/consistenthash/consistenthash.go index dde99a9..35cffb4 100644 --- a/common/consistenthash/consistenthash.go +++ b/common/consistenthash/consistenthash.go @@ -18,11 +18,14 @@ type Ring struct { // search will find the index of the node that is responsible for the range that // includes the hashed value of key. func (r *Ring) search(key string) int { - ///////////////////////// - // YOUR CODE GOES HERE // - ///////////////////////// - - return 0 + + // alterado + sfunc := func(i int) bool { + return r.Nodes[i].HashId >= hashId(key) + } + return sort.Search(r.Nodes.Len(), sfunc) + // alterado + //return 0 } // NewRing will create a new Ring object and return a pointer to it. diff --git a/dynamo/cache.go b/dynamo/cache.go index 0483a39..7c9dccd 100644 --- a/dynamo/cache.go +++ b/dynamo/cache.go @@ -5,13 +5,10 @@ import ( "sync" ) -/////////////////////////////// -// THIS FILE IS MISSING CODE // -/////////////////////////////// - // Cache is the struct that handle all the data storage for the dynamo server. type Cache struct { - data map[string]string + data map[string]string + time map[string]int64 // alterado sync.Mutex } @@ -20,7 +17,7 @@ func NewCache() *Cache { var s Cache s.data = make(map[string]string) - + s.time = make(map[string]int64) // alterado return &s } @@ -29,7 +26,7 @@ func NewCache() *Cache { func (cache *Cache) Get(key string) (value string, timestamp int64) { cache.Lock() value = cache.data[key] - timestamp = 0 + timestamp = cache.time[key] // alterado cache.Unlock() log.Printf("[CACHE] Getting Key '%v' with Value '%v' @ timestamp '%v'\n", key, value, timestamp) @@ -43,6 +40,7 @@ func (cache *Cache) Put(key string, value string, timestamp int64) { cache.Lock() cache.data[key] = value + cache.time[key] = timestamp // alterado cache.Unlock() return @@ -52,6 +50,7 @@ func (cache *Cache) Put(key string, value string, timestamp int64) { // except for testing purposes. func (cache *Cache) getAll() (data map[string]string, timestamps map[string]int64) { data = cache.data - timestamps = make(map[string]int64) + //timestamps = make(map[string]int64) // alterado + timestamps = cache.time // alterado return data, timestamps } diff --git a/dynamo/coordinator.go b/dynamo/coordinator.go index 408cc9e..ab3a044 100644 --- a/dynamo/coordinator.go +++ b/dynamo/coordinator.go @@ -190,9 +190,16 @@ func aggregateVotes(votes []*vote) (result string) { log.Printf("[COORDINATOR] Vote: %v\n", vote.value) } - ///////////////////////// - // YOUR CODE GOES HERE // - ///////////////////////// + // alterado + var latestStamp int64 = votes[0].timestamp result = votes[0].value + for _, vote := range votes { + if vote.timestamp > latestStamp { + latestStamp = vote.timestamp + result = vote.value + } + } + // alterado + return } From e803e85acf5c4ebf8011fc35b4daff6307054665 Mon Sep 17 00:00:00 2001 From: Ju Arai Date: Mon, 17 Oct 2016 09:13:10 -0200 Subject: [PATCH 2/2] Entrega lab 2 --- common/consistenthash/consistenthash.go | 1 + 1 file changed, 1 insertion(+) diff --git a/common/consistenthash/consistenthash.go b/common/consistenthash/consistenthash.go index 35cffb4..e9e954d 100644 --- a/common/consistenthash/consistenthash.go +++ b/common/consistenthash/consistenthash.go @@ -22,6 +22,7 @@ func (r *Ring) search(key string) int { // alterado sfunc := func(i int) bool { return r.Nodes[i].HashId >= hashId(key) + return r.Nodes[0] } return sort.Search(r.Nodes.Len(), sfunc) // alterado