use of org.neo4j.graphdb.schema.Schema in project neo4j by neo4j.
the class SchemaAcceptanceTest method shouldThrowConstraintViolationIfAskedToIndexPropertyThatIsAlreadyIndexed.
@Test
public void shouldThrowConstraintViolationIfAskedToIndexPropertyThatIsAlreadyIndexed() throws Exception {
// GIVEN
Schema schema;
try (Transaction tx = db.beginTx()) {
schema = db.schema();
schema.indexFor(label).on(propertyKey).create();
tx.success();
}
// WHEN
ConstraintViolationException caught = null;
try (Transaction tx = db.beginTx()) {
schema.indexFor(label).on(propertyKey).create();
tx.success();
} catch (ConstraintViolationException e) {
caught = e;
}
// THEN
assertThat(caught, not(nullValue()));
}
use of org.neo4j.graphdb.schema.Schema in project neo4j by neo4j.
the class LucenePartitionedIndexStressTesting method dropAllIndexes.
private void dropAllIndexes() {
try (Transaction transaction = db.beginTx()) {
Schema schema = db.schema();
schema.getConstraints().forEach(ConstraintDefinition::drop);
schema.getIndexes().forEach(IndexDefinition::drop);
transaction.success();
}
}
use of org.neo4j.graphdb.schema.Schema in project neo4j by neo4j.
the class LucenePartitionedIndexStressTesting method awaitIndexesOnline.
private void awaitIndexesOnline(GraphDatabaseService db) {
try (Transaction ignored = db.beginTx()) {
Schema schema = db.schema();
schema.awaitIndexesOnline(WAIT_DURATION_MINUTES, TimeUnit.MINUTES);
}
}
use of org.neo4j.graphdb.schema.Schema in project neo4j-documentation by neo4j.
the class EmbeddedNeo4jWithNewIndexing method main.
public static void main(final String[] args) throws IOException {
System.out.println("Starting database ...");
FileUtils.deleteRecursively(databaseDirectory);
// START SNIPPET: startDb
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(databaseDirectory);
// END SNIPPET: startDb
{
// START SNIPPET: createIndex
IndexDefinition indexDefinition;
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
indexDefinition = schema.indexFor(Label.label("User")).on("username").create();
tx.success();
}
// START SNIPPET: wait
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
schema.awaitIndexOnline(indexDefinition, 10, TimeUnit.SECONDS);
}
// START SNIPPET: progress
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
System.out.println(String.format("Percent complete: %1.0f%%", schema.getIndexPopulationProgress(indexDefinition).getCompletedPercentage()));
}
// END SNIPPET: progress
}
{
// START SNIPPET: addUsers
try (Transaction tx = graphDb.beginTx()) {
Label label = Label.label("User");
// Create some users
for (int id = 0; id < 100; id++) {
Node userNode = graphDb.createNode(label);
userNode.setProperty("username", "user" + id + "@neo4j.org");
}
System.out.println("Users created");
tx.success();
}
// END SNIPPET: addUsers
}
{
// START SNIPPET: findUsers
Label label = Label.label("User");
int idToFind = 45;
String nameToFind = "user" + idToFind + "@neo4j.org";
try (Transaction tx = graphDb.beginTx()) {
try (ResourceIterator<Node> users = graphDb.findNodes(label, "username", nameToFind)) {
ArrayList<Node> userNodes = new ArrayList<>();
while (users.hasNext()) {
userNodes.add(users.next());
}
for (Node node : userNodes) {
System.out.println("The username of user " + idToFind + " is " + node.getProperty("username"));
}
}
}
// END SNIPPET: findUsers
}
{
// START SNIPPET: resourceIterator
Label label = Label.label("User");
int idToFind = 45;
String nameToFind = "user" + idToFind + "@neo4j.org";
try (Transaction tx = graphDb.beginTx();
ResourceIterator<Node> users = graphDb.findNodes(label, "username", nameToFind)) {
Node firstUserNode;
if (users.hasNext()) {
firstUserNode = users.next();
}
users.close();
}
// END SNIPPET: resourceIterator
}
{
// START SNIPPET: updateUsers
try (Transaction tx = graphDb.beginTx()) {
Label label = Label.label("User");
int idToFind = 45;
String nameToFind = "user" + idToFind + "@neo4j.org";
for (Node node : loop(graphDb.findNodes(label, "username", nameToFind))) {
node.setProperty("username", "user" + (idToFind + 1) + "@neo4j.org");
}
tx.success();
}
// END SNIPPET: updateUsers
}
{
// START SNIPPET: deleteUsers
try (Transaction tx = graphDb.beginTx()) {
Label label = Label.label("User");
int idToFind = 46;
String nameToFind = "user" + idToFind + "@neo4j.org";
for (Node node : loop(graphDb.findNodes(label, "username", nameToFind))) {
node.delete();
}
tx.success();
}
// END SNIPPET: deleteUsers
}
{
// START SNIPPET: dropIndex
try (Transaction tx = graphDb.beginTx()) {
Label label = Label.label("User");
for (IndexDefinition indexDefinition : graphDb.schema().getIndexes(label)) {
// There is only one index
indexDefinition.drop();
}
tx.success();
}
// END SNIPPET: dropIndex
}
System.out.println("Shutting down database ...");
// START SNIPPET: shutdownDb
graphDb.shutdown();
// END SNIPPET: shutdownDb
}
use of org.neo4j.graphdb.schema.Schema in project neo4j-documentation by neo4j.
the class BlockTypeTest method should_replace_filenames_in_queries.
@Test
public void should_replace_filenames_in_queries() throws Exception {
assumeFalse(SystemUtils.IS_OS_WINDOWS);
// given
List<String> myQuery = Arrays.asList("[source, cypher]", "----", "LOAD CSV FROM \"my_file.csv\" AS line", "RETURN line;", "----");
GraphDatabaseFacade graph = mock(GraphDatabaseFacade.class);
Schema schema = mock(Schema.class);
when(graph.schema()).thenReturn(schema);
doNothing().when(schema).awaitIndexesOnline(anyLong(), any(TimeUnit.class));
when(graph.beginTx()).thenReturn(mock(Transaction.class));
Block block = new Block(myQuery, BlockType.CYPHER);
org.neo4j.graphdb.Result result = mock(org.neo4j.graphdb.Result.class);
ArgumentCaptor<String> fileQuery = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> httpQuery = ArgumentCaptor.forClass(String.class);
when(graph.execute(fileQuery.capture(), eq(Collections.emptyMap()))).thenReturn(result);
state = spy(new State(graph, null, new File("/dev/null"), "http://myurl"));
doReturn("apa").when(state).prettify(httpQuery.capture());
state.knownFiles.add("my_file.csv");
// when
block.process(state);
// then
assertThat(fileQuery.getValue(), containsString("file:/dev/null/my_file.csv"));
assertThat(httpQuery.getValue(), containsString("http://myurl/my_file.csv"));
}
Aggregations