-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition.go
89 lines (65 loc) · 1.52 KB
/
partition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Orangedb
import (
"strconv"
"sort"
"fmt"
)
// Chooser maps keys to shards
type Chooser interface {
// SetBuckets sets the list of known buckets from which the chooser should select
SetBuckets([]string) error
// Choose returns a bucket for a given key
Choose(key string) int
// Buckets returns the list of known buckets
Buckets() []string
}
// Shard is a named storage backend
type Shard struct {
Name string
//Backend Storage
}
type HashChooser struct {
mod int
}
func NewHashChooser(mod int) *HashChooser {
c := &HashChooser{
mod: mod,
}
return c
}
func (c *HashChooser) SetBuckets(buckets []string) error {
return nil
}
func (c *HashChooser) Choose(key string) int {
value ,_:= strconv.Atoi(key)
return value % c.mod;
}
func (c *HashChooser) ChooseReplicas(key string, n int) []string { return nil }
func (c *HashChooser) Buckets() []string { return nil }
type RangeChooser struct {
rangeList []string
partitionNum int
}
func NewRangeChooser() *RangeChooser {
c := &RangeChooser{
}
return c
}
func (c *RangeChooser) SetBuckets(buckets []string) error {
c.rangeList = buckets
sort.Strings(c.rangeList)
fmt.Println(c.rangeList)
c.partitionNum = len(buckets)+1
return nil
}
func (c *RangeChooser) Choose(key string) int {
var index int = 0
for index = 0; index < len(c.rangeList); index++ {
if key < c.rangeList[index] {
break
}
}
return index
}
func (c *RangeChooser) ChooseReplicas(key string, n int) []string { return nil }
func (c *RangeChooser) Buckets() []string { return nil }