use of org.vertexium.HistoricalPropertyValue.HistoricalPropertyValueBuilder in project vertexium by visallo.
the class AccumuloGraph method getHistoricalPropertyValues.
public Iterable<HistoricalPropertyValue> getHistoricalPropertyValues(Element element, String key, String name, Visibility visibility, Long startTime, Long endTime, Authorizations authorizations) {
Span trace = Trace.start("getHistoricalPropertyValues");
if (Trace.isTracing()) {
trace.data("key", key);
trace.data("name", name);
trace.data("visibility", visibility.getVisibilityString());
if (startTime != null) {
trace.data("startTime", Long.toString(startTime));
}
if (endTime != null) {
trace.data("endTime", Long.toString(endTime));
}
}
try {
ElementType elementType = ElementType.getTypeFromElement(element);
FetchHints fetchHints = FetchHints.PROPERTIES_AND_METADATA;
traceDataFetchHints(trace, fetchHints);
org.apache.accumulo.core.data.Range range = RangeUtils.createRangeFromString(element.getId());
final ScannerBase scanner = createElementScanner(fetchHints, elementType, ALL_VERSIONS, startTime, endTime, Lists.newArrayList(range), false, authorizations);
try {
Map<String, HistoricalPropertyValue> results = new HashMap<>();
ArrayListMultimap<String, String> activeVisibilities = ArrayListMultimap.create();
Map<String, Key> softDeleteObserved = Maps.newHashMap();
for (Map.Entry<Key, Value> column : scanner) {
String cq = column.getKey().getColumnQualifier().toString();
String columnVisibility = column.getKey().getColumnVisibility().toString();
if (column.getKey().getColumnFamily().equals(AccumuloElement.CF_PROPERTY)) {
if (visibility != null && !columnVisibility.equals(visibility.getVisibilityString())) {
continue;
}
PropertyColumnQualifier propertyColumnQualifier = KeyHelper.createPropertyColumnQualifier(cq, getNameSubstitutionStrategy());
if (name != null && !propertyColumnQualifier.getPropertyName().equals(name)) {
continue;
}
if (key != null && !propertyColumnQualifier.getPropertyKey().equals(key)) {
continue;
}
String resultsKey = propertyColumnQualifier.getDiscriminator(columnVisibility, column.getKey().getTimestamp());
long timestamp = column.getKey().getTimestamp();
Object value = vertexiumSerializer.bytesToObject(column.getValue().get());
Metadata metadata = new Metadata();
// TODO should we preserve these over time
Set<Visibility> hiddenVisibilities = null;
if (value instanceof StreamingPropertyValueRef) {
// noinspection unchecked
value = ((StreamingPropertyValueRef) value).toStreamingPropertyValue(this, timestamp);
}
String propertyKey = propertyColumnQualifier.getPropertyKey();
String propertyName = propertyColumnQualifier.getPropertyName();
Visibility propertyVisibility = accumuloVisibilityToVisibility(columnVisibility);
HistoricalPropertyValue hpv = new HistoricalPropertyValueBuilder(propertyKey, propertyName, timestamp).propertyVisibility(propertyVisibility).value(value).metadata(metadata).hiddenVisibilities(hiddenVisibilities).build();
String propIdent = propertyKey + ":" + propertyName;
activeVisibilities.put(propIdent, columnVisibility);
results.put(resultsKey, hpv);
} else if (column.getKey().getColumnFamily().equals(AccumuloElement.CF_PROPERTY_SOFT_DELETE)) {
PropertyColumnQualifier propertyColumnQualifier = KeyHelper.createPropertyColumnQualifier(cq, getNameSubstitutionStrategy());
String propertyKey = propertyColumnQualifier.getPropertyKey();
String propertyName = propertyColumnQualifier.getPropertyName();
String propIdent = propertyKey + ":" + propertyName;
activeVisibilities.remove(propIdent, columnVisibility);
softDeleteObserved.put(propIdent, column.getKey());
} else if (column.getKey().getColumnFamily().equals(AccumuloElement.CF_PROPERTY_METADATA)) {
PropertyMetadataColumnQualifier propertyMetadataColumnQualifier = KeyHelper.createPropertyMetadataColumnQualifier(cq, getNameSubstitutionStrategy());
String resultsKey = propertyMetadataColumnQualifier.getPropertyDiscriminator(column.getKey().getTimestamp());
HistoricalPropertyValue hpv = results.get(resultsKey);
if (hpv == null) {
continue;
}
Object value = vertexiumSerializer.bytesToObject(column.getValue().get());
Visibility metadataVisibility = accumuloVisibilityToVisibility(columnVisibility);
hpv.getMetadata().add(propertyMetadataColumnQualifier.getMetadataKey(), value, metadataVisibility);
}
}
for (Key entry : softDeleteObserved.values()) {
String cq = entry.getColumnQualifier().toString();
PropertyColumnQualifier propertyColumnQualifier = KeyHelper.createPropertyColumnQualifier(cq, getNameSubstitutionStrategy());
String propertyKey = propertyColumnQualifier.getPropertyKey();
String propertyName = propertyColumnQualifier.getPropertyName();
String propIdent = propertyKey + ":" + propertyName;
List<String> active = activeVisibilities.get(propIdent);
if (active == null || active.isEmpty()) {
long timestamp = entry.getTimestamp() + 1;
String columnVisibility = entry.getColumnVisibility().toString();
Visibility propertyVisibility = accumuloVisibilityToVisibility(columnVisibility);
HistoricalPropertyValue hpv = new HistoricalPropertyValueBuilder(propertyKey, propertyName, timestamp).propertyVisibility(propertyVisibility).isDeleted(true).build();
String resultsKey = propertyColumnQualifier.getDiscriminator(columnVisibility, timestamp);
results.put(resultsKey, hpv);
}
}
return new TreeSet<>(results.values());
} finally {
scanner.close();
}
} finally {
trace.stop();
}
}
use of org.vertexium.HistoricalPropertyValue.HistoricalPropertyValueBuilder in project vertexium by visallo.
the class InMemoryTableElement method getHistoricalPropertyValues.
public Iterable<HistoricalPropertyValue> getHistoricalPropertyValues(String key, String name, Visibility visibility, Long startTime, Long endTime, Authorizations authorizations) {
List<PropertyMutation> propertyMutations = findPropertyMutations(key, name, visibility);
List<HistoricalPropertyValue> historicalPropertyValues = new ArrayList<>();
/*
* There is the expectation that historical property values are a snapshot of the property in
* time. This method attempts to reconstruct the property current state from mutations.
*/
Map<String, HistoricalPropertyValueBuilder> currentPropertyBuilders = Maps.newHashMap();
Set<Visibility> hiddenVisibilities = new HashSet<>();
for (PropertyMutation m : propertyMutations) {
String propertyIdentifier = m.getPropertyKey() + m.getPropertyName();
HistoricalPropertyValueBuilder builder = currentPropertyBuilders.computeIfAbsent(propertyIdentifier, k -> new HistoricalPropertyValueBuilder(m.getPropertyKey(), m.getPropertyName(), m.getTimestamp()));
if (startTime != null && m.getTimestamp() < startTime) {
continue;
}
if (endTime != null && m.getTimestamp() > endTime) {
continue;
}
if (!canRead(m.getVisibility(), authorizations)) {
continue;
}
// Ignore workspace interactions to avoid duplicated entries
if (m.getVisibility() != null && m.getPropertyVisibility().getVisibilityString().matches("(.*)WORKSPACE(.*)")) {
continue;
}
if (m instanceof SoftDeletePropertyMutation) {
builder.isDeleted(true);
builder.timestamp(m.getTimestamp());
historicalPropertyValues.add(builder.build());
} else if (m instanceof AddPropertyMetadataMutation) {
builder.metadata(((AddPropertyMetadataMutation) m).getMetadata(FetchHints.ALL));
builder.timestamp(m.getTimestamp());
} else if (m instanceof MarkPropertyHiddenMutation) {
// Ignore
} else if (m instanceof MarkPropertyVisibleMutation) {
// Ignore
} else if (m instanceof AddPropertyValueMutation) {
AddPropertyValueMutation apvm = (AddPropertyValueMutation) m;
Object value = apvm.getValue();
value = loadIfStreamingPropertyValue(value, m.getTimestamp());
builder.propertyVisibility(m.getPropertyVisibility()).timestamp(m.getTimestamp()).value(value).metadata(apvm.getMetadata(FetchHints.ALL)).hiddenVisibilities(hiddenVisibilities).isDeleted(false);
// If the condition occurs, remove the delete event from the set.
if (historicalPropertyValues.size() > 0) {
HistoricalPropertyValue last = historicalPropertyValues.get(historicalPropertyValues.size() - 1);
if (propertyIdentifier.equals(last.getPropertyKey() + last.getPropertyName()) && last.isDeleted()) {
historicalPropertyValues.remove(historicalPropertyValues.size() - 1);
}
}
historicalPropertyValues.add(builder.build());
} else {
throw new VertexiumException("Unhandled PropertyMutation: " + m.getClass().getName());
}
}
Collections.reverse(historicalPropertyValues);
return historicalPropertyValues;
}
Aggregations