This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tidb.go
93 lines (80 loc) · 1.88 KB
/
tidb.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
package main
import (
"encoding/binary"
"encoding/json"
"sort"
"sync"
)
const defaulttablePath = "../storage/table"
// Table saves the info of a table
type Table struct {
Name string `json:"name"`
DB string `json:"db"`
ID int64 `json:"id"`
Indices map[int64]string `json:"indices"`
}
// TableSlice is the slice of tables
type TableSlice []*Table
func (s TableSlice) Len() int { return len(s) }
func (s TableSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s TableSlice) Less(i, j int) bool {
if s[i].DB < s[j].DB {
return true
} else if s[i].DB == s[j].DB && s[i].Name < s[j].Name {
return true
} else if s[i].DB == s[j].DB && s[i].Name == s[j].Name {
return s[i].ID < s[j].ID
}
return false
}
type TablesStore struct {
sync.RWMutex
*LeveldbStorage
}
func loadTables() []*Table {
tableSlice := make([]*Table, 0)
tables.RLock()
allValue := tables.Traversal()
tables.RUnlock()
for _, v := range allValue {
var table Table
err := json.Unmarshal([]byte(v), &table)
perr(err)
tableSlice = append(tableSlice, &table)
}
sort.Sort(TableSlice(tableSlice))
return tableSlice
}
func updateTables() {
dbInfos := dbRequest(0)
tables.Lock()
defer tables.Unlock()
for _, info := range dbInfos {
if info.State == 0 {
continue
}
tblInfos := tableRequest(0, info.Name.O)
for _, table := range tblInfos {
indices := make(map[int64]string, len(table.Indices))
for _, index := range table.Indices {
indices[index.ID] = index.Name.O
}
newTable := &Table{
ID: table.ID,
Name: table.Name.O,
DB: info.Name.O,
Indices: indices,
}
value, err := json.Marshal(newTable)
perr(err)
var key = make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(newTable.ID))
err = tables.Save(key, value)
perr(err)
}
}
}
var tables TablesStore
func init() {
tables.LeveldbStorage, _ = NewLeveldbStorage(defaulttablePath)
}