1
# Copyright 2015 Lars Wirzenius
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
# =*= License: GPL-3+ =*=
25
class GAChunkIndexes(object):
29
self.set_dirname('chunk-indexes')
35
def set_dirname(self, dirname):
36
self._dirname = dirname
38
def get_dirname(self):
43
self._data_is_loaded = False
50
blob = obnamlib.serialise_object(self._data)
51
filename = self._get_filename()
52
self._fs.overwrite_file(filename, blob)
54
def _get_filename(self):
55
return os.path.join(self.get_dirname(), 'data.dat')
57
def prepare_chunk_for_indexes(self, chunk_content):
58
return hashlib.sha512(chunk_content).hexdigest()
60
def put_chunk_into_indexes(self, chunk_id, token, client_id):
63
self._data['index'].append({
66
'client-id': client_id,
70
if not self._data_is_loaded:
71
filename = self._get_filename()
72
if self._fs.exists(filename):
73
blob = self._fs.cat(filename)
74
self._data = obnamlib.deserialise_object(blob)
75
assert self._data is not None
78
self._data_is_loaded = True
80
def _prepare_data(self):
81
if 'index' not in self._data:
82
self._data['index'] = []
84
def find_chunk_ids_by_content(self, chunk_content):
86
if 'index' in self._data:
87
token = self.prepare_chunk_for_indexes(chunk_content)
90
for record in self._data['index']
91
if record['sha512'] == token]
96
raise obnamlib.RepositoryChunkContentNotInIndexes()
99
def remove_chunk_from_indexes(self, chunk_id, client_id):
103
self._data['index'] = self._filter_out(
106
x['chunk-id'] == chunk_id and x['client-id'] == client_id)
108
def _filter_out(self, records, pred):
109
return [record for record in records if not pred(record)]
111
def remove_chunk_from_indexes_for_all_clients(self, chunk_id):
115
self._data['index'] = self._filter_out(
117
lambda x: x['chunk-id'] == chunk_id)
119
def validate_chunk_content(self, chunk_id):