-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseDbModel.go
More file actions
165 lines (132 loc) · 3.49 KB
/
baseDbModel.go
File metadata and controls
165 lines (132 loc) · 3.49 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package go_orm
import (
"sync"
)
const StatusActive = "Active"
type IBaseDbModel interface {
RegisterIntIndex(string string, AltIndex AltIntIndex)
RegisterStringIndex(string string, AltIndex AltStringIndex)
GetCache() interface{}
FindInCache(id int) PotokOrm
FindIndex(index string, id interface{}, onlyActive bool) PotokOrm
AddToCache(v PotokOrm)
ClearCache()
Len() int
}
type BaseDbModel struct {
cache map[int]*PotokOrm
cacheMutex *sync.RWMutex
isInit bool
altIntIndex map[string]AltIntIndex
altIntCache map[string]map[int]*PotokOrm
altStringIndex map[string]AltStringIndex
altStringCache map[string]map[string]*PotokOrm
}
func (m *BaseDbModel) GetCache() interface{} {
m.init()
return m.cache
}
func (m *BaseDbModel) init() {
if m.isInit != true {
m.cacheMutex = &sync.RWMutex{}
m.cache = make(map[int]*PotokOrm)
m.isInit = true
m.altIntIndex = make(map[string]AltIntIndex)
m.altIntCache = make(map[string]map[int]*PotokOrm)
m.altStringIndex = make(map[string]AltStringIndex)
m.altStringCache = make(map[string]map[string]*PotokOrm)
}
}
func (m *BaseDbModel) RegisterIntIndex(string string, AltIndex AltIntIndex) {
m.init()
m.altIntIndex[string] = AltIndex
}
func (m *BaseDbModel) RegisterStringIndex(string string, AltIndex AltStringIndex) {
m.init()
m.altStringIndex[string] = AltIndex
}
func (m *BaseDbModel) FindInCache(id int) PotokOrm {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
if resultVal, ok := m.cache[id]; ok {
result := (*resultVal).(PotokOrm)
if result.IsActive() {
return result
}
}
return nil
}
func (m *BaseDbModel) FindIndex(index string, id interface{}, onlyActive bool) PotokOrm {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
switch id.(type) {
case int:
if _, ok := m.altIntIndex[index]; ok {
if resultVal, ok := m.altIntCache[index][id.(int)]; ok {
result := *resultVal
if !onlyActive || result.IsActive() {
return result
}
}
}
case string:
if _, ok := m.altStringIndex[index]; ok {
if resultVal, ok := m.altStringCache[index][id.(string)]; ok {
result := *resultVal
if !onlyActive || result.IsActive() {
return result
}
}
}
default:
panic("Only string or int key are available.")
}
return nil
}
func (m *BaseDbModel) AddToCache(v PotokOrm) {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
if oldVal, ok := m.cache[v.GetId()]; ok {
for id, alterIndexFunc := range m.altIntIndex {
intIdToDelete := alterIndexFunc(*oldVal)
delete(m.altIntCache[id], intIdToDelete)
}
for id, alterIndexFunc := range m.altStringIndex {
stringIdToDelete := alterIndexFunc(*oldVal)
delete(m.altStringCache[id], stringIdToDelete)
}
}
m.cache[v.GetId()] = &v
for id, val := range m.altIntIndex {
if _, ok := m.altIntCache[id]; !ok {
m.altIntCache[id] = make(map[int]*PotokOrm)
}
m.altIntCache[id][val(v)] = &v
}
for id, val := range m.altStringIndex {
if _, ok := m.altStringCache[id]; !ok {
m.altStringCache[id] = make(map[string]*PotokOrm)
}
m.altStringCache[id][val(v)] = &v
}
}
func (m *BaseDbModel) ClearCache() {
m.init()
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
m.cache = make(map[int]*PotokOrm)
m.altIntCache = make(map[string]map[int]*PotokOrm)
m.altStringCache = make(map[string]map[string]*PotokOrm)
}
func (m *BaseDbModel) Len() int {
return len(m.cache)
}
type PotokOrm interface {
IsActive() bool
GetId() int
}
type AltIntIndex func(orm PotokOrm) int
type AltStringIndex func(orm PotokOrm) string