These Lua scripts are meant for redis EVAL
method.
As Redis is single threaded, the EVAL
method is
Atomic. This property is important if we want to
ensure concistency in operations that involve
check-and-set behaviour
- Source:
Members
(static) buySecurity
Buy Security Script Source (.lua)
buying security does the following
- calculate a
tradeId
from a monotonically increasing keytradeId.{user}
- update the totalBuyPrice,shares and average buy price in the users portfolio
- uses
msgpk
to store the trade for update calculations
- Source:
(static) createUser
Create User Script Source (.lua)
The createuser adds 2 keys to redis
user.{username} = {password}
, this is used to ensure unique usernames and update passwordsauth.{username}:{password} = 1
, this is used as a fast way to authenticate users to find username password matches
- Source:
(static) getAllTrades
- Source:
(static) sellSecurity
Sell Security Script Source (.lua)
selling security does the following
- calculate a
tradeId
from a monotonically increasing keytradeId.{user}
- Decrease the quantity of stocks and check value
- if value is less than 0 then roll back and return error
- else goto 3
- update the totalBuyPrice the users portfolio
- uses
msgpk
to store the trade for update calculations
- Source:
(static) updateSecurity
Update Trade Script Source (.lua)
The Update Script is used for UPDATE/DELETE operations It's strongly consistent for all operations as it only does changes to the db if the entire sequence of operations is valid, else backs off. It considers 2 cases
- Trades where tickerSymbol is not updated
- Get a list of all the trades
- Initialize the state to 0 shares, 0 price
- Start performing the trades one by one
- If the ID of the trade matches
- If operation is delete, ignore the trade
- Else, perform the updated trade
- Use the newState to calculate values of the portfolio
- Trades Where the tickerSymbol is updated
- This will follow the above method for 2 stocks
- It will delete the trade from the old Symbol
- Calculate the newState for the old Symbol
- Insert the trade in the new Symbol
- Calculate the newState for the new Symbol
- Update the portfolio for both the symbols
- Source: