~verterok/+junk/tritcask-trunk

« back to all changes in this revision

Viewing changes to tritcask.py

  • Committer: Guillermo Gonzalez
  • Date: 2012-01-09 05:48:06 UTC
  • Revision ID: guillo.gonzo@gmail.com-20120109054806-e9b3q8854cx5jscc
backport fix for empty hint file and cleanup/organize code a bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
204
204
        """Return true if there is a hint file on-disk."""
205
205
        return os.path.exists(self.hint_filename)
206
206
 
 
207
    @property
 
208
    def hint_size(self):
 
209
        """Return the hint file size."""
 
210
        if self.has_hint:
 
211
            return os.stat(self.hint_filename).st_size
 
212
        return 0
 
213
 
207
214
    def exists(self):
208
215
        return os.path.exists(self.filename)
209
216
 
436
443
        """Create the instance."""
437
444
        self.path = path
438
445
        self.tempfile = None
439
 
        if os.path.exists(self.path):
440
 
            # if it's there open only for read
 
446
        if os.path.exists(self.path) and os.stat(self.path).st_size > 0:
 
447
            # if it's there and size > 0, open only for read
441
448
            self.fd = open(self.path, 'rb')
442
449
        else:
443
450
            # this is a new hint file, lets create it as a tempfile.
758
765
        # sort the files by name, in order to load from older -> newest
759
766
        for data_file in sorted(self._immutable.values(),
760
767
                                key=attrgetter('filename')):
761
 
            if data_file.has_hint:
762
 
                # load directly from the hint file.
 
768
            if data_file.has_hint and data_file.hint_size > 0:
 
769
                # load directly from the hint file, only if the size > 0
763
770
                self._load_from_hint(data_file)
764
771
            elif data_file.exists() and data_file.size > 0:
765
772
                self._load_from_data(data_file)
812
819
                if build_hint:
813
820
                    hint_entry = HintEntry.from_tritcask_entry(entry)
814
821
                    hint_idx[hint_entry.key] = hint_entry
815
 
        if build_hint:
 
822
        if build_hint and hint_idx:
 
823
            # only build the hint file if hint_idx contains data
816
824
            with data_file.get_hint_file() as hint_file:
817
825
                for key, hint_entry in hint_idx.iteritems():
818
826
                    hint_file.write(hint_entry)