Back to index

obnam  1.1
chunklist.py
Go to the documentation of this file.
00001 # Copyright 2010  Lars Wirzenius
00002 # 
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import hashlib
00018 import struct
00019 import random
00020 import tracing
00021 
00022 import obnamlib
00023 
00024 
00025 class ChunkList(obnamlib.RepositoryTree):
00026 
00027     '''Repository's list of chunks.
00028     
00029     The list maps a chunk id to its checksum.
00030     
00031     The list is implemented as a B-tree, with the 64-bit chunk id as the
00032     key, and the checksum as the value.
00033     
00034     '''
00035 
00036     def __init__(self, fs, node_size, upload_queue_size, lru_size, hooks):
00037         tracing.trace('new ChunkList')
00038         self.key_bytes = len(self.key(0))
00039         obnamlib.RepositoryTree.__init__(self, fs, 'chunklist', self.key_bytes, 
00040                                          node_size, upload_queue_size, 
00041                                          lru_size, hooks)
00042         self.keep_just_one_tree = True
00043 
00044     def key(self, chunk_id):
00045         return struct.pack('!Q', chunk_id)
00046 
00047     def add(self, chunk_id, checksum):
00048         tracing.trace('chunk_id=%s', chunk_id)
00049         tracing.trace('checksum=%s', repr(checksum))
00050         self.start_changes()
00051         self.tree.insert(self.key(chunk_id), checksum)
00052         
00053     def get_checksum(self, chunk_id):
00054         if self.init_forest() and self.forest.trees:
00055             t = self.forest.trees[-1]
00056             return t.lookup(self.key(chunk_id))
00057         raise KeyError(chunk_id)
00058         
00059     def remove(self, chunk_id):
00060         tracing.trace('chunk_id=%s', chunk_id)
00061         self.start_changes()
00062         key = self.key(chunk_id)
00063         self.tree.remove_range(key, key)
00064