Java 类org.neo4j.graphdb.schema.IndexDefinition 实例源码

项目:neo4j-lucene5-index    文件:IndexConstraintsTest.java   
@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 = first( graphDb.schema().getIndexes( LABEL ) );
        index.drop();

        graphDb.schema().constraintFor( LABEL ).assertPropertyIsUnique( PROPERTY_KEY ).create();
        tx.success();
    }
    // assert no exception is thrown
}
项目:neo4j-lucene5-index    文件:IndexConstraintsTest.java   
private IndexDefinition getIndex( Label label, String propertyKey )
{
    try ( Transaction tx = graphDb.beginTx() )
    {
        IndexDefinition found = null;
        for ( IndexDefinition index : graphDb.schema().getIndexes( label ) )
        {
            if ( propertyKey.equals( single( index.getPropertyKeys() ) ) )
            {
                assertNull( "Found multiple indexes.", found );
                found = index;
            }
        }
        tx.success();
        return found;
    }
}
项目:neo4j-lucene5-index    文件:LuceneIndexRecoveryIT.java   
@Test
public void changeShouldBeIdempotentWhenDoingRecovery() throws Exception
{
    // Given
    startDb( createLuceneIndexFactory() );

    IndexDefinition indexDefinition = createIndex( myLabel );
    waitForIndex( indexDefinition );

    long node = createNode( myLabel, 12 );
    rotateLogsAndCheckPoint();

    updateNode( node, 13 );

    // And Given
    killDb();

    // When
    startDb( createLuceneIndexFactory() );

    // Then
    assertEquals( 0, doIndexLookup( myLabel, 12 ).size() );
    assertEquals( 1, doIndexLookup( myLabel, 13 ).size() );
}
项目:neo4j-lucene5-index    文件:LuceneIndexRecoveryIT.java   
@Test
public void removeShouldBeIdempotentWhenDoingRecovery() throws Exception
{
    // Given
    startDb( createLuceneIndexFactory() );

    IndexDefinition indexDefinition = createIndex( myLabel );
    waitForIndex( indexDefinition );

    long node = createNode( myLabel, 12 );
    rotateLogsAndCheckPoint();

    deleteNode( node );

    // And Given
    killDb();

    // When
    startDb( createLuceneIndexFactory() );

    // Then
    assertEquals( 0, doIndexLookup( myLabel, 12 ).size() );
}
项目:neo4j-lucene5-index    文件:LuceneIndexRecoveryIT.java   
@Test
public void shouldNotUpdateTwiceDuringRecovery() throws Exception
{
    // Given
    startDb( createLuceneIndexFactory() );

    IndexDefinition indexDefinition = createIndex( myLabel );
    waitForIndex( indexDefinition );

    long nodeId = createNode( myLabel, 12 );
    updateNode( nodeId, 14 );

    // And Given
    killDb();

    // When
    startDb( createLuceneIndexFactory() );

    // Then
    assertEquals( 0, doIndexLookup( myLabel, 12 ).size() );
    assertEquals( 1, doIndexLookup( myLabel, 14 ).size() );
}
项目:neo4j-lucene5-index    文件:SchemaIndexAcceptanceTest.java   
@Test
public void recoveryAfterCreateAndDropIndex() throws Exception
{
    // GIVEN
    IndexDefinition indexDefinition = createIndex( db, label, propertyKey );
    createSomeData( label, propertyKey );
    doStuff( db, label, propertyKey );
    dropIndex( indexDefinition );
    doStuff( db, label, propertyKey );

    // WHEN
    crashAndRestart();

    // THEN
    assertThat( getIndexes( db, label ), isEmpty() );
}
项目:SciGraph    文件:Neo4jModule.java   
public static void setupSchemaIndexes(GraphDatabaseService graphDb, Neo4jConfiguration config) {
  Map<String, Set<String>> schemaIndexes = config.getSchemaIndexes();
  for (Map.Entry<String, Set<String>> entry : schemaIndexes.entrySet()) {
    Label label = Label.label(entry.getKey());
    for (String property : entry.getValue()) {
      try (Transaction tx = graphDb.beginTx()) {
        Schema schema = graphDb.schema();
        IndexDefinition indexDefinition = schema.indexFor(label).on(property).create();
        tx.success();
        tx.close();

        Transaction tx2 = graphDb.beginTx();
        schema.awaitIndexOnline(indexDefinition, 2, TimeUnit.MINUTES);
        tx2.success();
        tx2.close();
      }
    }
  }
}
项目:WikiN-G-ER    文件:WikiNerGraphConnector.java   
private WikiNerGraphConnector(String dbDir) {
    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(dbDir);
    IndexDefinition indexDefinition;
    Schema schema;
    Transaction tx = graphDb.beginTx(); 

    schema = graphDb.schema();
    IndexManager index = graphDb.index();
    entities = index.forNodes(entityLabel.name());
    cities = index.forNodes(cityLabel.name());
    try{
         indexDefinition = schema.indexFor( entityLabel ).on( "nameType" ).create();
    }catch(Exception e){

    }

    tx.success();
    tx.close();
}
项目:neoprofiler    文件:SchemaProfiler.java   
public NeoProfile run(NeoProfiler parent) {
    SchemaProfile p = new SchemaProfile();

    try(Transaction tx = parent.getDB().beginTx()) {
        Schema schema = parent.getDB().schema();

        Iterator<ConstraintDefinition> constraints = schema.getConstraints().iterator();

        while(constraints.hasNext()) {          
            ConstraintDefinition c = constraints.next();                
            p.addConstraint(new NeoConstraint(true, false, c.getPropertyKeys(), c.getLabel(), c.getConstraintType()));              
        }

        Iterator<IndexDefinition> idxs = schema.getIndexes().iterator();
        while(idxs.hasNext()) {
            IndexDefinition idx = idxs.next();
            p.addConstraint(new NeoConstraint(idx.isConstraintIndex(), true, idx.getPropertyKeys(), idx.getLabel(), null));             
        }
    }
    return p;
}
项目:pheno4j    文件:DatabaseIndexCreator.java   
private boolean doesIndexExist(Iterable<IndexDefinition> indexes, String name, String on) {
    for (IndexDefinition index : indexes) {
        if (index.getLabel().equals(Label.label(name))
                && StreamSupport.stream(index.getPropertyKeys().spliterator(), false).anyMatch(on::equals)) {
            return true;
        }
    }
    return false;
}
项目:neo4j-lucene5-index    文件:TestLuceneSchemaBatchInsertIT.java   
@Test
public void shouldLoadAndUseLuceneProvider() throws Exception
{
    // GIVEN
    File storeDir = testDirectory.graphDbDir();
    BatchInserter inserter = BatchInserters.inserter( storeDir );
    inserter.createDeferredSchemaIndex( LABEL ).on( "name" ).create();

    // WHEN
    inserter.createNode( map( "name", "Mattias" ), LABEL );
    inserter.shutdown();

    // THEN
    GraphDatabaseFactory graphDatabaseFactory = new TestGraphDatabaseFactory();
    GraphDatabaseAPI db = (GraphDatabaseAPI) graphDatabaseFactory.newEmbeddedDatabase( storeDir );
    DependencyResolver dependencyResolver = db.getDependencyResolver();
    SchemaIndexProvider schemaIndexProvider = dependencyResolver.resolveDependency(
            SchemaIndexProvider.class,
            SchemaIndexProvider.HIGHEST_PRIORITIZED_OR_NONE );

    // assert the indexProvider is a Lucene one
    try ( Transaction ignore = db.beginTx() )
    {
        IndexDefinition indexDefinition = single( db.schema().getIndexes( LABEL ) );
        assertThat( db.schema().getIndexState( indexDefinition ), is( Schema.IndexState.ONLINE ) );
        assertThat( schemaIndexProvider, instanceOf( LuceneSchemaIndexProvider.class ) );
    }

    // CLEANUP
    db.shutdown();
}
项目:neo4j-lucene5-index    文件:IndexConstraintsTest.java   
@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 = first( graphDb.schema().getIndexes( LABEL ) );
        index.drop();

        graphDb.schema().constraintFor( LABEL ).assertPropertyIsUnique( PROPERTY_KEY ).create();
        tx.success();
    }
    // assert no exception is thrown
}
项目:neo4j-lucene5-index    文件:IndexConstraintsTest.java   
private IndexDefinition recreate( IndexDefinition index, int times )
{
    for ( int i = 0; i < times; i++ )
    {
        index.drop();
        index = graphDb.schema()
                .indexFor( index.getLabel() )
                .on( single( index.getPropertyKeys() ) )
                .create();
    }
    return index;
}
项目:neo4j-lucene5-index    文件:LuceneIndexRecoveryIT.java   
@Test
public void addShouldBeIdempotentWhenDoingRecovery() throws Exception
{
    // Given
    startDb( createLuceneIndexFactory() );

    IndexDefinition index = createIndex( myLabel );
    waitForIndex( index );

    long nodeId = createNode( myLabel, 12 );
    try(Transaction tx = db.beginTx())
    {
        assertNotNull( db.getNodeById( nodeId ) );
    }
    assertEquals( 1, doIndexLookup( myLabel, 12 ).size() );

    // And Given
    killDb();

    // When
    startDb( createLuceneIndexFactory() );

    // Then
    try(Transaction tx = db.beginTx())
    {
        assertNotNull( db.getNodeById( nodeId ) );
    }
    assertEquals( 1, doIndexLookup( myLabel, 12 ).size() );
}
项目:neo4j-lucene5-index    文件:LuceneIndexRecoveryIT.java   
@Test
public void shouldNotAddTwiceDuringRecoveryIfCrashedDuringPopulation() throws Exception
{
    // Given
    startDb( createAlwaysInitiallyPopulatingLuceneIndexFactory() );

    IndexDefinition indexDefinition = createIndex( myLabel );
    waitForIndex( indexDefinition );

    long nodeId = createNode( myLabel, 12 );
    assertEquals( 1, doIndexLookup( myLabel, 12 ).size() );

    // And Given
    killDb();

    // When
    startDb( createAlwaysInitiallyPopulatingLuceneIndexFactory() );

    try ( Transaction tx = db.beginTx() )
    {
        IndexDefinition index = db.schema().getIndexes().iterator().next();
        waitForIndex( index );

        // Then
        assertEquals( 12, db.getNodeById( nodeId ).getProperty( NUM_BANANAS_KEY ) );
        assertEquals( 1, doIndexLookup( myLabel, 12 ).size() );
    }
}
项目:neo4j-lucene5-index    文件:LuceneIndexRecoveryIT.java   
private IndexDefinition createIndex( Label label )
{
    try ( Transaction tx = db.beginTx() )
    {
        IndexDefinition definition = db.schema().indexFor( label ).on( NUM_BANANAS_KEY ).create();
        tx.success();
        return definition;
    }
}
项目:neo4j-lucene5-index    文件:LuceneIndexRecoveryIT.java   
private void waitForIndex( IndexDefinition definition )
{
    try ( Transaction tx = db.beginTx() )
    {
        db.schema().awaitIndexOnline( definition, 10, TimeUnit.SECONDS );
        tx.success();
    }
}
项目:neo4j-lucene5-index    文件:SchemaIndexAcceptanceTest.java   
private void dropIndex( IndexDefinition indexDefinition )
{
    try ( Transaction tx = db.beginTx() )
    {
        indexDefinition.drop();
        tx.success();
    }
}
项目:dswarm-graph-neo4j    文件:SchemaIndexUtils.java   
private static void getOrCreateSchemaIndex(final Label label, final String property, final GraphDatabaseService database, final String databaseIdentifier) throws DMPGraphException {

        final IndexDefinition indexDefinition = SchemaIndexUtils.getOrCreateIndex(label, property, database, databaseIdentifier);

        if (indexDefinition == null) {

            throw new DMPGraphException(
                    String.format("something went wrong while index determination/creation for label '%s' and property '%s' at database '%s'", label.name(),
                            property, databaseIdentifier));
        }
    }
项目:csvtreeloader    文件:CSVTreeBuilder.java   
private RootTreeNodeBuilder(ColumnSpec columnSpec, RelationshipType child, GraphDatabaseService db, ExecutionEngine engine) {
    super(columnSpec, null, child);
    this.db = db;
    this.engine = engine;
    try (Transaction tx = db.beginTx()) {
        boolean indexExists = false;
        for (IndexDefinition index : db.schema().getIndexes(this.column.label)) {
            ArrayList<String> keys = new ArrayList<String>();
            for (String key : index.getPropertyKeys()) {
                keys.add(key);
            }
            if (keys.size() != 1 || !keys.get(0).equals(this.column.property)) {
                throw new RuntimeException("Schema Index for " + this.column.label + "." + this.column.property
                        + " cannot be made because different index for " + this.column.label + "." + keys
                        + " already exists");
            }
            indexExists = true;
        }
        if (!indexExists) {
            db.schema().constraintFor(this.column.label).assertPropertyIsUnique(this.column.property).create();
        }
        tx.success();
    }
    queryString = "MERGE (n:" + column.label + " {" + column.property + ": {" + column.property + "}}) RETURN n";
    try (Transaction tx = db.beginTx()) {
        ResourceIterator<Object> resultIterator = engine.execute("MATCH (n:" + column.label + ") RETURN n").columnAs(
                "n");
        while (resultIterator.hasNext()) {
            Node node = (Node) resultIterator.next();
            cachedRoots.put(node.getProperty(column.property).toString(), node);
        }
        tx.success();
        debug("Cached " + cachedRoots.size() + " existing tree roots with Label '" + this.column.label + "'");
    }
}
项目:checklistbank    文件:NormalizerTest.java   
@Test
public void testNeoIndices() throws Exception {
  final UUID datasetKey = datasetKey(1);

  Normalizer norm = Normalizer.create(cfg, datasetKey);
  norm.run();

  openDb(datasetKey);
  compareStats(norm.getStats());

  Set<String> taxonIndices = Sets.newHashSet();
  taxonIndices.add(NeoProperties.TAXON_ID);
  taxonIndices.add(NeoProperties.SCIENTIFIC_NAME);
  taxonIndices.add(NeoProperties.CANONICAL_NAME);
  try (Transaction tx = beginTx()) {
    Schema schema = dao.getNeo().schema();
    for (IndexDefinition idf : schema.getIndexes(Labels.TAXON)) {
      List<String> idxProps = Iterables.asList(idf.getPropertyKeys());
      assertTrue(idxProps.size() == 1);
      assertTrue(taxonIndices.remove(idxProps.get(0)));
    }

    assertNotNull(Iterators.singleOrNull(dao.getNeo().findNodes(Labels.TAXON, NeoProperties.TAXON_ID, "1001")));
    assertNotNull(Iterators.singleOrNull(dao.getNeo().findNodes(Labels.TAXON, NeoProperties.SCIENTIFIC_NAME, "Crepis bakeri Greene")));
    assertNotNull(Iterators.singleOrNull(dao.getNeo().findNodes(Labels.TAXON, NeoProperties.CANONICAL_NAME, "Crepis bakeri")));

    assertNull(Iterators.singleOrNull(dao.getNeo().findNodes(Labels.TAXON, NeoProperties.TAXON_ID, "x1001")));
    assertNull(Iterators.singleOrNull(dao.getNeo().findNodes(Labels.TAXON, NeoProperties.SCIENTIFIC_NAME, "xCrepis bakeri Greene")));
    assertNull(Iterators.singleOrNull(dao.getNeo().findNodes(Labels.TAXON, NeoProperties.CANONICAL_NAME, "xCrepis bakeri")));
  }
}
项目:extended-objects    文件:CreateIndexTest.java   
/**
 * Find an index.
 *
 * @param graphDatabaseService
 *            The Graph database service.
 * @param label
 *            The label.
 * @param propertyName
 *            The property name.
 * @return The index or <code>null</code>:
 */
private IndexDefinition findIndex(GraphDatabaseService graphDatabaseService, Label label, String propertyName) {
    final Iterable<IndexDefinition> indexes = graphDatabaseService.schema().getIndexes(label);
    for (IndexDefinition indexDefinition : indexes) {
        for (String key : indexDefinition.getPropertyKeys()) {
            if (key.equals(propertyName)) {
                return indexDefinition;
            }
        }
    }
    return null;
}
项目:neo4art    文件:BatchInserterConnectionManager.java   
/**
 * @see org.neo4art.graphdb.connection.GraphDatabaseConnectionManager#createSchemaIndex(org.neo4j.graphdb.Label, java.lang.String)
 */
@Override
public IndexDefinition createSchemaIndex(Label label, String propertyKey) {

  return batchInserter.createDeferredSchemaIndex(label).on(propertyKey).create();
}
项目:dswarm-graph-neo4j    文件:SchemaIndexUtils.java   
public static IndexDefinition createIndex(final Label label, final String property, final GraphDatabaseService database, final String databaseIdentifier) {

        LOG.debug("try to create index for label = '{}' and property = '{}' at database '{}'", label.name(), property, databaseIdentifier);

        final IndexDefinition indexDefinition;

        try (final Transaction tx = database.beginTx()) {

            final IndexCreator indexCreator = database.schema().indexFor(label).on(property);
            indexDefinition = indexCreator.create();

            LOG.debug("created index for label = '{}' and property = '{}' at database '{}'", label.name(), property, databaseIdentifier);

            tx.success();
            tx.close();
        } catch (final Exception e) {

            LOG.error("sommething went wrong, while index creation for label '{}' and property '{}' at database '{}'", label, property, databaseIdentifier, e);

            return null;
        }

        return bringIndexOnline(label, property, database, databaseIdentifier, indexDefinition);
    }
项目:dswarm-graph-neo4j    文件:SchemaIndexUtils.java   
private static IndexDefinition bringIndexOnline(final Label label, final String property, final GraphDatabaseService database, final String databaseIdentifier,
        final IndexDefinition indexDefinition) {

    try (final Transaction tx = database.beginTx()) {

        LOG.debug("try to bring index online for label = '{}' and property = '{}' at database '{}'", label.name(), property, databaseIdentifier);

        database.schema().awaitIndexOnline(indexDefinition, 5, TimeUnit.SECONDS);

        LOG.debug("brought index online for label = '{}' and property = '{}' at database '{}'", label.name(), property, databaseIdentifier);

        tx.success();
        tx.close();

        return indexDefinition;
    } catch (final Exception e) {

        LOG.error("sommething went wrong, while bringing index online for label '{}' and property '{}' at database '{}'", label, property, databaseIdentifier, e);

        return null;
    }
}
项目:neo4art    文件:EmbeddedDatabaseConnectionManager.java   
/**
 * @see org.neo4art.graphdb.connection.GraphDatabaseConnectionManager#createIndex(org.neo4art.graphdb.indexes.IndexType, java.lang.String)
 */
@Override
public IndexDefinition createSchemaIndex(Label label, String propertyKey) {

  IndexDefinition indexDefinition;

  try (GraphDatabaseTransaction tx = getTransaction()) {

    indexDefinition = graphDatabaseService.schema().indexFor(label).on(propertyKey).create();

    tx.success();
  }

  return indexDefinition;
}
项目:neo4art    文件:GraphDatabaseConnectionManager.java   
/**
 * 
 * @param label
 * @param propertyKey
 * @return
 */
IndexDefinition createSchemaIndex(Label label, String propertyKey);
项目:dswarm-graph-neo4j    文件:MaintainResource.java   
private void deleteSomeSchemaIndices(final GraphDatabaseService database) throws DMPGraphException {

        MaintainResource.LOG.debug("start delete schema indices TX");

        try (final Transaction itx = database.beginTx()) {

            final Schema schema = database.schema();

            if (schema == null) {

                MaintainResource.LOG.debug("no schema available");

                itx.success();
                itx.close();

                return;
            }

            final Iterable<IndexDefinition> indexDefinitions = schema.getIndexes();

            if (indexDefinitions == null) {

                MaintainResource.LOG.debug("no schema indices available");

                itx.success();
                itx.close();

                return;
            }

            for (final IndexDefinition indexDefinition : indexDefinitions) {

                MaintainResource.LOG.debug("drop '{}' : '{}' schema index", indexDefinition.getLabel().name(),
                        indexDefinition.getPropertyKeys().iterator().next());

                indexDefinition.drop();
            }

            itx.success();
            itx.close();
        } catch (final Exception e) {

            final String message = "couldn't finish delete schema indices TX successfully";

            MaintainResource.LOG.error(message, e);

            throw new DMPGraphException(message);
        }

        MaintainResource.LOG.debug("finished delete schema indices TX");
    }