-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
394 lines (331 loc) · 6.75 KB
/
db.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package dokdb
// test sync 18:52
import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/google/uuid"
)
// db - struct for store json
type db struct {
// path to filename with raw json byte inside
filename string
// string is a random UUID
sync.Mutex
store map[string]object
}
// find all object with field=value
//
// FindByField
func (d *db) FindByField(jspath, value string) []object {
var objectList []object
println("")
println("FUNC Find by field")
for k, v := range d.store {
if v.Equals(jspath, value) {
objectList = append(objectList, v)
// println()
println("uuid=", k)
// printObject(v)
}
}
return objectList
}
// ----------------------
//
// NEW
//
// fn is filename.
// New make new *db.
// Then us Load for load file in db
func NewDB(fn string) *db {
println("func newdb")
return &db{
filename: fn,
store: make(map[string]object),
}
}
// ----------------------
//
// PRINT
//
// Print all store
func (d *db) Print() {
println("FUNC PRINT")
for k, v := range d.store {
println("uuid=", k)
printObject(v)
}
}
// ----------------------
//
// AddNewObjectFields
//
// AddNewObjectFields - add new json string and return UUID
func (d *db) AddNewObjectFields(lat, long float64, ct, ds string, js string) (id string) {
// println("")
// println("FUNC ADDJSON")
myUuid := uuid.New()
myuuidString := myUuid.String()
// println(myuuidString)
ov := object{}
ov.Lat = lat
ov.Long = long
ov.Id = myuuidString
ov.ContentType = ct
ov.Description = ds
str000 := strings.ReplaceAll(js, " ", "")
str001 := strings.ReplaceAll(str000, "\n", "")
str002 := strings.ReplaceAll(str001, "\t", "")
ov.Js = str002
d.Lock()
defer d.Unlock()
d.store[myuuidString] = ov
return myuuidString
}
// -----------------
//
// AddObject
//
// if object id == "" then generate id and save to db store
func (d *db) AddObject(o object) (id string, err error) {
println("")
println("ADD object to db")
if o.Id == "" {
o.Id = uuid.New().String()
}
d.Lock()
defer d.Unlock()
d.store[o.Id] = o
return o.Id, nil
}
// ------------------
//
// UPDATE
//
// search object by his id and update
func (d *db) UpdateObject(o object) (err error) {
println("")
println("UPDATE existing object")
d.Lock()
defer d.Unlock()
d.store[o.Id] = o
return nil
}
// ------------------
//
// FIND OBJECT BY UUID
//
// return object or make New object if empty
func (d *db) FindUUID(id string) (o object, err error) {
println("")
println("FIND by uuid")
object001, ok := d.store[id]
if !ok || id == "" {
return object{
coords: coords{
Lat: 0,
Long: 0,
},
Id: id,
ContentType: "",
Description: "",
Js: "",
}, errors.New("no id in db")
}
return object001, nil
}
// -----------------
//
// get value from object fild
func (d *db) GetField(id string, jspath string) (string, error) {
println("func getfield")
o001, ok := d.store[id]
if !ok {
return "", errors.New("no id in db")
}
return o001.GetField(jspath), nil
}
// ------------------
//
// UPDATE JSON
//
// update existing record by UUID
func (d *db) UpdateField(id string, field string, newfalue string) (err error) {
println("func db.updatefield")
object001, ok := d.store[id]
if !ok {
return errors.New("no id in db")
}
err = object001.SetField(field, newfalue)
if err != nil {
return err
}
d.Lock()
defer d.Unlock()
d.store[id] = object001
return nil
}
// --------------------
//
// SAVE
//
// Save "store map[string]placestruct to filename
func (d *db) Save() (er error) {
println("")
println("FUNC SAVE")
d.Lock()
defer d.Unlock()
fd, err := os.Create(d.filename)
if err != nil {
return err
}
// defer func(fd *os.File) {
// err := fd.Close()
// if err != nil {
// println("error close file")
// }
// }(fd)
// записываем красиво с отступами
tmpJ, err := json.MarshalIndent(d.store, "", " ")
if err != nil {
return err
}
// println("print string(tmpJ)")
// println(string(tmpJ))
// println("")
// println("store is converted to tmpJ []byte. len=", len(tmpJ))
writedLen, err := fd.Write(tmpJ)
if err != nil {
return err
}
println("writed to filename. len=", writedLen)
println("func Save ok")
return nil
}
// --------------------
//
// LOAD
//
// Load - load from "filename" to "store"
func (d *db) Load() (er error) {
println("")
println("FUNC LOAD json byte from filename=", d.filename)
rawbytes, err := os.ReadFile(d.filename)
if err != nil {
return err
}
d.Lock()
defer d.Unlock()
if err := json.Unmarshal(rawbytes, &d.store); err != nil {
return err
}
println("func Load ok")
return nil
}
// --------------------
//
// # FIND IN RECT
//
// find all objects in border and return slice
func (d *db) FindInRect(point1, point2 coords) (objectList []object) {
println("")
println("FUNC FindInRect")
for k, v := range d.store {
if v.InRect(point1, point2) {
objectList = append(objectList, v)
println()
println("uuid=", k)
printObject(v)
}
}
return objectList
}
// -------------------
//
// sort objects in rect by distance to p1
func (d *db) SortNearest(p coords, ol []object) {
sort.Slice(ol,
func(i int, j int) bool {
p1 := ol[i].coords
p2 := ol[j].coords
d1 := DistanceBetween(p1, p)
d2 := DistanceBetween(p2, p)
return d1 < d2
})
}
// --------------------
//
// # FIND IN RADIUS
//
// return objects in radius (meters)
func (d *db) FindInRadius(point coords, radiusMeters int64) (objectList []object) {
println("")
println("FUNC FindInRadius")
fmt.Printf("center lat= %8.2f long= %8.2f \n", point.Lat, point.Long)
println("radius=", radiusMeters)
for k, v := range d.store {
if checkPointInradius(point, radiusMeters, v.coords) {
objectList = append(objectList, v)
println("uuid= ", k)
printObject(v)
}
}
return objectList
}
// ------------------
//
// # GetAll
//
// get all []object
func (d *db) GetAll() (ol []object) {
println("")
println("func GetAll")
for k, v := range d.store {
ol = append(ol, v)
println(k)
}
return ol
}
// --------------------
//
// # getall_chan
//
// get all objects and return channel
func (d *db) GetAll_chan() chan object {
time.Sleep(3 * time.Second)
println("")
println("func GetAll_chan")
out := make(chan object)
go func() {
for k, v := range d.store {
println(" for k,v := range d.store key=", k)
v.Print()
out <- v
}
close(out)
}()
println("")
println("func GetAll_chan exit")
return out
}
// for k, v := range chan001 {
// if o, ok := <-v; ok == true {
// o.Print()
// }
// }
// --------------------
//
// find in rect
//
// find all objects in border and return chan
func (d *db) FindInRect_chan(point1, point2 coords, in chan<- object) <-chan object {
println("")
out := make(chan object)
return out
}