root/lang/perl/Senna/trunk/lib/Senna/Index.pm @ 3911

Revision 3911, 3.0 kB (checked in by daisuke, 6 years ago)

more code... (things are still broken)

  • Property svn:keywords set to Id
Line 
1# $Id$
2#
3# Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4# All rights reserved.
5
6package Senna::Index;
7use strict;
8use warnings;
9use Senna::Index::Info;
10
11sub create
12{
13    my $class = shift;
14    my %args  = @_;
15
16    my @xs_args = map { $args{$_} }
17        qw(path key_size flags initial_n_segments encoding);
18    pop @xs_args while @xs_args && ! defined $xs_args[-1];
19    $class->XS_create(@xs_args);
20}
21
22sub open
23{
24    my $class = shift;
25    if (@_ == 1) {
26        if(ref $_[0] eq 'HASH') {
27            @_ = ($_[0]->{path});
28        }
29    } elsif (@_ > 1) {
30        my %args  = @_;
31        @_ = ($args{path});
32    }
33
34    $class->_XS_open(@_);
35}
36
37sub info
38{
39    my $self = shift;
40    if ( wantarray ) {
41        return $self->XS_info();
42    } else {
43        my %h;
44        my @fields = qw(key_size flags initial_n_segments encoding nrecords_keys file_size_keys nrecords_lexicon file_size_lexicon inv_seg_size inv_chunk_size);
45        @h{ @fields } = $self->XS_info();
46
47        return Senna::Index::Info->new(%h);
48    }
49}
50
51sub insert
52{
53    my $self = shift;
54    if (@_ == 1 && ref $_[0] eq 'HASH') {
55        @_ = ($_[0]->{key}, $_[0]->{value});
56    } elsif (@_ > 2) {
57        my %args = @_;
58        @_ = ($args{key}, $args{value});
59    }
60    $self->_XS_insert(@_);
61}
62
63sub select
64{
65    my $self = shift;
66    if (@_ == 1 && ref $_[0] eq 'HASH') {
67        @_ = map { $_[0]->{$_} } qw(query records op optarg);
68    } elsif (@_ > 1) {
69        my %args = @_;
70        @_ = @args{qw(query records op optarg)};
71    }
72
73    $self->_XS_select(@_);
74}
75
761;
77
78__END__
79
80=head1 NAME
81
82Senna::Index - Senna Index Object
83
84=head1 SYNOPSIS
85
86  use Senna;
87
88  my $index = Senna::Index->create();
89  my $index = Senna::Index->open($path);
90
91  $index->insert($key, $value);
92  $index->select($query);
93
94=head1 METHODS
95
96=head2 create
97
98=head2 open
99
100Opens an existing index.
101
102  my $index = Senna::Index->open($path);
103  my $index = Senna::Index->open({ path => $path });
104
105For backwards compatibility, if given more than one argument, open() assumes
106that you've been given a key value pair like so:
107
108  my $index = Senna::Index->open(path => $path);
109
110However, note that this form is DEPRECATED. Use the HASHREF form instead
111
112=head2 info
113
114In scalar context, returns a Senna::Index::Info object.
115
116  $info = $index->info;
117  $info->key_size();
118
119In list context, returns the same informtion as a list:
120
121  ($key_size, $flags, $initial_n_segments, $encoding, $nrecords_keys,
122    $file_size_keys, $nrecords_lexicon, $file_size_lexicon, $inv_seg_size,
123      $inv_chunk_size) = $index->info
124
125=head2 path
126
127Returns the path to the senna index.
128
129=head2 close
130
131=head2 remove
132
133=head2 update
134
135=head2 insert
136
137Inserts a new entry in the index.
138
139  $index->insert($key, $value);
140  $index->insert({ key => $key, value => $value });
141
142For backwards compatibility if given more than 2 arguments, insert() assumes
143that you've been given a key/value pair like so:
144
145  $index->insert(key => $key, value => $value);
146
147However, note that this form is DEPRECATED. Use the HASHREF form instead
148
149=cut
Note: See TracBrowser for help on using the browser.