Invictus
Invictus

Reputation: 4338

Namespace behavior in kdb

I have a file in KDB project which only has namespace declared in it. e.g

.file.namespace1:`someValue;
.file.namespace2.dict: flip `value1`value2`value3!(`I;20;200);
.file.namespace3.dict: flip `value1`value2`value3!(`A;@[getVlueFor;a;{.log.info "Value missing for }x] ;300);

one of these namespace variable of type dictionary has some key whose value needs to be read from disk on server where process will run. So I used trap at @[f;y;e]as our project while compiling was giving error, as it was trying to evaluate it, but that file is not supposed to be on box where project is being compiled. ( our project is build with mvn, I am new to it ). Using trap helped me get rid of this error during compilation.This file which has all namespaces is loaded when process starts. Now since this file has just all namespaces I expect it to be available

If i hard-code everything in file that contains namespace, it all works fine. Wondering how can i try and debug this code: I tried loading the file using \l {filepath} and it did not complain about any error but the namespace declared inside files are still not visible on console. I also tried adding \d .filename at the top of file which contains namespace but still the namespace .filename is not visible.

Upvotes: 0

Views: 105

Answers (1)

Matt Moore
Matt Moore

Reputation: 2800

If running getVlueFor on a server which does not have access to an on disk file required to populate the value it will always error. You have 2 options:

  1. Error trap like you have done.
  2. Utilize an appropriate default.
q)`:B set 1000
cat test.q
getVlueFor:{get hsym x}

.file.namespace3.dict: flip enlist each `value1`value2`value3!(`A;{@[getVlueFor;x;2000]} `B;300)
.file.namespace4.dict: flip enlist each `value1`value2`value3!(`A;{@[getVlueFor;x;2000]} `C;300)

In this example value2 for .file.namespace3.dict and .file.namespace4.dict are defined with a default value of 2000 and attempt to read `:B and `:C from disk respectively. As `:B actually exists, it's value is set to 1000:

q test.q

.file.namespace3.dict

value1 value2 value3
--------------------
A      1000   300

As `:C doesn't exist it defaults to 2000:

.file.namespace4.dict

value1 value2 value3
--------------------
A      2000   300

Your example error trap contains multiple syntax errors so for clarity this is what it should look like:

cat test2.q
getVlueFor:{get hsym x}
.file.namespace5.dict: flip enlist each `value1`value2`value3!(`A;{@[getVlueFor;x;{[x;y] neg[1]"Error: value missing for:",string x;}x]} `D;300)

q test2.q

Error: value missing for:D

Upvotes: 1

Related Questions