/** * Returns the given iterator's single element or {@code null} if no * element found. If there is more than one element in the iterator a * {@link NoSuchElementException} will be thrown. * * If the {@code iterator} implements {@link Resource} it will be {@link Resource#close() closed} * in a {@code finally} block after the single item has been retrieved, or failed to be retrieved. * * @param <T> the type of elements in {@code iterator}. * @param iterator the {@link Iterator} to get elements from. * @return the single element in {@code iterator}, or {@code null} if no * element found. * @throws NoSuchElementException if more than one element was found. */ public static <T> T singleOrNull( Iterator<T> iterator ) { return single( iterator, null ); }
@Test void testSingleElement() { Object object = new Object(); Object object2 = new Object(); // single Iterable assertEquals( object, Iterables.single( asList( object ) ) ); assertThrows( NoSuchElementException.class, () -> Iterables.single( asList() ) ); assertThrows( NoSuchElementException.class, () -> Iterables.single( asList( object, object2 ) ) ); // single Iterator assertEquals( object, Iterators.single( asList( object ).iterator() ) ); assertThrows( NoSuchElementException.class, () -> Iterators.single( asList().iterator() ) ); assertThrows( NoSuchElementException.class, () -> Iterators.single( asList( object, object2 ).iterator() ) ); // singleOrNull Iterable assertEquals( object, Iterables.singleOrNull( asList( object ) ) ); assertNull( Iterables.singleOrNull( asList() ) ); assertThrows( NoSuchElementException.class, () -> Iterables.singleOrNull( asList( object, object2 ) ) ); // singleOrNull Iterator assertEquals( object, Iterators.singleOrNull( asList( object ).iterator() ) ); assertNull( Iterators.singleOrNull( asList().iterator() ) ); assertThrows( NoSuchElementException.class, () -> Iterators.singleOrNull( asList( object, object2 ).iterator() ) ); }
/** * Returns the given iterable's single element. If there are no elements * or more than one element in the iterable a {@link NoSuchElementException} * will be thrown. * * If the {@link Iterable#iterator() iterator} created by the {@code iterable} implements {@link Resource} * it will be {@link Resource#close() closed} in a {@code finally} block after the single item * has been retrieved, or failed to be retrieved. * * @param <T> the type of elements in {@code iterable}. * @param iterable the {@link Iterable} to get elements from. * @return the single element in the {@code iterable}. * @throws NoSuchElementException if there isn't exactly one element. */ public static <T> T single( Iterable<T> iterable ) { return Iterators.single( iterable.iterator() ); }
/** * Returns the given iterable's single element or {@code itemIfNone} if no * element found. If there is more than one element in the iterable a * {@link NoSuchElementException} will be thrown. * * If the {@link Iterable#iterator() iterator} created by the {@code iterable} implements {@link Resource} * it will be {@link Resource#close() closed} in a {@code finally} block after the single item * has been retrieved, or failed to be retrieved. * * @param <T> the type of elements in {@code iterable}. * @param iterable the {@link Iterable} to get elements from. * @param itemIfNone item to use if none is found * @return the single element in {@code iterable}, or {@code null} if no * element found. * @throws NoSuchElementException if more than one element was found. */ public static <T> T single( Iterable<T> iterable, T itemIfNone ) { return Iterators.single( iterable.iterator(), itemIfNone ); }
private static Node findNode( GraphDatabaseService db, Label label, String property, String value ) { try ( ResourceIterator<Node> nodes = db.findNodes( label, property, value ) ) { return Iterators.single( nodes ); } }
assertNotNull( Iterators.single( Iterators.filter( nodeFilter( id ), allNodes.iterator() ) ) );
@Test void shouldReportOwningNodeRecordNotPointingBack() { // given long nodeId = 12L; Collection<DynamicRecord> validLabelRecords = new ArrayList<>(); DynamicRecord dynamicRecord = inUse( new DynamicRecord( 0 ) ); allocateFromNumbers( validLabelRecords, new long[] {nodeId}, new ReusableRecordsAllocator( 66, dynamicRecord ) ); Collection<DynamicRecord> fakePointedToRecords = new ArrayList<>(); DynamicRecord dynamicRecord1 = inUse( new DynamicRecord( 1 ) ); allocateFromNumbers( fakePointedToRecords, new long[] {nodeId}, new ReusableRecordsAllocator( 66, dynamicRecord1 ) ); NodeRecord nodeRecord = inUse( new NodeRecord( nodeId, false, -1, -1 ) ); nodeRecord.setLabelField( dynamicPointer( fakePointedToRecords ), fakePointedToRecords ); add( nodeRecord ); // when DynamicLabelConsistencyReport report = check( Iterators.single( validLabelRecords.iterator() ) ); // then verify( report ).orphanDynamicLabelRecordDueToInvalidOwner( nodeRecord ); } }
@Test public void shouldDropUniquenessConstraintWithBackingIndexHavingNoOwner() throws Exception { // given try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( label ).assertPropertyIsUnique( key ).create(); tx.success(); } // when intentionally breaking the schema by setting the backing index rule to unused RecordStorageEngine storageEngine = db.getDependencyResolver().resolveDependency( RecordStorageEngine.class ); SchemaStore schemaStore = storageEngine.testAccessNeoStores().getSchemaStore(); SchemaRule indexRule = single( filter( rule -> rule instanceof StoreIndexDescriptor, schemaStore.loadAllSchemaRules() ) ); setOwnerNull( schemaStore, (StoreIndexDescriptor) indexRule ); // At this point the SchemaCache doesn't know about this change so we have to reload it storageEngine.loadSchemaCache(); try ( Transaction tx = db.beginTx() ) { single( db.schema().getConstraints( label ).iterator() ).drop(); tx.success(); } // then try ( Transaction ignore = db.beginTx() ) { assertFalse( db.schema().getConstraints().iterator().hasNext() ); assertFalse( db.schema().getIndexes().iterator().hasNext() ); } }
@Test public void shouldDropUniquenessConstraintWithBackingIndexNotInUse() { // given try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( label ).assertPropertyIsUnique( key ).create(); tx.success(); } // when intentionally breaking the schema by setting the backing index rule to unused RecordStorageEngine storageEngine = db.getDependencyResolver().resolveDependency( RecordStorageEngine.class ); SchemaStore schemaStore = storageEngine.testAccessNeoStores().getSchemaStore(); SchemaRule indexRule = single( filter( rule -> rule instanceof StoreIndexDescriptor, schemaStore.loadAllSchemaRules() ) ); setSchemaRecordNotInUse( schemaStore, indexRule.getId() ); // At this point the SchemaCache doesn't know about this change so we have to reload it storageEngine.loadSchemaCache(); try ( Transaction tx = db.beginTx() ) { single( db.schema().getConstraints( label ).iterator() ).drop(); tx.success(); } // then try ( Transaction ignore = db.beginTx() ) { assertFalse( db.schema().getConstraints().iterator().hasNext() ); assertFalse( db.schema().getIndexes().iterator().hasNext() ); } }
SchemaRule constraintRule = single( filter( rule -> rule instanceof ConstraintRule, schemaStore.loadAllSchemaRules() ) ); setSchemaRecordNotInUse( schemaStore, constraintRule.getId() ); SchemaRule indexRule = single( filter( rule -> rule instanceof StoreIndexDescriptor, schemaStore.loadAllSchemaRules() ) ); setOwnerNull( schemaStore, (StoreIndexDescriptor) indexRule );
Node node = Iterators.single( db.findNodes( markerLabel ) ); assertEquals( propertyValue, node.getProperty( testPropertyKey ) ); tx.success(); Node node = Iterators.single( db.findNodes( markerLabel ) ); node.removeProperty( testPropertyKey ); tx.success(); Node node = Iterators.single( db.findNodes( markerLabel ) ); assertFalse( node.hasProperty( testPropertyKey ) ); tx.success();
Node node = Iterators.single( db.findNodes( markerLabel ) ); assertArrayEquals( propertyValue, (byte[]) node.getProperty( testPropertyKey ) ); tx.success(); Node node = Iterators.single( db.findNodes( markerLabel ) ); node.removeProperty( testPropertyKey ); tx.success(); Node node = Iterators.single( db.findNodes( markerLabel ) ); assertFalse( node.hasProperty( testPropertyKey ) ); tx.success();
private static void assertDbAccessible( GraphDatabaseService db ) { Label label = () -> "Person"; String propertyKey = "name"; String propertyValue = "Thor Odinson"; try ( Transaction tx = db.beginTx() ) { db.createNode( label ).setProperty( propertyKey, propertyValue ); tx.success(); } try ( Transaction tx = db.beginTx() ) { Node node = single( db.findNodes( label ) ); assertEquals( propertyValue, node.getProperty( propertyKey ) ); tx.success(); } }
@Test public void shouldAllowMultilineFieldsWhenEnabled() throws Exception { // GIVEN File data = data( ":ID,name", "1,\"This is a line with\nnewlines in\"" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--multiline-fields", "true" ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { ResourceIterator<Node> allNodes = db.getAllNodes().iterator(); Node node = Iterators.single( allNodes ); allNodes.close(); assertEquals( "This is a line with\nnewlines in", node.getProperty( "name" ) ); tx.success(); } }
@Test public void shouldNotTrimStringsByDefault() throws Exception { // GIVEN String name = " This is a line with leading and trailing whitespaces "; File data = data( ":ID,name", "1,\"" + name + "\""); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath() ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { ResourceIterator<Node> allNodes = db.getAllNodes().iterator(); Node node = Iterators.single( allNodes ); allNodes.close(); assertEquals( name, node.getProperty( "name" ) ); tx.success(); } }
@Test public void shouldDropUniquenessConstraintWhereConstraintRecordIsMissing() throws Exception { // given try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( label ).assertPropertyIsUnique( key ).create(); tx.success(); } // when intentionally breaking the schema by setting the backing index rule to unused RecordStorageEngine storageEngine = db.getDependencyResolver().resolveDependency( RecordStorageEngine.class ); SchemaStore schemaStore = storageEngine.testAccessNeoStores().getSchemaStore(); SchemaRule indexRule = single( filter( rule -> rule instanceof ConstraintRule, schemaStore.loadAllSchemaRules() ) ); setSchemaRecordNotInUse( schemaStore, indexRule.getId() ); // At this point the SchemaCache doesn't know about this change so we have to reload it storageEngine.loadSchemaCache(); try ( Transaction tx = db.beginTx() ) { // We don't use single() here, because it is okay for the schema cache reload to clean up after us. db.schema().getConstraints( label ).forEach( ConstraintDefinition::drop ); db.schema().getIndexes( label ).forEach( IndexDefinition::drop ); tx.success(); } // then try ( Transaction ignore = db.beginTx() ) { assertFalse( db.schema().getConstraints().iterator().hasNext() ); assertFalse( db.schema().getIndexes().iterator().hasNext() ); } }
@Test public void shouldScanSingleRange() { // GIVEN int labelId1 = 1; int labelId2 = 2; long nodeId1 = 10; long nodeId2 = 11; start( asList( labelChanges( nodeId1, NO_LABELS, new long[]{labelId1} ), labelChanges( nodeId2, NO_LABELS, new long[]{labelId1, labelId2} ) ) ); // WHEN BoundedIterable<NodeLabelRange> reader = store.allNodeLabelRanges(); NodeLabelRange range = single( reader.iterator() ); // THEN assertArrayEquals( new long[]{nodeId1, nodeId2}, reducedNodes( range ) ); assertArrayEquals( new long[]{labelId1}, sorted( range.labels( nodeId1 ) ) ); assertArrayEquals( new long[]{labelId1, labelId2}, sorted( range.labels( nodeId2 ) ) ); }
private void assertDbAccessibleAsEmbedded() { GraphDatabaseAPI db = db(); Label label = () -> "Node"; String propertyKey = "key"; String propertyValue = "value"; try ( Transaction tx = db.beginTx() ) { db.createNode( label ).setProperty( propertyKey, propertyValue ); tx.success(); } try ( Transaction tx = db.beginTx() ) { Node node = single( db.findNodes( label ) ); assertEquals( propertyValue, node.getProperty( propertyKey ) ); tx.success(); } }
single( db.findNodes( label ) ), equalTo( node ) );