| 1 | package Store::Indexed; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.1'; | ||
| 6 | our $BACKEND; | ||
| 7 | |||
| 8 | sub import { | ||
| 9 | my ($class, @tags) = @_; | ||
| 10 | ($BACKEND) = map { uc($1) } grep { /^:(XS|PP)$/i } @tags; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my ($class, %args) = @_; | ||
| 15 | my $type = $args{backend} || $BACKEND || $ENV{STORE_BACKEND} || 'AUTO'; | ||
| 16 | $type = eval { require Store::Indexed::XS; 1 } ? 'XS' : 'PP' if $type eq 'AUTO'; | ||
| 17 | my $target = ($type eq 'XS') ? 'Store::Indexed::XS' : 'Store::Indexed::PP'; | ||
| 18 | warn $target; | ||
| 19 | eval { (my $f = "$target.pm") =~ s|::|/|g; require $f }; | ||
| 20 | die "Load failed: $@" if $@; | ||
| 21 | return $target->new(%args); | ||
| 22 | } | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | =pod | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Store::Indexed - A fast, key-indexed data store with dual XS and Pure-Perl backends. | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | use Store::Indexed; | ||
| 35 | |||
| 36 | # Auto-detects best backend (XS preferred) | ||
| 37 | my $store = Store::Indexed->new(qw(key1 key2 key3)); | ||
| 38 | |||
| 39 | # Accessors are generated dynamically based on keys | ||
| 40 | $store->set_key1(0, "value_for_id_0"); | ||
| 41 | my $val = $store->get_key1(0); | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | B<Store::Indexed> provides an interface for storing and retrieving data points | ||
| 46 | indexed by an integer ID and a column name. It uses a flat array structure under | ||
| 47 | the hood to achieve high performance. It supports both a highly optimized | ||
| 48 | C-based implementation (XS) and a portable Pure-Perl implementation (PP). | ||
| 49 | |||
| 50 | =head1 METHODS | ||
| 51 | |||
| 52 | =head2 new(@keys) | ||
| 53 | |||
| 54 | Creates a new C<Store::Indexed> instance. The list of C<@keys> defines the fixed | ||
| 55 | columns of the data store. This will dynamically generate C<get_$key>, | ||
| 56 | C<set_$key>, C<exists_$key>, and C<delete_$key> methods for each key provided. | ||
| 57 | |||
| 58 | =head2 Dynamic Accessors | ||
| 59 | |||
| 60 | For every key defined in C<new()>, the following methods are injected: | ||
| 61 | |||
| 62 | =over 4 | ||
| 63 | |||
| 64 | =item * C<get_$key($id)>: Returns the value at row C<$id> and column C<$key>. | ||
| 65 | |||
| 66 | =item * C<set_$key($id, $value)>: Sets the value at row C<$id> and column C<$key>. | ||
| 67 | |||
| 68 | =item * C<exists_$key($id)>: Returns true if a value exists at that location. | ||
| 69 | |||
| 70 | =item * C<delete_$key($id)>: Removes the value at that location. | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =head1 ENVIRONMENT | ||
| 75 | |||
| 76 | =over 4 | ||
| 77 | |||
| 78 | =item * C<STORE_BACKEND>: Set to C<XS> or C<PP> to globally define the preferred | ||
| 79 | backend for the current process. | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =head1 LICENSE | ||
| 84 | |||
| 85 | This library is free software; you can redistribute it and/or modify it | ||
| 86 | under the same terms as Perl itself. | ||
| 87 | |||
| 88 | =cut |