pebble

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 12 Imported by: 13

Documentation

Overview

Package pebble contains gotd storage implementations using CockroachDB pebble.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrStateNotFound = errors.New("state not found")

ErrStateNotFound is returned by StateStorage setters if user state does not exist, as updates.StateStorage requires.

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 pebble.

func NewAccessHasher added in v0.26.0

func NewAccessHasher(db *pebble.DB) *AccessHasher

NewAccessHasher creates new access hasher using pebble.

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.

func (*AccessHasher) WithWriteOptions added in v0.26.0

func (s *AccessHasher) WithWriteOptions(writeOpts *pebble.WriteOptions) *AccessHasher

WithWriteOptions sets pebble's write options for write operations.

Default is nil, which pebble treats as pebble.Sync.

type Credentials

type Credentials struct {
	kv.Credentials
}

Credentials stores user credentials to Pebble.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	pebbledb "github.com/cockroachdb/pebble"
	"github.com/go-faster/errors"

	tgauth "github.com/gotd/td/telegram/auth"

	"github.com/gotd/td/telegram"

	"github.com/gotd/contrib/auth"
	"github.com/gotd/contrib/auth/terminal"
	"github.com/gotd/contrib/pebble"
)

func pebbleAuth(ctx context.Context) error {
	db, err := pebbledb.Open("pebble.db", &pebbledb.Options{})
	if err != nil {
		return errors.Wrap(err, "create pebble storage")
	}
	cred := pebble.NewCredentials(db).
		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 := pebbleAuth(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}

func NewCredentials

func NewCredentials(db *pebble.DB) 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 *pebble.DB) *PeerStorage

NewPeerStorage creates new peer storage using pebble.

func (PeerStorage) Add

func (s PeerStorage) Add(ctx context.Context, value storage.Peer) (rerr error)

Add adds given peer to the storage.

func (PeerStorage) AddMany added in v0.26.0

func (s PeerStorage) AddMany(ctx context.Context, values []storage.Peer) (rerr error)

AddMany adds given peers to the storage in one batch.

A peer that cannot be encoded is skipped: the others are still written, and the encoding errors are returned.

func (PeerStorage) Assign

func (s PeerStorage) Assign(ctx context.Context, key string, value storage.Peer) (rerr error)

Assign adds given peer to the storage and associate it to the given key.

func (PeerStorage) Find

func (s PeerStorage) Find(ctx context.Context, key storage.PeerKey) (_ storage.Peer, rerr error)

Find finds peer using given key.

func (PeerStorage) Iterate added in v0.5.0

Iterate creates and returns new PeerIterator.

func (PeerStorage) Resolve

func (s PeerStorage) Resolve(ctx context.Context, key string) (_ storage.Peer, rerr error)

Resolve finds peer using associated key.

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.

func (*PeerStorage) WithWriteOptions added in v0.5.0

func (s *PeerStorage) WithWriteOptions(writeOpts *pebble.WriteOptions) *PeerStorage

WithWriteOptions sets pebble's write options for write operations.

Default is nil, which pebble treats as pebble.Sync: every write is synced to disk. Use pebble.NoSync to trade durability of the last writes for speed.

type PeersStorage added in v0.26.0

type PeersStorage struct {
	// contains filtered or unexported fields
}

PeersStorage is peers.Storage implementation using pebble.

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.

func NewPeersStorage added in v0.26.0

func NewPeersStorage(db *pebble.DB) *PeersStorage

NewPeersStorage creates new peers.Storage using pebble.

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(ctx 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) (int64, error)

GetContactsHash implements peers.Storage.

func (*PeersStorage) Save added in v0.26.0

func (s *PeersStorage) Save(_ context.Context, key peers.Key, value peers.Value) error

Save implements peers.Storage.

func (*PeersStorage) SaveContactsHash added in v0.26.0

func (s *PeersStorage) SaveContactsHash(_ context.Context, hash int64) error

SaveContactsHash implements peers.Storage.

func (*PeersStorage) SavePhone added in v0.26.0

func (s *PeersStorage) SavePhone(_ context.Context, phone string, key peers.Key) error

SavePhone implements peers.Storage.

func (*PeersStorage) WithWriteOptions added in v0.26.0

func (s *PeersStorage) WithWriteOptions(writeOpts *pebble.WriteOptions) *PeersStorage

WithWriteOptions sets pebble's write options for write operations.

Default is nil, which pebble treats as pebble.Sync: one fsync per write. peers.Manager saves every user and channel of every update, so use pebble.NoSync for high-volume clients; peers are refetched if lost.

type SessionStorage

type SessionStorage struct {
	kv.Session
}

SessionStorage is a MTProto session Pebble storage.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	pebbledb "github.com/cockroachdb/pebble"
	"github.com/go-faster/errors"

	"github.com/gotd/td/telegram"

	"github.com/gotd/contrib/pebble"
)

func pebbleStorage(ctx context.Context) error {
	db, err := pebbledb.Open("pebble.db", &pebbledb.Options{})
	if err != nil {
		return errors.Wrap(err, "create pebble storage")
	}
	storage := pebble.NewSessionStorage(db, "session")

	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 := pebbleStorage(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}

func NewSessionStorage

func NewSessionStorage(db *pebble.DB, key string) SessionStorage

NewSessionStorage creates new SessionStorage.

type StateStorage added in v0.26.0

type StateStorage struct {
	// contains filtered or unexported fields
}

StateStorage is updates.StateStorage implementation using pebble.

func NewStateStorage added in v0.26.0

func NewStateStorage(db *pebble.DB) *StateStorage

NewStateStorage creates new updates state storage using pebble.

Caller is responsible for db.Close() invocation.

func (*StateStorage) ForEachChannels added in v0.26.0

func (s *StateStorage) ForEachChannels(
	ctx context.Context,
	userID int64,
	f func(ctx context.Context, channelID int64, pts int) error,
) (rerr error)

ForEachChannels implements updates.StateStorage.

func (*StateStorage) GetChannelPts added in v0.26.0

func (s *StateStorage) GetChannelPts(_ context.Context, userID, channelID int64) (pts int, found bool, err error)

GetChannelPts implements updates.StateStorage.

func (*StateStorage) GetState added in v0.26.0

func (s *StateStorage) GetState(_ context.Context, userID int64) (updates.State, bool, error)

GetState implements updates.StateStorage.

func (*StateStorage) SetChannelPts added in v0.26.0

func (s *StateStorage) SetChannelPts(_ context.Context, userID, channelID int64, pts int) error

SetChannelPts implements updates.StateStorage.

func (*StateStorage) SetDate added in v0.26.0

func (s *StateStorage) SetDate(ctx context.Context, userID int64, date int) error

SetDate implements updates.StateStorage.

func (*StateStorage) SetDateSeq added in v0.26.0

func (s *StateStorage) SetDateSeq(ctx context.Context, userID int64, date, seq int) error

SetDateSeq implements updates.StateStorage.

func (*StateStorage) SetPts added in v0.26.0

func (s *StateStorage) SetPts(ctx context.Context, userID int64, pts int) error

SetPts implements updates.StateStorage.

func (*StateStorage) SetQts added in v0.26.0

func (s *StateStorage) SetQts(ctx context.Context, userID int64, qts int) error

SetQts implements updates.StateStorage.

func (*StateStorage) SetSeq added in v0.26.0

func (s *StateStorage) SetSeq(ctx context.Context, userID int64, seq int) error

SetSeq implements updates.StateStorage.

func (*StateStorage) SetState added in v0.26.0

func (s *StateStorage) SetState(_ context.Context, userID int64, state updates.State) error

SetState implements updates.StateStorage.

func (*StateStorage) WithWriteOptions added in v0.26.0

func (s *StateStorage) WithWriteOptions(writeOpts *pebble.WriteOptions) *StateStorage

WithWriteOptions sets pebble's write options for write operations.

Default is nil, which pebble treats as pebble.Sync.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL