File: stream.t

package info (click to toggle)
libkiokudb-perl 0.57-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,396 kB
  • sloc: perl: 13,314; makefile: 12
file content (80 lines) | stat: -rw-r--r-- 1,854 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Exception;
use Data::Stream::Bulk::Callback;

use KiokuDB;
use KiokuDB::Backend::Hash;
use KiokuDB::Stream::Objects;

{
    package KiokuDB_Test_Foo;
    use Moose;

    has id  => (is => 'rw', isa => 'Str');
    has num => (is => 'rw', isa => 'Int');
}

my $dir = KiokuDB->connect( "hash", serializer => 'memory');

my @objs = (
    KiokuDB_Test_Foo->new( id => 'one',   num => 1 ),
    KiokuDB_Test_Foo->new( id => 'two',   num => 2 ),
    KiokuDB_Test_Foo->new( id => 'three', num => 3 ),
    KiokuDB_Test_Foo->new( id => 'zero',  num => 0 ),
    KiokuDB_Test_Foo->new( id => 'four',  num => 4 ),
);

my @ids;

my @entries;

{
    my $s = $dir->new_scope;

    foreach my $obj (@objs) {
        lives_ok { $dir->store( $obj->id   => $obj ) } "can store " . $obj->id;
    }

    @ids = $dir->live_objects->objects_to_ids(@objs);

    @entries = map { $_->clone } $dir->live_objects->objects_to_entries(@objs);
}


sub iter {
    my @x = @_;
    Data::Stream::Bulk::Callback->new(
    callback =>
    sub { return unless @x; return [ shift @x ] })->filter(sub {[grep { $_->can("num") ? $_->num  : $_->data->{num} } @$_ ]});
}

is_deeply([map { $_->num } iter(@objs)->all],[1,2,3,4], "found 4 objects");

{
    my $stream = KiokuDB::Stream::Objects ->new(
        directory => $dir,
        entry_stream => iter(@entries),
    );

    is_deeply([map { $_->num } $stream->all],[1,2,3,4], "found 4 objects");
}

{
    my $s = $dir->new_scope;
    my $one = $dir->lookup('one');

    my $stream = $dir->grep(sub { 1 });

    is_deeply([sort map { $_->num } $stream->all],[0,1,2,3,4], "found all objects");

    lives_ok { $dir->delete($one) } "can delete previously live objects";

    is_deeply([sort map { $_->num } $dir->root_set->all], [0,2,3,4], "really deleted");
}

done_testing;