postgresstore

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2020 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package postgresstore implements a store that saves all the segments in a PostgreSQL database. It requires PostgreSQL >= 9.5 for "ON CONFLICT DO UPDATE" support.

Index

Constants

View Source
const (
	// Name is the name set in the store's information.
	Name = "postgres"

	// Description is the description set in the store's information.
	Description = "Stratumn's PostgreSQL Store"

	// DefaultURL is the default URL of the database.
	DefaultURL = "postgres://postgres@postgres/postgres?sslmode=disable"
)
View Source
const (
	SQLCreateLink = `` /* 171-byte string literal not displayed */

	SQLCreateLinkDegree = `
		INSERT INTO store_private.links_degree (
			link_hash,
			out_degree
		)
		VALUES ($1, 0)
	`
	SQLLockLinkDegree = `
		SELECT out_degree FROM store_private.links_degree 
		WHERE link_hash = $1 FOR UPDATE
	`
	SQLUpdateLinkDegree = `
		UPDATE store_private.links_degree SET out_degree = $2 
		WHERE link_hash = $1
	`
	SQLInitMap = `
		INSERT INTO store_private.process_maps (
			process,
			map_id
		)
		VALUES ($1, $2)
	`
	SQLAddReference = `
		INSERT INTO store_private.refs (
			link_hash,
			referenced_by
		)
		VALUES ($1, $2)
	`
	SQLReferencedBy = `
		SELECT referenced_by FROM store_private.refs
		WHERE link_hash = $1
	`
	SQLGetSegment = `` /* 142-byte string literal not displayed */

	SQLSaveValue = `
		INSERT INTO store.values (
			key,
			value
		)
		VALUES ($1, $2)
		ON CONFLICT (key)
		DO UPDATE SET
			value = $2
	`
	SQLGetValue = `
		SELECT value FROM store.values
		WHERE key = $1
	`
	SQLDeleteValue = `
		DELETE FROM store.values
		WHERE key = $1
		RETURNING value
	`
	SQLGetEvidences = `
		SELECT data FROM store.evidences
		WHERE link_hash = $1
	`
	SQLAddEvidence = `` /* 144-byte string literal not displayed */

)

Plain SQL statements. They need to be prepared before they can be used.

Variables

This section is empty.

Functions

func RegisterFlags

func RegisterFlags()

RegisterFlags registers the flags used by InitializeWithFlags.

Types

type Batch

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

Batch is the type that implements github.com/stratumn/go-core/store.Batch.

func NewBatch

func NewBatch(tx *sql.Tx) (*Batch, error)

NewBatch creates a new instance of a Postgres Batch.

func (Batch) AddEvidence added in v0.3.1

func (s Batch) AddEvidence(ctx context.Context, linkHash chainscript.LinkHash, evidence *chainscript.Evidence) error

AddEvidence implements github.com/stratumn/go-core/store.EvidenceWriter.AddEvidence.

func (b *Batch) CreateLink(ctx context.Context, link *chainscript.Link) (chainscript.LinkHash, error)

CreateLink wraps the underlying link creation and stops the batch as soon as an invalid link is encountered.

func (Batch) DeleteValue

func (s Batch) DeleteValue(ctx context.Context, key []byte) ([]byte, error)

DeleteValue implements github.com/stratumn/go-core/store.KeyValueStore.DeleteValue.

func (Batch) FindSegments

func (s Batch) FindSegments(ctx context.Context, filter *store.SegmentFilter) (*types.PaginatedSegments, error)

FindSegments implements github.com/stratumn/go-core/store.SegmentReader.FindSegments.

func (Batch) GetEvidences

func (s Batch) GetEvidences(ctx context.Context, linkHash chainscript.LinkHash) (types.EvidenceSlice, error)

GetEvidences implements github.com/stratumn/go-core/store.EvidenceReader.GetEvidences.

func (Batch) GetMapIDs

func (s Batch) GetMapIDs(ctx context.Context, filter *store.MapFilter) ([]string, error)

GetMapIDs implements github.com/stratumn/go-core/store.SegmentReader.GetMapIDs.

func (Batch) GetSegment

func (s Batch) GetSegment(ctx context.Context, linkHash chainscript.LinkHash) (*chainscript.Segment, error)

GetSegment implements github.com/stratumn/go-core/store.SegmentReader.GetSegment.

func (Batch) GetValue

func (s Batch) GetValue(ctx context.Context, key []byte) ([]byte, error)

GetValue implements github.com/stratumn/go-core/store.KeyValueStore.GetValue.

func (Batch) SetValue

func (s Batch) SetValue(ctx context.Context, key []byte, value []byte) error

SetValue implements github.com/stratumn/go-core/store.KeyValueStore.SetValue.

func (*Batch) Write

func (b *Batch) Write(ctx context.Context) (err error)

Write implements github.com/stratumn/go-core/store.Batch.Write.

type Config

type Config struct {
	// A version string that will be set in the store's information.
	Version string

	// A git commit hash that will be set in the store's information.
	Commit string

	// The URL of the PostgreSQL database, such as
	// "postgres://postgres@localhost/store?sslmode=disable".
	URL string
}

Config contains configuration options for the store.

type Info

type Info struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Version     string `json:"version"`
	Commit      string `json:"commit"`
}

Info is the info returned by GetInfo.

type SQLPreparer added in v0.3.1

type SQLPreparer interface {
	Prepare(query string) (*sql.Stmt, error)
}

SQLPreparer prepares statements.

type SQLPreparerQuerier added in v0.3.1

type SQLPreparerQuerier interface {
	SQLPreparer
	SQLQuerier
}

SQLPreparerQuerier prepares statements and executes queries.

type SQLQuerier added in v0.3.1

type SQLQuerier interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

SQLQuerier executes queries.

type SingletonTxFactory added in v0.3.1

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

SingletonTxFactory uses a single transaction under the hood. It can be used for batches since a transaction cannot contain sub-transactions.

func (*SingletonTxFactory) CommitTx added in v0.3.1

func (f *SingletonTxFactory) CommitTx(tx *sql.Tx) error

CommitTx does nothing. The owner of the SingletonTxFactory will commit directly on the tx object when ready (batch.Write()).

func (*SingletonTxFactory) NewTx added in v0.3.1

func (f *SingletonTxFactory) NewTx() (*sql.Tx, error)

NewTx returns the DB transaction. Clients should not directly call tx.Commit() or tx.Rollback().

func (*SingletonTxFactory) RollbackTx added in v0.3.1

func (f *SingletonTxFactory) RollbackTx(tx *sql.Tx, err error)

RollbackTx marks the transaction as rolled back. The owner of the SingletonTxFactory should rollback the transaction.

type StandardTxFactory added in v0.3.1

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

StandardTxFactory is just a wrapper around SQL transactions.

func (*StandardTxFactory) CommitTx added in v0.3.1

func (f *StandardTxFactory) CommitTx(tx *sql.Tx) error

CommitTx commits the transaction to the DB.

func (*StandardTxFactory) NewTx added in v0.3.1

func (f *StandardTxFactory) NewTx() (*sql.Tx, error)

NewTx creates a new DB transaction.

func (*StandardTxFactory) RollbackTx added in v0.3.1

func (f *StandardTxFactory) RollbackTx(tx *sql.Tx, _ error)

RollbackTx rolls back the transaction and logs errors.

type Store

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

Store is the type that implements github.com/stratumn/go-core/store.Adapter.

func Initialize

func Initialize(config *Config, create, drop, uniqueMapEntry bool) *Store

Initialize a postgres store adapter.

func InitializeWithFlags

func InitializeWithFlags(version, commit string) *Store

InitializeWithFlags should be called after RegisterFlags and flag.Parse to initialize a postgres adapter using flag values.

func New

func New(config *Config) (*Store, error)

New creates an instance of a Store.

func (*Store) AddEvidence

func (a *Store) AddEvidence(ctx context.Context, linkHash chainscript.LinkHash, evidence *chainscript.Evidence) error

AddEvidence implements github.com/stratumn/go-core/store.EvidenceWriter.AddEvidence.

func (*Store) AddStoreEventChannel

func (a *Store) AddStoreEventChannel(eventChan chan *store.Event)

AddStoreEventChannel implements github.com/stratumn/go-core/store.Adapter.AddStoreEventChannel

func (*Store) Close

func (a *Store) Close() error

Close closes the database connection.

func (*Store) Create

func (a *Store) Create() error

Create creates the database tables and indexes.

func (a *Store) CreateLink(ctx context.Context, link *chainscript.Link) (chainscript.LinkHash, error)

CreateLink implements github.com/stratumn/go-core/store.LinkWriter.CreateLink.

func (Store) DeleteValue

func (s Store) DeleteValue(ctx context.Context, key []byte) ([]byte, error)

DeleteValue implements github.com/stratumn/go-core/store.KeyValueStore.DeleteValue.

func (*Store) Drop

func (a *Store) Drop() error

Drop drops the database tables and indexes. It also rollbacks started batches.

func (*Store) EnforceUniqueMapEntry added in v0.3.1

func (a *Store) EnforceUniqueMapEntry() error

EnforceUniqueMapEntry makes sure each process map contains a single link without parent.

func (Store) FindSegments

func (s Store) FindSegments(ctx context.Context, filter *store.SegmentFilter) (*types.PaginatedSegments, error)

FindSegments implements github.com/stratumn/go-core/store.SegmentReader.FindSegments.

func (Store) GetEvidences

func (s Store) GetEvidences(ctx context.Context, linkHash chainscript.LinkHash) (types.EvidenceSlice, error)

GetEvidences implements github.com/stratumn/go-core/store.EvidenceReader.GetEvidences.

func (*Store) GetInfo

func (a *Store) GetInfo(ctx context.Context) (interface{}, error)

GetInfo implements github.com/stratumn/go-core/store.Adapter.GetInfo.

func (Store) GetMapIDs

func (s Store) GetMapIDs(ctx context.Context, filter *store.MapFilter) ([]string, error)

GetMapIDs implements github.com/stratumn/go-core/store.SegmentReader.GetMapIDs.

func (Store) GetSegment

func (s Store) GetSegment(ctx context.Context, linkHash chainscript.LinkHash) (*chainscript.Segment, error)

GetSegment implements github.com/stratumn/go-core/store.SegmentReader.GetSegment.

func (Store) GetValue

func (s Store) GetValue(ctx context.Context, key []byte) ([]byte, error)

GetValue implements github.com/stratumn/go-core/store.KeyValueStore.GetValue.

func (*Store) NewBatch

func (a *Store) NewBatch(ctx context.Context) (store.Batch, error)

NewBatch implements github.com/stratumn/go-core/store.Adapter.NewBatch.

func (*Store) Prepare

func (a *Store) Prepare() error

Prepare prepares the database stmts. It should be called once before interacting with segments. It assumes the tables have been created using Create().

func (Store) SetValue

func (s Store) SetValue(ctx context.Context, key []byte, value []byte) error

SetValue implements github.com/stratumn/go-core/store.KeyValueStore.SetValue.

type TxFactory added in v0.3.1

type TxFactory interface {
	NewTx() (*sql.Tx, error)
	CommitTx(tx *sql.Tx) error
	RollbackTx(tx *sql.Tx, err error)
}

TxFactory creates and manages transactions.

func NewSingletonTxFactory added in v0.3.1

func NewSingletonTxFactory(tx *sql.Tx) TxFactory

NewSingletonTxFactory creates a transaction factory using the underlying transaction. It makes sure all the work is done in a single transaction instead of many (since a transaction cannot contain nested sub-transactions).

func NewStandardTxFactory added in v0.3.1

func NewStandardTxFactory(db *sql.DB) TxFactory

NewStandardTxFactory creates a transaction factory using the underlying DB.

Jump to

Keyboard shortcuts

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