This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
/
user.go
280 lines (235 loc) · 6.11 KB
/
user.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
import (
"dubbo.apache.org/dubbo-go/v3/config"
hessian "github.com/apache/dubbo-go-hessian2"
)
func init() {
config.SetProviderService(new(UserProvider))
// ------for hessian2------
hessian.RegisterPOJO(&User{})
cache = newUserDB()
t1, _ := time.Parse(
time.RFC3339,
"2021-08-01T10:08:41+00:00")
cache.Add(&User{ID: "0001", Code: 1, Name: "tc", Age: 18, Time: t1})
cache.Add(&User{ID: "0002", Code: 2, Name: "ic", Age: 88, Time: t1})
}
var cache *userDB
// userDB cache user.
type userDB struct {
// key is name, value is user obj
nameIndex map[string]*User
// key is code, value is user obj
codeIndex map[int64]*User
lock sync.Mutex
}
// userDB create func
func newUserDB() *userDB {
return &userDB{
nameIndex: make(map[string]*User, 16),
codeIndex: make(map[int64]*User, 16),
lock: sync.Mutex{},
}
}
// nolint
func (db *userDB) Add(u *User) bool {
db.lock.Lock()
defer db.lock.Unlock()
if u.Name == "" || u.Code <= 0 {
return false
}
if !db.existName(u.Name) && !db.existCode(u.Code) {
return db.AddForName(u) && db.AddForCode(u)
}
return false
}
// nolint
func (db *userDB) AddForName(u *User) bool {
if len(u.Name) == 0 {
return false
}
if _, ok := db.nameIndex[u.Name]; ok {
return false
}
db.nameIndex[u.Name] = u
return true
}
// nolint
func (db *userDB) AddForCode(u *User) bool {
if u.Code <= 0 {
return false
}
if _, ok := db.codeIndex[u.Code]; ok {
return false
}
db.codeIndex[u.Code] = u
return true
}
// nolint
func (db *userDB) GetByName(n string) (*User, bool) {
db.lock.Lock()
defer db.lock.Unlock()
r, ok := db.nameIndex[n]
return r, ok
}
// nolint
func (db *userDB) GetByCode(n int64) (*User, bool) {
db.lock.Lock()
defer db.lock.Unlock()
r, ok := db.codeIndex[n]
return r, ok
}
func (db *userDB) existName(name string) bool {
if len(name) <= 0 {
return false
}
_, ok := db.nameIndex[name]
if ok {
return true
}
return false
}
func (db *userDB) existCode(code int64) bool {
if code <= 0 {
return false
}
_, ok := db.codeIndex[code]
if ok {
return true
}
return false
}
// User user obj.
type User struct {
ID string `json:"id,omitempty"`
Code int64 `json:"code,omitempty"`
Name string `json:"name,omitempty"`
Age int32 `json:"age,omitempty"`
Time time.Time `json:"time,omitempty"`
}
// UserProvider the dubbo provider.
// like: version: 1.0.0 group: test
type UserProvider struct{}
// CreateUser new user, PX config POST.
func (u *UserProvider) CreateUser(ctx context.Context, user *User) (*User, error) {
outLn("Req CreateUser data:%#v", user)
if user == nil {
return nil, errors.New("not found")
}
_, ok := cache.GetByName(user.Name)
if ok {
return nil, errors.New("data is exist")
}
b := cache.Add(user)
if b {
return user, nil
}
return nil, errors.New("add error")
}
// GetUserByName query by name, single param, PX config GET.
func (u *UserProvider) GetUserByName(ctx context.Context, name string) (*User, error) {
outLn("Req GetUserByName name:%#v", name)
r, ok := cache.GetByName(name)
if ok {
outLn("Req GetUserByName result:%#v", r)
return r, nil
}
return nil, nil
}
// GetUserByCode query by code, single param, PX config GET.
func (u *UserProvider) GetUserByCode(ctx context.Context, code int64) (*User, error) {
outLn("Req GetUserByCode name:%#v", code)
r, ok := cache.GetByCode(code)
if ok {
outLn("Req GetUserByCode result:%#v", r)
return r, nil
}
return nil, nil
}
// GetUserTimeout query by name, will timeout for pixiu.
func (u *UserProvider) GetUserTimeout(ctx context.Context, name string) (*User, error) {
outLn("Req GetUserByName name:%#v", name)
// sleep 10s, pixiu config less than 10s.
time.Sleep(10 * time.Second)
r, ok := cache.GetByName(name)
if ok {
outLn("Req GetUserByName result:%#v", r)
return r, nil
}
return nil, nil
}
// GetUserByNameAndAge query by name and age, two params, PX config GET.
func (u *UserProvider) GetUserByNameAndAge(ctx context.Context, name string, age int32) (*User, error) {
outLn("Req GetUserByNameAndAge name:%s, age:%d", name, age)
r, ok := cache.GetByName(name)
if ok && r.Age == age {
outLn("Req GetUserByNameAndAge result:%#v", r)
return r, nil
}
return r, nil
}
// UpdateUser update by user struct, my be another struct, PX config POST or PUT.
func (u *UserProvider) UpdateUser(ctx context.Context, user *User) (bool, error) {
outLn("Req UpdateUser data:%#v", user)
r, ok := cache.GetByName(user.Name)
if ok {
if user.ID != "" {
r.ID = user.ID
}
if user.Age >= 0 {
r.Age = user.Age
}
return true, nil
}
return false, errors.New("not found")
}
// UpdateUserByName update by user struct, my be another struct, PX config POST or PUT.
func (u *UserProvider) UpdateUserByName(ctx context.Context, name string, user *User) (bool, error) {
outLn("Req UpdateUserByName data:%#v", user)
r, ok := cache.GetByName(name)
if ok {
if user.ID != "" {
r.ID = user.ID
}
if user.Age >= 0 {
r.Age = user.Age
}
return true, nil
}
return false, errors.New("not found")
}
// nolint
func (u *UserProvider) Reference() string {
return "TripleUserProvider"
}
// nolint
func (u User) JavaClassName() string {
return "com.dubbogo.pixiu.TripleUserService"
}
// nolint
func outLn(format string, args ...interface{}) {
fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...)
}