Kdb+ communicates with the filesystem through
- one-shot operations
- handles to persistent connections
Handles are more efficient for multiple operations on a file.
File paths are displayed separated with forward slashes, regardless of the operating system.
¹ Has application beyond the file system.
Setting and getting
get let you treat files as variables that persist in the filesystem.
q)`:data/foo`:data/bar set'(42;"thin white duke") `:data/foo`:data/bar q)get `:data/foo 42 q)get `:data/bar "thin white duke"
Writing and reading
Any file can be read or written as bytes (binary). Text-file primitives handle text files.
0 associates with text;
1 with bytes.
The File Text operator
0: can also represent a table as strings, and interpret key-value pairs.
A persistent connection enables multiple operations on a file without repeatedly opening and closing it.
Opening a connection to a file returns a handle to the connection. The handle takes the form of an int that is also an applicable value.
System handles 0, 1, and 2 are to the console, stdout, and stderr. They are always open.
Opening a connection to a non-existent file creates it and any missing ancestor directories.
Applying the handle to data appends it to the file as bytes.
neg of the handle to char data appends it as text.
The result of a successful operation is the positive or negative handle.
q)key `:foo/ / does not exist q)show h:hopen `:foo/bar.txt 12i q)key `:foo/ / file and dir created ,`bar.txt q)neg[h] "hear the lark and hearken" -12i q)-12i "to the barking of the dog fox" -12i q)neg[h] "gone to ground" -12i q)hclose h q)hcount `:foo/bar.txt 71 q)read0 `:foo/bar.txt "hear the lark and hearken" "to the barking of the dog fox" "gone to ground" q)read0 (`:foo/bar.txt;10;20) "ark and hearken" "to t"
q)hopen ":foo/hello.dat" 7i q)7i 0x68656c6c6f776f726c64 7i q)hclose 7i q)read1 `:foo/hello.dat 0x68656c6c6f776f726c64
Relative filepaths are sought in the following locations, in order.