@Test
  public void testBasicOperation() throws ConnectionException, InterruptedException {

    ApplicationScope scope = new ApplicationScopeImpl(new SimpleId("organization"));

    IntegerField field = new IntegerField("count", 5);
    Id entityId = new SimpleId(UUIDGenerator.newTimeUUID(), "entity");
    UUID version = UUIDGenerator.newTimeUUID();
    UniqueValue stored = new UniqueValueImpl(field, entityId, version);
    strategy.write(scope, stored).execute();

    UniqueValueSet fields =
        strategy.load(scope, entityId.getType(), Collections.<Field>singleton(field));

    UniqueValue retrieved = fields.getValue(field.getName());
    Assert.assertNotNull(retrieved);
    assertEquals(stored, retrieved);

    Iterator<UniqueValue> allFieldsWritten = strategy.getAllUniqueFields(scope, entityId);

    assertTrue(allFieldsWritten.hasNext());

    // test this interface. In most cases, we won't know the field name, so we want them all
    UniqueValue allFieldsValue = allFieldsWritten.next();
    Assert.assertNotNull(allFieldsValue);

    assertEquals(field, allFieldsValue.getField());
    assertEquals(version, allFieldsValue.getEntityVersion());

    assertFalse(allFieldsWritten.hasNext());
  }
  @Test
  public void testCapitalizationFixes() throws ConnectionException {

    ApplicationScope scope = new ApplicationScopeImpl(new SimpleId("organization"));

    StringField field = new StringField("count", "MiXeD CaSe");
    Id entityId = new SimpleId(UUIDGenerator.newTimeUUID(), "entity");
    UUID version = UUIDGenerator.newTimeUUID();
    UniqueValue stored = new UniqueValueImpl(field, entityId, version);
    strategy.write(scope, stored).execute();

    UniqueValueSet fields =
        strategy.load(scope, entityId.getType(), Collections.<Field>singleton(field));

    UniqueValue value = fields.getValue(field.getName());

    assertEquals(field.getName(), value.getField().getName());

    assertEquals(entityId, value.getEntityId());

    // now test will all upper and all lower, we should get it all the same
    fields =
        strategy.load(
            scope,
            entityId.getType(),
            Collections.<Field>singleton(new StringField(field.getName(), "MIXED CASE")));

    value = fields.getValue(field.getName());

    assertEquals(field.getName(), value.getField().getName());

    assertEquals(entityId, value.getEntityId());

    fields =
        strategy.load(
            scope,
            entityId.getType(),
            Collections.<Field>singleton(new StringField(field.getName(), "mixed case")));

    value = fields.getValue(field.getName());

    assertEquals(field.getName(), value.getField().getName());

    assertEquals(entityId, value.getEntityId());

    Iterator<UniqueValue> allFieldsWritten = strategy.getAllUniqueFields(scope, entityId);

    assertTrue(allFieldsWritten.hasNext());

    // test this interface. In most cases, we won't know the field name, so we want them all
    UniqueValue writtenFieldEntry = allFieldsWritten.next();
    Assert.assertNotNull(writtenFieldEntry);

    assertEquals(field.getName(), writtenFieldEntry.getField().getName());
    assertEquals(field.getValue().toLowerCase(), writtenFieldEntry.getField().getValue());
    assertEquals(version, writtenFieldEntry.getEntityVersion());

    assertFalse(allFieldsWritten.hasNext());
  }
예제 #3
0
 /** Append the id to the string */
 private static final void idString(final StringBuilder builder, final String type, final Id id) {
   builder
       .append(type)
       .append("(")
       .append(id.getUuid())
       .append(ID_SEPERATOR)
       .append(id.getType().toLowerCase())
       .append(")");
 }
예제 #4
0
  /**
   * 4. Delete all entity documents out of elasticsearch. 5. Compact Graph so that it deletes the
   * marked values. 6. Delete entity from cassandra using the map manager.
   */
  private Id deleteAsync(MapManager mapManager, ApplicationScope applicationScope, Id entityId) {
    try {
      // Step 4 && 5

      if (!skipIndexingForType(entityId.getType(), applicationScope)) {

        asyncEventService.queueEntityDelete(applicationScope, entityId);
      }
      // Step 6
      // delete from our UUID index
      mapManager.delete(entityId.getUuid().toString());
      return entityId;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  @Test
  public void testWriteWithTTL() throws InterruptedException, ConnectionException {

    ApplicationScope scope = new ApplicationScopeImpl(new SimpleId("organization"));

    // write object that lives 2 seconds
    IntegerField field = new IntegerField("count", 5);
    Id entityId = new SimpleId(UUIDGenerator.newTimeUUID(), "entity");
    UUID version = UUIDGenerator.newTimeUUID();
    UniqueValue stored = new UniqueValueImpl(field, entityId, version);
    strategy.write(scope, stored, 5).execute();

    Thread.sleep(1000);

    // waited one sec, should be still here
    UniqueValueSet fields =
        strategy.load(scope, entityId.getType(), Collections.<Field>singleton(field));

    UniqueValue retrieved = fields.getValue(field.getName());

    Assert.assertNotNull(retrieved);
    assertEquals(stored, retrieved);

    Thread.sleep(5000);

    // wait another second, should be gone now
    fields = strategy.load(scope, entityId.getType(), Collections.<Field>singleton(field));

    UniqueValue nullExpected = fields.getValue(field.getName());
    Assert.assertNull(nullExpected);

    // we still want to retain the log entry, even if we don't retain the unique value.  Deleting
    // something
    // that doesn't exist is a tombstone, but so is the timeout.
    Iterator<UniqueValue> allFieldsWritten = strategy.getAllUniqueFields(scope, entityId);

    assertTrue(allFieldsWritten.hasNext());

    // test this interface. In most cases, we won't know the field name, so we want them all
    UniqueValue writtenFieldEntry = allFieldsWritten.next();
    Assert.assertNotNull(writtenFieldEntry);

    assertEquals(field, writtenFieldEntry.getField());
    assertEquals(version, writtenFieldEntry.getEntityVersion());

    assertFalse(allFieldsWritten.hasNext());
  }
예제 #6
0
  @Override
  public Observable<Id> deleteAllEntities(
      final ApplicationScope applicationScope, final int limit) {
    if (applicationScope
        .getApplication()
        .getUuid()
        .equals(CpNamingUtils.MANAGEMENT_APPLICATION_ID)) {
      throw new IllegalArgumentException("Can't delete from management app");
    }

    // EventBuilder eventBuilder = injector.getInstance(EventBuilder.class);
    Observable appObservable = Observable.just(applicationScope);
    EntityCollectionManager entityCollectionManager =
        entityCollectionManagerFactory.createCollectionManager(applicationScope);
    GraphManager graphManager = graphManagerFactory.createEdgeManager(applicationScope);
    MapManager mapManager = getMapManagerForTypes(applicationScope);

    Observable<Id> countObservable =
        allEntityIdsObservable
            .getEntities(appObservable)
            .map(entityIdScope -> ((EntityIdScope) entityIdScope).getId())
            .filter(
                id -> {
                  final String type = InflectionUtils.pluralize(((Id) id).getType());
                  return !(type.equals(Schema.COLLECTION_USERS)
                      || type.equals(Schema.COLLECTION_GROUPS)
                      || type.equals(InflectionUtils.pluralize(Schema.TYPE_APPLICATION))
                      || type.equals(Schema.COLLECTION_ROLES));
                }) // skip application entity and users and groups and roles
        ;

    if (limit > 0) {
      countObservable = countObservable.limit(limit);
    }

    countObservable =
        countObservable
            .map(
                id -> {
                  entityCollectionManager
                      .mark((Id) id)
                      .mergeWith(graphManager.markNode((Id) id, createGraphOperationTimestamp()))
                      .toBlocking()
                      .last();
                  return id;
                })
            .doOnNext(id -> deleteAsync(mapManager, applicationScope, (Id) id));
    return countObservable;
  }
  @Test
  public void testDelete() throws ConnectionException {

    ApplicationScope scope = new ApplicationScopeImpl(new SimpleId("organization"));

    IntegerField field = new IntegerField("count", 5);
    Id entityId = new SimpleId(UUIDGenerator.newTimeUUID(), "entity");
    UUID version = UUIDGenerator.newTimeUUID();
    UniqueValue stored = new UniqueValueImpl(field, entityId, version);
    strategy.write(scope, stored).execute();

    strategy.delete(scope, stored).execute();

    UniqueValueSet fields =
        strategy.load(scope, entityId.getType(), Collections.<Field>singleton(field));

    UniqueValue nullExpected = fields.getValue(field.getName());

    Assert.assertNull(nullExpected);

    Iterator<UniqueValue> allFieldsWritten = strategy.getAllUniqueFields(scope, entityId);

    assertFalse("No entries left", allFieldsWritten.hasNext());
  }
예제 #8
0
 /** Get the entity type */
 public static String getType(ApplicationScope applicationScope, Id entityId) {
   return getType(applicationScope, entityId.getType());
 }
  @Test
  public void twoFieldsPerVersion() throws ConnectionException, InterruptedException {

    ApplicationScope scope = new ApplicationScopeImpl(new SimpleId("organization"));

    Id entityId = new SimpleId(UUIDGenerator.newTimeUUID(), "entity");
    final UUID version1 = UUIDGenerator.newTimeUUID();

    // write V1 of everything
    IntegerField version1Field1 = new IntegerField("count", 1);
    StringField version1Field2 = new StringField("field", "v1value");

    UniqueValue version1Field1Value = new UniqueValueImpl(version1Field1, entityId, version1);
    UniqueValue version1Field2Value = new UniqueValueImpl(version1Field2, entityId, version1);

    final MutationBatch batch = strategy.write(scope, version1Field1Value);
    batch.mergeShallow(strategy.write(scope, version1Field2Value));

    // write V2 of everything
    final UUID version2 = UUIDGenerator.newTimeUUID();

    IntegerField version2Field1 = new IntegerField("count", 2);
    StringField version2Field2 = new StringField("field", "v2value");

    UniqueValue version2Field1Value = new UniqueValueImpl(version2Field1, entityId, version2);
    UniqueValue version2Field2Value = new UniqueValueImpl(version2Field2, entityId, version2);

    batch.mergeShallow(strategy.write(scope, version2Field1Value));
    batch.mergeShallow(strategy.write(scope, version2Field2Value));

    batch.execute();

    UniqueValueSet fields =
        strategy.load(
            scope, entityId.getType(), Arrays.<Field>asList(version1Field1, version1Field2));

    UniqueValue retrieved = fields.getValue(version1Field1.getName());

    assertEquals(version1Field1Value, retrieved);

    retrieved = fields.getValue(version1Field2.getName());
    assertEquals(version1Field2Value, retrieved);

    Iterator<UniqueValue> allFieldsWritten = strategy.getAllUniqueFields(scope, entityId);

    assertTrue(allFieldsWritten.hasNext());

    // test this interface. In most cases, we won't know the field name, so we want them all
    UniqueValue allFieldsValue = allFieldsWritten.next();

    // version 2 fields should come first, ordered by field name
    assertEquals(version2Field1, allFieldsValue.getField());
    assertEquals(version2, allFieldsValue.getEntityVersion());

    allFieldsValue = allFieldsWritten.next();

    assertEquals(version2Field2, allFieldsValue.getField());
    assertEquals(version2, allFieldsValue.getEntityVersion());

    // version 1 should come next ordered by field name
    allFieldsValue = allFieldsWritten.next();

    assertEquals(version1Field1, allFieldsValue.getField());
    assertEquals(version1, allFieldsValue.getEntityVersion());

    allFieldsValue = allFieldsWritten.next();

    assertEquals(version1Field2, allFieldsValue.getField());
    assertEquals(version1, allFieldsValue.getEntityVersion());

    assertFalse(allFieldsWritten.hasNext());
  }
예제 #10
0
  @Override
  public MutationBatch writeEdge(
      final ApplicationScope scope, final MarkedEdge markedEdge, final UUID timestamp) {

    ValidationUtils.validateApplicationScope(scope);
    GraphValidation.validateEdge(markedEdge);
    ValidationUtils.verifyTimeUuid(timestamp, "timestamp");

    final long now = timeService.getCurrentTime();
    final Id sourceNode = markedEdge.getSourceNode();
    final Id targetNode = markedEdge.getTargetNode();
    final String edgeType = markedEdge.getType();
    final long edgeTimestamp = markedEdge.getTimestamp();

    /** Source write */
    final DirectedEdgeMeta sourceEdgeMeta = DirectedEdgeMeta.fromSourceNode(sourceNode, edgeType);

    final Collection<Shard> sourceWriteShards =
        edgeShardStrategy.getWriteShards(scope, edgeTimestamp, sourceEdgeMeta).getWriteShards(now);

    final MutationBatch batch =
        shardedEdgeSerialization.writeEdgeFromSource(
            edgeColumnFamilies, scope, markedEdge, sourceWriteShards, sourceEdgeMeta, timestamp);

    /** Source with target type write */
    final DirectedEdgeMeta sourceTargetTypeEdgeMeta =
        DirectedEdgeMeta.fromSourceNodeTargetType(sourceNode, edgeType, targetNode.getType());

    final Collection<Shard> sourceTargetTypeWriteShards =
        edgeShardStrategy
            .getWriteShards(scope, edgeTimestamp, sourceTargetTypeEdgeMeta)
            .getWriteShards(now);

    batch.mergeShallow(
        shardedEdgeSerialization.writeEdgeFromSourceWithTargetType(
            edgeColumnFamilies,
            scope,
            markedEdge,
            sourceTargetTypeWriteShards,
            sourceTargetTypeEdgeMeta,
            timestamp));

    /** Target write */
    final DirectedEdgeMeta targetEdgeMeta = DirectedEdgeMeta.fromTargetNode(targetNode, edgeType);

    final Collection<Shard> targetWriteShards =
        edgeShardStrategy.getWriteShards(scope, edgeTimestamp, targetEdgeMeta).getWriteShards(now);

    batch.mergeShallow(
        shardedEdgeSerialization.writeEdgeToTarget(
            edgeColumnFamilies, scope, markedEdge, targetWriteShards, targetEdgeMeta, timestamp));

    /** Target with source type write */
    final DirectedEdgeMeta targetSourceTypeEdgeMeta =
        DirectedEdgeMeta.fromTargetNodeSourceType(targetNode, edgeType, sourceNode.getType());

    final Collection<Shard> targetSourceTypeWriteShards =
        edgeShardStrategy
            .getWriteShards(scope, edgeTimestamp, targetSourceTypeEdgeMeta)
            .getWriteShards(now);

    batch.mergeShallow(
        shardedEdgeSerialization.writeEdgeToTargetWithSourceType(
            edgeColumnFamilies,
            scope,
            markedEdge,
            targetSourceTypeWriteShards,
            targetSourceTypeEdgeMeta,
            timestamp));

    /** Version write */
    final DirectedEdgeMeta edgeVersionsMeta =
        DirectedEdgeMeta.fromEdge(sourceNode, targetNode, edgeType);

    final Collection<Shard> edgeVersionsShards =
        edgeShardStrategy
            .getWriteShards(scope, edgeTimestamp, edgeVersionsMeta)
            .getWriteShards(now);

    batch.mergeShallow(
        shardedEdgeSerialization.writeEdgeVersions(
            edgeColumnFamilies,
            scope,
            markedEdge,
            edgeVersionsShards,
            edgeVersionsMeta,
            timestamp));

    return batch;
  }