Index: lucene/contrib/misc/src/java/org/apache/lucene/index/codecs/spi/ContribMiscCodecProvider.java =================================================================== --- lucene/contrib/misc/src/java/org/apache/lucene/index/codecs/spi/ContribMiscCodecProvider.java (revision 0) +++ lucene/contrib/misc/src/java/org/apache/lucene/index/codecs/spi/ContribMiscCodecProvider.java (working copy) @@ -0,0 +1,40 @@ +package org.apache.lucene.index.codecs.spi; + +/** + * 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. + */ + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.PostingsFormat; + +import org.apache.lucene.index.codecs.appending.AppendingCodec; + +public final class ContribMiscCodecProvider extends SimpleCodecProvider { + + public ContribMiscCodecProvider() { + super( + Arrays.asList( + new AppendingCodec() + ), + Collections.emptyList() + ); + } + +} Index: lucene/contrib/misc/src/java/org/apache/lucene/index/codecs/spi/ContribMiscCodecProvider.java =================================================================== --- lucene/contrib/misc/src/java/org/apache/lucene/index/codecs/spi/ContribMiscCodecProvider.java (revision 0) +++ lucene/contrib/misc/src/java/org/apache/lucene/index/codecs/spi/ContribMiscCodecProvider.java (working copy) Property changes on: lucene/contrib/misc/src/java/org/apache/lucene/index/codecs/spi/ContribMiscCodecProvider.java ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Date Author Id Revision HeadURL Added: svn:eol-style ## -0,0 +1 ## +native Index: lucene/contrib/misc/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider =================================================================== --- lucene/contrib/misc/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (revision 0) +++ lucene/contrib/misc/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (working copy) @@ -0,0 +1,16 @@ +# 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. + +org.apache.lucene.index.codecs.spi.ContribMiscCodecProvider Index: lucene/contrib/misc/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider =================================================================== --- lucene/contrib/misc/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (revision 0) +++ lucene/contrib/misc/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (working copy) Property changes on: lucene/contrib/misc/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: lucene/src/java/org/apache/lucene/index/codecs/Codec.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/Codec.java (revision 1195718) +++ lucene/src/java/org/apache/lucene/index/codecs/Codec.java (working copy) @@ -18,13 +18,10 @@ */ import java.io.IOException; -import java.util.HashMap; -import java.util.Map; import java.util.Set; import org.apache.lucene.index.SegmentInfo; -import org.apache.lucene.index.codecs.lucene3x.Lucene3xCodec; -import org.apache.lucene.index.codecs.lucene40.Lucene40Codec; +import org.apache.lucene.index.codecs.spi.CodecLoader; import org.apache.lucene.store.Directory; /** @@ -66,17 +63,9 @@ /** looks up a codec by name */ public static Codec forName(String name) { - // TODO: Uwe fix this crap, this is just a stub! - return CORE_CODECS.get(name); + return CodecLoader.lookupCodec(name); } - @Deprecated - private static final Map CORE_CODECS = new HashMap(); - static { - CORE_CODECS.put("Lucene40", new Lucene40Codec()); - CORE_CODECS.put("Lucene3x", new Lucene3xCodec()); - } - private static Codec defaultCodec = Codec.forName("Lucene40"); // nocommit: should we remove these? Index: lucene/src/java/org/apache/lucene/index/codecs/lucene40/Lucene40Codec.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/lucene40/Lucene40Codec.java (revision 1195718) +++ lucene/src/java/org/apache/lucene/index/codecs/lucene40/Lucene40Codec.java (working copy) @@ -72,5 +72,6 @@ return defaultFormat; } - private final PostingsFormat defaultFormat = PostingsFormat.forName("Lucene40"); + // TODO: PostingsFormat.forName("Lucene40") does not work because of Chicken-and-Egg problem: + private final PostingsFormat defaultFormat = new Lucene40PostingsFormat(); } Index: lucene/src/java/org/apache/lucene/index/codecs/PostingsFormat.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/PostingsFormat.java (revision 1195718) +++ lucene/src/java/org/apache/lucene/index/codecs/PostingsFormat.java (working copy) @@ -18,18 +18,13 @@ */ import java.io.IOException; -import java.util.HashMap; -import java.util.Map; import java.util.Set; import org.apache.lucene.index.SegmentInfo; import org.apache.lucene.index.SegmentWriteState; import org.apache.lucene.index.SegmentReadState; -import org.apache.lucene.index.codecs.lucene40.Lucene40PostingsBaseFormat; -import org.apache.lucene.index.codecs.lucene40.Lucene40PostingsFormat; -import org.apache.lucene.index.codecs.memory.MemoryPostingsFormat; -import org.apache.lucene.index.codecs.pulsing.PulsingPostingsFormat; -import org.apache.lucene.index.codecs.simpletext.SimpleTextPostingsFormat; +import org.apache.lucene.index.codecs.spi.CodecLoader; + import org.apache.lucene.store.Directory; /** @lucene.experimental */ @@ -67,18 +62,6 @@ } public static PostingsFormat forName(String name) { - // TODO: Uwe fix me! - return CORE_FORMATS.get(name); + return CodecLoader.lookupPostingsFormat(name); } - /** Lucene's core postings formats. - * @lucene.internal - */ - @Deprecated - public static final Map CORE_FORMATS = new HashMap(); - static { - CORE_FORMATS.put("Lucene40", new Lucene40PostingsFormat()); - CORE_FORMATS.put("Pulsing", new PulsingPostingsFormat(new Lucene40PostingsBaseFormat(), 1)); - CORE_FORMATS.put("SimpleText", new SimpleTextPostingsFormat()); - CORE_FORMATS.put("Memory", new MemoryPostingsFormat()); - } } Index: lucene/src/java/org/apache/lucene/index/codecs/spi/CodecLoader.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/CodecLoader.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/CodecLoader.java (working copy) @@ -0,0 +1,93 @@ +package org.apache.lucene.index.codecs.spi; + +/** + * 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. + */ + +import java.util.Collections; +import java.util.Set; +import java.util.LinkedHashSet; +import java.util.ServiceLoader; + +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.PostingsFormat; + +/** + * Helper class for loading {@link Codec} and {@link PostingsFormat} using SPI. + * TODO: Somehow remove synchronization, ServiceLoader is not threadsafe? + * @lucene.internal + */ +public final class CodecLoader { + + private static final ServiceLoader loader = + ServiceLoader.load(CodecProvider.class); + private static Set codecCache = null, pfCache = null; + + private CodecLoader() {} + + public static synchronized void reload() { + loader.reload(); + codecCache = null; + pfCache = null; + } + + public static synchronized Codec lookupCodec(String name) { + for (CodecProvider cp : loader) { + final Codec codec = cp.lookupCodec(name); + if (codec != null) return codec; + } + throw new RuntimeException("A codec with name '"+name+"' does not exist. "+ + "You need to add the corresponding JAR file with a CodecProvider supporting this codec to your classpath."); + } + + public static synchronized Set availableCodecs() { + if (codecCache == null) { + final Set s = new LinkedHashSet(); + for (CodecProvider cp : loader) { + // don't use addAll, as we only want the first name of each duplicate! + for (String name : cp.availableCodecs()) { + if (!s.contains(name)) s.add(name); + } + } + codecCache = Collections.unmodifiableSet(s); + } + return codecCache; + } + + public static synchronized PostingsFormat lookupPostingsFormat(String name) { + for (CodecProvider cp : loader) { + final PostingsFormat pf = cp.lookupPostingsFormat(name); + if (pf != null) return pf; + } + throw new RuntimeException("A postings format with name '"+name+"' does not exist. "+ + "You need to add the corresponding JAR file with a CodecProvider supporting this format to your classpath."); + } + + public static synchronized Set availablePostingsFormats() { + if (pfCache == null) { + final Set s = new LinkedHashSet(); + for (CodecProvider cp : loader) { + // don't use addAll, as we only want the first name of each duplicate! + for (String name : cp.availablePostingsFormats()) { + if (!s.contains(name)) s.add(name); + } + } + pfCache = Collections.unmodifiableSet(s); + } + return pfCache; + } + +} Index: lucene/src/java/org/apache/lucene/index/codecs/spi/CodecLoader.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/CodecLoader.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/CodecLoader.java (working copy) Property changes on: lucene/src/java/org/apache/lucene/index/codecs/spi/CodecLoader.java ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Date Author Id Revision HeadURL Added: svn:eol-style ## -0,0 +1 ## +native Index: lucene/src/java/org/apache/lucene/index/codecs/spi/CodecProvider.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/CodecProvider.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/CodecProvider.java (working copy) @@ -0,0 +1,45 @@ +package org.apache.lucene.index.codecs.spi; + +/** + * 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. + */ + +import java.util.Set; + +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.PostingsFormat; + +/** + * This interface is used by SPI to make all codecs of a JAR + * file available through SPI via {@link java.util.ServiceLoader}. + * Implementations usually subclass {@link SimpleCodecProvider}. + * @lucene.internal + */ +public interface CodecProvider { + + /** Looks up a codec by name, must return {@code null} if this provider does not support this name. **/ + Codec lookupCodec(String name); + + /** Returns all codecs, this provider supports. **/ + Set availableCodecs(); + + /** Looks up a postings format by name, must return {@code null} if this provider does not support this name. **/ + PostingsFormat lookupPostingsFormat(String name); + + /** Returns all postings formats, this provider supports. **/ + Set availablePostingsFormats(); + +} Index: lucene/src/java/org/apache/lucene/index/codecs/spi/CodecProvider.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/CodecProvider.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/CodecProvider.java (working copy) Property changes on: lucene/src/java/org/apache/lucene/index/codecs/spi/CodecProvider.java ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Date Author Id Revision HeadURL Added: svn:eol-style ## -0,0 +1 ## +native Index: lucene/src/java/org/apache/lucene/index/codecs/spi/CoreCodecProvider.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/CoreCodecProvider.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/CoreCodecProvider.java (working copy) @@ -0,0 +1,52 @@ +package org.apache.lucene.index.codecs.spi; + +/** + * 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. + */ + +import java.util.Arrays; +import java.util.List; + +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.PostingsFormat; + +import org.apache.lucene.index.codecs.lucene3x.Lucene3xCodec; +import org.apache.lucene.index.codecs.lucene40.Lucene40Codec; + +import org.apache.lucene.index.codecs.lucene40.Lucene40PostingsBaseFormat; +import org.apache.lucene.index.codecs.lucene40.Lucene40PostingsFormat; +import org.apache.lucene.index.codecs.memory.MemoryPostingsFormat; +import org.apache.lucene.index.codecs.pulsing.PulsingPostingsFormat; +import org.apache.lucene.index.codecs.simpletext.SimpleTextPostingsFormat; + +public final class CoreCodecProvider extends SimpleCodecProvider { + + public CoreCodecProvider() { + super( + Arrays.asList( + new Lucene40Codec(), + new Lucene3xCodec() + ), + Arrays.asList( + new Lucene40PostingsFormat(), + new PulsingPostingsFormat(new Lucene40PostingsBaseFormat(), 1), + new SimpleTextPostingsFormat(), + new MemoryPostingsFormat() + ) + ); + } + +} Index: lucene/src/java/org/apache/lucene/index/codecs/spi/CoreCodecProvider.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/CoreCodecProvider.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/CoreCodecProvider.java (working copy) Property changes on: lucene/src/java/org/apache/lucene/index/codecs/spi/CoreCodecProvider.java ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Date Author Id Revision HeadURL Added: svn:eol-style ## -0,0 +1 ## +native Index: lucene/src/java/org/apache/lucene/index/codecs/spi/SimpleCodecProvider.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/SimpleCodecProvider.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/SimpleCodecProvider.java (working copy) @@ -0,0 +1,67 @@ +package org.apache.lucene.index.codecs.spi; + +/** + * 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. + */ + +import java.util.Set; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.PostingsFormat; + +/** + * Common base class for CodecProviders that uses maps to implement lookup + * @lucene.internal + */ +public abstract class SimpleCodecProvider implements CodecProvider { + + private final Map availableCodecs = new LinkedHashMap(); + private final Map availablePostingsFormats = new LinkedHashMap(); + + protected SimpleCodecProvider(List codecs, List postingsFormats) { + for (Codec codec : codecs) { + availableCodecs.put(codec.getName(), codec); + } + for (PostingsFormat pf : postingsFormats) { + availablePostingsFormats.put(pf.name, pf); + } + } + + @Override + public final Codec lookupCodec(String name) { + return availableCodecs.get(name); + } + + @Override + public final Set availableCodecs() { + return Collections.unmodifiableSet(availableCodecs.keySet()); + } + + @Override + public final PostingsFormat lookupPostingsFormat(String name) { + return availablePostingsFormats.get(name); + } + + @Override + public final Set availablePostingsFormats() { + return Collections.unmodifiableSet(availablePostingsFormats.keySet()); + } + +} Index: lucene/src/java/org/apache/lucene/index/codecs/spi/SimpleCodecProvider.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/spi/SimpleCodecProvider.java (revision 0) +++ lucene/src/java/org/apache/lucene/index/codecs/spi/SimpleCodecProvider.java (working copy) Property changes on: lucene/src/java/org/apache/lucene/index/codecs/spi/SimpleCodecProvider.java ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Date Author Id Revision HeadURL Added: svn:eol-style ## -0,0 +1 ## +native Index: lucene/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider =================================================================== --- lucene/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (revision 0) +++ lucene/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (working copy) @@ -0,0 +1,16 @@ +# 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. + +org.apache.lucene.index.codecs.spi.CoreCodecProvider Index: lucene/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider =================================================================== --- lucene/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (revision 0) +++ lucene/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider (working copy) Property changes on: lucene/src/resources/META-INF/services/org.apache.lucene.index.codecs.spi.CodecProvider ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native