Hi,I am trying to query a kdb+ database, via the c/kdb+ interface. Theresult of the query is stored in a K object. I need to copy the resultof the query to a C struct( basically the row values fetched from thequery.The C struct also stores the row values, which is identified byanother module. The C struct needs to be sent to the other module).I know that doing an assignment would be slow. Is it possible to do amemcpy from the K object to the C struct( assuming that the format ofthe result from the q query is same as that of the format of the Cstruct, considering datatypes as well.)? I see from the k.h headerfile that the K struct is essentially a linked list of type k0, andmemcpy maybe ruled out.typedef struct k0 {…struct k0*k;…};}*KDoes this sound like something that can be done?Regards,Nav
hi nav,
> I am trying to query a kdb+ database, via the c/kdb+ interface. The
> result of the query is stored in a K object. I need to copy the result
> of the query to a C struct( basically the row values fetched from the
> query.The C struct also stores the row values, which is identified by
> another module. The C struct needs to be sent to the other module).
this is an excerpt of CandK.txt.
Accessing a Kdb Server with KDBC
The following example can be found in
www.kx.com/a/kdb/connect/kdbc.txt. It illustrates communication
between a C program and a Kdb database server. The Kdb server,
listening on port 2001, is started with the following command, which
also creates the database from the SQL script sp.s (in the Kdb
download from the kx website).
k db sp.s -p 2001
The C program connects to the database server, sends it an SQL query
and processes the result. Note that the K result returned by the Kdb
server is a 3-item general list holding the column names of the result
table, the data in column order and the data types.
#include “k20.h”
extern printf(S s,…),gets(S);
main()
{ K q,r,n,d,t; // q=query, r=query result,
// n=column names, d=data, t=data types
cd(ksk(“h:3:(`;2001)”,0)); // connect to the server
cd(ksk(“k:{h 4:x}”,0)); // remote execution function
q=gnk(1,gp(“select sum qty by p from sp”)); // KSQL query
r=ksk(“k”,q),cd(q); // query result, free query
n=KK(r)[0],d=KK(r)[1]; // names, inverted data
t=KK(r)[2]; // data types
printf(“columns: %d\n”,n->n); // number of columns
{I i=0;for(;in;++i) // column names and types
printf(“%s %s\n”,KS(n)[i],KS(t)[i]);}
printf(“rows: %d\n”,KK(d)[0]->n); // number of rows
printf(“%s %d\n”,KS(KK(d)[0])[0],
KI(KK(d)[1])[0]); // first row(varchar,int)
cd(r); // free the result
cd(ksk(“3:h”,0)); // close the connection
return 0;}
> I know that doing an assignment would be slow.
you mean individually setting elements of your C struct?
if there is no other way for the other module to get data
other than via these structs, i’m afraid you have to
assign fields of the struct one by one.
> Is it possible to do a
> memcpy from the K object to the C struct
it’s not really useful unless copying an entire primitive array
(eg. int arrays). even then, you don’t need to copy in this
case if you make sure the k variable always has a positive
ref count.
> ( assuming that the format of
> the result from the q query is same as that of the format of the C
> struct, considering datatypes as well.)?
there may be some sort of marshalling/packing functions you
can call in q/k that pack up heterogeneous row into a char array
that can be memcpy’ed in C.
i think the fastest way to go is to flip the data so any row’s elements
are the same type, then assemble your structs from those arrays.
> I see from the k.h header
> file that the K struct is essentially a linked list of type k0, and
> memcpy maybe ruled out.
typedef struct k0{I c,t,n;struct k0*k[1];}K;
for an int array “ia”, a row would then look like:
ia->c=
ia->t=1
ia->n=
then KI(ai) is your row (actually column of original data)
KI(ia) is a macro that expands to ((I)((ia)->k))
that is, ia->k is actually not a , it’s sneakily
an array of ints.
ta, jack.
X-Mailer: Apple Mail (2.930.3)
don’t use this text. this is for k2 or k3.
use the code.kx.com or kx.com/q/c
felix
> don’t use this text. this is for k2 or k3.sorry, i must be getting old. https://code.kx.com/trac/wiki/Cookbook/InterfacingWithCis a good little tutorialta, jack