vgritsenko 2004/02/20 20:11:14
Modified: java/src/org/apache/xindice/core/indexer IndexManager.java
IndexPattern.java
Log:
Synchronize add / delete index operations
Revision Changes Path
1.29 +32 -34
xml-xindice/java/src/org/apache/xindice/core/indexer/IndexManager.java
Index: IndexManager.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/indexer/IndexManager.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- IndexManager.java 21 Feb 2004 02:11:23 -0000 1.28
+++ IndexManager.java 21 Feb 2004 04:11:14 -0000 1.29
@@ -30,6 +30,7 @@
import org.apache.xindice.util.ObjectStack;
import org.apache.xindice.util.SimpleConfigurable;
import org.apache.xindice.util.XindiceException;
+import org.apache.xindice.util.ReadOnlyException;
import org.apache.xindice.xml.SymbolTable;
import org.apache.xindice.xml.sax.CompressionHandler;
import org.apache.xindice.xml.sax.SAXEventGenerator;
@@ -86,7 +87,10 @@
private List newIndexers = new ArrayList(); // of IndexerInfo
/**
- * Create index manager for a collection
+ * Create IndexManager for a given collection
+ *
+ * @param collection Collection for this IndexManager
+ * @throws DBException if can't get collection's symbols
*/
public IndexManager(Collection collection) throws DBException {
this.collection = collection;
@@ -94,7 +98,9 @@
}
/**
- * Configure index manager, register all indexes
+ * Configure index manager, register all indexes specified in the
configuration
+ *
+ * @param config IndexManager configuration
*/
public void setConfig(Configuration config) throws XindiceException {
super.setConfig(config);
@@ -106,7 +112,7 @@
register(Class.forName(className), cfg);
} catch (Exception e) {
if (log.isWarnEnabled()) {
- log.warn("Failed to register index with class '" +
className + "' for collection " + collection.getCanonicalName(), e);
+ log.warn("Failed to register index with class '" +
className + "' for collection '" + collection.getCanonicalName() + "'", e);
}
}
}
@@ -119,7 +125,7 @@
*
* @return An array containing the Indexer names
*/
- public String[] list() {
+ public synchronized String[] list() {
return (String[]) indexes.keySet().toArray(EMPTY_STRINGS);
}
@@ -130,8 +136,7 @@
* @param name The Indexer to drop
* @return Whether or not the Indexer was dropped
*/
- public boolean drop(final String name) {
- Indexer idx = get(name);
+ public synchronized boolean drop(final String name) {
unregister(name);
config.processChildren(INDEX, new ConfigurationCallback() {
public void process(Configuration cfg) {
@@ -147,6 +152,7 @@
}
});
+ Indexer idx = get(name);
boolean res = false;
try {
res = idx.drop();
@@ -161,7 +167,7 @@
/**
* Drop all indexers
*/
- public void drop() {
+ public synchronized void drop() {
for (int i = 0; i < idxList.length; i++) {
drop(idxList[i].name);
}
@@ -174,35 +180,33 @@
* @param cfg The Indexer's configuration
* @return The Indexer that was created
*/
- public Indexer create(Configuration cfg) throws DBException {
- String className = cfg.getAttribute(CLASS);
+ public synchronized Indexer create(Configuration cfg) throws DBException
{
+ String name = cfg.getAttribute(NAME);
try {
// Check for duplicates
- String name = cfg.getAttribute(NAME);
Configuration[] cfgs = config.getChildren();
for (int i = 0; i < cfgs.length; i++) {
if (cfgs[i].getAttribute(NAME).equals(name)) {
- throw new DuplicateIndexException("Duplicate Index '" +
name + "'");
+ throw new DuplicateIndexException("Duplicate Index '" +
name + "' in collection '" + collection.getCanonicalName() + "'");
}
}
+ String className = cfg.getAttribute(CLASS);
+ Indexer idx = register(Class.forName(className), cfg);
config.add(cfg);
- Class c = Class.forName(className);
- Indexer idx = register(c, cfg);
-
return idx;
} catch (DBException e) {
throw e;
} catch (Exception e) {
- throw new CannotCreateException("Cannot create Index", e);
+ throw new CannotCreateException("Cannot create index '" + name +
"' in " + collection.getCanonicalName(), e);
}
}
/**
* Closes all indexers managed by this index manager.
*/
- public void close() {
+ public synchronized void close() {
for (int i = 0; i < idxList.length; i++) {
try {
idxList[i].indexer.close();
@@ -214,13 +218,13 @@
}
}
- public Indexer register(Class c, Configuration cfg) throws DBException {
+ public synchronized Indexer register(Class c, Configuration cfg) throws
DBException {
String name = null;
try {
Indexer idx = (Indexer) c.newInstance();
initialize(idx, cfg);
- name = idx.getName();
+ name = idx.getName();
if (name == null || name.trim().equals("")) {
throw new CannotCreateException("No name specified");
}
@@ -244,10 +248,7 @@
patternMap.put(pattern, info);
Map tbl = (Map) bestIndexers.get(style);
- if (tbl == null) {
- tbl = new WeakHashMap(); // FIXME: Review usage of
WeakHashMap
- bestIndexers.put(style, tbl);
- } else {
+ if (tbl != null) {
tbl.clear();
}
idxList = (IndexerInfo[])
indexes.values().toArray(EMPTY_INDEXERS);
@@ -256,21 +257,19 @@
} catch (DBException e) {
throw e;
} catch (Exception e) {
- throw new CannotCreateException("Cannot create Index '" + name +
"'", e);
+ throw new CannotCreateException("Cannot create Index '" + name +
"' in " + collection.getCanonicalName(), e);
}
}
- public void unregister(String name) {
+ public synchronized void unregister(String name) {
IndexerInfo idx = (IndexerInfo) indexes.remove(name);
String style = idx.style;
patternMap.remove(idx.pattern);
Map tbl = (Map) bestIndexers.get(style);
- if (tbl == null) {
- tbl = new WeakHashMap();
- bestIndexers.put(style, tbl);
+ if (tbl != null) {
+ tbl.clear();
}
- tbl.clear();
idxList = (IndexerInfo[]) indexes.values().toArray(EMPTY_INDEXERS);
}
@@ -353,7 +352,7 @@
* @param name The Indexer name
* @return The Indexer
*/
- public Indexer get(String name) {
+ public synchronized Indexer get(String name) {
IndexerInfo info = (IndexerInfo) indexes.get(name);
return info != null ? info.indexer : null;
}
@@ -369,9 +368,10 @@
public Indexer getBestIndexer(String style, IndexPattern pattern) {
Map tbl = (Map) bestIndexers.get(style);
if (tbl == null) {
- tbl = new WeakHashMap();
+ tbl = new WeakHashMap(); // FIXME: Review usage of WeakHashMap
bestIndexers.put(style, tbl);
}
+
Indexer idx = (Indexer) tbl.get(pattern);
if (idx == null) {
int highScore = 0;
@@ -425,7 +425,6 @@
/**
* IndexerInfo
*/
-
private class IndexerInfo {
public String name;
public String style;
@@ -445,7 +444,6 @@
* SAXHandler actually performs the work of adding and removing Indexer
* entries.
*/
-
private class SAXHandler implements ContentHandler, CompressionHandler {
private ObjectStack stack = new ObjectStack();
private IndexerInfo[] list;
1.13 +3 -2
xml-xindice/java/src/org/apache/xindice/core/indexer/IndexPattern.java
Index: IndexPattern.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/indexer/IndexPattern.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- IndexPattern.java 8 Feb 2004 02:50:21 -0000 1.12
+++ IndexPattern.java 21 Feb 2004 04:11:14 -0000 1.13
@@ -44,6 +44,7 @@
public static final int SCORE_NAME = 2;
public static final int SCORE_NATURAL = 3;
+
private SymbolTable symbols = null;
private String elemName = null;
private short elemID = PATTERN_NONE;