Simplify cache ring

The continuum slice is not needed since the number of partitions
doesn't change.  This removes the slice to make the mapping simpler.
pull/8856/head
Jason Wilder 2017-09-14 13:11:40 -06:00
parent 65bcddf6c0
commit 4124a8ed97
2 changed files with 7 additions and 14 deletions

View File

@ -36,10 +36,6 @@ type ring struct {
// len(partitions) <= len(continuum) // len(partitions) <= len(continuum)
partitions []*partition partitions []*partition
// A mapping of partition to location on the ring continuum. This is used
// to lookup a partition.
continuum []*partition
// Number of keys within the ring. This is used to provide a hint for // Number of keys within the ring. This is used to provide a hint for
// allocating the return values in keys(). It will not be perfectly accurate // allocating the return values in keys(). It will not be perfectly accurate
// since it doesn't consider adding duplicate keys, or trying to remove non- // since it doesn't consider adding duplicate keys, or trying to remove non-
@ -59,20 +55,17 @@ func newring(n int) (*ring, error) {
} }
r := ring{ r := ring{
continuum: make([]*partition, partitions), // maximum number of partitions. partitions: make([]*partition, n), // maximum number of partitions.
} }
// The trick here is to map N partitions to all points on the continuum, // The trick here is to map N partitions to all points on the continuum,
// such that the first eight bits of a given hash will map directly to one // such that the first eight bits of a given hash will map directly to one
// of the N partitions. // of the N partitions.
for i := 0; i < len(r.continuum); i++ { for i := 0; i < len(r.partitions); i++ {
if (i == 0 || i%(partitions/n) == 0) && len(r.partitions) < n { r.partitions[i] = &partition{
r.partitions = append(r.partitions, &partition{ store: make(map[string]*entry),
store: make(map[string]*entry), entrySizeHints: make(map[uint64]int),
entrySizeHints: make(map[uint64]int),
})
} }
r.continuum[i] = r.partitions[len(r.partitions)-1]
} }
return &r, nil return &r, nil
} }
@ -92,7 +85,7 @@ func (r *ring) reset() {
// getPartition retrieves the hash ring partition associated with the provided // getPartition retrieves the hash ring partition associated with the provided
// key. // key.
func (r *ring) getPartition(key []byte) *partition { func (r *ring) getPartition(key []byte) *partition {
return r.continuum[int(xxhash.Sum64(key)%partitions)] return r.partitions[int(xxhash.Sum64(key)%partitions)]
} }
// entry returns the entry for the given key. // entry returns the entry for the given key.

View File

@ -31,7 +31,7 @@ func TestRing_newRing(t *testing.T) {
// Check partitions distributed correctly // Check partitions distributed correctly
partitions := make([]*partition, 0) partitions := make([]*partition, 0)
for i, partition := range r.continuum { for i, partition := range r.partitions {
if i == 0 || partition != partitions[len(partitions)-1] { if i == 0 || partition != partitions[len(partitions)-1] {
partitions = append(partitions, partition) partitions = append(partitions, partition)
} }