Exemple #1
0
 def __setitem__(self, hash, value):
     with self.lock:
         value = InventoryItem(*value)
         try:
             makedirs(
                 path.join(self.baseDir, FilesystemInventory.objectDir,
                           hexlify(hash)))
         except OSError:
             pass
         try:
             with open(
                     path.join(self.baseDir, FilesystemInventory.objectDir,
                               hexlify(hash),
                               FilesystemInventory.metadataFilename),
                     'w') as f:
                 f.write("%s,%s,%s,%s," %
                         (value.type, value.stream, value.expires,
                          hexlify(value.tag)))
             with open(
                     path.join(self.baseDir, FilesystemInventory.objectDir,
                               hexlify(hash),
                               FilesystemInventory.dataFilename), 'w') as f:
                 f.write(value.payload)
         except IOError:
             raise KeyError
         try:
             self._inventory[value.stream][hash] = value
         except KeyError:
             self._inventory[value.stream] = {}
             self._inventory[value.stream][hash] = value
Exemple #2
0
 def _load(self):
     newInventory = {}
     for hashId in self.object_list():
         try:
             objectType, streamNumber, expiresTime, tag = self.getMetadata(
                 hashId)
             try:
                 newInventory[streamNumber][hashId] = InventoryItem(
                     objectType, streamNumber, None, expiresTime, tag)
             except KeyError:
                 newInventory[streamNumber] = {}
                 newInventory[streamNumber][hashId] = InventoryItem(
                     objectType, streamNumber, None, expiresTime, tag)
         except KeyError:
             print "error loading %s" % (hexlify(hashId))
     self._inventory = newInventory
Exemple #3
0
 def __getitem__(self, hash):
     with self.lock:
         if hash in self._inventory:
             return self._inventory[hash]
         rows = sqlQuery('SELECT objecttype, streamnumber, payload, expirestime, tag FROM inventory WHERE hash=?', hash)
         if not rows:
             raise KeyError(hash)
         return InventoryItem(*rows[0])
Exemple #4
0
 def by_type_and_tag(self, objectType, tag):
     with self.lock:
         values = [
             value for value in self._inventory.values()
             if value.type == objectType and value.tag == tag
         ]
         values += (InventoryItem(*value) for value in sqlQuery(
             'SELECT objecttype, streamnumber, payload, expirestime, tag FROM inventory WHERE objecttype=? AND tag=?',
             objectType, sqlite3.Binary(tag)))
         return values
Exemple #5
0
 def __getitem__(self, hash):
     for streamDict in self._inventory.values():
         try:
             retval = streamDict[hash]
         except KeyError:
             continue
         if retval.payload is None:
             retval = InventoryItem(retval.type, retval.stream,
                                    self.getData(hash), retval.expires,
                                    retval.tag)
         return retval
     raise KeyError(hash)
Exemple #6
0
 def by_type_and_tag(self, objectType, tag):
     retval = []
     for stream, streamDict in self._inventory:
         for hashId, item in streamDict:
             if item.type == objectType and item.tag == tag:
                 try:
                     if item.payload is None:
                         item.payload = self.getData(hashId)
                 except IOError:
                     continue
                 retval.append(
                     InventoryItem(item.type, item.stream, item.payload,
                                   item.expires, item.tag))
     return retval
Exemple #7
0
 def by_type_and_tag(self, objectType, tag=None):
     """
     Get all inventory items of certain *objectType*
     with *tag* if given.
     """
     query = [
         'SELECT objecttype, streamnumber, payload, expirestime, tag'
         ' FROM inventory WHERE objecttype=?', objectType
     ]
     if tag:
         query[0] += ' AND tag=?'
         query.append(sqlite3.Binary(tag))
     with self.lock:
         values = [
             value for value in self._inventory.values()
             if value.type == objectType and tag is None or value.tag == tag
         ] + [InventoryItem(*value) for value in sqlQuery(*query)]
         return values
Exemple #8
0
 def __setitem__(self, hash, value):
     with self.lock:
         value = InventoryItem(*value)
         self._inventory[hash] = value
         self._streams[value.stream].add(hash)
Exemple #9
0
 def __setitem__(self, hash_, value):
     with self.lock:
         value = InventoryItem(*value)
         self._inventory[hash_] = value
         self._objects[hash_] = value.stream