You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implemented journaling and snapshot functionalities
- Added journaling feature to log order book changes.
- Implemented snapshot feature to save the current state of the order book.
- Introduced `enabledJournaling`, `journal` and `snapshot` options in the Orderbook constructor to enable journaling and initialize with saved logs.
BRAKING CHANGES
- The order book now exposes a new `snapshot` function to take a snapshot of the order book and a new `lastOp` getter to retrieve the ID of the last operation performed.
- New `log` property to the responses of each operation when the `enableJournaling` option is enabled
- Every order now have a new `origSize` property
Copy file name to clipboardExpand all lines: README.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -252,6 +252,61 @@ bids: 90 -> 5 90 -> 5
252
252
80 -> 1 80 -> 1
253
253
```
254
254
255
+
## Options
256
+
257
+
The orderbook can be initialized with the following options by passing them to the constructor:
258
+
259
+
### Snapshot
260
+
A `snapshot` represents the state of the order book at a specific point in time. It includes the following properties:
261
+
262
+
-`asks`: An array of ask orders, where each order contains a price and a list of orders associated with that price.
263
+
-`bids`: An array of bid orders, where each order contains a price and a list of orders associated with that price.
264
+
-`ts`: A timestamp indicating when the snapshot was taken, in Unix timestamp format.
265
+
-`lastOp`: The id of the last operation included in the snapshot
266
+
267
+
Snapshots are crucial for restoring the order book to a previous state. The system can restore from a snapshot before processing any journal logs, ensuring consistency and accuracy.
268
+
After taking the snapshot, you can safely remove all logs preceding the `lastOp` id.
269
+
270
+
```js
271
+
constlob=newOrderBook({ enableJournaling:true});
272
+
273
+
// after every order save the log to the database
274
+
constorder=lob.limit("sell", "uniqueID", 55, 100)
275
+
awaitsaveLog(order.log)
276
+
277
+
// ... after some time take a snapshot of the order book and save it on the database
278
+
279
+
constsnapshot=lob.snapshot();
280
+
awaitsaveSnapshot(snapshot)
281
+
282
+
// If you want you can safely remove all logs preceding the `lastOp` id of the snapshot, and continue to save each subsequent log to the database
283
+
awaitremovePreviousLogs(snapshot.lastOp)
284
+
285
+
// On server restart get the snapshot from the database and initialize the order book
The `journal` feature allows for the logging of changes and activities within the orderbook and contains all the orders operations. This is useful for recovering the state of orderbook after unexpected events.
292
+
```js
293
+
// Assuming 'logs' is an array of log entries retrieved from the database
By combining snapshots with journaling, the system can effectively restore and audit the state of the order book, ensuring data integrity and providing a reliable mechanism for state recovery.
299
+
300
+
### Enable Journaling
301
+
`enabledJournaling` is a configuration setting that determines whether journaling is enabled or disabled. When enabled, all changes to the order book and related activities are logged into a journal. This helps in tracking and auditing the state of the order book over time.
302
+
```js
303
+
constlob=newOrderBook({ enableJournaling:true }); // false by default
0 commit comments