use of io.siddhi.core.table.holder.IndexEventHolder in project siddhi by wso2.
the class CacheTableFIFO method deleteOneEntryUsingCachePolicy.
@Override
public void deleteOneEntryUsingCachePolicy() {
IndexEventHolder indexEventHolder = (IndexEventHolder) stateHolder.getState().getEventHolder();
Set<Object> keys = indexEventHolder.getAllPrimaryKeyValues();
long minTimestamp = Long.MAX_VALUE;
Object keyOfMinTimestamp = null;
for (Object key : keys) {
Object[] data = indexEventHolder.getEvent(key).getOutputData();
long timestamp = (long) data[cachePolicyAttributePosition];
if (timestamp < minTimestamp) {
minTimestamp = timestamp;
keyOfMinTimestamp = key;
}
}
indexEventHolder.deleteEvent(keyOfMinTimestamp);
}
use of io.siddhi.core.table.holder.IndexEventHolder in project siddhi by wso2.
the class CacheTableLRU method deleteOneEntryUsingCachePolicy.
@Override
public void deleteOneEntryUsingCachePolicy() {
IndexEventHolder indexEventHolder = (IndexEventHolder) stateHolder.getState().getEventHolder();
Set<Object> keys = indexEventHolder.getAllPrimaryKeyValues();
long minTimestamp = Long.MAX_VALUE;
Object keyOfMinTimestamp = null;
for (Object key : keys) {
Object[] data = indexEventHolder.getEvent(key).getOutputData();
long timestamp = (long) data[cachePolicyAttributePosition];
if (timestamp < minTimestamp) {
minTimestamp = timestamp;
keyOfMinTimestamp = key;
}
}
indexEventHolder.deleteEvent(keyOfMinTimestamp);
}
use of io.siddhi.core.table.holder.IndexEventHolder in project siddhi by wso2.
the class CacheTableLFU method deleteEntriesUsingCachePolicy.
@Override
public void deleteEntriesUsingCachePolicy(int numRowsToDelete) {
IndexEventHolder indexEventHolder = (IndexEventHolder) stateHolder.getState().getEventHolder();
if (numRowsToDelete >= indexEventHolder.size()) {
indexEventHolder.deleteAll();
} else {
Set<Object> keys = indexEventHolder.getAllPrimaryKeyValues();
TreeMap<Integer, Object> toDelete = new TreeMap<>();
for (Object key : keys) {
if (toDelete.size() < numRowsToDelete) {
toDelete.put((Integer) indexEventHolder.getEvent(key).getOutputData()[cachePolicyAttributePosition], key);
} else {
Integer count = (Integer) indexEventHolder.getEvent(key).getOutputData()[cachePolicyAttributePosition];
Integer firstKey = toDelete.firstKey();
if (count < firstKey) {
toDelete.remove(firstKey);
toDelete.put(count, key);
}
}
}
for (Object deleteKey : toDelete.values()) {
if (deleteKey != null) {
indexEventHolder.deleteEvent(deleteKey);
}
}
}
}
use of io.siddhi.core.table.holder.IndexEventHolder in project siddhi by wso2.
the class CacheTableLFU method deleteOneEntryUsingCachePolicy.
@Override
public void deleteOneEntryUsingCachePolicy() {
IndexEventHolder indexEventHolder = (IndexEventHolder) stateHolder.getState().getEventHolder();
Set<Object> keys = indexEventHolder.getAllPrimaryKeyValues();
int minCount = Integer.MAX_VALUE;
Object keyOfMinCount = null;
for (Object key : keys) {
Object[] data = indexEventHolder.getEvent(key).getOutputData();
int count = (int) data[cachePolicyAttributePosition];
if (count < minCount) {
minCount = count;
keyOfMinCount = key;
}
}
indexEventHolder.deleteEvent(keyOfMinCount);
}
use of io.siddhi.core.table.holder.IndexEventHolder in project siddhi by wso2.
the class EventHolderPasser method parse.
public static EventHolder parse(AbstractDefinition tableDefinition, StreamEventFactory tableStreamEventFactory, SiddhiAppContext siddhiAppContext, boolean isCacheTable) {
ZeroStreamEventConverter eventConverter = new ZeroStreamEventConverter();
PrimaryKeyReferenceHolder[] primaryKeyReferenceHolders = null;
Map<String, Integer> indexMetaData = new HashMap<String, Integer>();
// primaryKey.
Annotation primaryKeyAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PRIMARY_KEY, tableDefinition.getAnnotations());
if (primaryKeyAnnotation != null) {
if (primaryKeyAnnotation.getElements().size() == 0) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_PRIMARY_KEY + " annotation " + "contains " + primaryKeyAnnotation.getElements().size() + " element, at '" + tableDefinition.getId() + "'");
}
primaryKeyReferenceHolders = primaryKeyAnnotation.getElements().stream().map(element -> element.getValue().trim()).map(key -> new PrimaryKeyReferenceHolder(key, tableDefinition.getAttributePosition(key))).toArray(PrimaryKeyReferenceHolder[]::new);
}
for (Annotation indexAnnotation : AnnotationHelper.getAnnotations(SiddhiConstants.ANNOTATION_INDEX, tableDefinition.getAnnotations())) {
if (indexAnnotation.getElements().size() == 0) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of " + "in-memory table should contain only one index element, but found " + indexAnnotation.getElements().size() + " element", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
} else if (indexAnnotation.getElements().size() > 1) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of the " + "in-memory table should only contain one index element but found " + indexAnnotation.getElements().size() + " elements. To use multiple indexes, " + "define multiple '@index(<index key>)' annotations with one index element " + "per each index key", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
}
for (Element element : indexAnnotation.getElements()) {
Integer previousValue = indexMetaData.put(element.getValue().trim(), tableDefinition.getAttributePosition(element.getValue().trim()));
if (previousValue != null) {
throw new SiddhiAppValidationException("Multiple " + SiddhiConstants.ANNOTATION_INDEX + " " + "annotations defined with same attribute '" + element.getValue().trim() + "', at '" + tableDefinition.getId() + "'", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
}
}
}
// not support indexBy.
Annotation indexByAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX_BY, tableDefinition.getAnnotations());
if (indexByAnnotation != null) {
throw new OperationNotSupportedException(SiddhiConstants.ANNOTATION_INDEX_BY + " annotation is not " + "supported anymore, please use @PrimaryKey or @Index annotations instead," + " at '" + tableDefinition.getId() + "'");
}
if (primaryKeyReferenceHolders != null || indexMetaData.size() > 0) {
boolean isNumeric = false;
if (primaryKeyReferenceHolders != null) {
if (primaryKeyReferenceHolders.length == 1) {
Attribute.Type type = tableDefinition.getAttributeType(primaryKeyReferenceHolders[0].getPrimaryKeyAttribute());
if (type == Attribute.Type.DOUBLE || type == Attribute.Type.FLOAT || type == Attribute.Type.INT || type == Attribute.Type.LONG) {
isNumeric = true;
}
}
}
if (isCacheTable) {
return new IndexEventHolderForCache(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
} else {
return new IndexEventHolder(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
}
} else {
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
for (Attribute attribute : tableDefinition.getAttributeList()) {
metaStreamEvent.addOutputData(attribute);
}
StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, tableStreamEventFactory);
return new ListEventHolder(tableStreamEventFactory, eventConverter, new StreamEventClonerHolder(streamEventCloner));
}
}
Aggregations