~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: Robert Collins
  • Date: 2008-04-08 21:41:15 UTC
  • mto: This revision was merged to the branch mainline in revision 3350.
  • Revision ID: robertc@robertcollins.net-20080408214115-qiz6rr5kyt5mxsyi
 * ``VersionedFile.clear_cache`` and ``enable_cache`` are deprecated.
   These methods added significant complexity to the ``VersionedFile``
   implementation, but were only used for optimising fetches from knits - 
   which can be done from outside the knit layer, or via a caching
   decorator. As knits are not the default format, the complexity is no
   longer worth paying. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
623
623
        for (version_id, options, parents, size), access_memo in zip(
624
624
            records, positions):
625
625
            index_entries.append((version_id, options, access_memo, parents))
626
 
            if self._data._do_cache:
627
 
                self._data._cache[version_id] = data[offset:offset+size]
628
626
            offset += size
629
627
        self._index.add_versions(index_entries)
630
628
 
631
 
    def enable_cache(self):
632
 
        """Start caching data for this knit"""
633
 
        self._data.enable_cache()
634
 
 
635
 
    def clear_cache(self):
636
 
        """Clear the data cache only."""
637
 
        self._data.clear_cache()
638
 
 
639
629
    def copy_to(self, name, transport):
640
630
        """See VersionedFile.copy_to()."""
641
631
        # copy the current index to a temp index to avoid racing with local
2379
2369
        """
2380
2370
        self._access = access
2381
2371
        self._checked = False
2382
 
        # TODO: jam 20060713 conceptually, this could spill to disk
2383
 
        #       if the cached size gets larger than a certain amount
2384
 
        #       but it complicates the model a bit, so for now just use
2385
 
        #       a simple dictionary
2386
 
        self._cache = {}
2387
 
        self._do_cache = False
2388
 
 
2389
 
    def enable_cache(self):
2390
 
        """Enable caching of reads."""
2391
 
        self._do_cache = True
2392
 
 
2393
 
    def clear_cache(self):
2394
 
        """Clear the record cache."""
2395
 
        self._do_cache = False
2396
 
        self._cache = {}
2397
2372
 
2398
2373
    def _open_file(self):
2399
2374
        return self._access.open_file()
2499
2474
        # uses readv so nice and fast we hope.
2500
2475
        if len(records):
2501
2476
            # grab the disk data needed.
2502
 
            if self._cache:
2503
 
                # Don't check _cache if it is empty
2504
 
                needed_offsets = [index_memo for version_id, index_memo
2505
 
                                              in records
2506
 
                                              if version_id not in self._cache]
2507
 
            else:
2508
 
                needed_offsets = [index_memo for version_id, index_memo
2509
 
                                               in records]
2510
 
 
 
2477
            needed_offsets = [index_memo for version_id, index_memo
 
2478
                                           in records]
2511
2479
            raw_records = self._access.get_raw_records(needed_offsets)
2512
2480
 
2513
2481
        for version_id, index_memo in records:
2514
 
            if version_id in self._cache:
2515
 
                # This data has already been validated
2516
 
                data = self._cache[version_id]
2517
 
            else:
2518
 
                data = raw_records.next()
2519
 
                if self._do_cache:
2520
 
                    self._cache[version_id] = data
2521
 
 
2522
 
                # validate the header
2523
 
                df, rec = self._parse_record_header(version_id, data)
2524
 
                df.close()
 
2482
            data = raw_records.next()
 
2483
            # validate the header
 
2484
            df, rec = self._parse_record_header(version_id, data)
 
2485
            df.close()
2525
2486
            yield version_id, data
2526
2487
 
2527
2488
    def read_records_iter(self, records):
2537
2498
        if not records:
2538
2499
            return
2539
2500
 
2540
 
        if self._cache:
2541
 
            # Skip records we have alread seen
2542
 
            yielded_records = set()
2543
 
            needed_records = set()
2544
 
            for record in records:
2545
 
                if record[0] in self._cache:
2546
 
                    if record[0] in yielded_records:
2547
 
                        continue
2548
 
                    yielded_records.add(record[0])
2549
 
                    data = self._cache[record[0]]
2550
 
                    content, digest = self._parse_record(record[0], data)
2551
 
                    yield (record[0], content, digest)
2552
 
                else:
2553
 
                    needed_records.add(record)
2554
 
            needed_records = sorted(needed_records, key=operator.itemgetter(1))
2555
 
        else:
2556
 
            needed_records = sorted(set(records), key=operator.itemgetter(1))
2557
 
 
 
2501
        needed_records = sorted(set(records), key=operator.itemgetter(1))
2558
2502
        if not needed_records:
2559
2503
            return
2560
2504
 
2566
2510
        for (version_id, index_memo), data in \
2567
2511
                izip(iter(needed_records), raw_data):
2568
2512
            content, digest = self._parse_record(version_id, data)
2569
 
            if self._do_cache:
2570
 
                self._cache[version_id] = data
2571
2513
            yield version_id, content, digest
2572
2514
 
2573
2515
    def read_records(self, records):