1 |
williamc |
1.1 |
#
|
2 |
|
|
# ObjectStore.pm
|
3 |
|
|
#
|
4 |
|
|
# Originally Written by Christopher Williams
|
5 |
|
|
#
|
6 |
|
|
# Description
|
7 |
|
|
# -----------
|
8 |
|
|
# An very simple object persistency manager for small scale use.
|
9 |
|
|
# Objects are saved and recovered on demand
|
10 |
|
|
# Objects to be stored in this manager must implement the following interface
|
11 |
williamc |
1.2 |
# new(ObjectStore) :
|
12 |
|
|
# store(location) : dump out to location
|
13 |
|
|
# restore(location) : restore from location
|
14 |
williamc |
1.5 |
# objects must inherit from StorableObject
|
15 |
williamc |
1.1 |
#
|
16 |
|
|
# Interface
|
17 |
|
|
# ---------
|
18 |
williamc |
1.3 |
# new(store_directory,[object]) : A new ObjectStore object. An alternative
|
19 |
|
|
# object can be specified with which to
|
20 |
|
|
# construct the stored objects with by
|
21 |
|
|
# supplying the object reference
|
22 |
williamc |
1.13 |
# if store_directory begins with "<" - does
|
23 |
|
|
# not attempt to build it if it dosnt exist
|
24 |
williamc |
1.1 |
# store(oref,@keys) : store an object - each object must have
|
25 |
|
|
# a unique key
|
26 |
|
|
# find(@keys) : find and recover object(s) from the store
|
27 |
|
|
# that match keys. Returned as a list of oref
|
28 |
williamc |
1.2 |
# sequence(@keys) : return the sequence number of the object
|
29 |
|
|
# matching the keys
|
30 |
|
|
# An object is assigned a new sequence number
|
31 |
|
|
# with each store.
|
32 |
williamc |
1.9 |
# delete(@keys) : Delete the object with the given key
|
33 |
|
|
# alias(\@refofkeys,\@aliaskeys) : attatch another set of keys to objects that
|
34 |
|
|
# match the refofkeys
|
35 |
|
|
# note that these are references to the arrays
|
36 |
|
|
# unalias(@aliaskeys) :remove an alias
|
37 |
williamc |
1.12 |
# location() : return the top directory where the store is
|
38 |
|
|
# located
|
39 |
williamc |
1.1 |
|
40 |
|
|
package ObjectUtilities::ObjectStore;
|
41 |
|
|
require 5.004;
|
42 |
|
|
use Utilities::AddDir;
|
43 |
|
|
use Utilities::HashDB;
|
44 |
|
|
|
45 |
|
|
sub new {
|
46 |
|
|
my $class=shift;
|
47 |
|
|
my $dir=shift;
|
48 |
|
|
$self={};
|
49 |
williamc |
1.3 |
# do we have an alternative object to instantiate objects with
|
50 |
|
|
if ( @_ ) {
|
51 |
|
|
$self->{storeobject}=shift;
|
52 |
|
|
}
|
53 |
|
|
else {
|
54 |
|
|
$self->{storeobject}=$self;
|
55 |
|
|
}
|
56 |
williamc |
1.1 |
bless $self, $class;
|
57 |
williamc |
1.13 |
($self->{location}=$dir)=~s/^\<//;
|
58 |
|
|
$self->{admindir}=$self->{location}."/admin";
|
59 |
|
|
if ( $dir=~"^\<" ) {
|
60 |
|
|
}
|
61 |
|
|
else {
|
62 |
|
|
AddDir::adddir($self->{admindir});
|
63 |
|
|
}
|
64 |
|
|
if ( -d $self->{location} ) {
|
65 |
williamc |
1.1 |
$self->{storage}=$dir."/objects";
|
66 |
|
|
AddDir::adddir($self->{storage});
|
67 |
|
|
$self->_init();
|
68 |
|
|
}
|
69 |
|
|
else {
|
70 |
|
|
return undef;
|
71 |
|
|
}
|
72 |
williamc |
1.9 |
#print "ObjectStore -- $self->{storeobject}\n";
|
73 |
williamc |
1.1 |
return $self;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
williamc |
1.12 |
sub location {
|
77 |
|
|
my $self=shift;
|
78 |
|
|
return $self->{location};
|
79 |
|
|
}
|
80 |
williamc |
1.1 |
|
81 |
|
|
sub store {
|
82 |
|
|
my $self=shift;
|
83 |
|
|
my $oref=shift;
|
84 |
|
|
my @keys=@_;
|
85 |
|
|
|
86 |
|
|
my $oreftype=ref($oref);
|
87 |
williamc |
1.11 |
#print "$self: Storing an object of type : $oreftype\n";
|
88 |
williamc |
1.2 |
my $filename=$self->_filename(@keys);
|
89 |
williamc |
1.8 |
$oref->store($self->_fullfilename($filename));
|
90 |
williamc |
1.2 |
# Update the sequence number to track changes
|
91 |
|
|
my @seqnumbers=$self->{filehash}->getdata("__seq_number",@keys);
|
92 |
|
|
my $seqnumb=((@seqnumbers)?$seqnumbers[0]:-1)+1;
|
93 |
|
|
$self->{filehash}->deletedata("__seq_number",@keys);
|
94 |
|
|
$self->{filehash}->setdata($seqnumb,"__seq_number",@keys);
|
95 |
|
|
|
96 |
williamc |
1.1 |
$self->{filehash}->deletedata(@keys);
|
97 |
|
|
$self->{filehash}->setdata($filename,@keys); # persistent
|
98 |
|
|
$self->{typehash}->deletedata($filename);
|
99 |
|
|
$self->{typehash}->setdata($oreftype,$filename); # persistent
|
100 |
|
|
$self->{orefhash}->deletedata($filename);
|
101 |
|
|
$self->{orefhash}->setdata($oref,$filename); # transient
|
102 |
|
|
$self->_storedb();
|
103 |
|
|
}
|
104 |
|
|
|
105 |
williamc |
1.2 |
sub sequence {
|
106 |
|
|
my $self=shift;
|
107 |
|
|
my @keys=@_;
|
108 |
|
|
|
109 |
|
|
my @seq=$self->{filehash}->getdata("__seq_number",@keys);
|
110 |
williamc |
1.4 |
return (($#seq == 0)?$seq[0]:-1);
|
111 |
williamc |
1.2 |
}
|
112 |
|
|
|
113 |
williamc |
1.1 |
sub find {
|
114 |
|
|
my $self=shift;
|
115 |
williamc |
1.3 |
my @keys=@_;
|
116 |
williamc |
1.1 |
|
117 |
|
|
my @oref=();
|
118 |
|
|
my $oref;
|
119 |
|
|
my @types;
|
120 |
|
|
my $type;
|
121 |
|
|
my @validobjs=();
|
122 |
|
|
my $fh;
|
123 |
|
|
|
124 |
williamc |
1.11 |
#print "$self: Searching for : @keys\n";
|
125 |
williamc |
1.1 |
my @datafiles=$self->{filehash}->getdata(@keys);
|
126 |
|
|
foreach $file ( @datafiles ) {
|
127 |
|
|
@oref=$self->{orefhash}->getdata($file);
|
128 |
|
|
if ( $#oref < 0 ) { # we need to instatniate it
|
129 |
|
|
@types=$self->{typehash}->getdata($file);
|
130 |
|
|
$type=$types[0];
|
131 |
|
|
eval "require $type";
|
132 |
williamc |
1.3 |
$oref=$type->new($self->{storeobject});
|
133 |
williamc |
1.8 |
$oref->restore($self->_fullfilename($file));
|
134 |
williamc |
1.1 |
}
|
135 |
|
|
else {
|
136 |
|
|
$oref=$oref[0];
|
137 |
|
|
}
|
138 |
|
|
push @validobjs, $oref;
|
139 |
|
|
}
|
140 |
|
|
return @validobjs;
|
141 |
williamc |
1.9 |
}
|
142 |
|
|
|
143 |
|
|
sub alias {
|
144 |
|
|
my $self=shift;
|
145 |
|
|
$self->{filehash}->alias(@_);
|
146 |
williamc |
1.10 |
$self->_storedb();
|
147 |
williamc |
1.9 |
}
|
148 |
|
|
|
149 |
|
|
sub unalias {
|
150 |
|
|
my $self=shift;
|
151 |
|
|
$self->{filehash}->unalias(@_);
|
152 |
williamc |
1.10 |
$self->_storedb();
|
153 |
williamc |
1.9 |
}
|
154 |
|
|
|
155 |
|
|
sub delete {
|
156 |
|
|
my $self=shift;
|
157 |
|
|
my @keys=@_;
|
158 |
|
|
|
159 |
|
|
my $filename=$self->_filename(@keys);
|
160 |
|
|
$self->{filehash}->deletedata("__seq_number",@keys);
|
161 |
|
|
$self->{filehash}->deletedata(@keys);
|
162 |
|
|
$self->{typehash}->deletedata($filename);
|
163 |
|
|
$self->{orefhash}->deletedata($filename);
|
164 |
|
|
$self->_storedb();
|
165 |
|
|
print "Deleting ".$self->_fullfilename($filename)."\n";
|
166 |
|
|
unlink $self->_fullfilename($filename);
|
167 |
williamc |
1.1 |
}
|
168 |
|
|
|
169 |
|
|
#
|
170 |
|
|
# Private routines
|
171 |
|
|
#
|
172 |
|
|
sub _filename {
|
173 |
|
|
my $self=shift;
|
174 |
|
|
my @keys=@_;
|
175 |
|
|
|
176 |
williamc |
1.2 |
my ($file)=$self->{filehash}->getdata(@keys);
|
177 |
williamc |
1.3 |
if ( ! defined $file ) {
|
178 |
williamc |
1.2 |
# need to generate a new filename - a random number will do
|
179 |
|
|
srand();
|
180 |
|
|
do {
|
181 |
williamc |
1.7 |
$file=int(rand 99999999)+1;
|
182 |
|
|
} until ( ! ( -e $self->_fullfilename($file) ) );
|
183 |
williamc |
1.2 |
}
|
184 |
williamc |
1.7 |
return $file;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
sub _fullfilename {
|
188 |
|
|
my $self=shift;
|
189 |
|
|
my $file=shift;
|
190 |
|
|
|
191 |
|
|
if ( $file!~/^\// ) { # return only full path names
|
192 |
|
|
$file=$self->{storage}."/".$file;
|
193 |
|
|
}
|
194 |
williamc |
1.2 |
return $file;
|
195 |
williamc |
1.1 |
}
|
196 |
|
|
|
197 |
|
|
sub _storedb {
|
198 |
|
|
my $self=shift;
|
199 |
|
|
$self->{filehash}->store($self->{admindir}."/filedb");
|
200 |
|
|
$self->{typehash}->store($self->{admindir}."/typedb");
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
sub _restoredb {
|
204 |
|
|
my $self=shift;
|
205 |
|
|
if ( -f $self->{admindir}."/filedb" ) {
|
206 |
|
|
$self->{filehash}->restore($self->{admindir}."/filedb");
|
207 |
|
|
$self->{typehash}->restore($self->{admindir}."/typedb");
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
sub _openfile {
|
212 |
|
|
my $self=shift;
|
213 |
|
|
my $filename=shift;
|
214 |
|
|
|
215 |
|
|
local $fh=FileHandle->new();
|
216 |
|
|
open ( $fh, $filename) or die "Unable to open $filename\n $!\n";
|
217 |
|
|
return $fh;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
sub _init {
|
221 |
|
|
my $self=shift;
|
222 |
|
|
$self->{filehash}=Utilities::HashDB->new();
|
223 |
|
|
$self->{typehash}=Utilities::HashDB->new();
|
224 |
|
|
$self->{orefhash}=Utilities::HashDB->new();
|
225 |
|
|
$self->_restoredb();
|
226 |
|
|
}
|