This file is indexed.

/usr/share/perl5/KiokuDB/Tutorial.pod is in libkiokudb-perl 0.56-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
=pod

=head1 NAME

KiokuDB::Tutorial - Getting started with KiokuDB

=head1 INSTALLATION

The easiest way to install L<KiokuDB> along with a number of backends is
L<Task::KiokuDB>.

L<KiokuDB> depends on L<Moose> and a few other modules out of the box, but no
specific storage module.

L<KiokuDB> is a frontend to several backends, much like L<DBI> uses DBDs to
connect to actual databases.

For development and testing you can use the L<KiokuDB::Backend::Hash> backend,
which is an in memory store, but for production use L<KiokuDB::Backend::DBI> or
L<KiokuDB::Backend::BDB> are the recommended backends.

See below for instructions on getting L<KiokuDB::Backend::BDB> installed.

=head1 CREATING A DIRECTORY HANDLE

A KiokuDB directory is the main object through which all work is done.

The simplest directory that is ready for use can be created like this:

    my $dir = KiokuDB->new(
        backend => KiokuDB::Backend::Hash->new
    );

We will revisit other more interesting backend configuration later in this
document, but for now this will do.

You can also use DSN strings to connect to the various backends:

    KiokuDB->connect("hash");

    KiokuDB->connect("dbi:SQLite:dbname=foo", create => 1);

    KiokuDB->connect("bdb:dir=foo", create => 1);

You can also use a configuration file:

    KiokuDB->connect("/path/to/my_db.yml");

Which is just a YAML file:

    ---
    # these are basically the arguments for 'new'
    backend:
      class: KiokuDB::Backend::DBI
      dsn: dbi:SQLite:dbname=/tmp/test.db
      create: 1

=head1 USING THE DBI BACKEND

During this tutorial we will be using the DBI backend for two reasons. The
first is L<DBI>'s ubiquity. The second is the possibility of easily looking
behind the scenes, to more clearly demonstrate what L<KiokuDB> is doing.

That said, the examples will work with all backends exactly the same.

First we create C<$dir>:

    my $dir = KiokuDB->connect(
        "dbi:SQLite:dbname=kiokudb_tutorial.db",
        create => 1, # this causes the tables to be created
    );

Note that if you are connecting with a username and password you need to
specify these as named arguments:

    my $dir = KiokuDB->connect(
        $dsn,
        user     => $user,
        password => $password,
    );

=head1 INSERTING OBJECTS

Let's start by defining a simple class using L<Moose>:

    package Person;
    use Moose;

    has name => (
        isa => "Str",
        is  => "rw",
    );

We can instantiate it:

    my $obj = Person->new( name => "Homer Simpson" );

and insert the object to the database as follows:

    my $scope = $dir->new_scope;

    my $homer_id = $dir->store($obj);

This is very trivial use of L<KiokuDB>, but it illustrates a few important
things.

First, no schema is necessary. L<KiokuDB> uses L<Moose> to introspect your
object without needing to predefine anything like tables.

Second, every object in the database has an ID. If you don't choose an ID for
an object, L<KiokuDB> will assign a UUID instead.

This ID is like a primary key in a relational database.

You can also specify an ID instead of letting one be generated:

    $dir->store( homer => $obj );

Third, all L<KiokuDB> operations need to be performed within a B<scope>. The
scope is not really doing anything important in this simple example, but
becomes necessary when cycles and weak references are in use. We will look into
that in more detail later.

=head1 LOADING OBJECTS

So now that Homer has been inserted into the database, we can fetch him out of
there using the ID we got from C<store>.

    my $homer = $dir->lookup($homer_id);

Assuming that C<$scope> and C<$obj> are still in scope, C<$homer> and C<$obj>
will actually be the same object:

    # this is true:
    refaddr($homer) == refaddr($obj)

This is because L<KiokuDB> tracks which objects are "live" in the
B<live object set> (L<KiokuDB::LiveObjects>).

If the object wasn't already in memory then L<KiokuDB> would have fetched it
from the backend instead.

=head1 WHAT WAS STORED

Let's peek into the database:

    % sqlite3 kiokudb_tutorial.db
    SQLite version 3.4.0
    Enter ".help" for instructions
    sqlite>

The database schema has two tables, C<entries> and C<gin_index>:

    sqlite> .tables
    entries    gin_index

C<gin_index> is used for more complex queries, and we'll get back to it at the
end of the tutorial.

For now let's just have a closer look at C<entries>:

    sqlite> .schema entries
    CREATE TABLE entries (
      id varchar NOT NULL,
      data blob NOT NULL,
      class varchar,
      root boolean NOT NULL,
      tied char(1),
      PRIMARY KEY (id)
    );

The main columns are C<id> and C<data>. In L<KiokuDB> every object has an ID
which serves as a primary key and a BLOB of data associated with it.

Since the default serializer for the DBI backend is
L<KiokuDB::Serializer::JSON>, we examine the data.

First let's set C<sqlite>'s output mode to C<line>. This is easier to read with
large columns:

    sqlite> .mode line

And select the data from the table:

    sqlite> select id, data from entries;
       id = 201C5B55-E759-492F-8F20-A529C7C02C8B
     data = {"__CLASS__":"Person","data":{"name":"Homer Simpson"},"id":"201C5B55-E759-492F-8F20-A529C7C02C8B","root":true}

As you can see the C<name> attribute is stored under the C<data> key inside the
blob, as is the object's class.

The C<data> column contains all of the data necessary to recreate the object.

All the other columns are only for searches. Later on you'll also see how to
create user defined columns.

When using L<KiokuDB::Backend::BDB> the on-disk format is just a hash of C<id>
to C<data> with no additional columns.

=head1 OBJECT RELATIONSHIPS

Let's extend the C<Person> class to hold some more interesting data than just a
C<name>:

    package Person;

    has spouse => (
        isa => "Person",
        is  => "rw",
        weak_ref => 1,
    );

This new C<spouse> attribute will hold a reference to another person object.

Let's first create and insert another object:

    my $marge_id = $dir->store(
        Person->new( name => "Marge Simpson" ),
    );

Now that we have both objects in the database, let's link them together:

    {
        my $scope = $dir->new_scope;

        my ( $marge, $homer ) = $dir->lookup( $marge_id, $homer_id );

        $marge->spouse($homer);
        $homer->spouse($marge);

        $dir->store( $marge, $homer );
    }

Now we have created a persistent B<object graph>, that is several objects which
point to each other.

The reason C<spouse> had the C<weak_ref> option was so that this circular
structure will not leak.

When then objects are updated in the database, L<KiokuDB> sees that their
C<spouse> attribute contains references, and this relationship will be encoded
using their unique ID in storage.

To load the graph, we can do something like this:

    {
        my $scope = $dir->new_scope;

        my $homer = $dir->lookup($homer_id);

        print $homer->spouse->name; # Marge Simpson
    }

    {
        my $scope = $dir->new_scope;

        my $marge = $dir->lookup($marge_id);

        print $marge->spouse->name; # Homer Simpson

        refaddr($marge) == refaddr($marge->spouse->spouse); # true
    }

When L<KiokuDB> is loading the initial object, all the objects the object
depends on will also be loaded. The C<spouse> attribute contains a
reference to another object (by ID), and this link is resolved at inflation
time.

=head2 The purpose of C<new_scope>

This is where C<new_scope> becomes important. As objects are inflated from the
database, they are pushed onto the live object scope, in order to increase
their reference count.

If this was not done, by the time C<$homer> was returned from C<lookup> his
C<spouse> attribute would have been cleared because there is no other reference
to Marge.

This demonstrates why:

    sub get_homer {
        my $homer = Person->new( name => "Homer Simpson" );
        my $marge = Person->new( name => "Marge Simpson" );

        $homer->spouse($marge);
        $marge->spouse($homer);

        return $homer;

        # at this point $homer and $marge go out of scope
        # $homer has a refcount of 1 because it's the return value
        # $marge has a refcount of 0, and gets destroyed
        # the weak reference in $homer->spouse is cleared
    }

    my $homer = get_homer();

    $homer->spouse; # this returns undef

By using this idiom:

    {
        my $scope = $dir->new_scope;

        # do all KiokuDB work in here
    }

You are ensuring that the objects live at least as long as is necessary.

In a web application context you usually create one new scope per request. In
fact, L<Catalyst::Model::KiokuDB> does this automatically.

=head1 REFERENCES IN THE DATABASE

Now that we have an object graph in the database let's have another look at
what's inside.

    sqlite> select id, data from entries;
       id = 201C5B55-E759-492F-8F20-A529C7C02C8B
     data = {"__CLASS__":"Person","data":{"name":"Homer Simpson","spouse":{"$ref":"05A8D61C-6139-4F51-A748-101010CC8B02.data"}},"id":"201C5B55-E759-492F-8F20-A529C7C02C8B","root":true}

       id = 05A8D61C-6139-4F51-A748-101010CC8B02
     data = {"__CLASS__":"Person","data":{"name":"Marge Simpson","spouse":{"$ref":"201C5B55-E759-492F-8F20-A529C7C02C8B.data"}},"id":"05A8D61C-6139-4F51-A748-101010CC8B02","root":true}

You'll notice the C<spouse> field has a JSON object with a C<$ref> field inside
it holding the UUID of the target object.

When data is loaded L<KiokuDB> queues up references to unloaded objects and
then loads them in order to materialize the memory resident object graph.

If you're curious about why the data is represented this way, this format is
called C<JSPON>, or JavaScript Persistent Object Notation
(L<http://www.jspon.org/>). When using L<KiokuDB::Backend::Storable> the
L<KiokuDB::Entry> and L<KiokuDB::Reference> objects are serialized with their
storable hooks instead.

=head1 OBJECT SETS

More complex relationships (not necessarily 1 to 1) are usually easy to model
with L<Set::Object>.

Let's extend the C<Person> class to add such a relationship:

    package Person;

    has children => (
        does => "KiokuDB::Set",
        is   => "rw",
    );

L<KiokuDB::Set> objects are L<KiokuDB> specific wrappers for L<Set::Object>.

    my @kids = map { Person->new( name => $_ ) } qw(maggie lisa bart);

    use KiokuDB::Util qw(set);

    my $set = set(@kids);

    $homer->children($set);

    $dir->store($homer);

The C<set> convenience function creates a new L<KiokuDB::Set::Transient>
object. A transient set is one which started its life in memory space (as
opposed to a set that was loaded from the database).

The C<weak_set> convenience function also exists, creating a transient set with
L<Set::Object::Weak> used internally to help avoid circular structures (for
instance if setting a C<parent> attribute in our example).

The set object behaves pretty much like a normal L<Set::Object>:

    my @kids = $dir->lookup($homer_id)->children->members;

The main difference is that sets coming from the database are deferred by
default, that is the objects in C<@kids> are not loaded until they are actually
needed.

This allows large object graphs to exist in the database, while only being
partially loaded, without breaking the encapsulation of user objects. This
behavior is implemented in L<KiokuDB::Set::Deferred> and
L<KiokuDB::Set::Loaded>.

This set object is optimized to make most operations defer loading. For
instance, if you intersect two deferred sets, only the members of the
intersection set will need to be loaded.

=head1 THE TYPEMAP

Storing an object with L<KiokuDB> involves passing it to L<KiokuDB::Collapser>,
the object that "flattens" objects into L<KiokuDB::Entry> before the entries
are inserted into the backend.

The collapser uses a L<KiokuDB::TypeMap> object that tells it how objects of
each type should be collapsed.

During retrieval of objects the same typemap is used to reinflate objects back
into working objects.

Trying to store an object that is not in the typemap is an error. The reason
behind this is that it doesn't make sense to store every type of object (for
instance C<DBI> handles need a socket, objects based on XS modules have an
internal pointer as an integer, whose address won't be valid the next time it's
loaded), and even though the majority of objects are safe to serialize, even a
small bit of unreported fragility is usually enough to create large, hard to
debug problems.

An exception to this rule is L<Moose> based objects, because they have
sufficient meta information available through L<Moose>'s powerful reflection
support in order to be safely serialized.

Additionally, the standard backends provide a default typemap for common
objects (L<DateTime>, L<Path::Class>, etc), which by default is merged with any
custom typemap you pass to L<KiokuDB>.

So, in order to actually get L<KiokuDB> to store things like L<Class::Accessor>
based objects, you can do something like this:

    KiokuDB->new(
        backend => $backend,
        allow_classes => [qw(My::Object)],
    );

Which is shorthand for:

    my $dir = KiokuDB->new(
        backend => $backend,
        typemap => KiokuDB::TypeMap->new(
            entries => {
                "My::Object" => KiokuDB::TypeMap::Entry::Naive->new,
            },
        ),
    );

L<KiokuDB::TypeMap::Entry::Naive> is a type map entry that performs naive
collapsing of the object, by simply walking it recursively.

When the collapser encounters an object it will ask
L<KiokuDB::TypeMap::Resolver> for a collapsing routine based on the class of
the object.

This lookup is typically performed by C<ref $object>, not using inheritance,
because a typemap entry that is safe to use with a superclass isn't necessarily
safe to use with a subclass. If you B<do> want inherited entries, specify
C<isa_entries>:

    KiokuDB::TypeMap->new(
        isa_entries => {
            "My::Object" => KiokuDB::TypeMap::Entry::Naive->new,
        },
    );

If no normal (C<ref> keyed) entry is found for an object, the isa entries are
searched for a superclass of that object. Subclass entries are tried before
superclass entries. The result of this lookup is cached, so it only happens
once per class.

=head2 Typemap Entries

If you want to do custom serialization hooks, you can specify hooks to collapse
your object:

    KiokuDB::TypeMap::Entry::Callback->new(
        collapse => sub {
            my $object = shift;

            ...

            return @some_args;
        },
        expand => sub {
            my ( $class, @some_args ) = @_;

            ...

            return $object;
        },
    );

These hooks are called as methods on the object to be collapsed.

For instance the L<Path::Class> related typemap ISA entry is:

    'Path::Class::Entity' => KiokuDB::TypeMap::Entry::Callback->new(
        intrinsic => 1,
        collapse  => "stringify",
        expand    => "new",
    );

The C<intrinsic> flag is discussed in the next section.

Another option for typemap entries is L<KiokuDB::TypeMap::Entry::Passthrough>,
which is appropriate when you know the backend's serialization can handle that
data type natively.

For example, if your object has a L<Storable> hook which you know is
appropriate (e.g. contains no sub objects that need to be collapsible) and your
backend uses L<KiokuDB::Backend::Serialize::Storable>. L<DateTime> is an
example of a class with such storable hopes:

    'DateTime' => KiokuDB::Backend::Entry::Passthrough->new( intrinsic => 1 )

=head2 Intrinsic vs. First Class

In L<KiokuDB> every object is normally assigned an ID, and if the object is
shared by several objects this relationship will be preserved.

However, for some objects this is not the desired behavior. These are objects
that represent values, like L<DateTime>, L<Path::Class> entries, L<URI>
objects, etc.

L<KiokuDB> can be asked to collapse such objects B<intrinsicly>, that is
instead of creating a new L<KiokuDB::Entry> with its own ID for the object, the
object gets collapsed directly into its parent's structures.

This means that shared references that are collapsed intrinsically will be
loaded back from the database as two distinct copies, so updates to one will
not affect the other.

For instance, when we run the following code:

    use Path::Class;

    my $path = file(qw(path to foo));

    $obj_1->file($path);

    $obj_2->file($path);

    $dir->store( $obj_1, $obj_2 );

While the following is true when the data is being inserted, it will no longer
be true when C<$obj_1> and C<$obj_2> are loaded from the database:

    refaddr($obj_1->file) == refaddr($obj_2->file)

This is because both C<$obj_1> and C<$obj_2> each got its own copy of C<$path>.

This behavior is usually more appropriate for objects that aren't mutated, but
are instead cloned and replaced, and for which creating a first class entry in
the backend with its own ID is undesired.

=head2 The Default Typemap

Each backend comes with a default typemap, with some built in entries for
common CPAN modules' objects. L<KiokuDB::TypeMap::Default> contains more
details.

=head1 SIMPLE SEARCHES

Most backends support an inefficient but convenient simple search, which scans
the entries and matches fields.

If you want to make use of this API we suggest using L<KiokuDB::Backend::DBI>
since simple searching is implemented using an SQL where clause, which is much
more efficient (you do have to set up the column manually though).

Calling the C<search> method with a hash reference as the only argument invokes
the simple search functionality, returning a L<Data::Stream::Bulk> with the
results:

    my $stream = $dir->search({ name => "Homer Simpson" });

    while ( my $block = $stream->next ) {
        foreach my $object ( @$block ) {
            # $object->name eq "Homer Simpson"
       }
    }

This exact API is intentionally still underdefined. In the future it will be
compatible with L<DBIx::Class> 0.09's syntax.

=head2 DBI SEARCH COLUMNS

In order to make use of the simple search API we need to configure columns for
our DBI backend.

Let's create a 'name' column to search by:

    my $dir = KiokuDB->connect(
        "dbi:SQLite:dbname=foo",
        columns => [
            # specify extra columns for the 'entries' table
            # in the same format you pass to DBIC's add_columns

            name => {
                data_type => "varchar",
                is_nullable => 1, # probably important
            },
        ],
    );

You can either alter the schema manually, or use C<kioku dump> to back up your
data, delete the database, connect with C<< create => 1 >> and then use
C<kioku load>.

To populate this column we'll need to load Homer and update him:

    {
        my $s = $dir->new_scope;
        $dir->update( $dir->lookup( $homer_id ) );
    }

And this is what it looks in the database:

       id = 201C5B55-E759-492F-8F20-A529C7C02C8B
     name = Homer Simpson

=head1 GETTING STARTED WITH BDB

The most mature backend for L<KiokuDB> is L<KiokuDB::Backend::BDB>. It performs
very well, and supports many features, like L<Search::GIN> integration to
provide customized indexing of your objects and transactions.

L<KiokuDB::Backend::DBI> is newer and not as tested, but also supports
transactions and L<Search::GIN> based queries. It performs quite well too, but
isn't as fast as L<KiokuDB::Backend::BDB>.

=head2 Installing L<KiokuDB::Backend::BDB>

L<KiokuDB::Backend::BDB> needs the L<BerkeleyDB> module, and a recent version
of Berkeley DB itself, which can be found here:
L<http://www.oracle.com/technology/software/products/berkeley-db/db/index.html>.

BerkeleyDB (the library) normally installs into C</usr/local/BerkeleyDB.4.7>,
while L<BerkeleyDB> (the module) looks for it in C</usr/local/BerkeleyDB>, so
adding a symbolic link should make installation easy.

Once you have L<BerkeleyDB> installed, L<KiokuDB::Backend::BDB> should install
without problem and you can use it with L<KiokuDB>.

=head2 Using L<KiokuDB::Backend::BDB>

To use the BDB backend we must first create the storage. To do this the
C<create> flag must be passed:

    my $backend = KiokuDB::Backend::BDB->new(
        manager => {
            home   => Path::Class::Dir->new(qw(path to storage)),
            create => 1,
        },
    );

The BDB backend uses L<BerkeleyDB::Manager> to do a lot of the L<BerkeleyDB>
gruntwork. The L<BerkeleyDB::Manager> object will be instantiated using the
arguments provided in the C<manager> attribute.

Now that the storage is created we can make use of this backend, much like before:

    my $dir = KiokuDB->new( backend => $backend );

Subsequent opens will not require the C<create> argument to be true, but it
doesn't hurt.

This C<connect> call is equivalent to the above:

    my $dir = KiokuDB->connect( "bdb:dir=path/to/storage", create => 1 );

=head1 TRANSACTIONS

Some backends (ones which do the L<KiokuDB::Backend::Role::TXN> role) can be used
with transactions.

If you are familiar with L<DBIx::Class> this should be very familiar:

    $dir->txn_do(sub {
        $dir->store($obj);
    });

This will create a L<BerkeleyDB> level transaction, and all changes to the
database are committed if the block was executed cleanly.

If any error occurred the transaction will be rolled back, and the changes will
not be visible to subsequent reads.

Note that L<KiokuDB> does B<not> touch live instances, so if you do something
like

    $dir->txn_do(sub {
        my $scope = $dir->new_scope;

        $obj->name("Dancing Hippy");
        $dir->store($obj);

        die "an error";
    });

the C<name> attribute is B<not> rolled back, it is simply the C<store>
operation that gets reverted.

Transactions will nest properly, and with most backends they generally increase
write performance as well.

=head1 QUERIES

L<KiokuDB::Backend::BDB::GIN> is a subclass of L<KiokuDB::Backend::BDB> that
provides L<Search::GIN> integration.

L<Search::GIN> is a framework to index and query objects, inspired by Postgres'
internal GIN api. GIN stands for Generalized Inverted Indexes.

Using L<Search::GIN> arbitrary search keys can be indexed for your objects, and
these objects can then be looked up using queries.

For instance, one of the pre canned searches L<Search::GIN> supports out of the
box is class indexing. Let's use L<Search::GIN::Extract::Callback> to do custom
indexing of our objects:

    my $dir = KiokuDB->new(
        backend => KiokuDB::Backend::BDB::GIN->new(
            extract => Search::GIN::Extract::Callback->new(
                extract => sub {
                    my ( $obj, $extractor, @args ) = @_;

                    if ( $obj->isa("Person") ) {
                        return {
                            type => "user",
                            name => $obj->name,
                        };
                    }

                    return;
                },
            ),
        ),
    );

    $dir->store( @random_objects );

To look up the objects, we use the a manual key lookup query:

    my $query = Search::GIN::Query::Manual->new(
        values => {
            type => "person",
        },
    );

    my $stream = $dir->search($query);

The result is L<Data::Stream::Bulk> object that represents the search results.
It can be iterated as follows:

    while ( my $block = $stream->next ) {
        foreach my $person ( @$block ) {
            print "found a person: ", $person->name;
        }
    }

Or even more simply, if you don't mind loading the whole resultset into memory:

    my @people = $stream->all;

L<Search::GIN> is very much in its infancy, and is very under documented.
However it does work for simple searches such as this and contains pre canned
solutions like L<Search::GIN::Extract::Class>.

In short, it works today, but watch this space for new developments.