Documentation
¶
Overview ¶
Package leftjoin defines two 'tables' and implements a 'left join' between them
Index ¶
- func ObtainAnSetNxLockForTable(rc *redis.Client, tableName string, timeout, retryPeriod time.Duration) (obtainedALock bool, err error)
- func PrintJoinedOrders(rc *redis.Client) (err error)
- func ReleaseAnSetNxLockForTable(rc *redis.Client, tableName string) (err error)
- func Run()
- func WithNxLock(rc *redis.Client, tableName string, body func() error) (err error)
- type Client
- type Order
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 ¶
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 ¶
ReleaseAnSetNxLockForTable releases a lock. If lock was not there, error is returned
func WithNxLock ¶
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 ¶
Client is used to serialize client (purchaser)
func GetAllClients ¶
GetAllClients returns a slice of all clients
func (*Client) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler
func (*Client) UnmarshalBinary ¶
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 ¶
GetOrderByID loads an order from database, using ID. If no order found, error is returned
func GetOrdersByClientid ¶
GetOrdersByClientid uses an index to get all clients
func (*Order) MarshalBinary ¶
MarshalBinary implements encoding.BinaryMarshaler
func (*Order) UnmarshalBinary ¶
UnmarshalBinary implements encoding.BinaryUnmarshaler