leftjoin

package
v0.0.0-...-b96bacd Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package leftjoin defines two 'tables' and implements a 'left join' between them

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ObtainAnSetNxLockForTable

func ObtainAnSetNxLockForTable(rc *redis.Client,
	tableName string,
	timeout, retryPeriod time.Duration) (obtainedALock bool, err error)

ObtainAnSetNxLockForTable tries to lock table, using SETNX with key = "tablename.updating"

func PrintJoinedOrders

func PrintJoinedOrders(rc *redis.Client) (err error)

PrintJoinedOrders prints a sort of left join of clients to orders. There is a flaw here. We lock orders once for each client, so orders can change during our query. Also orders can change between we time we get clients and the time we get orders. But this is still an excercise :) In real world, we might lock the entier database, or lock both tables, or use multi-version architecture like in postgres.

func ReleaseAnSetNxLockForTable

func ReleaseAnSetNxLockForTable(rc *redis.Client, tableName string) (err error)

ReleaseAnSetNxLockForTable releases a lock. If lock was not there, error is returned

func Run

func Run()

Run joins two hashes. Inspired by https://redis.io/topics/indexes

func WithNxLock

func WithNxLock(rc *redis.Client, tableName string, body func() error) (err error)

WithNxLock obtains a lock on a table, then runs body, then, if body returns no error, releases a lock Lock is implemented with the SETNX $tableName.updating command. While obtaining, WithNxLock waits for 3 seconds and then returns an error with "Failed to lock table %s" message

Types

type Client

type Client struct {
	ID   int
	Name string
}

Client is used to serialize client (purchaser)

func GetAllClients

func GetAllClients(rc *redis.Client) (cArray []*Client, err error)

GetAllClients returns a slice of all clients

func (*Client) MarshalBinary

func (c *Client) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Client) Save

func (c *Client) Save(rc *redis.Client) (err error)

Save writes client to the redis db

func (*Client) UnmarshalBinary

func (c *Client) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

type Order

type Order struct {
	ID       int
	Clientid int
	Product  string
	// Currency as represented as Rational https://play.golang.org/p/vQ6hT-zcYt
	Total big.Rat
}

Order is used to serialize purchase

func GetOrderByID

func GetOrderByID(rc *redis.Client, ID int) (o *Order, err error)

GetOrderByID loads an order from database, using ID. If no order found, error is returned

func GetOrdersByClientid

func GetOrdersByClientid(rc *redis.Client, Clientid int) (oArray []*Order, err error)

GetOrdersByClientid uses an index to get all clients

func (*Order) MarshalBinary

func (o *Order) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (*Order) Save

func (o *Order) Save(rc *redis.Client) (err error)

Save writes order to the redis db

func (*Order) UnmarshalBinary

func (o *Order) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

Jump to

Keyboard shortcuts

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