kdb+k

Kdb+ data fomat when writing to a file


I'm trying to understand what happen when someone try to write to a file the next way:

q)h:hopen `:out
q)h (1 2;3)
3i
q)hclose h
q)read1 `:out
0x07000200000001000000000000000200000000000000f90300000000000000

this is not the same as a binary representation:

q)-8!(1 2;3)
0x010000002d00000000000200000007000200000001000000000000000200000000000000f90300000000000000

Solution

  • Technically you can read the object if you have a little prior knowledge about the object, such that you can fabricate a header:

    q)read1`:out
    0x07000200000001000000000000000200000000000000f90300000000000000
    q)-9!read1`:out
    'badmsg
      [0]  -9!read1`:out
             ^
    q)-9!{0x01,0x000000,(reverse 0x0 vs `int$count[x]+1+3+4+1+1+4),0x00,0x00,(reverse 0x0 vs 2i),x}read1`:out
    1 2
    3
    

    The header here is comprised of:

    0x01 - little endian
    0x000000 - filler
    message length (count of raw `x` plus the header additions)
    0x00 - type (generic list = type 0) ... you have to know this in advance
    0x00 - attributes .... none here, you would have to know this
    length of list .... here we knew it was a 2-item list
    x  - the raw bytecode of the object without header 
    

    As rianoc pointed out, there are better ways to write such objects such that they can be more easily read without requiring advanced knowledge