/// <summary>
        /// Constructor with the given index directory and callback to notify when the indexes were updated.
        /// </summary>
        /// <exception cref="System.IO.IOException"></exception>
        public IndexAndTaxonomyReplicationHandler(Directory indexDirectory, Directory taxonomyDirectory, Func <bool?> callback)
        {
            this.indexDirectory    = indexDirectory;
            this.taxonomyDirectory = taxonomyDirectory;
            this.callback          = callback;

            currentVersion       = null;
            currentRevisionFiles = null;

            bool indexExists    = DirectoryReader.IndexExists(indexDirectory);
            bool taxonomyExists = DirectoryReader.IndexExists(taxonomyDirectory);

            if (indexExists != taxonomyExists)
            {
                throw new InvalidOperationException(string.Format("search and taxonomy indexes must either both exist or not: index={0} taxo={1}", indexExists, taxonomyExists));
            }

            if (indexExists)
            {
                IndexCommit indexCommit    = IndexReplicationHandler.GetLastCommit(indexDirectory);
                IndexCommit taxonomyCommit = IndexReplicationHandler.GetLastCommit(taxonomyDirectory);

                currentRevisionFiles = IndexAndTaxonomyRevision.RevisionFiles(indexCommit, taxonomyCommit);
                currentVersion       = IndexAndTaxonomyRevision.RevisionVersion(indexCommit, taxonomyCommit);

                WriteToInfoStream(
                    string.Format("constructor(): currentVersion={0} currentRevisionFiles={1}", currentVersion, currentRevisionFiles),
                    string.Format("constructor(): indexCommit={0} taxoCommit={1}", indexCommit, taxonomyCommit));
            }
        }
Esempio n. 2
0
        public void TestSegmentsFileLast()
        {
            Directory         indexDir = NewDirectory();
            IndexWriterConfig conf     = new IndexWriterConfig(TEST_VERSION_CURRENT, null);

            conf.IndexDeletionPolicy = new SnapshotDeletionPolicy(conf.IndexDeletionPolicy);
            IndexWriter indexWriter = new IndexWriter(indexDir, conf);

            Directory taxoDir = NewDirectory();

            IndexAndTaxonomyRevision.SnapshotDirectoryTaxonomyWriter taxoWriter = new IndexAndTaxonomyRevision.SnapshotDirectoryTaxonomyWriter(taxoDir);
            try
            {
                indexWriter.AddDocument(NewDocument(taxoWriter));
                indexWriter.Commit();
                taxoWriter.Commit();
                IRevision rev         = new IndexAndTaxonomyRevision(indexWriter, taxoWriter);
                var       sourceFiles = rev.SourceFiles;
                assertEquals(2, sourceFiles.Count);
                foreach (var files in sourceFiles.Values)
                {
                    string lastFile = files.Last().FileName;
                    assertTrue(lastFile.StartsWith(IndexFileNames.SEGMENTS, StringComparison.Ordinal) && !lastFile.Equals(IndexFileNames.SEGMENTS_GEN, StringComparison.Ordinal));
                }
            }
            finally
            {
                IOUtils.Dispose(indexWriter, taxoWriter, taxoDir, indexDir);
            }
        }
Esempio n. 3
0
        public void TestRevisionRelease()
        {
            Directory         indexDir = NewDirectory();
            IndexWriterConfig conf     = new IndexWriterConfig(TEST_VERSION_CURRENT, null);

            conf.IndexDeletionPolicy = new SnapshotDeletionPolicy(conf.IndexDeletionPolicy);
            IndexWriter indexWriter = new IndexWriter(indexDir, conf);

            Directory taxoDir = NewDirectory();

            IndexAndTaxonomyRevision.SnapshotDirectoryTaxonomyWriter taxoWriter = new IndexAndTaxonomyRevision.SnapshotDirectoryTaxonomyWriter(taxoDir);
            try
            {
                indexWriter.AddDocument(NewDocument(taxoWriter));
                indexWriter.Commit();
                taxoWriter.Commit();
                IRevision rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter);
                // releasing that revision should not delete the files
                rev1.Release();
                assertTrue(SlowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1"));
                assertTrue(SlowFileExists(taxoDir, IndexFileNames.SEGMENTS + "_1"));

                rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter); // create revision again, so the files are snapshotted
                indexWriter.AddDocument(NewDocument(taxoWriter));
                indexWriter.Commit();
                taxoWriter.Commit();
                assertNotNull(new IndexAndTaxonomyRevision(indexWriter, taxoWriter));
                rev1.Release(); // this release should trigger the delete of segments_1
                assertFalse(SlowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1"));
            }
            finally
            {
                IOUtils.Dispose(indexWriter, taxoWriter, taxoDir, indexDir);
            }
        }
        public void TestOpen()
        {
            Directory         indexDir = NewDirectory();
            IndexWriterConfig conf     = new IndexWriterConfig(TEST_VERSION_CURRENT, null);

            conf.IndexDeletionPolicy = new SnapshotDeletionPolicy(conf.IndexDeletionPolicy);
            IndexWriter indexWriter = new IndexWriter(indexDir, conf);

            Directory taxoDir = NewDirectory();

            IndexAndTaxonomyRevision.SnapshotDirectoryTaxonomyWriter taxoWriter = new IndexAndTaxonomyRevision.SnapshotDirectoryTaxonomyWriter(taxoDir);
            try
            {
                indexWriter.AddDocument(NewDocument(taxoWriter));
                indexWriter.Commit();
                taxoWriter.Commit();
                IRevision rev = new IndexAndTaxonomyRevision(indexWriter, taxoWriter);
                foreach (var e in rev.SourceFiles)
                {
                    string    source = e.Key;
                    Directory dir    = source.Equals(IndexAndTaxonomyRevision.INDEX_SOURCE, StringComparison.Ordinal) ? indexDir : taxoDir;
                    foreach (RevisionFile file in e.Value)
                    {
                        using (IndexInput src = dir.OpenInput(file.FileName, IOContext.READ_ONCE))
                            using (System.IO.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);
                            }
                    }
                }
            }
            finally
            {
                IOUtils.Dispose(indexWriter, taxoWriter, taxoDir, indexDir);
            }
        }
Esempio n. 5
0
        public virtual int CompareTo(IRevision other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            IndexAndTaxonomyRevision itr = other as IndexAndTaxonomyRevision;

            if (itr == null)
            {
                throw new ArgumentException(string.Format("Cannot compare IndexAndTaxonomyRevision to a {0}", other.GetType()), "other");
            }

            int cmp = indexCommit.CompareTo(itr.indexCommit);

            return(cmp != 0 ? cmp : taxonomyCommit.CompareTo(itr.taxonomyCommit));
        }