storage

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangefeedEvent added in v1.2.0

type ChangefeedEvent struct {
	Table string        `json:"table"`
	Key   []interface{} `json:"key"`
	Value *EventRowData `json:"value"`
	After *EventRowData `json:"after"`
}

ChangefeedEvent represents a changefeed event from CockroachDB

type CockroachClusterInfo

type CockroachClusterInfo struct {
	TotalNodes  int64                   `json:"total_nodes"`
	LiveNodes   int64                   `json:"live_nodes"`
	CurrentNode *CockroachClusterNode   `json:"current_node"`
	AllNodes    []*CockroachClusterNode `json:"all_nodes"`
	ClusterName string                  `json:"cluster_name"`
	IsCluster   bool                    `json:"is_cluster"`
}

CockroachClusterInfo represents cluster summary information

type CockroachClusterNode

type CockroachClusterNode struct {
	NodeID        int64     `json:"node_id"`
	Address       string    `json:"address"`
	SQLAddress    string    `json:"sql_address"`
	Locality      string    `json:"locality"`
	ClusterName   string    `json:"cluster_name"`
	ServerVersion string    `json:"server_version"`
	BuildTag      string    `json:"build_tag"`
	StartedAt     time.Time `json:"started_at"`
	IsLive        bool      `json:"is_live"`
	Ranges        int64     `json:"ranges"`
	Leases        int64     `json:"leases"`
}

CockroachClusterNode represents a node in the CockroachDB cluster

type CompiledFilter

type CompiledFilter struct {
	IDs     map[string]bool
	Authors map[string]bool
	Kinds   map[int]bool
	Since   *time.Time
	Until   *time.Time
	Tags    map[string]map[string]bool
	Limit   int
	Search  string
}

CompiledFilter represents a pre-compiled filter for efficient matching

func CompileFilter

func CompileFilter(f nostr.Filter) *CompiledFilter

CompileFilter pre-compiles a nostr filter for efficient matching

func (*CompiledFilter) BuildQuery

func (cf *CompiledFilter) BuildQuery() (string, []interface{}, error)

BuildQuery constructs the SQL query using the most efficient index

func (*CompiledFilter) GetBestIndex

func (cf *CompiledFilter) GetBestIndex() string

GetBestIndex determines the most efficient index to use for the filter

type DB

type DB struct {
	Pool  *pgxpool.Pool
	Bloom *bloom.BloomFilter
	// contains filtered or unexported fields
}

DB represents the CockroachDB connection

func InitDB

func InitDB(ctx context.Context, dbURI string, maxWSConnections int) (*DB, error)

InitDB initializes the CockroachDB connection with retries and optimized connection pooling

func (*DB) BatchInsertEvents

func (db *DB) BatchInsertEvents(ctx context.Context, events []nostr.Event) error

BatchInsertEvents optimized for CockroachDB with timeout handling

func (*DB) CleanExpiredEvents

func (db *DB) CleanExpiredEvents(ctx context.Context) (int, error)

CleanExpiredEvents removes events with expiration tags that have passed their expiration time

func (*DB) CloseDB

func (db *DB) CloseDB() error

CloseDB closes the database connection

func (*DB) CreateDatabaseIfNotExists

func (db *DB) CreateDatabaseIfNotExists(ctx context.Context, dbName string) error

CreateDatabaseIfNotExists creates the specified database if it doesn't exist

func (*DB) DeleteExpiredEvents

func (db *DB) DeleteExpiredEvents(ctx context.Context) error

DeleteExpiredEvents removes events that have expired based on the "expiration" tag.

func (*DB) EventExists

func (db *DB) EventExists(ctx context.Context, eventID string) (bool, error)

func (*DB) ExecuteBatch

func (db *DB) ExecuteBatch(ctx context.Context, batch *pgx.Batch) error

ExecuteBatch handles batch inserts or updates

func (*DB) ExecuteCommand

func (db *DB) ExecuteCommand(ctx context.Context, query string, args ...interface{}) error

ExecuteCommand handles INSERT, UPDATE, DELETE commands

func (*DB) ExecuteQuery

func (db *DB) ExecuteQuery(ctx context.Context, query string, args ...interface{}) (pgx.Row, error)

ExecuteQuery handles single-row queries (SELECT)

func (*DB) GetAddressableEvent

func (db *DB) GetAddressableEvent(ctx context.Context, pubkey string, kind int, dVal string) (nostr.Event, error)

GetAddressableEvent retrieves the latest addressable event for a given pubkey, kind, and 'd' tag.

func (*DB) GetClusterHealth

func (db *DB) GetClusterHealth(ctx context.Context) (map[string]interface{}, error)

GetClusterHealth returns cluster health information

func (*DB) GetCockroachClusterInfo

func (db *DB) GetCockroachClusterInfo(ctx context.Context) (*CockroachClusterInfo, error)

GetCockroachClusterInfo retrieves cluster information from CockroachDB

func (*DB) GetEventByID

func (db *DB) GetEventByID(ctx context.Context, eventID string) (nostr.Event, error)

GetEventByID retrieves a single event by its ID.

func (*DB) GetEventCount

func (db *DB) GetEventCount(ctx context.Context, filter nostr.Filter) (int64, error)

GetEventCount returns the count of events matching the given filter

func (*DB) GetEvents

func (db *DB) GetEvents(ctx context.Context, filter nostr.Filter) ([]nostr.Event, error)

GetEvents retrieves events based on Nostr filters

func (*DB) GetReplaceableEvent

func (db *DB) GetReplaceableEvent(ctx context.Context, pubkey string, kind int) (nostr.Event, error)

GetReplaceableEvent retrieves the latest replaceable event for a given pubkey and kind.

func (*DB) GetTotalEventCount

func (db *DB) GetTotalEventCount(ctx context.Context) (int64, error)

GetTotalEventCount returns the total number of events stored in the database

func (*DB) InitializeChangefeed added in v1.2.0

func (db *DB) InitializeChangefeed(ctx context.Context) error

InitializeChangefeed verifies changefeed capability for distributed event synchronization

func (*DB) InitializeSchema

func (db *DB) InitializeSchema(ctx context.Context) error

InitializeSchema creates the necessary database and tables if they don't exist

func (*DB) InsertAddressableEvent

func (db *DB) InsertAddressableEvent(ctx context.Context, evt nostr.Event) error

InsertAddressableEvent upserts (pubkey, kind, dTag) = unique

func (*DB) InsertEvent

func (db *DB) InsertEvent(ctx context.Context, evt nostr.Event) error

InsertEvent directly inserts a single event

func (*DB) InsertReplaceableEvent

func (db *DB) InsertReplaceableEvent(ctx context.Context, evt nostr.Event) error

func (*DB) Ping added in v1.3.3

func (db *DB) Ping() error

Ping checks database connectivity

func (*DB) RebuildBloomFilter

func (db *DB) RebuildBloomFilter(ctx context.Context) error

RebuildBloomFilter fetches all event IDs from CockroachDB and updates the Bloom filter.

func (*DB) SetEventDispatcher added in v1.2.0

func (db *DB) SetEventDispatcher(ed *EventDispatcher)

SetEventDispatcher sets the event dispatcher reference for immediate local broadcasting

func (*DB) StartExpiredEventsCleaner

func (db *DB) StartExpiredEventsCleaner(ctx context.Context, interval time.Duration)

StartExpiredEventsCleaner starts a background goroutine to clean expired events periodically

func (*DB) Stats added in v1.3.3

func (db *DB) Stats() DatabaseStats

Stats returns database connection pool statistics

func (*DB) VerifySchema

func (db *DB) VerifySchema(ctx context.Context) error

VerifySchema checks if all required tables exist

type DBState

type DBState int

DBState represents the current state of the database connection

const (
	DBStateInitial DBState = iota
	DBStateConnecting
	DBStateConnected
	DBStateDisconnecting
	DBStateClosed
)

type DatabaseStats added in v1.3.3

type DatabaseStats struct {
	OpenConnections    int
	InUse              int
	Idle               int
	MaxOpenConnections int
	MaxIdleConnections int
}

DatabaseStats represents database connection pool statistics

type EventDispatcher added in v1.2.0

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

EventDispatcher manages real-time event distribution across relay instances

func NewEventDispatcher added in v1.2.0

func NewEventDispatcher(db *DB) *EventDispatcher

NewEventDispatcher creates a new event dispatcher for real-time events

func (*EventDispatcher) AddClient added in v1.2.0

func (ed *EventDispatcher) AddClient(clientID string) chan *nostr.Event

AddClient registers a new client for event notifications

func (*EventDispatcher) GetClientCount added in v1.2.0

func (ed *EventDispatcher) GetClientCount() int

GetClientCount returns the number of active clients

func (*EventDispatcher) RemoveClient added in v1.2.0

func (ed *EventDispatcher) RemoveClient(clientID string)

RemoveClient unregisters a client from event notifications

func (*EventDispatcher) Start added in v1.2.0

func (ed *EventDispatcher) Start() error

Start begins listening to the changefeed and processing events

func (*EventDispatcher) Stop added in v1.2.0

func (ed *EventDispatcher) Stop()

Stop stops the event dispatcher

type EventProcessor

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

EventProcessor manages event processing with a worker pool

func NewEventProcessor

func NewEventProcessor(ctx context.Context, db *DB, bufferSize int) *EventProcessor

NewEventProcessor creates a new event processor

func (*EventProcessor) QueueDeletion

func (ep *EventProcessor) QueueDeletion(evt nostr.Event) bool

QueueDeletion is called by the validator AFTER it has verified that the deleter has the right to try. The function will:

  1. delete all owned referenced events (same pubkey)
  2. store the deletion event itself

It reuses the same retry / back‑pressure mechanism.

func (*EventProcessor) QueueEvent

func (ep *EventProcessor) QueueEvent(evt nostr.Event) bool

QueueEvent adds an event to processing queue with non-blocking behavior

func (*EventProcessor) Shutdown

func (ep *EventProcessor) Shutdown()

Shutdown gracefully stops processing

type EventRowData added in v1.2.0

type EventRowData struct {
	ID        string          `json:"id"`
	PubKey    string          `json:"pubkey"`
	CreatedAt int64           `json:"created_at"`
	Kind      int             `json:"kind"`
	Tags      json.RawMessage `json:"tags"`
	Content   string          `json:"content"`
	Sig       string          `json:"sig"`
}

EventRowData represents the event data structure from the database

func (*EventRowData) ToNostrEvent added in v1.2.0

func (e *EventRowData) ToNostrEvent() (*nostr.Event, error)

ToNostrEvent converts EventRowData to a nostr.Event

Jump to

Keyboard shortcuts

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