Documentation
¶
Overview ¶
Package bbolt contains gotd storage implementations using etcd bbolt.
Index ¶
- type AccessHasher
- func (s *AccessHasher) GetChannelAccessHash(_ context.Context, userID, channelID int64) (accessHash int64, found bool, err error)
- func (s *AccessHasher) GetUserAccessHash(_ context.Context, userID, targetUserID int64) (accessHash int64, found bool, err error)
- func (s *AccessHasher) SetChannelAccessHash(_ context.Context, userID, channelID, accessHash int64) error
- func (s *AccessHasher) SetUserAccessHash(_ context.Context, userID, targetUserID, accessHash int64) error
- type Credentials
- type PeerStorage
- func (s PeerStorage) Add(ctx context.Context, value storage.Peer) error
- func (s PeerStorage) Assign(ctx context.Context, key string, value storage.Peer) error
- func (s PeerStorage) Find(ctx context.Context, key storage.PeerKey) (p storage.Peer, rerr error)
- func (s PeerStorage) Iterate(ctx context.Context) (storage.PeerIterator, error)
- func (s PeerStorage) Resolve(ctx context.Context, key string) (p storage.Peer, rerr error)
- func (s *PeerStorage) WithBinaryEncoding() *PeerStorage
- type PeersStorage
- func (s *PeersStorage) Find(_ context.Context, key peers.Key) (value peers.Value, found bool, err error)
- func (s *PeersStorage) FindPhone(_ context.Context, phone string) (key peers.Key, value peers.Value, found bool, err error)
- func (s *PeersStorage) GetContactsHash(context.Context) (hash int64, err error)
- func (s *PeersStorage) Save(_ context.Context, key peers.Key, value peers.Value) error
- func (s *PeersStorage) SaveContactsHash(_ context.Context, hash int64) error
- func (s *PeersStorage) SavePhone(_ context.Context, phone string, key peers.Key) error
- type SessionStorage
- type State
- func (s *State) ForEachChannels(ctx context.Context, userID int64, ...) error
- func (s *State) GetChannelPts(_ context.Context, userID, channelID int64) (pts int, found bool, err error)
- func (s *State) GetState(_ context.Context, userID int64) (state updates.State, found bool, err error)
- func (s *State) SetChannelPts(_ context.Context, userID, channelID int64, pts int) error
- func (s *State) SetDate(_ context.Context, userID int64, date int) error
- func (s *State) SetDateSeq(_ context.Context, userID int64, date, seq int) error
- func (s *State) SetPts(_ context.Context, userID int64, pts int) error
- func (s *State) SetQts(_ context.Context, userID int64, qts int) error
- func (s *State) SetSeq(_ context.Context, userID int64, seq int) error
- func (s *State) SetState(_ context.Context, userID int64, state updates.State) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccessHasher ¶ added in v0.26.0
type AccessHasher struct {
// contains filtered or unexported fields
}
AccessHasher is updates.ChannelAccessHasher and updates.UserAccessHasher implementation using bbolt.
It uses the same per-user buckets as State, so both can share one db.
func NewAccessHasher ¶ added in v0.26.0
func NewAccessHasher(db *bolt.DB) *AccessHasher
NewAccessHasher creates new access hasher over bbolt.
Caller is responsible for db.Close() invocation.
func (*AccessHasher) GetChannelAccessHash ¶ added in v0.26.0
func (s *AccessHasher) GetChannelAccessHash(_ context.Context, userID, channelID int64) (accessHash int64, found bool, err error)
GetChannelAccessHash implements updates.ChannelAccessHasher.
func (*AccessHasher) GetUserAccessHash ¶ added in v0.26.0
func (s *AccessHasher) GetUserAccessHash(_ context.Context, userID, targetUserID int64) (accessHash int64, found bool, err error)
GetUserAccessHash implements updates.UserAccessHasher.
func (*AccessHasher) SetChannelAccessHash ¶ added in v0.26.0
func (s *AccessHasher) SetChannelAccessHash(_ context.Context, userID, channelID, accessHash int64) error
SetChannelAccessHash implements updates.ChannelAccessHasher.
func (*AccessHasher) SetUserAccessHash ¶ added in v0.26.0
func (s *AccessHasher) SetUserAccessHash(_ context.Context, userID, targetUserID, accessHash int64) error
SetUserAccessHash implements updates.UserAccessHasher.
type Credentials ¶
type Credentials struct {
kv.Credentials
}
Credentials stores user credentials to bbolt.
Example ¶
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/go-faster/errors"
bboltdb "go.etcd.io/bbolt"
"github.com/gotd/td/telegram"
tgauth "github.com/gotd/td/telegram/auth"
"github.com/gotd/contrib/auth"
"github.com/gotd/contrib/auth/terminal"
"github.com/gotd/contrib/bbolt"
)
func bboltAuth(ctx context.Context) error {
db, err := bboltdb.Open("bbolt.db", 0666, &bboltdb.Options{}) // nolint:gocritic
if err != nil {
return errors.Wrap(err, "create bbolt storage")
}
cred := bbolt.NewCredentials(db, []byte("bucket")).
WithPhoneKey("phone").
WithPasswordKey("password")
client, err := telegram.ClientFromEnvironment(telegram.Options{})
if err != nil {
return errors.Wrap(err, "create client")
}
return client.Run(ctx, func(ctx context.Context) error {
return client.Auth().IfNecessary(
ctx,
tgauth.NewFlow(auth.Build(cred, terminal.OS()), tgauth.SendCodeOptions{}),
)
})
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := bboltAuth(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
Output:
func NewCredentials ¶
func NewCredentials(db *bbolt.DB, bucket []byte) Credentials
NewCredentials creates new Credentials.
type PeerStorage ¶
type PeerStorage struct {
// contains filtered or unexported fields
}
PeerStorage is a peer storage based on pebble.
func NewPeerStorage ¶
func NewPeerStorage(db *bbolt.DB, bucket []byte) *PeerStorage
NewPeerStorage creates new peer storage using bbolt.
func (PeerStorage) Assign ¶
Assign adds given peer to the storage and associate it to the given key.
func (PeerStorage) Iterate ¶
func (s PeerStorage) Iterate(ctx context.Context) (storage.PeerIterator, error)
Iterate creates and returns new PeerIterator.
func (*PeerStorage) WithBinaryEncoding ¶ added in v0.26.0
func (s *PeerStorage) WithBinaryEncoding() *PeerStorage
WithBinaryEncoding makes the storage write peers in the compact binary format (storage.PeerEncodingBinary) instead of JSON. Values in either format are read, but versions of this package before the binary format can't read binary values: downgrading needs the peer storage cleared.
type PeersStorage ¶ added in v0.26.0
type PeersStorage struct {
// contains filtered or unexported fields
}
PeersStorage is peers.Storage implementation using bbolt.
It stores access hashes for peers.Manager of github.com/gotd/td/telegram/peers, unlike PeerStorage, which stores storage.Peer of github.com/gotd/contrib/storage. It uses its own buckets, so it can share one db with other storages of this package.
func NewPeersStorage ¶ added in v0.26.0
func NewPeersStorage(db *bolt.DB) *PeersStorage
NewPeersStorage creates new peers.Storage using bbolt.
Caller is responsible for db.Close() invocation.
func (*PeersStorage) Find ¶ added in v0.26.0
func (s *PeersStorage) Find(_ context.Context, key peers.Key) (value peers.Value, found bool, err error)
Find implements peers.Storage.
func (*PeersStorage) FindPhone ¶ added in v0.26.0
func (s *PeersStorage) FindPhone(_ context.Context, phone string) (key peers.Key, value peers.Value, found bool, err error)
FindPhone implements peers.Storage.
func (*PeersStorage) GetContactsHash ¶ added in v0.26.0
func (s *PeersStorage) GetContactsHash(context.Context) (hash int64, err error)
GetContactsHash implements peers.Storage.
func (*PeersStorage) SaveContactsHash ¶ added in v0.26.0
func (s *PeersStorage) SaveContactsHash(_ context.Context, hash int64) error
SaveContactsHash implements peers.Storage.
type SessionStorage ¶
SessionStorage is a MTProto session bbolt storage.
Example ¶
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/go-faster/errors"
bboltdb "go.etcd.io/bbolt"
"github.com/gotd/td/telegram"
"github.com/gotd/contrib/bbolt"
)
func bboltStorage(ctx context.Context) error {
db, err := bboltdb.Open("bbolt.db", 0666, &bboltdb.Options{}) // nolint:gocritic
if err != nil {
return errors.Wrap(err, "create bbolt storage")
}
storage := bbolt.NewSessionStorage(db, "session", []byte("bucket"))
client, err := telegram.ClientFromEnvironment(telegram.Options{
SessionStorage: storage,
})
if err != nil {
return errors.Wrap(err, "create client")
}
return client.Run(ctx, func(ctx context.Context) error {
_, err := client.Auth().Bot(ctx, os.Getenv("BOT_TOKEN"))
return err
})
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := bboltStorage(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
Output:
func NewSessionStorage ¶
func NewSessionStorage(db *bbolt.DB, key string, bucket []byte) SessionStorage
NewSessionStorage creates new SessionStorage.
type State ¶ added in v0.19.0
type State struct {
// contains filtered or unexported fields
}
State is updates.StateStorage implementation using bbolt.
func NewStateStorage ¶ added in v0.19.0
NewStateStorage creates new state storage over bbolt.
Caller is responsible for db.Close() invocation.