storage

package
v0.0.0-...-04c462b Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Copyright 2022 The Gidari Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Copyright 2022 The Gidari Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Copyright 2022 The Gidari Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Copyright 2022 The Gidari Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Copyright 2022 The Gidari Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Index

Constants

View Source
const (
	// MongoType is the byte representation of a mongo database.
	MongoType uint8 = iota

	// PostgresType is the byte representation of a postgres database.
	PostgresType
)

Variables

View Source
var (
	ErrDNSNotSupported     = fmt.Errorf("dns is not supported")
	ErrTransactionNotFound = fmt.Errorf("transaction not found")
	ErrNoTables            = fmt.Errorf("no tables found")
	ErrTransactionAborted  = fmt.Errorf("transaction aborted")
)

Functions

func DNSNotSupportedError

func DNSNotSupportedError(dns string) error

DNSNotSupported wraps an error with ErrDNSNotSupported.

func Scheme

func Scheme(t uint8) string

Scheme takes a byte and returns the associated DNS root database resource.

Types

type Mongo

type Mongo struct {
	*mongo.Client
	// contains filtered or unexported fields
}

Mongo is a wrapper for *mongo.Client, use to perform CRUD operations on a mongo DB instance.

func NewMongo

func NewMongo(ctx context.Context, uri string) (*Mongo, error)

NewMongo will return a new mongo client that can be used to perform CRUD operations on a mongo DB instance. This constructor uses a URI to make the client connection, and the URI is of the form Mongo://username:password@host:port

func (*Mongo) Close

func (m *Mongo) Close()

Close will close the mongo client.

func (*Mongo) IsNoSQL

func (m *Mongo) IsNoSQL() bool

IsNoSQL returns "true" indicating that the "MongoDB" database is NoSQL.

func (*Mongo) ListPrimaryKeys

func (m *Mongo) ListPrimaryKeys(ctx context.Context) (*proto.ListPrimaryKeysResponse, error)

ListPrimaryKeys will return a "proto.ListPrimaryKeysResponse" containing a list of primary keys data for all tables in a database. MongoDB does not have a concept of primary keys, so we will return the "_id" field as the primary key for all collections in the database associated with the underlying connection string.

func (*Mongo) ListTables

func (m *Mongo) ListTables(ctx context.Context) (*proto.ListTablesResponse, error)

ListTables will return a list of all tables in the MongoDB database.

func (*Mongo) StartTx

func (m *Mongo) StartTx(ctx context.Context) (*Txn, error)

StartTx will start a mongodb session where all data from write methods can be rolled back.

MongoDB best practice is to "abort any multi-document transactions that runs for more than 60 seconds". The resulting error for exceeding this time constraint is "TransactionExceededLifetimeLimitSeconds". To maintain agnostism at the repository layer, we implement the logic to handle these transactions errors in the storage layer. Therefore, every 60 seconds, the transacting data will be committed commit the transaction and start a new one.

func (*Mongo) Truncate

Truncate will delete all records in a collection.

func (*Mongo) Type

func (m *Mongo) Type() uint8

Type returns the type of storage.

func (*Mongo) Upsert

func (m *Mongo) Upsert(ctx context.Context, req *proto.UpsertRequest) (*proto.UpsertResponse, error)

Upsert will insert or update a record in a collection.

type Postgres

type Postgres struct {
	*sql.DB
	// contains filtered or unexported fields
}

Postgres is a wrapper around the sql.DB object.

func NewPostgres

func NewPostgres(ctx context.Context, connectionURL string) (*Postgres, error)

NewPostgres will return a new Postgres option for querying data through a Postgres DB.

func (*Postgres) Close

func (pg *Postgres) Close()

Close will close the underlying database / transaction.

func (*Postgres) IsNoSQL

func (pg *Postgres) IsNoSQL() bool

IsNoSQL returns "false" to indicate that "Postgres" is not a NoSQL database.

func (*Postgres) ListColumns

func (pg *Postgres) ListColumns(ctx context.Context) (*proto.ListColumnsResponse, error)

ListColumns will set a complete list of available columns per table on the response.

func (*Postgres) ListPrimaryKeys

func (pg *Postgres) ListPrimaryKeys(ctx context.Context) (*proto.ListPrimaryKeysResponse, error)

ListPrimaryKeys will list all primary keys for all of the tables in the database defined by the DNS used to create the postgres instance.

func (*Postgres) ListTables

func (pg *Postgres) ListTables(ctx context.Context) (*proto.ListTablesResponse, error)

ListTables will set a complete list of available tables on the response.

func (*Postgres) StartTx

func (pg *Postgres) StartTx(ctx context.Context) (*Txn, error)

StartTx will start a transaction on the Postgres connection. The transaction ID is returned and should be used to commit or rollback the transaction.

func (*Postgres) Truncate

Truncate will truncate a table.

func (*Postgres) Type

func (pg *Postgres) Type() uint8

Type implements the storage interface.

func (*Postgres) Upsert

Upsert will insert the records on the request if they do not exist in the database. On conflict, it will use the PK on the request record to update the data in the database. An upsert request will update the entire table for a given record, include fields that have not been set directly.

type Service

type Service struct {
	Storage
}

Service is a wrapper for a Storage implementation.

func New

func New(ctx context.Context, dns string) (*Service, error)

New will attempt to return a generic storage object given a DNS.

type Storage

type Storage interface {
	// Close will disconnect the storage device.
	Close()

	// ListPrimaryKeys will return a list of primary keys for all tables in the database.
	ListPrimaryKeys(ctx context.Context) (*proto.ListPrimaryKeysResponse, error)

	// ListTables will return a list of all tables in the database.
	ListTables(ctx context.Context) (*proto.ListTablesResponse, error)

	// IsNoSQL will return true if the storage device is a NoSQL database.
	IsNoSQL() bool

	// StartTx will start a transaction and return a "Tx" object that can be used to put operations on a channel,
	// commit the result of all operations sent to the transaction, or rollback the result of all operations sent
	// to the transaction.
	StartTx(context.Context) (*Txn, error)

	// Truncate will delete all data from the storage device for ast list of tables.
	Truncate(context.Context, *proto.TruncateRequest) (*proto.TruncateResponse, error)

	// Type returns the type of storage device.
	Type() uint8

	// Upsert will insert or update a batch of records in the storage device.
	Upsert(context.Context, *proto.UpsertRequest) (*proto.UpsertResponse, error)
}

Storage is an interface that defines the methods that a storage device should implement.

type Transactor

type Transactor interface {
	Commit() error
	Rollback() error
	Send(TxnChanFn)
}

Transactor is an interface that can be used to perform CRUD operations within the context of a database transaction.

type Txn

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

Txn is a wrapper for a mongo session that can be used to perform CRUD operations on a mongo DB instance.

func (*Txn) Commit

func (txn *Txn) Commit() error

Commit will commit the transaction.

func (*Txn) Rollback

func (txn *Txn) Rollback() error

Rollback will rollback the transaction.

func (*Txn) Send

func (txn *Txn) Send(fn TxnChanFn)

Send will send a function to the transaction channel.

type TxnChanFn

type TxnChanFn func(context.Context, Storage) error

TxnChanFn is a function that will be sent to the transaction channel.

Jump to

Keyboard shortcuts

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