summaryrefslogtreecommitdiff
path: root/obnamlib/blob_store.py
blob: 219e585a06996d38498e7b1702f319023c0b60c3 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright 2015-2016  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/>.
#
# =*= License: GPL-3+ =*=


import obnamlib


class BlobStore(object):

    def __init__(self):
        self._bag_store = None
        self._bag = None
        self._max_bag_size = 0
        self._cached_blobs = BlobCache()
        self._cached_blobs.set_max_bytes(2**24)

    def set_bag_store(self, bag_store):
        self._bag_store = bag_store

    def set_max_bag_size(self, max_bag_size):
        self._max_bag_size = max_bag_size

    def set_max_cache_bytes(self, max_bytes):  # pragma: no cover
        self._cached_blobs.set_max_bytes(max_bytes)

    def get_blob(self, blob_id):
        bag_id, index = obnamlib.parse_object_id(blob_id)
        if self._bag and bag_id == self._bag.get_id():
            return self._bag[index]
        if blob_id in self._cached_blobs:
            return self._cached_blobs.get(blob_id)
        if self._bag_store.has_bag(bag_id):
            bag = self._bag_store.get_bag(bag_id)
            for i, this_blob in enumerate(bag):
                this_id = obnamlib.make_object_id(bag_id, i)
                self._cached_blobs.put(this_id, this_blob)
            return bag[index]
        return None

    def put_blob(self, blob):
        if self._bag is None:
            self._bag = self._new_bag()
        blob_id = self._bag.append(blob)
        if self._bag.get_bytes() >= self._max_bag_size:
            self.flush()
        return blob_id

    def _new_bag(self):
        bag = obnamlib.Bag()
        bag.set_id(self._bag_store.reserve_bag_id())
        return bag

    def get_well_known_blob(self, well_known_name):
        if self._bag_store.has_bag(well_known_name):
            bag = self._bag_store.get_bag(well_known_name)
            if len(bag) > 0:
                return bag[0]
        return None

    def put_well_known_blob(self, well_known_name, blob):
        bag = obnamlib.Bag()
        bag.set_id(well_known_name)
        bag.append(blob)
        self._bag_store.put_bag(bag)

    def flush(self):
        if self._bag is not None:
            self._bag_store.put_bag(self._bag)
            self._bag = None


class BlobCache(object):

    def __init__(self):
        self._max_bytes = None
        self._clear()

    def _clear(self):
        self._cache = {}
        self._cache_size = 0

    def set_max_bytes(self, max_bytes):
        self._max_bytes = max_bytes

    def put(self, blob_id, blob):
        if self._cache_size + len(blob) > self._max_bytes:  # pragma: no cover
            self._clear()
        self._cache[blob_id] = blob
        self._cache_size += len(blob)

    def get(self, blob_id):
        return self._cache[blob_id]

    def __contains__(self, blob_id):
        return blob_id in self._cache