19 using System.Collections.Generic;
22 namespace Lucene.Net.Index
38 internal int bytesPerPosting;
39 internal int postingsFreeChunk;
42 private int postingsFreeCount;
43 private int postingsAllocCount;
44 internal bool trackAllocations;
48 this.docWriter = docWriter;
49 this.consumer = consumer;
50 this.nextTermsHash = nextTermsHash;
51 this.trackAllocations = trackAllocations;
58 bytesPerPosting = consumer.BytesPerPosting() + 4 *
DocumentsWriter.POINTER_NUM_BYTE;
59 postingsFreeChunk = (int) (
DocumentsWriter.BYTE_BLOCK_SIZE / bytesPerPosting);
67 internal TermsHashPerThread AddThread(DocInverterPerThread docInverterPerThread, TermsHashPerThread primaryPerThread)
69 return new TermsHashPerThread(docInverterPerThread,
this, nextTermsHash, primaryPerThread);
72 internal override void SetFieldInfos(FieldInfos fieldInfos)
74 this.fieldInfos = fieldInfos;
75 consumer.SetFieldInfos(fieldInfos);
81 public override void Abort()
84 if (nextTermsHash != null)
85 nextTermsHash.Abort();
91 System.Diagnostics.Debug.Assert(postingsFreeCount == postingsAllocCount,
"Thread.currentThread().getName()" +
": postingsFreeCount=" + postingsFreeCount +
" postingsAllocCount=" + postingsAllocCount +
" consumer=" + consumer);
94 if (newSize != postingsFreeList.Length)
96 if (postingsFreeCount > newSize)
100 docWriter.BytesAllocated(-(postingsFreeCount - newSize) * bytesPerPosting);
102 postingsFreeCount = newSize;
103 postingsAllocCount = newSize;
106 RawPostingList[] newArray =
new RawPostingList[newSize];
107 Array.Copy(postingsFreeList, 0, newArray, 0, postingsFreeCount);
108 postingsFreeList = newArray;
112 internal override void CloseDocStore(SegmentWriteState state)
116 consumer.CloseDocStore(state);
117 if (nextTermsHash != null)
118 nextTermsHash.CloseDocStore(state);
122 internal override void Flush(IDictionary<InvertedDocConsumerPerThread, ICollection<InvertedDocConsumerPerField>> threadsAndFields, SegmentWriteState state)
126 var childThreadsAndFields =
new Dictionary<TermsHashConsumerPerThread, ICollection<TermsHashConsumerPerField>>();
127 Dictionary<InvertedDocConsumerPerThread, ICollection<InvertedDocConsumerPerField>> nextThreadsAndFields;
129 if (nextTermsHash != null)
131 nextThreadsAndFields =
new Dictionary<InvertedDocConsumerPerThread, ICollection<InvertedDocConsumerPerField>>();
134 nextThreadsAndFields = null;
136 foreach (var entry
in threadsAndFields)
138 TermsHashPerThread perThread = (TermsHashPerThread) entry.Key;
140 ICollection<InvertedDocConsumerPerField> fields = entry.Value;
142 var fieldsIt = fields.GetEnumerator();
143 ICollection<TermsHashConsumerPerField> childFields =
new HashSet<TermsHashConsumerPerField>();
144 ICollection<InvertedDocConsumerPerField> nextChildFields;
146 if (nextTermsHash != null)
148 nextChildFields =
new HashSet<InvertedDocConsumerPerField>();
151 nextChildFields = null;
153 while (fieldsIt.MoveNext())
155 TermsHashPerField perField = (TermsHashPerField) fieldsIt.Current;
156 childFields.Add(perField.consumer);
157 if (nextTermsHash != null)
158 nextChildFields.Add(perField.nextPerField);
161 childThreadsAndFields[perThread.consumer] = childFields;
162 if (nextTermsHash != null)
163 nextThreadsAndFields[perThread.nextPerThread] = nextChildFields;
166 consumer.Flush(childThreadsAndFields, state);
168 ShrinkFreePostings(threadsAndFields, state);
170 if (nextTermsHash != null)
171 nextTermsHash.Flush(nextThreadsAndFields, state);
175 public override bool FreeRAM()
177 if (!trackAllocations)
185 if (postingsFreeCount >= postingsFreeChunk)
186 numToFree = postingsFreeChunk;
188 numToFree = postingsFreeCount;
192 for (
int i = postingsFreeCount - numToFree; i < postingsFreeCount; i++)
194 postingsFreeList[i] = null;
197 postingsFreeCount -= numToFree;
198 postingsAllocCount -= numToFree;
199 bytesFreed = -numToFree * bytesPerPosting;
206 docWriter.BytesAllocated(bytesFreed);
209 if (nextTermsHash != null)
210 any |= nextTermsHash.FreeRAM();
220 System.Diagnostics.Debug.Assert(postings.Length >= numPostings);
225 System.Diagnostics.Debug.Assert(postingsFreeCount + numPostings <= postingsFreeList.Length);
226 Array.Copy(postings, 0, postingsFreeList, postingsFreeCount, numPostings);
227 postingsFreeCount += numPostings;
236 System.Diagnostics.Debug.Assert(docWriter.writer.TestPoint(
"TermsHash.getPostings start"));
238 System.Diagnostics.Debug.Assert(postingsFreeCount <= postingsFreeList.Length);
239 System.Diagnostics.Debug.Assert(postingsFreeCount <= postingsAllocCount,
"postingsFreeCount=" + postingsFreeCount +
" postingsAllocCount=" + postingsAllocCount);
242 if (postingsFreeCount < postings.Length)
243 numToCopy = postingsFreeCount;
245 numToCopy = postings.Length;
246 int start = postingsFreeCount - numToCopy;
247 System.Diagnostics.Debug.Assert(start >= 0);
248 System.Diagnostics.Debug.Assert(start + numToCopy <= postingsFreeList.Length);
249 System.Diagnostics.Debug.Assert(numToCopy <= postings.Length);
250 Array.Copy(postingsFreeList, start, postings, 0, numToCopy);
253 if (numToCopy != postings.Length)
255 int extra = postings.Length - numToCopy;
256 int newPostingsAllocCount = postingsAllocCount + extra;
258 consumer.CreatePostings(postings, numToCopy, extra);
259 System.Diagnostics.Debug.Assert(docWriter.writer.TestPoint(
"TermsHash.getPostings after create"));
260 postingsAllocCount += extra;
262 if (trackAllocations)
263 docWriter.BytesAllocated(extra * bytesPerPosting);
265 if (newPostingsAllocCount > postingsFreeList.Length)
271 postingsFreeCount -= numToCopy;
273 if (trackAllocations)
274 docWriter.BytesUsed(postings.Length * bytesPerPosting);