-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
389 lines (338 loc) · 7.96 KB
/
event.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
package eventmgr
import (
"bytes"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/dgraph-io/badger/v4"
bh "github.com/digisan/db-helper/badger"
. "github.com/digisan/go-generics"
lk "github.com/digisan/logkit"
"github.com/google/uuid"
)
// key: ID;
// value: others
// if changed, modify 1) NewXXX, 2) KO/VO 3) (Key/Value)FieldAddr, 4) Marshal, 5) Unmarshal.
type Event struct {
ID string
Tm time.Time
Owner string
EvtType string
RawJSON string
Public bool
Deleted bool
Followee string
fnDbStore func(*Event) error
}
// if [id] is empty, a new one will be assigned to event.
func NewEvent(id, owner, evtType, raw, flwee string) *Event {
if len(id) == 0 {
id = uuid.NewString()
}
return &Event{
ID: id,
Tm: time.Now().Truncate(time.Second),
Owner: owner,
EvtType: evtType,
RawJSON: raw,
Public: false,
Deleted: false,
Followee: flwee,
fnDbStore: bh.UpsertOneObject[Event],
}
}
func (evt Event) String() string {
if len(evt.ID) > 0 {
sb := strings.Builder{}
t, v := reflect.TypeOf(evt), reflect.ValueOf(evt)
for i := 0; i < t.NumField(); i++ {
fld, val := t.Field(i), v.Field(i)
if NotIn(fld.Name, "fnDbStore") {
sb.WriteString(fmt.Sprintf("%-12s %v\n", fld.Name+":", val.Interface()))
}
}
return sb.String()
}
return "[Empty Event]"
}
// db key order
const (
KO_Id int = iota
KO_END
)
func (evt *Event) KeyFieldAddr(ko int) *string {
mFldAddr := map[int]*string{
KO_Id: &evt.ID,
}
return mFldAddr[ko]
}
// db value order
const (
VO_Tm int = iota
VO_Owner
VO_EvtType
VO_RawJSON
VO_Pub
VO_Del
VO_Flwee
VO_END
)
func (evt *Event) ValFieldAddr(vo int) any {
mFldAddr := map[int]any{
VO_Tm: &evt.Tm,
VO_Owner: &evt.Owner,
VO_EvtType: &evt.EvtType,
VO_RawJSON: &evt.RawJSON,
VO_Pub: &evt.Public,
VO_Del: &evt.Deleted,
VO_Flwee: &evt.Followee,
}
return mFldAddr[vo]
}
////////////////////////////////////////////////////
func (evt *Event) BadgerDB() *badger.DB {
return DbGrp.IDEvt
}
func (evt *Event) Key() []byte {
return []byte(evt.ID)
}
func (evt *Event) Marshal(at any) (forKey, forValue []byte) {
lk.FailOnErrWhen(len(evt.ID) == 0, "%v", errors.New("empty event id"))
forKey = evt.Key()
for i := 0; i < VO_END; i++ {
switch v := evt.ValFieldAddr(i).(type) {
case *string:
forValue = append(forValue, []byte(*v)...)
case *bool:
forValue = append(forValue, []byte(fmt.Sprint(*v))...)
case *time.Time:
forValue = append(forValue, []byte(v.Format(time.RFC3339))...)
}
if i < VO_END-1 {
forValue = append(forValue, []byte(SEP)...)
}
}
return
}
func (evt *Event) Unmarshal(dbKey, dbVal []byte) (any, error) {
evt.ID = string(dbKey)
segs := bytes.SplitN(dbVal, []byte(SEP), VO_END)
for i := 0; i < VO_END; i++ {
sval := string(segs[i])
switch i {
case VO_Tm:
t, err := time.Parse(time.RFC3339, sval)
if err != nil {
lk.WarnOnErr("%v", err)
return nil, err
}
*evt.ValFieldAddr(i).(*time.Time) = t
case VO_Pub, VO_Del:
pub, err := strconv.ParseBool(sval)
if err != nil {
lk.WarnOnErr("%v", err)
return nil, err
}
*evt.ValFieldAddr(i).(*bool) = pub
default:
*evt.ValFieldAddr(i).(*string) = sval
}
}
evt.fnDbStore = bh.UpsertOneObject[Event]
return evt, nil
}
func (evt *Event) Publish(pub bool) error {
evt.Public = pub
return evt.fnDbStore(evt)
}
func (evt *Event) markDeleted() error {
evt.Deleted = true
return evt.fnDbStore(evt)
}
func FetchEvent(aliveOnly bool, id string) (*Event, error) {
event, err := bh.GetOneObject[Event]([]byte(id))
if err != nil {
return nil, err
}
if aliveOnly {
if event != nil && !event.Deleted {
return event, err
}
return nil, err
}
return event, err
}
func FetchEvents(aliveOnly bool, ids ...string) (events []*Event, err error) {
for _, id := range ids {
event, err := FetchEvent(aliveOnly, id)
if err != nil {
return nil, err
}
if event != nil {
events = append(events, event)
}
}
return
}
func PubEvent(id string, flag bool) error {
event, err := FetchEvent(true, id)
if err != nil {
return err
}
if event != nil {
return event.Publish(flag)
}
return fmt.Errorf("couldn't find %s, publish nothing", id)
}
func EventExists(id string) bool {
event, err := FetchEvent(true, id)
return err == nil && event != nil
}
func EventHappened(id string) bool {
event, err := FetchEvent(false, id)
return err == nil && event != nil
}
func delOneEventID(span string, id string) (bool, error) {
eIds, err := FetchEvtID([]byte(span))
if err != nil {
return false, err
}
tmpEvents := FilterMap(
eIds,
func(i int, s string) bool { return s != id },
func(i int, e string) TempEvt { return TempEvt{evtID: e} },
)
es := &EventSpan{mSpanCache: map[string][]TempEvt{span: tmpEvents}}
if err := bh.UpsertPartObject(es, span); err != nil {
return false, err
}
// Update Current Cache
spanNow := NowSpan()
es.mSpanCache[spanNow] = FilterMap4SglTyp(es.mSpanCache[spanNow], func(i int, e TempEvt) bool { return e.evtID != id }, nil)
return len(eIds) > len(tmpEvents), nil
}
func DelEvent(ids ...string) ([]string, error) {
es.mtx.Lock()
defer es.mtx.Unlock()
var deleted []string
for _, id := range ids {
event, err := FetchEvent(false, id)
if err != nil {
return nil, err
}
if event != nil {
// STEP 1: delete from span-ids
ok, err := delOneEventID(CreateSpanAt(event.Tm), id)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("%v, @%v", "event-id in Span-EventID is not registered normally", id)
}
// STEP 2: mark deleted
if err := event.markDeleted(); err != nil {
return nil, err
}
deleted = append(deleted, id)
}
}
return deleted, nil
}
func EraseEvent(ids ...string) ([]string, error) {
DbGrp.Lock()
defer DbGrp.Unlock()
var erased []string
for _, id := range ids {
// STEP 1: fetch event to get its tm
event, err := FetchEvent(false, id)
if err != nil {
return nil, err
}
if event == nil {
continue
}
// STEP 2: delete from id-event
n, err := bh.DeleteOneObject[Event]([]byte(id))
if err != nil {
return nil, err
}
if n == 0 {
continue
}
// event content has been deleted, then...
if n == 1 {
// STEP 3: delete from span-ids
span := CreateSpanAt(event.Tm)
ok, err := delOneEventID(span, id)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("%v, @%v", "event-id in Event-Body is not consistent with Span-EventID", id)
}
// eIds, err := FetchEvtIDs([]byte(span))
// if err != nil {
// return -1, err
// }
// tmpEvents := FilterMap(
// eIds,
// func(i int, s string) bool { return s != id },
// func(i int, e string) TempEvt { return TempEvt{evtID: e} },
// )
// es := &EventSpan{mSpanCache: map[string][]TempEvt{span: tmpEvents}}
// if err := bh.UpsertPartObject(es, span); err != nil {
// return -1, err
// }
// STEP 4: delete from own
n, err := deleteOwn(event.Owner, event.Tm.Format("200601"), span, id)
if err != nil {
return nil, err
}
if n != 1 {
return nil, fmt.Errorf("%v", "n must be 1 in deleting from Own")
}
// STEP 5: delete follow
if _, err = deleteEventFollow(id); err != nil {
return nil, err
}
// STEP 6: delete participants
if _, err = deleteParticipate(id); err != nil {
return nil, err
}
//
erased = append(erased, id)
}
}
return erased, nil
}
func DelGlobalEventID(ids ...string) ([]string, error) {
es.mtx.Lock()
defer es.mtx.Unlock()
spans, err := FetchSpan(nil)
if err != nil {
return nil, err
}
ids = Settify(ids...)
var deleted []string
SPAN:
for _, span := range spans {
for _, id := range ids {
ok, err := delOneEventID(span, id)
if err != nil {
return deleted, err
}
if ok {
deleted = append(deleted, id)
ids = FilterMap4SglTyp(ids, func(i int, e string) bool { return e != id }, nil)
if len(deleted) == len(ids) {
break SPAN
}
continue SPAN
}
}
}
return deleted, nil
}