File: SectionUpdates.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (289 lines) | stat: -rw-r--r-- 10,014 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//------------------------------------------------------------------------------
// <copyright file="SectionUpdates.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration {
    using System.Collections;

    //
    // A collection of updates to the sections that have the same location attributes.
    //
    internal class SectionUpdates {
        private string      _name;          // name of this section, for debugging
        private Hashtable   _groups;        // group name -> SectionUpdates
        private Hashtable   _sections;      // section name -> Update
        private int         _cUnretrieved;  // number of items not retrieved in update algorithm.
        private int         _cMoved;        // number of items moved (new or location attributes changed).
        private Update      _sectionGroupUpdate;   // update for this group
        private bool        _isNew;         // is the entire section new (all sections and subgroups)?

        internal SectionUpdates(string name) {
            _name = name;
            _groups = new Hashtable();
            _sections = new Hashtable();
        }

        internal bool IsNew {
            get {return _isNew;}
            set {_isNew = value;}
        }

        internal bool IsEmpty {
            get {
                return _groups.Count == 0 && _sections.Count == 0;
            }
        }

        //
        // Find the SectionUpdates for a configKey, and create it if it does not exist.
        //
        private SectionUpdates FindSectionUpdates(string configKey, bool isGroup) {
            string group, dummy;

            if (isGroup) {
                group = configKey;
            }
            else {
                BaseConfigurationRecord.SplitConfigKey(configKey, out group, out dummy);
            }

            Debug.Assert(String.IsNullOrEmpty(_name), "FindSectionUpdates assumes search is from root record");
            SectionUpdates sectionUpdates = this;
            if (group.Length != 0) {
                // find the SectionUpdates for the group
                string [] groups = group.Split(BaseConfigurationRecord.ConfigPathSeparatorParams);
                foreach (string groupPart in groups) {
                    SectionUpdates sectionUpdatesChild = (SectionUpdates) sectionUpdates._groups[groupPart];
                    if (sectionUpdatesChild == null) {
                        sectionUpdatesChild = new SectionUpdates(groupPart);
                        sectionUpdates._groups[groupPart] = sectionUpdatesChild;
                    }

                    sectionUpdates = sectionUpdatesChild;
                }
            }

            return sectionUpdates;
        }

        //
        // Recursively check whether this group has all new updates.
        // An update is new if all sections are new and all subgroups are new.
        //
        internal void CompleteUpdates() {
            bool allSubgroupsAreNew = true;

            // call CompleteUpdates() for all children
            foreach (SectionUpdates sectionUpdates in _groups.Values) {
                sectionUpdates.CompleteUpdates();
                if (!sectionUpdates.IsNew) {
                    allSubgroupsAreNew = false;
                }
            }
            
            _isNew = allSubgroupsAreNew && _cMoved == _sections.Count;
        }

        //
        // Add one update.
        //
        internal void AddSection(Update update) {
            SectionUpdates sectionUpdates = FindSectionUpdates(update.ConfigKey, false);
            sectionUpdates._sections.Add(update.ConfigKey, update);

            // Maintain counts.
            sectionUpdates._cUnretrieved++;
            if (update.Moved) {
                sectionUpdates._cMoved++;
            }
        }

        //
        // Add a section group update.
        //
        internal void AddSectionGroup(Update update) {
            SectionUpdates sectionUpdates = FindSectionUpdates(update.ConfigKey, true);
            sectionUpdates._sectionGroupUpdate = update;
        }

        //
        // Retrieve an update if it has not yet been retrieved.
        //
        private Update GetUpdate(string configKey) {
            Update update = (Update) _sections[configKey];
            if (update != null) {
                if (update.Retrieved) {
                    update = null;
                }
                else {
                    update.Retrieved = true;
                    _cUnretrieved--;
                    if (update.Moved) {
                        _cMoved--;
                    }
                }
            }

            return update;
        }

        //
        // Get the update for a section group.
        //
        internal DeclarationUpdate GetSectionGroupUpdate() {
            if (_sectionGroupUpdate != null && !_sectionGroupUpdate.Retrieved) {
                _sectionGroupUpdate.Retrieved = true;
                return (DeclarationUpdate) _sectionGroupUpdate;
            }

            return null;
        }

        internal DefinitionUpdate GetDefinitionUpdate(string configKey) {
            return (DefinitionUpdate) GetUpdate(configKey);
        }

        internal DeclarationUpdate GetDeclarationUpdate(string configKey) {
            return (DeclarationUpdate) GetUpdate(configKey);
        }

        internal SectionUpdates GetSectionUpdatesForGroup(string group) {
            return (SectionUpdates) _groups[group];
        }

        //
        // Return true if this section group or any of its children have unretrieved sections.
        //
        internal bool HasUnretrievedSections() {
            if (_cUnretrieved > 0 || (_sectionGroupUpdate != null && !_sectionGroupUpdate.Retrieved)) {
                return true;
            }

            foreach (SectionUpdates sectionUpdates in _groups.Values) {
                if (sectionUpdates.HasUnretrievedSections()) {
                    return true;
                }
            }

            return false;
        }
        internal void MarkAsRetrieved()
        {
            _cUnretrieved = 0;
            foreach (SectionUpdates sectionUpdates in _groups.Values) {
                sectionUpdates.MarkAsRetrieved();
            }
            if (_sectionGroupUpdate != null){
                _sectionGroupUpdate.Retrieved = true;
            }
        }

        internal void MarkGroupAsRetrieved(string groupName)
        {
            SectionUpdates sectionUpdates = _groups[groupName] as SectionUpdates;
            if (sectionUpdates != null) {
                sectionUpdates.MarkAsRetrieved();
            }
        }
        //
        // Return true if this section group contains any new section groups, false otherwise.
        //
        internal bool HasNewSectionGroups() 
        {
            foreach (SectionUpdates sectionUpdates in _groups.Values) {
                if (sectionUpdates.IsNew)
                    return true;
            }

            return false;
        }

        //
        // Return a sorted list of the names of unretrieved sections in this group.
        //
        internal string[] GetUnretrievedSectionNames() {
            if (_cUnretrieved == 0)
                return null;

            string[] sectionNames = new string[_cUnretrieved];
            int i = 0;
            foreach (Update update in _sections.Values) {
                if (!update.Retrieved) {
                    sectionNames[i] = update.ConfigKey;
                    i++;
                }
            }

            Array.Sort(sectionNames);
            return sectionNames;
        }

        //
        // Return a sorted list of the names of moved and unretrieved sections in this group.
        //
        internal string[] GetMovedSectionNames() {
            if (_cMoved == 0)
                return null;

            string[] sectionNames = new string[_cMoved];
            int i = 0;
            foreach (Update update in _sections.Values) {
                if (update.Moved && !update.Retrieved) {
                    sectionNames[i] = update.ConfigKey;
                    i++;
                }
            }

            Array.Sort(sectionNames);
            return sectionNames;
        }

        //
        // Return a sorted list of the names of groups with unretrieved sections.
        //
        internal string[] GetUnretrievedGroupNames() {
            ArrayList unretrievedGroups = new ArrayList();

            foreach (DictionaryEntry de in _groups) {
                string group = (string) de.Key;
                SectionUpdates sectionUpdates = (SectionUpdates) de.Value;
                if (sectionUpdates.HasUnretrievedSections()) {
                    unretrievedGroups.Add(group);
                }
            }

            if (unretrievedGroups.Count == 0)
                return null;

            string[] groupNames = new string[unretrievedGroups.Count];
            unretrievedGroups.CopyTo(groupNames);
            Array.Sort(groupNames);
            return groupNames;
        }

        //
        // Return a sorted list of the names of new section groups with unretrieved sections.
        //
        internal string[] GetNewGroupNames() {
            ArrayList newsGroups = new ArrayList();

            foreach (DictionaryEntry de in _groups) {
                string group = (string) de.Key;
                SectionUpdates sectionUpdates = (SectionUpdates) de.Value;
                if (sectionUpdates.IsNew && sectionUpdates.HasUnretrievedSections()) {
                    newsGroups.Add(group);
                }
            }

            if (newsGroups.Count == 0)
                return null;

            string[] groupNames = new string[newsGroups.Count];
            newsGroups.CopyTo(groupNames);
            Array.Sort(groupNames);
            return groupNames;
        }
    }
}