-
Notifications
You must be signed in to change notification settings - Fork 199
/
HashStore.pm
244 lines (181 loc) · 5.8 KB
/
HashStore.pm
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
=head1 NAME
Bio::JBrowse::HashStore - on-disk 2-level hash table
=head1 DESCRIPTION
Makes an on-disk hash table, designed to be easily read as static files over HTTP.
Makes a set of JSON-encoded files at paths based on a hash of the key.
For example:
path/to/dir/29c/c14/fc.json
path/to/dir/23f/5ad/11.json
path/to/dir/711/7c1/29.json
path/to/dir/5ec/b24/6a.json
path/to/dir/4de/9ac/c6.json
path/to/dir/41b/c43/27.json
path/to/dir/28c/d86/e9.json
Where each file contains a JSON object containing data items like:
{ foo: "bar", ... }
Where "foo" is the original key, and "bar" is the JSON-encoded data.
=cut
package Bio::JBrowse::HashStore;
use strict;
use warnings;
use JSON 2;
use File::Spec ();
use File::Path ();
use Cache::Ref::FIFO ();
use ExternalSorter;
my $bucket_class = 'Bio::JBrowse::HashStore::Bucket';
=head2 open( dir => "/path/to/dir", hash_bits => 16 )
=cut
sub open {
my $class = shift;
# source of data: defaults, overridden by open args, overridden by meta.json contents
my $self = bless { @_ }, $class;
$self->empty if $self->{empty};
%$self = (
%$self,
%{$self->_read_meta}
);
$self->{hash_bits} ||= 16;
$self->{hash_characters} = int( $self->{hash_bits}/4 );
$self->{file_extension} = '.json';
$self->{bucket_cache} = Cache::Ref::FIFO->new( size => 200 );
$self->{bucket_path_cache} = Cache::Ref::FIFO->new( size => 1000 );
return bless $self, $class;
}
# write out meta.json file when the store itself is destroyed
sub DESTROY {
my ( $self ) = @_;
File::Path::mkpath( $self->{dir} );
my $meta_path = $self->_meta_path;
CORE::open my $out, '>', $meta_path or die "$! writing $meta_path";
$out->print( JSON::to_json(
{
hash_bits => $self->{hash_bits},
%{ $self->{meta} || {} }
}
)) or die "$! writing $meta_path";
}
sub _meta_path {
File::Spec->catfile( shift->{dir}, 'meta.json' );
}
sub _read_meta {
my ( $self ) = @_;
my $meta_path = $self->_meta_path;
return {} unless -r $meta_path;
CORE::open my $meta, '<', $meta_path or die "$! reading $meta_path";
local $/;
my $d = eval { JSON::from_json( scalar <$meta> ) };
warn $@ if $@;
return $d || {};
}
=head2 get( $key )
=cut
sub get {
my ( $self, $key ) = @_;
my $bucket = $self->_getBucket( $key );
return $bucket->{data}{$key};
}
=head2 set( $key, $value )
=cut
sub set {
my ( $self, $key, $value ) = @_;
my $bucket = $self->_getBucket( $key );
$bucket->{data}{$key} = $value;
$bucket->{dirty} = 1;
$self->{meta}{last_changed_entry} = $key;
return $value;
}
=head2 sort_stream( $data_stream )
Given a data stream (sub that returns arrayrefs of [ key, ... ] when
called repeatedly), returns another data stream that returns the
arrayrefs in order of their hash bucket numbers.
Using this can greatly speed up bulk operations on the hash store,
because it allows the internal caches of the HashStore to operate at
maximum efficiency.
This is achieved by doing an external sort of the data items, which
involves writing all of the data items to temporary files and then
reading them back in sorted order.
=cut
sub sort_stream {
my ( $self, $in_stream ) = @_;
my $sorter = ExternalSorter->new(
sub ($$) {
$_[0]->[0] cmp $_[1]->[0]
}, 32_000_000 );
while( my $data = $in_stream->() ) {
# hash each of the keys and values, spool them to a single log file
$sorter->add( [ $self->_hexHash( $data->[0] ), $data ] );
}
$sorter->finish;
return sub {
my $d = $sorter->get or return;
return $d->[1];
};
}
=head2 empty
Clear the store of all contents. Deletes all files and directories
from the store directory.
=cut
sub empty {
my ( $self ) = @_;
File::Path::rmtree( $self->{dir} );
File::Path::mkpath( $self->{dir} );
}
########## helper methods ###########
sub _hexHash {
my ( $self, $key ) = @_;
my $crc = ( $self->{crc} ||= do { require Digest::Crc32; Digest::Crc32->new } )
->strcrc32( $key );
my $hex = lc sprintf( '%08x', $crc );
$hex = substr( $hex, 8-$self->{hash_characters} );
return $hex;
}
sub _hexToPath {
my ( $self, $hex ) = @_;
my @dir = ( $self->{dir}, reverse $hex =~ /(.{1,3})/g );
my $file = (pop @dir).$self->{file_extension};
my $dir = File::Spec->catdir(@dir);
return { dir => $dir, fullpath => File::Spec->catfile( $dir, $file ) };
}
sub _getBucket {
my ( $self, $key ) = @_;
my $pathinfo = $self->{bucket_path_cache}->compute( $key, sub { $self->_hexToPath( $self->_hexHash( $key ) ); } );
return $self->{bucket_cache}->compute( $pathinfo->{fullpath}, sub { $self->_readBucket( $pathinfo ); } );
}
sub _readBucket {
my ( $self, $pathinfo ) = @_;
my $path = $pathinfo->{fullpath};
my $dir = $pathinfo->{dir};
if( -f $path ) {
local $/;
CORE::open my $in, '<', $path or die "$! reading $path";
return $bucket_class->new(
dir => $dir,
fullpath => $path,
data => eval { JSON::from_json( scalar <$in> ) } || {}
);
}
else {
return $bucket_class->new(
dir => $dir,
fullpath => $path,
data => {},
dirty => 1
);
}
}
######## inner class for on-disk hash buckets ##########
package Bio::JBrowse::HashStore::Bucket;
sub new {
my $class = shift;
bless { @_ }, $class;
}
# when a bucket is deleted, flush it to disk
sub DESTROY {
my ( $self ) = @_;
return unless $self->{dirty} && %{$self->{data}};
File::Path::mkpath( $self->{dir} );
CORE::open my $out, '>', $self->{fullpath} or die "$! writing $self->{fullpath}";
$out->print( JSON::to_json( $self->{data} ) ) or die "$! writing to $self->{fullpath}";
}
1;