1
package org.apache.lucene.index;
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
11
* http://www.apache.org/licenses/LICENSE-2.0
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.
20
import java.io.IOException;
21
import java.util.concurrent.atomic.AtomicInteger;
23
import org.apache.lucene.store.IndexInput;
24
import org.apache.lucene.store.IndexOutput;
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).
35
final class SegmentNorms implements Cloneable {
37
/** norms header placeholder */
38
static final byte[] NORMS_HEADER = new byte[]{'N','R','M',-1};
42
// If this instance is a clone, the originalNorm
43
// references the Norm that has a real open IndexInput:
44
private SegmentNorms origNorm;
46
private IndexInput in;
47
private long normSeek;
49
// null until bytes is set
50
private AtomicInteger bytesRef;
55
boolean rollbackDirty;
57
private final SegmentReader owner;
59
public SegmentNorms(IndexInput in, int number, long normSeek, SegmentReader owner) {
62
this.normSeek = normSeek;
66
public synchronized void incRef() {
67
assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
71
private void closeInput() throws IOException {
73
if (in != owner.singleNormStream) {
74
// It's private to us -- just close it
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;
89
public synchronized void decRef() throws IOException {
90
assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
92
if (--refCount == 0) {
93
if (origNorm != null) {
101
assert bytesRef != null;
102
bytesRef.decrementAndGet();
106
assert bytesRef == null;
111
// Load bytes but do not cache them if they were not
113
public synchronized void bytes(byte[] bytesOut, int offset, int len) throws IOException {
114
assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
116
// Already cached -- copy from cache:
117
assert len <= owner.maxDoc();
118
System.arraycopy(bytes, 0, bytesOut, offset, len);
121
if (origNorm != null) {
122
// Ask origNorm to load
123
origNorm.bytes(bytesOut, offset, len);
125
// We are orig -- read ourselves from disk:
128
in.readBytes(bytesOut, offset, len, false);
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
143
bytes = origNorm.bytes();
144
bytesRef = origNorm.bytesRef;
145
bytesRef.incrementAndGet();
147
// Once we've loaded the bytes we no longer need
153
// We are the origNorm, so load the bytes for real
155
final int count = owner.maxDoc();
156
bytes = new byte[count];
158
// Since we are orig, in must not be null
164
in.readBytes(bytes, 0, count, false);
167
bytesRef = new AtomicInteger(1);
176
AtomicInteger bytesRef() {
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);
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();
201
// Returns a copy of this Norm instance that shares
202
// IndexInput & bytes with the original one
204
public synchronized Object clone() {
205
assert refCount > 0 && (origNorm == null || origNorm.refCount > 0);
209
clone = (SegmentNorms) super.clone();
210
} catch (CloneNotSupportedException cnse) {
212
throw new RuntimeException("unexpected CloneNotSupportedException", cnse);
217
assert bytesRef != null;
218
assert origNorm == null;
220
// Clone holds a reference to my bytes:
221
clone.bytesRef.incrementAndGet();
223
assert bytesRef == null;
224
if (origNorm == null) {
225
// I become the origNorm for the clone:
226
clone.origNorm = this;
228
clone.origNorm.incRef();
231
// Only the origNorm will actually readBytes from in:
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;
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;
249
out.writeBytes(SegmentNorms.NORMS_HEADER, 0, SegmentNorms.NORMS_HEADER.length);
250
out.writeBytes(bytes, owner.maxDoc());
258
owner.directory().deleteFile(normFileName);
259
} catch (Throwable t) {
260
// suppress this so we keep throwing the
261
// original exception