コード例 #1
0
  @Test
  public void basicProperties() throws Exception {
    GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
    PropertyContainer graphProperties = properties(db);
    assertThat(graphProperties, inTx(db, not(hasProperty("test"))));

    Transaction tx = db.beginTx();
    graphProperties.setProperty("test", "yo");
    assertEquals("yo", graphProperties.getProperty("test"));
    tx.success();
    tx.finish();
    assertThat(graphProperties, inTx(db, hasProperty("test").withValue("yo")));
    tx = db.beginTx();
    assertNull(graphProperties.removeProperty("something non existent"));
    assertEquals("yo", graphProperties.removeProperty("test"));
    assertNull(graphProperties.getProperty("test", null));
    graphProperties.setProperty("other", 10);
    assertEquals(10, graphProperties.getProperty("other"));
    graphProperties.setProperty("new", "third");
    tx.success();
    tx.finish();
    assertThat(graphProperties, inTx(db, not(hasProperty("test"))));
    assertThat(graphProperties, inTx(db, hasProperty("other").withValue(10)));
    assertThat(getPropertyKeys(db, graphProperties), containsOnly("other", "new"));

    tx = db.beginTx();
    graphProperties.setProperty("rollback", true);
    assertEquals(true, graphProperties.getProperty("rollback"));
    tx.finish();
    assertThat(graphProperties, inTx(db, not(hasProperty("rollback"))));
    db.shutdown();
  }
コード例 #2
0
 private void setSuccess(GraphDatabaseAPI graphdb, boolean success) {
   try (Transaction tx = graphdb.beginTx()) {
     Node node = graphdb.createNode();
     node.setProperty("success", success);
     tx.success();
   }
 }
コード例 #3
0
  @Test
  public void setManyGraphProperties() throws Exception {
    GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase();

    Transaction tx = db.beginTx();
    Object[] values =
        new Object[] {
          10,
          "A string value",
          new float[] {1234.567F, 7654.321F},
          "A rather longer string which wouldn't fit inlined #!)(&¤"
        };
    int count = 200;
    for (int i = 0; i < count; i++) {
      properties(db).setProperty("key" + i, values[i % values.length]);
    }
    tx.success();
    tx.finish();

    for (int i = 0; i < count; i++) {
      assertThat(
          properties(db), inTx(db, hasProperty("key" + i).withValue(values[i % values.length])));
    }
    clearCache(db);
    for (int i = 0; i < count; i++) {
      assertThat(
          properties(db), inTx(db, hasProperty("key" + i).withValue(values[i % values.length])));
    }
    db.shutdown();
  }
コード例 #4
0
 protected Relationship[] createRelationshipChain(RelationshipType type, int length) {
   try (Transaction transaction = db.beginTx()) {
     Relationship[] relationshipChain = createRelationshipChain(db.createNode(), type, length);
     transaction.success();
     return relationshipChain;
   }
 }
コード例 #5
0
 protected void assertNodeExists(long id) {
   try (Transaction ignore = db.beginTx()) {
     db.getNodeById(id);
   } catch (NotFoundException e) {
     fail("Node " + id + " should exist");
   }
 }
コード例 #6
0
 public Response<IdAllocation> allocateIds(IdType idType) {
   IdGenerator generator = graphDb.getIdGeneratorFactory().get(idType);
   IdAllocation result =
       new IdAllocation(
           generator.nextIdBatch(ID_GRAB_SIZE), generator.getHighId(), generator.getDefragCount());
   return ServerUtil.packResponseWithoutTransactionStream(graphDb.getStoreId(), result);
 }
コード例 #7
0
 @Override
 public void run(GraphDatabaseAPI graphdb) {
   try (Transaction ignored = graphdb.beginTx()) {
     for (int i = 0; i < count; i++)
       graphdb.index().forNodes("index" + i).get("name", i).getSingle();
   }
   if (resume) resumeFlushThread();
 }
コード例 #8
0
 @Override
 public void run(GraphDatabaseAPI graphdb) {
   try (Transaction tx = graphdb.beginTx()) {
     for (int i = 0; i < 3; i++)
       graphdb.index().forNodes("index" + i).add(graphdb.createNode(), "name", "" + i);
     tx.success();
   }
 }
コード例 #9
0
 protected void assertNodeDoesntExist(long id) {
   try (Transaction ignore = db.beginTx()) {
     db.getNodeById(id);
     fail("Relationship " + id + " shouldn't exist");
   } catch (NotFoundException e) {
     // Good
   }
 }
コード例 #10
0
 public MasterImpl(GraphDatabaseAPI db, Logging logging, Config config) {
   this.graphDb = db;
   this.msgLog = logging.getMessagesLog(getClass());
   this.config = config;
   graphProperties =
       graphDb.getDependencyResolver().resolveDependency(NodeManager.class).getGraphProperties();
   txManager = graphDb.getDependencyResolver().resolveDependency(TransactionManager.class);
 }
コード例 #11
0
ファイル: IndexNodeDocIT.java プロジェクト: hikarusw/neo4j
 long createNode() {
   GraphDatabaseAPI graphdb = server().getDatabase().getGraph();
   try (Transaction tx = graphdb.beginTx()) {
     Node node = graphdb.createNode();
     tx.success();
     return node.getId();
   }
 }
コード例 #12
0
 @Override
 public void run(GraphDatabaseAPI graphdb) {
   try (Transaction ignored = graphdb.beginTx()) {
     // TODO: Pass a node reference around of assuming the id will be deterministically assigned,
     // artifact of removing the reference node, upon which this test used to depend.
     assertTrue((Boolean) graphdb.getNodeById(3).getProperty("success"));
   }
 }
コード例 #13
0
 private Set<Node> doIndexLookup(Label myLabel, Object value) {
   Transaction tx = db.beginTx();
   Iterable<Node> iter = db.findNodesByLabelAndProperty(myLabel, NUM_BANANAS_KEY, value);
   Set<Node> nodes = asUniqueSet(iter);
   tx.success();
   tx.finish();
   return nodes;
 }
コード例 #14
0
 private void createARelationship(GraphDatabaseAPI db) {
   Transaction tx = db.beginTx();
   Node node1 = db.createNode();
   Node node2 = db.createNode();
   node1.createRelationshipTo(node2, withName("friend"));
   tx.success();
   tx.finish();
 }
コード例 #15
0
 public Response<Pair<Integer, Long>> getMasterIdForCommittedTx(long txId, StoreId storeId) {
   XaDataSource nioneoDataSource = graphDb.getXaDataSourceManager().getNeoStoreDataSource();
   try {
     Pair<Integer, Long> masterId = nioneoDataSource.getMasterForCommittedTx(txId);
     return ServerUtil.packResponseWithoutTransactionStream(graphDb.getStoreId(), masterId);
   } catch (IOException e) {
     throw new RuntimeException("Couldn't get master ID for " + txId, e);
   }
 }
コード例 #16
0
ファイル: TestLogPruning.java プロジェクト: rocio/neo4j
 private void doTransaction() {
   Transaction tx = db.beginTx();
   try {
     db.createNode();
     tx.success();
   } finally {
     tx.finish();
   }
 }
コード例 #17
0
 protected Node getCurrentNode() throws RemoteException, ShellException {
   Serializable current =
       shellServer.interpretVariable(shellClient.getId(), Variables.CURRENT_KEY);
   int nodeId = parseInt(current.toString().substring(1));
   try (Transaction tx = db.beginTx()) {
     Node nodeById = db.getNodeById(nodeId);
     tx.success();
     return nodeById;
   }
 }
コード例 #18
0
 private void updateNode(long nodeId, int value) {
   Transaction tx = db.beginTx();
   try {
     Node node = db.getNodeById(nodeId);
     node.setProperty(NUM_BANANAS_KEY, value);
     tx.success();
   } finally {
     tx.finish();
   }
 }
コード例 #19
0
  private void createIndex(Label label) {
    Transaction tx = db.beginTx();
    IndexDefinition definition = db.schema().indexFor(label).on(NUM_BANANAS_KEY).create();
    tx.success();
    tx.finish();

    tx = db.beginTx();
    db.schema().awaitIndexOnline(definition, 10, TimeUnit.SECONDS);
    tx.finish();
  }
コード例 #20
0
 @Before
 public void before() {
   GraphDatabaseAPI graphDatabaseAPI = dbRule.getGraphDatabaseAPI();
   this.db = graphDatabaseAPI;
   DependencyResolver dependencyResolver = graphDatabaseAPI.getDependencyResolver();
   this.bridge = dependencyResolver.resolveDependency(ThreadToStatementContextBridge.class);
   graphDatabaseAPI
       .getDependencyResolver()
       .resolveDependency(Monitors.class)
       .addMonitorListener(indexOnlineMonitor);
 }
コード例 #21
0
 @Test
 public void testSetSoftRefCache() {
   ArrayList<CacheProvider> cacheList = new ArrayList<>();
   TestGraphDatabaseFactory gdbf = new TestGraphDatabaseFactory();
   cacheList.add(new SoftCacheProvider());
   gdbf.setCacheProviders(cacheList);
   GraphDatabaseAPI db = (GraphDatabaseAPI) gdbf.newImpermanentDatabase();
   assertEquals(
       SoftCacheProvider.NAME,
       db.getDependencyResolver().resolveDependency(NodeManager.class).getCacheType().getName());
 }
コード例 #22
0
 private long createNode(Label label, int number)
     throws PropertyKeyNotFoundException, LabelNotFoundKernelException {
   Transaction tx = db.beginTx();
   try {
     Node node = db.createNode(label);
     node.setProperty(NUM_BANANAS_KEY, number);
     tx.success();
     return node.getId();
   } finally {
     tx.finish();
   }
 }
コード例 #23
0
 @Override
 public Response<Void> pushTransaction(RequestContext context, String resourceName, long tx) {
   graphDb
       .getTxIdGenerator()
       .committed(
           graphDb.getXaDataSourceManager().getXaDataSource(resourceName),
           context.getEventIdentifier(),
           tx,
           context.machineId());
   return new Response<Void>(
       null, graphDb.getStoreId(), TransactionStream.EMPTY, ResourceReleaser.NO_OP);
 }
コード例 #24
0
  @Test
  public void twoUncleanInARow() throws Exception {
    String storeDir = new File("dir").getAbsolutePath();
    EphemeralFileSystemAbstraction snapshot = produceUncleanStore(fs.get(), storeDir);
    snapshot = produceUncleanStore(snapshot, storeDir);
    snapshot = produceUncleanStore(snapshot, storeDir);

    GraphDatabaseAPI db =
        (GraphDatabaseAPI)
            new TestGraphDatabaseFactory().setFileSystem(snapshot).newImpermanentDatabase(storeDir);
    assertThat(properties(db), inTx(db, hasProperty("prop").withValue("Some value")));
    db.shutdown();
  }
コード例 #25
0
 protected Relationship[] createRelationshipChain(
     Node startingFromNode, RelationshipType type, int length) {
   try (Transaction tx = db.beginTx()) {
     Relationship[] rels = new Relationship[length];
     Node firstNode = startingFromNode;
     for (int i = 0; i < rels.length; i++) {
       Node secondNode = db.createNode();
       rels[i] = firstNode.createRelationshipTo(secondNode, type);
       firstNode = secondNode;
     }
     tx.success();
     return rels;
   }
 }
コード例 #26
0
 private Relationship relationship(long id) throws RelationshipNotFoundException {
   try {
     return graphDb.getRelationshipById(id);
   } catch (NotFoundException e) {
     throw new RelationshipNotFoundException();
   }
 }
コード例 #27
0
 private Node node(long id) throws NodeNotFoundException {
   try {
     return graphDb.getNodeById(id);
   } catch (NotFoundException e) {
     throw new NodeNotFoundException(e);
   }
 }
コード例 #28
0
  private void tryOnce(final GraphDatabaseAPI db, final Node node, int iterations)
      throws Throwable {
    db.getDependencyResolver().resolveDependency(Caches.class).clear();
    ExecutorService executor = newCachedThreadPool();
    final CountDownLatch startSignal = new CountDownLatch(1);
    int threads = getRuntime().availableProcessors();
    final List<Throwable> errors = Collections.synchronizedList(new ArrayList<Throwable>());
    for (int i = 0; i < threads; i++) {
      executor.submit(
          new Runnable() {
            @Override
            public void run() {
              awaitStartSignalAndRandomTimeLonger(startSignal);
              Transaction transaction = db.beginTx();
              try {
                assertEquals(relCount, count(node.getRelationships()));
              } catch (Throwable e) {
                errors.add(e);
              } finally {
                transaction.finish();
              }
            }
          });
    }
    startSignal.countDown();
    executor.shutdown();
    executor.awaitTermination(10, SECONDS);

    if (!errors.isEmpty()) {
      throw new MultipleCauseException(
          format("Exception(s) after %s iterations with %s threads", iterations, threads), errors);
    }
  }
コード例 #29
0
 private void flushAll() {
   db.getDependencyResolver()
       .resolveDependency(XaDataSourceManager.class)
       .getNeoStoreDataSource()
       .getNeoStore()
       .flushAll();
 }
コード例 #30
0
 private int sizeOfRelationship(Relationship relationship) {
   try (Transaction ignore = db.beginTx()) {
     return getRelationshipCache()
         .get(relationship.getId())
         .sizeOfObjectInBytesIncludingOverhead();
   }
 }