Revision 5a96180b pithos/backends/modular.py

b/pithos/backends/modular.py
35 35
import os
36 36
import time
37 37
import logging
38
import hashlib
39 38
import binascii
40 39

  
41 40
from base import NotAllowedError, QuotaError, BaseBackend
42 41

  
42
from pithos.lib.hashmap import HashMap
43

  
43 44
( CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED ) = range(3)
44 45

  
45 46
inf = float('inf')
......
50 51
logger = logging.getLogger(__name__)
51 52

  
52 53

  
53
class HashMap(list):
54
    
55
    def __init__(self, blocksize, blockhash):
56
        super(HashMap, self).__init__()
57
        self.blocksize = blocksize
58
        self.blockhash = blockhash
59
    
60
    def _hash_raw(self, v):
61
        h = hashlib.new(self.blockhash)
62
        h.update(v)
63
        return h.digest()
64
    
65
    def hash(self):
66
        if len(self) == 0:
67
            return self._hash_raw('')
68
        if len(self) == 1:
69
            return self.__getitem__(0)
70
        
71
        h = list(self)
72
        s = 2
73
        while s < len(h):
74
            s = s * 2
75
        h += [('\x00' * len(h[0]))] * (s - len(h))
76
        while len(h) > 1:
77
            h = [self._hash_raw(h[x] + h[x + 1]) for x in range(0, len(h), 2)]
78
        return h[0]
79

  
80

  
81 54
def backend_method(func=None, autocommit=1):
82 55
    if func is None:
83 56
        def fn(func):

Also available in: Unified diff