-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbingo.go
115 lines (97 loc) · 2.56 KB
/
bingo.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package bingodb
import (
"fmt"
"log"
"strings"
"sync/atomic"
)
type Bingo struct {
tables map[string]*Table
keeper *Keeper
systemMetrics *SystemMetrics
ServerConfig *ServerConfig
}
func (bingo *Bingo) TablesArray() []*TableInfo {
tables := make([]*TableInfo, 0, len(bingo.tables))
for _, table := range bingo.tables {
tables = append(tables, table.Info())
}
return tables
}
func (bingo *Bingo) Table(name string) (value *Table, ok bool) {
value, ok = bingo.tables[name]
return
}
func NewBingoFromConfigFile(configFile string) *Bingo {
bingo := newBingo()
err := ParseConfig(bingo, configFile)
if err != nil {
fmt.Println(err)
log.Fatalf("error: %v", err)
}
bingo.Start()
return bingo
}
func newBingo() *Bingo {
bingo := &Bingo{tables: make(map[string]*Table)}
bingo.keeper = NewKeeper(bingo)
bingo.systemMetrics = NewSystemMetrics(bingo)
return bingo
}
func (bingo *Bingo) Start() {
bingo.keeper.start()
bingo.systemMetrics.start()
}
func (bingo *Bingo) Stop() {
bingo.keeper.stop()
bingo.systemMetrics.stop()
}
func (bingo *Bingo) setTableMetrics() {
for _, source := range bingo.tables {
if config := source.metricsConfig; !strings.HasPrefix(source.name, "_") && config != nil {
NewTableMetrics(
source,
source.makeMetricsTable(),
config.Ttl,
config.Interval)
}
}
// Make global system metrics
metricsFields := make(map[string]*FieldSchema)
metricsFields["key"] = &FieldSchema{Name: "key", Type: "string"}
metricsFields["value"] = &FieldSchema{Name: "value", Type: "integer"}
metricsFields["time"] = &FieldSchema{Name: "time", Type: "integer"}
metricsFields["expireAt"] = &FieldSchema{Name: "expireAt", Type: "integer"}
metricsPrimaryKeySchema := &KeySchema{hashKey: metricsFields["key"], sortKey: metricsFields["time"]}
bingo.tables["_metrics"] = newTable(
bingo,
"_metrics",
&TableSchema{
fields: metricsFields,
primaryKey: metricsPrimaryKeySchema,
expireField: metricsFields["expireAt"],
},
&PrimaryIndex{index: newIndex(metricsPrimaryKeySchema)},
make(map[string]*SubIndex),
nil,
true,
)
}
func (bingo *Bingo) AddScan() {
atomic.AddInt64(&bingo.systemMetrics.scan, 1)
}
func (bingo *Bingo) AddGet() {
atomic.AddInt64(&bingo.systemMetrics.get, 1)
}
func (bingo *Bingo) AddPut() {
atomic.AddInt64(&bingo.systemMetrics.put, 1)
}
func (bingo *Bingo) AddRemove() {
atomic.AddInt64(&bingo.systemMetrics.remove, 1)
}
func (bingo *Bingo) AddExpire(i int64) {
atomic.AddInt64(&bingo.systemMetrics.expire, i)
}
func (bingo *Bingo) KeeperSize() int64 {
return bingo.keeper.list.Size()
}