-
Notifications
You must be signed in to change notification settings - Fork 26
/
logt.go
261 lines (214 loc) · 5.99 KB
/
logt.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
// SPDX-License-Identifier: MIT
package rawread
import (
"context"
"encoding/json"
"fmt"
"math"
"time"
bmap "github.com/dgraph-io/sroar"
"go.cryptoscope.co/luigi"
"go.cryptoscope.co/margaret"
librarian "go.cryptoscope.co/margaret/indexes"
"go.cryptoscope.co/margaret/multilog/roaring"
"go.cryptoscope.co/muxrpc/v2"
"go.mindeco.de/log"
"go.mindeco.de/log/level"
"go.cryptoscope.co/muxrpc/v2/typemux"
"go.cryptoscope.co/ssb"
"go.cryptoscope.co/ssb/internal/mutil"
"go.cryptoscope.co/ssb/internal/transform"
"go.cryptoscope.co/ssb/message"
"go.cryptoscope.co/ssb/private"
"go.cryptoscope.co/ssb/repo"
)
type Plugin struct {
rxlog margaret.Log
types *roaring.MultiLog
priv *roaring.MultiLog
isSelf ssb.Authorizer
unboxer *private.Manager
res *repo.SequenceResolver
h muxrpc.Handler
info log.Logger
}
func NewByTypePlugin(
log log.Logger,
rootLog margaret.Log,
tl *roaring.MultiLog,
pl *roaring.MultiLog,
pm *private.Manager,
res *repo.SequenceResolver,
isSelf ssb.Authorizer,
) ssb.Plugin {
plug := &Plugin{
rxlog: rootLog,
types: tl,
priv: pl,
unboxer: pm,
res: res,
isSelf: isSelf,
info: log,
}
h := typemux.New(log)
h.RegisterSource(muxrpc.Method{"messagesByType"}, plug)
plug.h = &h
return plug
}
func (lt Plugin) Name() string { return "msgTypes" }
func (Plugin) Method() muxrpc.Method { return muxrpc.Method{"messagesByType"} }
func (lt Plugin) Handler() muxrpc.Handler { return lt.h }
func (g Plugin) HandleSource(ctx context.Context, req *muxrpc.Request, w *muxrpc.ByteSink) error {
var (
start = time.Now()
logger = log.With(g.info, "method", "messagesByType")
qry message.MessagesByTypeArgs
args []message.MessagesByTypeArgs
)
err := json.Unmarshal(req.RawArgs, &args)
if err != nil {
// assume just string for type
var args []string
err := json.Unmarshal(req.RawArgs, &args)
if err != nil {
return fmt.Errorf("byType: bad request data: %w", err)
}
if len(args) != 1 {
return fmt.Errorf("byType: bad request data: assumed string argument for type field")
}
qry.Type = args[0]
// Defaults for no arguments
qry.Keys = true
qry.Limit = -1
} else {
nargs := len(args)
if nargs == 1 {
qry = args[0]
} else {
return fmt.Errorf("byType: bad request data: assumed one argument object but got %d", nargs)
}
}
remote, err := ssb.GetFeedRefFromAddr(req.RemoteAddr())
if err != nil {
return fmt.Errorf("failed to establish remote: %w", err)
}
isSelf := g.isSelf.Authorize(remote)
if qry.Private && isSelf != nil {
return fmt.Errorf("not authroized")
}
logger = log.With(logger, "type", qry.Type)
// create toJSON sink
snk := transform.NewKeyValueWrapper(w, qry.Keys)
// wrap it into a counter for debugging
var cnt int
snk = newSinkCounter(&cnt, snk)
idxAddr := librarian.Addr("string:" + qry.Type)
if qry.Live {
if qry.Private {
return fmt.Errorf("TODO: fix live && private")
}
typed, err := g.types.Get(idxAddr)
if err != nil {
return fmt.Errorf("failed to load typed log: %w", err)
}
src, err := mutil.Indirect(g.rxlog, typed).Query(
margaret.Limit(int(qry.Limit)),
margaret.Live(qry.Live))
if err != nil {
return fmt.Errorf("logT: failed to qry tipe: %w", err)
}
// if qry.Private { TODO
// src = g.unboxedSrc(src)
// g.unboxer.WrappedUnboxingSink(snk)
// }
err = luigi.Pump(ctx, snk, src)
if err != nil {
return fmt.Errorf("logT: failed to pump msgs: %w", err)
}
return snk.Close()
}
/* TODO: i'm skipping a fairly big refactor here to find out what works first.
ideallly the live and not-live code would just be the same, somehow shoving it into Query(...).
Same goes for timestamp sorting and private.
Private is at least orthogonal, whereas sorting and live don't go well together.
*/
// not live
typed, err := g.types.LoadInternalBitmap(idxAddr)
if err != nil {
level.Warn(g.info).Log("event", "failed to load type bitmap", "err", err)
return snk.Close()
}
if qry.Private {
snk = g.unboxer.WrappedUnboxingSink(snk)
} else {
// filter all boxed messages from the stream
box1, err := g.priv.LoadInternalBitmap(librarian.Addr("meta:box1"))
if err != nil {
// TODO: compare not found
// return errors.Wrap(err, "failed to load bmap for box1")
box1 = bmap.NewBitmap()
}
box2, err := g.priv.LoadInternalBitmap(librarian.Addr("meta:box2"))
if err != nil {
// TODO: compare not found
// return errors.Wrap(err, "failed to load bmap for box2")
box2 = bmap.NewBitmap()
}
box1.Or(box2) // all the boxed messages
// remove all the boxed ones from the type we are looking up
it := box1.NewIterator()
for it.HasNext() {
it.Next()
v := it.Val()
if typed.Contains(v) {
typed.Remove(v)
}
}
}
// TODO: set _all_ correctly if gt=0 && lt=0
if qry.Lt == 0 {
qry.Lt = math.MaxInt64
}
var filter = func(ts int64) bool {
isGreater := ts > int64(qry.Gt)
isSmaller := ts < int64(qry.Lt)
return isGreater && isSmaller
}
sort, err := g.res.SortAndFilterBitmap(typed, repo.SortByClaimed, filter, qry.Reverse)
if err != nil {
return fmt.Errorf("failed to filter bitmap: %w", err)
}
sorted := time.Now()
level.Debug(logger).Log("event", "sorted seqs", "n", len(sort), "took", time.Since(start))
for _, res := range sort {
v, err := g.rxlog.Get(int64(res.Seq))
if err != nil {
if margaret.IsErrNulled(err) {
continue
}
level.Warn(logger).Log("event", " failed to get seq", "seq", res.Seq, "err", err)
continue
}
if err := snk.Pour(ctx, v); err != nil {
level.Warn(logger).Log("event", "messagesByType failed to send", "seq", res.Seq, "err", err)
break
}
if qry.Limit >= 0 {
qry.Limit--
if qry.Limit == 0 {
break
}
}
}
level.Debug(logger).Log("event", "messages streamed", "cnt", cnt, "took", time.Since(sorted))
return snk.Close()
}
func newSinkCounter(counter *int, sink luigi.Sink) luigi.FuncSink {
return func(ctx context.Context, v interface{}, err error) error {
if err != nil {
return err
}
*counter++
return sink.Pour(ctx, v)
}
}