mogui commented on a change in pull request #679: URL: https://github.com/apache/lucene/pull/679#discussion_r811871746
########## File path: lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorReadonly.java ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.monitor; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Collections; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.core.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexNotFoundException; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.store.FSDirectory; +import org.junit.Test; + +public class TestMonitorReadonly extends MonitorTestBase { + private static final Analyzer ANALYZER = new WhitespaceAnalyzer(); + + @Test + public void testReadonlyMonitorThrowsOnInexistentIndex() { + Path indexDirectory = createTempDir(); + MonitorConfiguration config = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse), + true); + assertThrows( + IndexNotFoundException.class, + () -> { + new Monitor(ANALYZER, config); + }); + } + + @Test + public void testReadonlyMonitorThrowsWhenCallingWriteRequests() throws IOException { + Path indexDirectory = createTempDir(); + MonitorConfiguration writeConfig = + new MonitorConfiguration() + .setIndexPath( + indexDirectory, MonitorQuerySerializer.fromParser(MonitorTestBase::parse)); + + // this will create the index + Monitor writeMonitor = new Monitor(ANALYZER, writeConfig); + writeMonitor.close(); + + MonitorConfiguration config = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse), + true); + try (Monitor monitor = new Monitor(ANALYZER, config)) { + assertThrows( + IllegalStateException.class, + () -> { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + monitor.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + }); + + assertThrows( + IllegalStateException.class, + () -> { + monitor.deleteById("query1"); + }); + + assertThrows( + IllegalStateException.class, + () -> { + monitor.clear(); + }); + } + } + + @Test + public void testSettingCustomDirectory() throws IOException { + Path indexDirectory = createTempDir(); + Document doc = new Document(); + doc.add(newTextField(FIELD, "This is a Foobar test document", Field.Store.NO)); + + MonitorConfiguration writeConfig = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse)); + + try (Monitor writeMonitor = new Monitor(ANALYZER, writeConfig)) { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + writeMonitor.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + TermQuery query2 = new TermQuery(new Term(FIELD, "Foobar")); + writeMonitor.register( + new MonitorQuery("query2", query2, query.toString(), Collections.emptyMap())); + MatchingQueries<QueryMatch> matches = writeMonitor.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches.getMatches()); + assertEquals(2, matches.getMatchCount()); + assertNotNull(matches.matches("query2")); + } + } + + public void testMonitorReadOnlyCouldReadOnTheSameIndex() throws IOException { + Path indexDirectory = createTempDir(); + Document doc = new Document(); + doc.add(newTextField(FIELD, "This is a test document", Field.Store.NO)); + + MonitorConfiguration writeConfig = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse)); + + try (Monitor writeMonitor = new Monitor(ANALYZER, writeConfig)) { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + writeMonitor.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + } + + MonitorConfiguration readConfig = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse), + true); + + try (Monitor readMonitor1 = new Monitor(ANALYZER, readConfig)) { + MatchingQueries<QueryMatch> matches = readMonitor1.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches.getMatches()); + assertEquals(1, matches.getMatchCount()); + assertNotNull(matches.matches("query1")); + } + + try (Monitor readMonitor2 = new Monitor(ANALYZER, readConfig)) { + MatchingQueries<QueryMatch> matches = readMonitor2.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches.getMatches()); + assertEquals(1, matches.getMatchCount()); + assertNotNull(matches.matches("query1")); + + assertThrows( + IllegalStateException.class, + () -> { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + readMonitor2.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + }); + } + } Review comment: Hi @romseygeek I am doing it, but indeed I cannot make it work, I am missing something? No change in monitor before purging it's ok, but after a purge still no changes, I wasn' calling maybeRefresh on the searchManager but also calling it before purging, I have still satale search results. What am I doing wrong ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org