cus

package module
v0.0.0-...-67048cc Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 6 Imported by: 0

README

Cassandra unique storage

Golang library for storing unique data in Cassandra database, by maintaining checksums of the objects that are stored. It uses LWT to maintain consistency across all data.

Interface

create_test.go

type UniqueStore interface {
	// Create unique object and store it in cassandra
	// In case of the object data is already persisted with other object
	// the function returns the `duplicateId` of the object along with err = `ErrDuplicateObject`
	Create(id string, data []byte) (duplicateId *string, err error)
	// Get returns the object data
	// In case the object is not found - then err = `ErrNotFound` and the data will be `nil`
	Get(id string) (data []byte, err error)
	// CreateSchema creates the tables if they do not exist in the default cluster keyspace
	// If `force` flag is set to true, then the table will be first droped if it exists
	CreateSchema(force bool) error
	// Delete removes the data from the db
	// In case the object is not found - then err = `ErrNotFound` and the data will be `nil`
	// In any other case the original error will be propagated
	Delete(id string) (err error)
}

Blog post

For more details see the blog post

Usage

Import the module:

go get github.com/npenkov/cus

Initialize the storage with cassandra session

cluster := gocql.NewCluster(hostAndPort)
cluster.Keyspace = "cus"
cluster.Timeout = time.Millisecond * 2000
cluster.DisableInitialHostLookup = true
session, err := cluster.CreateSession()
store = cus.NewCassadnraUniqueStore(session)
Store objects

create_test.go

myKey := "my-store-key"
data := []byte{0x64, 0x65, 0x66}

dupId, err := store.Create(myKey, data)
if err != nil || dupId != nil {
  if err == store.ErrDuplicateObject {
    fmt.Printf("The data stored is duplicate with id : %s", *dupId)
  } else {
    fmt.Printf("Failed to fetch data : %v", err)
  }
}
Fetch objects

get_test.go

fetchedData, err := store.Get("55")
if err != nil || fetchedData == nil {
  if err == store.ErrNotFound {
    fmt.Printf("No record found for id 55")
  } else {
    fmt.Printf("Error fetching data from database : %v", err)
  }
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDuplicateObject = errors.New("Duplicate object")
	ErrNotFound        = errors.New("Not found")
)

Functions

This section is empty.

Types

type CassandraUniqueStore

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

func (*CassandraUniqueStore) Create

func (s *CassandraUniqueStore) Create(id string, data []byte) (duplicateId *string, err error)

func (*CassandraUniqueStore) CreateSchema

func (s *CassandraUniqueStore) CreateSchema(force bool) error

func (*CassandraUniqueStore) Delete

func (s *CassandraUniqueStore) Delete(id string) (err error)

func (*CassandraUniqueStore) Get

func (s *CassandraUniqueStore) Get(id string) (data []byte, err error)

type UniqueStore

type UniqueStore interface {
	// Create unique object and store it in cassandra
	// In case of the object data is already persisted with other object
	// the function returns the `duplicateId` of the object along with err = `ErrDuplicateObject`
	Create(id string, data []byte) (duplicateId *string, err error)
	// Get returns the object data
	// In case the object is not found - then err = `ErrNotFound` and the data will be `nil`
	Get(id string) (data []byte, err error)
	// CreateSchema creates the tables if they do not exist in the default cluster keyspace
	// If `force` flag is set to true, then the table will be first droped if it exists
	CreateSchema(force bool) error
	// Delete removes the data from the db
	// In case the object is not found - then err = `ErrNotFound` and the data will be `nil`
	// In any other case the original error will be propagated
	Delete(id string) (err error)
}

Unique store represents the interface for storing and retrieving unique objects from/to database

func NewCassadnraUniqueStore

func NewCassadnraUniqueStore(session *gocql.Session) UniqueStore

Jump to

Keyboard shortcuts

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