gitpan/CDB_File — pull request #1 by gnatyna (2014-01-16 11:56:58+00:00)
Title: CDB_File::ALLKEYS - fetch all cdb's items, works faster then iteration of NEXTKEY

```
commit c0881877e4c21851b029aa252bd807594cfa1a69
Author: Your Name
Date: Thu Jan 16 15:42:18 2014 +0400

6. Getting all keys from file(ALLKEYS function).

use CDB_File;

my $cdb = CDB_File::ALLKEYS("fil.cdb");
my $items = CDB_File::ALLKEYS($cdb);
print join("\n",@$items);

diff --git a/CDB_File.pm b/CDB_File.pm
index 1eaeaaf..b43ffc2 100644
--- a/CDB_File.pm
+++ b/CDB_File.pm
@@ -210,6 +210,15 @@ new one is Ced.)
}

$new->finish or die "$0: CDB_File finish failed: $!\n";
+
+6. Getting all keys from file.
+
+ use CDB_File;
+
+ my $cdb = CDB_File::ALLKEYS("fil.cdb");
+ my $items = CDB_File::ALLKEYS($cdb);
+ print join("\n",@$items);
+

=head1 REPEATED KEYS

diff --git a/CDB_File.xs b/CDB_File.xs
index 012f43d..cdce637 100644
--- a/CDB_File.xs
+++ b/CDB_File.xs
@@ -581,7 +581,42 @@ cdb_FIRSTKEY(this)
ST(0) = sv_mortalcopy(this->curkey);
else
XSRETURN_UNDEF; /* empty database */
+
+AV *
+cdb_ALLKEYS(this)
+ cdb * this

+ PREINIT:
+ char buf[8];
+ U32 klen,dlen;
+ SV *x;
+
+ CODE:
+ RETVAL = newAV();
+ sv_2mortal((SV *)RETVAL);
+ cdb_findstart(this);
+
+ iter_start(this);
+
+ if(this->curpos >= this->end)
+ XSRETURN_UNDEF;
+
+ while (this->curpos < this->end) {
+ if (cdb_read(this, buf, 8, this->curpos) == -1) readerror();
+ uint32_unpack(buf, &klen);
+ x = newSVpvn("", 0);
+ SvGROW(x, klen); SvCUR_set(x, klen);
+ if (cdb_read(this, SvPVX(x), klen, this->curpos + 8) == -1) readerror();
+ av_push(RETVAL, x);
+
+ uint32_unpack(buf, &klen);
+ uint32_unpack(buf + 4, &dlen);
+ this->curpos += 8 + klen + dlen;
+ }
+
+ OUTPUT:
+ RETVAL
+
SV *
cdb_NEXTKEY(this, k)
cdb * this
```