App-BorgRestore

 view release on metacpan or  search on metacpan

lib/App/BorgRestore/DB.pm  view on Meta::CPAN

}

method get_archive_id($archive) {
	my $st = $self->{dbh}->prepare("select `id` from `archives` where `archive_name` = ?;");
	$archive = untaint($archive, qr(.*));
	$st->execute($archive);
	my $result = $st->fetchrow_hashref;
	return untaint($result->{id}, qr(.*));
}

method get_archives_for_path($path) {
	my $st = $self->{dbh}->prepare('select * from `files` where `path` = ?;');
	$st->execute(untaint($path, qr(.*)));

	my @ret;

	my $result = $st->fetchrow_hashref;
	my $archives = $self->get_archive_names();

	for my $archive (@$archives) {
		my $archive_id = $self->get_archive_id($archive);
		my $timestamp = $result->{$archive_id};

		push @ret, {
			modification_time => $timestamp,
			archive => $archive,
		};
	}

	return \@ret;
}

method _insert_path($archive_id, $path, $time) {
	my $st = $self->{dbh}->prepare_cached('insert or ignore into `files` (`path`, `'.$archive_id.'`)
		values(?, ?)');
	$st->execute($path, $time);
}

method add_path($archive_id, $path, $time) {
	$self->_insert_path($archive_id, $path, $time);

	my $st = $self->{dbh}->prepare_cached('update files set `'.$archive_id.'` = ? where `path` = ?');
	$st->execute($time, $path);
}

method update_path_if_greater($archive_id, $path, $time) {
	$self->_insert_path($archive_id, $path, $time);

	my $st = $self->{dbh}->prepare_cached('update files set `'.$archive_id.'` = ? where `path` = ? and (`'.$archive_id.'` < ? or `'.$archive_id.'` is null)');
	$st->execute($time, $path, $time);
}

method begin_work() {
	$self->{dbh}->begin_work();
}

method commit() {
	$self->{dbh}->commit();
}

method verify_cache_fill_rate_ok() {
	my $used = $self->{dbh}->sqlite_db_status()->{cache_used}->{current};
	$log->debugf("sqlite page cache usage: %s", format_bytes($used, si=>1));
	if ($used > $self->{cache_size} * 1024 * 0.95) {
		$log->debugf("sqlite cache usage is %s of %s", format_bytes($used, si=>1), format_bytes($self->{cache_size} * 1024, si => 1));
		$log->debug("Consider increasing the sqlite cache if you notice performance issues (see documentation of App::BorgRestore::Settings)");
	}
}

method search_path($pattern) {
	$log->debugf("Preparing path search for pattern '%s'", $pattern);
	my $st = $self->{dbh}->prepare('select path from files where path like ?');
	$log->debug("Executing search");
	$st->execute($pattern);
	$log->debug("Fetching search result");

	my @ret;
	while (my $row = $st->fetchrow_hashref()) {
		push @ret, $row->{path};
	}

	$log->debugf("Found %d matching paths", 0+@ret);
	return \@ret;
}

method vacuum() {
	$self->{dbh}->do("vacuum");
}


1;

__END__



( run in 0.806 second using v1.01-cache-2.11-cpan-39bf76dae61 )