@Override public void run() { awaitStartSignal(); while ( failure.get() == null && !stopSignal.get() ) { try ( Transaction tx = db.beginTx() ) { // ArrayIndexOutOfBoundsException happens here Iterables.count( parentNode.getRelationships( RELTYPE, OUTGOING ) ); parentNode.createRelationshipTo( db.createNode(), RELTYPE ); tx.success(); } catch ( Exception e ) { failure.compareAndSet( null, e ); } } }
private List<IndexDefinition> indexes( GraphDatabaseService database ) { try ( Transaction ignored = database.beginTx() ) { return Iterables.asList( database.schema().getIndexes() ); } }
/** * Gets all {@link RecordFormats} that the selector is aware of. * @return An iterable over all known record formats. */ public static Iterable<RecordFormats> allFormats() { Iterable<RecordFormats.Factory> loadableFormatFactories = Service.load( RecordFormats.Factory.class ); Iterable<RecordFormats> loadableFormats = map( RecordFormats.Factory::newInstance, loadableFormatFactories ); return concat( KNOWN_FORMATS, loadableFormats ); }
@Override public Iterable<IndexDescriptor> constraintIndexesCreatedInTx() { if ( createdConstraintIndexesByConstraint != null && !createdConstraintIndexesByConstraint.isEmpty() ) { return map( TxState::getIndexForIndexBackedConstraint, createdConstraintIndexesByConstraint.keySet() ); } return Iterables.empty(); }
@Test public void shouldWriteCreatedSchemaRuleToStore() throws Exception { // GIVEN SchemaRecord beforeRecords = serialize( rule, id, false, false); SchemaRecord afterRecords = serialize( rule, id, true, true); when( neoStores.getSchemaStore() ).thenReturn( schemaStore ); // WHEN visitSchemaRuleCommand( storeApplier, new SchemaRuleCommand( beforeRecords, afterRecords, rule ) ); // THEN verify( schemaStore ).updateRecord( Iterables.first( afterRecords ) ); }
@Test public void shouldListAddedLabels() throws Exception { // Given int labelId = 2; when( tokenRead.nodeLabelName( labelId ) ).thenReturn( "theLabel" ); state.nodeDoAddLabel( labelId, 1L ); // When Iterable<LabelEntry> labelEntries = snapshot().assignedLabels(); // Then LabelEntry entry = single( labelEntries ); assertThat( entry.label().name(), equalTo( "theLabel" ) ); assertThat( entry.node().getId(), equalTo( 1L ) ); }
@Test public void shouldCatchArithmeticExceptionsAndTryNext() { // GIVEN NumberArrayFactory throwingMemoryFactory = mock( NumberArrayFactory.class ); ArithmeticException failure = new ArithmeticException( "This is an artificial failure" ); doThrow( failure ).when( throwingMemoryFactory ).newByteArray( anyLong(), any( byte[].class ), anyLong() ); FailureMonitor monitor = new FailureMonitor(); NumberArrayFactory factory = new NumberArrayFactory.Auto( monitor, throwingMemoryFactory, NumberArrayFactory.HEAP ); int itemSize = 4; // WHEN ByteArray array = factory.newByteArray( KILO, new byte[itemSize], 0 ); array.setInt( KILO - 10, 0, 12345 ); // THEN verify( throwingMemoryFactory, times( 1 ) ).newByteArray( eq( KILO ), any( byte[].class ), eq( 0L ) ); assertTrue( array instanceof HeapByteArray ); assertEquals( 12345, array.getInt( KILO - 10, 0 ) ); assertEquals( KILO * itemSize, monitor.memory ); assertEquals( NumberArrayFactory.HEAP, monitor.successfulFactory ); assertEquals( throwingMemoryFactory, single( monitor.attemptedAllocationFailures ).getFactory() ); assertThat( single( monitor.attemptedAllocationFailures ).getFailure().getMessage(), containsString( failure.getMessage() ) ); }
@Test public void showsDeletedNodes() throws Exception { // Given state.nodeDoDelete( 1L ); state.nodeDoDelete( 2L ); int labelId = 15; when( tokenRead.nodeLabelName( labelId ) ).thenReturn( "label" ); ops.withNode( 1, new long[]{labelId}, genericMap( "key", Values.of( "p" ) ) ); ops.withNode( 2, NO_LABELS ); // When & Then TxStateTransactionDataSnapshot snapshot = snapshot(); assertThat( idList( snapshot.deletedNodes() ), equalTo( asList( 1L, 2L ) ) ); assertThat( single( snapshot.removedLabels() ).label().name(), equalTo( "label" ) ); assertThat( single( snapshot.removedNodeProperties() ).key(), equalTo( "key" ) ); }
@Test void testPathNodes() { when( spi.newNodeProxy( Mockito.anyLong() ) ).thenAnswer( new NodeProxyAnswer() ); Path path = new PathImpl.Builder( createNodeProxy( 1 ) ) .push( createRelationshipProxy( 1, 2 ) ) .push( createRelationshipProxy( 2, 3 ) ) .build( new PathImpl.Builder( createNodeProxy( 3 ) ) ); Iterable<Node> nodes = path.nodes(); List<Node> nodeList = Iterables.asList( nodes ); assertEquals( 3, nodeList.size() ); assertEquals( 1, nodeList.get( 0 ).getId() ); assertEquals( 2, nodeList.get( 1 ).getId() ); assertEquals( 3, nodeList.get( 2 ).getId() ); }
long[] arrayPropertyValue = new long[]{1021L, 321L, 343212L}; Transaction tx = db.beginTx(); Node nodeBelowTheLine = db.createNode(); nodeBelowTheLine.setProperty( propertyKey, intPropertyValue ); assertEquals( idBelow, nodeBelowTheLine.getId() ); Node nodeAboveTheLine = db.createNode(); nodeAboveTheLine.setProperty( propertyKey, stringPropertyValue ); Relationship relBelowTheLine = nodeBelowTheLine.createRelationshipTo( nodeAboveTheLine, this ); relBelowTheLine.setProperty( propertyKey, arrayPropertyValue ); assertEquals( stringPropertyValue, nodeAboveTheLine.getProperty( propertyKey ) ); assertTrue( Arrays.equals( arrayPropertyValue, (long[]) relBelowTheLine.getProperty( propertyKey ) ) ); tx.success(); tx.close(); assertEquals( highMark, relAboveTheLine.getId() ); assertEquals( asSet( asList( relBelowTheLine, relAboveTheLine ) ), asSet( Iterables.asCollection( db.getNodeById( idBelow ).getRelationships() ) ) ); transaction.success();
@Test public void mustDiscoverNodeInStoreMissingFromIndex() throws Exception { GraphDatabaseService db = createDatabase(); try ( Transaction tx = db.beginTx() ) { db.execute( format( NODE_CREATE, "nodes", array( "Label" ), array( "prop" ) ) ).close(); tx.success(); } StoreIndexDescriptor indexDescriptor; long nodeId; try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); indexDescriptor = getIndexDescriptor( first( db.schema().getIndexes() ) ); Node node = db.createNode( Label.label( "Label" ) ); node.setProperty( "prop", "value" ); nodeId = node.getId(); tx.success(); } IndexingService indexes = getIndexingService( db ); IndexProxy indexProxy = indexes.getIndexProxy( indexDescriptor.schema() ); try ( IndexUpdater updater = indexProxy.newUpdater( IndexUpdateMode.ONLINE ) ) { updater.process( IndexEntryUpdate.remove( nodeId, indexDescriptor, Values.stringValue( "value" ) ) ); } db.shutdown(); ConsistencyCheckService.Result result = checkConsistency(); assertFalse( result.isSuccessful() ); }
@Test public void shouldCreateRelationship() throws Exception { long n1, n2; try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { n1 = graphDb.createNode().getId(); n2 = graphDb.createNode().getId(); tx.success(); } long r; try ( Transaction tx = beginTransaction() ) { int label = tx.token().relationshipTypeGetOrCreateForName( "R" ); r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { List<Relationship> relationships = Iterables.asList( graphDb.getNodeById( n1 ).getRelationships() ); assertEquals( 1, relationships.size() ); assertEquals( relationships.get( 0 ).getId(), r ); } }
@Test public void convertIndexToConstraintWithExistingData() { try ( Transaction tx = graphDb.beginTx() ) { for ( int i = 0; i < 2000; i++ ) { Node node = graphDb.createNode( LABEL ); node.setProperty( PROPERTY_KEY, i ); } tx.success(); } try ( Transaction tx = graphDb.beginTx() ) { graphDb.schema().indexFor( LABEL ).on( PROPERTY_KEY ).create(); tx.success(); } try ( Transaction tx = graphDb.beginTx() ) { IndexDefinition index = firstOrNull( graphDb.schema().getIndexes( LABEL ) ); index.drop(); graphDb.schema().constraintFor( LABEL ).assertPropertyIsUnique( PROPERTY_KEY ).create(); tx.success(); } // assert no exception is thrown }
@Test public void lazyLoadWithinWriteTransaction() throws Exception { // Given FileSystemAbstraction fileSystem = fs.get(); BatchInserter inserter = BatchInserters.inserter( testDirectory.databaseDir(), fileSystem ); int count = 3000; long nodeId = inserter.createNode( mapWithManyProperties( count /* larger than initial property index load threshold */ ) ); inserter.shutdown(); GraphDatabaseService db = new TestGraphDatabaseFactory().setFileSystem( fileSystem ).newImpermanentDatabase( testDirectory.databaseDir() ); // When try ( Transaction tx = db.beginTx() ) { db.createNode(); Node node = db.getNodeById( nodeId ); // Then assertEquals( count, Iterables.count( node.getPropertyKeys() ) ); tx.success(); } finally { db.shutdown(); } }
private void changeRandomNode( GraphDatabaseService db, int nodeCount, RandomValues random ) { try ( Transaction tx = db.beginTx() ) { long nodeId = random.nextInt( nodeCount ); Node node = db.getNodeById( nodeId ); Object[] keys = Iterables.asCollection( node.getPropertyKeys() ).toArray(); String key = (String) random.among( keys ); if ( random.nextFloat() < 0.1 ) { // REMOVE node.removeProperty( key ); } else { // CHANGE node.setProperty( key, random.nextValue().asObject() ); } tx.success(); } catch ( NotFoundException e ) { // It's OK, it happens if some other thread deleted that property in between us reading it and // removing or setting it } }
@Test public void shouldIgnoreEmptyQuotedStringsIfConfiguredTo() throws Exception { // GIVEN File data = data( ":ID,one,two,three", "1,\"\",,value" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--ignore-empty-strings", "true" ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { Node node = Iterables.single( db.getAllNodes() ); assertFalse( node.hasProperty( "one" ) ); assertFalse( node.hasProperty( "two" ) ); assertEquals( "value", node.getProperty( "three" ) ); tx.success(); } }
private ConstraintDefinition getConstraint( Label label, String propertyKey ) { try ( Transaction tx = graphDb.beginTx() ) { ConstraintDefinition found = null; for ( ConstraintDefinition constraint : graphDb.schema().getConstraints( label ) ) { if ( propertyKey.equals( single( constraint.getPropertyKeys() ) ) ) { assertNull( "Found multiple constraints.", found ); found = constraint; } } tx.success(); return found; } } }
@Test public void convertIndexToConstraint() { try ( Transaction tx = graphDb.beginTx() ) { graphDb.schema().indexFor( LABEL ).on( PROPERTY_KEY ).create(); tx.success(); } try ( Transaction tx = graphDb.beginTx() ) { IndexDefinition index = firstOrNull( graphDb.schema().getIndexes( LABEL ) ); index.drop(); graphDb.schema().constraintFor( LABEL ).assertPropertyIsUnique( PROPERTY_KEY ).create(); tx.success(); } // assert no exception is thrown }