Getting replay error for keyed tables kdb+

67 Views Asked by At

This is my tickerplant schema :

sym.q

trade:([]time:`timespan$();sym:`symbol$();price:`float$();size:`long$())

I am going to key this table based on sym in my rdb q code.

rdb.q

/q tick/r.q [host]:port[:usr:pwd] [host]:port[:usr:pwd]
/2008.09.09 .k ->.q

if[not "w"=first string .z.o;system "sleep 1"];

upd:upsert;

/ get the ticker plant and history ports, defaults are 5010,5012
.u.x:.z.x,(count .z.x)_(":5010";":5012");

/ end of day: save, clear, hdb reload
.u.end:{t:tables`.;t@:where `g=attr each t@\:`sym;.Q.hdpf[`$":",.u.x 1;`:.;x;`sym];@[;`sym;`g#] each t;};

/ init schema and sync up from log file;cd to hdb(so client save can run)

/ Initialize some tables as keyed tables 
.u.rep:{(.[;();:;].)each x;
    `sym xkey `trade;
    if[null first y;:()];
    -11!y;
    system "cd ",1_-10_string first reverse y};

/ HARDCODE \cd if other than logdir/db

/ connect to ticker plant for (schema;(logcount;log))
.u.rep .(hopen `$":",.u.x 0)"(.u.sub[`;`];`.u `i`L)";

When I run the publisher it upserts the data without any errors :

q)trade
sym| time                 price size
---| -------------------------------
ABC| 0D13:50:59.254254870 50    654 

Now I was going to test its replay functionality by quitting that process and starting it again, then it showed this error :

KDB+ 4.0 2023.11.03 Copyright (C) 1993-2023 Kx Systems
l64/ 6(24)core 23967MB naseef naseef-b365m-gaming-hd 127.0.1.1 EXPIRE 2024.08.14 [email protected] KOD #5015621

'type
  [3]  /home/naseef/q/l64/tick/r.q:20: .u.rep:
 if[null first y;:()];
 -11!y;
    ^
 system "cd ",1_-10_string first reverse y}

I have also scenarios where there are two keyed tables, but that isnt shown here.

Any help would be appreciated thanks!

1

There are 1 best solutions below

5
Alexander Unterrainer On

I would avoid to key a table in the schema and persist tick messages to a TP Log file. (I am actually not 100% sure you can do that). But you can definitely not store keyed tables to disk using spalyed tables (see here https://code.kx.com/q4m3/14_Introduction_to_Kdb%2B/#142-splayed-tables)

"There are serious restrictions on what can be splayed.

Tables can be splayed. Keyed tables cannot.

This might seem to preclude relational capabilities since it eliminates persisting a foreign key relation. But the day is saved by link columns, which can be persisted. See §14.1.2."

If you would like to work with keyed tables, store them as unkeyed table and key them when you load them, on the RTS (real-time subscriber) or other services that contain business logic.

Hope this helps.

If you struggle with tick.q I have created a blog post explaining line by line. Feel free to have a look

https://www.defconq.tech/docs/tutorials/tick