-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
249 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...ity/kernel/src/main/java/org/neo4j/kernel/impl/factory/StatementLocksFactorySelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.kernel.impl.factory; | ||
|
||
import java.util.List; | ||
|
||
import org.neo4j.helpers.Service; | ||
import org.neo4j.helpers.collection.Iterables; | ||
import org.neo4j.helpers.collection.Iterators; | ||
import org.neo4j.kernel.configuration.Config; | ||
import org.neo4j.kernel.impl.locking.Locks; | ||
import org.neo4j.kernel.impl.locking.SimpleStatementLocksFactory; | ||
import org.neo4j.kernel.impl.locking.StatementLocksFactory; | ||
import org.neo4j.kernel.impl.logging.LogService; | ||
import org.neo4j.logging.Log; | ||
|
||
public class StatementLocksFactorySelector | ||
{ | ||
private final Locks locks; | ||
private final Config config; | ||
private final Log log; | ||
|
||
public StatementLocksFactorySelector( Locks locks, Config config, LogService logService ) | ||
{ | ||
this.locks = locks; | ||
this.config = config; | ||
this.log = logService.getInternalLog( getClass() ); | ||
} | ||
|
||
public StatementLocksFactory select() | ||
{ | ||
StatementLocksFactory statementLocksFactory; | ||
|
||
String serviceName = StatementLocksFactory.class.getSimpleName(); | ||
List<StatementLocksFactory> factories = serviceLoadFactories(); | ||
if ( factories.isEmpty() ) | ||
{ | ||
statementLocksFactory = new SimpleStatementLocksFactory(); | ||
|
||
log.info( "No services implementing " + serviceName + " found. " + | ||
"Using " + SimpleStatementLocksFactory.class.getSimpleName() ); | ||
} | ||
else if ( factories.size() == 1 ) | ||
{ | ||
statementLocksFactory = factories.get( 0 ); | ||
|
||
log.info( "Found single implementation of " + serviceName + | ||
". Namely " + statementLocksFactory.getClass().getSimpleName() ); | ||
} | ||
else | ||
{ | ||
throw new IllegalStateException( | ||
"Found more than one implementation of " + serviceName + ": " + factories ); | ||
} | ||
|
||
statementLocksFactory.initialize( locks, config ); | ||
|
||
return statementLocksFactory; | ||
} | ||
|
||
/** | ||
* Load all available factories via {@link Service}. | ||
* <b>Visible for testing only.</b> | ||
* | ||
* @return list of available factories. | ||
*/ | ||
List<StatementLocksFactory> serviceLoadFactories() | ||
{ | ||
return Iterables.asList( Service.load( StatementLocksFactory.class ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...kernel/src/test/java/org/neo4j/kernel/impl/factory/StatementLocksFactorySelectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.kernel.impl.factory; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.neo4j.kernel.configuration.Config; | ||
import org.neo4j.kernel.impl.locking.Locks; | ||
import org.neo4j.kernel.impl.locking.Locks.Client; | ||
import org.neo4j.kernel.impl.locking.SimpleStatementLocks; | ||
import org.neo4j.kernel.impl.locking.SimpleStatementLocksFactory; | ||
import org.neo4j.kernel.impl.locking.StatementLocks; | ||
import org.neo4j.kernel.impl.locking.StatementLocksFactory; | ||
import org.neo4j.kernel.impl.logging.LogService; | ||
import org.neo4j.kernel.impl.logging.NullLogService; | ||
|
||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Matchers.same; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class StatementLocksFactorySelectorTest | ||
{ | ||
@Test | ||
public void loadSimpleStatementLocksFactoryWhenNoServices() | ||
{ | ||
Locks locks = mock( Locks.class ); | ||
Locks.Client locksClient = mock( Client.class ); | ||
when( locks.newClient() ).thenReturn( locksClient ); | ||
|
||
StatementLocksFactorySelector loader = newLoader( locks ); | ||
|
||
StatementLocksFactory factory = loader.select(); | ||
StatementLocks statementLocks = factory.newInstance(); | ||
|
||
assertThat( factory, instanceOf( SimpleStatementLocksFactory.class ) ); | ||
assertThat( statementLocks, instanceOf( SimpleStatementLocks.class ) ); | ||
|
||
assertSame( locksClient, statementLocks.optimistic() ); | ||
assertSame( locksClient, statementLocks.pessimistic() ); | ||
} | ||
|
||
@Test | ||
public void loadSingleAvailableFactory() | ||
{ | ||
Locks locks = mock( Locks.class ); | ||
StatementLocksFactory factory = mock( StatementLocksFactory.class ); | ||
|
||
StatementLocksFactorySelector loader = newLoader( locks, factory ); | ||
|
||
StatementLocksFactory loadedFactory = loader.select(); | ||
|
||
assertSame( factory, loadedFactory ); | ||
verify( factory ).initialize( same( locks ), any( Config.class ) ); | ||
} | ||
|
||
@Test | ||
public void throwWhenMultipleFactoriesLoaded() | ||
{ | ||
TestStatementLocksFactorySelector loader = newLoader( mock( Locks.class ), | ||
mock( StatementLocksFactory.class ), | ||
mock( StatementLocksFactory.class ), | ||
mock( StatementLocksFactory.class ) ); | ||
|
||
try | ||
{ | ||
loader.select(); | ||
} | ||
catch ( Exception e ) | ||
{ | ||
assertThat( e, instanceOf( IllegalStateException.class ) ); | ||
} | ||
} | ||
|
||
private static TestStatementLocksFactorySelector newLoader( Locks locks, StatementLocksFactory... factories ) | ||
{ | ||
return new TestStatementLocksFactorySelector( locks, Config.empty(), NullLogService.getInstance(), factories ); | ||
} | ||
|
||
private static class TestStatementLocksFactorySelector extends StatementLocksFactorySelector | ||
{ | ||
private final List<StatementLocksFactory> factories; | ||
|
||
TestStatementLocksFactorySelector( Locks locks, Config config, LogService logService, | ||
StatementLocksFactory... factories ) | ||
{ | ||
super( locks, config, logService ); | ||
this.factories = Arrays.asList( factories ); | ||
} | ||
|
||
@Override | ||
List<StatementLocksFactory> serviceLoadFactories() | ||
{ | ||
return factories; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.