Search in sources :

Example 1 with ValueTuple

use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.

the class FunctionalSet method compareWith.

/*
     * revisit this if the incoming min/max from 4.2.0 are insufficient. It has not been used before public ValueTuple min() { ValueTuple min = null; for
     * (Iterator<T> iterator = iterator(); iterator.hasNext();) { ValueTuple next = iterator.next(); if (min == null) { min = next; } else if (compare(min,
     * next) >= 0) { min = next; } } return min; }
     * 
     * public ValueTuple max() { ValueTuple max = null; for (Iterator<T> iterator = iterator(); iterator.hasNext();) { ValueTuple next = iterator.next(); if
     * (max == null) { max = next; } else if (compare(max, next) <= 0) { max = next; } } return max; }
     * 
     * private int compare(ValueTuple left, ValueTuple right) { Object leftValue = left.getValue(); Object rightValue = right.getValue(); if (leftValue
     * instanceof String && rightValue instanceof String) { return ((String) leftValue).compareTo((String) rightValue); } if (leftValue instanceof Number &&
     * rightValue instanceof Number) { return Double.compare(((Number) leftValue).doubleValue(), ((Number) rightValue).doubleValue()); } if (leftValue
     * instanceof Date && rightValue instanceof Date) { return Long.compare(((Date) leftValue).getTime(), ((Date) rightValue).getTime()); } if (leftValue
     * instanceof Type && rightValue instanceof Type) { return ((Type) leftValue).getDelegate().compareTo(((Type) rightValue).getDelegate()); } return 0; }
     */
public Object compareWith(Object reference, String operatorString) {
    Collection<Object> values = new HashSet<>();
    for (Iterator<T> iterator = iterator(); iterator.hasNext(); ) {
        ValueTuple next = iterator.next();
        Object nextValue = next.getValue();
        if (nextValue instanceof Type) {
            Type nextType = (Type) nextValue;
            Class typeClass = nextType.getClass();
            Type referenceType = Type.Factory.createType(typeClass.getName());
            referenceType.setDelegateFromString(reference.toString());
            boolean keep = OperationEvaluator.compare(nextType.normalize(), referenceType.normalize(), operatorString);
            if (keep) {
                values.add(next);
            }
        }
    }
    log.debug("returning:" + values);
    return values;
}
Also used : NumberType(datawave.data.type.NumberType) Type(datawave.data.type.Type) ValueTuple(datawave.query.attributes.ValueTuple) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with ValueTuple

use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.

the class FunctionalSet method getGroupsForValue.

public Object getGroupsForValue(String value) {
    Set<String> groups = Sets.newHashSet();
    for (Iterator<T> iterator = iterator(); iterator.hasNext(); ) {
        ValueTuple next = iterator.next();
        String field = next.first();
        Object val = next.getValue();
        if (val instanceof String) {
            if (((String) val).equals(value)) {
                groups.add(field.substring(field.indexOf('.') + 1));
            }
        } else if (val instanceof Number) {
            String longValue = ((Number) val).toString();
            if (longValue.equals(value)) {
                groups.add(field.substring(field.indexOf('.') + 1));
            }
        } else if (val instanceof NumberType) {
            String longValue = "" + ((NumberType) val).getDelegate().longValue();
            if (longValue.equals(value)) {
                groups.add(field.substring(field.indexOf('.') + 1));
            }
        }
    }
    return !groups.isEmpty() ? groups.iterator().next() : null;
}
Also used : NumberType(datawave.data.type.NumberType) ValueTuple(datawave.query.attributes.ValueTuple)

Example 3 with ValueTuple

use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.

the class JexlEvaluation method apply.

@Override
public boolean apply(Tuple3<Key, Document, DatawaveJexlContext> input) {
    Object o = script.execute(input.third());
    if (log.isTraceEnabled()) {
        log.trace("Evaluation of " + query + " against " + input.third() + " returned " + o);
    }
    boolean matched = isMatched(o);
    // Add delayed info to document
    if (matched && input.third() instanceof DelayedNonEventIndexContext) {
        ((DelayedNonEventIndexContext) input.third()).populateDocument(input.second());
    }
    if (arithmetic instanceof HitListArithmetic) {
        HitListArithmetic hitListArithmetic = (HitListArithmetic) arithmetic;
        if (matched) {
            Document document = input.second();
            Attributes attributes = new Attributes(input.second().isToKeep());
            for (ValueTuple hitTuple : hitListArithmetic.getHitTuples()) {
                ColumnVisibility cv = null;
                String term = hitTuple.getFieldName() + ':' + hitTuple.getValue();
                if (hitTuple.getSource() != null) {
                    cv = hitTuple.getSource().getColumnVisibility();
                }
                // fall back to extracting column visibility from document
                if (cv == null) {
                    // get the visibility for the record with this hit
                    cv = HitListArithmetic.getColumnVisibilityForHit(document, term);
                // if no visibility computed, then there were no hits that match fields still in the document......
                }
                if (cv != null) {
                    // unused
                    // will force an update to make the metadata valid
                    long timestamp = document.getTimestamp();
                    Content content = new Content(term, document.getMetadata(), document.isToKeep());
                    content.setColumnVisibility(cv);
                    attributes.add(content);
                }
            }
            if (attributes.size() > 0) {
                document.put(HIT_TERM_FIELD, attributes);
            }
        }
        hitListArithmetic.clear();
    }
    return matched;
}
Also used : DelayedNonEventIndexContext(datawave.query.jexl.DelayedNonEventIndexContext) HitListArithmetic(datawave.query.jexl.HitListArithmetic) ValueTuple(datawave.query.attributes.ValueTuple) Content(datawave.query.attributes.Content) Attributes(datawave.query.attributes.Attributes) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Document(datawave.query.attributes.Document)

Example 4 with ValueTuple

use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.

the class EvaluationPhaseFilterFunctions method beforeDate.

/**
 * Searches for a date before end (exclusively)
 *
 * @param fieldValue
 *            An iterable of field values in the supplied format
 * @param pattern
 *            A date format to be supplied to java.text.SimpleDateFormat
 * @param end
 *            An end date in the supplied rangePattern format
 * @param rangePattern
 *            A date format to be supplied to java.text.SimpleDateFormat
 * @return True if the datetime occurs before the provided datetime value
 */
public static FunctionalSet<ValueTuple> beforeDate(Iterable<?> fieldValue, String pattern, String end, String rangePattern) {
    FunctionalSet<ValueTuple> matches = FunctionalSet.emptySet();
    if (fieldValue != null) {
        try {
            DateFormat format = newSimpleDateFormat(pattern);
            DateFormat rangeFormat = newSimpleDateFormat(rangePattern);
            long lStart = 0;
            long lEnd = getTime(end, rangeFormat) - 1;
            for (Object o : fieldValue) {
                if (betweenInclusive(getTime(o, format), lStart, lEnd)) {
                    matches = FunctionalSet.singleton(getHitTerm(o));
                    break;
                }
            }
        } catch (ParseException pe) {
            log.error(pe.getMessage());
        } catch (NumberFormatException nfe) {
            log.error("Unable to numeric argument " + end + ": " + nfe.getMessage());
        }
    }
    return matches;
}
Also used : ValueTuple(datawave.query.attributes.ValueTuple) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException)

Example 5 with ValueTuple

use of datawave.query.attributes.ValueTuple in project datawave by NationalSecurityAgency.

the class EvaluationPhaseFilterFunctions method getAllMatches.

/**
 * Returns a set that contains the hit term for each field value where the regex matches against the value of the field value. If the regex string contains
 * case-insensitive flags, e.g. {@code (?i).*(?-i)}, a search for a match will also be done against the normalized value of the field value.
 * <p>
 * Note: the regex will be compiled into a {@link Pattern} with case-insensitive and multiline matching. Additionally, a regex of {@code ".*"} still
 * requires a value to be present. In other words, searching for {@code FIELD:'.*'} requires a value for {@code FIELD} to exist in the document to match.
 *
 * @param values
 *            the values to evaluate
 * @param regex
 *            the regex
 * @return a {@link FunctionalSet} with the matching hit term, or an empty set if no matches were found
 */
public static FunctionalSet<ValueTuple> getAllMatches(Iterable<?> values, String regex) {
    if (values != null) {
        final Pattern pattern = JexlPatternCache.getPattern(regex);
        final boolean caseInsensitive = regex.matches(CASE_INSENSITIVE);
        // @formatter:off
        FunctionalSet<ValueTuple> matches = StreamSupport.stream(values.spliterator(), false).filter(Objects::nonNull).filter((value) -> isMatchForPattern(pattern, caseInsensitive, value)).map(EvaluationPhaseFilterFunctions::getHitTerm).collect(Collectors.toCollection(FunctionalSet::new));
        // @formatter:on
        return FunctionalSet.unmodifiableSet(matches);
    }
    return FunctionalSet.emptySet();
}
Also used : Pattern(java.util.regex.Pattern) ValueTuple(datawave.query.attributes.ValueTuple)

Aggregations

ValueTuple (datawave.query.attributes.ValueTuple)26 DateFormat (java.text.DateFormat)10 ParseException (java.text.ParseException)10 SimpleDateFormat (java.text.SimpleDateFormat)10 HashSet (java.util.HashSet)5 NumberType (datawave.data.type.NumberType)3 Attributes (datawave.query.attributes.Attributes)2 TypeAttribute (datawave.query.attributes.TypeAttribute)2 FunctionalSet (datawave.query.collections.FunctionalSet)2 Key (org.apache.accumulo.core.data.Key)2 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)2 GeoNormalizer (datawave.data.normalizer.GeoNormalizer)1 AbstractGeometryType (datawave.data.type.AbstractGeometryType)1 GeoType (datawave.data.type.GeoType)1 LcNoDiacriticsType (datawave.data.type.LcNoDiacriticsType)1 Type (datawave.data.type.Type)1 AbstractGeometry (datawave.data.type.util.AbstractGeometry)1 MarkingFunctions (datawave.marking.MarkingFunctions)1 Content (datawave.query.attributes.Content)1 Document (datawave.query.attributes.Document)1