[exo-jcr-commits] exo-jcr SVN: r1147 - jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene.
do-not-reply at jboss.org
do-not-reply at jboss.org
Tue Dec 22 11:31:08 EST 2009
Author: nzamosenchuk
Date: 2009-12-22 11:31:07 -0500 (Tue, 22 Dec 2009)
New Revision: 1147
Modified:
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexInfos.java
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java
Log:
EXOJCR-327: changed visibility of IndexInfos's methods from "default" to public because other implementations may be situated in other packages. MultiIndex class is extended with new method, used to update indexLists.
Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexInfos.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexInfos.java 2009-12-22 16:01:28 UTC (rev 1146)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexInfos.java 2009-12-22 16:31:07 UTC (rev 1147)
@@ -33,191 +33,231 @@
/**
* Stores a sequence of index names.
*/
-class IndexInfos {
+class IndexInfos
+{
- /**
- * For new segment names.
- */
- private int counter = 0;
+ /**
+ * For new segment names.
+ */
+ protected int counter = 0;
- /**
- * Flag that indicates if index infos needs to be written to disk.
- */
- private boolean dirty = false;
+ /**
+ * Flag that indicates if index infos needs to be written to disk.
+ */
+ protected boolean dirty = false;
- /**
- * List of index names
- */
- private List indexes = new ArrayList();
+ /**
+ * List of index names
+ */
+ protected List indexes = new ArrayList();
- /**
- * Set of names for quick lookup.
- */
- private Set names = new HashSet();
+ /**
+ * Set of names for quick lookup.
+ */
+ protected Set names = new HashSet();
- /**
- * Name of the file where the infos are stored.
- */
- private final String name;
+ /**
+ * Name of the file where the infos are stored.
+ */
+ protected final String name;
- /**
- * Creates a new IndexInfos using <code>fileName</code>.
- *
- * @param fileName the name of the file where infos are stored.
- */
- IndexInfos(String fileName) {
- this.name = fileName;
- }
+ /**
+ * Used to call-back, when index list is changed
+ */
+ protected MultiIndex multiIndex = null;
- /**
- * Returns <code>true</code> if this index infos exists in
- * <code>dir</code>.
- *
- * @param dir the directory where to look for the index infos.
- * @return <code>true</code> if it exists; <code>false</code> otherwise.
- * @throws IOException if an error occurs while reading from the directory.
- */
- boolean exists(Directory dir) throws IOException {
- return dir.fileExists(name);
- }
+ /**
+ * Creates a new IndexInfos using <code>fileName</code>.
+ *
+ * @param fileName the name of the file where infos are stored.
+ */
+ public IndexInfos(String fileName)
+ {
+ this.name = fileName;
+ }
- /**
- * Returns the name of the file where infos are stored.
- *
- * @return the name of the file where infos are stored.
- */
- String getFileName() {
- return name;
- }
+ /**
+ * Returns <code>true</code> if this index infos exists in
+ * <code>dir</code>.
+ *
+ * @param dir the directory where to look for the index infos.
+ * @return <code>true</code> if it exists; <code>false</code> otherwise.
+ * @throws IOException if an error occurs while reading from the directory.
+ */
+ public boolean exists(Directory dir) throws IOException
+ {
+ return dir.fileExists(name);
+ }
- /**
- * Reads the index infos.
- *
- * @param dir the directory from where to read the index infos.
- * @throws IOException if an error occurs.
- */
- void read(Directory dir) throws IOException {
- InputStream in = new IndexInputStream(dir.openInput(name));
- try {
- DataInputStream di = new DataInputStream(in);
- counter = di.readInt();
- for (int i = di.readInt(); i > 0; i--) {
- String indexName = di.readUTF();
- indexes.add(indexName);
- names.add(indexName);
- }
- } finally {
- in.close();
- }
- }
+ /**
+ * Returns the name of the file where infos are stored.
+ *
+ * @return the name of the file where infos are stored.
+ */
+ public String getFileName()
+ {
+ return name;
+ }
- /**
- * Writes the index infos to disk if they are dirty.
- *
- * @param dir the directory where to write the index infos.
- * @throws IOException if an error occurs.
- */
- void write(Directory dir) throws IOException {
- // do not write if not dirty
- if (!dirty) {
- return;
- }
+ /**
+ * Reads the index infos.
+ *
+ * @param dir the directory from where to read the index infos.
+ * @throws IOException if an error occurs.
+ */
+ public void read(Directory dir) throws IOException
+ {
+ InputStream in = new IndexInputStream(dir.openInput(name));
+ try
+ {
+ DataInputStream di = new DataInputStream(in);
+ counter = di.readInt();
+ for (int i = di.readInt(); i > 0; i--)
+ {
+ String indexName = di.readUTF();
+ indexes.add(indexName);
+ names.add(indexName);
+ }
+ }
+ finally
+ {
+ in.close();
+ }
+ }
- OutputStream out = new IndexOutputStream(dir.createOutput(name + ".new"));
- try {
- DataOutputStream dataOut = new DataOutputStream(out);
- dataOut.writeInt(counter);
- dataOut.writeInt(indexes.size());
- for (int i = 0; i < indexes.size(); i++) {
- dataOut.writeUTF(getName(i));
- }
- } finally {
- out.close();
- }
- // delete old
- if (dir.fileExists(name)) {
- dir.deleteFile(name);
- }
- dir.renameFile(name + ".new", name);
- dirty = false;
- }
+ /**
+ * Writes the index infos to disk if they are dirty.
+ *
+ * @param dir the directory where to write the index infos.
+ * @throws IOException if an error occurs.
+ */
+ public void write(Directory dir) throws IOException
+ {
+ // do not write if not dirty
+ if (!dirty)
+ {
+ return;
+ }
- /**
- * Returns the index name at position <code>i</code>.
- * @param i the position.
- * @return the index name.
- */
- String getName(int i) {
- return (String) indexes.get(i);
- }
+ OutputStream out = new IndexOutputStream(dir.createOutput(name + ".new"));
+ try
+ {
+ DataOutputStream dataOut = new DataOutputStream(out);
+ dataOut.writeInt(counter);
+ dataOut.writeInt(indexes.size());
+ for (int i = 0; i < indexes.size(); i++)
+ {
+ dataOut.writeUTF(getName(i));
+ }
+ }
+ finally
+ {
+ out.close();
+ }
+ // delete old
+ if (dir.fileExists(name))
+ {
+ dir.deleteFile(name);
+ }
+ dir.renameFile(name + ".new", name);
+ dirty = false;
+ }
- /**
- * Returns the index name at position <code>i</code>.
- * @param i the position.
- * @return the index name.
- */
- Set<String> getNames() {
- return new HashSet<String>(indexes);
- }
-
- /**
- * Returns the number of index names.
- * @return the number of index names.
- */
- int size() {
- return indexes.size();
- }
+ /**
+ * Returns the index name at position <code>i</code>.
+ * @param i the position.
+ * @return the index name.
+ */
+ public String getName(int i)
+ {
+ return (String)indexes.get(i);
+ }
- /**
- * Adds a name to the index infos.
- * @param name the name to add.
- */
- void addName(String name) {
- if (names.contains(name)) {
- throw new IllegalArgumentException("already contains: " + name);
- }
- indexes.add(name);
- names.add(name);
- dirty = true;
- }
+ /**
+ * Returns the index name at position <code>i</code>.
+ * @param i the position.
+ * @return the index name.
+ */
+ public Set<String> getNames()
+ {
+ return new HashSet<String>(indexes);
+ }
- /**
- * Removes the name from the index infos.
- * @param name the name to remove.
- */
- void removeName(String name) {
- indexes.remove(name);
- names.remove(name);
- dirty = true;
- }
+ /**
+ * Returns the number of index names.
+ * @return the number of index names.
+ */
+ public int size()
+ {
+ return indexes.size();
+ }
- /**
- * Removes the name from the index infos.
- * @param i the position.
- */
- void removeName(int i) {
- Object name = indexes.remove(i);
- names.remove(name);
- dirty = true;
- }
+ /**
+ * Adds a name to the index infos.
+ * @param name the name to add.
+ */
+ public void addName(String name)
+ {
+ if (names.contains(name))
+ {
+ throw new IllegalArgumentException("already contains: " + name);
+ }
+ indexes.add(name);
+ names.add(name);
+ dirty = true;
+ }
- /**
- * Returns <code>true</code> if <code>name</code> exists in this
- * <code>IndexInfos</code>; <code>false</code> otherwise.
- *
- * @param name the name to test existence.
- * @return <code>true</code> it is exists in this <code>IndexInfos</code>.
- */
- boolean contains(String name) {
- return names.contains(name);
- }
+ /**
+ * Removes the name from the index infos.
+ * @param name the name to remove.
+ */
+ public void removeName(String name)
+ {
+ indexes.remove(name);
+ names.remove(name);
+ dirty = true;
+ }
- /**
- * Returns a new unique name for an index folder.
- * @return a new unique name for an index folder.
- */
- String newName() {
- dirty = true;
- return "_" + Integer.toString(counter++, Character.MAX_RADIX);
- }
+ /**
+ * Removes the name from the index infos.
+ * @param i the position.
+ */
+ public void removeName(int i)
+ {
+ Object name = indexes.remove(i);
+ names.remove(name);
+ dirty = true;
+ }
+
+ /**
+ * Returns <code>true</code> if <code>name</code> exists in this
+ * <code>IndexInfos</code>; <code>false</code> otherwise.
+ *
+ * @param name the name to test existence.
+ * @return <code>true</code> it is exists in this <code>IndexInfos</code>.
+ */
+ public boolean contains(String name)
+ {
+ return names.contains(name);
+ }
+
+ /**
+ * Returns a new unique name for an index folder.
+ * @return a new unique name for an index folder.
+ */
+ public String newName()
+ {
+ dirty = true;
+ return "_" + Integer.toString(counter++, Character.MAX_RADIX);
+ }
+
+ /**
+ * Injects MultiIndex instance that is used to callback, when list of indexes if changed.
+ * Default implementation (IndexInfos class) actually doesn't use it.
+ * @param multiIndex
+ */
+ public void setMultiIndex(MultiIndex multiIndex)
+ {
+ this.multiIndex = multiIndex;
+ }
}
Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java 2009-12-22 16:01:28 UTC (rev 1146)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java 2009-12-22 16:31:07 UTC (rev 1147)
@@ -835,7 +835,7 @@
for (int i = 0; i < indexes.size(); i++)
{
PersistentIndex pIdx = (PersistentIndex)indexes.get(i);
-
+
if (indexNames.contains(pIdx.getName()))
{
try
@@ -844,9 +844,10 @@
}
catch (FileNotFoundException e)
{
- if(directoryManager.hasDirectory(pIdx.getName())){
- throw e;
- }
+ if (directoryManager.hasDirectory(pIdx.getName()))
+ {
+ throw e;
+ }
}
}
}
@@ -2417,8 +2418,12 @@
/**
* Temporary solution for indexer in cluster. This method re-reads list of indexes from FS.
- * @throws IOException
+ * @Deprecated, because new implementation is in the development. IndexInfos class will now callback
+ * method {@link MultiIndex#refreshIndexList()}. Will be removed, when jBossCacheIndexInfos is
+ * introduced.
+ * @throws IOException
*/
+ @Deprecated
protected void refreshIndexesList() throws IOException
{
log.info("Refreshing list of indexes...");
@@ -2436,7 +2441,7 @@
Set<String> added = new HashSet<String>(newIndexNames.getNames());
added.removeAll(indexNames.getNames());
-
+
// remove removed indexes
Iterator<PersistentIndex> iterator = indexes.iterator();
while (iterator.hasNext())
@@ -2471,4 +2476,63 @@
}
indexNames = newIndexNames;
}
+
+ /**
+ * Refresh list of indexes. Used to be called from IndexInfos. New, actual list is read from
+ * IndexInfos.
+ * @throws IOException
+ */
+ public void refreshIndexList() throws IOException
+ {
+ // TODO: re-study synchronization here.
+ synchronized (updateMonitor)
+ {
+ log.info("Refreshing list of indexes...");
+ // release reader if any
+ releaseMultiReader();
+ // prepare added/removed sets
+ Set<String> newList = new HashSet<String>(indexNames.getNames());
+
+ // remove removed indexes
+ Iterator<PersistentIndex> iterator = indexes.iterator();
+ while (iterator.hasNext())
+ {
+ PersistentIndex index = iterator.next();
+ String name = ((PersistentIndex)index).getName();
+ // if current index not in new list, close it, cause it is deleted.
+ if (!newList.contains(name))
+ {
+ ((PersistentIndex)index).close();
+ iterator.remove();
+ }
+ else
+ {
+ // remove from list, cause this segment of index still present and indexes list contains
+ // PersistentIndex instance related to this index..
+ newList.remove(name);
+ }
+ }
+ // now newList contains ONLY new, added indexes, deleted indexes, are removed from list.
+ for (String name : newList)
+ {
+ // only open if it still exists
+ // it is possible that indexNames still contains a name for
+ // an index that has been deleted, but indexNames has not been
+ // written to disk.
+ if (!directoryManager.hasDirectory(name))
+ {
+ log.debug("index does not exist anymore: " + name);
+ // move on to next index
+ continue;
+ }
+ PersistentIndex index =
+ new PersistentIndex(name, handler.getTextAnalyzer(), handler.getSimilarity(), cache, indexingQueue,
+ directoryManager);
+ index.setMaxFieldLength(handler.getMaxFieldLength());
+ index.setUseCompoundFile(handler.getUseCompoundFile());
+ index.setTermInfosIndexDivisor(handler.getTermInfosIndexDivisor());
+ indexes.add(index);
+ }
+ }
+ }
}
More information about the exo-jcr-commits
mailing list