Example #1
0
 private void renderProperties(PropertyRenderer<E> propertyRenderer, PropertyContainer container)
     throws E {
   for (String key : container.getPropertyKeys()) {
     propertyRenderer.renderProperty(key, container.getProperty(key));
   }
   propertyRenderer.done();
 }
Example #2
0
 private void checkProperty(PropertyContainer container, String prop, Object value) {
   if (prop.startsWith("!")) {
     assertFalse(container + " has not " + prop, container.hasProperty(prop.substring(1)));
     return;
   }
   assertEquals(container + " " + prop + ": ", container.getProperty(prop), value);
 }
Example #3
0
 private void loadProperties(PropertyContainer entity) {
   Transaction transaction = db.beginTx();
   try {
     for (String key : entity.getPropertyKeys()) {
       entity.getProperty(key);
     }
   } finally {
     transaction.finish();
   }
 }
Example #4
0
 public <T extends Element> Set<String> getInternalIndexKeys(final Class<T> elementClass) {
   final String propertyName = elementClass.getSimpleName() + INDEXED_KEYS_POSTFIX;
   final PropertyContainer pc = ((GraphDatabaseAPI) this.rawGraph).getKernelData().properties();
   try {
     final String[] keys = (String[]) pc.getProperty(propertyName);
     return new HashSet<String>(Arrays.asList(keys));
   } catch (Exception e) {
     return Collections.emptySet();
   }
 }
Example #5
0
 static void copyProperties(PropertyContainer container, Element element) {
   for (String key : container.getPropertyKeys()) {
     Object property = container.getProperty(key);
     if (property.getClass().isArray()) {
       List<Object> propertyList = new ArrayList<>();
       for (int i = 0; i < Array.getLength(property); i++) {
         propertyList.add(Array.get(property, i));
       }
       property = propertyList;
     }
     element.setProperty(key, property);
   }
 }
Example #6
0
 private <T extends Element> void dropInternalIndexKey(
     final String key, final Class<T> elementClass) {
   final String propertyName = elementClass.getSimpleName() + INDEXED_KEYS_POSTFIX;
   final PropertyContainer pc = ((GraphDatabaseAPI) this.rawGraph).getKernelData().properties();
   try {
     final String[] keys = (String[]) pc.getProperty(propertyName);
     final Set<String> temp = new HashSet<String>(Arrays.asList(keys));
     temp.remove(key);
     pc.setProperty(propertyName, temp.toArray(new String[temp.size()]));
   } catch (Exception e) {
     // no indexed_keys kernel data property
   }
 }
Example #7
0
 private String formatProperties(PropertyContainer pc) {
   StringBuilder result = new StringBuilder();
   List<String> keys = Iterables.toList(pc.getPropertyKeys());
   Collections.sort(keys);
   for (String prop : keys) {
     if (result.length() > 0) {
       result.append(", ");
     }
     result.append(quote(prop)).append(":");
     Object value = pc.getProperty(prop);
     result.append(toString(value));
   }
   return "{" + result + "}";
 }
  public void run(GraphDatabaseService graphDb) {
    timerOff(MAIN_TIMER);
    Transaction tx = graphDb.beginTx();
    try {
      this.container = createContainer(graphDb);
      tx.success();
    } finally {
      tx.finish();
    }
    timerOn(MAIN_TIMER);

    tx = graphDb.beginTx();
    try {
      int max = getNumberOfIterations();
      Object propertyValue = getPropertyValue();
      if (getPropertyValue().getClass().isArray()) {
        int arraySize = Array.getLength(getPropertyValue());
        max /= arraySize;
      }
      for (int i = 0; i < max; i++) {
        container.setProperty("my_key", propertyValue);
      }
      tx.success();
    } finally {
      tx.finish();
    }
    container = null;
  }
 public Object entityToBean(@NotNull PropertyContainer entity, @NotNull Object bean) {
   Object value = entity.getProperty(propertyName, null);
   if (converter != null && value != null) {
     value = converter.fromNeo4j(value);
   }
   writeBeanValue(bean, value);
   return value;
 }
 public boolean removeValueFromArray(PropertyContainer container, String key, Object value) {
   Transaction tx = graphDb.beginTx();
   try {
     Collection<Object> values = getPropertyValues(container, key);
     boolean changed = values.remove(value);
     if (changed) {
       if (values.isEmpty()) {
         container.removeProperty(key);
       } else {
         container.setProperty(key, asPropertyValue(values));
       }
     }
     tx.success();
     return changed;
   } finally {
     tx.finish();
   }
 }
Example #11
0
  @Test
  public void testEquals() {
    GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
    PropertyContainer graphProperties = properties(db);
    Transaction tx = db.beginTx();
    try {
      graphProperties.setProperty("test", "test");
      tx.success();
    } finally {
      tx.finish();
    }

    assertEquals(graphProperties, properties(db));
    db.shutdown();
    db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
    assertFalse(graphProperties.equals(properties(db)));
    db.shutdown();
  }
Example #12
0
 // For compatibility reasons with Cypher
 public boolean isDeleted(PropertyContainer entity) {
   if (entity instanceof Node) {
     return isDeleted((Node) entity);
   } else if (entity instanceof Relationship) {
     return isDeleted((Relationship) entity);
   } else {
     throw new IllegalArgumentException(
         "Unknown entity type: " + entity + ", " + entity.getClass());
   }
 }
 public List<Object> getPropertyValues(PropertyContainer container, String key) {
   Transaction tx = graphDb.beginTx();
   try {
     Object value = container.getProperty(key, null);
     List<Object> result = value == null ? new ArrayList<Object>() : propertyValueAsList(value);
     tx.success();
     return result;
   } finally {
     tx.finish();
   }
 }
Example #14
0
  private boolean checkProperties(
      AbstractPatternObject<? extends PropertyContainer> patternObject, PropertyContainer object) {
    PropertyContainer associatedObject = patternObject.getAssociation();
    if (associatedObject != null && !object.equals(associatedObject)) {
      return false;
    }

    for (Map.Entry<String, Collection<ValueMatcher>> matchers :
        patternObject.getPropertyConstraints()) {
      String key = matchers.getKey();
      Object propertyValue = object.getProperty(key, null);
      for (ValueMatcher matcher : matchers.getValue()) {
        if (!matcher.matches(propertyValue)) {
          return false;
        }
      }
    }

    return true;
  }
 /**
  * Wraps a single {@link PropertyContainer#getProperty(String)} in a transaction.
  *
  * @param container the {@link PropertyContainer}.
  * @param key the property key.
  * @return the result from {@link PropertyContainer#getProperty(String)}.
  */
 public Object getProperty(PropertyContainer container, String key) {
   assertPropertyKeyNotNull(key);
   Transaction tx = graphDb().beginTx();
   try {
     Object result = container.getProperty(key);
     tx.success();
     return result;
   } finally {
     tx.finish();
   }
 }
 /**
  * Wraps a single {@link PropertyContainer#removeProperty(String)} in a transaction.
  *
  * @param container the {@link PropertyContainer}.
  * @param key the property key.
  * @return the old value of the property or null if the property didn't exist
  */
 public Object removeProperty(PropertyContainer container, String key) {
   assertPropertyKeyNotNull(key);
   Transaction tx = graphDb().beginTx();
   try {
     Object oldValue = container.removeProperty(key);
     tx.success();
     return oldValue;
   } finally {
     tx.finish();
   }
 }
 public Object beanToEntity(@NotNull PropertyContainer entity, @NotNull Object bean) {
   Object value = readBeanValue(bean);
   if (converter != null && value != null) {
     value = converter.toNeo4j(value);
   }
   boolean updateIndex = false;
   Object oldValue = null;
   if (indexMapping != null) {
     oldValue = entity.getProperty(propertyName, null);
     updateIndex = !Neo4jUtils.equals(oldValue, value);
   }
   if (value != null) {
     entity.setProperty(propertyName, value);
   } else {
     entity.removeProperty(propertyName);
   }
   if (updateIndex) {
     indexMapping.update(entity, oldValue, value);
   }
   return value;
 }
 public boolean addValueToArray(PropertyContainer container, String key, Object value) {
   Transaction tx = graphDb.beginTx();
   try {
     Collection<Object> values = getPropertyValues(container, key);
     boolean changed = values.contains(value) ? false : values.add(value);
     if (changed) {
       container.setProperty(key, asPropertyValue(values));
     }
     tx.success();
     return changed;
   } finally {
     tx.finish();
   }
 }
  /**
   * Wraps a single {@link PropertyContainer#setProperty(String, Object)} in a transaction.
   *
   * @param container the {@link PropertyContainer}.
   * @param key the property key.
   * @param value the property value.
   */
  public void setProperty(PropertyContainer container, String key, Object value) {
    assertPropertyKeyNotNull(key);
    if (value == null) {
      throw new IllegalArgumentException("Value for property '" + key + "' can't be null");
    }

    Transaction tx = graphDb().beginTx();
    try {
      container.setProperty(key, value);
      tx.success();
    } finally {
      tx.finish();
    }
  }
  @Override
  public Envelope decodeEnvelope(PropertyContainer container) {
    Object propValue = container.getProperty(propertyName);

    if (propValue instanceof Double[]) {
      Double[] bbox = (Double[]) propValue;
      return new Envelope(bbox[0], bbox[2], bbox[1], bbox[3]);
    } else if (propValue instanceof double[]) {
      double[] bbox = (double[]) propValue;
      return new Envelope(bbox[0], bbox[2], bbox[1], bbox[3]);
    } else {
      // invalid content
      return new Envelope();
    }
  }
Example #21
0
  protected void setProperties(PropertyContainer entity, String propertyJson)
      throws ShellException {
    if (propertyJson == null) {
      return;
    }

    try {
      Map<String, Object> properties = parseJSONMap(propertyJson);
      for (Map.Entry<String, Object> entry : properties.entrySet()) {
        entity.setProperty(entry.getKey(), entry.getValue());
      }
    } catch (JSONException e) {
      throw ShellException.wrapCause(e);
    }
  }
 private void assertHasProperties(PropertyContainer container, Map<String, Object> properties) {
   for (Map.Entry<String, Object> entry : properties.entrySet()) {
     assertEquals(entry.getValue(), container.getProperty(entry.getKey()));
   }
 }
 @Override
 public String convert(PropertyContainer value, Class<String> type) {
   return (String) value.getProperty("name");
 }
Example #24
0
 private void formatProperties(PrintWriter out, PropertyContainer pc) {
   if (!pc.getPropertyKeys().iterator().hasNext()) return;
   out.print(" ");
   final String propertyString = formatProperties(pc);
   out.print(propertyString);
 }
Example #25
0
 private void setProperties(Map<String, Object> properties, PropertyContainer entity) {
   for (Map.Entry<String, Object> property : properties.entrySet()) {
     entity.setProperty(property.getKey(), property.getValue());
   }
 }
Example #26
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();
  }