Skip to content

Commit

Permalink
[proxima-core] #91 add support for replicating entity from another en…
Browse files Browse the repository at this point in the history
…tity
  • Loading branch information
je-ik committed May 21, 2018
1 parent 8b1f195 commit 92b5a3c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,11 @@ default String toAttributePrefix() {
*/
boolean isPublic();

/**
* Convert this attribute back to builder.
* @param repo the repository
* @return builder representing this attribute
*/
public AttributeDescriptor.Builder toBuilder(Repository repo);

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,12 @@ public ValueSerializer<T> getValueSerializer() {
return Objects.requireNonNull(valueSerializer);
}

@Override
public Builder toBuilder(Repository repo) {
return AttributeDescriptor.newBuilder(repo)
.setName(getName())
.setEntity(getEntity())
.setSchemeURI(getSchemeURI());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ public String toString() {
return "AttributeDescriptor(entity=" + entity + ", name=" + name + ")";
}


}
94 changes: 69 additions & 25 deletions core/src/main/java/cz/o2/proxima/repository/ConfigRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import cz.o2.proxima.storage.commitlog.CommitLogReader;
import cz.o2.proxima.storage.randomaccess.RandomAccessReader;
import cz.o2.proxima.util.Classpath;
import cz.o2.proxima.util.Pair;
import java.io.Serializable;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Configuration;
Expand All @@ -38,6 +39,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -360,35 +362,77 @@ private void readEntityDescriptors(Config cfg) {
}

Map<String, Object> entitiesCfg = toMap("entities", entities.unwrapped());
List<Pair<String, String>> replicaEntities = new ArrayList<>();
for (Map.Entry<String, Object> e : entitiesCfg.entrySet()) {
String entityName = e.getKey();
Map<String, Object> entityAttrs = toMap(
"entities." + entityName + ".attributes",
toMap("entities." + entityName, e.getValue()).get("attributes"));
EntityDescriptor.Builder entity = EntityDescriptor.newBuilder()
.setName(entityName);

// first regular attributes
entityAttrs.forEach((key, value) -> {
Map<String, Object> settings = toMap(
"entities." + entityName + ".attributes." + key, value);
if (settings.get("proxy") == null) {
loadRegular(entityName, key, settings, entity);
}
});

// next proxies
entityAttrs.forEach((key, value) -> {
Map<String, Object> settings = toMap(
"entities." + entityName + ".attributes." + key, value);
if (settings.get("proxy") != null) {
loadProxy(key, settings, entity);
}
});
Map<String, Object> cfgMap = toMap("entities." + entityName, e.getValue());
Object attributes = cfgMap.get("attributes");
final EntityDescriptor entity;
if (attributes != null) {
entity = loadEntityWithAttributes(entityName, attributes);
log.info("Adding entity {}", entityName);
entitiesByName.put(entityName, entity);
} else if (cfgMap.get("from") != null) {
String fromName = cfgMap.get("from").toString();
replicaEntities.add(Pair.of(entityName, fromName));
} else {
throw new IllegalArgumentException(
"Invalid entity specfication. Entity " + entityName + " has no attributes");
}
}

log.info("Adding entity {}", entityName);
entitiesByName.put(entityName, entity.build());
for (Pair<String, String> p : replicaEntities) {
String entityName = p.getFirst();
String fromName = p.getSecond();
EntityDescriptor from = Optional
.ofNullable(entitiesByName.get(fromName))
.orElseThrow(() -> new IllegalArgumentException(
"Entity " + fromName + " doesn't exist"));
EntityDescriptor entity = loadEntityFromExisting(entityName, from);
log.info("Adding entity {} as replica of {}", entityName, fromName);
entitiesByName.put(entityName, entity);
}

}

private EntityDescriptor loadEntityWithAttributes(
String entityName, Object attributes) {

Map<String, Object> entityAttrs = toMap(
"entities." + entityName + ".attributes",
attributes);
EntityDescriptor.Builder entity = EntityDescriptor.newBuilder()
.setName(entityName);

// first regular attributes
entityAttrs.forEach((key, value) -> {
Map<String, Object> settings = toMap(
"entities." + entityName + ".attributes." + key, value);
if (settings.get("proxy") == null) {
loadRegular(entityName, key, settings, entity);
}
});

// next proxies
entityAttrs.forEach((key, value) -> {
Map<String, Object> settings = toMap(
"entities." + entityName + ".attributes." + key, value);
if (settings.get("proxy") != null) {
loadProxy(key, settings, entity);
}
});
return entity.build();
}

private EntityDescriptor loadEntityFromExisting(
String entityName, EntityDescriptor from) {

EntityDescriptor.Builder builder = EntityDescriptor.newBuilder()
.setName(entityName);
from.getAllAttributes().forEach(attr -> {
builder.addAttribute(attr.toBuilder(this).setEntity(entityName).build());
});
return builder.build();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,10 @@ public void testRepositorySerializable() throws Exception {
assertNotNull(clone.getConfig());
}

@Test
public void testEntityReplication() {
assertTrue(repo.findEntity("replica").isPresent());
assertEquals(7, repo.findEntity("replica").get().getAllAttributes().size());
}

}
14 changes: 14 additions & 0 deletions core/src/test/resources/test-reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@

}

replica {
from: gateway
}

}

attributeFamilies: {
Expand Down Expand Up @@ -95,6 +99,15 @@
type: primary
access: "commit-log, random-access, partitioned-cached-view"
}

replica-storage {
entity: replica
attributes: [ "*" ]
storage: "inmem:///proxima/replica"
type: primary
access: "commit-log"
}

}

transformations {
Expand All @@ -114,6 +127,7 @@
filter: cz.o2.proxima.storage.PassthroughFilter
disabled: true
}

}

}
Expand Down

0 comments on commit 92b5a3c

Please sign in to comment.