-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachev2.go
268 lines (233 loc) · 5.72 KB
/
cachev2.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"context"
"fmt"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"os"
"strings"
"sync"
)
type codeable struct {
id interface{}
code interface{}
}
type commons struct {
Conn *pgxpool.Pool
debugger Debugger
nomenclators sync.Map
pendingInsert sync.Map
tables_relation map[string]string
}
type queableCode struct {
code string
data *[]interface{}
}
func (ptr *commons) cacheUsefulLifeByType() {
conn, error := ptr.Conn.Acquire(context.Background())
defer conn.Release()
if error != nil {
debugger.error(error.Error())
os.Exit(2)
}
usefulLifeQuery := "select sf.useful_life, t.code as tid from sub_families sf inner join types t on sf.id = t.sub_family_id"
rows, err :=conn.Query(context.Background(), usefulLifeQuery)
if err != nil {
ptr.debugger.error(err.Error())
os.Exit(1)
}
dest := make([]codeable,0)
var code codeable
for rows.Next() {
rows.Scan(&code.code,&code.id)
dest = append(dest, code)
}
ptr.nomenclators.Store("useful_life_by_type", &dest)
}
func (ptr *commons) init() {
cd := make(map[string]string)
cd["acquisition_types"] = "id, code"
cd["sub_families"] = "id, code"
cd["types"] = "id, code"
cd["brands"] = "id, name"
cd["asset_models"] = "id, name"
cd["edifices"] = "id, code, name"
cd["centers"] = "id, code, name"
cd["levels"] = "id, code, name"
cd["spaces"] = "id, code, name"
cd["cost_centers"] = "id, code"
cd["entities"] = "id, name"
cd["colors"] = "id, code"
cd["materials"] = "id, code"
cd["areas"] = "id, code, name"
cd["operation_vars"] = "id, code"
cd["tags"] = "id, full_code"
ptr.tables_relation = cd
ptr.loadCodeablesNomenclators(make([]string,0))
ptr.cacheUsefulLifeByType()
}
func (ptr *commons ) isEnqueuedForInsert(key string, code string) (interface{},bool){
queue, _ := ptr.pendingInsert.Load(key)
if queue != nil {
for _, c := range queue.([]queableCode) {
if c.code == code {
return queue,true
}
}
}
return queue,false
}
func (ptr *commons ) batchInsert(wg *sync.WaitGroup){
defer wg.Done()
ptr.pendingInsert.Range(func(key interface{}, value interface{} ) bool {
conn, error := ptr.Conn.Acquire(context.Background())
defer conn.Release()
if error != nil {
debugger.error(error.Error())
os.Exit(2)
}
query := getQueryForBatchInsert(key.(string))
batch := pgx.Batch{}
for _, queable := range value.([]queableCode) {
if key == "imageables" {
fmt.Println(*queable.data, queable.code)
fmt.Println()
}
batch.Queue(query,*queable.data...)
}
if key == "imageables" {
os.Exit(1)
}
r := conn.SendBatch(context.Background(), &batch)
for i := 0; i < batch.Len(); i++ {
_, err :=r.Exec()
if err != nil {
cache.debugger.debug(err.Error(), " ",key)
}
}
defer ptr.pendingInsert.Delete(key)
return true
})
}
func (ptr *commons ) enqueueForInsert(key string, code string, params *[]interface{}) bool{
data, enqueued := ptr.isEnqueuedForInsert(key, code)
if enqueued == false {
if data == nil {
data = make([]queableCode,0)
}
queable := queableCode{code: code, data: params}
data = append(data.([]queableCode), queable)
ptr.pendingInsert.Store(key, data)
return false
}
return true
}
func (ptr *commons) loadCodeablesNomenclators(tables []string ) {
tablesToProcess := make(map[string]string)
if len(tables) >0 {
for _, table := range tables {
tablesToProcess[table] = ptr.tables_relation[table]
}
}else{
tablesToProcess = ptr.tables_relation
}
len := len(tablesToProcess)
wg := &sync.WaitGroup{}
wg.Add(len)
for key, val := range tablesToProcess {
go ptr.refresh(key, val, wg)
}
wg.Wait()
}
func(ptr *commons) refresh(table string, val string, wg *sync.WaitGroup) {
defer wg.Done()
conn := ptr.Conn
paramsCount := len(strings.Split(val,","))
container := make([]interface{},paramsCount)
for i := 0; i < paramsCount; i++ {
index := i
var a interface{}
container[index] = &a
}
target := table
query := fmt.Sprintf("select %v from %v order by id asc ",val, target)
rows, err :=conn.Query(context.Background(), query)
if err != nil {
ptr.debugger.debug(err.Error())
return
}
dest := make([]codeable,0)
var code codeable
for rows.Next() {
if paramsCount >2 {
rows.Scan(container...)
code.id = *(container[0].(*interface{}))
code.code = make([]interface{},0)
for _, i2 := range container[1:] {
val:= ( *(i2.(*interface{})))
code.code= append(code.code.([]interface{}), val)
}
//a := container[0].(*interface{})
}else{
rows.Scan(&code.id,&code.code)
}
dest = append(dest, code)
}
ptr.nomenclators.Store(table, &dest)
}
func (ptr *commons) findByCode(key string, code string) interface{}{
list , _ := ptr.nomenclators.Load(key)
for _, c := range *list.(*[]codeable) {
switch (c.code).(type) {
case []interface{}:
if (c.code).([]interface{})[0] == code {
return c.id
}
case interface{}:
if c.code == code {
return int(c.id.(int64))
}
case string:
if c.code == code {
return c.id
}
}
}
return -1
}
func (ptr *commons) fetch(key string, code string) interface{}{
list , _ := ptr.nomenclators.Load(key)
for _, c := range *list.(*[]codeable) {
switch (c.code).(type) {
case []interface{}:
if (c.code).([]interface{})[0] == code {
return c
}
case interface{}:
case string:
if c.code == code {
return c
}
}
}
return -1
}
func (ptr *commons) findById(key string, id int) interface{}{
list , _ := ptr.nomenclators.Load(key)
for _, c := range *list.(*[]codeable) {
realId := realint64(c.id)
if realId == int64(id) {
return c.code
}
}
return -1
}
func realint64(tipe interface{}) int64{
switch tipe.(type) {
case int:
return int64(tipe.(int))
case int64:
return tipe.(int64)
}
return tipe.(int64)
}