add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / index / StoredFieldsWriter.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import org.apache.lucene.store.RAMOutputStream;
22 import org.apache.lucene.util.ArrayUtil;
23 import org.apache.lucene.util.RamUsageEstimator;
24
25 /** This is a DocFieldConsumer that writes stored fields. */
26 final class StoredFieldsWriter {
27
28   FieldsWriter fieldsWriter;
29   final DocumentsWriter docWriter;
30   final FieldInfos fieldInfos;
31   int lastDocID;
32
33   PerDoc[] docFreeList = new PerDoc[1];
34   int freeCount;
35
36   public StoredFieldsWriter(DocumentsWriter docWriter, FieldInfos fieldInfos) {
37     this.docWriter = docWriter;
38     this.fieldInfos = fieldInfos;
39   }
40
41   public StoredFieldsWriterPerThread addThread(DocumentsWriter.DocState docState) throws IOException {
42     return new StoredFieldsWriterPerThread(docState, this);
43   }
44
45   synchronized public void flush(SegmentWriteState state) throws IOException {
46     if (state.numDocs > lastDocID) {
47       initFieldsWriter();
48       fill(state.numDocs);
49     }
50
51     if (fieldsWriter != null) {
52       fieldsWriter.close();
53       fieldsWriter = null;
54       lastDocID = 0;
55
56       String fieldsIdxName = IndexFileNames.segmentFileName(state.segmentName, IndexFileNames.FIELDS_INDEX_EXTENSION);
57       if (4 + ((long) state.numDocs) * 8 != state.directory.fileLength(fieldsIdxName)) {
58         throw new RuntimeException("after flush: fdx size mismatch: " + state.numDocs + " docs vs " + state.directory.fileLength(fieldsIdxName) + " length in bytes of " + fieldsIdxName + " file exists?=" + state.directory.fileExists(fieldsIdxName));
59       }
60     }
61   }
62
63   private synchronized void initFieldsWriter() throws IOException {
64     if (fieldsWriter == null) {
65       fieldsWriter = new FieldsWriter(docWriter.directory, docWriter.getSegment(), fieldInfos);
66       lastDocID = 0;
67     }
68   }
69
70   int allocCount;
71
72   synchronized PerDoc getPerDoc() {
73     if (freeCount == 0) {
74       allocCount++;
75       if (allocCount > docFreeList.length) {
76         // Grow our free list up front to make sure we have
77         // enough space to recycle all outstanding PerDoc
78         // instances
79         assert allocCount == 1+docFreeList.length;
80         docFreeList = new PerDoc[ArrayUtil.oversize(allocCount, RamUsageEstimator.NUM_BYTES_OBJECT_REF)];
81       }
82       return new PerDoc();
83     } else {
84       return docFreeList[--freeCount];
85     }
86   }
87
88   synchronized void abort() {
89     if (fieldsWriter != null) {
90       fieldsWriter.abort();
91       fieldsWriter = null;
92       lastDocID = 0;
93     }
94   }
95
96   /** Fills in any hole in the docIDs */
97   void fill(int docID) throws IOException {
98     // We must "catch up" for all docs before us
99     // that had no stored fields:
100     while(lastDocID < docID) {
101       fieldsWriter.skipDocument();
102       lastDocID++;
103     }
104   }
105
106   synchronized void finishDocument(PerDoc perDoc) throws IOException {
107     assert docWriter.writer.testPoint("StoredFieldsWriter.finishDocument start");
108     initFieldsWriter();
109
110     fill(perDoc.docID);
111
112     // Append stored fields to the real FieldsWriter:
113     fieldsWriter.flushDocument(perDoc.numStoredFields, perDoc.fdt);
114     lastDocID++;
115     perDoc.reset();
116     free(perDoc);
117     assert docWriter.writer.testPoint("StoredFieldsWriter.finishDocument end");
118   }
119
120   synchronized void free(PerDoc perDoc) {
121     assert freeCount < docFreeList.length;
122     assert 0 == perDoc.numStoredFields;
123     assert 0 == perDoc.fdt.length();
124     assert 0 == perDoc.fdt.getFilePointer();
125     docFreeList[freeCount++] = perDoc;
126   }
127
128   class PerDoc extends DocumentsWriter.DocWriter {
129     final DocumentsWriter.PerDocBuffer buffer = docWriter.newPerDocBuffer();
130     RAMOutputStream fdt = new RAMOutputStream(buffer);
131     int numStoredFields;
132
133     void reset() {
134       fdt.reset();
135       buffer.recycle();
136       numStoredFields = 0;
137     }
138
139     @Override
140     void abort() {
141       reset();
142       free(this);
143     }
144
145     @Override
146     public long sizeInBytes() {
147       return buffer.getSizeInBytes();
148     }
149
150     @Override
151     public void finish() throws IOException {
152       finishDocument(this);
153     }
154   }
155 }