~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Patch Queue Manager
  • Date: 2012-03-28 16:13:49 UTC
  • mfrom: (6499.2.3 948339-config-caching)
  • Revision ID: pqm@pqm.ubuntu.com-20120328161349-2gsc0g11fcu43hlc
(vila) Properly share mutable config sections and save the branch config
 only during the final unlock (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2956
2956
            # Report concurrent updates in an ad-hoc way. This should only
2957
2957
            # occurs when different processes try to update the same option
2958
2958
            # which is not supported (as in: the config framework is not meant
2959
 
            # to be used a sharing mechanism).
 
2959
            # to be used as a sharing mechanism).
2960
2960
            if expected != reloaded:
2961
2961
                if actual is _DeletedOption:
2962
2962
                    actual = '<DELETED>'
2982
2982
    mutable_section_class = MutableSection
2983
2983
 
2984
2984
    def __init__(self):
2985
 
        # Which sections need to be saved
2986
 
        self.dirty_sections = []
 
2985
        # Which sections need to be saved (by section id). We use a dict here
 
2986
        # so the dirty sections can be shared by multiple callers.
 
2987
        self.dirty_sections = {}
2987
2988
 
2988
2989
    def is_loaded(self):
2989
2990
        """Returns True if the Store has been loaded.
3032
3033
        raise NotImplementedError(self.save)
3033
3034
 
3034
3035
    def _need_saving(self):
3035
 
        for s in self.dirty_sections:
 
3036
        for s in self.dirty_sections.values():
3036
3037
            if s.orig:
3037
3038
                # At least one dirty section contains a modification
3038
3039
                return True
3052
3053
        # get_mutable_section() call below.
3053
3054
        self.unload()
3054
3055
        # Apply the changes from the preserved dirty sections
3055
 
        for dirty in dirty_sections:
3056
 
            clean = self.get_mutable_section(dirty.id)
 
3056
        for section_id, dirty in dirty_sections.iteritems():
 
3057
            clean = self.get_mutable_section(section_id)
3057
3058
            clean.apply_changes(dirty, self)
3058
3059
        # Everything is clean now
3059
 
        self.dirty_sections = []
 
3060
        self.dirty_sections = {}
3060
3061
 
3061
3062
    def save_changes(self):
3062
3063
        """Saves the Store to persistent storage if changes occurred.
3142
3143
 
3143
3144
    def unload(self):
3144
3145
        self._config_obj = None
3145
 
        self.dirty_sections = []
 
3146
        self.dirty_sections = {}
3146
3147
 
3147
3148
    def _load_content(self):
3148
3149
        """Load the config file bytes.
3196
3197
        if not self._need_saving():
3197
3198
            return
3198
3199
        # Preserve the current version
3199
 
        current = self._config_obj
3200
 
        dirty_sections = list(self.dirty_sections)
 
3200
        dirty_sections = dict(self.dirty_sections.items())
3201
3201
        self.apply_changes(dirty_sections)
3202
3202
        # Save to the persistent storage
3203
3203
        self.save()
3238
3238
        except errors.NoSuchFile:
3239
3239
            # The file doesn't exist, let's pretend it was empty
3240
3240
            self._load_from_string('')
 
3241
        if section_id in self.dirty_sections:
 
3242
            # We already created a mutable section for this id
 
3243
            return self.dirty_sections[section_id]
3241
3244
        if section_id is None:
3242
3245
            section = self._config_obj
3243
3246
        else:
3244
3247
            section = self._config_obj.setdefault(section_id, {})
3245
3248
        mutable_section = self.mutable_section_class(section_id, section)
3246
3249
        # All mutable sections can become dirty
3247
 
        self.dirty_sections.append(mutable_section)
 
3250
        self.dirty_sections[section_id] = mutable_section
3248
3251
        return mutable_section
3249
3252
 
3250
3253
    def quote(self, value):