-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindexmanage.go
129 lines (94 loc) · 1.96 KB
/
indexmanage.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package indexmap
import "sync"
const (
MainIndexType int32 = iota
UniqueIndexType
NormalIndexType
)
type IndexManage struct {
mutex sync.RWMutex
MainIndexName string
Indexs map[string]Index
}
func (this *IndexManage) InsertData(name string, data interface{}) bool{
this.mutex.Lock()
defer this.mutex.Unlock()
_, ok := this.Indexs[name]
if !ok {
return false
}
v, ok := this.Indexs[this.MainIndexName]
if !ok {
return false
}
rev := v.Query(v.PFuncGetField()(data)...)
if nil != rev {
return false
}
datatmp := &Record{
IsValid : true,
Data : data,
}
for _, v := range this.Indexs {
v.Insert(datatmp)
}
return true
}
func (this *IndexManage) DeleteData(name string, key ...interface{}){
this.mutex.Lock()
defer this.mutex.Unlock()
_, ok := this.Indexs[name]
if !ok {
return
}
data := this.Indexs[name].Query(key...)
if nil == data {
return
}
this.Indexs[name].Delete(key...)
}
func (this *IndexManage) QueryData(name string, field ...interface{})([]interface{}){
this.mutex.RLock()
defer this.mutex.RUnlock()
_, ok := this.Indexs[name]
if !ok {
return nil
}
data := this.Indexs[name].Query(field...)
return data
}
func (this *IndexManage) ModifyData(name string, data interface{})bool{
this.mutex.Lock()
defer this.mutex.Unlock()
// 查找是否存在索引
index, ok := this.Indexs[name]
if !ok {
return false
}
// 判断索引是不是唯一键或者主键
if UniqueIndexType != index.IndexType() && MainIndexType != index.IndexType() {
return false
}
// 查找是否存在主键
v, ok := this.Indexs[this.MainIndexName]
if !ok {
return false
}
// 查找是否有数据
revdata := v.Query(v.PFuncGetField()(data)...)
if nil == revdata {
return false
}
// 通过主索引删除数据
for _, tmp := range revdata {
v.Delete(v.PFuncGetField()(tmp)...)
}
for _, v := range this.Indexs {
datatmp := &Record{
IsValid : true,
Data : data,
}
v.Insert(datatmp)
}
return true
}