Created
December 17, 2019 00:55
-
-
Save richardleach/b6c7488698d09ee00ac9d7b2e31dc9fe to your computer and use it in GitHub Desktop.
Rax wrapper WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Rax::Iterator; | |
| use FFI::Platypus::Record; | |
| record_layout( | |
| int => 'flags', | |
| opaque => 'rt', | |
| 'string rw' => 'key', | |
| opaque => 'data', | |
| size_t => 'key_len', | |
| size_t => 'key_max', | |
| 'string(128)' => 'key_static_string', | |
| opaque => 'node', | |
| opaque => 'stack', | |
| opaque => 'node_cb' | |
| ); | |
| package main; | |
| use Data::Dumper qw(Dumper); | |
| use FFI::Platypus; | |
| #package Local::Rax | |
| my $ffi = FFI::Platypus->new( api => 1 ); | |
| $ffi->lib('/root/PerlExperiments/rax/rax-oom-test'); | |
| $ffi->type("record(Rax::Iterator)*" => 'RaxIterator'); | |
| # attach the C localtime function as a constructor | |
| # $ffi->attach( raxStart => ['time_t*'] => 'Raxiterator', sub { | |
| # my($inner, $class, $time) = @_; | |
| # $time = time unless defined $time; | |
| # $inner->(\$time); | |
| # }); | |
| # call dynamically | |
| #$ffi->function( puts => ['string'] => 'int' )->call("hello world"); | |
| # attach as a xsub and call (much faster) | |
| $ffi->attach( raxNew => [] => 'opaque' ); | |
| $ffi->attach( raxInsert => ['opaque','string','int','opaque','opaque'] => 'int'); | |
| $ffi->attach( raxShow => ['opaque'] => undef ); | |
| $ffi->attach( raxTryInsert => ['opaque','string','int','opaque','opaque'] => 'int' ); | |
| $ffi->attach( raxRemove => ['opaque','string','int','opaque'] => 'int' ); | |
| #int raxRemove(rax *rax, unsigned char *s, size_t len, void **old); | |
| $ffi->attach( raxFind => ['opaque','string','int'] => 'opaque' ); | |
| #void *raxFind(rax *rax, unsigned char *s, size_t len); | |
| $ffi->attach( raxStart => ['RaxIterator','opaque'] => undef); | |
| $ffi->attach( raxSeek => ['RaxIterator','string','string','int'] => 'int'); | |
| $ffi->attach ( raxNext => ['RaxIterator'] => 'int' ); | |
| $ffi->attach ( raxPrev => ['opaque'] => 'int' ); | |
| $ffi->attach ( raxStop => ['opaque'] => 'int' ); | |
| $ffi->attach ( raxCompare => ['opaque','string','string','int'] => 'int'); | |
| $ffi->attach ( raxEOF => ['opaque'] => 'int' ); | |
| $ffi->attach( raxRandomWalk => ['opaque','int'] => 'int' ); | |
| my $rax = raxNew(); | |
| say Dumper($rax); | |
| say "Insert 'cat':". Dumper( raxInsert($rax,'cat',3,undef,undef) ); | |
| say "Insert 'catwoman':". Dumper( raxInsert($rax,'catwoman',8,undef,undef) ); | |
| say "Insert 'catmat':". Dumper( raxInsert($rax,'catmat',6,undef,undef) ); | |
| say "Insert 'bat':". Dumper( raxInsert($rax,'bat',3,undef,undef) ); | |
| say "Insert 'batman':". Dumper( raxInsert($rax,'batman',6,undef,undef) ); | |
| say "Insert 'batmobile':". Dumper( raxInsert($rax,'batmobile',9,undef,undef) ); | |
| say "Insert 'mit':". Dumper( raxTryInsert($rax,'mit',3,undef,undef) ); | |
| say "Insert 'mitten':". Dumper( raxTryInsert($rax,'mitten',6,undef,undef) ); | |
| say "Insert 'mittens':". Dumper( raxTryInsert($rax,'mittens',7,undef,undef) ); | |
| say "Insert 'bat':". Dumper( raxTryInsert($rax,'bat',3,undef,undef) ); | |
| say "Remove 'bat':". Dumper( raxRemove($rax,'bat',3,undef,undef) ); | |
| say "Insert 'bat':". Dumper( raxTryInsert($rax,'bat',3,undef,undef) ); | |
| say Dumper ( raxShow($rax) ) ; | |
| say "Find 'bat':". Dumper( raxFind($rax,'bat',3) ); | |
| say "Find 'mit':". Dumper( raxFind($rax,'mit',3) ); | |
| say "Find 'mit':". Dumper( raxFind($rax,'mit',3) ); | |
| say "Find 'zat':". Dumper( raxFind($rax,'zat',3) ); | |
| #say "raxNotFound:". Dumper( raxNotFound ); | |
| say "Looking at Iterators\n"; | |
| #my $iterator = Rax::Iterator->new(undef,undef,undef,undef,undef,undef,undef,undef,undef,undef); | |
| my $iterator = Rax::Iterator->new(); | |
| say "new Iterator contains: ". Dumper($iterator); | |
| say "Starting iterator: ".Dumper( raxStart($iterator, $rax) ); | |
| say "started Iterator contains: ". Dumper($iterator); | |
| say "About to seek first element"; | |
| say "Seek first element: ".Dumper( raxSeek($iterator,'>=','bat',3) ); | |
| my $val; | |
| while( raxNext($iterator)) { | |
| say "Comparing ".$iterator." against batmobile: ".Dumper( $val = raxCompare($iterator,">=",'batmobile',9) ); | |
| last if $val; | |
| say "Iterator currently contains: ". $iterator->key; | |
| } | |
| raxStop($iterator); | |
| __END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment