From a26a40b5a8b45fd12fdf4e10309f98fab2ecd285 Mon Sep 17 00:00:00 2001 From: Mikhaylo Demianenko Date: Fri, 29 Jul 2016 17:35:43 +0200 Subject: [PATCH] Cleanups, additional tests for read only cases and pieces of read only infrastructure. --- .../api/impl/index/AbstractLuceneIndex.java | 3 +- .../kernel/api/impl/index/LuceneIndex.java | 3 +- .../index/backup/LuceneIndexSnapshots.java | 11 +- .../backup/SnapshotReleaseException.java | 4 +- .../UnsupportedIndexDeletionPolicy.java | 4 +- ...=> WritableIndexSnapshotFileIterator.java} | 4 +- .../partition/ReadOnlyIndexPartition.java | 2 +- ...eadOnlyIndexSnapshotFileIteratorTest.java} | 45 +++++--- ...WritableIndexSnapshotFileIteratorTest.java | 38 +++++++ .../partition/IndexPartitionFactoryTest.java | 100 ++++++++++++++++++ .../LuceneLabelScanIndexBuilderTest.java | 76 +++++++++++++ .../labelscan/LuceneLabelScanStoreTest.java | 18 ++-- .../ReadOnlyLuceneLabelScanIndexTest.java | 87 +++++++++++++++ .../schema/LuceneSchemaIndexBuilderTest.java | 77 ++++++++++++++ .../schema/ReadOnlyLuceneSchemaIndexTest.java | 99 +++++++++++++++++ 15 files changed, 534 insertions(+), 37 deletions(-) rename community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/{LuceneIndexSnapshotFileIterator.java => WritableIndexSnapshotFileIterator.java} (90%) rename community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/{LuceneIndexSnapshotFileIteratorTest.java => ReadOnlyIndexSnapshotFileIteratorTest.java} (70%) create mode 100644 community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/WritableIndexSnapshotFileIteratorTest.java create mode 100644 community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/partition/IndexPartitionFactoryTest.java create mode 100644 community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanIndexBuilderTest.java create mode 100644 community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/ReadOnlyLuceneLabelScanIndexTest.java create mode 100644 community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/LuceneSchemaIndexBuilderTest.java create mode 100644 community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/ReadOnlyLuceneSchemaIndexTest.java diff --git a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/AbstractLuceneIndex.java b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/AbstractLuceneIndex.java index 29df80b61ba6c..5771676904ed6 100644 --- a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/AbstractLuceneIndex.java +++ b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/AbstractLuceneIndex.java @@ -37,6 +37,7 @@ import org.neo4j.helpers.Exceptions; import org.neo4j.helpers.collection.Iterators; import org.neo4j.io.IOUtils; +import org.neo4j.kernel.api.impl.index.backup.WritableIndexSnapshotFileIterator; import org.neo4j.kernel.api.impl.index.partition.AbstractIndexPartition; import org.neo4j.kernel.api.impl.index.partition.IndexPartitionFactory; import org.neo4j.kernel.api.impl.index.partition.PartitionSearcher; @@ -238,7 +239,7 @@ public LuceneAllDocumentsReader allDocumentsReader() * * @return iterator over all index files. * @throws IOException - * @see org.neo4j.kernel.api.impl.index.backup.LuceneIndexSnapshotFileIterator + * @see WritableIndexSnapshotFileIterator */ public ResourceIterator snapshot() throws IOException { diff --git a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/LuceneIndex.java b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/LuceneIndex.java index a0a8604c86e9a..21d0654dc34b3 100644 --- a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/LuceneIndex.java +++ b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/LuceneIndex.java @@ -25,6 +25,7 @@ import java.util.List; import org.neo4j.graphdb.ResourceIterator; +import org.neo4j.kernel.api.impl.index.backup.WritableIndexSnapshotFileIterator; import org.neo4j.kernel.api.impl.index.partition.AbstractIndexPartition; /** @@ -106,7 +107,7 @@ public interface LuceneIndex extends Closeable * * @return iterator over all index files. * @throws IOException - * @see org.neo4j.kernel.api.impl.index.backup.LuceneIndexSnapshotFileIterator + * @see WritableIndexSnapshotFileIterator */ ResourceIterator snapshot() throws IOException; diff --git a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshots.java b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshots.java index a5b62ad174be6..6ba1ce8a89ab4 100644 --- a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshots.java +++ b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshots.java @@ -56,7 +56,7 @@ public static ResourceIterator forIndex( File indexFolder, IndexWriter ind { SnapshotDeletionPolicy policy = (SnapshotDeletionPolicy) deletionPolicy; return hasCommits( indexWriter ) - ? new LuceneIndexSnapshotFileIterator( indexFolder, policy ) + ? new WritableIndexSnapshotFileIterator( indexFolder, policy ) : emptyIterator(); } else @@ -77,6 +77,10 @@ public static ResourceIterator forIndex( File indexFolder, IndexWriter ind */ public static ResourceIterator forIndex( File indexFolder, Directory directory ) throws IOException { + if ( !hasCommits( directory ) ) + { + return emptyIterator(); + } Collection indexCommits = DirectoryReader.listCommits( directory ); IndexCommit indexCommit = Iterables.last( indexCommits ); return new ReadOnlyIndexSnapshotFileIterator( indexFolder, indexCommit ); @@ -85,6 +89,11 @@ public static ResourceIterator forIndex( File indexFolder, Directory direc private static boolean hasCommits( IndexWriter indexWriter ) throws IOException { Directory directory = indexWriter.getDirectory(); + return hasCommits( directory ); + } + + private static boolean hasCommits( Directory directory ) throws IOException + { return DirectoryReader.indexExists( directory ) && SegmentInfos.readLatestCommit( directory ) != null; } } diff --git a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/SnapshotReleaseException.java b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/SnapshotReleaseException.java index 7f6b2bd2402c6..6886bf8bc878d 100644 --- a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/SnapshotReleaseException.java +++ b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/SnapshotReleaseException.java @@ -20,10 +20,10 @@ package org.neo4j.kernel.api.impl.index.backup; /** - * Exception that is thrown by {@link LuceneIndexSnapshotFileIterator} in case if exception + * Exception that is thrown by {@link WritableIndexSnapshotFileIterator} in case if exception * occurred during index snapshot release * - * @see LuceneIndexSnapshotFileIterator + * @see WritableIndexSnapshotFileIterator * @see org.apache.lucene.index.SnapshotDeletionPolicy * @see org.apache.lucene.index.IndexCommit */ diff --git a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/UnsupportedIndexDeletionPolicy.java b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/UnsupportedIndexDeletionPolicy.java index ef7741a6c2a58..05563844aac99 100644 --- a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/UnsupportedIndexDeletionPolicy.java +++ b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/UnsupportedIndexDeletionPolicy.java @@ -20,10 +20,10 @@ package org.neo4j.kernel.api.impl.index.backup; /** - * Exception that is throw by {@link LuceneIndexSnapshotFileIterator} in case if there is an attempt to create a + * Exception that is throw by {@link WritableIndexSnapshotFileIterator} in case if there is an attempt to create a * snapshot on a index with index policy that does not support snapshots. * - * @see LuceneIndexSnapshotFileIterator + * @see WritableIndexSnapshotFileIterator * @see org.apache.lucene.index.SnapshotDeletionPolicy */ class UnsupportedIndexDeletionPolicy extends RuntimeException diff --git a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshotFileIterator.java b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/WritableIndexSnapshotFileIterator.java similarity index 90% rename from community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshotFileIterator.java rename to community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/WritableIndexSnapshotFileIterator.java index f5d65969640bb..9805268a99bc5 100644 --- a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshotFileIterator.java +++ b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/backup/WritableIndexSnapshotFileIterator.java @@ -31,11 +31,11 @@ * Internally uses {@link SnapshotDeletionPolicy#snapshot()} to create an {@link IndexCommit} that represents * consistent state of the index for a particular point in time. */ -public class LuceneIndexSnapshotFileIterator extends ReadOnlyIndexSnapshotFileIterator +public class WritableIndexSnapshotFileIterator extends ReadOnlyIndexSnapshotFileIterator { private final SnapshotDeletionPolicy snapshotDeletionPolicy; - LuceneIndexSnapshotFileIterator( File indexDirectory, SnapshotDeletionPolicy snapshotDeletionPolicy ) + WritableIndexSnapshotFileIterator( File indexDirectory, SnapshotDeletionPolicy snapshotDeletionPolicy ) throws IOException { super( indexDirectory, snapshotDeletionPolicy.snapshot() ); diff --git a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/partition/ReadOnlyIndexPartition.java b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/partition/ReadOnlyIndexPartition.java index 2a56b8d8f7cd5..9d34516d4d5b5 100644 --- a/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/partition/ReadOnlyIndexPartition.java +++ b/community/lucene-index/src/main/java/org/neo4j/kernel/api/impl/index/partition/ReadOnlyIndexPartition.java @@ -70,7 +70,7 @@ public PartitionSearcher acquireSearcher() throws IOException @Override public void maybeRefreshBlocking() throws IOException { - + // nothing to refresh in read only partition } @Override diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshotFileIteratorTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/ReadOnlyIndexSnapshotFileIteratorTest.java similarity index 70% rename from community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshotFileIteratorTest.java rename to community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/ReadOnlyIndexSnapshotFileIteratorTest.java index 1e15d7945770f..25716e44114c4 100644 --- a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/LuceneIndexSnapshotFileIteratorTest.java +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/ReadOnlyIndexSnapshotFileIteratorTest.java @@ -24,7 +24,6 @@ import org.apache.lucene.document.StringField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.RAMDirectory; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -36,49 +35,47 @@ import java.util.stream.Stream; import org.neo4j.graphdb.ResourceIterator; -import org.neo4j.helpers.collection.Iterators; import org.neo4j.io.IOUtils; import org.neo4j.kernel.api.impl.index.IndexWriterConfigs; +import org.neo4j.kernel.api.impl.index.storage.DirectoryFactory; import org.neo4j.test.TargetDirectory; import static java.util.stream.Collectors.toSet; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; -public class LuceneIndexSnapshotFileIteratorTest +public class ReadOnlyIndexSnapshotFileIteratorTest { @Rule public final TargetDirectory.TestDirectory testDir = TargetDirectory.testDirForTest( getClass() ); - private File indexDir; - private Directory dir; - private IndexWriter writer; + protected File indexDir; + protected Directory dir; @Before - public void initializeLuceneResources() throws IOException + public void setUp() throws IOException { indexDir = testDir.directory(); - dir = new RAMDirectory(); - writer = new IndexWriter( dir, IndexWriterConfigs.standard() ); + dir = DirectoryFactory.PERSISTENT.open( indexDir ); } @After - public void closeLuceneResources() throws IOException + public void tearDown() throws IOException { - IOUtils.closeAll( writer, dir ); + IOUtils.closeAll( dir ); } @Test public void shouldReturnRealSnapshotIfIndexAllowsIt() throws IOException { - insertRandomDocuments( writer ); + prepareIndex(); Set files = listDir( dir ); assertFalse( files.isEmpty() ); - try ( ResourceIterator snapshot = LuceneIndexSnapshots.forIndex( indexDir, writer ) ) + try ( ResourceIterator snapshot = makeSnapshot() ) { - Set snapshotFiles = Iterators.asList( snapshot ).stream().map( File::getName ).collect( toSet() ); + Set snapshotFiles = snapshot.stream().map( File::getName ).collect( toSet() ); assertEquals( files, snapshotFiles ); } } @@ -86,12 +83,25 @@ public void shouldReturnRealSnapshotIfIndexAllowsIt() throws IOException @Test public void shouldReturnEmptyIteratorWhenNoCommitsHaveBeenMade() throws IOException { - try ( ResourceIterator snapshot = LuceneIndexSnapshots.forIndex( indexDir, writer ) ) + try ( ResourceIterator snapshot = makeSnapshot() ) { assertFalse( snapshot.hasNext() ); } } + private void prepareIndex() throws IOException + { + try ( IndexWriter writer = new IndexWriter( dir, IndexWriterConfigs.standard() ) ) + { + insertRandomDocuments( writer ); + } + } + + protected ResourceIterator makeSnapshot() throws IOException + { + return LuceneIndexSnapshots.forIndex( indexDir, dir ); + } + private static void insertRandomDocuments( IndexWriter writer ) throws IOException { Document doc = new Document(); @@ -104,6 +114,9 @@ private static void insertRandomDocuments( IndexWriter writer ) throws IOExcepti private static Set listDir( Directory dir ) throws IOException { String[] files = dir.listAll(); - return Stream.of( files ).collect( toSet() ); + return Stream.of( files ) + .filter( file -> !IndexWriter.WRITE_LOCK_NAME.equals( file ) ) + .collect( toSet() ); } + } diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/WritableIndexSnapshotFileIteratorTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/WritableIndexSnapshotFileIteratorTest.java new file mode 100644 index 0000000000000..005b94091efb4 --- /dev/null +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/backup/WritableIndexSnapshotFileIteratorTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2002-2016 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.kernel.api.impl.index.backup; + +import org.apache.lucene.index.IndexWriter; + +import java.io.File; +import java.io.IOException; + +import org.neo4j.graphdb.ResourceIterator; +import org.neo4j.kernel.api.impl.index.IndexWriterConfigs; + +public class WritableIndexSnapshotFileIteratorTest extends ReadOnlyIndexSnapshotFileIteratorTest +{ + + @Override + protected ResourceIterator makeSnapshot() throws IOException + { + return LuceneIndexSnapshots.forIndex( indexDir, new IndexWriter( dir, IndexWriterConfigs.standard() ) ); + } +} diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/partition/IndexPartitionFactoryTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/partition/IndexPartitionFactoryTest.java new file mode 100644 index 0000000000000..8d58600405cb3 --- /dev/null +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/index/partition/IndexPartitionFactoryTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2002-2016 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.kernel.api.impl.index.partition; + +import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.store.Directory; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.io.File; +import java.io.IOException; + +import org.neo4j.kernel.api.impl.index.IndexWriterConfigs; +import org.neo4j.kernel.api.impl.index.storage.DirectoryFactory; +import org.neo4j.test.TargetDirectory; + +import static org.junit.Assert.assertEquals; + +public class IndexPartitionFactoryTest +{ + + @Rule + public final TargetDirectory.TestDirectory testDirectory = TargetDirectory.testDirForTest( getClass() ); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private Directory directory; + + @Before + public void setUp() throws IOException + { + directory = DirectoryFactory.PERSISTENT.open( testDirectory.directory() ); + } + + @Test + public void createReadOnlyPartition() throws Exception + { + prepareIndex(); + try ( AbstractIndexPartition indexPartition = + new ReadOnlyIndexPartitionFactory().createPartition( testDirectory.directory(), directory ) ) + { + expectedException.expect( UnsupportedOperationException.class ); + + indexPartition.getIndexWriter(); + } + } + + @Test + public void createWritablePartition() throws Exception + { + try ( AbstractIndexPartition indexPartition = + new WritableIndexPartitionFactory( IndexWriterConfigs::standard ) + .createPartition( testDirectory.directory(), directory ) ) + { + + try ( IndexWriter indexWriter = indexPartition.getIndexWriter() ) + { + indexWriter.addDocument( new Document() ); + indexWriter.commit(); + indexPartition.maybeRefreshBlocking(); + try ( PartitionSearcher searcher = indexPartition.acquireSearcher() ) + { + assertEquals( "We should be able to see newly added document ", + 1, searcher.getIndexSearcher().getIndexReader().numDocs() ); + } + } + } + } + + private void prepareIndex() throws IOException + { + File location = testDirectory.directory(); + try ( AbstractIndexPartition ignored = + new WritableIndexPartitionFactory( IndexWriterConfigs::standard ) + .createPartition( location, DirectoryFactory.PERSISTENT.open( location ) ) ) + { + // empty + } + } +} diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanIndexBuilderTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanIndexBuilderTest.java new file mode 100644 index 0000000000000..f2eb74d686971 --- /dev/null +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanIndexBuilderTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2002-2016 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.kernel.api.impl.labelscan; + +import org.junit.Rule; +import org.junit.Test; + +import org.neo4j.graphdb.factory.GraphDatabaseSettings; +import org.neo4j.helpers.collection.MapUtil; +import org.neo4j.kernel.configuration.Config; +import org.neo4j.kernel.configuration.Settings; +import org.neo4j.kernel.impl.factory.OperationalMode; +import org.neo4j.test.TargetDirectory; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class LuceneLabelScanIndexBuilderTest +{ + + @Rule + public final TargetDirectory.TestDirectory testDir = TargetDirectory.testDirForTest( getClass() ); + + @Test + public void readOnlyIndexCreation() throws Exception + { + try ( LabelScanIndex index = LuceneLabelScanIndexBuilder.create() + .withIndexRootFolder( testDir.graphDbDir() ) + .withConfig( getReadOnlyConfig() ) + .withOperationalMode( OperationalMode.single ) + .build() ) + { + assertTrue( "Builder should construct read only index.", index.isReadOnly() ); + } + } + + @Test + public void writableIndexCreation() throws Exception + { + try ( LabelScanIndex index = LuceneLabelScanIndexBuilder.create() + .withIndexRootFolder( testDir.graphDbDir() ) + .withConfig( getDefaultConfig() ) + .withOperationalMode( OperationalMode.single ) + .build() ) + { + assertFalse( "Builder should construct writable index.", index.isReadOnly() ); + } + } + + private Config getDefaultConfig() + { + return Config.empty(); + } + + private Config getReadOnlyConfig() + { + return getDefaultConfig().with( MapUtil.stringMap( GraphDatabaseSettings.read_only.name(), Settings.TRUE ) ); + } +} diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanStoreTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanStoreTest.java index f42fdcfa5d0d3..04369f5af2159 100644 --- a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanStoreTest.java +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/LuceneLabelScanStoreTest.java @@ -326,7 +326,7 @@ public void shouldUpdateAFullRange() throws Exception start( label0Updates ); // when - write( Collections.emptyIterator() ); + write( Collections.emptyIterator() ); // then LabelScanReader reader = store.newReader(); @@ -549,19 +549,15 @@ private void startLabelScanStore( List existingData, Config con private FullStoreChangeStream asStream( final List existingData ) { - return new FullStoreChangeStream() + return writer -> { - @Override - public long applyTo( LabelScanWriter writer ) throws IOException + long count = 0; + for ( NodeLabelUpdate update : existingData ) { - long count = 0; - for ( NodeLabelUpdate update : existingData ) - { - writer.write( update ); - count++; - } - return count; + writer.write( update ); + count++; } + return count; }; } diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/ReadOnlyLuceneLabelScanIndexTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/ReadOnlyLuceneLabelScanIndexTest.java new file mode 100644 index 0000000000000..58f8be94da661 --- /dev/null +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/labelscan/ReadOnlyLuceneLabelScanIndexTest.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2002-2016 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.kernel.api.impl.labelscan; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.io.IOException; + +import org.neo4j.io.fs.DefaultFileSystemAbstraction; +import org.neo4j.kernel.api.impl.index.storage.DirectoryFactory; +import org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage; +import org.neo4j.kernel.api.impl.labelscan.storestrategy.BitmapDocumentFormat; +import org.neo4j.test.TargetDirectory; + +import static org.junit.Assert.assertTrue; + +public class ReadOnlyLuceneLabelScanIndexTest +{ + @Rule + public final TargetDirectory.TestDirectory testDirectory = TargetDirectory.testDirForTest( getClass() ); + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + + private ReadOnlyLuceneLabelScanIndex luceneLabelScanIndex; + + @Before + public void setUp() + { + PartitionedIndexStorage indexStorage = new PartitionedIndexStorage( DirectoryFactory.PERSISTENT, + new DefaultFileSystemAbstraction(), testDirectory.directory(), "1" ); + luceneLabelScanIndex = new ReadOnlyLuceneLabelScanIndex( BitmapDocumentFormat._32, indexStorage ); + } + + @After + public void tearDown() throws IOException + { + luceneLabelScanIndex.close(); + } + + @Test + public void readOnlyIndexMode() throws Exception + { + assertTrue( luceneLabelScanIndex.isReadOnly() ); + } + + @Test + public void writerIsNotAccessibleInReadOnlyMode() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + luceneLabelScanIndex.getLabelScanWriter(); + } + + @Test + public void indexCreationInReadOnlyModeIsNotSupported() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + luceneLabelScanIndex.create(); + } + + @Test + public void indexDeletionInReadOnlyModeIsNotSupported() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + luceneLabelScanIndex.drop(); + } +} diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/LuceneSchemaIndexBuilderTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/LuceneSchemaIndexBuilderTest.java new file mode 100644 index 0000000000000..53fe0bc0dcd91 --- /dev/null +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/LuceneSchemaIndexBuilderTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2002-2016 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.kernel.api.impl.schema; + +import org.junit.Rule; +import org.junit.Test; + +import org.neo4j.graphdb.factory.GraphDatabaseSettings; +import org.neo4j.helpers.collection.MapUtil; +import org.neo4j.kernel.configuration.Config; +import org.neo4j.kernel.configuration.Settings; +import org.neo4j.kernel.impl.factory.OperationalMode; +import org.neo4j.test.TargetDirectory; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class LuceneSchemaIndexBuilderTest +{ + @Rule + public final TargetDirectory.TestDirectory testDir = TargetDirectory.testDirForTest( getClass() ); + + @Test + public void readOnlyIndexCreation() throws Exception + { + try ( SchemaIndex schemaIndex = LuceneSchemaIndexBuilder.create() + .withConfig( getReadOnlyConfig() ) + .withOperationalMode( OperationalMode.single ) + .withIndexRootFolder( testDir.graphDbDir() ) + .withIndexIdentifier( "a" ) + .build() ) + { + assertTrue( "Builder should construct read only index.", schemaIndex.isReadOnly() ); + } + } + + @Test + public void writableIndexCreation() throws Exception + { + try ( SchemaIndex schemaIndex = LuceneSchemaIndexBuilder.create() + .withConfig( getDefaultConfig() ) + .withOperationalMode( OperationalMode.single ) + .withIndexRootFolder( testDir.graphDbDir() ) + .withIndexIdentifier( "b" ) + .build() ) + { + assertFalse( "Builder should construct writable index.", schemaIndex.isReadOnly() ); + } + } + + private Config getDefaultConfig() + { + return Config.empty(); + } + + private Config getReadOnlyConfig() + { + return getDefaultConfig().with( MapUtil.stringMap( GraphDatabaseSettings.read_only.name(), Settings.TRUE ) ); + } +} diff --git a/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/ReadOnlyLuceneSchemaIndexTest.java b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/ReadOnlyLuceneSchemaIndexTest.java new file mode 100644 index 0000000000000..593acc7b11554 --- /dev/null +++ b/community/lucene-index/src/test/java/org/neo4j/kernel/api/impl/schema/ReadOnlyLuceneSchemaIndexTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2002-2016 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.kernel.api.impl.schema; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.io.IOException; + +import org.neo4j.io.fs.DefaultFileSystemAbstraction; +import org.neo4j.kernel.api.impl.index.partition.ReadOnlyIndexPartitionFactory; +import org.neo4j.kernel.api.impl.index.storage.DirectoryFactory; +import org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage; +import org.neo4j.kernel.api.index.IndexConfiguration; +import org.neo4j.kernel.configuration.Config; +import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig; +import org.neo4j.test.TargetDirectory; + +import static org.junit.Assert.assertTrue; + +public class ReadOnlyLuceneSchemaIndexTest +{ + @Rule + public final TargetDirectory.TestDirectory testDirectory = TargetDirectory.testDirForTest( getClass() ); + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + private ReadOnlyLuceneSchemaIndex luceneSchemaIndex; + + @Before + public void setUp() + { + PartitionedIndexStorage indexStorage = new PartitionedIndexStorage( DirectoryFactory.PERSISTENT, + new DefaultFileSystemAbstraction(), testDirectory.directory(), "1" ); + Config config = Config.empty(); + IndexSamplingConfig samplingConfig = new IndexSamplingConfig( config ); + luceneSchemaIndex = new ReadOnlyLuceneSchemaIndex( indexStorage, IndexConfiguration.NON_UNIQUE, + samplingConfig, new ReadOnlyIndexPartitionFactory() ); + } + + @After + public void tearDown() throws IOException + { + luceneSchemaIndex.close(); + } + + @Test + public void indexDeletionIndReadOnlyModeIsNotSupported() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + luceneSchemaIndex.drop(); + } + + @Test + public void indexCreationInReadOnlyModeIsNotSupported() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + luceneSchemaIndex.create(); + } + + @Test + public void readOnlyIndexMarkingIsNotSupported() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + luceneSchemaIndex.markAsOnline(); + } + + @Test + public void readOnlyIndexMode() throws Exception + { + assertTrue( luceneSchemaIndex.isReadOnly() ); + } + + @Test + public void writerIsNotAccessibleInReadOnlyMode() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + luceneSchemaIndex.getIndexWriter(); + } +}