mirror of
https://github.com/borgbackup/borg.git
synced 2025-01-31 03:31:41 +00:00
Lazy transactions
This commit is contained in:
parent
f9bdd72848
commit
f56b6bf4cc
1 changed files with 15 additions and 5 deletions
|
@ -93,15 +93,18 @@ def _available_indices(self, reverse=False):
|
|||
names.sort(reverse=reverse)
|
||||
return names
|
||||
|
||||
def open_index(self, head):
|
||||
def open_index(self, head, read_only=False):
|
||||
if head is None:
|
||||
self.index = NSIndex.create(os.path.join(self.path, 'index.tmp'))
|
||||
self.segments = {}
|
||||
self.compact = set()
|
||||
else:
|
||||
shutil.copy(os.path.join(self.path, 'index.%d' % head),
|
||||
os.path.join(self.path, 'index.tmp'))
|
||||
self.index = NSIndex(os.path.join(self.path, 'index.tmp'))
|
||||
if read_only:
|
||||
self.index = NSIndex(os.path.join(self.path, 'index.%d') % head)
|
||||
else:
|
||||
shutil.copy(os.path.join(self.path, 'index.%d' % head),
|
||||
os.path.join(self.path, 'index.tmp'))
|
||||
self.index = NSIndex(os.path.join(self.path, 'index.tmp'))
|
||||
hints = read_msgpack(os.path.join(self.path, 'hints.%d' % head))
|
||||
if hints['version'] != 1:
|
||||
raise ValueError('Unknown hints file version: %d' % hints['version'])
|
||||
|
@ -185,10 +188,11 @@ def recover(self, path):
|
|||
def rollback(self):
|
||||
"""
|
||||
"""
|
||||
self._active_txn = False
|
||||
self.io = LoggedIO(self.path, self.max_segment_size, self.segments_per_dir)
|
||||
if self.io.head is not None and not os.path.exists(os.path.join(self.path, 'index.%d' % self.io.head)):
|
||||
self.recover(self.path)
|
||||
self.open_index(self.io.head)
|
||||
self.open_index(self.io.head, read_only=True)
|
||||
|
||||
@deferrable
|
||||
def get(self, id):
|
||||
|
@ -200,6 +204,9 @@ def get(self, id):
|
|||
|
||||
@deferrable
|
||||
def put(self, id, data):
|
||||
if not self._active_txn:
|
||||
self._active_txn = True
|
||||
self.open_index(self.io.head)
|
||||
try:
|
||||
segment, _ = self.index[id]
|
||||
self.segments[segment] -= 1
|
||||
|
@ -214,6 +221,9 @@ def put(self, id, data):
|
|||
|
||||
@deferrable
|
||||
def delete(self, id):
|
||||
if not self._active_txn:
|
||||
self._active_txn = True
|
||||
self.open_index(self.io.head)
|
||||
try:
|
||||
segment, offset = self.index.pop(id)
|
||||
self.segments[segment] -= 1
|
||||
|
|
Loading…
Reference in a new issue