Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solucao do Lab 2 #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions common/consistenthash/consistenthash.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ func (r *Ring) search(key string) int {
/////////////////////////
// YOUR CODE GOES HERE //
/////////////////////////

return 0
var (
nodeIdx int
updated bool
)

nodeIdx = 0;
updated = false
for idx, elem := range r.Nodes {
if elem.HashId > hashId(key) &&
(elem.HashId < r.Nodes[nodeIdx].HashId || !updated) {
nodeIdx = idx
updated = true
}
}
return nodeIdx
}

// NewRing will create a new Ring object and return a pointer to it.
Expand Down
7 changes: 5 additions & 2 deletions dynamo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -20,6 +21,7 @@ func NewCache() *Cache {
var s Cache

s.data = make(map[string]string)
s.timeStamps = make(map[string]int64)

return &s
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -52,6 +55,6 @@ 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 = cache.timeStamps
return data, timestamps
}
12 changes: 12 additions & 0 deletions dynamo/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ func aggregateVotes(votes []*vote) (result string) {
/////////////////////////
// YOUR CODE GOES HERE //
/////////////////////////
var (
lowestTimestamp int64
)

lowestTimestamp = votes[0].timestamp
result = votes[0].value
for _, vote := range votes {
if (lowestTimestamp > vote.timestamp) {
lowestTimestamp = vote.timestamp
result = vote.value
}
}

return
}