-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.go
More file actions
433 lines (373 loc) · 11.1 KB
/
Copy pathstorage.go
File metadata and controls
433 lines (373 loc) · 11.1 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"path/filepath"
"time"
"github.com/go-faster/errors"
bolt "go.etcd.io/bbolt"
bolterrors "go.etcd.io/bbolt/errors"
"github.com/gotd/td/telegram/updates"
"github.com/gotd/td/tg"
)
// Bucket names. The server runs a single account, so user IDs are not part of
// the keys.
var (
// accessHashBucket holds channel access hashes, keyed by channel ID.
accessHashBucket = []byte("access_hash")
// dialogsBucket holds the persisted dialog cache, keyed by channel ID.
dialogsBucket = []byte("dialogs")
// messagesBucket holds per-channel sub-buckets of buffered unread messages.
messagesBucket = []byte("messages")
)
// openStateDB opens (creating if needed) the bbolt database used to persist the
// updates state (pts/qts/date/seq) and channel access hashes, so that the
// updates manager can recover via getDifference across restarts instead of
// re-syncing from scratch.
func openStateDB(cfg Config) (*bolt.DB, error) {
path := filepath.Join(cfg.SessionDir, "updates.bolt")
db, err := bolt.Open(path, 0o600, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
return nil, errors.Wrapf(err, "open %s", path)
}
return db, nil
}
// accessHasher is an updates.ChannelAccessHasher backed by bbolt.
type accessHasher struct {
db *bolt.DB
}
var _ updates.ChannelAccessHasher = accessHasher{}
func (h accessHasher) SetChannelAccessHash(_ context.Context, _, channelID, accessHash int64) error {
return h.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(accessHashBucket)
if err != nil {
return errors.Wrap(err, "create bucket")
}
return b.Put(i64b(channelID), i64b(accessHash))
})
}
func (h accessHasher) GetChannelAccessHash(_ context.Context, _, channelID int64) (int64, bool, error) {
var (
hash int64
found bool
)
err := h.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(accessHashBucket)
if b == nil {
return nil
}
v := b.Get(i64b(channelID))
if v == nil {
return nil
}
hash = b2i64(v)
found = true
return nil
})
if err != nil {
return 0, false, errors.Wrap(err, "get access hash")
}
return hash, found, nil
}
// dialogStore persists the dialog cache to bbolt so that the dialog list does
// not need to be re-fetched on every start. On restart the persisted cache is
// kept current by the updates manager via getDifference.
type dialogStore struct {
db *bolt.DB
}
// storedDialog is the on-disk representation of an UnreadChannel. The access
// hash is stored explicitly so the input peer can be rebuilt on load.
type storedDialog struct {
ID int64 `json:"id"`
Title string `json:"title"`
Username string `json:"username,omitempty"`
UnreadCount int `json:"unread_count"`
UnreadMark bool `json:"unread_mark,omitempty"`
Broadcast bool `json:"broadcast,omitempty"`
Megagroup bool `json:"megagroup,omitempty"`
ReadInboxMaxID int `json:"read_inbox_max_id"`
AccessHash int64 `json:"access_hash"`
}
func toStored(ch UnreadChannel) storedDialog {
var accessHash int64
if ipc, ok := ch.peer.(*tg.InputPeerChannel); ok {
accessHash = ipc.AccessHash
}
return storedDialog{
ID: ch.ID,
Title: ch.Title,
Username: ch.Username,
UnreadCount: ch.UnreadCount,
UnreadMark: ch.UnreadMark,
Broadcast: ch.Broadcast,
Megagroup: ch.Megagroup,
ReadInboxMaxID: ch.readInboxMaxID,
AccessHash: accessHash,
}
}
func (s storedDialog) toChannel() UnreadChannel {
return UnreadChannel{
ID: s.ID,
Title: s.Title,
Username: s.Username,
UnreadCount: s.UnreadCount,
UnreadMark: s.UnreadMark,
Broadcast: s.Broadcast,
Megagroup: s.Megagroup,
readInboxMaxID: s.ReadInboxMaxID,
peer: &tg.InputPeerChannel{
ChannelID: s.ID,
AccessHash: s.AccessHash,
},
}
}
// put upserts a single dialog.
func (d *dialogStore) put(ch UnreadChannel) error {
data, err := json.Marshal(toStored(ch))
if err != nil {
return errors.Wrap(err, "marshal dialog")
}
return d.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(dialogsBucket)
if err != nil {
return errors.Wrap(err, "create bucket")
}
return b.Put(i64b(ch.ID), data)
})
}
// putAll replaces the whole persisted dialog set, dropping dialogs that are no
// longer present.
func (d *dialogStore) putAll(chs []UnreadChannel) error {
return d.db.Update(func(tx *bolt.Tx) error {
if err := tx.DeleteBucket(dialogsBucket); err != nil && !errors.Is(err, bolterrors.ErrBucketNotFound) {
return errors.Wrap(err, "delete bucket")
}
b, err := tx.CreateBucket(dialogsBucket)
if err != nil {
return errors.Wrap(err, "create bucket")
}
for _, ch := range chs {
data, err := json.Marshal(toStored(ch))
if err != nil {
return errors.Wrap(err, "marshal dialog")
}
if err := b.Put(i64b(ch.ID), data); err != nil {
return errors.Wrap(err, "put dialog")
}
}
return nil
})
}
// delete removes a single dialog by channel ID.
func (d *dialogStore) delete(channelID int64) error {
return d.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(dialogsBucket)
if b == nil {
return nil
}
return b.Delete(i64b(channelID))
})
}
// load returns all persisted dialogs.
func (d *dialogStore) load() ([]UnreadChannel, error) {
var out []UnreadChannel
err := d.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(dialogsBucket)
if b == nil {
return nil
}
return b.ForEach(func(_, v []byte) error {
var sd storedDialog
if err := json.Unmarshal(v, &sd); err != nil {
return errors.Wrap(err, "unmarshal dialog")
}
out = append(out, sd.toChannel())
return nil
})
})
if err != nil {
return nil, errors.Wrap(err, "load dialogs")
}
return out, nil
}
// messageStore buffers recent unread messages per channel so that
// read_channel_unread can be served without a messages.getHistory RPC. Messages
// arrive for free on the update stream (updateNewChannelMessage, and the same
// after getDifference); this mirrors tdlib, which serves history from its local
// store and only hits the server to fill gaps.
//
// Layout: messagesBucket -> per-channel sub-bucket keyed by i64b(channelID) ->
// message ID (big-endian, so keys sort by ID) -> JSON Message.
type messageStore struct {
db *bolt.DB
cap int // max buffered messages per channel; older ones are trimmed.
}
// msgKey encodes a message ID as a big-endian key so bbolt iterates in ID order.
func msgKey(id int) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(id))
return b
}
// channelBucket returns the per-channel message sub-bucket, creating it (and the
// root) when create is set. Returns nil when it does not exist and create is
// false.
func channelBucket(tx *bolt.Tx, channelID int64, create bool) (*bolt.Bucket, error) {
if !create {
root := tx.Bucket(messagesBucket)
if root == nil {
return nil, nil
}
return root.Bucket(i64b(channelID)), nil
}
root, err := tx.CreateBucketIfNotExists(messagesBucket)
if err != nil {
return nil, errors.Wrap(err, "root bucket")
}
b, err := root.CreateBucketIfNotExists(i64b(channelID))
if err != nil {
return nil, errors.Wrap(err, "channel bucket")
}
return b, nil
}
// append buffers an incoming message, trimming the oldest beyond the cap.
func (s *messageStore) append(channelID int64, m Message) error {
data, err := json.Marshal(m)
if err != nil {
return errors.Wrap(err, "marshal message")
}
return s.db.Update(func(tx *bolt.Tx) error {
b, err := channelBucket(tx, channelID, true)
if err != nil {
return err
}
if err := b.Put(msgKey(m.ID), data); err != nil {
return errors.Wrap(err, "put message")
}
return trimOldest(b, s.cap)
})
}
// edit overwrites a buffered message in place. Messages that are not buffered
// (already read/pruned, or never seen) are ignored.
func (s *messageStore) edit(channelID int64, m Message) error {
data, err := json.Marshal(m)
if err != nil {
return errors.Wrap(err, "marshal message")
}
return s.db.Update(func(tx *bolt.Tx) error {
b, err := channelBucket(tx, channelID, false)
if err != nil || b == nil {
return err
}
if b.Get(msgKey(m.ID)) == nil {
return nil
}
if err := b.Put(msgKey(m.ID), data); err != nil {
return errors.Wrap(err, "put message")
}
return nil
})
}
// deleteMessages drops buffered messages by ID.
func (s *messageStore) deleteMessages(channelID int64, ids []int) error {
return s.db.Update(func(tx *bolt.Tx) error {
b, err := channelBucket(tx, channelID, false)
if err != nil || b == nil {
return err
}
for _, id := range ids {
if err := b.Delete(msgKey(id)); err != nil {
return errors.Wrap(err, "delete message")
}
}
return nil
})
}
// pruneRead drops buffered messages that are now read (ID <= maxID).
func (s *messageStore) pruneRead(channelID int64, maxID int) error {
return s.db.Update(func(tx *bolt.Tx) error {
b, err := channelBucket(tx, channelID, false)
if err != nil || b == nil {
return err
}
max := msgKey(maxID)
var keys [][]byte
c := b.Cursor()
for k, _ := c.First(); k != nil && bytes.Compare(k, max) <= 0; k, _ = c.Next() {
keys = append(keys, append([]byte(nil), k...))
}
for _, k := range keys {
if err := b.Delete(k); err != nil {
return errors.Wrap(err, "prune message")
}
}
return nil
})
}
// deleteChannel drops the whole message buffer of a channel.
func (s *messageStore) deleteChannel(channelID int64) error {
return s.db.Update(func(tx *bolt.Tx) error {
root := tx.Bucket(messagesBucket)
if root == nil {
return nil
}
if err := root.DeleteBucket(i64b(channelID)); err != nil && !errors.Is(err, bolterrors.ErrBucketNotFound) {
return errors.Wrap(err, "delete channel messages")
}
return nil
})
}
// load returns buffered unread messages (ID > afterID), newest first. A positive
// limit caps the result; limit <= 0 returns all buffered unread messages.
func (s *messageStore) load(channelID int64, afterID, limit int) ([]Message, error) {
var out []Message
err := s.db.View(func(tx *bolt.Tx) error {
b, err := channelBucket(tx, channelID, false)
if err != nil || b == nil {
return err
}
after := msgKey(afterID)
c := b.Cursor()
for k, v := c.Last(); k != nil && bytes.Compare(k, after) > 0; k, v = c.Prev() {
var m Message
if err := json.Unmarshal(v, &m); err != nil {
return errors.Wrap(err, "unmarshal message")
}
out = append(out, m)
if limit > 0 && len(out) >= limit {
break
}
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "load messages")
}
return out, nil
}
// trimOldest deletes the lowest-ID (oldest) messages until at most max remain.
func trimOldest(b *bolt.Bucket, max int) error {
var keys [][]byte
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
keys = append(keys, append([]byte(nil), k...))
}
if len(keys) <= max {
return nil
}
for _, k := range keys[:len(keys)-max] {
if err := b.Delete(k); err != nil {
return errors.Wrap(err, "trim message")
}
}
return nil
}
func i64b(v int64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(v))
return b
}
func b2i64(b []byte) int64 {
return int64(binary.LittleEndian.Uint64(b))
}