~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml8.py

  • Committer: John Arbash Meinel
  • Date: 2008-12-10 17:38:20 UTC
  • mto: This revision was merged to the branch mainline in revision 3912.
  • Revision ID: john@arbash-meinel.com-20081210173820-sjxjc8ktpuhnl1g4
Use a FIFOCache instead of an LRUCache, and factor out elt.get

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    cache_utf8,
22
22
    errors,
23
23
    inventory,
24
 
    lru_cache,
 
24
    fifo_cache,
25
25
    revision as _mod_revision,
26
26
    trace,
27
27
    )
41
41
    ">":">",
42
42
    }
43
43
# A cache of InventoryEntry objects
44
 
_entry_cache = lru_cache.LRUCache(10*1024)
 
44
_entry_cache = fifo_cache.FIFOCache(10*1024)
45
45
 
46
46
 
47
47
def _ensure_utf8_re():
356
356
        for e in elt:
357
357
            ie = self._unpack_entry(e)
358
358
            inv.add(ie)
359
 
        if len(inv) > _entry_cache._max_cache:
360
 
            new_len = len(inv) * 1.2
361
 
            trace.note('Resizing inventory cache to %s', new_len)
362
 
            _entry_cache.resize(new_len)
363
359
        return inv
364
360
 
365
361
    def _unpack_entry(self, elt):
 
362
        get_cached = _get_utf8_or_ascii
 
363
        elt_get = elt.get
 
364
 
 
365
        file_id = elt_get('file_id')
 
366
        revision = elt_get('revision')
 
367
        # Check and see if we have already unpacked this exact entry
 
368
        key = (file_id, revision)
 
369
        try:
 
370
            # We copy it, because some operatations may mutate it
 
371
            return _entry_cache[key].copy()
 
372
        except KeyError:
 
373
            pass
 
374
 
366
375
        kind = elt.tag
367
376
        if not InventoryEntry.versionable_kind(kind):
368
377
            raise AssertionError('unsupported entry kind %s' % kind)
369
378
 
370
 
        get_cached = _get_utf8_or_ascii
371
 
 
372
 
        file_id = elt.get('file_id')
373
 
        revision = elt.get('revision')
374
 
        # Check and see if we have already unpacked this exact entry
375
 
        key = (file_id, revision)
376
 
        cached_ie = _entry_cache.get(key, None)
377
 
        if cached_ie is not None:
378
 
            # We copy it, because some operatations may mutate it
379
 
            return cached_ie.copy()
380
 
 
381
379
        file_id = get_cached(file_id)
382
380
        if revision is not None:
383
381
            revision = get_cached(revision)
384
 
        parent_id = elt.get('parent_id')
 
382
        parent_id = elt_get('parent_id')
385
383
        if parent_id is not None:
386
384
            parent_id = get_cached(parent_id)
387
385
 
388
386
        if kind == 'directory':
389
387
            ie = inventory.InventoryDirectory(file_id,
390
 
                                              elt.get('name'),
 
388
                                              elt_get('name'),
391
389
                                              parent_id)
392
390
        elif kind == 'file':
393
391
            ie = inventory.InventoryFile(file_id,
394
 
                                         elt.get('name'),
 
392
                                         elt_get('name'),
395
393
                                         parent_id)
396
 
            ie.text_sha1 = elt.get('text_sha1')
397
 
            if elt.get('executable') == 'yes':
 
394
            ie.text_sha1 = elt_get('text_sha1')
 
395
            if elt_get('executable') == 'yes':
398
396
                ie.executable = True
399
 
            v = elt.get('text_size')
 
397
            v = elt_get('text_size')
400
398
            ie.text_size = v and int(v)
401
399
        elif kind == 'symlink':
402
400
            ie = inventory.InventoryLink(file_id,
403
 
                                         elt.get('name'),
 
401
                                         elt_get('name'),
404
402
                                         parent_id)
405
 
            ie.symlink_target = elt.get('symlink_target')
 
403
            ie.symlink_target = elt_get('symlink_target')
406
404
        else:
407
405
            raise errors.UnsupportedInventoryKind(kind)
408
406
        ie.revision = revision