|
| 1 | +--- |
| 2 | +title: RBB (r.q) | Documentation for q and kdb+ |
| 3 | +description: How to construct an RDB |
| 4 | +keywords: kdb+, q, rdb, streaming |
| 5 | +--- |
| 6 | +# Real-time Database (RDB) using r.q |
| 7 | + |
| 8 | +`r.q` is available from :fontawesome-brands-github:[KxSystems/kdb-tick](https://github.com/KxSystems/kdb-tick) |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +A kdb+ process acting as an RDB stores a current day’s data in-memory for client queries. |
| 13 | +It can write its contents to disk at end-of-day, clearing out it in-memory data to prepare for the next day. |
| 14 | +After writing data to disk, it instructs a HDB to load the written data. |
| 15 | + |
| 16 | +### Customization |
| 17 | + |
| 18 | +`r.q` provides a starting point to most environments. The source code is freely avaialble and can be tailered to individual needs. For example: |
| 19 | + |
| 20 | +#### Memory use |
| 21 | + |
| 22 | +The default RDB stores all of a days data in memory before end-of-day writing to disk. The host machines should be configured that all required resources |
| 23 | +can handle the demands that may be made of them (both for today and the future). |
| 24 | +Depending on when there may be periods of low/no activity, [garbage collection](../ref/dotq.md#gc-garbage-collect) could be deployed after clearing tables at end-of-day, or a system for intra-day writedowns. |
| 25 | + |
| 26 | +#### User queries |
| 27 | + |
| 28 | +A gateway process should control user queries and authorization/authentication, using RDBs/RTEs/HDBs to retrieve the required information. |
| 29 | +If known/common queries can be designed, the RDB can load additional scripts to pre-define functions a gateway can call. |
| 30 | + |
| 31 | +### End-of-day |
| 32 | + |
| 33 | +The end-of-day event is governed by the tickerplant process. The tickerplant calls the RDB [`.u.end`](#uend) function when this event occurs. |
| 34 | +The main end-of-day event for an RDB is to save todays data from memory to disk, clear its tables and cause the HDB to be aware of a new days dataset for it to access. |
| 35 | + |
| 36 | +!!! Note "[.u.rep](#urep) sets the HDB directory be the same as the tickerplant log file directory. This can be edited to use a different directory if required" |
| 37 | + |
| 38 | +### Recovery |
| 39 | + |
| 40 | +Using IPC, the RDB process can retrieve the current tickerplant log location and use via the [variables](tickq.md#variables) the tickerplant maintains. |
| 41 | +The function [`.u.rep`](#urep) is then used to populate any tables from the log. |
| 42 | + |
| 43 | +!!! Note "The RDB should be able to access the tickerplant log from a directory on the same machine. The RDB/tickerplant can be changed to reside on different hosts but this increases the resources needed to transmit the log file contents over the network." |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +```bash |
| 48 | +q tick/r.q [host1]:port1[:usr:pwd] [host2]:port2[:usr:pwd] [-p 5020] |
| 49 | +``` |
| 50 | + |
| 51 | +| Parameter Name | Description | Default | |
| 52 | +| ---- | ---- | --- | |
| 53 | +| host1 | host running kdb+ instance that the RDB will subscribe to e.g. tickerplant host | localhost | |
| 54 | +| port1 | port of kdb+ instance that the RDB will subscribe to e.g. tickerplant port | 5010 | |
| 55 | +| host2 | host of kdb+ instance to inform at end-of-day, after data saved to disk e.g. HBD host | localhost | |
| 56 | +| port2 | port of kdb+ instance to inform at end-of-day, after data saved to disk e.g. HBD port | 5012 | |
| 57 | +| usr | username | <none> | |
| 58 | +| pwd | password | <none> | |
| 59 | +| -p | [listening port](../basics/cmdline.md#-p-listening-port) for client communications | <none> | |
| 60 | + |
| 61 | +!!! Note "Standard kdb+ [command line options](../basics/cmdline.md) may also be passed" |
| 62 | + |
| 63 | +## Variables |
| 64 | + |
| 65 | +| Name | Description | |
| 66 | +| ---- | ---- | |
| 67 | +| .u.x | Connection list. First element populated by [`host1`](#usage) (tickerplant), and second element populated by [`host2`](#usage) (HDB) | |
| 68 | + |
| 69 | +## Functions |
| 70 | + |
| 71 | +Functions are open source & open to customisation. |
| 72 | + |
| 73 | +### upd |
| 74 | + |
| 75 | +Called by external process to update table data. Defaults to [`insert`](../ref/insert.md) to insert/append data to a table. |
| 76 | + |
| 77 | +```q |
| 78 | +upd[x;y] |
| 79 | +``` |
| 80 | +Where |
| 81 | + |
| 82 | +* `x` is a symbol atom naming a table |
| 83 | +* `y` is table data to add to table `x`, which can contain one or more rows. |
| 84 | + |
| 85 | +### .u.end |
| 86 | + |
| 87 | +Perform end-of-day actions of saving tables to disk, clearing tables and running reload on HDB instance to make it aware of new day of data. |
| 88 | + |
| 89 | +```q |
| 90 | +.u.end[x] |
| 91 | +``` |
| 92 | +Where x is the date that has ended. |
| 93 | + |
| 94 | +Actions performed: |
| 95 | + |
| 96 | +* finds all tables with the group attribute on the sym column |
| 97 | +* calls [`.Q.dpft`](../ref/dotq.md#hdpf-save-tables), with params: |
| 98 | + * HDB connection from [`.u.x`](#variables) (second element) |
| 99 | + * current directory (note: directory was changed in [`.u.rep`](#urep)) |
| 100 | + * date passed to this function (`x`) i.e. day that is ending |
| 101 | + * `` `sym `` column |
| 102 | +* re-apply group attribute to sym column for those tables found in first steps (as clearing the table removed grouped attribute) |
| 103 | + |
| 104 | +### .u.rep |
| 105 | + |
| 106 | +Initialise RDB by creating tables, which is then populated with any existing tickerplant log. Will set the HDB directory to use at end-of-day. |
| 107 | + |
| 108 | +```q |
| 109 | +.u.rep[x;y] |
| 110 | +``` |
| 111 | +Where |
| 112 | + |
| 113 | +* `x` is a list of table details, each element a two item list |
| 114 | + * symbol for table name |
| 115 | + * schema table |
| 116 | +* `y` is the tickerplant log details.log comprising of a two item list: |
| 117 | + * a long for the log count (null represents no log) |
| 118 | + * a file symbol for the location of the current tickerplant log (null represents no log) |
| 119 | + |
| 120 | +Actions performed: |
| 121 | + |
| 122 | +* tables are created using `x` |
| 123 | +* if a tickerplant log file has been provided |
| 124 | + * log file is replayed using [`-11!`](../basics/internal.md#-11-streaming-execute) to populate tables |
| 125 | + * set the HDB directory by changing the working directory to the same directory as used by the log file (see [`tick.q`](tickq.md#usage) for details on how to alter the log file directory). |
| 126 | + |
0 commit comments