-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
163 lines (148 loc) · 4.67 KB
/
client.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
package hzgorm
import (
"fmt"
"github.com/hazelcast/hazelcast-go-client/core"
"github.com/hazelcast/hazelcast-go-client/core/predicate"
"github.com/jinzhu/gorm"
"log"
"reflect"
)
func (hz *hzGorm) cachePut(scope *gorm.Scope) {
hzMap, err := hz.Client.GetMap(scope.TableName())
if err != nil {
log.Printf("Couldn't get hazelcast map cache for put: %v", err.Error())
} else {
jsonValue, err := core.CreateHazelcastJSONValue(scope.Value)
if err != nil {
log.Printf("Couldn't serialize as json for hazelcast map cache: %v", err.Error())
} else {
primaryKey := fmt.Sprintf("%v", scope.PrimaryKeyValue())
err := hzMap.PutTransient(primaryKey, jsonValue, hz.getQueryTtl())
if err != nil {
log.Printf("Couldn't put on hazelcast map cache: %v", err.Error())
}
}
}
}
func (hz *hzGorm) cacheHit(scope *gorm.Scope) {
if !scope.IndirectValue().IsValid() {
return
}
obj := hz.utils.createNewStructType(scope.IndirectValue())
var fieldNames []string
hz.utils.structGetFieldNamesDeep(obj, &fieldNames)
if scope.SQL == "" {
log.Panic("THERE IS NO SQL QUERY !!!")
}
sqlPredicate := hz.predicateBuilder(scope.TableName(), scope.SQL, scope.SQLVars, fieldNames)
queryOrder, limitValue := hz.parseLimitAndOrder(scope.SQL)
hzMap, _ := hz.Client.GetMap(scope.TableName())
if sqlPredicate == "" {
values, err := hzMap.Values()
if err != nil {
// continues for db ops
hz.continueForDbOperations(scope, true, queryOrder, limitValue)
return
}
if len(values) == 0 {
// continues for db ops
hz.continueForDbOperations(scope, true, queryOrder, limitValue)
return
}
hz.addJsonToScopeStruct(scope, values, limitValue)
return
} else {
values, err := hzMap.ValuesWithPredicate(predicate.SQL(sqlPredicate))
if err != nil {
// continues for db ops
hz.continueForDbOperations(scope, false, queryOrder, limitValue)
return
}
if len(values) == 0 {
// continues for db ops
hz.disableCallback("hzgorm:before_query")
hz.continueForDbOperations(scope, false, queryOrder, limitValue)
hz.enableCallback("hzgorm:before_query")
return
}
hz.addJsonToScopeStruct(scope, values, limitValue)
return
}
}
func (hz *hzGorm) continueForDbOperations(scope *gorm.Scope, isFindAll bool, queryOrder string, limit int) {
if isFindAll {
if queryOrder == desc {
hz.disableCallback(All)
hz.db.Limit(limit).Order(scope.PrimaryKey() +" "+ desc).Find(scope.Value)
hz.enableCallback(All)
} else {
hz.disableCallback(All)
hz.db.Limit(limit).Find(scope.Value)
hz.enableCallback(All)
}
} else {
hz.db.Raw(scope.SQL, scope.SQLVars).Scan(scope.Value)
}
switch reflect.TypeOf(scope.Value).Elem().Kind() {
case reflect.Slice:
s := reflect.ValueOf(scope.Value).Elem()
for i := 0; i < s.Len(); i++ {
primaryKeyValue := hz.utils.determinePrimaryKeyValue(s.Index(i), scope.PrimaryKey())
mp, _ := hz.Client.GetMap(scope.TableName())
jsonValue, jerr := core.CreateHazelcastJSONValue(s.Index(i).Interface())
if jerr != nil {
log.Printf("Couldn't serialize as json for hazelcast map cache: %v", jerr.Error())
continue
}
err := mp.PutTransient(primaryKeyValue, jsonValue, hz.getQueryTtl())
if err != nil {
log.Printf("Couldn't put value: %v", jsonValue)
}
}
case reflect.Struct:
s := reflect.ValueOf(scope.Value)
primaryKeyValue := hz.utils.determinePrimaryKeyValue(s, scope.PrimaryKey())
mp, _ := hz.Client.GetMap(scope.TableName())
jsonValue, jerr := core.CreateHazelcastJSONValue(s.Interface())
if jerr != nil {
log.Printf("Create Hazelcast Json Value Error: %v", jerr)
return
}
err := mp.PutTransient(primaryKeyValue, jsonValue, hz.getQueryTtl())
if err != nil {
log.Printf("Couldn't put value: %v", jsonValue)
}
}
}
func (hz *hzGorm) addJsonToScopeStruct(scope *gorm.Scope, values []interface{}, limit int) {
limitCounter := 0
for _, val := range values {
if limit != -1 {
if limitCounter <= limit {
hz.addJson(scope, val, nil)
limitCounter++
} else {
break
}
} else {
hz.addJson(scope, val, nil)
}
}
}
func (hz *hzGorm) addJson(scope *gorm.Scope, val interface{}, reversedVal interface{}) {
cleanObject := hz.utils.createNewStructInterface(scope.IndirectValue())
var err error
if reversedVal == nil {
err = val.(*core.HazelcastJSONValue).Unmarshal(&cleanObject)
} else {
err = reversedVal.(*core.HazelcastJSONValue).Unmarshal(&cleanObject)
}
if err == nil {
if scope.IndirectValue().Kind() == reflect.Struct {
scope.IndirectValue().Set(reflect.ValueOf(cleanObject).Elem())
} else if scope.IndirectValue().Kind() == reflect.Slice {
result := reflect.Append(scope.IndirectValue(), reflect.ValueOf(cleanObject).Elem())
scope.IndirectValue().Set(result)
}
}
}