SOLR-729 -- Context.getDataSource(String) gives current entity's DataSource instance regardless of argument.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@689867 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2008-08-28 16:04:45 +00:00
parent d8822fee8d
commit 9a89d44c88
6 changed files with 53 additions and 12 deletions

View File

@ -32,6 +32,9 @@ Bug Fixes
use the complete string for parsing. Failure to do so will result in an exception.
(Stefan Oestreicher via shalin)
2. SOLR-729: Context.getDataSource(String) gives current entity's DataSource instance regardless of argument.
(Noble Paul, shalin)
Other Changes

View File

@ -72,18 +72,21 @@ public abstract class Context {
public abstract VariableResolver getVariableResolver();
/**
* Gets the datasource instance defined for this entity.
* Gets the datasource instance defined for this entity. Do not close() this instance.
* Transformers should use the getDataSource(String name) method.
*
* @return a new DataSource instance as configured for the current entity
* @see org.apache.solr.handler.dataimport.DataSource
* @see #getDataSource(String)
*/
public abstract DataSource getDataSource();
/**
* Gets a new DataSource instance with a name.
*
* Gets a new DataSource instance with a name. Ensure that you close() this after use
* because this is created just for this method call.
*
* @param name Name of the dataSource as defined in the dataSource tag
* @return a new DataSource instance as configured for the named entity
* @return a new DataSource instance
* @see org.apache.solr.handler.dataimport.DataSource
*/
public abstract DataSource getDataSource(String name);

View File

@ -63,7 +63,7 @@ public class ContextImpl extends Context {
}
public DataSource getDataSource(String name) {
return dataImporter.getDataSourceInstance(entity);
return dataImporter.getDataSourceInstance(entity, name, this);
}
public boolean isRootEntity() {

View File

@ -270,20 +270,20 @@ public class DataImporter {
key.dataSrc = new MockDataSource();
return;
}
key.dataSrc = getDataSourceInstance(key);
key.dataSrc = getDataSourceInstance(key, key.dataSource, null);
}
DataSource getDataSourceInstance(DataConfig.Entity key) {
Properties p = dataSourceProps.get(key.dataSource);
DataSource getDataSourceInstance(DataConfig.Entity key, String name, Context ctx ) {
Properties p = dataSourceProps.get(name);
if (p == null)
p = config.dataSources.get(key.dataSource);
p = config.dataSources.get(name);
if (p == null)
p = dataSourceProps.get(null);// for default data source
if (p == null)
p = config.dataSources.get(null);
if (p == null)
throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
"No dataSource :" + key.dataSource + " available for entity :"
"No dataSource :" + name + " available for entity :"
+ key.name);
String impl = p.getProperty(TYPE);
DataSource dataSrc = null;
@ -300,8 +300,10 @@ public class DataImporter {
try {
Properties copyProps = new Properties();
copyProps.putAll(p);
dataSrc.init(new ContextImpl(key, null, dataSrc, 0,
Collections.EMPTY_MAP, new HashMap(), null, this), copyProps);
if(ctx == null)
ctx = new ContextImpl(key, null, dataSrc, 0,
Collections.EMPTY_MAP, new HashMap(), null, this);
dataSrc.init(ctx, copyProps);
} catch (Exception e) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
"Failed to initialize DataSource: " + key.dataSource, e);

View File

@ -19,9 +19,11 @@ package org.apache.solr.handler.dataimport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* <p>
@ -65,4 +67,25 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
assertQ(req("id:1"), "//*[@numFound='1']");
}
@Test
@SuppressWarnings("unchecked")
public void testContext() throws Exception {
List rows = new ArrayList();
rows.add(createMap("id", "1", "desc", "one"));
MockDataSource.setIterator("select * from x", rows.iterator());
super.runFullImport(loadDataConfig("data-config-with-transformer.xml"));
}
public static class MockTransformer extends Transformer {
public Object transformRow(Map<String, Object> row, Context context) {
Assert.assertTrue("Context gave incorrect data source", context.getDataSource("mockDs") instanceof MockDataSource2);
return row;
}
}
public static class MockDataSource2 extends MockDataSource {
}
}

View File

@ -0,0 +1,10 @@
<dataConfig>
<dataSource type="MockDataSource" />
<dataSource name="mockDs" type="TestDocBuilder2$MockDataSource2" />
<document>
<entity name="x" query="select * from x" transformer="TestDocBuilder2$MockTransformer">
<field column="id" />
<field column="desc" />
</entity>
</document>
</dataConfig>