Search in sources :

Example 1 with MODE

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();
    }
}
Also used : MultiTableBatchWriter(org.apache.accumulo.core.client.MultiTableBatchWriter) MODE(datawave.webservice.modification.ModificationRequestBase.MODE) MetadataHelper(datawave.query.util.MetadataHelper) Type(datawave.data.type.Type) KeyType(datawave.query.data.parsers.DatawaveKey.KeyType) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) DatawaveKey(datawave.query.data.parsers.DatawaveKey) Key(org.apache.accumulo.core.data.Key) PartialKey(org.apache.accumulo.core.data.PartialKey) Pair(org.apache.accumulo.core.util.Pair)

Aggregations

Type (datawave.data.type.Type)1 DatawaveKey (datawave.query.data.parsers.DatawaveKey)1 KeyType (datawave.query.data.parsers.DatawaveKey.KeyType)1 MetadataHelper (datawave.query.util.MetadataHelper)1 MODE (datawave.webservice.modification.ModificationRequestBase.MODE)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)1 MultiTableBatchWriter (org.apache.accumulo.core.client.MultiTableBatchWriter)1 Key (org.apache.accumulo.core.data.Key)1 PartialKey (org.apache.accumulo.core.data.PartialKey)1 Value (org.apache.accumulo.core.data.Value)1 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)1 Pair (org.apache.accumulo.core.util.Pair)1