forked from digisan/event-mgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
own.go
112 lines (99 loc) · 2.73 KB
/
own.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
package eventmgr
import (
"errors"
"fmt"
"strings"
"github.com/dgraph-io/badger/v4"
bh "github.com/digisan/db-helper/badger"
. "github.com/digisan/go-generics"
lk "github.com/digisan/logkit"
)
// key: Owner@YYYYMM-27582250-1
// value: EventIDs ([uuid])
type Own struct {
OwnerYMSpan string // uname@202206-27582250-1
EventIDs []string // [uuid], owner self events
fnDbStore func(*Own) error // in db.go
}
func (own Own) String() string {
sb := strings.Builder{}
sb.WriteString(own.OwnerYMSpan + "\n")
for i, id := range own.EventIDs {
sb.WriteString(fmt.Sprintf("\t%02d\t%s\n", i, id))
}
return sb.String()
}
func (own *Own) BadgerDB() *badger.DB {
return DbGrp.MyID
}
func (own *Own) Key() []byte {
return []byte(own.OwnerYMSpan)
}
func (own *Own) Marshal(at any) (forKey, forValue []byte) {
lk.FailOnErrWhen(len(own.OwnerYMSpan) == 0, "%v", errors.New("empty owner"))
forKey = own.Key()
forValue = []byte(fmt.Sprint(own.EventIDs))
return
}
func (own *Own) Unmarshal(dbKey, dbVal []byte) (any, error) {
own.OwnerYMSpan = string(dbKey)
dbValStr := string(dbVal)
dbValStr = strings.TrimPrefix(dbValStr, "[")
dbValStr = strings.TrimSuffix(dbValStr, "]")
dbValStr = strings.TrimSpace(dbValStr)
own.EventIDs = IF(len(dbValStr) > 0, strings.Split(dbValStr, " "), []string{})
own.fnDbStore = bh.UpsertOneObject[Own]
return own, nil
}
func streamUpdateOwn(span string, tmpEvts ...TempEvt) error {
mOwnerEventIDs := make(map[string][]string)
for _, evt := range tmpEvts {
key := evt.owner + "@" + evt.yyyymm + "-" + span
mOwnerEventIDs[key] = append(mOwnerEventIDs[key], evt.evtID)
}
for owner, ids := range mOwnerEventIDs {
own := &Own{
OwnerYMSpan: owner,
EventIDs: ids,
fnDbStore: bh.UpsertOneObject[Own],
}
if err := own.fnDbStore(own); err != nil {
return err
}
}
return nil
}
func FetchOwn(owner, yyyymm string) ([]string, error) {
objects, err := bh.GetObjects[Own]([]byte(owner+"@"+yyyymm+"-"), nil)
if err != nil {
return nil, err
}
keys := FilterMap(objects, nil, func(i int, e *Own) string {
return e.OwnerYMSpan
})
rtIds := []string{}
for _, key := range keys {
own, err := bh.GetOneObject[Own]([]byte(key))
if err != nil {
return nil, err
}
rtIds = append(rtIds, own.EventIDs...)
}
return Settify(rtIds...), nil
}
func deleteOwn(owner, yyyymm, span, id string) (int, error) {
key := fmt.Sprintf("%s@%s-%s", owner, yyyymm, span)
own, err := bh.GetOneObject[Own]([]byte(key))
if err != nil {
return -1, err
}
if own == nil {
return 0, err
}
prevN := len(own.EventIDs)
FilterFast(&own.EventIDs, func(i int, e string) bool { return e != id })
if err := bh.UpsertOneObject(own); err != nil {
return -1, err
}
return prevN - len(own.EventIDs), nil
}