storage

package
v0.0.0-...-51d629d Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2016 License: BSD-3-Clause, BSD-3-Clause Imports: 4 Imported by: 0

README

Storage drivers must follow these rules in order to work properly:

  1. They must follow the gdriver interface and have: New() Identify(ident)

  2. They must follow the StorageDriver interface (defined in storageInterface.go) for a minimum list of requirements.

  3. Each driver has an interface layer (Store) that acts as an interface layer:

CALLER ----> Store -----> Driver

Store defines any possible call. It then casts the selected Driver to the inteface. for example, if you are calling Ping() it will peform callPing, ok := store.Driver.(Ping) and if if fails then Ping() is not implemented by the driver. If it succeeds then it calls the cast Ping class.

  1. The driver (identified within the DriverStructure) must have one function: Open():

    func (t DriverStructure) Open(dsnConnect string, options string) (storage.Conn, error)

     The string 'options' is set in the configuration and passed to the open call. It can be ignored or
     provide extra information to use within the driver.
    

    The 'storage.conn' is an interface that must implement the following calls:

     UserUpdate(user *record.User) error
     UserInsert(user *record.User) error
    
     UserFetch(key, value string) (*record.User, error)
    

    It may optionally implement: CreateStore() error If implemented, it should create any directory/resource/tables required for the driver to save information. If possible, it should be non-destructive, i.e., it should not delete or destroy anything in the system.

     Close() error
         Close will allow you to close any connection to the database/file that has been created.
         Close should not return an error if it is called multiple times and there is nothing to
         close. (Meaning, keep the state in the driver and don't force service routines to track
         if it needs to be closed or not.)
    
     Reset()
         Reset should reset any errors and cleanup any data, for example if you have implemented any
         cursor reads. Calling Reset will never return an error.
    
     Ping() error
         Ping can be used to see if the data store connection is alive. Ping should only return
         an error if there is a data store connection error.
    
    	Release() error
    	    Release should release any locks/resources that have been set. This is useful for file locks
    	    or database record locks. Note that, if implemented, the function MUST be able to be called
    	    any number of times even if no lock/resources need to be released. The state of resources to
    	    be released must be kept by the driver, not by the caller.
    
  2. All functions from all classes must return errors of the type defined by ecode.ErrorCoder. If you want to return additional information, for example status or field information, you should create another interface that implements the same as ErrorCoder but with additional fields.

  3. The record.User level functions manipulates the in-memory image of a user. It needs to perform any operations that will alter or set information for each service call.

  4. The store-level driver has very few operations: Insert, Update and Fetch. Records are not deleted by GUS but can be marked for deletion instead by the record/User level. The driver is only responsible for mapping the record-level fields back and forth with the database level fields.

  5. The driver-level interface has 'aliases' for Fetches (e.g. UserFetchByGuid) that make calling the lower routines a little bit easier. These routines will call the single UserFetch function with extra parameters.

Documentation

Overview

Package storage provides field encryption for any driver. The encryption should be enabled using the driver options

Index

Constants

View Source
const (
	FieldEmail = `Email`
	FieldName  = `FullName`
	FieldGUID  = `Guid`
	FieldLogin = `LoginName`
	FieldToken = `Token`
)

These are the names of fields we expect to occur in the database and will pass to database functions when performing UserFetch operations. You may map them in the driver-level routines in order to provide names that are more appropriate to the driver mechanism.

View Source
const DriverGroup = "storage"

DriverGroup defines a logical grouping for the drivers

View Source
const MatchAnyDomain = "*"

MatchAnyDomain is a special character that should be used to search ALL domains.

Variables

This section is empty.

Functions

This section is empty.

Types

type Closer

type Closer interface {
	Close() error
}

Closer is an optional interface. If this isn't implemented, no error is reported.

type Conn

type Conn interface {
	UserUpdate(user *tenant.User) error
	UserInsert(user *tenant.User) error

	UserFetch(domain, key, value string) (*tenant.User, error)
}

Conn has a minimum call set that every driver is required to implement

type Creater

type Creater interface {
	CreateStore() error
}

Creater is an optional Storge Creation interface

type Encrypter

type Encrypter interface {
	Encrypt(string) string
	Decrypt(string) string

	SetKey(key string)
}

Encrypter provides the interface that storage classes need to support encryption

type HeaderMap

type HeaderMap map[string]string

func NewHeaderMap

func NewHeaderMap(source string) HeaderMap

func (*HeaderMap) ToString

func (h *HeaderMap) ToString() string

type Opener

type Opener interface {
	Open(connect string, extraDriverOptions string) (Conn, error)
}

Opener is an interface that will open up the storage driver for stroing data

type Pinger

type Pinger interface {
	Ping() error
}

Pinger is an optional database 'ping' interface. This will check the database connection

type Releaser

type Releaser interface {
	Release() error
}

Releaser optional interface. This will release any locks/resources that a driver may have set For example, the MySQL will do a SELECT...FOR UPDATE for all of the FetchXXX calls. The release will cause an explicit commit. This, in the code, will be called by a 'defer' call after any fetch/insert operation. For other drivers, it can be ignored or perform any other operation required. Note that SQLITE doesn't do anything at this stage as it isn't really considered a robust, fully hardened storage mechanism. Document-style interfaces will probably not use it either.

type Reseter

type Reseter interface {
	Reset()
}

Reseter is an optional interface. This will reset any errors and cleanup any intermediate results

type StorageDriver

type StorageDriver interface {
	Open(connect string, extraDriverOptions string) (Conn, error)
}

StorageDriver interface defines very general, high level operations for retrieval and storage of data. The back-storage can be a flat file, database or document store. The interfaces specify NO sql methods and flatten out operations

type Store

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

Store holds the state for any storage driver. It allows you to have consistent returns, such as getting the last error, discovering how a connection was made (connectString) or the name of the driver (name)

func (*Store) ClearErrors

func (s *Store) ClearErrors()

ClearErrors simply clears the last stored error

func (*Store) Close

func (s *Store) Close() error

Close the connection to the storage mechanism. If there is no close routine ignore the call

func (*Store) CreateStore

func (s *Store) CreateStore() error

CreateStore , if implemented, initialises any storage. If not implemented, an error will be returned.

func (*Store) FetchUserByEmail

func (s *Store) FetchUserByEmail(domain, email string) (*tenant.User, error)

FetchUserByEmail Emails are not unique, except within a domain.

func (*Store) FetchUserByGUID

func (s *Store) FetchUserByGUID(guid string) (*tenant.User, error)

FetchUserByGUID No domains are required as this is the primary (or unique) key

func (*Store) FetchUserByLogin

func (s *Store) FetchUserByLogin(domain, loginName string) (*tenant.User, error)

FetchUserByLogin Login names are only unique within the domain

func (*Store) FetchUserByToken

func (s *Store) FetchUserByToken(token string) (*tenant.User, error)

FetchUserByToken If the user is not logged in, a 'User not found' error is returned.

func (*Store) GetDriverInterface

func (s *Store) GetDriverInterface() gdriver.DriverInterface

GetDriverInterface is a low level function that replaces the raw storage driver with the driver passed.

func (*Store) GetStorageConnector

func (s *Store) GetStorageConnector() Conn

GetStorageConnector will return the actual connection to the database for low-level access. This should be avoided unless you are coding for a very non-portable function

func (*Store) GetStorageDriver

func (s *Store) GetStorageDriver() StorageDriver

GetStorageDriver will return the objects storage driver. This is for internal use only

func (*Store) Id

func (s *Store) Id() string

Id will return the identity of the storage driver

func (*Store) IsOpen

func (s *Store) IsOpen() bool

IsOpen will return the the open status of the connection

func (*Store) LastError

func (s *Store) LastError() error

LastError returns the last known error condition that was given by a call

func (*Store) LongHelp

func (s *Store) LongHelp() string

LongHelp will return a long description of the storage driver. It should give a fair amount of detail

func (*Store) Open

func (s *Store) Open(connect string, extraDriverOptions string) error

Open a connection to the storage mechanism and return both a storage structure and an error status of the open

func (*Store) Ping

func (s *Store) Ping() error

Ping will test to see if the storage system is alive. This is an optional routine. If it doesn't exist, a nil return occurs (no error)

func (*Store) Release

func (s *Store) Release() error

Release any locks or memory

func (*Store) Reset

func (s *Store) Reset()

Reset any errors or intermediate conditions

func (*Store) SetDriverInterface

func (s *Store) SetDriverInterface(x gdriver.DriverInterface)

SetDriverInterface is a low level function and should never be called. It will force the storage driver to use the passed driver instead of the one defined.

func (*Store) SetLastError

func (s *Store) SetLastError(err error) *Store

SetLastError will save the last error that occured in this class

func (*Store) SetStorageDriver

func (s *Store) SetStorageDriver(x StorageDriver)

SetStorageDriver will set the higher-level driver to the storagedriver passed.

func (*Store) ShortHelp

func (s *Store) ShortHelp() string

ShortHelp will return a brief description of the storage driver.

func (*Store) UserFetch

func (s *Store) UserFetch(domain, lookupKey, lookkupValue string) (*tenant.User, error)

UserFetch will find a tenant using the domain, a field name and the field value. There will only be one record returned. If you pass MatchAnyDomain as the domain, this will only be valid for a small number of key-types (e.g. enforced unique keys.)

func (*Store) UserInsert

func (s *Store) UserInsert(user *tenant.User) error

UserInsert will attempt to insert a new 'tenant' record in the datase. It makes sure the database is open and will save any error code that occurs.

func (*Store) UserUpdate

func (s *Store) UserUpdate(user *tenant.User) error

UserUpdate will update the 'tenant record in the database. It makes sure the database is open to stop any problems with low-level drivers

type Storer

type Storer interface {
	// Required wrappers for required StorageDriver functions
	Open(connect string, extraDriverOptions string) error
	UserFetch(domain, lookupKey, lookkupValue string) (*tenant.User, error)
	UserInsert(user *tenant.User) error
	UserUpdate(user *tenant.User) error

	// Optional device connection functions
	CreateStore() error
	Close() error
	GetStorageConnector() Conn
	LastError() error
	IsOpen() bool
	Ping() error
	Release() error
	Reset()

	// The following are convienence wrapper functions for the UserXXX functions
	FetchUserByEmail(domain, email string) (*tenant.User, error)
	FetchUserByGUID(guid string) (*tenant.User, error)
	FetchUserByLogin(domain, loginName string) (*tenant.User, error)
	FetchUserByToken(token string) (*tenant.User, error)

	//  The following are wrappers for the gdriver routines.
	Id() string
	ShortHelp() string
	LongHelp() string

	// Internal routines
	SetDriverInterface(x gdriver.DriverInterface)
	GetDriverInterface() gdriver.DriverInterface

	SetStorageDriver(x StorageDriver)
	GetStorageDriver() StorageDriver
}

Storer interface gives the set of methods that a storage driver MAY implement. Because some are optional, see the methods for Store for ones that are required.

func GetDefaultDriver

func GetDefaultDriver() Storer

GetDefaultDriver fetches the name of the default driver and then activates the driver

func GetDriver

func GetDriver(name string) Storer

GetDriver will pick the driver from the map and return a fully initialised storage driver to the caller.

func NewStore

func NewStore(name string) Storer

NewStore will return an address of the Store structure.

func Open

func Open(defaultDriverName, dsn, options string) (Storer, error)

Open will set the default name and then open the driver for storage activity

func SetDefault

func SetDefault(name string) Storer

SetDefault sets the name of the driver that should be the default driver.

Directories

Path Synopsis
drivers

Jump to

Keyboard shortcuts

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