| 1 | package Kx; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use POSIX (); | ||
| 8 | |||
| 9 | my $DEBUG = 0; | ||
| 10 | |||
| 11 | $Kx::VERSION = '0.039'; | ||
| 12 | |||
| 13 | my %NULL = ( | ||
| 14 | 'symbol' => '`', | ||
| 15 | 'short' => "0Nh", | ||
| 16 | 'int' => "0N", | ||
| 17 | 'long' => "0Nj", | ||
| 18 | 'real' => "0Ne", | ||
| 19 | 'float' => "0n", | ||
| 20 | 'char' => " ", | ||
| 21 | 'month' => '0Nm', | ||
| 22 | 'date' => '0Nd', | ||
| 23 | 'datetime' => '0Nz', | ||
| 24 | 'minute' => '0Nu', | ||
| 25 | 'second' => '0Nv', | ||
| 26 | 'time' => '0Nt', | ||
| 27 | ); | ||
| 28 | my %CAST = ( | ||
| 29 | 'symbol' => '`$"', | ||
| 30 | 'short' => '"h"$', | ||
| 31 | 'int' => '"i"$', | ||
| 32 | 'long' => '"j"$', | ||
| 33 | 'real' => '"e"$', | ||
| 34 | 'float' => '"f"$', | ||
| 35 | 'char' => '"c"$', | ||
| 36 | 'month' => '"m"$', | ||
| 37 | 'date' => '"d"$', | ||
| 38 | 'datetime' => '"z"$', | ||
| 39 | 'minute' => '"u"$', | ||
| 40 | 'second' => '"v"$', | ||
| 41 | 'time' => '"t"$', | ||
| 42 | ); | ||
| 43 | |||
| 44 | my @TYP = qw/KC KD KE KF KG KH KI KJ KM KS KT KU KV KZ XD XT/; | ||
| 45 | |||
| 46 | my %DB = (); # Place to store class wide connections to KDBs | ||
| 47 | |||
| 48 | sub whowasi { (caller(1))[3] . '()' } | ||
| 49 | |||
| 50 | sub AUTOLOAD { | ||
| 51 | # This AUTOLOAD is used to 'autoload' constants from the constant() | ||
| 52 | # XS function. | ||
| 53 | |||
| 54 | my $constname; | ||
| 55 | our $AUTOLOAD; | ||
| 56 | ($constname = $AUTOLOAD) =~ s/.*:://; | ||
| 57 | croak "&Kx::constant not defined" if $constname eq 'constant'; | ||
| 58 | my ($error, $val) = constant($constname); | ||
| 59 | if ($error) { croak $error; } | ||
| 60 | { | ||
| 61 | no strict 'refs'; | ||
| 62 | *$AUTOLOAD = sub { $val }; | ||
| 63 | } | ||
| 64 | goto &$AUTOLOAD; | ||
| 65 | } | ||
| 66 | |||
| 67 | require XSLoader; | ||
| 68 | XSLoader::load('Kx', $Kx::VERSION); | ||
| 69 | |||
| 70 | =head1 NAME | ||
| 71 | |||
| 72 | Kx - Perl extension for Kdb+ http://kx.com | ||
| 73 | |||
| 74 | =head1 SYNOPSIS | ||
| 75 | |||
| 76 | use Kx; | ||
| 77 | |||
| 78 | =head1 DESCRIPTION | ||
| 79 | |||
| 80 | Alpha code. | ||
| 81 | Create a wrapper around Kdb+ and Q in Perl using the C interface to Kdb+ | ||
| 82 | |||
| 83 | =head1 EXPORT | ||
| 84 | |||
| 85 | None by default. | ||
| 86 | |||
| 87 | =head1 METHODS | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | =head2 New | ||
| 92 | |||
| 93 | $k = Kx->new(name=>'local22', host=>'localhost', port=>2222); | ||
| 94 | |||
| 95 | Create a new K object. Set the connection paramaters to conect to 'host' | ||
| 96 | and 'port' as specified. | ||
| 97 | |||
| 98 | No connection is made to the server until you call $k->connect() | ||
| 99 | |||
| 100 | If you don't define a name it defaults to 'default'. Each subsequent call | ||
| 101 | to new() will use the same 'default' connection. | ||
| 102 | |||
| 103 | So once you make a connection later calls to new() with the same name | ||
| 104 | will use the same connection without further connect() calls required. | ||
| 105 | |||
| 106 | $k = new K host=>'localhost', port=>2222; | ||
| 107 | $k->connect(); # defaults to default; | ||
| 108 | |||
| 109 | # picks up previous default connection to localhost port 2222 and | ||
| 110 | # will use it as well. | ||
| 111 | $k1 = new K; | ||
| 112 | |||
| 113 | Also username and passwords are supported. Just add the userpass | ||
| 114 | attribute thus: | ||
| 115 | |||
| 116 | $k = Kx->new(name=>'local22', | ||
| 117 | host=>'localhost', | ||
| 118 | port=>2222, | ||
| 119 | userpass=>'user:pass'); | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | # we use %DB to hold all our connection details. | ||
| 124 | sub new | ||
| 125 | { | ||
| 126 | my $self = shift; | ||
| 127 | my %opts = @_; | ||
| 128 | my $class = ref($self) || $self; | ||
| 129 | |||
| 130 | my $name = 'default'; | ||
| 131 | $name = $opts{'name'} if defined $opts{'name'}; | ||
| 132 | |||
| 133 | my $ref = {'name' => $name}; | ||
| 134 | if(defined $opts{'host'}) | ||
| 135 | { | ||
| 136 | $DB{$name}{'host'} = $opts{'host'}; | ||
| 137 | } | ||
| 138 | if(defined $opts{'port'}) | ||
| 139 | { | ||
| 140 | $DB{$name}{'port'} = $opts{'port'}; | ||
| 141 | } | ||
| 142 | if(defined $opts{'userpass'}) | ||
| 143 | { | ||
| 144 | $DB{$name}{'userpass'} = $opts{'userpass'}; | ||
| 145 | } | ||
| 146 | if(defined $opts{'check_for_errors'}) | ||
| 147 | { | ||
| 148 | $DB{$name}{'check_for_errors'} = $opts{'check_for_errors'}; | ||
| 149 | } | ||
| 150 | |||
| 151 | # Get hold of any previously defined connection handle | ||
| 152 | if(defined $DB{$name}{'kdb'}) | ||
| 153 | { | ||
| 154 | $ref->{'kdb'} = $DB{$name}{'kdb'}; # no need for connect | ||
| 155 | $DB{$name}{'count'}++; | ||
| 156 | } | ||
| 157 | return bless $ref, $class; | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 Connect | ||
| 161 | |||
| 162 | To connect to the 'default' server. | ||
| 163 | |||
| 164 | unless($k->connect()) | ||
| 165 | { | ||
| 166 | warn "Can't connect to Kdb+ server\n"; | ||
| 167 | } | ||
| 168 | |||
| 169 | To connect to a defined server say 'local22' | ||
| 170 | |||
| 171 | unless($k->connect('local22')) | ||
| 172 | { | ||
| 173 | warn "Can't connect to local22 Kdb+ server\n"; | ||
| 174 | } | ||
| 175 | |||
| 176 | =cut | ||
| 177 | sub connect | ||
| 178 | { | ||
| 179 | my $self = shift; | ||
| 180 | my $name = shift || 'default'; | ||
| 181 | |||
| 182 | return undef unless exists $DB{$name}{'host'} && exists $DB{$name}{'port'}; | ||
| 183 | |||
| 184 | if(!defined $DB{$name}{'kdb'}) | ||
| 185 | { | ||
| 186 | # Create a new connection | ||
| 187 | # host, port and username password details | ||
| 188 | if(defined $DB{$name}{'userpass'}) | ||
| 189 | { | ||
| 190 | $DB{$name}{'kdb'} = khpu($DB{$name}{'host'}, | ||
| 191 | $DB{$name}{'port'}, | ||
| 192 | $DB{$name}{'userpass'}); | ||
| 193 | } | ||
| 194 | # host, port only | ||
| 195 | else | ||
| 196 | { | ||
| 197 | $DB{$name}{'kdb'} = khp($DB{$name}{'host'}, $DB{$name}{'port'}); | ||
| 198 | } | ||
| 199 | unless($DB{$name}{'kdb'} > 0) | ||
| 200 | { | ||
| 201 | carp "Kx->connect failed to connect. ". &whowasi ."\n" if $DEBUG; | ||
| 202 | undef $self->{'kdb'}; | ||
| 203 | undef $DB{$name}{'kdb'}; | ||
| 204 | return undef; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | # Are we moving between connections | ||
| 209 | if($self->{'name'} ne $name) | ||
| 210 | { | ||
| 211 | $DB{$self->{'name'}}{'count'}--; | ||
| 212 | } | ||
| 213 | |||
| 214 | $DB{$name}{'count'}++; | ||
| 215 | $self->{'kdb'} = $DB{$name}{'kdb'}; | ||
| 216 | $self->{'name'} = $name; | ||
| 217 | |||
| 218 | return $self->{'kdb'}; | ||
| 219 | } | ||
| 220 | |||
| 221 | =head2 Environment | ||
| 222 | |||
| 223 | There are a number of environment details you can glean from the Kdb+ | ||
| 224 | server you are connected to. They are: | ||
| 225 | |||
| 226 | my $arrayref = $k->tables; # The tables defined | ||
| 227 | my $arrayref = $k->funcs; # The functions defined | ||
| 228 | my $arrayref = $k->views; # The views defined | ||
| 229 | my $arrayref = $k->variables; # The variables defined | ||
| 230 | my $arrayref = $k->memory; # The memory details \w | ||
| 231 | |||
| 232 | my $dir = $k->cwd; # The current working directory | ||
| 233 | my $dir = $k->chdir($newdir); # Set the cwd | ||
| 234 | my $num = $k->GMToffset; # Offset from GMT for times | ||
| 235 | |||
| 236 | If you make changes that effects these environmental details then call | ||
| 237 | the env() method to update what is known. This module doesn't continually | ||
| 238 | hassle the server for these details. | ||
| 239 | |||
| 240 | @details = $k->env; # Get the environment from the server | ||
| 241 | |||
| 242 | $details[0] => [ tables ] | ||
| 243 | $details[1] => [ funcs ] | ||
| 244 | $details[2] => [ views ] | ||
| 245 | $details[3] => [ variables ] | ||
| 246 | $details[4] => 'GMToffset' | ||
| 247 | $details[5] => 'releasedate' | ||
| 248 | $details[6] => 'gmt' | ||
| 249 | $details[7] => 'localtime' | ||
| 250 | $details[8] => [ memory ] | ||
| 251 | $details[7] => 'cwd' | ||
| 252 | |||
| 253 | You can also execute OS commands on the server end and gather the results | ||
| 254 | like this. | ||
| 255 | |||
| 256 | $arref = $k->oscmd("ls -l /"); | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub env | ||
| 261 | { | ||
| 262 | my $self = shift; | ||
| 263 | return undef unless defined $self->{'kdb'}; | ||
| 264 | |||
| 265 | # OK we have aconnection and it is valid. Pick up some meta details | ||
| 266 | # from the server | ||
| 267 | $self->{'tables'} = k2parray0(k($self->{'kdb'},'\a')); | ||
| 268 | $self->{'funcs'} = k2parray0(k($self->{'kdb'},'\f')); | ||
| 269 | $self->{'views'} = k2parray0(k($self->{'kdb'},'\b')); | ||
| 270 | $self->{'variables'} = k2parray0(k($self->{'kdb'},'\v')); | ||
| 271 | $self->{'GMToffset'} = k2pscalar0(k($self->{'kdb'},'(.z.Z-.z.z)*24')); | ||
| 272 | $self->{'releasedate'} = k2pscalar0(k($self->{'kdb'},'.z.k')); | ||
| 273 | $self->{'gmt'} = k2pscalar0(k($self->{'kdb'},'.z.z')); | ||
| 274 | $self->{'local'} = k2pscalar0(k($self->{'kdb'},'.z.Z')); | ||
| 275 | $self->{'mem'} = k2parray0(k($self->{'kdb'},'\w')); | ||
| 276 | $self->{'cwd'} = k2pscalar0(k($self->{'kdb'},'\cd')); | ||
| 277 | |||
| 278 | return ( | ||
| 279 | $self->{'tables'}, | ||
| 280 | $self->{'funcs'}, | ||
| 281 | $self->{'views'}, | ||
| 282 | $self->{'variables'}, | ||
| 283 | $self->{'GMToffset'}, | ||
| 284 | $self->{'releasedate'}, | ||
| 285 | $self->{'gmt'}, | ||
| 286 | $self->{'local'}, | ||
| 287 | $self->{'mem'}, | ||
| 288 | $self->{'cwd'}, | ||
| 289 | ); | ||
| 290 | } | ||
| 291 | |||
| 292 | # a:@[.:;"\\ls -l /tmp/zzz > /tmp/.qoscmd 2>&1; cat /tmp/.qoscmd";()] | ||
| 293 | sub oscmd | ||
| 294 | { | ||
| 295 | my $self = shift; | ||
| 296 | my $cmd = shift || return undef; | ||
| 297 | my $cap = shift; | ||
| 298 | |||
| 299 | my $q = '@[.:;"\\\\' . $cmd; | ||
| 300 | |||
| 301 | if(defined $cap) | ||
| 302 | { | ||
| 303 | # Use cap as a capture file to grap stdout and stderr | ||
| 304 | $q .= "> $cap 2>&1; cat $cap"; | ||
| 305 | } | ||
| 306 | |||
| 307 | $q .= '";()]'; | ||
| 308 | my $ref = k2parray0(k($self->{'kdb'},$q)); | ||
| 309 | my @d = map {$_->[0]} @$ref; | ||
| 310 | return \@d; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub memory | ||
| 314 | { | ||
| 315 | my $self = shift; | ||
| 316 | $self->{'mem'} = k2parray0(k($self->{'kdb'},'\w')); | ||
| 317 | return $self->{'mem'}; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub cwd | ||
| 321 | { | ||
| 322 | my $self = shift; | ||
| 323 | $self->{'cwd'} = k2pscalar0(k($self->{'kdb'},'\cd')); | ||
| 324 | return $self->{'cwd'}; | ||
| 325 | } | ||
| 326 | |||
| 327 | sub chdir | ||
| 328 | { | ||
| 329 | my $self = shift; | ||
| 330 | my $d = shift || ''; | ||
| 331 | $self->{'cwd'} = k2pscalar0(k($self->{'kdb'},"\\cd $d")); | ||
| 332 | $self->{'cwd'} = k2pscalar0(k($self->{'kdb'},'\cd')); | ||
| 333 | return $self->{'cwd'}; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub tables | ||
| 337 | { | ||
| 338 | my $self = shift; | ||
| 339 | $self->env unless defined $self->{'tables'}; | ||
| 340 | return undef unless $self->{'tables'}; | ||
| 341 | return $self->{'tables'}; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub funcs | ||
| 345 | { | ||
| 346 | my $self = shift; | ||
| 347 | $self->env unless defined $self->{'funcs'}; | ||
| 348 | return undef unless $self->{'funcs'}; | ||
| 349 | return $self->{'funcs'}; | ||
| 350 | } | ||
| 351 | |||
| 352 | sub views | ||
| 353 | { | ||
| 354 | my $self = shift; | ||
| 355 | $self->env unless defined $self->{'views'}; | ||
| 356 | return undef unless $self->{'views'}; | ||
| 357 | return $self->{'views'}; | ||
| 358 | } | ||
| 359 | |||
| 360 | sub variables | ||
| 361 | { | ||
| 362 | my $self = shift; | ||
| 363 | $self->env unless defined $self->{'variables'}; | ||
| 364 | return undef unless $self->{'variables'}; | ||
| 365 | return $self->{'variables'}; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub GMToffset | ||
| 369 | { | ||
| 370 | my $self = shift; | ||
| 371 | $self->env unless defined $self->{'GMToffset'}; | ||
| 372 | return undef unless $self->{'GMToffset'}; | ||
| 373 | return $self->{'GMToffset'}; | ||
| 374 | } | ||
| 375 | |||
| 376 | =head2 TABLES | ||
| 377 | |||
| 378 | You don't need to use this just use the cmd() interface if you like. | ||
| 379 | However if your lazy like me.... read on | ||
| 380 | |||
| 381 | Each of these accessors have a method name starting with 'T'. To help | ||
| 382 | distinguish them as cooperating methods. | ||
| 383 | |||
| 384 | Create a new table in Kdb+ named mytab with 3 columns col1, col2 and | ||
| 385 | col3. The keys will be on col1 and col3 This equates to the Q command | ||
| 386 | |||
| 387 | # Q command | ||
| 388 | q)mytab:([col1:;col3:] col2:) | ||
| 389 | |||
| 390 | # The long winded Perl way | ||
| 391 | $k->Tnew(name=>'mytab',keys=>['col1','col3'],cols=>['col2']); | ||
| 392 | |||
| 393 | To add data use Tinsert(). Each row is added in the order defined | ||
| 394 | above. This line adds 1 into col1, 2 into col3 and 3 into col2 as the | ||
| 395 | keys are always defined before the other columns. | ||
| 396 | |||
| 397 | $k->Tinsert('mytab',1,2,3); | ||
| 398 | |||
| 399 | To do a select over a table use Tselect(). Tselect() takes a variable | ||
| 400 | name as its first argument. The select will be executed and assigned to | ||
| 401 | the variable you define. This way no data is passed from Kdb+ to the | ||
| 402 | client until it is needed. | ||
| 403 | |||
| 404 | $k->Tselect('a','select from mytab where col1>4'); | ||
| 405 | |||
| 406 | This is really just the same as | ||
| 407 | |||
| 408 | # q command | ||
| 409 | a:select from mytab where col1>4 | ||
| 410 | |||
| 411 | To get the details of the stored selection | ||
| 412 | |||
| 413 | my $numrows = $k->Tnumrows('a'); | ||
| 414 | my $numcols = $k->Tnumcols('a'); | ||
| 415 | |||
| 416 | This only works on variables that are tables returned from a selection. | ||
| 417 | |||
| 418 | Tget() Tindex() Tcol() and Theader() are only useful once you have done a | ||
| 419 | Tget(). | ||
| 420 | |||
| 421 | Remember it is probably better to only pull back small tables less than | ||
| 422 | say a few tens of thousand of rows as you'll eat up memory fast. | ||
| 423 | |||
| 424 | You may have run a number of Tselects() and now wish to pull back the | ||
| 425 | data. To do this use Tget() | ||
| 426 | |||
| 427 | $k->Tget('table'); # table must be a table in the server | ||
| 428 | |||
| 429 | Tget() can also be used with select type queries that return a table as | ||
| 430 | their result. It also handles indexed tables better than the cmd() | ||
| 431 | method. | ||
| 432 | |||
| 433 | To get access to random values in the returned table from Tget(). | ||
| 434 | |||
| 435 | $val = $k->Tindex(row,col); | ||
| 436 | |||
| 437 | This only works for simple tables holding scalars in each row. Don't try | ||
| 438 | this if the index would point to a mulit-valued list. Actually it sort of | ||
| 439 | works for lists and when it does $val is an array reference. If you have | ||
| 440 | troubles use Tcol(). | ||
| 441 | |||
| 442 | To get the list of column names as Kdb+ knows them. | ||
| 443 | |||
| 444 | my $header = $k->Theader(); | ||
| 445 | print "@$header\n"; | ||
| 446 | |||
| 447 | To get the meta data for a table as defined in KDB do this. | ||
| 448 | |||
| 449 | my @meta = $k->Tmeta($table); | ||
| 450 | foreach(@meta) | ||
| 451 | { | ||
| 452 | print "(name type) => (@$_)\n"; | ||
| 453 | } | ||
| 454 | |||
| 455 | |||
| 456 | To get a Perl reference to a column of data from the table (as K is | ||
| 457 | column oriented) do the following: | ||
| 458 | |||
| 459 | my $colref = $k->Tcol(0); # get the zeroth column | ||
| 460 | print "Column 0 data is: @$colref\n"; | ||
| 461 | |||
| 462 | I advise against using this on large columns or tables as it is very | ||
| 463 | memory inefficent. Better to use $k->cmd() interface to pull back | ||
| 464 | exactly what you want first. The column reference above is a Perl copy of | ||
| 465 | the data structure held in Kdb+ memory format in the client. This can be | ||
| 466 | over 3 times larger in core than the Kdb+ data. | ||
| 467 | |||
| 468 | If you need to access data via rows then use $k->Trow(). Given a row | ||
| 469 | number it will return a reference to the row. The first row is at zero 0. | ||
| 470 | |||
| 471 | my $row = $k->Trow(0); # get the zeroth row | ||
| 472 | print "Row 0 data is: @$row\n"; | ||
| 473 | |||
| 474 | Finally to delete or remove a table by name from the server: | ||
| 475 | |||
| 476 | $k->Tdelete('table'); | ||
| 477 | |||
| 478 | Here is a list of the complete table methods we have so far: | ||
| 479 | |||
| 480 | $k->Tnew(name=>'thename',keys=>[],cols=>[]); | ||
| 481 | $k->Tinsert('table',1,2,3); | ||
| 482 | $k->Tbulkinsert('table',col1=>[],col2=>[],...); | ||
| 483 | $k->Tget('select statement'); | ||
| 484 | $scalar = $k->Tindex($row,$col); | ||
| 485 | $arref = $k->Tcol(2); # 3rd col vector | ||
| 486 | $arref = $k->Trow(2); # 3rd row | ||
| 487 | $arref = $k->Theader; | ||
| 488 | $x = $k->Tnumrows; | ||
| 489 | $y = $k->Tnumcols; | ||
| 490 | $k->Tselect('table','select statement'); | ||
| 491 | $k->Tsave('table','file'); | ||
| 492 | $k->Tappend('table','file'); | ||
| 493 | $k->Tload('table','file'); | ||
| 494 | $k->Tdelete('table'); | ||
| 495 | |||
| 496 | If you want a faster bulk insert function use: | ||
| 497 | |||
| 498 | |||
| 499 | $k->Tfastbulkinsert('mytab',$col1,$col2,$col3...); | ||
| 500 | |||
| 501 | Here col1 col2 etc are infact in core Kdb+ structures and must be in the | ||
| 502 | same order as the declaration use when you used Tnew(). This is almost 3 | ||
| 503 | times faster than Tbulkinsert but uses more memory in the client. See the | ||
| 504 | test files that came with this module for more details on how it is used. | ||
| 505 | |||
| 506 | =cut | ||
| 507 | |||
| 508 | sub Tnew | ||
| 509 | { | ||
| 510 | my $self = shift; | ||
| 511 | my %arg = @_; | ||
| 512 | |||
| 513 | return undef unless defined $self->{'kdb'}; | ||
| 514 | return undef unless defined $arg{'name'}; | ||
| 515 | return undef unless defined $arg{'cols'}; | ||
| 516 | |||
| 517 | delete $self->{'COLS'} if defined $self->{'COLS'}; | ||
| 518 | |||
| 519 | # string to create the table | ||
| 520 | my $q = $arg{'name'}. ":(["; | ||
| 521 | |||
| 522 | # string to create a bulk insert function | ||
| 523 | my $b = 'pblkinsert_'.$arg{'name'}.':{insert[`'.$arg{'name'}.']('; | ||
| 524 | |||
| 525 | my $i = 0; | ||
| 526 | if(defined $arg{'keys'}) | ||
| 527 | { | ||
| 528 | foreach my $key (@{$arg{'keys'}}) | ||
| 529 | { | ||
| 530 | $q .= $key.':();'; | ||
| 531 | $b .= $key.":(x[$i]);"; | ||
| 532 | $i++; | ||
| 533 | } | ||
| 534 | chop($q); # one ; too many | ||
| 535 | } | ||
| 536 | $q .= "]"; | ||
| 537 | foreach my $c (@{$arg{'cols'}}) | ||
| 538 | { | ||
| 539 | $q .= $c . ':();'; | ||
| 540 | $b .= $c . ":(x[$i]);"; | ||
| 541 | $i++; | ||
| 542 | } | ||
| 543 | chop($q); chop($b); | ||
| 544 | $q .= ')'; # mytab:([col1:();col3:()]col2:()) | ||
| 545 | $b .= ')}'; # pblkinsert_mytab:{insert[`mytab](c1:(x[0]);c2:(x[1]))} | ||
| 546 | |||
| 547 | # Create the table | ||
| 548 | my $k = k($self->{'kdb'},$q); | ||
| 549 | if($k == 0) | ||
| 550 | { | ||
| 551 | dor0($k); # release memory | ||
| 552 | return undef; | ||
| 553 | } | ||
| 554 | if(kType($k) < 0) | ||
| 555 | { | ||
| 556 | carp "Kx->tablecreate error ", k2pscalar($k),"\n"; | ||
| 557 | dor0($k); # release memory | ||
| 558 | return undef; | ||
| 559 | } | ||
| 560 | dor0($k); # release memory | ||
| 561 | |||
| 562 | # Create the bulkinsert function | ||
| 563 | $k = k($self->{'kdb'},$b); | ||
| 564 | if($k == 0) | ||
| 565 | { | ||
| 566 | dor0($k); # release memory | ||
| 567 | return undef; | ||
| 568 | } | ||
| 569 | if(kType($k) < 0) | ||
| 570 | { | ||
| 571 | carp "Kx->tablecreate bulkinsert function error ", k2pscalar($k),"\n"; | ||
| 572 | dor0($k); # release memory | ||
| 573 | return undef; | ||
| 574 | } | ||
| 575 | |||
| 576 | dor0($k); # release memory | ||
| 577 | return 1; | ||
| 578 | } | ||
| 579 | |||
| 580 | sub Tdelete | ||
| 581 | { | ||
| 582 | my $self=shift; | ||
| 583 | my $var = shift || return undef; | ||
| 584 | |||
| 585 | delete $self->{'COLS'} if defined $self->{'COLS'}; | ||
| 586 | |||
| 587 | #$self->{'K'} = k($self->{'kdb'},"$var: null"); | ||
| 588 | # .[`.;();_;`d] will remove the d symbol from the current workspace | ||
| 589 | # This uses the Dot Fucntional form of Amend .[d;i;f;y] where | ||
| 590 | # d is a dictionary, `d do it in place | ||
| 591 | # i is and index, possibly multi level | ||
| 592 | # f is a function to apply | ||
| 593 | # y is the right hand side of a dyadic function | ||
| 594 | # | ||
| 595 | # So `. is the symbol name for the current workplace to be Ammeded in | ||
| 596 | # place | ||
| 597 | # () is an index for the whole domain of . | ||
| 598 | # _ is the drop function | ||
| 599 | # `d is the symbol name we wish to drop | ||
| 600 | my $k = k($self->{'kdb'},".[`.;();_;`$var]"); | ||
| 601 | dor0($k); # release memory | ||
| 602 | return 1; | ||
| 603 | } | ||
| 604 | |||
| 605 | sub check_for_errors | ||
| 606 | { | ||
| 607 | my $k = shift; | ||
| 608 | my $q = shift || ""; | ||
| 609 | |||
| 610 | if($k == 0) | ||
| 611 | { | ||
| 612 | carp "Undefined K structure\n"; | ||
| 613 | return 0; | ||
| 614 | } | ||
| 615 | if(kType($k) == -128) | ||
| 616 | { | ||
| 617 | carp "K error ", k2pscalar($k)," $q\n"; | ||
| 618 | return 0; | ||
| 619 | } | ||
| 620 | return 1; | ||
| 621 | } | ||
| 622 | |||
| 623 | # $k->Tinsert('mytab',1,2,3); a single row | ||
| 624 | # | ||
| 625 | sub Tinsert | ||
| 626 | { | ||
| 627 | my $self = shift; | ||
| 628 | my $table = shift; | ||
| 629 | |||
| 630 | return undef unless defined $self->{'kdb'}; | ||
| 631 | |||
| 632 | delete $self->{'COLS'} if defined $self->{'COLS'}; | ||
| 633 | |||
| 634 | # q)insert[`mytab](1;2;3) | ||
| 635 | my $q = 'insert[`' . $table . '](' . join(';',@_) . ')'; | ||
| 636 | |||
| 637 | my $k = k($self->{'kdb'},$q); | ||
| 638 | my $r = 1; | ||
| 639 | if(exists $self->{'check_for_errors'}) | ||
| 640 | { | ||
| 641 | $r = check_for_errors($k,$q); | ||
| 642 | } | ||
| 643 | dor0($k); | ||
| 644 | return $r; | ||
| 645 | } | ||
| 646 | |||
| 647 | |||
| 648 | # $k->bulkinsert('mytab',$col1,$col2,$col3...); | ||
| 649 | # | ||
| 650 | sub bulkinsert | ||
| 651 | { | ||
| 652 | my $self = shift; | ||
| 653 | my $table = shift; | ||
| 654 | |||
| 655 | return undef unless defined $self->{'kdb'}; | ||
| 656 | |||
| 657 | # Create the argument list | ||
| 658 | my $cols = ktn(0,scalar @_); | ||
| 659 | for(my $i=0; $i < @_; $i++) | ||
| 660 | { | ||
| 661 | setKarraymixed($cols,$i,$_[$i]) || croak "Can't setKarraymixed "; | ||
| 662 | } | ||
| 663 | |||
| 664 | k1(-($self->{'kdb'}),"pblkinsert_$table",$cols); | ||
| 665 | return 1; | ||
| 666 | } | ||
| 667 | |||
| 668 | # $k->Tbulkinsert('mytab',$k=>$colref,$k1=>$col1ref); | ||
| 669 | # | ||
| 670 | sub Tbulkinsert | ||
| 671 | { | ||
| 672 | my $self = shift; | ||
| 673 | my $table = shift; | ||
| 674 | |||
| 675 | return undef unless defined $self->{'kdb'}; | ||
| 676 | |||
| 677 | # q)insert[`mytab](id:($id);p:($prop);v:($v);tm:($z)) | ||
| 678 | my $q = 'insert[`' . $table . ']('; | ||
| 679 | while(@_) | ||
| 680 | { | ||
| 681 | my $key = shift @_; | ||
| 682 | my $aref = shift @_; | ||
| 683 | return undef unless $aref; | ||
| 684 | $q .= "$key:(" . join(';',@$aref) .');'; | ||
| 685 | } | ||
| 686 | chop $q; | ||
| 687 | $q .= ')'; | ||
| 688 | |||
| 689 | k(-($self->{'kdb'}),$q); | ||
| 690 | return 1; | ||
| 691 | } | ||
| 692 | |||
| 693 | sub Tsave | ||
| 694 | { | ||
| 695 | my $self = shift; | ||
| 696 | my $table = shift; | ||
| 697 | my $file = shift; | ||
| 698 | |||
| 699 | return undef unless defined $table; | ||
| 700 | if(!defined $file && defined $table) | ||
| 701 | { | ||
| 702 | # Then filename os tablename | ||
| 703 | $file = $table | ||
| 704 | } | ||
| 705 | |||
| 706 | return undef unless defined $self->{'kdb'}; | ||
| 707 | |||
| 708 | # q).[`:filename;();:;tablename] | ||
| 709 | my $q = '.[`$":' . $file . '";();:;' . $table .']'; | ||
| 710 | |||
| 711 | my $k = k($self->{'kdb'},$q); | ||
| 712 | my $r = check_for_errors($k,$q); | ||
| 713 | dor0($k); | ||
| 714 | return $r; | ||
| 715 | } | ||
| 716 | |||
| 717 | sub Tappend | ||
| 718 | { | ||
| 719 | my $self = shift; | ||
| 720 | my $table = shift; | ||
| 721 | my $file = shift; | ||
| 722 | |||
| 723 | return undef unless defined $table; | ||
| 724 | if(!defined $file && defined $table) | ||
| 725 | { | ||
| 726 | # Then filename os tablename | ||
| 727 | $file = $table | ||
| 728 | } | ||
| 729 | |||
| 730 | return undef unless defined $self->{'kdb'}; | ||
| 731 | |||
| 732 | # q).[`:filename;();:;tablename] | ||
| 733 | my $q = '.[`$":' . $file . '";();,;' . $table .']'; | ||
| 734 | |||
| 735 | my $k = k($self->{'kdb'},$q); | ||
| 736 | my $r = check_for_errors($k,$q); | ||
| 737 | dor0($k); | ||
| 738 | return $r; | ||
| 739 | } | ||
| 740 | |||
| 741 | sub Tload | ||
| 742 | { | ||
| 743 | my $self = shift; | ||
| 744 | my $table = shift; | ||
| 745 | my $file = shift; | ||
| 746 | |||
| 747 | return undef unless defined $table; | ||
| 748 | if(!defined $file && defined $table) | ||
| 749 | { | ||
| 750 | # Then filename os tablename | ||
| 751 | $file = $table | ||
| 752 | } | ||
| 753 | |||
| 754 | return undef unless defined $self->{'kdb'}; | ||
| 755 | |||
| 756 | # q).[`:filename;();:;tablename] | ||
| 757 | my $q = "$table: value`\$\":$file\""; | ||
| 758 | |||
| 759 | my $k = k($self->{'kdb'},$q); | ||
| 760 | my $r = check_for_errors($k,$q); | ||
| 761 | dor0($k); | ||
| 762 | return $r; | ||
| 763 | } | ||
| 764 | |||
| 765 | # $k->Tselect('a','select sum size by sym from trade where date=2006.09.25'); | ||
| 766 | # | ||
| 767 | #To get the details of the stored selection | ||
| 768 | # | ||
| 769 | # my $numrows = $k->Tnumrows('a'); | ||
| 770 | # my $numcols = $k->Tnumcols('a'); | ||
| 771 | sub Tselect | ||
| 772 | { | ||
| 773 | my $self = shift; | ||
| 774 | my $table = shift || croak; | ||
| 775 | |||
| 776 | return undef unless defined $self->{'kdb'}; | ||
| 777 | |||
| 778 | # q)a: select col1 from mytab where col1 > 7 | ||
| 779 | my $q = $table . ':' . join('',@_); | ||
| 780 | |||
| 781 | my $k = k($self->{'kdb'},$q); | ||
| 782 | my $r = check_for_errors($k,$q); | ||
| 783 | dor0($k); | ||
| 784 | return $r; | ||
| 785 | } | ||
| 786 | |||
| 787 | sub Tnumrows | ||
| 788 | { | ||
| 789 | my $self = shift; | ||
| 790 | my $table = shift || croak; | ||
| 791 | |||
| 792 | return undef unless defined $self->{'kdb'}; | ||
| 793 | |||
| 794 | # q)count mytab | ||
| 795 | my $q = "count $table"; | ||
| 796 | |||
| 797 | my $k = k($self->{'kdb'},$q); | ||
| 798 | my $rows = k2pscalar0($k); | ||
| 799 | |||
| 800 | return $rows; | ||
| 801 | } | ||
| 802 | |||
| 803 | sub Tnumcols | ||
| 804 | { | ||
| 805 | my $self = shift; | ||
| 806 | my $table = shift || croak; | ||
| 807 | |||
| 808 | return undef unless defined $self->{'kdb'}; | ||
| 809 | |||
| 810 | my $q = "cols $table"; | ||
| 811 | |||
| 812 | my $k = k($self->{'kdb'},$q); | ||
| 813 | my $cols = k2parray0($k); | ||
| 814 | my $numcols = scalar @$cols; | ||
| 815 | |||
| 816 | return $numcols; | ||
| 817 | } | ||
| 818 | |||
| 819 | sub Tget | ||
| 820 | { | ||
| 821 | my $self = shift; | ||
| 822 | my $cmd = shift || return undef; | ||
| 823 | |||
| 824 | |||
| 825 | dor0($self->{'K'}) if defined $self->{'K'}; # release memory | ||
| 826 | return undef unless defined $self->{'kdb'}; | ||
| 827 | |||
| 828 | $self->{'K'} = kTable($self->{'kdb'},$cmd); | ||
| 829 | if($self->{'K'} == 0) | ||
| 830 | { | ||
| 831 | return undef; | ||
| 832 | } | ||
| 833 | if(kType($self->{'K'}) < 0) | ||
| 834 | { | ||
| 835 | carp "Kx->Tget error $cmd", k2pscalar0($self->{'K'}),"\n"; | ||
| 836 | return undef; | ||
| 837 | } | ||
| 838 | |||
| 839 | $self->{'colnames'} = k2parray(kTableH($self->{'K'})); | ||
| 840 | $self->{'NUMROWS'} = kTableNumRows($self->{'K'}); | ||
| 841 | $self->{'NUMCOLS'} = kTableNumCols($self->{'K'}); | ||
| 842 | $self->{'COLS'} = kTableCols($self->{'K'}); | ||
| 843 | |||
| 844 | return($self->{'NUMROWS'}, $self->{'NUMCOLS'}); | ||
| 845 | } | ||
| 846 | |||
| 847 | |||
| 848 | sub Tmeta | ||
| 849 | { | ||
| 850 | my $self = shift; | ||
| 851 | my $table = shift; | ||
| 852 | |||
| 853 | #my $q = "select c,t from meta $table"; | ||
| 854 | my $q = "meta $table"; | ||
| 855 | my $meta = kTable($self->{'kdb'},$q); | ||
| 856 | return undef if $meta == 0; | ||
| 857 | if(kType($meta) < 0) | ||
| 858 | { | ||
| 859 | carp "Kx->meta error $q", k2pscalar0($meta),"\n"; | ||
| 860 | return undef; | ||
| 861 | } | ||
| 862 | |||
| 863 | my $rows = kTableNumRows($meta); | ||
| 864 | my @m = (); | ||
| 865 | for(my $i=0; $i < $rows; $i++) | ||
| 866 | { | ||
| 867 | my $type = kTableIndex($meta,$i,1); # Version 2.2 support | ||
| 868 | if($type =~ /^\d+/) | ||
| 869 | { | ||
| 870 | $type = chr($type); | ||
| 871 | } | ||
| 872 | push(@m,[kTableIndex($meta,$i,0), $type]); | ||
| 873 | } | ||
| 874 | dor0($meta); | ||
| 875 | |||
| 876 | return @m; | ||
| 877 | } | ||
| 878 | |||
| 879 | sub Tcol | ||
| 880 | { | ||
| 881 | my $self = shift; | ||
| 882 | my $col = shift; | ||
| 883 | return undef unless defined $self->{'COLS'}; | ||
| 884 | return undef unless $col >= 0 && $col < $self->{'NUMCOLS'}; | ||
| 885 | |||
| 886 | my $c = kStructi($self->{'COLS'},$col); | ||
| 887 | return k2parray($c); | ||
| 888 | } | ||
| 889 | |||
| 890 | sub Tindex | ||
| 891 | { | ||
| 892 | my $self = shift; | ||
| 893 | my $row = shift; | ||
| 894 | my $column = shift; | ||
| 895 | |||
| 896 | return undef unless defined $self->{'K'}; | ||
| 897 | return undef unless defined $self->{'COLS'}; | ||
| 898 | return undef unless $column >= 0 && $column < $self->{'NUMCOLS'}; | ||
| 899 | return undef unless $row >= 0 && $row < $self->{'NUMROWS'}; | ||
| 900 | |||
| 901 | return kTableIndex($self->{'K'},$row,$column); | ||
| 902 | } | ||
| 903 | |||
| 904 | sub Trow | ||
| 905 | { | ||
| 906 | my $self = shift; | ||
| 907 | my $row = shift; | ||
| 908 | |||
| 909 | return undef unless defined $self->{'K'}; | ||
| 910 | return undef unless defined $self->{'COLS'}; | ||
| 911 | return undef unless $row >= 0 && $row < $self->{'NUMROWS'}; | ||
| 912 | |||
| 913 | my @rtn = (); | ||
| 914 | my $colidx = $self->{'NUMCOLS'} -1; | ||
| 915 | for my $col (0..$colidx) | ||
| 916 | { | ||
| 917 | push(@rtn, kTableIndex($self->{'K'},$row,$col)); | ||
| 918 | } | ||
| 919 | |||
| 920 | return \@rtn; | ||
| 921 | } | ||
| 922 | |||
| 923 | sub Theader | ||
| 924 | { | ||
| 925 | my $self = shift; | ||
| 926 | return undef unless defined $self->{'K'}; | ||
| 927 | return undef unless defined $self->{'COLS'}; | ||
| 928 | |||
| 929 | return k2parray(kTableH($self->{'K'})); | ||
| 930 | } | ||
| 931 | |||
| 932 | =head2 COMMANDS | ||
| 933 | |||
| 934 | Execute the code on an already accessable Kdb+ server. The query | ||
| 935 | is executed and the results are held in K structures in RAM. Example | ||
| 936 | |||
| 937 | $return = $k->cmd('b:til 100'); | ||
| 938 | |||
| 939 | If you just what to send a command to the Kdb+ server and not wait then | ||
| 940 | use the following. No return value is provided. | ||
| 941 | |||
| 942 | $k->whenever('b:til 100'); | ||
| 943 | |||
| 944 | The cmd() method also allows up to two extra arguments that are normally | ||
| 945 | K objects. You normally call cmd() this way when you have a function to | ||
| 946 | call. Here is a dodgy example. | ||
| 947 | |||
| 948 | my $data = $k->listof(length($arrsym),Kx::KG()); # list of length bytes | ||
| 949 | $data->setbin($arrsym); | ||
| 950 | |||
| 951 | $result = $k->cmd('{[x]insert[`mytab](0;x;.z.z)}',$data->val); | ||
| 952 | |||
| 953 | The cmd() function will return a reference to an array if the Q command | ||
| 954 | returns a list. It will return a simple scalar if the result is a scalar | ||
| 955 | response from Q. It will return a hash reference if the return result | ||
| 956 | from Q is either table/keyed table/dictionary. You need to know what you | ||
| 957 | are doing so can know what the result is (or use Perl's ref()). | ||
| 958 | |||
| 959 | Do not execute queries that return large 'keyed' tables as a copy of the table | ||
| 960 | in unkeyed form is held to convert to a Perl Hash before being freed. | ||
| 961 | |||
| 962 | Note: cmd() does not convert a keyed table to an unkeyed table in memory. | ||
| 963 | It holds onto what was passed back from KDB+ as is. If you want get at | ||
| 964 | the underlying K structure and change it use Tget() instead. Tget() will | ||
| 965 | convert a keyed table to an unkeyed table and hold it in memory. | ||
| 966 | |||
| 967 | If you have a Q script that you wish to run against the Kdb+ server you | ||
| 968 | can use the do(file) method. Any error in your script that is caught will | ||
| 969 | stop do(file) from proceeding. If you don't care when it is done then use | ||
| 970 | dolater(file). | ||
| 971 | |||
| 972 | Both do() and dolater() don't return anything useful. They just blindly | ||
| 973 | execute each line of Q against the server. If you want to check each | ||
| 974 | command and do stuff as a result then use cmd() and check the result. | ||
| 975 | |||
| 976 | An example file name foo.txt holds the lines: | ||
| 977 | |||
| 978 | t:([]a:();b:()) | ||
| 979 | insert[`t](`a;10.70) | ||
| 980 | insert[`t](`b;-5.6) | ||
| 981 | insert[`t](`c;21.73) | ||
| 982 | |||
| 983 | You can run that file by doing this: | ||
| 984 | |||
| 985 | $k->do("foo.txt"); | ||
| 986 | |||
| 987 | =cut | ||
| 988 | |||
| 989 | sub do | ||
| 990 | { | ||
| 991 | my $self = shift; | ||
| 992 | my $file = shift || return undef; | ||
| 993 | |||
| 994 | my $k; | ||
| 995 | open(F,$file) || return undef; | ||
| 996 | while(<F>) | ||
| 997 | { | ||
| 998 | chomp; | ||
| 999 | $k = k($self->{'kdb'},$_); | ||
| 1000 | if($k == 0) | ||
| 1001 | { | ||
| 1002 | dor0($k); # release memory | ||
| 1003 | return undef; | ||
| 1004 | } | ||
| 1005 | if(kType($k) == -128) | ||
| 1006 | { | ||
| 1007 | my $err = k2pscalar0($k); | ||
| 1008 | carp "Kx->do error $file $err on line $_\n"; | ||
| 1009 | return $err; | ||
| 1010 | } | ||
| 1011 | dor0($k); # release memory | ||
| 1012 | } | ||
| 1013 | return 1; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | sub cmd | ||
| 1017 | { | ||
| 1018 | my $self = shift; | ||
| 1019 | my $cmd = shift || return undef; | ||
| 1020 | my @arg = @_; | ||
| 1021 | |||
| 1022 | my $k; | ||
| 1023 | if(@arg > 2) | ||
| 1024 | { | ||
| 1025 | carp "Kx->cmd(self,cmd,arg1,arg2) max extra args is 2, use lists"; | ||
| 1026 | return undef; | ||
| 1027 | } | ||
| 1028 | return undef unless defined $self->{'kdb'}; | ||
| 1029 | |||
| 1030 | if(@arg == 1) | ||
| 1031 | { | ||
| 1032 | $k = k1($self->{'kdb'},$cmd,$arg[0]); | ||
| 1033 | } | ||
| 1034 | elsif(@arg == 2) | ||
| 1035 | { | ||
| 1036 | $k = k2($self->{'kdb'},$cmd,$arg[0],$arg[1]); | ||
| 1037 | } | ||
| 1038 | else | ||
| 1039 | { | ||
| 1040 | $k = k($self->{'kdb'},$cmd); | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | if($k == 0) | ||
| 1044 | { | ||
| 1045 | return undef; | ||
| 1046 | } | ||
| 1047 | |||
| 1048 | dor0($self->{'K'}) if defined $self->{'K'}; # release memory | ||
| 1049 | $self->{'K'} = $k; | ||
| 1050 | _val($k); | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | sub whenever | ||
| 1054 | { | ||
| 1055 | my $self = shift; | ||
| 1056 | my $cmd = shift || return undef; | ||
| 1057 | |||
| 1058 | return undef unless defined $self->{'kdb'}; | ||
| 1059 | |||
| 1060 | k(-($self->{'kdb'}),$cmd); | ||
| 1061 | return undef; | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | =head2 ATOMS and STRUCTURES | ||
| 1065 | |||
| 1066 | To create Kdb+ atoms locally in RAM use the following calls. | ||
| 1067 | |||
| 1068 | $k = new K; | ||
| 1069 | |||
| 1070 | my $d; | ||
| 1071 | $d=$k->bool(0); # boolean | ||
| 1072 | $d=$k->byte(100); # char | ||
| 1073 | $d=$k->char(ord('a')); # char | ||
| 1074 | $d=$k->short(20); | ||
| 1075 | $d=$k->int(70); | ||
| 1076 | $d=$k->long(93939); | ||
| 1077 | $d=$k->real(20.44); # remember 20.44 may look close as a real | ||
| 1078 | $d=$k->float(20.44); # should look closer to 20.44 as a float | ||
| 1079 | $d=$k->sym('mysymbol'); # A Kdb+ symbol | ||
| 1080 | $d=$k->date(2007,4,22); # integer encoded date year, month, day | ||
| 1081 | $d=$k->dt(time()); # Kdb+ datetime from epoch | ||
| 1082 | $d=$k->tm(100); # Time type in milliseconds | ||
| 1083 | |||
| 1084 | These allow for fine grained control over the 'type' of K object you | ||
| 1085 | want. If you don't mind particularly about the type conversions then you | ||
| 1086 | can use perl2K() like this. | ||
| 1087 | |||
| 1088 | $d = $k->perl2K('mysymbol'); | ||
| 1089 | $d = $k->perl2K([qw/this will be a K list of symbols/]); | ||
| 1090 | $d = $k->perl2K({this => 1, that => 2, 'is a' => 'dict'}); | ||
| 1091 | |||
| 1092 | To get a Perl value back from a Kdb+ atom try this; | ||
| 1093 | |||
| 1094 | my $val = $d->val(); | ||
| 1095 | |||
| 1096 | To get the internal value back from a Kdb+ atom try this; | ||
| 1097 | |||
| 1098 | my $val = $k->kval; # used in $x->cmd('func',$val) | ||
| 1099 | |||
| 1100 | As a further comment on the date() method. When you look at the value | ||
| 1101 | retuned from a date() call it is in epoch seconds. | ||
| 1102 | |||
| 1103 | $k = new K; | ||
| 1104 | my $date = $k->date(2007,4,22); | ||
| 1105 | print scalar localtime($date->val),"\n"; | ||
| 1106 | |||
| 1107 | Further more KDB+ Datetimes are held as a C double in memory. The | ||
| 1108 | integral part is the number of days since 1/1/2000 and the fractional | ||
| 1109 | part is the fraction of the day. You have some control over how datetimes | ||
| 1110 | are returned from KDB+ back into Perl data structures. By default a | ||
| 1111 | conversion to epoch seconds will be made. You can also get epoch seconds | ||
| 1112 | with milliseconds and you can also turn off conversion all together. | ||
| 1113 | |||
| 1114 | Kx::__Z2epoch(0); # turn off epoch conversion | ||
| 1115 | Kx::__Z2epoch(1); # turn on epoch conversion (default) | ||
| 1116 | Kx::__Z2epoch(2); # turn on epoch conversion plus milliseconds | ||
| 1117 | |||
| 1118 | These have immediate effects on how datetimes are converted into Perl | ||
| 1119 | data structures. These do not effect what is held in RAM after a call to | ||
| 1120 | KDB+ has been made, just how they are converted into Perl. | ||
| 1121 | |||
| 1122 | These methods use the underlying functions as listed below. Don't use | ||
| 1123 | these unless you know what your doing. They are listed here for | ||
| 1124 | completeness and so you can use them if you really want. But don't. | ||
| 1125 | |||
| 1126 | Kx::kb(integer) => Create boolean 0|1 | ||
| 1127 | Kx::kg(integer) => Create a byte/char | ||
| 1128 | Kx::kh(integer) => Create a short | ||
| 1129 | Kx::ki(integer) => Create and integer | ||
| 1130 | Kx::kj(longval) => Create a long | ||
| 1131 | Kx::ke(realval) => Create a real | ||
| 1132 | Kx::kf(floatval) => Create a float | ||
| 1133 | Kx::kc(charval) => Create a char from an int ord() | ||
| 1134 | Kx::ks(symbol) => Create a symbol from a string | ||
| 1135 | Kx::kd(date) => Create a date - See K dates | ||
| 1136 | Kx::kz(datetime) => Create a datetime - See K dates | ||
| 1137 | Kx::kt(time) => Create a time | ||
| 1138 | |||
| 1139 | Kx::p2k($ref) => return a K structure describing the Reference | ||
| 1140 | Kx::k2p(K) => return a Perl structure from a Kdb+ structure | ||
| 1141 | |||
| 1142 | Kx::k2pscalar(K) => return a scalar from a Kdb+ atom | ||
| 1143 | Kx::k2parray(K) => return an array from a Kdb+ list | ||
| 1144 | Kx::k2phash(K) => return a hash from a Kdb+ dict/table | ||
| 1145 | |||
| 1146 | Kx::phash2k($href) => return a Kdb+ dict from a Perl hash ref | ||
| 1147 | Kx::parray2k($aref) => return a Kdb+ list from a Perl array ref | ||
| 1148 | Kx::pscalar2k($srf) => return a Kdb+ atom from a Perl scalar ref | ||
| 1149 | |||
| 1150 | Example: | ||
| 1151 | |||
| 1152 | # Simple create | ||
| 1153 | my $k = new K; | ||
| 1154 | my $bool = $k->bool(0); | ||
| 1155 | print "My boolean in K is ",$bool->val,"\n"; | ||
| 1156 | |||
| 1157 | =cut | ||
| 1158 | |||
| 1159 | # Create an atom in a generic fashion not an OO function call. Creates a | ||
| 1160 | # K object as its return value | ||
| 1161 | sub _atom | ||
| 1162 | { | ||
| 1163 | my ($val,$code) = @_; | ||
| 1164 | |||
| 1165 | # Check for null | ||
| 1166 | return(undef) unless(defined $val); | ||
| 1167 | |||
| 1168 | # Default to creating symbols | ||
| 1169 | $code = \&ks unless defined $code; | ||
| 1170 | |||
| 1171 | my $k = Kx->new(); | ||
| 1172 | $k->{'K'} = $code->($val); | ||
| 1173 | return $k; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | # There is very little checking done by this code. | ||
| 1177 | sub bool { _atom($_[1],\&kb); } | ||
| 1178 | sub byte { _atom($_[1],\&kg); } | ||
| 1179 | sub short { _atom($_[1],\&kh); } | ||
| 1180 | sub int { _atom($_[1],\&ki); } | ||
| 1181 | sub long { _atom($_[1],\&kj); } | ||
| 1182 | sub real { _atom($_[1],\&ke); } | ||
| 1183 | sub float { _atom($_[1],\&kf); } | ||
| 1184 | sub char { _atom($_[1],\&kc); } | ||
| 1185 | sub sym { _atom($_[1],\&ks); } | ||
| 1186 | sub date { _atom(ymd($_[1],$_[2],$_[3]),\&kd); } | ||
| 1187 | sub month { _atom($_[1],\&ki); } | ||
| 1188 | sub second { _atom($_[1],\&ki); } | ||
| 1189 | sub dt { _atom(epoch2Z($_[1]),\&kz); } | ||
| 1190 | sub tm { _atom($_[1],\&kt); } | ||
| 1191 | |||
| 1192 | sub perl2k | ||
| 1193 | { | ||
| 1194 | my $self = shift; | ||
| 1195 | my $v = shift || return undef; | ||
| 1196 | |||
| 1197 | my $k = Kx->new(); | ||
| 1198 | if(ref($v)) | ||
| 1199 | { | ||
| 1200 | if(ref($v) eq 'SCALAR' || ref($v) eq 'ARRAY' || ref($v) eq 'HASH') | ||
| 1201 | { | ||
| 1202 | $k->{'K'} = p2k($v); | ||
| 1203 | } | ||
| 1204 | else | ||
| 1205 | { | ||
| 1206 | carp("Kx->perl2k(x): x can only be a ref to scalar, array, hash"); | ||
| 1207 | } | ||
| 1208 | } | ||
| 1209 | else | ||
| 1210 | { | ||
| 1211 | $k->{'K'} = p2k(\$v); | ||
| 1212 | } | ||
| 1213 | return $k; | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | sub val | ||
| 1217 | { | ||
| 1218 | my $self = shift; | ||
| 1219 | return _val($self->{'K'}); | ||
| 1220 | } | ||
| 1221 | |||
| 1222 | sub _val | ||
| 1223 | { | ||
| 1224 | my $k = shift; | ||
| 1225 | |||
| 1226 | return undef unless defined $k; | ||
| 1227 | |||
| 1228 | my $type = kType($k); | ||
| 1229 | if($type == -128) # Its an error | ||
| 1230 | { | ||
| 1231 | my $err = k2pscalar0($k); | ||
| 1232 | carp "K error: $err \n"; | ||
| 1233 | return $err; | ||
| 1234 | } | ||
| 1235 | elsif($type >= 0 && $type < KT()) # Its a list | ||
| 1236 | { | ||
| 1237 | my $ref = k2parray($k); | ||
| 1238 | |||
| 1239 | # Now if its a list of char vals or a list of bytes then we | ||
| 1240 | # convert it to a single string, possibly binary | ||
| 1241 | if($type == KG() || $type == KC()) | ||
| 1242 | { | ||
| 1243 | return $ref->[0]; | ||
| 1244 | } | ||
| 1245 | else # default return array ref | ||
| 1246 | { | ||
| 1247 | return $ref; | ||
| 1248 | } | ||
| 1249 | } | ||
| 1250 | elsif($type < 0 || $type == 98 || $type == 99) # Scalar/Hash etc | ||
| 1251 | { | ||
| 1252 | return k2p($k); | ||
| 1253 | } | ||
| 1254 | else # nothing to return so say OK | ||
| 1255 | { | ||
| 1256 | return 1; | ||
| 1257 | } | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | sub kval | ||
| 1261 | { | ||
| 1262 | return undef unless defined $_[0]->{'K'}; | ||
| 1263 | return $_[0]->{'K'}; | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | =head2 LISTS | ||
| 1267 | |||
| 1268 | These list functions create in memory local lists outside of any 'q' | ||
| 1269 | running process. These will allow you to create very large simple lists | ||
| 1270 | without blowing out all your memory. | ||
| 1271 | |||
| 1272 | To create a simple Kdb+ list of a single type use the listof() function. | ||
| 1273 | The type of the list is passed in as the second aregument and can be one | ||
| 1274 | of: | ||
| 1275 | |||
| 1276 | Kx::KC() char | ||
| 1277 | Kx::KD() date yyyy mm dd | ||
| 1278 | Kx::KE() real | ||
| 1279 | Kx::KF() float | ||
| 1280 | Kx::KG() byte | ||
| 1281 | Kx::KH() short | ||
| 1282 | Kx::KI() integer | ||
| 1283 | Kx::KJ() long | ||
| 1284 | Kx::KM() month | ||
| 1285 | Kx::KS() symbol (internalised string) | ||
| 1286 | Kx::KT() time | ||
| 1287 | Kx::KU() minute | ||
| 1288 | Kx::KV() second | ||
| 1289 | Kx::KZ() datetime epoch seconds | ||
| 1290 | |||
| 1291 | Example simple lists: | ||
| 1292 | |||
| 1293 | my $k = new K; | ||
| 1294 | my $list = $k->listof(20,Kx::KS()); # List of 20 symbols | ||
| 1295 | for( my $i=0; $i < 20; $i++) | ||
| 1296 | { | ||
| 1297 | $list->at($i,"symbol$i"); | ||
| 1298 | } | ||
| 1299 | |||
| 1300 | # To get at the 4th element | ||
| 1301 | my $sym = $list->at(3); # symbol3 | ||
| 1302 | |||
| 1303 | my $perl_list = $list->list; | ||
| 1304 | print "Symbols are @$perl_list\n"; | ||
| 1305 | |||
| 1306 | # dates | ||
| 1307 | $d = $k->listof(20,Kx::KD()); | ||
| 1308 | for( my $i=0; $i < 20; $i++) | ||
| 1309 | { | ||
| 1310 | $d->at($i,2007,4,$i+1); # 20070401 -> 20070421 | ||
| 1311 | } | ||
| 1312 | |||
| 1313 | # Add an extra date to the end of the list | ||
| 1314 | my $day = $k->date(2007,4,30); | ||
| 1315 | $d->joinatom($day->kval); | ||
| 1316 | |||
| 1317 | There is also another method defined setbin() that sets binary | ||
| 1318 | data into a list of bytes. You can use this to save serialised Perl | ||
| 1319 | data structures into Kdb+ tables (much like a blob or text field in SQL | ||
| 1320 | DBs). Here is an example: | ||
| 1321 | |||
| 1322 | use K; | ||
| 1323 | use Compress::Zlib qw/compress uncompress/; | ||
| 1324 | use Data::Dumper; | ||
| 1325 | $Data::Dumper::Indent = 0; # no newlines, important | ||
| 1326 | |||
| 1327 | my $k = new K(host=>"localhost", port=>2222, check_for_errors=>1); | ||
| 1328 | my $rv = $k->connect; | ||
| 1329 | die "Can't connect to KDB+ " unless $rv; | ||
| 1330 | |||
| 1331 | # create new table in q | ||
| 1332 | $k->Tnew(name=>'mytab',cols=>[qw/id data ts/]); | ||
| 1333 | |||
| 1334 | # Build a large complicated Perl structure. | ||
| 1335 | my $arr = { a=>['a','b','c',1,2,3], b=>'this is a test'}; | ||
| 1336 | for (0..10000) | ||
| 1337 | { | ||
| 1338 | $arr->{$_} = {$_ => $_}; | ||
| 1339 | $arr->{"a$_"} = [$_, $_]; | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | # Serialise it as a compressed piece of data | ||
| 1343 | my $arrsym = Dumper($arr); | ||
| 1344 | print "Dumper size is: ", length $arrsym, "\n"; | ||
| 1345 | $arrsym = compress( $arrsym ); | ||
| 1346 | print "Compress Dumper size is: ", length $arrsym, "\n"; | ||
| 1347 | |||
| 1348 | # An in memory Kdb+ list of bytes to hold the compressed data | ||
| 1349 | my $data = $k->listof(length($arrsym),Kx::KG()); # list of length bytes | ||
| 1350 | $data->setbin($arrsym); | ||
| 1351 | |||
| 1352 | # Insert it into a table using a function call. | ||
| 1353 | $k->cmd('{[x]insert[`mytab](0;x;.z.z)}',$data->kval); | ||
| 1354 | |||
| 1355 | # Select a single row from the table, and return it's data | ||
| 1356 | $binary = $k->cmd('(select data from mytab where id=0)[0;`data]'); | ||
| 1357 | |||
| 1358 | # Get the data back into Perl string form | ||
| 1359 | $arrsym = uncompress($binary); | ||
| 1360 | #print $arrsym,"\n"; | ||
| 1361 | |||
| 1362 | # Eval the string into a Perl data structure the hard way | ||
| 1363 | my $VAR1; | ||
| 1364 | eval $arrsym; | ||
| 1365 | print $VAR1->{'b'},"\n"; | ||
| 1366 | |||
| 1367 | |||
| 1368 | =head2 Mixed Lists | ||
| 1369 | |||
| 1370 | We only handle straight lists. By that I mean single dimensional mixed | ||
| 1371 | lists. No lists of list only lists of simple atoms of mixed type | ||
| 1372 | |||
| 1373 | # The zero in line below says its to be a mixed list | ||
| 1374 | my $list = $k->listof(40,0); # mixed list 40 elements | ||
| 1375 | $list->at(0,$k->float(22.22)); | ||
| 1376 | |||
| 1377 | $list->at(1,$k->symbol('this is a test')); | ||
| 1378 | . | ||
| 1379 | . | ||
| 1380 | $list->at(39,$k->date(2007,2,28)); | ||
| 1381 | |||
| 1382 | This is handy for creating multiple arguments to a KDB+ function call. | ||
| 1383 | |||
| 1384 | =cut | ||
| 1385 | |||
| 1386 | sub joinatom | ||
| 1387 | { | ||
| 1388 | my $self = shift; | ||
| 1389 | my $atom = shift; | ||
| 1390 | |||
| 1391 | no warnings; | ||
| 1392 | $self->{'K'} = call_ja($self->{'K'},$atom->val); | ||
| 1393 | } | ||
| 1394 | |||
| 1395 | sub list | ||
| 1396 | { | ||
| 1397 | my $self = shift; | ||
| 1398 | return undef unless defined $self->{'K'}; | ||
| 1399 | |||
| 1400 | return k2parray($self->{'K'}); | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | sub av2k | ||
| 1404 | { | ||
| 1405 | my $self=shift; | ||
| 1406 | my ($typ,$aref) = @_; | ||
| 1407 | |||
| 1408 | unless(defined $typ && $typ > 0 && $typ < 20) | ||
| 1409 | { | ||
| 1410 | carp "Kx->av2k(self,x,typ) typ must be @TYP\n"; | ||
| 1411 | return undef; | ||
| 1412 | } | ||
| 1413 | |||
| 1414 | dor0($self->{'K'}) if defined $self->{'K'}; # release memory | ||
| 1415 | |||
| 1416 | $self->{'K'} = newKarray($typ,$aref); | ||
| 1417 | return $self; | ||
| 1418 | } | ||
| 1419 | |||
| 1420 | sub listof | ||
| 1421 | { | ||
| 1422 | my $self=shift; | ||
| 1423 | my ($x,$typ) = @_; | ||
| 1424 | |||
| 1425 | if($x <= 0) | ||
| 1426 | { | ||
| 1427 | carp "Kx->listof(self,x,typ) x=$x less than zero "; | ||
| 1428 | return undef; | ||
| 1429 | } | ||
| 1430 | unless(defined $typ && $typ >= 0 && $typ < 20) | ||
| 1431 | { | ||
| 1432 | carp "Kx->listof(self,x,typ) typ must be @TYP\n"; | ||
| 1433 | return undef; | ||
| 1434 | } | ||
| 1435 | |||
| 1436 | my $k = Kx->new(); | ||
| 1437 | $k->{'K'} = ktn($typ,$x); | ||
| 1438 | return $k; | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | sub getbin | ||
| 1442 | { | ||
| 1443 | my $self=shift; | ||
| 1444 | my $ref = getKarraybinary($self->{'K'},0,0); | ||
| 1445 | return $ref; | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | sub setbin | ||
| 1449 | { | ||
| 1450 | my $self=shift; | ||
| 1451 | my ($val) = @_; | ||
| 1452 | |||
| 1453 | unless(setKarraybinary($self->{'K'},0,$val)) | ||
| 1454 | { | ||
| 1455 | carp "Kx->setbin error in binary copy\n"; | ||
| 1456 | return 0; | ||
| 1457 | } | ||
| 1458 | return 1; | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | sub at | ||
| 1462 | { | ||
| 1463 | my $self=shift; | ||
| 1464 | my ($x,@val) = @_; | ||
| 1465 | |||
| 1466 | # What type am I? | ||
| 1467 | my $mytype = kType($self->{'K'}); | ||
| 1468 | |||
| 1469 | # I need to be a list | ||
| 1470 | unless($mytype >=0) | ||
| 1471 | { | ||
| 1472 | carp "Can't call Kx->at() on a non list K onject"; | ||
| 1473 | return undef; | ||
| 1474 | } | ||
| 1475 | |||
| 1476 | # If it is a set operation | ||
| 1477 | if(defined $val[0]) | ||
| 1478 | { | ||
| 1479 | my $k; | ||
| 1480 | |||
| 1481 | # Want to store only Kdb+ structure directly or via an object for | ||
| 1482 | # mixed lists and scalars for simple lists. | ||
| 1483 | # Here we work out where the $k variable will come from | ||
| 1484 | if($mytype == 0 && ref($val[0]) eq 'KstructPtr') | ||
| 1485 | { | ||
| 1486 | $k = $val[0]; | ||
| 1487 | } | ||
| 1488 | elsif($mytype == 0 && ref($val[0]) eq 'Kx') | ||
| 1489 | { | ||
| 1490 | $k = $val[0]->{'K'}; | ||
| 1491 | } | ||
| 1492 | elsif($mytype > 0 && $#val == 2) # Assume a date yyyy,mm,dd | ||
| 1493 | { | ||
| 1494 | $k = ymd(@val); | ||
| 1495 | } | ||
| 1496 | elsif($mytype > 0) | ||
| 1497 | { | ||
| 1498 | $k = $val[0]; | ||
| 1499 | } | ||
| 1500 | else | ||
| 1501 | { | ||
| 1502 | carp "Kx->at(pos,val) Invalid val type for this list type"; | ||
| 1503 | return undef; | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | # Am I a List of K objects? | ||
| 1507 | if( $mytype == 0) | ||
| 1508 | { | ||
| 1509 | # $k is of type K | ||
| 1510 | unless(setKarraymixed($self->{'K'},$x,$k)) | ||
| 1511 | { | ||
| 1512 | carp "Kx->at(pos,val) pos=$x out of bounds? "; | ||
| 1513 | } | ||
| 1514 | return 0; | ||
| 1515 | } | ||
| 1516 | else # Simple list | ||
| 1517 | { | ||
| 1518 | # $k is a Perl scalar | ||
| 1519 | unless(setKarraysimple($self->{'K'},$x,$k)) | ||
| 1520 | { | ||
| 1521 | carp "Kx->at(pos,val) type mismatch or pos=$x out of bounds"; | ||
| 1522 | } | ||
| 1523 | return 0; | ||
| 1524 | } | ||
| 1525 | return undef; | ||
| 1526 | } | ||
| 1527 | else # A get operation | ||
| 1528 | { | ||
| 1529 | return getKarray($self->{'K'},$x); | ||
| 1530 | } | ||
| 1531 | } | ||
| 1532 | |||
| 1533 | =head2 Utility Methods | ||
| 1534 | |||
| 1535 | $k->dump0() will return a string describing the under lying K structure. | ||
| 1536 | |||
| 1537 | Kx::dump($k) will print out the K structure of $k | ||
| 1538 | |||
| 1539 | $sym = Kx::makesym("string") will convert a simple string into a quoted | ||
| 1540 | symbol suitable for usage in KDB+ | ||
| 1541 | |||
| 1542 | make_C() will convert its argument into a suitable string quoted | ||
| 1543 | as a KDB+ character list. | ||
| 1544 | |||
| 1545 | my $c = Kx::make_C("now is\tthe \n time for \n help"); | ||
| 1546 | |||
| 1547 | There is also make_s(): | ||
| 1548 | |||
| 1549 | my $sym = Kx::make_s("a symbol"); # `$"a symbol" | ||
| 1550 | my $sym = Kx::make_s(undef); # a null symbol ` | ||
| 1551 | |||
| 1552 | =cut | ||
| 1553 | |||
| 1554 | sub make_s | ||
| 1555 | { | ||
| 1556 | if(!defined $_[0]) | ||
| 1557 | { | ||
| 1558 | return '`'; | ||
| 1559 | } | ||
| 1560 | return "`\$\"$_[0]\""; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | sub make_C | ||
| 1564 | { | ||
| 1565 | if(!defined $_[0]) | ||
| 1566 | { | ||
| 1567 | return $NULL{'char'}; | ||
| 1568 | } | ||
| 1569 | return '"c"$0x' . join('', map { sprintf "%02x", $_ } unpack("C*",$_[0])); | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | sub make_i | ||
| 1573 | { | ||
| 1574 | if(!defined $_[0]) | ||
| 1575 | { | ||
| 1576 | return $NULL{'int'}; | ||
| 1577 | } | ||
| 1578 | return $_[0]+0; | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | sub _getepoch | ||
| 1582 | { | ||
| 1583 | if($_[0] =~ /^now/io) | ||
| 1584 | { | ||
| 1585 | return time; | ||
| 1586 | } | ||
| 1587 | elsif($_[0] =~ /^never/io) | ||
| 1588 | { | ||
| 1589 | return 1999999999; | ||
| 1590 | } | ||
| 1591 | else | ||
| 1592 | { | ||
| 1593 | return $_[0]; | ||
| 1594 | } | ||
| 1595 | } | ||
| 1596 | |||
| 1597 | sub make_z_epoch_gmt | ||
| 1598 | { | ||
| 1599 | if(!defined $_[0]) | ||
| 1600 | { | ||
| 1601 | return $NULL{'datetime'}; | ||
| 1602 | } | ||
| 1603 | return POSIX::strftime "%Y.%m.%dT%H:%M:%S", gmtime(_getepoch($_[0])); | ||
| 1604 | } | ||
| 1605 | |||
| 1606 | sub make_z_epoch_local | ||
| 1607 | { | ||
| 1608 | if(!defined $_[0]) | ||
| 1609 | { | ||
| 1610 | return $NULL{'datetime'}; | ||
| 1611 | } | ||
| 1612 | return POSIX::strftime "%Y.%m.%dT%H:%M:%S", localtime(_getepoch($_[0])); | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | sub dump0 | ||
| 1616 | { | ||
| 1617 | my $self = shift; | ||
| 1618 | |||
| 1619 | return undef unless defined $self->{'K'}; | ||
| 1620 | my $refcnt = kRefCnt($self->{'K'}); | ||
| 1621 | my $type = kType($self->{'K'}); | ||
| 1622 | my $att = kAtt($self->{'K'}); | ||
| 1623 | my $num = kNum($self->{'K'}); | ||
| 1624 | my $val = k2pscalar($self->{'K'}); | ||
| 1625 | |||
| 1626 | return "{Value: $val, RefCnt=>$refcnt, Type=>$type, Att=>$att, Num=>$num}\n"; | ||
| 1627 | |||
| 1628 | } | ||
| 1629 | |||
| 1630 | sub dump | ||
| 1631 | { | ||
| 1632 | my $k = shift; | ||
| 1633 | |||
| 1634 | return undef unless defined $k; | ||
| 1635 | |||
| 1636 | my $refcnt = kRefCnt($k); | ||
| 1637 | my $type = kType($k); | ||
| 1638 | my $att = kAtt($k); | ||
| 1639 | my $num = kNum($k); | ||
| 1640 | my $val = k2pscalar($k) || 'no value'; | ||
| 1641 | |||
| 1642 | print "{Value: $val, RefCnt=>$refcnt, Type=>$type, Att=>$att, Num=>$num}\n"; | ||
| 1643 | |||
| 1644 | } | ||
| 1645 | |||
| 1646 | sub DESTROY | ||
| 1647 | { | ||
| 1648 | my $self = shift; | ||
| 1649 | my $name = $self->{'name'} || return; | ||
| 1650 | |||
| 1651 | return unless defined $self->{'K'}; | ||
| 1652 | return unless ref($self->{'K'}) eq 'KstructPtr'; | ||
| 1653 | |||
| 1654 | dor0($self->{'K'}); | ||
| 1655 | |||
| 1656 | $DB{$name}{'count'}--; | ||
| 1657 | if($DB{$name}{'count'} <= 0 && exists $DB{$name}{'kdb'}) | ||
| 1658 | { | ||
| 1659 | POSIX::close($DB{$name}{'kdb'}); | ||
| 1660 | undef($DB{$name}); | ||
| 1661 | } | ||
| 1662 | undef $self->{'K'}; | ||
| 1663 | undef $self->{'COLS'}; | ||
| 1664 | } | ||
| 1665 | |||
| 1666 | sub dor0 | ||
| 1667 | { | ||
| 1668 | my $k = shift; | ||
| 1669 | unless(defined $k) | ||
| 1670 | { | ||
| 1671 | carp &whowasi; | ||
| 1672 | carp "Kx::dor0() must be called with an argument: "; | ||
| 1673 | } | ||
| 1674 | unless(ref($k) eq 'KstructPtr') | ||
| 1675 | { | ||
| 1676 | carp &whowasi; | ||
| 1677 | carp "Kx::dor0() argument not a KstructPtr: ", ref($k), ": "; | ||
| 1678 | } | ||
| 1679 | r0($k); | ||
| 1680 | } | ||
| 1681 | |||
| 1682 | |||
| 1683 | ##################################################################### | ||
| 1684 | # K List package # | ||
| 1685 | ##################################################################### | ||
| 1686 | # A class implementing a tied ordinary array should define the following | ||
| 1687 | # methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE and perhaps UNTIE | ||
| 1688 | # and/or DESTROY. | ||
| 1689 | # | ||
| 1690 | # FETCHSIZE and STORESIZE are used to provide $#array and equivalent | ||
| 1691 | # "scalar(@array)" access. | ||
| 1692 | # | ||
| 1693 | # The methods POP, PUSH, SHIFT, UNSHIFT, SPLICE, DELETE, and EXISTS | ||
| 1694 | # | ||
| 1695 | |||
| 1696 | =head1 Kx::LIST | ||
| 1697 | |||
| 1698 | You may wish to tie a Perl array to a Kdb+ variable. Well, you can do | ||
| 1699 | that as well. Try something like this: | ||
| 1700 | |||
| 1701 | use K; | ||
| 1702 | |||
| 1703 | my %config = ( | ||
| 1704 | host=>"localhost", | ||
| 1705 | port=>2222, | ||
| 1706 | userpass=>'user:pass', # optional | ||
| 1707 | type=>'symbol', | ||
| 1708 | list=>'d', | ||
| 1709 | create=>1 | ||
| 1710 | ); | ||
| 1711 | tie(@a, 'Kx::LIST', %config); | ||
| 1712 | |||
| 1713 | # push lost of stuff on an array | ||
| 1714 | my @array = (qw/aaaa bbbbb ccccc ddddddddd e f j h i j k l/) x 30000 | ||
| 1715 | ; | ||
| 1716 | push(@a,@array); | ||
| 1717 | push(@a,@array); | ||
| 1718 | push(@a,@array); | ||
| 1719 | print "\@a has ", scalar(@a)," elements\n"; | ||
| 1720 | |||
| 1721 | # Store | ||
| 1722 | $a[3] = "Help me"; | ||
| 1723 | print "Elementt 3 is ",$a[3],"\n"; | ||
| 1724 | |||
| 1725 | All the functions defined in perltie for lists are included. | ||
| 1726 | |||
| 1727 | Note: 'type' is a Kdb+ type as defined in Types below - it is the | ||
| 1728 | type for the array. Only simple types are allowed at the moment. | ||
| 1729 | |||
| 1730 | =cut | ||
| 1731 | |||
| 1732 | package Kx::LIST; | ||
| 1733 | |||
| 1734 | use 5.008; | ||
| 1735 | use strict; | ||
| 1736 | use warnings; | ||
| 1737 | use Carp; | ||
| 1738 | sub whowasi { (caller(1))[3] . '()' } | ||
| 1739 | |||
| 1740 | sub TIEARRAY | ||
| 1741 | { | ||
| 1742 | carp &whowasi if $DEBUG; | ||
| 1743 | my $class = shift; | ||
| 1744 | my %opts = @_; | ||
| 1745 | my $d; # list | ||
| 1746 | |||
| 1747 | my $name = $opts{'name'} || 'default'; | ||
| 1748 | my $ref = {'name' => $name}; | ||
| 1749 | if(defined $opts{'host'}) | ||
| 1750 | { | ||
| 1751 | $DB{$name}{'host'} = $opts{'host'}; | ||
| 1752 | } | ||
| 1753 | if(defined $opts{'port'}) | ||
| 1754 | { | ||
| 1755 | $DB{$name}{'port'} = $opts{'port'}; | ||
| 1756 | } | ||
| 1757 | if(defined $opts{'list'}) | ||
| 1758 | { | ||
| 1759 | $ref->{'list'} = $opts{'list'}; | ||
| 1760 | } | ||
| 1761 | if(defined $opts{'create'}) | ||
| 1762 | { | ||
| 1763 | $ref->{'create'} = 1; | ||
| 1764 | } | ||
| 1765 | my $type = $opts{'type'} || 'symbol'; | ||
| 1766 | $ref->{'type'} = $type; | ||
| 1767 | |||
| 1768 | return undef unless exists $CAST{$type}; | ||
| 1769 | return undef unless defined $DB{$name}{'host'} && defined $DB{$name}{'port'}; | ||
| 1770 | return undef unless defined $ref->{'list'}; | ||
| 1771 | |||
| 1772 | # Get hold of any previously defined connection handle | ||
| 1773 | if(defined $DB{$name}{'kdb'}) | ||
| 1774 | { | ||
| 1775 | $ref->{'kdb'} = $DB{$name}{'kdb'}; # no need for connect | ||
| 1776 | $DB{$name}{'count'}++; | ||
| 1777 | } | ||
| 1778 | else # get connected a new | ||
| 1779 | { | ||
| 1780 | if(defined $DB{$name}{'userpass'}) | ||
| 1781 | { | ||
| 1782 | $ref->{'kdb'} = Kx::khpu($DB{$name}{'host'}, | ||
| 1783 | $DB{$name}{'port'}, | ||
| 1784 | $DB{$name}{'userpass'}); | ||
| 1785 | } | ||
| 1786 | # host, port only | ||
| 1787 | else | ||
| 1788 | { | ||
| 1789 | $ref->{'kdb'} = Kx::khp($DB{$name}{'host'}, $DB{$name}{'port'}); | ||
| 1790 | } | ||
| 1791 | unless($ref->{'kdb'} > 0) | ||
| 1792 | { | ||
| 1793 | undef $ref->{'kdb'}; | ||
| 1794 | return undef; | ||
| 1795 | } | ||
| 1796 | $DB{$name}{'kdb'} = $ref->{'kdb'}; | ||
| 1797 | $DB{$name}{'count'}++; | ||
| 1798 | } | ||
| 1799 | |||
| 1800 | # OK check if the variable already exists. If not then create it | ||
| 1801 | my $var = Kx::k2parray0(Kx::k($ref->{'kdb'},'\v')); | ||
| 1802 | $d = $ref->{'list'}; | ||
| 1803 | if(!grep(/^$d$/,@$var) || defined $ref->{'create'}) | ||
| 1804 | { | ||
| 1805 | my $r = Kx::k($ref->{'kdb'},"$d:()"); | ||
| 1806 | if($r == 0) | ||
| 1807 | { | ||
| 1808 | carp "Undefined K structure in TIELIST\n"; | ||
| 1809 | return undef; | ||
| 1810 | } | ||
| 1811 | if(Kx::kType($r) < 0) | ||
| 1812 | { | ||
| 1813 | carp "Kx::LIST error ", Kx::k2pscalar($r),"\n"; | ||
| 1814 | return undef; | ||
| 1815 | } | ||
| 1816 | $ref->{'count'} = 0; | ||
| 1817 | } | ||
| 1818 | |||
| 1819 | |||
| 1820 | return bless $ref, $class; | ||
| 1821 | } | ||
| 1822 | |||
| 1823 | sub FETCH | ||
| 1824 | { | ||
| 1825 | my $self = shift; | ||
| 1826 | my $i = shift; | ||
| 1827 | |||
| 1828 | my $list = $self->{'list'}; | ||
| 1829 | return undef if $i < 0 || $i >= $self->{'count'}; | ||
| 1830 | |||
| 1831 | $self->{'val'} = Kx::k2pscalar0(Kx::k($self->{'kdb'},"$list\[$i\]")); | ||
| 1832 | return $self->{'val'}; | ||
| 1833 | } | ||
| 1834 | |||
| 1835 | sub STORE | ||
| 1836 | { | ||
| 1837 | my $self = shift; | ||
| 1838 | my $i = shift; | ||
| 1839 | my $val = shift; | ||
| 1840 | my $list = $self->{'list'}; | ||
| 1841 | |||
| 1842 | return undef if $i < 0; | ||
| 1843 | $self->EXTEND($i) if $i >= $self->{'count'}; | ||
| 1844 | |||
| 1845 | # Cast the value | ||
| 1846 | $val = $CAST{$self->{'type'}} . $val; | ||
| 1847 | $val .= '"' if $self->{'type'} eq 'symbol'; | ||
| 1848 | |||
| 1849 | $self->{'val'} = Kx::k2pscalar0(Kx::k($self->{'kdb'},"$list\[$i\]:$val")); | ||
| 1850 | return $self->{'val'}; | ||
| 1851 | } | ||
| 1852 | |||
| 1853 | sub FETCHSIZE | ||
| 1854 | { | ||
| 1855 | my $self = shift; | ||
| 1856 | my $list = $self->{'list'}; | ||
| 1857 | |||
| 1858 | $self->{'count'} = Kx::k2pscalar0(Kx::k($self->{'kdb'},"count $list")); | ||
| 1859 | return $self->{'count'}; | ||
| 1860 | } | ||
| 1861 | |||
| 1862 | # STORESIZE this, count | ||
| 1863 | # Sets the total number of items in the tied array associated with | ||
| 1864 | # object this to be count. If this makes the array larger then | ||
| 1865 | # class's mapping of "undef" should be returned for new positions. | ||
| 1866 | # If the array becomes smaller then entries beyond count should be | ||
| 1867 | # deleted. | ||
| 1868 | # | ||
| 1869 | sub STORESIZE | ||
| 1870 | { | ||
| 1871 | my $self = shift; | ||
| 1872 | my $count = shift; | ||
| 1873 | my $list = $self->{'list'}; | ||
| 1874 | my $oldsz = $self->{'count'}; | ||
| 1875 | |||
| 1876 | my $q = ''; | ||
| 1877 | if($count < $oldsz) | ||
| 1878 | { | ||
| 1879 | # Truncate the list | ||
| 1880 | $q = "$list:$count#$list"; | ||
| 1881 | } | ||
| 1882 | elsif($count > $oldsz) | ||
| 1883 | { | ||
| 1884 | my $null = $NULL{$self->{'type'}}; | ||
| 1885 | my $diff = $count - $oldsz; | ||
| 1886 | $q = "$list:$list,$diff#$null"; | ||
| 1887 | } | ||
| 1888 | else | ||
| 1889 | { | ||
| 1890 | # nothing to do | ||
| 1891 | } | ||
| 1892 | |||
| 1893 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 1894 | Kx::dor0($r); | ||
| 1895 | |||
| 1896 | $self->{'count'} = $count; | ||
| 1897 | } | ||
| 1898 | |||
| 1899 | sub EXTEND | ||
| 1900 | { | ||
| 1901 | my $self = shift; | ||
| 1902 | my $count = shift; | ||
| 1903 | $self->STORESIZE($count); | ||
| 1904 | } | ||
| 1905 | |||
| 1906 | sub CLEAR | ||
| 1907 | { | ||
| 1908 | my $self = shift; | ||
| 1909 | my $list = $self->{'list'}; | ||
| 1910 | $self->{'count'} = 0; | ||
| 1911 | |||
| 1912 | my $q = "$list:()"; | ||
| 1913 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 1914 | Kx::dor0($r); | ||
| 1915 | } | ||
| 1916 | |||
| 1917 | sub UNTIE | ||
| 1918 | { | ||
| 1919 | carp &whowasi if $DEBUG; | ||
| 1920 | my $self = shift; | ||
| 1921 | Kx::DESTROY($self); | ||
| 1922 | } | ||
| 1923 | |||
| 1924 | |||
| 1925 | sub POP | ||
| 1926 | { | ||
| 1927 | my $self = shift; | ||
| 1928 | my $list = $self->{'list'}; | ||
| 1929 | |||
| 1930 | return undef if $self->{'count'} <= 0; | ||
| 1931 | |||
| 1932 | $self->{'count'}--; | ||
| 1933 | |||
| 1934 | # Get value | ||
| 1935 | my $q = "-1#$list"; | ||
| 1936 | $self->{'val'} = Kx::k2parray0(Kx::k($self->{'kdb'},$q)); | ||
| 1937 | |||
| 1938 | # reduce list | ||
| 1939 | $q = "$list:-1_$list"; | ||
| 1940 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 1941 | Kx::dor0($r); | ||
| 1942 | |||
| 1943 | return $self->{'val'}[0]; | ||
| 1944 | } | ||
| 1945 | |||
| 1946 | sub PUSH | ||
| 1947 | { | ||
| 1948 | my $self = shift; | ||
| 1949 | my @arr = @_; | ||
| 1950 | my $list = $self->{'list'}; | ||
| 1951 | my $cast = $CAST{$self->{'type'}}; | ||
| 1952 | |||
| 1953 | my $issymbol = $self->{'type'} eq 'symbol'; | ||
| 1954 | |||
| 1955 | # Do N at a time | ||
| 1956 | while(@arr) | ||
| 1957 | { | ||
| 1958 | my $q = "$list:$list,("; | ||
| 1959 | for my $v (splice(@arr,0,500)) | ||
| 1960 | { | ||
| 1961 | $v = $cast . $v; | ||
| 1962 | $v .= '"' if $issymbol; | ||
| 1963 | $q .= "$v;"; | ||
| 1964 | } | ||
| 1965 | chop($q); | ||
| 1966 | $q .= ')'; | ||
| 1967 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 1968 | Kx::dor0($r); | ||
| 1969 | } | ||
| 1970 | |||
| 1971 | return $self->FETCHSIZE(); | ||
| 1972 | } | ||
| 1973 | |||
| 1974 | sub SHIFT | ||
| 1975 | { | ||
| 1976 | my $self = shift; | ||
| 1977 | my $list = $self->{'list'}; | ||
| 1978 | |||
| 1979 | return undef if $self->{'count'} <= 0; | ||
| 1980 | |||
| 1981 | $self->{'count'}--; | ||
| 1982 | |||
| 1983 | my $q = "1#$list"; | ||
| 1984 | $self->{'val'} = Kx::k2parray0(Kx::k($self->{'kdb'},$q)); | ||
| 1985 | $q = "$list:1_$list"; | ||
| 1986 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 1987 | Kx::dor0($r); | ||
| 1988 | return $self->{'val'}[0]; | ||
| 1989 | } | ||
| 1990 | |||
| 1991 | sub UNSHIFT | ||
| 1992 | { | ||
| 1993 | my $self = shift; | ||
| 1994 | my @arr = @_; | ||
| 1995 | my $list = $self->{'list'}; | ||
| 1996 | my $cast = $CAST{$self->{'type'}}; | ||
| 1997 | |||
| 1998 | my $issymbol = $self->{'type'} eq 'symbol'; | ||
| 1999 | |||
| 2000 | # Do N at a time | ||
| 2001 | while(@arr) | ||
| 2002 | { | ||
| 2003 | my $q = "$list:("; | ||
| 2004 | my $len = @arr; | ||
| 2005 | $len = 500 if $len > 500; | ||
| 2006 | for my $v (splice(@arr,-$len)) | ||
| 2007 | { | ||
| 2008 | $v = $cast . $v; | ||
| 2009 | $v .= '"' if $issymbol; | ||
| 2010 | $q .= "$v;"; | ||
| 2011 | } | ||
| 2012 | chop($q); | ||
| 2013 | $q .= "),$list"; | ||
| 2014 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 2015 | Kx::dor0($r); | ||
| 2016 | } | ||
| 2017 | |||
| 2018 | return $self->FETCHSIZE(); | ||
| 2019 | } | ||
| 2020 | |||
| 2021 | # SPLICE this, offset, length, LIST | ||
| 2022 | # Perform the equivalent of "splice" on the array. | ||
| 2023 | # | ||
| 2024 | # offset is optional and defaults to zero, negative values count back | ||
| 2025 | # from the end of the array. | ||
| 2026 | # | ||
| 2027 | # length is optional and defaults to rest of the array. | ||
| 2028 | # | ||
| 2029 | # LIST may be empty. | ||
| 2030 | # | ||
| 2031 | # Returns a list of the original length elements at offset. | ||
| 2032 | sub SPLICE | ||
| 2033 | { | ||
| 2034 | my $self = shift; | ||
| 2035 | my $i = shift || 0; | ||
| 2036 | my $len = shift || $self->{'count'} - $i; | ||
| 2037 | my @arr = @_; | ||
| 2038 | |||
| 2039 | my $list = $self->{'list'}; | ||
| 2040 | my $count = $self->{'count'}; | ||
| 2041 | |||
| 2042 | # Sanity check on $i and $len | ||
| 2043 | return undef if abs($i) >= $count; | ||
| 2044 | return undef if $len <= 0; | ||
| 2045 | if($i < 0) | ||
| 2046 | { | ||
| 2047 | $len = abs($i) if $len > abs($i); # Clamp length if too big | ||
| 2048 | $i = $count + $i + 1; | ||
| 2049 | } | ||
| 2050 | return undef if ($len+$i) > $count; | ||
| 2051 | |||
| 2052 | my $q; | ||
| 2053 | |||
| 2054 | # First get hold of the old data to return | ||
| 2055 | $q = "$list\[(til $len) + $i\]"; | ||
| 2056 | my $aref = Kx::k2parray0(Kx::k($self->{'kdb'},$q)); | ||
| 2057 | |||
| 2058 | if(@arr) | ||
| 2059 | { | ||
| 2060 | # Now add the new stuff | ||
| 2061 | $q = "$list\[(til $len) + $i\]:("; | ||
| 2062 | my $cast = $CAST{$self->{'type'}}; | ||
| 2063 | |||
| 2064 | my $issymbol = $self->{'type'} eq 'symbol'; | ||
| 2065 | |||
| 2066 | for(my $j=0; $j < $len && $j <= $#arr; $j++) | ||
| 2067 | { | ||
| 2068 | my $v = $cast . $arr[$j]; | ||
| 2069 | $v .= '"' if $issymbol; | ||
| 2070 | $q .= "$v;"; | ||
| 2071 | } | ||
| 2072 | chop($q); | ||
| 2073 | $q .= ')'; | ||
| 2074 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 2075 | Kx::dor0($r); | ||
| 2076 | } | ||
| 2077 | else | ||
| 2078 | { | ||
| 2079 | # delete the slice. from 0 to $i to $j cut the list and join the | ||
| 2080 | # front and back bits back together | ||
| 2081 | my $j = $i + $len; | ||
| 2082 | $q = "$list:raze (0 $i $j\_$list)[0 2]"; | ||
| 2083 | my $r = Kx::k($self->{'kdb'},$q); | ||
| 2084 | Kx::dor0($r); | ||
| 2085 | } | ||
| 2086 | return @$aref; | ||
| 2087 | } | ||
| 2088 | |||
| 2089 | sub DELETE | ||
| 2090 | { | ||
| 2091 | my $self = shift; | ||
| 2092 | my $i = shift; | ||
| 2093 | my $list = $self->{'list'}; | ||
| 2094 | |||
| 2095 | return undef if $i < 0; | ||
| 2096 | $self->EXTEND($i) if $i >= $self->{'count'}; | ||
| 2097 | |||
| 2098 | # A null value | ||
| 2099 | my $val = $NULL{$self->{'type'}}; | ||
| 2100 | |||
| 2101 | $self->{'val'} = Kx::k2pscalar0(Kx::k($self->{'kdb'},"$list\[$i\]:$val")); | ||
| 2102 | return $self->{'val'}; | ||
| 2103 | } | ||
| 2104 | |||
| 2105 | sub EXISTS | ||
| 2106 | { | ||
| 2107 | my $self = shift; | ||
| 2108 | my $i = shift; | ||
| 2109 | |||
| 2110 | return 0 if $i < 0 || $i >= $self->{'count'}; | ||
| 2111 | my $list = $self->{'list'}; | ||
| 2112 | my $null = $NULL{$self->{'type'}}; | ||
| 2113 | return Kx::k2pscalar0(Kx::k($self->{'kdb'},"not $list\[$i\]=$null")); | ||
| 2114 | } | ||
| 2115 | |||
| 2116 | |||
| 2117 | package Kx::HASH; | ||
| 2118 | ##################################################################### | ||
| 2119 | # K Hash package # | ||
| 2120 | ##################################################################### | ||
| 2121 | |||
| 2122 | =head1 Kx::HASH | ||
| 2123 | |||
| 2124 | You may wish to tie a Perl hash to a Kdb+ variable. Well, you can do | ||
| 2125 | that as well. Try something like this: | ||
| 2126 | |||
| 2127 | use K; | ||
| 2128 | |||
| 2129 | my %config = ( | ||
| 2130 | host=>"localhost", | ||
| 2131 | port=>2222, | ||
| 2132 | userpass=>'user:pass', # optional | ||
| 2133 | ktype=>'symbol', | ||
| 2134 | vtype=>'int', | ||
| 2135 | dict=>'x', | ||
| 2136 | create=>1 | ||
| 2137 | ); | ||
| 2138 | tie(%x, 'Kx::HASH', %config); | ||
| 2139 | |||
| 2140 | print "Size of hash x is :". scalar %x ."\n"; | ||
| 2141 | for(0..5) | ||
| 2142 | { | ||
| 2143 | $x{"a$_"} = $_; | ||
| 2144 | } | ||
| 2145 | |||
| 2146 | %y = %x; | ||
| 2147 | |||
| 2148 | for(0..5) | ||
| 2149 | { | ||
| 2150 | print $y{"a$_"}," " if exists $y{"a$_"}; | ||
| 2151 | } | ||
| 2152 | print "\n"; | ||
| 2153 | |||
| 2154 | while(($k,$v) = each %x) | ||
| 2155 | { | ||
| 2156 | print "Key=>$k is $v\n"; | ||
| 2157 | } | ||
| 2158 | untie(%x); | ||
| 2159 | |||
| 2160 | All the functions defined in perltie for hashs are included. | ||
| 2161 | |||
| 2162 | Note: ktype is a Kdb+ type as defined in Types below - it is the | ||
| 2163 | 'key' type for the hash. vtype is also defined in Types - it is the | ||
| 2164 | value type. Only simple types are allowed at the moment. | ||
| 2165 | |||
| 2166 | =cut | ||
| 2167 | use 5.008; | ||
| 2168 | use strict; | ||
| 2169 | use warnings; | ||
| 2170 | use Carp; | ||
| 2171 | sub whowasi { (caller(1))[3] . '()' } | ||
| 2172 | |||
| 2173 | sub DESTROY | ||
| 2174 | { | ||
| 2175 | carp &whowasi if $DEBUG; | ||
| 2176 | my $self = shift; | ||
| 2177 | dor0($self->{'K'}) if defined $self->{'K'}; # release memory | ||
| 2178 | } | ||
| 2179 | |||
| 2180 | sub TIEHASH | ||
| 2181 | { | ||
| 2182 | carp &whowasi if $DEBUG; | ||
| 2183 | my $class = shift; | ||
| 2184 | my %opts = @_; | ||
| 2185 | my $d; # Dictionary | ||
| 2186 | |||
| 2187 | return undef unless defined $opts{'dict'}; | ||
| 2188 | |||
| 2189 | my $name = 'default'; | ||
| 2190 | $name = $opts{'name'} if defined $opts{'name'}; | ||
| 2191 | |||
| 2192 | my $ref = {'name' => $name}; | ||
| 2193 | if(defined $opts{'host'}) | ||
| 2194 | { | ||
| 2195 | $DB{$name}{'host'} = $opts{'host'}; | ||
| 2196 | } | ||
| 2197 | if(defined $opts{'port'}) | ||
| 2198 | { | ||
| 2199 | $DB{$name}{'port'} = $opts{'port'}; | ||
| 2200 | } | ||
| 2201 | if(defined $opts{'check_for_errors'}) | ||
| 2202 | { | ||
| 2203 | $DB{$name}{'check_for_errors'} = $opts{'check_for_errors'}; | ||
| 2204 | } | ||
| 2205 | |||
| 2206 | if(defined $opts{'dict'}) | ||
| 2207 | { | ||
| 2208 | $ref->{'dict'} = $opts{'dict'}; | ||
| 2209 | } | ||
| 2210 | if(defined $opts{'create'}) | ||
| 2211 | { | ||
| 2212 | $ref->{'create'} = 1; | ||
| 2213 | } | ||
| 2214 | my $ktype = $opts{'ktype'} || 'symbol'; | ||
| 2215 | my $vtype = $opts{'vtype'} || 'symbol'; | ||
| 2216 | $ref->{'ktype'} = $ktype; | ||
| 2217 | $ref->{'vtype'} = $vtype; | ||
| 2218 | |||
| 2219 | |||
| 2220 | # Get hold of any previously defined connection handle | ||
| 2221 | if(defined $DB{$name}{'kdb'}) | ||
| 2222 | { | ||
| 2223 | $ref->{'kdb'} = $DB{$name}{'kdb'}; # no need for connect | ||
| 2224 | $DB{$name}{'count'}++; | ||
| 2225 | } | ||
| 2226 | else # get connected a new | ||
| 2227 | { | ||
| 2228 | if(defined $DB{$name}{'userpass'}) | ||
| 2229 | { | ||
| 2230 | $ref->{'kdb'} = Kx::khpu($DB{$name}{'host'}, | ||
| 2231 | $DB{$name}{'port'}, | ||
| 2232 | $DB{$name}{'userpass'}); | ||
| 2233 | } | ||
| 2234 | # host, port only | ||
| 2235 | else | ||
| 2236 | { | ||
| 2237 | $ref->{'kdb'} = Kx::khp($DB{$name}{'host'}, $DB{$name}{'port'}); | ||
| 2238 | } | ||
| 2239 | unless($ref->{'kdb'} > 0) | ||
| 2240 | { | ||
| 2241 | undef $ref->{'kdb'}; | ||
| 2242 | return undef; | ||
| 2243 | } | ||
| 2244 | $DB{$name}{'kdb'} = $ref->{'kdb'}; | ||
| 2245 | $DB{$name}{'count'}++; | ||
| 2246 | } | ||
| 2247 | |||
| 2248 | # OK check if the variable already exists. If not then create it | ||
| 2249 | my $var = Kx::k2parray0(Kx::k($ref->{'kdb'},'\v')); | ||
| 2250 | $d = $ref->{'dict'}; | ||
| 2251 | if(!grep(/^$d$/,@$var) || defined $ref->{'create'}) | ||
| 2252 | { | ||
| 2253 | # Need to create it. | ||
| 2254 | my $r = Kx::k($ref->{'kdb'},"$d:(`$ktype\$())!`$vtype\$()"); | ||
| 2255 | if($r == 0) | ||
| 2256 | { | ||
| 2257 | carp "Undefined K structure in TIEHASH\n"; | ||
| 2258 | return undef; | ||
| 2259 | } | ||
| 2260 | if(Kx::kType($r) < 0) | ||
| 2261 | { | ||
| 2262 | carp "K error ", Kx::k2pscalar($r),"\n"; | ||
| 2263 | return undef; | ||
| 2264 | } | ||
| 2265 | } | ||
| 2266 | |||
| 2267 | |||
| 2268 | return bless $ref, $class; | ||
| 2269 | } | ||
| 2270 | |||
| 2271 | sub FETCH | ||
| 2272 | { | ||
| 2273 | carp &whowasi if $DEBUG; | ||
| 2274 | my $self = shift; | ||
| 2275 | my $key = shift; | ||
| 2276 | |||
| 2277 | my $dict = $self->{'dict'}; | ||
| 2278 | my $ktype = $self->{'ktype'}; | ||
| 2279 | |||
| 2280 | # Cast key to right type | ||
| 2281 | $key = $CAST{$ktype} . $key; | ||
| 2282 | $key .= '"' if $ktype eq 'symbol'; | ||
| 2283 | $self->{'val'} = Kx::k2pscalar0(Kx::k($self->{'kdb'},"$dict $key")); | ||
| 2284 | return $self->{'val'}; | ||
| 2285 | } | ||
| 2286 | |||
| 2287 | sub STORE | ||
| 2288 | { | ||
| 2289 | carp &whowasi if $DEBUG; | ||
| 2290 | my $self = shift; | ||
| 2291 | my $key = shift; | ||
| 2292 | my $val = shift; | ||
| 2293 | |||
| 2294 | my $dict = $self->{'dict'}; | ||
| 2295 | my $ktype = $self->{'ktype'}; | ||
| 2296 | my $vtype = $self->{'vtype'}; | ||
| 2297 | |||
| 2298 | # Cast key and value to right type | ||
| 2299 | if($ktype eq "symbol") | ||
| 2300 | { | ||
| 2301 | $key = '`$"'.$key.'"'; | ||
| 2302 | } | ||
| 2303 | else | ||
| 2304 | { | ||
| 2305 | $key = $CAST{$ktype} . $key; | ||
| 2306 | } | ||
| 2307 | if($vtype eq "symbol") | ||
| 2308 | { | ||
| 2309 | $val = '`$"'.$val.'"'; | ||
| 2310 | } | ||
| 2311 | else | ||
| 2312 | { | ||
| 2313 | $val = $CAST{$vtype} . $val; | ||
| 2314 | } | ||
| 2315 | my $r = Kx::k($self->{'kdb'},"$dict"."[$key]:$val"); | ||
| 2316 | Kx::dor0($r); | ||
| 2317 | return; | ||
| 2318 | } | ||
| 2319 | |||
| 2320 | sub DELETE | ||
| 2321 | { | ||
| 2322 | carp &whowasi if $DEBUG; | ||
| 2323 | my $self = shift; | ||
| 2324 | my $key = shift; | ||
| 2325 | |||
| 2326 | my $dict = $self->{'dict'}; | ||
| 2327 | $self->{'K'} = Kx::k($self->{'kdb'},".[`$dict;();_;$key]"); | ||
| 2328 | Kx::dor0($self->{'K'}); | ||
| 2329 | return 1; | ||
| 2330 | } | ||
| 2331 | |||
| 2332 | sub CLEAR | ||
| 2333 | { | ||
| 2334 | carp &whowasi if $DEBUG; | ||
| 2335 | my $self = shift; | ||
| 2336 | |||
| 2337 | my $d = $self->{'dict'}; | ||
| 2338 | my $ktype = $self->{'ktype'}; | ||
| 2339 | my $vtype = $self->{'vtype'}; | ||
| 2340 | |||
| 2341 | my $r = Kx::k($self->{'kdb'},"$d:(`$ktype\$())!`$vtype\$()"); | ||
| 2342 | Kx::dor0($r); | ||
| 2343 | return 1; | ||
| 2344 | } | ||
| 2345 | |||
| 2346 | sub EXISTS | ||
| 2347 | { | ||
| 2348 | carp &whowasi if $DEBUG; | ||
| 2349 | my $self = shift; | ||
| 2350 | my $key = shift; | ||
| 2351 | |||
| 2352 | my $dict = $self->{'dict'}; | ||
| 2353 | my $ktype = $self->{'ktype'}; | ||
| 2354 | |||
| 2355 | # Cast key to right type | ||
| 2356 | $key = $CAST{$ktype} . $key; | ||
| 2357 | $key .= '"' if $ktype eq 'symbol'; | ||
| 2358 | my $null = $NULL{$self->{'vtype'}}; | ||
| 2359 | $self->{'K'} = Kx::k($self->{'kdb'},"not $dict"."[$key]=$null"); | ||
| 2360 | return Kx::k2pscalar0($self->{'K'}); | ||
| 2361 | } | ||
| 2362 | |||
| 2363 | sub FIRSTKEY | ||
| 2364 | { | ||
| 2365 | carp &whowasi if $DEBUG; | ||
| 2366 | my $self = shift; | ||
| 2367 | |||
| 2368 | my $dict = $self->{'dict'}; | ||
| 2369 | $self->{'i'} = 0; | ||
| 2370 | $self->{'count'} = Kx::k2pscalar0(Kx::k($self->{'kdb'},"count $dict")); | ||
| 2371 | $self->{'K'} = Kx::k($self->{'kdb'},"(key $dict)[0]"); | ||
| 2372 | return Kx::k2pscalar0($self->{'K'}); | ||
| 2373 | } | ||
| 2374 | |||
| 2375 | sub NEXTKEY | ||
| 2376 | { | ||
| 2377 | carp &whowasi if $DEBUG; | ||
| 2378 | my $self = shift; | ||
| 2379 | |||
| 2380 | my $dict = $self->{'dict'}; | ||
| 2381 | $self->{'i'}++; | ||
| 2382 | return undef if $self->{'i'} >= $self->{'count'}; | ||
| 2383 | |||
| 2384 | my $i = $self->{'i'}; | ||
| 2385 | $self->{'K'} = Kx::k($self->{'kdb'},"(key $dict)[$i]"); | ||
| 2386 | return Kx::k2pscalar0($self->{'K'}); | ||
| 2387 | } | ||
| 2388 | |||
| 2389 | sub SCALAR | ||
| 2390 | { | ||
| 2391 | carp &whowasi if $DEBUG; | ||
| 2392 | my $self = shift; | ||
| 2393 | |||
| 2394 | my $dict = $self->{'dict'}; | ||
| 2395 | $self->{'K'} = Kx::k($self->{'kdb'},"count $dict"); | ||
| 2396 | return Kx::k2pscalar0($self->{'K'}); | ||
| 2397 | } | ||
| 2398 | |||
| 2399 | sub UNTIE | ||
| 2400 | { | ||
| 2401 | my $self = shift; | ||
| 2402 | Kx::DESTROY($self); | ||
| 2403 | } | ||
| 2404 | |||
| 2405 | package Kx; | ||
| 2406 | |||
| 2407 | # Preloaded methods go here. | ||
| 2408 | |||
| 2409 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||
| 2410 | |||
| 2411 | 1; | ||
| 2412 | __END__ |