/
Codec.java
161 lines (135 loc) · 5.45 KB
/
Codec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.codecs;
import java.util.Objects;
import java.util.ServiceLoader; // javadocs
import java.util.Set;
import org.apache.lucene.index.IndexWriterConfig; // javadocs
import org.apache.lucene.util.NamedSPILoader;
/**
* Encodes/decodes an inverted index segment.
*
* <p>Note, when extending this class, the name ({@link #getName}) is written into the index. In
* order for the segment to be read, the name must resolve to your implementation via {@link
* #forName(String)}. This method uses Java's {@link ServiceLoader Service Provider Interface} (SPI)
* to resolve codec names.
*
* <p>If you implement your own codec, make sure that it has a no-arg constructor so SPI can load
* it.
*
* @see ServiceLoader
*/
public abstract class Codec implements NamedSPILoader.NamedSPI {
/**
* This static holder class prevents classloading deadlock by delaying init of default codecs and
* available codecs until needed.
*/
private static final class Holder {
private static final NamedSPILoader<Codec> LOADER = new NamedSPILoader<>(Codec.class);
private Holder() {}
static NamedSPILoader<Codec> getLoader() {
if (LOADER == null) {
throw new IllegalStateException(
"You tried to lookup a Codec by name before all Codecs could be initialized. "
+ "This likely happens if you call Codec#forName from a Codec's ctor.");
}
return LOADER;
}
static Codec defaultCodec = LOADER.lookup("Lucene99");
}
private final String name;
/**
* Creates a new codec.
*
* <p>The provided name will be written into the index segment: in order to for the segment to be
* read this class should be registered with Java's SPI mechanism (registered in META-INF/ of your
* jar file, etc).
*
* @param name must be all ascii alphanumeric, and less than 128 characters in length.
*/
protected Codec(String name) {
NamedSPILoader.checkServiceName(name);
this.name = name;
}
/** Returns this codec's name */
@Override
public final String getName() {
return name;
}
/** Encodes/decodes postings */
public abstract PostingsFormat postingsFormat();
/** Encodes/decodes docvalues */
public abstract DocValuesFormat docValuesFormat();
/** Encodes/decodes stored fields */
public abstract StoredFieldsFormat storedFieldsFormat();
/** Encodes/decodes term vectors */
public abstract TermVectorsFormat termVectorsFormat();
/** Encodes/decodes field infos file */
public abstract FieldInfosFormat fieldInfosFormat();
/** Encodes/decodes segment info file */
public abstract SegmentInfoFormat segmentInfoFormat();
/** Encodes/decodes document normalization values */
public abstract NormsFormat normsFormat();
/** Encodes/decodes live docs */
public abstract LiveDocsFormat liveDocsFormat();
/** Encodes/decodes compound files */
public abstract CompoundFormat compoundFormat();
/** Encodes/decodes points index */
public abstract PointsFormat pointsFormat();
/** Encodes/decodes numeric vector fields */
public abstract KnnVectorsFormat knnVectorsFormat();
/** looks up a codec by name */
public static Codec forName(String name) {
return Holder.getLoader().lookup(name);
}
/** returns a list of all available codec names */
public static Set<String> availableCodecs() {
return Holder.getLoader().availableServices();
}
/**
* Reloads the codec list from the given {@link ClassLoader}. Changes to the codecs are visible
* after the method ends, all iterators ({@link #availableCodecs()},...) stay consistent.
*
* <p><b>NOTE:</b> Only new codecs are added, existing ones are never removed or replaced.
*
* <p><em>This method is expensive and should only be called for discovery of new codecs on the
* given classpath/classloader!</em>
*/
public static void reloadCodecs(ClassLoader classloader) {
Holder.getLoader().reload(classloader);
}
/** expert: returns the default codec used for newly created {@link IndexWriterConfig}s. */
public static Codec getDefault() {
if (Holder.defaultCodec == null) {
throw new IllegalStateException(
"You tried to lookup the default Codec before all Codecs could be initialized. "
+ "This likely happens if you try to get it from a Codec's ctor.");
}
return Holder.defaultCodec;
}
/** expert: sets the default codec used for newly created {@link IndexWriterConfig}s. */
public static void setDefault(Codec codec) {
Holder.defaultCodec = Objects.requireNonNull(codec);
}
/**
* returns the codec's name. Subclasses can override to provide more detail (such as parameters).
*/
@Override
public String toString() {
return name;
}
}