Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
TermsHashPerThread.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Index
21 {
22 
24  {
25 
26  internal TermsHash termsHash;
27  internal TermsHashConsumerPerThread consumer;
28  internal TermsHashPerThread nextPerThread;
29 
30  internal CharBlockPool charPool;
31  internal IntBlockPool intPool;
32  internal ByteBlockPool bytePool;
33  internal bool primary;
34  internal DocumentsWriter.DocState docState;
35 
36  internal RawPostingList[] freePostings = new RawPostingList[256];
37  internal int freePostingsCount;
38 
39  public TermsHashPerThread(DocInverterPerThread docInverterPerThread, TermsHash termsHash, TermsHash nextTermsHash, TermsHashPerThread primaryPerThread)
40  {
41  docState = docInverterPerThread.docState;
42 
43  this.termsHash = termsHash;
44  this.consumer = termsHash.consumer.AddThread(this);
45 
46  if (nextTermsHash != null)
47  {
48  // We are primary
49  charPool = new CharBlockPool(termsHash.docWriter);
50  primary = true;
51  }
52  else
53  {
54  charPool = primaryPerThread.charPool;
55  primary = false;
56  }
57 
58  intPool = new IntBlockPool(termsHash.docWriter, termsHash.trackAllocations);
59  bytePool = new ByteBlockPool(termsHash.docWriter.byteBlockAllocator, termsHash.trackAllocations);
60 
61  if (nextTermsHash != null)
62  nextPerThread = nextTermsHash.AddThread(docInverterPerThread, this);
63  else
64  nextPerThread = null;
65  }
66 
67  internal override InvertedDocConsumerPerField AddField(DocInverterPerField docInverterPerField, FieldInfo fieldInfo)
68  {
69  return new TermsHashPerField(docInverterPerField, this, nextPerThread, fieldInfo);
70  }
71 
72  public override void Abort()
73  {
74  lock (this)
75  {
76  Reset(true);
77  consumer.Abort();
78  if (nextPerThread != null)
79  nextPerThread.Abort();
80  }
81  }
82 
83  // perField calls this when it needs more postings:
84  internal void MorePostings()
85  {
86  System.Diagnostics.Debug.Assert(freePostingsCount == 0);
87  termsHash.GetPostings(freePostings);
88  freePostingsCount = freePostings.Length;
89  System.Diagnostics.Debug.Assert(noNullPostings(freePostings, freePostingsCount, "consumer=" + consumer));
90  }
91 
92  private static bool noNullPostings(RawPostingList[] postings, int count, System.String details)
93  {
94  for (int i = 0; i < count; i++)
95  System.Diagnostics.Debug.Assert(postings[i] != null, "postings[" + i + "] of " + count + " is null: " + details);
96  return true;
97  }
98 
99  public override void StartDocument()
100  {
101  consumer.StartDocument();
102  if (nextPerThread != null)
103  nextPerThread.consumer.StartDocument();
104  }
105 
106  public override DocumentsWriter.DocWriter FinishDocument()
107  {
108  DocumentsWriter.DocWriter doc = consumer.FinishDocument();
109 
110  DocumentsWriter.DocWriter doc2;
111  if (nextPerThread != null)
112  doc2 = nextPerThread.consumer.FinishDocument();
113  else
114  doc2 = null;
115  if (doc == null)
116  return doc2;
117  else
118  {
119  doc.SetNext(doc2);
120  return doc;
121  }
122  }
123 
124  // Clear all state
125  internal void Reset(bool recyclePostings)
126  {
127  intPool.Reset();
128  bytePool.Reset();
129 
130  if (primary)
131  charPool.Reset();
132 
133  if (recyclePostings)
134  {
135  termsHash.RecyclePostings(freePostings, freePostingsCount);
136  freePostingsCount = 0;
137  }
138  }
139  }
140 }