lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Otis Gospodnetic <otis_gospodne...@yahoo.com>
Subject Re: patch: adding IndexReader.isCurrent()
Date Fri, 03 Jun 2005 21:18:59 GMT
Looks good to me.  You may also want to add that instance method to
IndexReader that somebody asked for the other day...

Here:

"Hello,

Am I missing an obvious way to return version of already opened index?

Static method IndexReader.getCurrentVersion() uses SegmentInfos to
read version number from "segments" files. This returns current
version of index in Directory but I need a reliable way to obtain
version of IndexReader.

This can be solved using some locking in higher level API. However,
all what's actually needed is a simple method addition to IndexReader:

  /**
   * Version number when this IndexReader was opened.
   */
  public long getVersion() {
    return segmentInfos.getVersion();
  }

that just passes version from segmentInfos instance used by
IndexReader. Is there a commiter willing to make such change please?

BTW, SigmentInfos.readCurrentVersion() reads ''segments" without
locking (COMMIT_LOCK). Is there a good reason or just an omission?

Best regards,
Radomir
"



Otis


--- Daniel Naber <lucenelist@danielnaber.de> wrote:

> Hi,
> 
> a patch similar to this was proposed a year ago but then got lost. I
> will 
> apply it unless someone objects. It fixes the problem that you cannot
> 
> safely use IndexReader.getCurrentVersion() to detect changes, as the 
> version number is reset to 0 if the index is re-created.
> 
> Also see 
>
http://www.mail-archive.com/lucene-dev%40jakarta.apache.org/msg06143.html
> 
> Regards
>  Daniel
> 
> -- 
> http://www.danielnaber.de
> > Index:
>
/home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/IndexReader.java
> ===================================================================
> ---
>
/home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/IndexReader.java
> (Revision 178894)
> +++
>
/home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/IndexReader.java
> (Arbeitskopie)
> @@ -224,6 +224,7 @@
>     * @param directory where the index resides.
>     * @return version number.
>     * @throws IOException if segments file cannot be read
> +   * @deprecated use {@link #isCurrent()} instead
>     */
>    public static long getCurrentVersion(String directory) throws
> IOException {
>      return getCurrentVersion(new File(directory));
> @@ -236,6 +237,7 @@
>     * @param directory where the index resides.
>     * @return version number.
>     * @throws IOException if segments file cannot be read
> +   * @deprecated use {@link #isCurrent()} instead
>     */
>    public static long getCurrentVersion(File directory) throws
> IOException {
>      Directory dir = FSDirectory.getDirectory(directory, false);
> @@ -251,12 +253,27 @@
>     * @param directory where the index resides.
>     * @return version number.
>     * @throws IOException if segments file cannot be read.
> +   * @deprecated use {@link #isCurrent()} instead
>     */
>    public static long getCurrentVersion(Directory directory) throws
> IOException {
>      return SegmentInfos.readCurrentVersion(directory);
>    }
>  
>    /**
> +   * Check whether this IndexReader still works on a current version
> of the index.
> +   * If this is not the case you will need to re-open the
> IndexReader to
> +   * make sure you see the latest changes made to the index.
> +   * 
> +   * @throws IOException
> +   */
> +  public boolean isCurrent() throws IOException {
> +    if (SegmentInfos.readCurrentVersion(directory) >
> segmentInfos.getVersion()) {
> +      return false;
> +    }
> +    return true;
> +  }
> +
> +  /**
>     *  Return an array of term frequency vectors for the specified
> document.
>     *  The array contains a vector for each vectorized field in the
> document.
>     *  Each vector contains terms and frequencies for all terms in a
> given vectorized field.
> Index:
>
/home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/SegmentInfos.java
> ===================================================================
> ---
>
/home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/SegmentInfos.java
> (Revision 171375)
> +++
>
/home/dnaber/workspace/LuceneSVN/src/java/org/apache/lucene/index/SegmentInfos.java
> (Arbeitskopie)
> @@ -29,7 +29,11 @@
>    public static final int FORMAT = -1;
>    
>    public int counter = 0;    // used to name new segments
> -  private long version = 0; //counts how often the index has been
> changed by adding or deleting docs
> +  /**
> +   * counts how often the index has been changed by adding or
> deleting docs.
> +   * starting with the current time in milliseconds forces to create
> unique version numbers.
> +   */
> +  private long version = System.currentTimeMillis();
>  
>    public final SegmentInfo info(int i) {
>      return (SegmentInfo) elementAt(i);
> @@ -59,7 +63,7 @@
>        
>        if(format >= 0){    // in old format the version number may be
> at the end of the file
>          if (input.getFilePointer() >= input.length())
> -          version = 0; // old file format without version number
> +          version = System.currentTimeMillis(); // old file format
> without version number
>          else
>            version = input.readLong(); // read version
>        }
> 
> >
---------------------------------------------------------------------
> To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-dev-help@lucene.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message