Esempio n. 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();
  }
 private void setSuccess(GraphDatabaseAPI graphdb, boolean success) {
   try (Transaction tx = graphdb.beginTx()) {
     Node node = graphdb.createNode();
     node.setProperty("success", success);
     tx.success();
   }
 }
Esempio n. 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();
  }
Esempio n. 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;
   }
 }
Esempio n. 5
0
 protected void assertNodeExists(long id) {
   try (Transaction ignore = db.beginTx()) {
     db.getNodeById(id);
   } catch (NotFoundException e) {
     fail("Node " + id + " should exist");
   }
 }
Esempio n. 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);
 }
 @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();
 }
 @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();
   }
 }
Esempio n. 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
   }
 }
Esempio n. 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);
 }
Esempio n. 11
0
 long createNode() {
   GraphDatabaseAPI graphdb = server().getDatabase().getGraph();
   try (Transaction tx = graphdb.beginTx()) {
     Node node = graphdb.createNode();
     tx.success();
     return node.getId();
   }
 }
Esempio n. 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"));
   }
 }
Esempio n. 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;
 }
 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();
 }
Esempio n. 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);
   }
 }
Esempio n. 16
0
 private void doTransaction() {
   Transaction tx = db.beginTx();
   try {
     db.createNode();
     tx.success();
   } finally {
     tx.finish();
   }
 }
Esempio n. 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;
   }
 }
Esempio n. 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();
   }
 }
Esempio n. 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();
  }
 @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);
 }
Esempio n. 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());
 }
Esempio n. 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();
   }
 }
Esempio n. 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);
 }
Esempio n. 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();
  }
Esempio n. 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;
   }
 }
Esempio n. 26
0
 private Relationship relationship(long id) throws RelationshipNotFoundException {
   try {
     return graphDb.getRelationshipById(id);
   } catch (NotFoundException e) {
     throw new RelationshipNotFoundException();
   }
 }
Esempio n. 27
0
 private Node node(long id) throws NodeNotFoundException {
   try {
     return graphDb.getNodeById(id);
   } catch (NotFoundException e) {
     throw new NodeNotFoundException(e);
   }
 }
  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);
    }
  }
Esempio n. 29
0
 private void flushAll() {
   db.getDependencyResolver()
       .resolveDependency(XaDataSourceManager.class)
       .getNeoStoreDataSource()
       .getNeoStore()
       .flushAll();
 }
Esempio n. 30
0
 private int sizeOfRelationship(Relationship relationship) {
   try (Transaction ignore = db.beginTx()) {
     return getRelationshipCache()
         .get(relationship.getId())
         .sizeOfObjectInBytesIncludingOverhead();
   }
 }