public void TestRevisionRelease()
        {
            Directory dir = NewDirectory();
            IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null);
            conf.IndexDeletionPolicy = new SnapshotDeletionPolicy(conf.IndexDeletionPolicy);
            IndexWriter writer = new IndexWriter(dir, conf);
            try
            {
                writer.AddDocument(new Document());
                writer.Commit();
                IRevision rev1 = new IndexRevision(writer);
                // releasing that revision should not delete the files
                rev1.Release();
                assertTrue(SlowFileExists(dir, IndexFileNames.SEGMENTS + "_1"));

                rev1 = new IndexRevision(writer); // create revision again, so the files are snapshotted
                writer.AddDocument(new Document());
                writer.Commit();
                assertNotNull(new IndexRevision(writer));
                rev1.Release(); // this release should trigger the delete of segments_1
                assertFalse(SlowFileExists(dir, IndexFileNames.SEGMENTS + "_1"));
            }
            finally
            {
                IOUtils.Dispose(writer, dir);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Returns a map of the revision files from the given <see cref="IndexCommit"/>s of the search and taxonomy indexes.
 /// </summary>
 /// <exception cref="IOException"></exception>
 public static IDictionary <string, IList <RevisionFile> > RevisionFiles(IndexCommit indexCommit, IndexCommit taxonomyCommit)
 {
     return(new Dictionary <string, IList <RevisionFile> > {
         { INDEX_SOURCE, IndexRevision.RevisionFiles(indexCommit).Values.First() },
         { TAXONOMY_SOURCE, IndexRevision.RevisionFiles(taxonomyCommit).Values.First() }
     });
 }
Beispiel #3
0
        public void TestSerialization()
        {
            Directory         directory = NewDirectory();
            IndexWriterConfig config    = new IndexWriterConfig(TEST_VERSION_CURRENT, null);

            config.IndexDeletionPolicy = new SnapshotDeletionPolicy(config.IndexDeletionPolicy);

            IndexWriter writer = new IndexWriter(directory, config);

            writer.AddDocument(new Document());
            writer.Commit();
            IRevision revision = new IndexRevision(writer);

            SessionToken session1 = new SessionToken("17", revision);
            MemoryStream baos     = new MemoryStream();

            session1.Serialize(new DataOutputStream(baos));
            byte[] b = baos.ToArray();

            SessionToken session2 = new SessionToken(new DataInputStream(new MemoryStream(b)));

            assertEquals(session1.Id, session2.Id);
            assertEquals(session1.Version, session2.Version);
            assertEquals(1, session2.SourceFiles.Count);
            assertEquals(session1.SourceFiles.Count, session2.SourceFiles.Count);
            assertEquals(session1.SourceFiles.Keys, session2.SourceFiles.Keys);
            IList <RevisionFile> files1 = session1.SourceFiles.Values.First();
            IList <RevisionFile> files2 = session2.SourceFiles.Values.First();

            assertEquals(files1, files2);

            IOUtils.Dispose(writer, directory);
        }
Beispiel #4
0
        public virtual int CompareTo(IRevision other)
        {
            //TODO: This breaks the contract and will fail if called with a different implementation
            //      This is a flaw inherited from the original source...
            //      It should at least provide a better description to the InvalidCastException
            IndexRevision or = (IndexRevision)other;

            return(commit.CompareTo(or.commit));
        }
        public void TestPublishOlderRev()
        {
            replicator.Publish(CreateRevision(1));
            IRevision old = new IndexRevision(sourceWriter);

            replicator.Publish(CreateRevision(2));
            try
            {
                replicator.Publish(old);
                fail("should have failed to publish an older revision");
            }
            catch (ArgumentException)
            {
                // expected
            }
            assertEquals(1, DirectoryReader.ListCommits(sourceDirectory).size());
        }
        public void TestOpen()
        {
            Directory         dir  = NewDirectory();
            IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null);

            conf.IndexDeletionPolicy = new SnapshotDeletionPolicy(conf.IndexDeletionPolicy);
            IndexWriter writer = new IndexWriter(dir, conf);

            try
            {
                writer.AddDocument(new Document());
                writer.Commit();
                IRevision rev         = new IndexRevision(writer);
                var       sourceFiles = rev.SourceFiles;
                string    source      = sourceFiles.Keys.First();
                foreach (RevisionFile file in sourceFiles.Values.First())
                {
                    IndexInput src = dir.OpenInput(file.FileName, IOContext.READ_ONCE);
                    Stream     @in = rev.Open(source, file.FileName);
                    assertEquals(src.Length, @in.Length);
                    byte[] srcBytes = new byte[(int)src.Length];
                    byte[] inBytes  = new byte[(int)src.Length];
                    int    offset   = 0;
                    if (Random.nextBoolean())
                    {
                        int skip = Random.Next(10);
                        if (skip >= src.Length)
                        {
                            skip = 0;
                        }
                        @in.Seek(skip, SeekOrigin.Current);
                        src.Seek(skip);
                        offset = skip;
                    }
                    src.ReadBytes(srcBytes, offset, srcBytes.Length - offset);
                    @in.Read(inBytes, offset, inBytes.Length - offset);
                    assertArrayEquals(srcBytes, inBytes);
                    IOUtils.Dispose(src, @in);
                }
            }
            finally
            {
                IOUtils.Dispose(writer, dir);
            }
        }
        /// <summary>
        /// Constructor with the given index directory and callback to notify when the
        /// indexes were updated.
        /// </summary>
        public IndexReplicationHandler(Directory indexDirectory, Func <bool?> callback) // LUCENENET TODO: API - shouldn't this be Action ?
        {
            this.InfoStream     = InfoStream.Default;
            this.callback       = callback;
            this.indexDirectory = indexDirectory;

            currentVersion       = null;
            currentRevisionFiles = null;

            if (DirectoryReader.IndexExists(indexDirectory))
            {
                IList <IndexCommit> commits = DirectoryReader.ListCommits(indexDirectory);
                IndexCommit         commit  = commits[commits.Count - 1];

                currentVersion       = IndexRevision.RevisionVersion(commit);
                currentRevisionFiles = IndexRevision.RevisionFiles(commit);

                WriteToInfoStream(
                    string.Format("constructor(): currentVersion={0} currentRevisionFiles={1}", currentVersion, currentRevisionFiles),
                    string.Format("constructor(): commit={0}", commit));
            }
        }
 public void TestSegmentsFileLast()
 {
     Directory dir = NewDirectory();
     IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null);
     conf.IndexDeletionPolicy = new SnapshotDeletionPolicy(conf.IndexDeletionPolicy);
     IndexWriter writer = new IndexWriter(dir, conf);
     try
     {
         writer.AddDocument(new Document());
         writer.Commit();
         IRevision rev = new IndexRevision(writer);
         var sourceFiles = rev.SourceFiles;
         assertEquals(1, sourceFiles.Count);
         var files = sourceFiles.Values.First();
         string lastFile = files.Last().FileName;
         assertTrue(lastFile.StartsWith(IndexFileNames.SEGMENTS, StringComparison.Ordinal) && !lastFile.Equals(IndexFileNames.SEGMENTS_GEN, StringComparison.Ordinal));
     }
     finally
     {
         IOUtils.Dispose(writer, dir);
     }
 }
Beispiel #9
0
        public void TestSerialization()
        {
            Directory         directory = NewDirectory();
            IndexWriterConfig config    = new IndexWriterConfig(TEST_VERSION_CURRENT, null);

            config.IndexDeletionPolicy = new SnapshotDeletionPolicy(config.IndexDeletionPolicy);

            IndexWriter writer = new IndexWriter(directory, config);

            writer.AddDocument(new Document());
            writer.Commit();
            IRevision revision = new IndexRevision(writer);

            SessionToken session1 = new SessionToken("17", revision);
            MemoryStream baos     = new MemoryStream();

            session1.Serialize(new DataOutputStream(baos));
            byte[] b = baos.ToArray();

            SessionToken session2 = new SessionToken(new DataInputStream(new MemoryStream(b)));

            assertEquals(session1.Id, session2.Id);
            assertEquals(session1.Version, session2.Version);
            assertEquals(1, session2.SourceFiles.Count);
            assertEquals(session1.SourceFiles.Count, session2.SourceFiles.Count);

            // LUCENENET: Collections don't compare automatically in .NET and J2N has no structural equality
            // checking on Keys, so using CollectionAssert here. This is set
            // equality (where order doesn't matter) because in Java the keys and values collections are sets.
            CollectionAssert.AreEquivalent(session1.SourceFiles.Keys, session2.SourceFiles.Keys);
            IList <RevisionFile> files1 = session1.SourceFiles.Values.First();
            IList <RevisionFile> files2 = session2.SourceFiles.Values.First();

            assertEquals(files1, files2, aggressive: false);

            IOUtils.Dispose(writer, directory);
        }