summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2018-09-06 16:02:04 +0200
committerFlorian Pritz <bluewind@xinu.at>2018-09-06 18:12:31 +0200
commit892db03a49333daf7d808395a0a25e81ec2a76ab (patch)
tree5eff23956ca91d49b3652a3f454f3471e171bc47
parenta6ef292ee6aca67cef76ebcee0d6bca19698ff39 (diff)
downloadApp-BorgRestore-892db03a49333daf7d808395a0a25e81ec2a76ab.tar.gz
App-BorgRestore-892db03a49333daf7d808395a0a25e81ec2a76ab.tar.xz
Extract lookup table into dedicated package
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--lib/App/BorgRestore.pm47
-rw-r--r--lib/App/BorgRestore/PathTimeTable/Memory.pm67
2 files changed, 71 insertions, 43 deletions
diff --git a/lib/App/BorgRestore.pm b/lib/App/BorgRestore.pm
index 5fd36ff..cad8cb1 100644
--- a/lib/App/BorgRestore.pm
+++ b/lib/App/BorgRestore.pm
@@ -8,6 +8,7 @@ our $VERSION = "3.1.0";
use App::BorgRestore::Borg;
use App::BorgRestore::DB;
use App::BorgRestore::Helper;
+use App::BorgRestore::PathTimeTable::Memory;
use App::BorgRestore::Settings;
use autodie;
@@ -376,32 +377,6 @@ method restore_simple($path, $timespec, $destination) {
$self->restore($backup_path, $selected_archive, $destination);
}
-method _add_path_to_hash($hash, $path, $time) {
- my @components = split /\//, $path;
-
- my $node = $hash;
-
- if ($path eq ".") {
- if ($time > $$node[1]) {
- $$node[1] = $time;
- }
- return;
- }
-
- # each node is an arrayref of the format [$hashref_of_children, $mtime]
- # $hashref_of_children is undef if there are no children
- for my $component (@components) {
- if (!defined($$node[0]->{$component})) {
- $$node[0]->{$component} = [undef, $time];
- }
- # update mtime per child
- if ($time > $$node[1]) {
- $$node[1] = $time;
- }
- $node = $$node[0]->{$component};
- }
-}
-
=head3 search_path
my $paths = $app->search_path($pattern)
@@ -467,7 +442,7 @@ method _handle_added_archives($borg_archives) {
for my $archive (@$add_archives) {
my $start = Time::HiRes::gettimeofday();
- my $lookuptable = [{}, 0];
+ my $lookuptable = App::BorgRestore::PathTimeTable::Memory->new({db => $self->{db}});
$log->infof("Adding archive %s", $archive);
@@ -480,7 +455,7 @@ method _handle_added_archives($borg_archives) {
if ($line =~ m/^.{4} (?<year>....)-(?<month>..)-(?<day>..) (?<hour>..):(?<minute>..):(?<second>..) (?<path>.+)$/) {
my $time = POSIX::mktime($+{second},$+{minute},$+{hour},$+{day},$+{month}-1,$+{year}-1900);
#$log->debugf("Adding path %s with time %s", $+{path}, $time);
- $self->_add_path_to_hash($lookuptable, $+{path}, $time);
+ $lookuptable->add_path($+{path}, $time);
}
});
@@ -489,7 +464,7 @@ method _handle_added_archives($borg_archives) {
$self->{db}->begin_work;
$self->{db}->add_archive_name($archive);
my $archive_id = $self->{db}->get_archive_id($archive);
- $self->_save_node($archive_id, undef, $lookuptable);
+ $lookuptable->save_nodes($archive_id);
$self->{db}->commit;
$self->{db}->vacuum;
$self->{db}->verify_cache_fill_rate_ok();
@@ -499,20 +474,6 @@ method _handle_added_archives($borg_archives) {
}
}
-method _save_node($archive_id, $prefix, $node) {
- for my $child (keys %{$$node[0]}) {
- my $path;
- $path = $prefix."/" if defined($prefix);
- $path .= $child;
-
- my $time = $$node[0]->{$child}[1];
- $self->{db}->add_path($archive_id, $path, $time);
-
- $self->_save_node($archive_id, $path, $$node[0]->{$child});
- }
-}
-
-
=head3 update_cache
$app->update_cache();
diff --git a/lib/App/BorgRestore/PathTimeTable/Memory.pm b/lib/App/BorgRestore/PathTimeTable/Memory.pm
new file mode 100644
index 0000000..7aee5b0
--- /dev/null
+++ b/lib/App/BorgRestore/PathTimeTable/Memory.pm
@@ -0,0 +1,67 @@
+package App::BorgRestore::PathTimeTable::Memory;
+use v5.14;
+use strict;
+use warnings;
+
+use Function::Parameters;
+
+method new($class: $deps = {}) {
+ return $class->new_no_defaults($deps);
+}
+
+method new_no_defaults($class: $deps = {}) {
+ my $self = {};
+ bless $self, $class;
+ $self->{deps} = $deps;
+ $self->{lookuptable} = [{}, 0];
+ $self->{nodes_to_flatten} = [];
+ $self->{nodes} = [];
+ return $self;
+}
+
+method add_path($path, $time) {
+ my @components = split /\//, $path;
+
+ my $node = $self->{lookuptable};
+
+ if ($path eq ".") {
+ if ($time > $$node[1]) {
+ $$node[1] = $time;
+ }
+ return;
+ }
+
+ # each node is an arrayref of the format [$hashref_of_children, $mtime]
+ # $hashref_of_children is undef if there are no children
+ for my $component (@components) {
+ if (!defined($$node[0]->{$component})) {
+ $$node[0]->{$component} = [undef, $time];
+ }
+ # update mtime per child
+ if ($time > $$node[1]) {
+ $$node[1] = $time;
+ }
+ $node = $$node[0]->{$component};
+ }
+}
+
+method save_nodes($archive_id) {
+ $self->_save_node($archive_id, undef, $self->{lookuptable});
+}
+
+method _save_node($archive_id, $prefix, $node) {
+ for my $child (keys %{$$node[0]}) {
+ my $path;
+ $path = $prefix."/" if defined($prefix);
+ $path .= $child;
+
+ my $time = $$node[0]->{$child}[1];
+ $self->{deps}->{db}->add_path($archive_id, $path, $time);
+
+ $self->_save_node($archive_id, $path, $$node[0]->{$child});
+ }
+}
+
+1;
+
+__END__