pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / SegmentNorms.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 java.util.concurrent.atomic.AtomicInteger;
22
23 import org.apache.lucene.store.IndexInput;
24 import org.apache.lucene.store.IndexOutput;
25
26 /**
27  * Byte[] referencing is used because a new norm object needs 
28  * to be created for each clone, and the byte array is all 
29  * that is needed for sharing between cloned readers.  The 
30  * current norm referencing is for sharing between readers 
31  * whereas the byte[] referencing is for copy on write which 
32  * is independent of reader references (i.e. incRef, decRef).
33  */
34
35 final class SegmentNorms implements Cloneable {
36
37   /** norms header placeholder */
38   static final byte[] NORMS_HEADER = new byte[]{'N','R','M',-1};
39
40   int refCount = 1;
41
42   // If this instance is a clone, the originalNorm
43   // references the Norm that has a real open IndexInput:
44   private SegmentNorms origNorm;
45
46   private IndexInput in;
47   private long normSeek;
48
49   // null until bytes is set
50   private AtomicInteger bytesRef;
51   private byte[] bytes;
52   private int number;
53
54   boolean dirty;
55   boolean rollbackDirty;
56   
57   private final SegmentReader owner;
58   
59   public SegmentNorms(IndexInput in, int number, long normSeek, SegmentReader owner) {
60     this.in = in;
61     this.number = number;
62     this.normSeek = normSeek;
63     this.owner = owner;
64   }
65
66   public synchronized void incRef() {
67     assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
68     refCount++;
69   }
70
71   private void closeInput() throws IOException {
72     if (in != null) {
73       if (in != owner.singleNormStream) {
74         // It's private to us -- just close it
75         in.close();
76       } else {
77         // We are sharing this with others -- decRef and
78         // maybe close the shared norm stream
79         if (owner.singleNormRef.decrementAndGet() == 0) {
80           owner.singleNormStream.close();
81           owner.singleNormStream = null;
82         }
83       }
84
85       in = null;
86     }
87   }
88
89   public synchronized void decRef() throws IOException {
90     assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
91
92     if (--refCount == 0) {
93       if (origNorm != null) {
94         origNorm.decRef();
95         origNorm = null;
96       } else {
97         closeInput();
98       }
99
100       if (bytes != null) {
101         assert bytesRef != null;
102         bytesRef.decrementAndGet();
103         bytes = null;
104         bytesRef = null;
105       } else {
106         assert bytesRef == null;
107       }
108     }
109   }
110
111   // Load bytes but do not cache them if they were not
112   // already cached
113   public synchronized void bytes(byte[] bytesOut, int offset, int len) throws IOException {
114     assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
115     if (bytes != null) {
116       // Already cached -- copy from cache:
117       assert len <= owner.maxDoc();
118       System.arraycopy(bytes, 0, bytesOut, offset, len);
119     } else {
120       // Not cached
121       if (origNorm != null) {
122         // Ask origNorm to load
123         origNorm.bytes(bytesOut, offset, len);
124       } else {
125         // We are orig -- read ourselves from disk:
126         synchronized(in) {
127           in.seek(normSeek);
128           in.readBytes(bytesOut, offset, len, false);
129         }
130       }
131     }
132   }
133
134   // Load & cache full bytes array.  Returns bytes.
135   public synchronized byte[] bytes() throws IOException {
136     assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
137     if (bytes == null) {                     // value not yet read
138       assert bytesRef == null;
139       if (origNorm != null) {
140         // Ask origNorm to load so that for a series of
141         // reopened readers we share a single read-only
142         // byte[]
143         bytes = origNorm.bytes();
144         bytesRef = origNorm.bytesRef;
145         bytesRef.incrementAndGet();
146
147         // Once we've loaded the bytes we no longer need
148         // origNorm:
149         origNorm.decRef();
150         origNorm = null;
151
152       } else {
153         // We are the origNorm, so load the bytes for real
154         // ourself:
155         final int count = owner.maxDoc();
156         bytes = new byte[count];
157
158         // Since we are orig, in must not be null
159         assert in != null;
160
161         // Read from disk.
162         synchronized(in) {
163           in.seek(normSeek);
164           in.readBytes(bytes, 0, count, false);
165         }
166
167         bytesRef = new AtomicInteger(1);
168         closeInput();
169       }
170     }
171
172     return bytes;
173   }
174
175   // Only for testing
176   AtomicInteger bytesRef() {
177     return bytesRef;
178   }
179
180   // Called if we intend to change a norm value.  We make a
181   // private copy of bytes if it's shared with others:
182   public synchronized byte[] copyOnWrite() throws IOException {
183     assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
184     bytes();
185     assert bytes != null;
186     assert bytesRef != null;
187     if (bytesRef.get() > 1) {
188       // I cannot be the origNorm for another norm
189       // instance if I'm being changed.  Ie, only the
190       // "head Norm" can be changed:
191       assert refCount == 1;
192       final AtomicInteger oldRef = bytesRef;
193       bytes = owner.cloneNormBytes(bytes);
194       bytesRef = new AtomicInteger(1);
195       oldRef.decrementAndGet();
196     }
197     dirty = true;
198     return bytes;
199   }
200   
201   // Returns a copy of this Norm instance that shares
202   // IndexInput & bytes with the original one
203   @Override
204   public synchronized Object clone() {
205     assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
206       
207     SegmentNorms clone;
208     try {
209       clone = (SegmentNorms) super.clone();
210     } catch (CloneNotSupportedException cnse) {
211       // Cannot happen
212       throw new RuntimeException("unexpected CloneNotSupportedException", cnse);
213     }
214     clone.refCount = 1;
215
216     if (bytes != null) {
217       assert bytesRef != null;
218       assert origNorm == null;
219
220       // Clone holds a reference to my bytes:
221       clone.bytesRef.incrementAndGet();
222     } else {
223       assert bytesRef == null;
224       if (origNorm == null) {
225         // I become the origNorm for the clone:
226         clone.origNorm = this;
227       }
228       clone.origNorm.incRef();
229     }
230
231     // Only the origNorm will actually readBytes from in:
232     clone.in = null;
233
234     return clone;
235   }
236
237   // Flush all pending changes to the next generation
238   // separate norms file.
239   public void reWrite(SegmentInfo si) throws IOException {
240     assert refCount > 0 && (origNorm == null || origNorm.refCount > 0): "refCount=" + refCount + " origNorm=" + origNorm;
241
242     // NOTE: norms are re-written in regular directory, not cfs
243     si.advanceNormGen(this.number);
244     final String normFileName = si.getNormFileName(this.number);
245     IndexOutput out = owner.directory().createOutput(normFileName);
246     boolean success = false;
247     try {
248       try {
249         out.writeBytes(SegmentNorms.NORMS_HEADER, 0, SegmentNorms.NORMS_HEADER.length);
250         out.writeBytes(bytes, owner.maxDoc());
251       } finally {
252         out.close();
253       }
254       success = true;
255     } finally {
256       if (!success) {
257         try {
258           owner.directory().deleteFile(normFileName);
259         } catch (Throwable t) {
260           // suppress this so we keep throwing the
261           // original exception
262         }
263       }
264     }
265     this.dirty = false;
266   }
267 }