diff --git a/lucene/src/java/org/apache/lucene/index/codecs/CodecProvider.java b/lucene/src/java/org/apache/lucene/index/codecs/CodecProvider.java index 04f8c591f7a..c67c465befd 100644 --- a/lucene/src/java/org/apache/lucene/index/codecs/CodecProvider.java +++ b/lucene/src/java/org/apache/lucene/index/codecs/CodecProvider.java @@ -88,6 +88,15 @@ public class CodecProvider { return codec; } + /** + * Returns true iff a codec with the given name is registered + * @param name codec name + * @return true iff a codec with the given name is registered, otherwise false. + */ + public synchronized boolean isCodecRegistered(String name) { + return codecs.containsKey(name); + } + public SegmentInfosWriter getSegmentInfosWriter() { return infosWriter; } @@ -145,6 +154,14 @@ public class CodecProvider { return codec; } + /** + * Returns true if this provider has a Codec registered for this + * field. + */ + public synchronized boolean hasFieldCodec(String name) { + return perFieldMap.containsKey(name); + } + /** * Returns the default {@link Codec} for this {@link CodecProvider} * diff --git a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java index 02e2b5cae97..47541e56ce1 100644 --- a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java @@ -1382,6 +1382,11 @@ public abstract class LuceneTestCase extends Assert { return codec.name; } + @Override + public synchronized boolean hasFieldCodec(String name) { + return true; // we have a codec for every field + } + @Override public synchronized String toString() { return "RandomCodecProvider: " + previousMappings.toString(); diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index d132b655be2..7b6affb1b15 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -144,6 +144,11 @@ New Features fq={!join from=name to=parent}eyes:blue (yonik) +* SOLR-1942: Added the ability to select codec per fieldType in schema.xml + as well as support custom CodecProviders in solrconfig.xml. + NOTE: IndexReaderFactory now has a codecProvider that should be passed + to IndexReader.open (in the case you have a custom IndexReaderFactory). + (simonw via rmuir) Optimizations diff --git a/solr/src/java/org/apache/solr/core/CodecProviderFactory.java b/solr/src/java/org/apache/solr/core/CodecProviderFactory.java new file mode 100644 index 00000000000..e3bc9851699 --- /dev/null +++ b/solr/src/java/org/apache/solr/core/CodecProviderFactory.java @@ -0,0 +1,32 @@ +package org.apache.solr.core; + +/** + * 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 org.apache.lucene.index.codecs.CodecProvider; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.util.plugin.NamedListInitializedPlugin; + +/** + * Factory for plugging in a custom {@link CodecProvider} + */ +public abstract class CodecProviderFactory implements NamedListInitializedPlugin { + public void init(NamedList args) { + } + + public abstract CodecProvider create(); +} diff --git a/solr/src/java/org/apache/solr/core/IndexReaderFactory.java b/solr/src/java/org/apache/solr/core/IndexReaderFactory.java index 4204a9ebb4f..fb87650a170 100644 --- a/solr/src/java/org/apache/solr/core/IndexReaderFactory.java +++ b/solr/src/java/org/apache/solr/core/IndexReaderFactory.java @@ -19,6 +19,7 @@ package org.apache.solr.core; import java.io.IOException; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.codecs.CodecProvider; import org.apache.lucene.store.Directory; import org.apache.solr.common.util.NamedList; import org.apache.solr.util.plugin.NamedListInitializedPlugin; @@ -28,6 +29,7 @@ import org.apache.solr.util.plugin.NamedListInitializedPlugin; */ public abstract class IndexReaderFactory implements NamedListInitializedPlugin { protected int termInfosIndexDivisor = 1;//IndexReader.DEFAULT_TERMS_INDEX_DIVISOR; Set this once Lucene makes this public. + protected CodecProvider provider; /** * Potentially initializes {@link #termInfosIndexDivisor}. Overriding classes should call super.init() in order * to make sure termInfosIndexDivisor is set. @@ -63,4 +65,11 @@ public abstract class IndexReaderFactory implements NamedListInitializedPlugin { */ public abstract IndexReader newReader(Directory indexDir, boolean readOnly) throws IOException; + + /** + * Sets the codec provider for this IndexReaderFactory + */ + public void setCodecProvider(CodecProvider provider) { + this.provider = provider; + } } diff --git a/solr/src/java/org/apache/solr/core/SchemaCodecProvider.java b/solr/src/java/org/apache/solr/core/SchemaCodecProvider.java new file mode 100644 index 00000000000..3d0ffab1835 --- /dev/null +++ b/solr/src/java/org/apache/solr/core/SchemaCodecProvider.java @@ -0,0 +1,155 @@ +package org.apache.solr.core; + +/** + * 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.Collection; +import java.util.Set; + +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.CodecProvider; +import org.apache.lucene.index.codecs.SegmentInfosReader; +import org.apache.lucene.index.codecs.SegmentInfosWriter; +import org.apache.solr.schema.IndexSchema; +import org.apache.solr.schema.SchemaField; + +/** + * Selects a codec based on a {@link IndexSchema}. This {@link CodecProvider} + * also supports dynamic fields such that not all field codecs need to be known + * in advance + */ +final class SchemaCodecProvider extends CodecProvider { + private final IndexSchema schema; + private final CodecProvider delegate; + + SchemaCodecProvider(IndexSchema schema, CodecProvider delegate) { + this.schema = schema; + this.delegate = delegate; + } + + @Override + public Codec lookup(String name) { + synchronized (delegate) { + return delegate.lookup(name); + } + } + + @Override + public String getFieldCodec(String name) { + synchronized (delegate) { + if (!delegate.hasFieldCodec(name)) { + final SchemaField fieldOrNull = schema.getFieldOrNull(name); + if (fieldOrNull == null) { + throw new IllegalArgumentException("no such field " + name); + } + String codecName = fieldOrNull.getType().getCodec(); + if (codecName == null) { + codecName = delegate.getDefaultFieldCodec(); + } + delegate.setFieldCodec(name, codecName); + return codecName; + } + return delegate.getFieldCodec(name); + } + } + + @Override + public int hashCode() { + return delegate.hashCode(); + } + + @Override + public void register(Codec codec) { + throw new UnsupportedOperationException(); + } + + @Override + public void unregister(Codec codec) { + throw new UnsupportedOperationException(); + } + + @Override + public Collection getAllExtensions() { + return delegate.getAllExtensions(); + } + + @Override + public SegmentInfosWriter getSegmentInfosWriter() { + return delegate.getSegmentInfosWriter(); + } + + @Override + public SegmentInfosReader getSegmentInfosReader() { + return delegate.getSegmentInfosReader(); + } + + @Override + public boolean equals(Object obj) { + return delegate.equals(obj); + } + + @Override + public void setFieldCodec(String field, String codec) { + throw new UnsupportedOperationException(); + } + + @Override + public String getDefaultFieldCodec() { + return delegate.getDefaultFieldCodec(); + } + + @Override + public boolean isCodecRegistered(String name) { + synchronized (delegate) { + return delegate.isCodecRegistered(name); + } + } + + @Override + public void setDefaultFieldCodec(String codec) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean hasFieldCodec(String fieldName) { + synchronized (delegate) { + if (!delegate.hasFieldCodec(fieldName)) { + final SchemaField fieldOrNull = schema.getFieldOrNull(fieldName); + if (fieldOrNull == null) { + return false; + } + String codecName = fieldOrNull.getType().getCodec(); + if (codecName == null) { + codecName = delegate.getDefaultFieldCodec(); + } + delegate.setFieldCodec(fieldName, codecName); + } + return true; + } + } + + @Override + public String toString() { + return "SchemaCodecProvider(" + delegate.toString() + ")"; + } + + @Override + public Set listAll() { + synchronized (delegate) { + return delegate.listAll(); + } + } +} diff --git a/solr/src/java/org/apache/solr/core/SolrConfig.java b/solr/src/java/org/apache/solr/core/SolrConfig.java index 9d079b8eb2d..3bdd551c5a4 100644 --- a/solr/src/java/org/apache/solr/core/SolrConfig.java +++ b/solr/src/java/org/apache/solr/core/SolrConfig.java @@ -39,6 +39,8 @@ import org.apache.solr.spelling.QueryConverter; import org.apache.solr.highlight.SolrHighlighter; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.index.IndexDeletionPolicy; +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.CodecProvider; import org.apache.lucene.util.Version; import org.slf4j.Logger; @@ -202,6 +204,7 @@ public class SolrConfig extends Config { loadPluginInfo(DirectoryFactory.class,"directoryFactory",false, true); loadPluginInfo(IndexDeletionPolicy.class,"mainIndex/deletionPolicy",false, true); + loadPluginInfo(CodecProviderFactory.class,"mainIndex/codecProviderFactory",false, false); loadPluginInfo(IndexReaderFactory.class,"indexReaderFactory",false, true); loadPluginInfo(UpdateRequestProcessorChain.class,"updateRequestProcessorChain",false, false); diff --git a/solr/src/java/org/apache/solr/core/SolrCore.java b/solr/src/java/org/apache/solr/core/SolrCore.java index a8a63c5b398..4bf9288999a 100644 --- a/solr/src/java/org/apache/solr/core/SolrCore.java +++ b/solr/src/java/org/apache/solr/core/SolrCore.java @@ -20,6 +20,8 @@ package org.apache.solr.core; import org.apache.lucene.index.IndexDeletionPolicy; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.CodecProvider; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.store.Directory; import org.apache.solr.common.SolrException; @@ -93,6 +95,7 @@ public final class SolrCore implements SolrInfoMBean { private IndexDeletionPolicyWrapper solrDelPolicy; private DirectoryFactory directoryFactory; private IndexReaderFactory indexReaderFactory; + private final CodecProvider codecProvider; public long getStartTime() { return startTime; } @@ -330,6 +333,7 @@ public final class SolrCore implements SolrInfoMBean { indexReaderFactory = new StandardIndexReaderFactory(); } this.indexReaderFactory = indexReaderFactory; + this.indexReaderFactory.setCodecProvider(codecProvider); } // protect via synchronized(SolrCore.class) @@ -366,7 +370,7 @@ public final class SolrCore implements SolrInfoMBean { log.warn(logid+"Solr index directory '" + new File(indexDir) + "' doesn't exist." + " Creating new index..."); - SolrIndexWriter writer = new SolrIndexWriter("SolrCore.initIndex", indexDir, getDirectoryFactory(), true, schema, solrConfig.mainIndexConfig, solrDelPolicy); + SolrIndexWriter writer = new SolrIndexWriter("SolrCore.initIndex", indexDir, getDirectoryFactory(), true, schema, solrConfig.mainIndexConfig, solrDelPolicy, codecProvider); writer.close(); } @@ -493,6 +497,7 @@ public final class SolrCore implements SolrInfoMBean { initDeletionPolicy(); + this.codecProvider = initCodecProvider(solrConfig, schema); initIndex(); initWriters(); @@ -555,6 +560,19 @@ public final class SolrCore implements SolrInfoMBean { resourceLoader.inform(infoRegistry); } + private CodecProvider initCodecProvider(SolrConfig solrConfig, IndexSchema schema) { + final PluginInfo info = solrConfig.getPluginInfo(CodecProviderFactory.class.getName()); + CodecProvider cp; + if (info != null) { + CodecProviderFactory factory = (CodecProviderFactory) schema.getResourceLoader().newInstance(info.className); + factory.init(info.initArgs); + cp = factory.create(); + } else { + // make sure we use the default if nothing is configured + cp = CodecProvider.getDefault(); + } + return new SchemaCodecProvider(schema, cp); + } /** * Load the request processors @@ -1618,6 +1636,10 @@ public final class SolrCore implements SolrInfoMBean { } return lst; } + + public CodecProvider getCodecProvider() { + return codecProvider; + } } diff --git a/solr/src/java/org/apache/solr/core/StandardIndexReaderFactory.java b/solr/src/java/org/apache/solr/core/StandardIndexReaderFactory.java index 2695cb26b76..5767d296049 100644 --- a/solr/src/java/org/apache/solr/core/StandardIndexReaderFactory.java +++ b/solr/src/java/org/apache/solr/core/StandardIndexReaderFactory.java @@ -35,6 +35,6 @@ public class StandardIndexReaderFactory extends IndexReaderFactory { @Override public IndexReader newReader(Directory indexDir, boolean readOnly) throws IOException { - return IndexReader.open(indexDir, null, readOnly, termInfosIndexDivisor); + return IndexReader.open(indexDir, null, readOnly, termInfosIndexDivisor, provider); } } diff --git a/solr/src/java/org/apache/solr/schema/FieldType.java b/solr/src/java/org/apache/solr/schema/FieldType.java index 2767c738424..2c78c6ecd08 100644 --- a/solr/src/java/org/apache/solr/schema/FieldType.java +++ b/solr/src/java/org/apache/solr/schema/FieldType.java @@ -167,6 +167,12 @@ public abstract class FieldType extends FieldProperties { initArgs.remove("positionIncrementGap"); } + final String codec = initArgs.get("codec"); + if (codec != null) { + this.codec = codec; + initArgs.remove("codec"); + } + if (initArgs.size() > 0) { throw new RuntimeException("schema fieldtype " + typeName + "("+ this.getClass().getName() + ")" @@ -537,6 +543,15 @@ public abstract class FieldType extends FieldProperties { this.similarity = similarity; } + /** + * The codec ID used for this field type + */ + protected String codec; + + public String getCodec() { + return codec; + } + /** * calls back to TextResponseWriter to write the field value */ diff --git a/solr/src/java/org/apache/solr/update/SolrIndexWriter.java b/solr/src/java/org/apache/solr/update/SolrIndexWriter.java index 476104b8bb6..5ecb4998e2d 100644 --- a/solr/src/java/org/apache/solr/update/SolrIndexWriter.java +++ b/solr/src/java/org/apache/solr/update/SolrIndexWriter.java @@ -18,7 +18,10 @@ package org.apache.solr.update; import org.apache.lucene.index.*; +import org.apache.lucene.index.IndexWriterConfig.OpenMode; +import org.apache.lucene.index.codecs.CodecProvider; import org.apache.lucene.store.*; +import org.apache.lucene.util.Version; import org.apache.solr.common.SolrException; import org.apache.solr.core.DirectoryFactory; import org.apache.solr.schema.IndexSchema; @@ -27,6 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -79,16 +83,21 @@ public class SolrIndexWriter extends IndexWriter { return d; } - public SolrIndexWriter(String name, String path, DirectoryFactory dirFactory, boolean create, IndexSchema schema, SolrIndexConfig config, IndexDeletionPolicy delPolicy) throws IOException { + public SolrIndexWriter(String name, String path, DirectoryFactory dirFactory, boolean create, IndexSchema schema, SolrIndexConfig config, IndexDeletionPolicy delPolicy, CodecProvider codecProvider) throws IOException { super( getDirectory(path, dirFactory, config), config.toIndexWriterConfig(schema). setOpenMode(create ? IndexWriterConfig.OpenMode.CREATE : IndexWriterConfig.OpenMode.APPEND). - setIndexDeletionPolicy(delPolicy) + setIndexDeletionPolicy(delPolicy).setCodecProvider(codecProvider) ); log.debug("Opened Writer " + name); this.name = name; + setInfoStream(config); + } + + private void setInfoStream(SolrIndexConfig config) + throws IOException { String infoStreamFile = config.infoStreamFile; if (infoStreamFile != null) { File f = new File(infoStreamFile); diff --git a/solr/src/java/org/apache/solr/update/UpdateHandler.java b/solr/src/java/org/apache/solr/update/UpdateHandler.java index cd13a4935ab..b90b80e11bd 100644 --- a/solr/src/java/org/apache/solr/update/UpdateHandler.java +++ b/solr/src/java/org/apache/solr/update/UpdateHandler.java @@ -98,7 +98,7 @@ public abstract class UpdateHandler implements SolrInfoMBean { } protected SolrIndexWriter createMainIndexWriter(String name, boolean removeAllExisting) throws IOException { - return new SolrIndexWriter(name,core.getNewIndexDir(), core.getDirectoryFactory(), removeAllExisting, schema, core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy()); + return new SolrIndexWriter(name,core.getNewIndexDir(), core.getDirectoryFactory(), removeAllExisting, schema, core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy(), core.getCodecProvider()); } protected final Term idTerm(String readableId) { diff --git a/solr/src/test-files/solr/conf/schema_codec.xml b/solr/src/test-files/solr/conf/schema_codec.xml new file mode 100644 index 00000000000..2a69f11dc21 --- /dev/null +++ b/solr/src/test-files/solr/conf/schema_codec.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + string_f + string_f + diff --git a/solr/src/test-files/solr/conf/solrconfig_codec.xml b/solr/src/test-files/solr/conf/solrconfig_codec.xml new file mode 100644 index 00000000000..0a1a7e12646 --- /dev/null +++ b/solr/src/test-files/solr/conf/solrconfig_codec.xml @@ -0,0 +1,33 @@ + + + + + ${tests.luceneMatchVersion:LUCENE_CURRENT} + + + + Pulsing + + org.apache.lucene.index.codecs.simpletext.SimpleTextCodec + org.apache.lucene.index.codecs.preflex.PreFlexCodec + + + + + + diff --git a/solr/src/test/org/apache/solr/BasicFunctionalityTest.java b/solr/src/test/org/apache/solr/BasicFunctionalityTest.java index 3b12f7978e4..cb87ce8dc9f 100644 --- a/solr/src/test/org/apache/solr/BasicFunctionalityTest.java +++ b/solr/src/test/org/apache/solr/BasicFunctionalityTest.java @@ -120,7 +120,7 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 { // test merge factor picked up SolrCore core = h.getCore(); - SolrIndexWriter writer = new SolrIndexWriter("testWriter",core.getNewIndexDir(), core.getDirectoryFactory(), false, core.getSchema(), core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy()); + SolrIndexWriter writer = new SolrIndexWriter("testWriter",core.getNewIndexDir(), core.getDirectoryFactory(), false, core.getSchema(), core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy(), core.getCodecProvider()); assertEquals("Mergefactor was not picked up", ((LogMergePolicy) writer.getConfig().getMergePolicy()).getMergeFactor(), 8); writer.close(); diff --git a/solr/src/test/org/apache/solr/cloud/BasicZkTest.java b/solr/src/test/org/apache/solr/cloud/BasicZkTest.java index 01025fcf730..0f2ad828014 100644 --- a/solr/src/test/org/apache/solr/cloud/BasicZkTest.java +++ b/solr/src/test/org/apache/solr/cloud/BasicZkTest.java @@ -47,7 +47,7 @@ public class BasicZkTest extends AbstractZkTestCase { SolrCore core = h.getCore(); SolrIndexWriter writer = new SolrIndexWriter("testWriter", core .getNewIndexDir(), core.getDirectoryFactory(), false, core.getSchema(), - core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy()); + core.getSolrConfig().mainIndexConfig, core.getDeletionPolicy(), core.getCodecProvider()); assertEquals("Mergefactor was not picked up", ((LogMergePolicy)writer.getConfig().getMergePolicy()).getMergeFactor(), 8); writer.close(); diff --git a/solr/src/test/org/apache/solr/core/MockCodecProviderFactory.java b/solr/src/test/org/apache/solr/core/MockCodecProviderFactory.java new file mode 100644 index 00000000000..08f29dcf1a6 --- /dev/null +++ b/solr/src/test/org/apache/solr/core/MockCodecProviderFactory.java @@ -0,0 +1,63 @@ +package org.apache.solr.core; + +/** + * 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 org.apache.lucene.index.codecs.Codec; +import org.apache.lucene.index.codecs.CodecProvider; +import org.apache.lucene.index.codecs.pulsing.PulsingCodec; +import org.apache.lucene.index.codecs.standard.StandardCodec; +import org.apache.solr.common.util.NamedList; + +/** + * CodecProviderFactory for testing, it inits a CP with Standard and Pulsing, + * and also adds any codecs specified by classname in solrconfig. + */ +public class MockCodecProviderFactory extends CodecProviderFactory { + private String defaultCodec; + private NamedList codecs; + + @Override + public void init(NamedList args) { + super.init(args); + defaultCodec = (String) args.get("defaultCodec"); + codecs = (NamedList) args.get("codecs"); + } + + @Override + public CodecProvider create() { + CodecProvider cp = new CodecProvider(); + cp.register(new StandardCodec()); + cp.register(new PulsingCodec(1)); + if (codecs != null) { + for (Object codec : codecs.getAll("name")) { + if (!cp.isCodecRegistered((String)codec)) { + try { + Class clazz = Class.forName((String)codec).asSubclass(Codec.class); + cp.register(clazz.newInstance()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + } + if (defaultCodec != null) { + cp.setDefaultFieldCodec(defaultCodec); + } + return cp; + } +} diff --git a/solr/src/test/org/apache/solr/core/TestCodecProviderSupport.java b/solr/src/test/org/apache/solr/core/TestCodecProviderSupport.java new file mode 100644 index 00000000000..10897a9eeab --- /dev/null +++ b/solr/src/test/org/apache/solr/core/TestCodecProviderSupport.java @@ -0,0 +1,106 @@ +package org.apache.solr.core; + +/** + * 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.Map; + +import org.apache.lucene.index.codecs.CodecProvider; +import org.apache.lucene.index.codecs.standard.StandardCodec; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.schema.SchemaField; +import org.junit.BeforeClass; + +public class TestCodecProviderSupport extends SolrTestCaseJ4 { + + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig_codec.xml", "schema_codec.xml"); + } + + public void testCodecs() { + CodecProvider codecProvider = h.getCore().getCodecProvider(); + Map fields = h.getCore().getSchema().getFields(); + SchemaField schemaField = fields.get("string_pulsing_f"); + assertEquals("Pulsing", codecProvider.getFieldCodec(schemaField.getName())); + schemaField = fields.get("string_simpletext_f"); + assertEquals("SimpleText", + codecProvider.getFieldCodec(schemaField.getName())); + schemaField = fields.get("string_standard_f"); + assertEquals("Standard", codecProvider.getFieldCodec(schemaField.getName())); + schemaField = fields.get("string_f"); + assertEquals("Pulsing", codecProvider.getFieldCodec(schemaField.getName())); + + assertTrue(codecProvider.hasFieldCodec("string_simpletext_f")); + assertTrue(codecProvider.hasFieldCodec("string_standard_f")); + assertTrue(codecProvider.hasFieldCodec("string_f")); + } + + public void testDynamicFields() { + CodecProvider codecProvider = h.getCore().getCodecProvider(); + + assertTrue(codecProvider.hasFieldCodec("bar_simple")); + assertTrue(codecProvider.hasFieldCodec("bar_pulsing")); + assertTrue(codecProvider.hasFieldCodec("bar_standard")); + + assertEquals("SimpleText", codecProvider.getFieldCodec("foo_simple")); + assertEquals("Pulsing", codecProvider.getFieldCodec("foo_pulsing")); + assertEquals("Standard", codecProvider.getFieldCodec("foo_standard")); + } + + public void testUnmodifiable() { + CodecProvider codecProvider = h.getCore().getCodecProvider(); + try { + codecProvider.setDefaultFieldCodec("foo"); + fail("should be unmodifiable"); + } catch (UnsupportedOperationException e) { + // + } + + try { + codecProvider.setFieldCodec("foo", "bar"); + fail("should be unmodifiable"); + } catch (UnsupportedOperationException e) { + // + } + + try { + codecProvider.register(new StandardCodec()); + fail("should be unmodifiable"); + } catch (UnsupportedOperationException e) { + // + } + + try { + codecProvider.unregister(new StandardCodec()); + fail("should be unmodifiable"); + } catch (UnsupportedOperationException e) { + // + } + } + + public void testUnknownField() { + CodecProvider codecProvider = h.getCore().getCodecProvider(); + try { + codecProvider.getFieldCodec("notexisting"); + fail("field is not existing"); + } catch (IllegalArgumentException e) { + // + } + + } +}