summaryrefslogtreecommitdiff
path: root/obnamlib/fmt_6/chunklist.py
blob: f648d5bbdec9968812bddd12f95e7b09656236d0 (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
# Copyright 2010-2014  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import hashlib
import struct
import random
import tracing

import obnamlib


class ChunkList(obnamlib.RepositoryTree):

    '''Repository's list of chunks.

    The list maps a chunk id to its checksum.

    The list is implemented as a B-tree, with the 64-bit chunk id as the
    key, and the checksum as the value.

    '''

    def __init__(self, fs, node_size, upload_queue_size, lru_size, hooks):
        tracing.trace('new ChunkList')
        self.fmt = '!Q'
        self.key_bytes = struct.calcsize(self.fmt)
        obnamlib.RepositoryTree.__init__(self, fs, 'chunklist', self.key_bytes,
                                         node_size, upload_queue_size,
                                         lru_size, hooks)
        self.keep_just_one_tree = True

    def key(self, chunk_id):
        return struct.pack(self.fmt, chunk_id)

    def add(self, chunk_id, checksum):
        tracing.trace('chunk_id=%s', chunk_id)
        tracing.trace('checksum=%s', repr(checksum))
        self.start_changes()
        self.tree.insert(self.key(chunk_id), checksum)

    def get_checksum(self, chunk_id):
        if self.init_forest() and self.forest.trees:
            t = self.forest.trees[-1]
            return t.lookup(self.key(chunk_id))
        raise KeyError(chunk_id)

    def remove(self, chunk_id):
        tracing.trace('chunk_id=%s', chunk_id)
        self.start_changes()
        key = self.key(chunk_id)
        self.tree.remove_range(key, key)