stores

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 43 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNegativeOffset      = errors.New("offset can not be negative")
	ErrNegativeMaxDowntime = errors.New("max downtime can not be negative")
)
View Source
var SQLiteTimestampFormats = []string{
	"2006-01-02 15:04:05.999999999-07:00",
	"2006-01-02T15:04:05.999999999-07:00",
	"2006-01-02 15:04:05.999999999",
	"2006-01-02T15:04:05.999999999",
	"2006-01-02 15:04:05",
	"2006-01-02T15:04:05",
	"2006-01-02 15:04",
	"2006-01-02T15:04",
	"2006-01-02",
}

SQLiteTimestampFormats were taken from github.com/mattn/go-sqlite3 and are used when parsing a string to a date

Functions

func DBConfigFromEnv

func DBConfigFromEnv() (uri, user, password, dbName string)

func NewEphemeralSQLiteConnection

func NewEphemeralSQLiteConnection(name string) gorm.Dialector

NewEphemeralSQLiteConnection creates a connection to an in-memory SQLite DB. NOTE: Use simple names such as a random hex identifier or the filepath.Base of a test's name. Certain symbols will break the cfg string and cause a file to be created on disk.

mode: set to memory for in-memory database
cache: set to shared which is required for in-memory databases
_foreign_keys: enforce foreign_key relations

func NewMetricsSQLiteConnection added in v0.7.0

func NewMetricsSQLiteConnection(path string) gorm.Dialector

NewMetricsSQLiteConnection opens a sqlite db at the given path similarly to NewSQLiteConnection but with weaker consistency guarantees since it's optimised for recording metrics.

func NewMySQLConnection

func NewMySQLConnection(user, password, addr, dbName string) gorm.Dialector

NewMySQLConnection creates a connection to a MySQL database.

func NewSQLLogger

func NewSQLLogger(l *zap.Logger, config LoggerConfig) logger.Interface

func NewSQLiteConnection

func NewSQLiteConnection(path string) gorm.Dialector

NewSQLiteConnection opens a sqlite db at the given path.

_busy_timeout: set to prevent concurrent transactions from failing and
  instead have them block
_foreign_keys: enforce foreign_key relations
_journal_mode: set to WAL instead of delete since it's usually the fastest.
  Only downside is that the db won't work on network drives. In that case this
  should be made configurable and set to TRUNCATE or any of the other options.
  For reference see https://github.com/mattn/go-sqlite3#connection-string.

Types

type Config added in v1.0.1

type Config struct {
	Conn                          gorm.Dialector
	ConnMetrics                   gorm.Dialector
	Alerts                        alerts.Alerter
	PartialSlabDir                string
	Migrate                       bool
	AnnouncementMaxAge            time.Duration
	PersistInterval               time.Duration
	WalletAddress                 types.Address
	SlabBufferCompletionThreshold int64
	Logger                        *zap.SugaredLogger
	GormLogger                    glogger.Interface
	RetryTransactionIntervals     []time.Duration
}

Config contains all params for creating a SQLStore

type ContractCommon

type ContractCommon struct {
	FCID        fileContractID `gorm:"unique;index;NOT NULL;column:fcid;size:32"`
	RenewedFrom fileContractID `gorm:"index;size:32"`

	ContractPrice  currency
	State          contractState `gorm:"index;NOT NULL;default:0"`
	TotalCost      currency
	ProofHeight    uint64 `gorm:"index;default:0"`
	RevisionHeight uint64 `gorm:"index;default:0"`
	RevisionNumber string `gorm:"NOT NULL;default:'0'"` // string since db can't store math.MaxUint64
	Size           uint64
	StartHeight    uint64 `gorm:"index;NOT NULL"`
	WindowStart    uint64 `gorm:"index;NOT NULL;default:0"`
	WindowEnd      uint64 `gorm:"index;NOT NULL;default:0"`

	// spending fields
	UploadSpending      currency
	DownloadSpending    currency
	FundAccountSpending currency
	DeleteSpending      currency
	ListSpending        currency
}

type LoggerConfig

type LoggerConfig struct {
	IgnoreRecordNotFoundError bool
	LogLevel                  logger.LogLevel
	SlowThreshold             time.Duration
}

type Model

type Model struct {
	ID        uint `gorm:"primarykey"`
	CreatedAt time.Time
}

Model defines the common fields of every table. Same as Model but excludes soft deletion since it breaks cascading deletes.

type SQLStore

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

SQLStore is a helper type for interacting with a SQL-based backend.

func NewSQLStore

func NewSQLStore(cfg Config) (*SQLStore, modules.ConsensusChangeID, error)

NewSQLStore uses a given Dialector to connect to a SQL database. NOTE: Only pass migrate=true for the first instance of SQLHostDB if you connect via the same Dialector multiple times.

func (*SQLStore) AbortMultipartUpload added in v0.6.0

func (s *SQLStore) AbortMultipartUpload(ctx context.Context, bucket, path string, uploadID string) error

func (*SQLStore) Accounts

func (s *SQLStore) Accounts(ctx context.Context) ([]api.Account, error)

Accounts returns all accounts from the db.

func (*SQLStore) AddContract

func (s *SQLStore) AddContract(ctx context.Context, c rhpv2.ContractRevision, contractPrice, totalCost types.Currency, startHeight uint64, state string) (_ api.ContractMetadata, err error)

func (*SQLStore) AddMultipartPart added in v0.6.0

func (s *SQLStore) AddMultipartPart(ctx context.Context, bucket, path, contractSet, eTag, uploadID string, partNumber int, slices []object.SlabSlice) (err error)

func (*SQLStore) AddPartialSlab

func (s *SQLStore) AddPartialSlab(ctx context.Context, data []byte, minShards, totalShards uint8, contractSet string) ([]object.SlabSlice, int64, error)

func (*SQLStore) AddRenewedContract

func (s *SQLStore) AddRenewedContract(ctx context.Context, c rhpv2.ContractRevision, contractPrice, totalCost types.Currency, startHeight uint64, renewedFrom types.FileContractID, state string) (api.ContractMetadata, error)

AddRenewedContract adds a new contract which was created as the result of a renewal to the store. The old contract specified as 'renewedFrom' will be deleted from the active contracts and moved to the archive. Both new and old contract will be linked to each other through the RenewedFrom and RenewedTo fields respectively.

func (*SQLStore) AddWebhook

func (s *SQLStore) AddWebhook(wb webhooks.Webhook) error

func (*SQLStore) AncestorContracts

func (s *SQLStore) AncestorContracts(ctx context.Context, id types.FileContractID, startHeight uint64) ([]api.ArchivedContract, error)

func (*SQLStore) ArchiveAllContracts

func (s *SQLStore) ArchiveAllContracts(ctx context.Context, reason string) error

func (*SQLStore) ArchiveContract

func (s *SQLStore) ArchiveContract(ctx context.Context, id types.FileContractID, reason string) error

func (*SQLStore) ArchiveContracts

func (s *SQLStore) ArchiveContracts(ctx context.Context, toArchive map[types.FileContractID]string) error

func (*SQLStore) Autopilot

func (s *SQLStore) Autopilot(ctx context.Context, id string) (api.Autopilot, error)

func (*SQLStore) Autopilots

func (s *SQLStore) Autopilots(ctx context.Context) ([]api.Autopilot, error)

func (*SQLStore) Bucket added in v0.6.0

func (s *SQLStore) Bucket(ctx context.Context, bucket string) (api.Bucket, error)

func (*SQLStore) Close

func (s *SQLStore) Close() error

Close closes the underlying database connection of the store.

func (*SQLStore) CompleteMultipartUpload added in v0.6.0

func (s *SQLStore) CompleteMultipartUpload(ctx context.Context, bucket, path string, uploadID string, parts []api.MultipartCompletedPart) (_ api.MultipartCompleteResponse, err error)

func (*SQLStore) Contract

func (*SQLStore) ContractMetrics added in v0.7.0

func (s *SQLStore) ContractMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.ContractMetricsQueryOpts) ([]api.ContractMetric, error)

func (*SQLStore) ContractPruneMetrics added in v0.7.0

func (s *SQLStore) ContractPruneMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.ContractPruneMetricsQueryOpts) ([]api.ContractPruneMetric, error)

func (*SQLStore) ContractRoots

func (s *SQLStore) ContractRoots(ctx context.Context, id types.FileContractID) (roots []types.Hash256, err error)

func (*SQLStore) ContractSetChurnMetrics added in v0.7.0

func (s *SQLStore) ContractSetChurnMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.ContractSetChurnMetricsQueryOpts) ([]api.ContractSetChurnMetric, error)

func (*SQLStore) ContractSetMetrics added in v0.7.0

func (s *SQLStore) ContractSetMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.ContractSetMetricsQueryOpts) ([]api.ContractSetMetric, error)

func (*SQLStore) ContractSets

func (s *SQLStore) ContractSets(ctx context.Context) ([]string, error)

func (*SQLStore) ContractSize

func (s *SQLStore) ContractSize(ctx context.Context, id types.FileContractID) (api.ContractSize, error)

func (*SQLStore) ContractSizes

func (s *SQLStore) ContractSizes(ctx context.Context) (map[types.FileContractID]api.ContractSize, error)

func (*SQLStore) Contracts

func (s *SQLStore) Contracts(ctx context.Context, opts api.ContractsOpts) ([]api.ContractMetadata, error)

func (*SQLStore) CopyObject added in v0.6.0

func (s *SQLStore) CopyObject(ctx context.Context, srcBucket, dstBucket, srcPath, dstPath, mimeType string, metadata api.ObjectUserMetadata) (om api.ObjectMetadata, err error)

func (*SQLStore) CreateBucket added in v0.6.0

func (s *SQLStore) CreateBucket(ctx context.Context, bucket string, policy api.BucketPolicy) error

func (*SQLStore) CreateMultipartUpload added in v0.6.0

func (s *SQLStore) CreateMultipartUpload(ctx context.Context, bucket, path string, ec object.EncryptionKey, mimeType string, metadata api.ObjectUserMetadata) (api.MultipartCreateResponse, error)

func (*SQLStore) DeleteBucket added in v0.6.0

func (s *SQLStore) DeleteBucket(ctx context.Context, bucket string) error

func (*SQLStore) DeleteHostSector added in v0.7.0

func (s *SQLStore) DeleteHostSector(ctx context.Context, hk types.PublicKey, root types.Hash256) (int, error)

func (*SQLStore) DeleteSetting

func (s *SQLStore) DeleteSetting(ctx context.Context, key string) error

DeleteSetting implements the bus.SettingStore interface.

func (*SQLStore) DeleteWebhook

func (s *SQLStore) DeleteWebhook(wb webhooks.Webhook) error

func (*SQLStore) FetchPartialSlab

func (s *SQLStore) FetchPartialSlab(ctx context.Context, ec object.EncryptionKey, offset, length uint32) ([]byte, error)

func (*SQLStore) Height

func (s *SQLStore) Height() uint64

func (*SQLStore) Host

func (ss *SQLStore) Host(ctx context.Context, hostKey types.PublicKey) (hostdb.HostInfo, error)

Host returns information about a host.

func (*SQLStore) HostAllowlist

func (ss *SQLStore) HostAllowlist(ctx context.Context) (allowlist []types.PublicKey, err error)

func (*SQLStore) HostBlocklist

func (ss *SQLStore) HostBlocklist(ctx context.Context) (blocklist []string, err error)

func (*SQLStore) Hosts

func (ss *SQLStore) Hosts(ctx context.Context, offset, limit int) ([]hostdb.Host, error)

Hosts returns non-blocked hosts at given offset and limit.

func (*SQLStore) HostsForScanning

func (ss *SQLStore) HostsForScanning(ctx context.Context, maxLastScan time.Time, offset, limit int) ([]hostdb.HostAddress, error)

HostsForScanning returns the address of hosts for scanning.

func (*SQLStore) ListBuckets added in v0.6.0

func (s *SQLStore) ListBuckets(ctx context.Context) ([]api.Bucket, error)

func (*SQLStore) ListObjects added in v0.6.0

func (s *SQLStore) ListObjects(ctx context.Context, bucket, prefix, sortBy, sortDir, marker string, limit int) (api.ObjectsListResponse, error)

TODO: we can use ObjectEntries instead of ListObject if we want to use '/' as a delimiter for now (see backend.go) but it would be interesting to have arbitrary 'delim' support in ListObjects.

func (*SQLStore) MarkPackedSlabsUploaded

func (s *SQLStore) MarkPackedSlabsUploaded(ctx context.Context, slabs []api.UploadedPackedSlab) error

MarkPackedSlabsUploaded marks the given slabs as uploaded and deletes them from the buffer.

func (*SQLStore) MultipartUpload added in v0.6.0

func (s *SQLStore) MultipartUpload(ctx context.Context, uploadID string) (resp api.MultipartUpload, err error)

func (*SQLStore) MultipartUploadParts added in v0.6.0

func (s *SQLStore) MultipartUploadParts(ctx context.Context, bucket, object string, uploadID string, marker int, limit int64) (resp api.MultipartListPartsResponse, _ error)

func (*SQLStore) MultipartUploads added in v0.6.0

func (s *SQLStore) MultipartUploads(ctx context.Context, bucket, prefix, keyMarker, uploadIDMarker string, limit int) (resp api.MultipartListUploadsResponse, err error)

func (*SQLStore) Object

func (s *SQLStore) Object(ctx context.Context, bucket, path string) (obj api.Object, err error)

func (*SQLStore) ObjectEntries

func (s *SQLStore) ObjectEntries(ctx context.Context, bucket, path, prefix, sortBy, sortDir, marker string, offset, limit int) (metadata []api.ObjectMetadata, hasMore bool, err error)

func (*SQLStore) ObjectMetadata added in v1.0.6

func (s *SQLStore) ObjectMetadata(ctx context.Context, bucket, path string) (api.Object, error)

ObjectMetadata returns an object's metadata

func (*SQLStore) ObjectsBySlabKey

func (s *SQLStore) ObjectsBySlabKey(ctx context.Context, bucket string, slabKey object.EncryptionKey) (metadata []api.ObjectMetadata, err error)

func (*SQLStore) ObjectsStats

func (s *SQLStore) ObjectsStats(ctx context.Context, opts api.ObjectsStatsOpts) (api.ObjectsStatsResponse, error)

ObjectsStats returns some info related to the objects stored in the store. To reduce locking and make sure all results are consistent, everything is done within a single transaction.

func (*SQLStore) PackedSlabsForUpload

func (s *SQLStore) PackedSlabsForUpload(ctx context.Context, lockingDuration time.Duration, minShards, totalShards uint8, set string, limit int) ([]api.PackedSlab, error)

PackedSlabsForUpload returns up to 'limit' packed slabs that are ready for uploading. They are locked for 'lockingDuration' time before being handed out again.

func (*SQLStore) PerformanceMetrics added in v0.7.0

func (s *SQLStore) PerformanceMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.PerformanceMetricsQueryOpts) ([]api.PerformanceMetric, error)

func (*SQLStore) ProcessConsensusChange

func (ss *SQLStore) ProcessConsensusChange(cc modules.ConsensusChange)

ProcessConsensusChange implements consensus.Subscriber.

func (*SQLStore) PruneMetrics added in v0.7.0

func (s *SQLStore) PruneMetrics(ctx context.Context, metric string, cutoff time.Time) error

func (*SQLStore) RecordContractMetric added in v0.7.0

func (s *SQLStore) RecordContractMetric(ctx context.Context, metrics ...api.ContractMetric) error

func (*SQLStore) RecordContractPruneMetric added in v0.7.0

func (s *SQLStore) RecordContractPruneMetric(ctx context.Context, metrics ...api.ContractPruneMetric) error

func (*SQLStore) RecordContractSetChurnMetric added in v0.7.0

func (s *SQLStore) RecordContractSetChurnMetric(ctx context.Context, metrics ...api.ContractSetChurnMetric) error

func (*SQLStore) RecordContractSetMetric added in v0.7.0

func (s *SQLStore) RecordContractSetMetric(ctx context.Context, metrics ...api.ContractSetMetric) error

func (*SQLStore) RecordContractSpending

func (s *SQLStore) RecordContractSpending(ctx context.Context, records []api.ContractSpendingRecord) error

func (*SQLStore) RecordHostScans

func (ss *SQLStore) RecordHostScans(ctx context.Context, scans []hostdb.HostScan) error

func (*SQLStore) RecordPerformanceMetric added in v0.7.0

func (s *SQLStore) RecordPerformanceMetric(ctx context.Context, metrics ...api.PerformanceMetric) error

func (*SQLStore) RecordPriceTables

func (ss *SQLStore) RecordPriceTables(ctx context.Context, priceTableUpdate []hostdb.PriceTableUpdate) error

func (*SQLStore) RecordWalletMetric added in v0.7.0

func (s *SQLStore) RecordWalletMetric(ctx context.Context, metrics ...api.WalletMetric) error

func (*SQLStore) RefreshHealth

func (s *SQLStore) RefreshHealth(ctx context.Context) error

func (*SQLStore) RemoveContractSet

func (s *SQLStore) RemoveContractSet(ctx context.Context, name string) error

func (*SQLStore) RemoveObject

func (s *SQLStore) RemoveObject(ctx context.Context, bucket, key string) error

func (*SQLStore) RemoveObjects

func (s *SQLStore) RemoveObjects(ctx context.Context, bucket, prefix string) error

func (*SQLStore) RemoveOfflineHosts

func (ss *SQLStore) RemoveOfflineHosts(ctx context.Context, minRecentFailures uint64, maxDowntime time.Duration) (removed uint64, err error)

func (*SQLStore) RenameObject

func (s *SQLStore) RenameObject(ctx context.Context, bucket, keyOld, keyNew string, force bool) error

func (*SQLStore) RenameObjects

func (s *SQLStore) RenameObjects(ctx context.Context, bucket, prefixOld, prefixNew string, force bool) error

func (*SQLStore) RenewedContract

func (s *SQLStore) RenewedContract(ctx context.Context, renewedFrom types.FileContractID) (_ api.ContractMetadata, err error)

func (*SQLStore) ResetConsensusSubscription

func (s *SQLStore) ResetConsensusSubscription() error

func (*SQLStore) ResetLostSectors added in v0.7.0

func (s *SQLStore) ResetLostSectors(ctx context.Context, hk types.PublicKey) error

func (*SQLStore) SaveAccounts

func (s *SQLStore) SaveAccounts(ctx context.Context, accounts []api.Account) error

SaveAccounts saves the given accounts in the db, overwriting any existing ones.

func (*SQLStore) SearchHosts

func (ss *SQLStore) SearchHosts(ctx context.Context, filterMode, addressContains string, keyIn []types.PublicKey, offset, limit int) ([]hostdb.Host, error)

func (*SQLStore) SearchObjects

func (s *SQLStore) SearchObjects(ctx context.Context, bucket, substring string, offset, limit int) ([]api.ObjectMetadata, error)

func (*SQLStore) SetContractSet

func (s *SQLStore) SetContractSet(ctx context.Context, name string, contractIds []types.FileContractID) error

func (*SQLStore) SetUncleanShutdown added in v0.6.0

func (s *SQLStore) SetUncleanShutdown() error

SetCleanShutdown sets the clean shutdown flag on the accounts to 'false' and also sets the 'requires_sync' flag. That way, the autopilot will know to sync all accounts after an unclean shutdown and the bus will know not to apply drift.

func (*SQLStore) Setting

func (s *SQLStore) Setting(ctx context.Context, key string) (string, error)

Setting implements the bus.SettingStore interface.

func (*SQLStore) Settings

func (s *SQLStore) Settings(ctx context.Context) ([]string, error)

Settings implements the bus.SettingStore interface.

func (*SQLStore) Slab

func (*SQLStore) SlabBuffers

func (s *SQLStore) SlabBuffers(ctx context.Context) ([]api.SlabBuffer, error)

func (*SQLStore) Transactions

func (s *SQLStore) Transactions(before, since time.Time, offset, limit int) ([]wallet.Transaction, error)

Transactions implements wallet.SingleAddressStore.

func (*SQLStore) UnhealthySlabs

func (s *SQLStore) UnhealthySlabs(ctx context.Context, healthCutoff float64, set string, limit int) ([]api.UnhealthySlab, error)

UnhealthySlabs returns up to 'limit' slabs that do not reach full redundancy in the given contract set. These slabs need to be migrated to good contracts so they are restored to full health.

func (*SQLStore) UnspentSiacoinElements

func (s *SQLStore) UnspentSiacoinElements(matured bool) ([]wallet.SiacoinElement, error)

UnspentSiacoinElements implements wallet.SingleAddressStore.

func (*SQLStore) UpdateAutopilot

func (s *SQLStore) UpdateAutopilot(ctx context.Context, ap api.Autopilot) error

func (*SQLStore) UpdateBucketPolicy added in v0.6.0

func (s *SQLStore) UpdateBucketPolicy(ctx context.Context, bucket string, policy api.BucketPolicy) error

func (*SQLStore) UpdateHostAllowlistEntries

func (ss *SQLStore) UpdateHostAllowlistEntries(ctx context.Context, add, remove []types.PublicKey, clear bool) (err error)

func (*SQLStore) UpdateHostBlocklistEntries

func (ss *SQLStore) UpdateHostBlocklistEntries(ctx context.Context, add, remove []string, clear bool) (err error)

func (*SQLStore) UpdateObject

func (s *SQLStore) UpdateObject(ctx context.Context, bucket, path, contractSet, eTag, mimeType string, metadata api.ObjectUserMetadata, o object.Object) error

func (*SQLStore) UpdateSetting

func (s *SQLStore) UpdateSetting(ctx context.Context, key, value string) error

UpdateSetting implements the bus.SettingStore interface.

func (*SQLStore) UpdateSlab

func (ss *SQLStore) UpdateSlab(ctx context.Context, s object.Slab, contractSet string) error

func (*SQLStore) WalletMetrics added in v0.7.0

func (s *SQLStore) WalletMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.WalletMetricsQueryOpts) ([]api.WalletMetric, error)

func (*SQLStore) Webhooks

func (s *SQLStore) Webhooks() ([]webhooks.Webhook, error)

type SlabBuffer

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

type SlabBufferManager

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

func (*SlabBufferManager) AddPartialSlab

func (mgr *SlabBufferManager) AddPartialSlab(ctx context.Context, data []byte, minShards, totalShards uint8, contractSet uint) ([]object.SlabSlice, int64, error)

func (*SlabBufferManager) BufferSize added in v0.6.0

func (mgr *SlabBufferManager) BufferSize(gid bufferGroupID) (total int64)

func (*SlabBufferManager) Close

func (mgr *SlabBufferManager) Close() error

func (*SlabBufferManager) FetchPartialSlab

func (mgr *SlabBufferManager) FetchPartialSlab(ctx context.Context, ec object.EncryptionKey, offset, length uint32) ([]byte, error)

func (*SlabBufferManager) RemoveBuffers

func (mgr *SlabBufferManager) RemoveBuffers(fileNames ...string)

func (*SlabBufferManager) SlabBuffers

func (mgr *SlabBufferManager) SlabBuffers() (sbs []api.SlabBuffer)

func (*SlabBufferManager) SlabsForUpload

func (mgr *SlabBufferManager) SlabsForUpload(ctx context.Context, lockingDuration time.Duration, minShards, totalShards uint8, set uint, limit int) (slabs []api.PackedSlab, _ error)

Jump to

Keyboard shortcuts

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