use of datawave.webservice.modification.ModificationRequestBase.MODE in project datawave by NationalSecurityAgency.
the class MutableMetadataHandler method process.
public void process(Connector con, ModificationRequestBase request, Map<String, Set<String>> mutableFieldList, Set<Authorizations> userAuths, String user, boolean purgeIndex, boolean insertHistory) throws Exception {
DefaultModificationRequest mr = DefaultModificationRequest.class.cast(request);
if (null == mr.getEvents() || mr.getEvents().isEmpty()) {
throw new IllegalArgumentException("No events specified for modification");
}
String fieldName = mr.getFieldName();
MetadataHelper helper = getMetadataHelper(con);
MODE mode = mr.getMode();
MultiTableBatchWriter writer = con.createMultiTableBatchWriter(new BatchWriterConfig().setMaxLatency(1, TimeUnit.SECONDS).setMaxMemory(1048576L).setMaxWriteThreads(4));
try {
for (EventIdentifier e : mr.getEvents()) {
String shardId = e.getShardId();
String datatype = e.getDatatype();
Set<String> datatypeFilter = Collections.singleton(datatype);
String eventUid = e.getEventUid();
String oldFieldValue = null;
Map<String, String> oldFieldMarkings = null;
String oldColumnVisibility = null;
List<Pair<Key, Value>> currentEntryList = null;
int valHistoryCount = 0;
/*
* Makes all fields mutable for services requiring no history.
*/
if (insertHistory && !isFieldMutable(mutableFieldList, datatype, fieldName))
throw new IllegalArgumentException("Field " + fieldName + " is not mutable");
boolean isIndexed = helper.isIndexed(fieldName, datatypeFilter);
boolean isReverseIndexed = helper.isReverseIndexed(fieldName, datatypeFilter);
boolean isIndexOnly = helper.getIndexOnlyFields(datatypeFilter).contains(fieldName);
boolean isContent = (contentFields != null && contentFields.contains(fieldName));
Set<Type<?>> dataTypes = helper.getDatatypesForField(fieldName, Collections.singleton(datatype));
if ((isIndexed || isReverseIndexed || isIndexOnly) && (null == dataTypes || dataTypes.isEmpty()))
throw new IllegalStateException("Field " + fieldName + " is marked index only but has no dataTypes");
long origTimestamp = getOriginalEventTimestamp(con, userAuths, shardId, datatype, eventUid);
// Count the history entries if history is going to be inserted.
if (insertHistory && (MODE.INSERT.equals(mode) || MODE.UPDATE.equals(mode))) {
List<Pair<Key, Value>> fieldHistoryList = getField(con, userAuths, shardId, datatype, eventUid, "HISTORY_" + fieldName, null, new HashMap<>(), null);
for (Pair<Key, Value> p : fieldHistoryList) {
if (p.getFirst().getColumnQualifier().find(mr.getFieldValue()) > -1) {
++valHistoryCount;
}
}
}
if (MODE.UPDATE.equals(mode) || MODE.DELETE.equals(mode)) {
if (MODE.UPDATE.equals(mode)) {
oldFieldValue = mr.getOldFieldValue();
oldFieldMarkings = mr.getOldFieldMarkings();
oldColumnVisibility = mr.getOldColumnVisibility();
if (null == oldFieldValue)
throw new IllegalArgumentException("fieldValue parameter required for update");
} else {
oldFieldValue = mr.getFieldValue();
oldFieldMarkings = mr.getFieldMarkings();
oldColumnVisibility = mr.getColumnVisibility();
if (null == oldFieldValue)
throw new IllegalArgumentException("fieldValue parameter required for delete");
}
ColumnVisibility oldViz = null;
if (null != oldColumnVisibility) {
oldViz = new ColumnVisibility(oldColumnVisibility);
}
// find the current values
currentEntryList = getField(con, userAuths, shardId, datatype, eventUid, fieldName, oldFieldValue, oldFieldMarkings, oldViz);
if (oldFieldValue != null && currentEntryList.isEmpty()) {
throw new IllegalArgumentException("Modification request rejected. Current value of " + fieldName + " does not match submitted value.");
}
} else {
if (null == mr.getFieldValue())
throw new IllegalArgumentException("fieldValue parameter required for insert");
}
if (MODE.INSERT.equals(mode)) {
String fieldValue = mr.getFieldValue();
Map<String, String> fieldMarkings = mr.getFieldMarkings();
String columnVisibility = mr.getColumnVisibility();
ColumnVisibility colviz = null;
if (null != columnVisibility) {
colviz = new ColumnVisibility(columnVisibility);
}
insert(writer, shardId, datatype, eventUid, fieldMarkings, colviz, fieldName, fieldValue, isIndexOnly, isIndexed, isReverseIndexed, dataTypes, user, MODE.INSERT, origTimestamp + valHistoryCount, insertHistory);
} else if (MODE.DELETE.equals(mode)) {
delete(writer, con, userAuths, currentEntryList, isIndexOnly, isIndexed, isReverseIndexed, isContent, dataTypes, user, MODE.DELETE, origTimestamp + valHistoryCount, purgeIndex, insertHistory);
} else {
delete(writer, con, userAuths, currentEntryList, isIndexOnly, isIndexed, isReverseIndexed, isContent, dataTypes, user, MODE.UPDATE, origTimestamp + valHistoryCount, purgeIndex, insertHistory);
String fieldValue = mr.getFieldValue();
Map<String, String> fieldMarkings = mr.getFieldMarkings();
String columnVisibility = mr.getColumnVisibility();
ColumnVisibility colviz = null;
if (null != columnVisibility) {
colviz = new ColumnVisibility(columnVisibility);
}
insert(writer, shardId, datatype, eventUid, fieldMarkings, colviz, fieldName, fieldValue, isIndexOnly, isIndexed, isReverseIndexed, dataTypes, user, MODE.UPDATE, origTimestamp + valHistoryCount, insertHistory);
}
}
} finally {
writer.close();
}
}
Aggregations