database

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// MongoDefaultTimeout is the timeout for the context used in testing
	// whenever a context is sent to mongo
	MongoDefaultTimeout = time.Minute
)

Variables

View Source
var (
	// ErrDuplicateKey is returned when an insert is attempted that violates the
	// unique constraint on a certain field.
	ErrDuplicateKey = errors.New("E11000 duplicate key")

	// ErrIndexCreateFailed is returned when an error occurred when trying to
	// ensure an index
	ErrIndexCreateFailed = errors.New("failed to create index")

	// ErrIndexDropFailed is returned when an error occurred when trying to
	// drop an index
	ErrIndexDropFailed = errors.New("failed to drop an index")

	// ErrNoDocumentsFound is returned when a database operation completes
	// successfully but it doesn't find or affect any documents.
	ErrNoDocumentsFound = errors.New("no documents")

	// ErrNoEntriesUpdated is returned when no entries were updated after an
	// update was performed.
	ErrNoEntriesUpdated = errors.New("no entries updated")

	// ErrSkylinkExists is returned when we try to add a skylink to the database
	// and it already exists there.
	ErrSkylinkExists = errors.New("skylink already exists")

	// ServerUID is a random string that uniquely identifies the server
	ServerUID string

	// True is a helper value, so we can pass a *bool to MongoDB's methods.
	True = true
)

Functions

This section is empty.

Types

type AllowListedSkylink struct {
	ID             primitive.ObjectID `bson:"_id,omitempty"`
	Hash           Hash               `bson:"hash"`
	Description    string             `bson:"description"`
	TimestampAdded time.Time          `bson:"timestamp_added"`
}

AllowListedSkylink is a skylink that is allow listed and thus prohibited from ever being blocked.

type BlockedSkylink struct {
	ID                primitive.ObjectID `bson:"_id,omitempty"`
	Failed            bool               `bson:"failed"`
	Hash              Hash               `bson:"hash"`
	Invalid           bool               `bson:"invalid"`
	Reporter          Reporter           `bson:"reporter"`
	Reverted          bool               `bson:"reverted"`
	RevertedTags      []string           `bson:"reverted_tags"`
	Tags              []string           `bson:"tags"`
	TimestampAdded    time.Time          `bson:"timestamp_added"`
	TimestampReverted time.Time          `bson:"timestamp_reverted"`
}

BlockedSkylink is a skylink blocked by an external request.

func (*BlockedSkylink) Validate

func (bsl *BlockedSkylink) Validate() error

Validate is a small helper function that ensures the required properties are set on the BlockedSkylink object.

type DB

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

DB holds a connection to the database, as well as helpful shortcuts to collections and utilities.

NOTE: update the 'Purge' method when adding new collections

func New

func New(ctx context.Context, uri string, creds options.Credential, logger *logrus.Logger) (*DB, error)

New creates a new database connection.

func NewCustomDB

func NewCustomDB(ctx context.Context, uri string, dbName string, creds options.Credential, logger *logrus.Logger) (*DB, error)

NewCustomDB creates a new database connection to a database with a custom name.

func NewTestDB

func NewTestDB(ctx context.Context, dbName string) *DB

NewTestDB returns a test database, the database gets purged and on error we panic.

func (*DB) BlockedHashes

func (db *DB) BlockedHashes(ctx context.Context, sort, skip, limit int) ([]BlockedSkylink, bool, error)

BlockedHashes allows to pass a skip and limit parameter and returns an array of blocked hashes alongside a boolean that indicates whether there's more documents after the current 'page'.

func (*DB) Close

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

Close disconnects the db.

func (db *DB) CreateAllowListedSkylink(ctx context.Context, skylink *AllowListedSkylink) error

CreateAllowListedSkylink creates a new allowlisted skylink. If the skylink already exists it does nothing and returns without failure.

func (db *DB) CreateBlockedSkylink(ctx context.Context, skylink *BlockedSkylink) error

CreateBlockedSkylink creates a new skylink. If the skylink already exists it returns ErrSkylinkExists.

func (*DB) CreateBlockedSkylinkBulk

func (db *DB) CreateBlockedSkylinkBulk(ctx context.Context, skylinks []BlockedSkylink) (int, error)

CreateBlockedSkylinkBulk creates new blocked skylinks in bulk. It returns the number of created entries.

func (*DB) FindByHash

func (db *DB) FindByHash(ctx context.Context, hash Hash) (*BlockedSkylink, error)

FindByHash fetches the DB record that corresponds to the given hash from the database.

func (*DB) HashesToBlock

func (db *DB) HashesToBlock(ctx context.Context, from time.Time) ([]Hash, error)

HashesToBlock sweeps the database for unblocked hashes after the given timestamp.

func (*DB) HashesToRetry

func (db *DB) HashesToRetry(ctx context.Context) ([]Hash, error)

HashesToRetry returns all hashes that failed to get blocked the first time around. This is a retry mechanism to ensure we keep retrying to block those hashes, but at the same try 'unblock' the main block loop in order for it to run smoothly.

func (*DB) IsAllowListed

func (db *DB) IsAllowListed(ctx context.Context, hash crypto.Hash) (bool, error)

IsAllowListed returns whether the given skylink is on the allow list.

func (*DB) MarkFailed

func (db *DB) MarkFailed(ctx context.Context, hashes []Hash) error

MarkFailed will mark the given documents as failed

func (*DB) MarkInvalid

func (db *DB) MarkInvalid(ctx context.Context, hashes []Hash) error

MarkInvalid will mark the given documents as invalid

func (*DB) MarkSucceeded

func (db *DB) MarkSucceeded(ctx context.Context, hashes []Hash) error

MarkSucceeded will toggle the failed flag for all documents in the given list of hashes that are currently marked as failed.

func (*DB) Ping

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

Ping sends a ping command to verify that the client can connect to the DB and specifically to the primary.

func (*DB) Purge

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

Purge deletes all documents from all collections in the database

NOTE: this function should never be called in production and should only be used for testing purposes

type Hash

type Hash struct {
	crypto.Hash
}

Hash is a struct that embeds the crypto.Hash, allowing us to implement the bsoncodec ValueMarshaler interfaces.

func DiffHashes

func DiffHashes(array []Hash, others ...[]Hash) []Hash

DiffHashes is a helper function that returns an array of hashes that are part of the base array but are not present in any of the other arrays.

func HashBytes

func HashBytes(b []byte) Hash

HashBytes returns the Hash of the given bytes.

func NewHash

func NewHash(sl skymodules.Skylink) Hash

NewHash returns the Hash of the given skylink.

func (Hash) MarshalBSONValue

func (h Hash) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bsoncodec.ValueMarshaler interface.

func (*Hash) UnmarshalBSONValue

func (h *Hash) UnmarshalBSONValue(t bsontype.Type, b []byte) error

UnmarshalBSONValue implements the bsoncodec.ValueUnmarshaler interface.

type Reporter

type Reporter struct {
	Name            string `bson:"name"`
	Email           string `bson:"email"`
	OtherContact    string `bson:"other_contact"`
	Sub             string `bson:"sub,omitempty"`
	Unauthenticated bool   `bson:"unauthenticated,omitempty"`
}

Reporter is a person who reported that a given skylink should be blocked.

Jump to

Keyboard shortcuts

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