summaryrefslogtreecommitdiffstats
path: root/lib/App/BorgRestore/PathTimeTable/Memory.pm
blob: 012e937fe875481cbf9d34dd73e50892b2167cfd (plain)
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
package App::BorgRestore::PathTimeTable::Memory;
use v5.14;
use strict;
use warnings;

use Function::Parameters;

=head1 NAME

App::BorgRestore::PathTimeTable::Memory - In-Memory preparation of new archive data

=head1 DESCRIPTION

This is used by L<App::BorgRestore> to add new archive data into the database.
Data is prepared in memory first and only written to the database once.

=cut

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 set_archive_id($archive_id) {
	$self->{archive_id} = $archive_id;
}

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() {
	$self->_save_node($self->{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__