implemented correct GetParameterValues which unrolls ParameterNames into xsd:strings
[perl-cwmp.git] / lib / CWMP / Store.pm
index a307b31..7a69269 100644 (file)
@@ -7,15 +7,14 @@ use warnings;
 
 use base qw/Class::Accessor/;
 __PACKAGE__->mk_accessors( qw/
-debug
+module
 path
-
-db
+debug
 / );
 
 use Carp qw/confess/;
 use Data::Dump qw/dump/;
-use DBM::Deep;
+use Module::Pluggable search_path => 'CWMP::Store', sub_name => 'possible_stores', require => 1;
 
 =head1 NAME
 
@@ -26,7 +25,9 @@ CWMP::Store - parsist CPE state on disk
 =head2 new
 
   my $store = CWMP::Store->new({
+       module => 'DBMDeep',
        path => '/path/to/state.db',
+       clean => 1,
        debug => 1,
   });
 
@@ -36,25 +37,40 @@ sub new {
        my $class = shift;
        my $self = $class->SUPER::new( @_ );
 
-       warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug;
+       confess "requed parametar module is missing" unless $self->module;
 
-       confess "need path to state.db" unless ( $self->path );
+       # XXX it's important to call possible_stores once, because current_store won't work
+       my @plugins = $self->possible_stores();
 
-       $self->db(
-               DBM::Deep->new(
-                       file => $self->path,
-                       locking => 1,
-                       autoflush => 1,
-               )
-       );
+       warn "Found store plugins: ", join(", ", @plugins ), "\n" if $self->debug;
 
-       foreach my $init ( qw/ state / ) {
-               $self->db->put( $init => {} ) unless $self->db->get( $init );
-       }
+       $self->current_store->open( @_ );
+
+       # so that we don't have to check if it's defined
+       $self->debug( 0 ) unless $self->debug;
 
        return $self;
 }
 
+=head2 current_store
+
+Returns currnet store plugin object
+
+=cut
+
+sub current_store {
+       my $self = shift;
+
+       my $module = $self->module;
+       my $s = $self->only( ref($self).'::'.$module );
+
+       confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
+
+#      warn "#### current store = $s\n" if $self->debug > 4;
+
+       return $s;
+}
+
 =head2 update_state
 
   $store->update_state( ID => $ID, $state );
@@ -71,7 +87,7 @@ sub update_state {
        confess "need $k value" unless $v;
        confess "need state" unless $state;
 
-       warn "## update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug;
+       warn "#### update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug > 4;
 
        my $uid;
 
@@ -86,31 +102,25 @@ sub update_state {
                $uid = $v;
        }
 
-       if ( my $o = $self->db->get('state')->get( $uid ) ) {
-               warn "## update state of $uid [$v]\n" if $self->debug;
-               return $o->import( $state );
-       } else {
-               warn "## create new state for $uid [$v]\n" if $self->debug;
-               return $self->db->get('state')->put( $uid => $state );
-       }
+       $self->current_store->update_uid_state( $uid, $state );
 }
 
-=head2 state
+=head2 get_state
 
-  my $state = $store->state( ID => $ID );
-  my $state = $store->state( uid => $uid );
+  my $state = $store->get_state( ID => $ID );
+  my $state = $store->get_state( uid => $uid );
 
 Returns normal unblessed hash (actually, in-memory copy of state in database).
 
 =cut
 
-sub state {
+sub get_state {
        my $self = shift;
        my ( $k, $v ) = @_;
        confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
        confess "need $k value" unless $v;
 
-       warn "## state( $k => $v )\n" if $self->debug;
+       warn "#### get_state( $k => $v )\n" if $self->debug > 4;
 
        my $uid;
 
@@ -125,24 +135,20 @@ sub state {
                $uid = $v;
        }
 
-       if ( my $state = $self->db->get('state')->get( $uid ) ) {
-               return $state->export;
-       } else {
-               return;
-       }
+       return $self->current_store->get_state( $uid );
 
 }
 
-=head2 known_CPE
+=head2 all_uids
 
-  my @cpe = $store->known_CPE;
+  my @cpe = $store->all_uids;
 
 =cut
 
-sub known_CPE {
+sub all_uids {
        my $self = shift;
-       my @cpes = keys %{ $self->db->{state} };
-       warn "all CPE: ", dump( @cpes ), "\n" if $self->debug;
+       my @cpes = $self->current_store->all_uids;
+       warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug;
        return @cpes;
 }
 
@@ -163,7 +169,7 @@ sub ID_to_uid {
 
        confess "need ID" unless $ID;
 
-       warn "ID_to_uid",dump( $ID, $state ),$/ if $self->debug;
+       warn "#### ID_to_uid",dump( $ID, $state ),$/ if $self->debug > 4;
 
        $session->{ $ID }->{last_seen} = time();
 
@@ -180,13 +186,10 @@ sub ID_to_uid {
                return $uid;
        } else {
                warn "## can't find uid for ID $ID, first seen?\n";
-               return;
        }
 
        # TODO: expire sessions longer than 30m
 
-       warn "current session = ",dump( $session );
-
        return;
 }