Skip to content

Commit

Permalink
Properly update readers
Browse files Browse the repository at this point in the history
  • Loading branch information
ragadeeshu committed Sep 12, 2017
1 parent e4511b2 commit d7f6e22
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 22 deletions.
Binary file not shown.
@@ -1,5 +1,25 @@
/*
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.insight;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
Expand All @@ -15,12 +35,12 @@ public class InsightIndex
{
private final InsightLuceneIndex nodeIndex;

public InsightIndex( FileSystemAbstraction fileSystem, int[] properties ) throws IOException
public InsightIndex( FileSystemAbstraction fileSystem, File file, int[] properties ) throws IOException
{
LuceneIndexStorageBuilder storageBuilder = LuceneIndexStorageBuilder.create();
storageBuilder.withFileSystem( fileSystem ).withIndexIdentifier( "insightNodes" )
.withDirectoryFactory( directoryFactory( false, fileSystem ) )
.withIndexRootFolder( Paths.get( "insightindex" ).toFile() );
.withIndexRootFolder( Paths.get( file.getAbsolutePath(),"insightindex" ).toFile() );

WritableIndexPartitionFactory partitionFactory = new WritableIndexPartitionFactory(
IndexWriterConfigs::population );
Expand All @@ -33,7 +53,7 @@ public InsightIndex( FileSystemAbstraction fileSystem, int[] properties ) throws
public InsightIndexTransactionEventUpdater getUpdater() throws IOException
{
WritableDatabaseInsightIndex writableDatabaseInsightIndex = new WritableDatabaseInsightIndex( nodeIndex );
return new InsightIndexTransactionEventUpdater( writableDatabaseInsightIndex.getIndexWriter() );
return new InsightIndexTransactionEventUpdater( writableDatabaseInsightIndex );
}

public InsightIndexReader getReader() throws IOException
Expand Down
@@ -1,3 +1,22 @@
/*
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.insight;

import org.neo4j.collection.primitive.PrimitiveLongIterator;
Expand Down
@@ -1,3 +1,22 @@
/*
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.insight;

import org.apache.lucene.document.Document;
Expand All @@ -14,11 +33,11 @@

public class InsightIndexTransactionEventUpdater implements TransactionEventHandler<Object>
{
private PartitionedInsightIndexWriter indexWriter;
private WritableDatabaseInsightIndex writableDatabaseInsightIndex;

InsightIndexTransactionEventUpdater( PartitionedInsightIndexWriter indexWriter )
InsightIndexTransactionEventUpdater( WritableDatabaseInsightIndex writableDatabaseInsightIndex )
{
this.indexWriter = indexWriter;
this.writableDatabaseInsightIndex = writableDatabaseInsightIndex;
}

@Override
Expand All @@ -45,13 +64,21 @@ private void writeNodeData( Iterable<PropertyEntry<Node>> propertyEntries )
Values.of( propertyEntry.value() ) ) );
try
{
indexWriter.addDocument( document );
writableDatabaseInsightIndex.getIndexWriter().addDocument( document );
}
catch ( IOException e )
{
e.printStackTrace();
}
}
try
{
writableDatabaseInsightIndex.maybeRefreshBlocking();
}
catch ( IOException e )
{
e.printStackTrace();
}
}

@Override
Expand Down
@@ -1,3 +1,22 @@
/*
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.insight;

import org.apache.lucene.index.DirectoryReader;
Expand Down
Expand Up @@ -56,8 +56,6 @@ public void addDocument( Document doc ) throws IOException
{
IndexWriter indexWriter = getIndexWriter( 1 );
indexWriter.addDocument( doc );
indexWriter.prepareCommit();
indexWriter.commit();
}

public void addDocuments( int numDocs, Iterable<Document> documents ) throws IOException
Expand Down
@@ -1,3 +1,22 @@
/*
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.insight;

import java.io.IOException;
Expand Down
@@ -1,5 +1,25 @@
/*
* Copyright (c) 2002-2017 "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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.insight;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -10,48 +30,45 @@
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.EmbeddedDatabaseRule;
import org.neo4j.test.rule.ImpermanentDatabaseRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;

import static org.junit.Assert.assertEquals;

public class InsightLuceneIndexUpdaterTest
{
@ClassRule
public static DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();
@ClassRule
public static TestDirectory testDirectory = TestDirectory.testDirectory( fileSystemRule );
@Rule
public EphemeralFileSystemRule fileSystemRule = new EphemeralFileSystemRule();
@Rule
public DatabaseRule dbRule = new ImpermanentDatabaseRule();
public DatabaseRule dbRule = new EmbeddedDatabaseRule(testDirectory.graphDbDir());

private static final Label LABEL = Label.label( "label1" );

@Test
public void shouldFindNodeWithString() throws IOException
{
// given
GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
InsightIndex insightIndex = new InsightIndex( fileSystemRule.get(), new int[]{1} );
InsightIndex insightIndex = new InsightIndex( fileSystemRule, testDirectory.graphDbDir(), new int[]{1} );
db.registerTransactionEventHandler( insightIndex.getUpdater() );

try ( Transaction tx = db.beginTx() )
{
for ( int i = 0; i < 1; i++ )
{
Node node = db.createNode( LABEL );
node.setProperty( "prop", "aaaaa" );
node.setProperty( "prop", "hej" );
}

tx.success();
}

// when
try ( Transaction tx = db.beginTx() )
{
tx.success();
}
InsightIndexReader reader = insightIndex.getReader();

assertEquals( 0, reader.query( "aaaaa" ).next() );
assertEquals( 0, reader.query( "hej" ).next() );

}
}

0 comments on commit d7f6e22

Please sign in to comment.