File: DB_File.pm

package info (click to toggle)
libvcp-perl 0.9-20050110-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,608 kB
  • ctags: 827
  • sloc: perl: 18,194; makefile: 42; sh: 11
file content (273 lines) | stat: -rw-r--r-- 5,486 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
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
package VCP::DB_File;

=head1 NAME

VCP::DB_File - Persistant storage for vcp_state data

=head1 SYNOPSIS

    use base qw( VCP::DB_File );

=head1 DESCRIPTION

By default, most VCP::Dest::* drivers keep track of the relationship
between the id field assigned by the (original) VCP::Source::* driver
and the final name and rev_id (or whatever fields are important to it)
in the destination repository so that the previous_id fields, which
refer to the original id, may be resolved when backfilling or branching.

The VCP::*::revml drivers do not do this; they do not need to resolve id
fields.

The intent for this file is to serve as a base class so that individual
sites may write their own ", ref $self, " plugins to, for instance, store this
state in a RDBMS table.  This is not quite offered at this time; we need
to add an option to the appropriate VCP::Dest::* modules to allow the
appropriate ", ref $self, " file to be loaded.

To write your own ", ref $self, " file, see VCP::DB_File::sdbm.

=for test_script t/01db_file.t

=cut

$VERSION = 1 ;

use strict ;
use Carp;
use VCP::Debug qw( :debug );
use VCP::Utils qw( start_dir_rel2abs );

#use fields (
#   'StoreLoc',   ## Where the data should be kept.
#   'TableName',  ## database table name
#);

=head2 Methods

=over

=cut

=item new

   VCP::DB_File::foo->new(
      StoreLoc => $dir,  ## path to a dir to keep the state store in
   );

The C<Store> field indicates where the ", ref $self, " should be stored, for
instance a DBI specification string or a directory to place a
"revmap.db" file in.  There is no control over the filename, as
different storage modes may need different conventions.

=cut

sub new {
   my $class = shift;
   my $self = bless { @_ }, $class;

   my $type = delete $self->{Type} || "sdbm";

   Carp::confess "undefined TableName" unless defined $self->{TableName};

   $self->{StoreLoc} = "vcp_state"
      unless defined $self->{StoreLoc};

   $self->{StoreLoc} = start_dir_rel2abs( 
      File::Spec->catdir( $self->{StoreLoc}, $self->{TableName} )
   );

   debug "storing ", ref $self, " in ", $self->store_loc
      if debugging;

   return $self ;
}

=item store_loc

Gets (does not set) the StoreLoc field as an absolute path.

=cut

sub store_loc {
   my $self = shift;
   return $self->{StoreLoc};
}

=item delete_db

   $db->delete_db;

Deletes the persitent store.  This should remove all files, lock files,
etc.  for filesystem stores and drop any tables for RDBMS stores.

Default action is to call close_db; subclasses should perform the
deletion.

(subclasses should call C<$self->SUPER::delete_db before doing anything
else in their delete_db() overrides).

=cut

sub delete_db {
   my $self = shift;

   $self->close_db;
   debug "deleting ", ref $self, " in ", $self->store_loc if debugging;
}


=item open_db

   $db->open_db;

Creates a new or opens an existing db.

(subclasses should call C<$self->SUPER::open_db before doing anything
else in their open_db() overrides).

=cut

sub open_db {
   my $self = shift;
   debug "opening ", ref $self, " in ", $self->store_loc if debugging;
}

=item close_db

   $db->close_db;

(subclasses should call C<$self->SUPER::close_db before doing anything
else in their close_db() overrides).

=cut

sub close_db {
   my $self = shift;
   debug "closing ", ref $self, " in ", $self->store_loc if debugging;
}

=item set

   $db->set( $key, @values );

Sets the values for $key.

=cut

=item get

   my @values = $db->get( $key );

Gets the values for $key.

=item keys

   my @keys = $db->keys;

Returns a list of ARRAY references.  Each ARRAY ref is a key.

=cut

=back

=head1 HELPER METHODS

These are provided to make subclassing a tad easier

=over

=cut

=item mkdir_store_loc

   $self->mkdir_store_loc;

A helper method for subclasses' open_db()s to create the directory
referred to by store_loc and any missing parent dirs.

=cut

sub mkdir_store_loc {
   my $self = shift;

   return if -e $self->store_loc;

   debug "making dir ", $self->store_loc
      if debugging;

   require File::Path;
   File::Path::mkpath( [ $self->store_loc ] );
}

=item rmdir_store_loc

   $self->rmdir_store_loc;

A helper method for subclasses' delete_db()s to remove the directory
referred to by store_loc.

=cut

sub rmdir_store_loc {
   my $self = shift;

   return unless -e $self->store_loc;

   require File::Path;
   File::Path::rmtree( [ $self->store_loc ] );
}

=item pack_values

   my $v = $self->pack_values( @values );

Combines the parameters in to a single string.

=cut

## The serialization is designed to be simple: we alter '%' to '%%' and ';' to
## '%,', then join with ";".  This way, we can split on ';', then
## descape all '%%' and '%,'.  We might be able to optimize this a bit
## by only using one s/// in packing and only one in unpacking.

sub pack_values {
   shift;
   confess "no values to pack" unless @_;
   confess "can't pack undef" if grep !defined, @_;
   join ";", map { my $v = $_; $v =~ s/%/%%/g; $v =~ s/;/%,/g; $v } @_;
}

=item upack_values

   my @v = $self->unpack_values( $v );

=cut

sub unpack_values {
   shift;
   confess unless @_ == 1;
   my @v = split /;/, "$_[0];", -1;
   pop @v;
   @v = map { s/%%/%/g; s/%,/;/g; $_ } @v;
   return @v;
}



=back

=head1 AUTHOR

Barrie Slaymaker <barries@slaysys.com>

=head1 COPYRIGHT

Copyright (c) 2000, 2001, 2002 Perforce Software, Inc.
All rights reserved.

See L<VCP::License|VCP::License> (C<vcp help license>) for the terms of use.

=cut

1