tmpstorage

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package tmpstorage is a temporary data storage based on using mongo DB

Data of any type can be stored for indicated period of time. Mongodb set up is required for this package. Visit https://docs.mongodb.com/ to find assistance with accomplishing following steps:

1. set up mongodb

2. create user

3. create database for data storage

Usage starts with DataStorage initialization, using DB URL, DB name and log switcher:

storage, err := tmpstorage.InitializeDataStorage(
	"mongodb://username:password@localhost:27017",
	"my_database",
	false,
)

if err != nil {
	log.Panic(err)
}

This will create storage instance, which is responsible for adding, updating, loading and deleting data

Data format for adding and updating is specific and can be created via factory, which is injected. So if you want to add some Person type data to storage:

type Person struct {
	name string
	age int
}

You should do the following:

person := Person{name: "Mathew Murdock", age: 28}
dataToStoreDto := storage.DataToStoreDtoFactory.CreateWithExistenceDuration(
	"thisIsMyDataKey",
	person,
	1000*time.Second, //TTL duration from now
)

Considering all the above mentioned, data can be added as follows:

err := storage.Add(dataToStoreDto)
if err != nil {
	log.Panic(err)
}

And updates as follows:

err := storage.Update(dataToStoreDto)
if err != nil {
	log.Panic(err)
}

Loaded from storage as follows:

loadedPerson := new(Person)
//person will be filled with data from storage
err := storage.Load("dataKeyToFind", loadedPerson)
if err != nil {
	log.Panic(err)
}

Deleted from storage as follows:

err := storage.Delete("dataKeyToDelete", person)
if err != nil {
	log.Panic(err)
}
Example (DataStorageUsage)
storage, err := InitializeDataStorage(
	"mongodb://username:password@localhost:27017",
	"my_database",
	false,
)

if err != nil {
	log.Panic(err)
}

type Person struct {
	name string
	age  int
}

person := Person{name: "Mathew Murdock", age: 28}
dataToStoreDto := storage.DataToStoreDtoFactory.CreateWithExistenceDuration(
	"thisIsMyDataKey",
	person,
	1000*time.Second, //TTL duration from now
)

err = storage.Add(dataToStoreDto)
if err != nil {
	log.Panic(err)
}

err = storage.Update(dataToStoreDto)
if err != nil {
	log.Panic(err)
}

loadedPerson := new(Person)
//loadedPerson will be filled with data from storage
err = storage.Load("thisIsMyDataKey", loadedPerson)
if err != nil {
	log.Panic(err)
}

err = storage.Delete("thisIsMyDataKey", person)
if err != nil {
	log.Panic(err)
}
Output:

Example (DataToStoreDtoFactory)
storage, err := InitializeDataStorage(
	"mongodb://username:password@localhost:27017",
	"my_database",
	false,
)

if err != nil {
	log.Panic(err)
}

type Person struct {
	name string
	age  int
}

person := Person{name: "Mathew Murdock", age: 28}

//expiration time for data has been set to 120 seconds from now
dataToStoreDtoExample1 := storage.DataToStoreDtoFactory.CreateWithExistenceDuration(
	"thisIsMyDataKeyOne",
	person,
	120*time.Second, //TTL duration from now
)

fmt.Println(dataToStoreDtoExample1)

expirationTime, err := time.Parse("2006-01-02 15:04:05", "2025-01-01 00:00:03")
if err != nil {
	log.Panic(err)
}

dataToStoreDtoExample2 := storage.DataToStoreDtoFactory.Create(
	"thisIsMyDataKeyTwo",
	person,
	expirationTime,
)

fmt.Println(dataToStoreDtoExample2)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataStorage

type DataStorage struct {
	DataToStoreDtoFactory DataToStoreDtoFactoryInterface
	// contains filtered or unexported fields
}

DataStorage is the main set of the package, which is used to store, load, delete data to/from temporary data storage

func InitializeDataStorage

func InitializeDataStorage(databaseURL, databaseName string, isDebugModeOn bool) (*DataStorage, error)

InitializeDataStorage is used to initialize DataStorage which requires mongo database URL and the name of the database for storing data. `isDebugModeOn` is responsible for turning on/off logs, provided by tmpstorage package

func (*DataStorage) Add

func (storage *DataStorage) Add(dataToStoreDto DataToStoreDto) TraceableErrorInterface

Add method is responsible for storing data. It accepts DataToStoreDto as input parameter. Data is stored in the collection with the name, based on DataToStoreDto.Data type

func (*DataStorage) AddOrUpdate

func (storage *DataStorage) AddOrUpdate(dataToStoreDto DataToStoreDto) TraceableErrorInterface

AddOrUpdate method is simply a usage of Add and Update methods. If there is no data with such key in storage, method Add will be used, otherwise Update will be used

func (*DataStorage) Delete

func (storage *DataStorage) Delete(dataKey string, data interface{}) TraceableErrorInterface

Delete method is responsible for removing data from storage. It looks for data by parameter `dataKey` and all the parameters from `data` in the collection with name, based on type of `data`. Same data can be passed, which has been passed to `Load` method. In case nothing is deleted after method calling, TraceableErrorInterface will be returned

func (*DataStorage) Load

func (storage *DataStorage) Load(dataKey string, dataPointer interface{}) TraceableErrorInterface

Load method is responsible for loading data from the storage. It looks for data by parameter `dataKey` in the collection with the name, based on dataPointerMapped type. `dataPointerMapped` is the pointer to empty instance of previously stored data. In case data is found in storage, `dataPointerMapped` will be filled with information from the storage. In case nothing is found, TraceableErrorInterface will be returned

func (*DataStorage) Update

func (storage *DataStorage) Update(dataToStoreDto DataToStoreDto) TraceableErrorInterface

Update method is responsible for updating data in storage. It accepts DataToStoreDto as input parameter. DataToStoreDto.DataKey is used for search, DataToStoreDto.Data and DataToStoreDto.ExpiresAt are used for updating and will replace currently storage values. In case nothing is updated, TraceableErrorInterface will be returned

type DataToStoreDto

type DataToStoreDto struct {
	DataKey   string      `bson:"dataKey"`
	Data      interface{} `bson:"data"`
	ExpiresAt time.Time   `bson:"expiresAt"`
}

DataToStoreDto is the structure, which is used by DataStorage as the input parameter when storing to storage or deleting from it

DataKey is the identified for stored data e.g. ULID or UUID can be used. Value should be unique within collection nonetheless same value can be used for different types of stored data

Data is the structure, which must be stored. It will be stored in self-named collection

ExpiresAt is the time.Time structure, which indicates time after which data will be removed from storage automatically

type DataToStoreDtoFactory

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

DataToStoreDtoFactory is the factory for DataToStoreDto which has several ways of creating DataToStoreDto

func (*DataToStoreDtoFactory) Create

func (factory *DataToStoreDtoFactory) Create(
	dataKey string,
	data interface{},
	expiresAt time.Time,
) DataToStoreDto

Create is the implementation of self-titled method, which is represented in DataToStoreDtoFactoryInterface

func (*DataToStoreDtoFactory) CreateWithExistenceDuration

func (factory *DataToStoreDtoFactory) CreateWithExistenceDuration(
	dataKey string,
	data interface{},
	existenceDuration time.Duration,
) DataToStoreDto

CreateWithExistenceDuration is the implementation of self-titled method, which is represented in DataToStoreDtoFactoryInterface

type DataToStoreDtoFactoryInterface

type DataToStoreDtoFactoryInterface interface {
	//Create is the method for DataToStoreDto creation
	//from parameters, which are needed in DataToStoreDto
	//(info regarding these parameters can be found	in DataToStoreDto docs)
	Create(dataKey string, data interface{}, expiresAt time.Time) DataToStoreDto

	//CreateWithExistenceDuration is the method for DataToStoreDto
	//creation from parameters:
	//
	//dataKey, data (info regarding these parameters can be found
	//in DataToStoreDto docs)
	//
	//existenceDuration (parameter, which indicates desired time
	//to live for stored data starting from the moment of
	//DataToStoreDto creation
	CreateWithExistenceDuration(dataKey string, data interface{}, existenceDuration time.Duration) DataToStoreDto
}

DataToStoreDtoFactoryInterface is the interface for the factory which is responsible for creating new instance of DataToStoreDto

type TraceableErrorInterface

type TraceableErrorInterface interface {
	Error() string

	//Previous makes it possible to retrieve previously occurred error
	Previous() error
}

TraceableErrorInterface is the error, which can contain information about previously occurred errors

Source Files

Jump to

Keyboard shortcuts

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