Search in sources :

Example 16 with BiPredicate

use of java.util.function.BiPredicate in project nifi by apache.

the class ListFile method createFileFilter.

private BiPredicate<Path, BasicFileAttributes> createFileFilter(final ProcessContext context) {
    final long minSize = context.getProperty(MIN_SIZE).asDataSize(DataUnit.B).longValue();
    final Double maxSize = context.getProperty(MAX_SIZE).asDataSize(DataUnit.B);
    final long minAge = context.getProperty(MIN_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
    final Long maxAge = context.getProperty(MAX_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
    final boolean ignoreHidden = context.getProperty(IGNORE_HIDDEN_FILES).asBoolean();
    final Pattern filePattern = Pattern.compile(context.getProperty(FILE_FILTER).getValue());
    final String indir = context.getProperty(DIRECTORY).evaluateAttributeExpressions().getValue();
    final boolean recurseDirs = context.getProperty(RECURSE).asBoolean();
    final String pathPatternStr = context.getProperty(PATH_FILTER).getValue();
    final Pattern pathPattern = (!recurseDirs || pathPatternStr == null) ? null : Pattern.compile(pathPatternStr);
    return (path, attributes) -> {
        if (minSize > attributes.size()) {
            return false;
        }
        if (maxSize != null && maxSize < attributes.size()) {
            return false;
        }
        final long fileAge = System.currentTimeMillis() - attributes.lastModifiedTime().toMillis();
        if (minAge > fileAge) {
            return false;
        }
        if (maxAge != null && maxAge < fileAge) {
            return false;
        }
        if (ignoreHidden && path.toFile().isHidden()) {
            return false;
        }
        if (pathPattern != null) {
            Path reldir = Paths.get(indir).relativize(path).getParent();
            if (reldir != null && !reldir.toString().isEmpty()) {
                if (!pathPattern.matcher(reldir.toString()).matches()) {
                    return false;
                }
            }
        }
        // Verify that we have at least read permissions on the file we're considering grabbing
        if (!Files.isReadable(path)) {
            return false;
        }
        return filePattern.matcher(path.getFileName().toString()).matches();
    };
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) Date(java.util.Date) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Scope(org.apache.nifi.components.state.Scope) Locale(java.util.Locale) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) Map(java.util.Map) Path(java.nio.file.Path) TriggerSerially(org.apache.nifi.annotation.behavior.TriggerSerially) DateFormat(java.text.DateFormat) Set(java.util.Set) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) AllowableValue(org.apache.nifi.components.AllowableValue) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) Collectors(java.util.stream.Collectors) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) Stateful(org.apache.nifi.annotation.behavior.Stateful) AbstractListProcessor(org.apache.nifi.processor.util.list.AbstractListProcessor) List(java.util.List) Stream(java.util.stream.Stream) Tags(org.apache.nifi.annotation.documentation.Tags) DataUnit(org.apache.nifi.processor.DataUnit) Pattern(java.util.regex.Pattern) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) FileInfo(org.apache.nifi.processors.standard.util.FileInfo) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) Relationship(org.apache.nifi.processor.Relationship) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) FileStore(java.nio.file.FileStore) Files(java.nio.file.Files) ProcessContext(org.apache.nifi.processor.ProcessContext) IOException(java.io.IOException) SeeAlso(org.apache.nifi.annotation.documentation.SeeAlso) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) Paths(java.nio.file.Paths) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) Collections(java.util.Collections) Path(java.nio.file.Path) Pattern(java.util.regex.Pattern)

Example 17 with BiPredicate

use of java.util.function.BiPredicate in project unipop by unipop-graph.

the class DateFieldPropertySchema method getValues.

@Override
public Set<Object> getValues(PredicatesHolder predicatesHolder) {
    Stream<PredicatesHolder> predicates = predicatesHolder.findKey(this.key).map(this::explodeConnective);
    Map<String, Date> datePredicates = new HashMap<>();
    predicates.flatMap(p -> p.getPredicates().stream()).forEach(has -> {
        String biPredicate = has.getBiPredicate().toString();
        Object value = has.getValue();
        switch(biPredicate) {
            case "eq":
                datePredicates.put("eq", fromDisplay(value.toString()));
                break;
            case "gt":
                datePredicates.put("gt", fromDisplay(value.toString()));
            case "gte":
                datePredicates.put("gte", fromDisplay(value.toString()));
                break;
            case "lt":
                datePredicates.put("lt", fromDisplay(value.toString()));
                break;
            case "lte":
                datePredicates.put("lte", fromDisplay(value.toString()));
                break;
            default:
                throw new IllegalArgumentException("cant get value");
        }
    });
    if (datePredicates.size() == 0)
        return Collections.emptySet();
    if (datePredicates.containsKey("eq"))
        return Collections.singleton(toSource(datePredicates.get("eq")));
    else if ((datePredicates.containsKey("gt") || datePredicates.containsKey("gte")) && (datePredicates.containsKey("lt") || datePredicates.containsKey("lte"))) {
        Date from = datePredicates.containsKey("gt") ? datePredicates.get("gt") : datePredicates.get("gte");
        Date to = datePredicates.containsKey("lt") ? datePredicates.get("lt") : datePredicates.get("lte");
        List<Date> dates = new ArrayList<>();
        long interval = this.interval;
        long endTime = to.getTime();
        long curTime = from.getTime();
        while (curTime <= endTime) {
            dates.add(new Date(curTime));
            curTime += interval;
        }
        return dates.stream().map(this::toSource).collect(Collectors.toSet());
    } else
        throw new IllegalArgumentException("cant get only gt or lt value");
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) MultiDateFormat(org.unipop.util.MultiDateFormat) java.util(java.util) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) SimpleDateFormat(java.text.SimpleDateFormat) Collectors(java.util.stream.Collectors) PredicatesHolderFactory(org.unipop.query.predicates.PredicatesHolderFactory) BiPredicate(java.util.function.BiPredicate) JSONObject(org.json.JSONObject) Stream(java.util.stream.Stream) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) ConversionUtils(org.unipop.util.ConversionUtils) P(org.apache.tinkerpop.gremlin.process.traversal.P) JSONArray(org.json.JSONArray) DateFormat(java.text.DateFormat) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) JSONObject(org.json.JSONObject)

Example 18 with BiPredicate

use of java.util.function.BiPredicate in project pravega by pravega.

the class TableSegmentImplTest method testIterator.

private <T> void testIterator(Function<SegmentIteratorArgs, AsyncIterator<IteratorItem<T>>> newIterator, Supplier<ByteBuf> getLastRequestFromKey, Supplier<ByteBuf> getLastRequestToKey, Function<TableSegmentEntry, T> getItemFromEntry, Consumer<List<T>> sendReply, BiPredicate<T, T> checkItemEquality) throws Exception {
    val suggestedKeyCount = 3;
    // Generate 100 Entries and split them into batches.
    val allEntries = IntStream.range(0, 100).mapToObj(i -> versionedEntry(i * 10L, Integer.toString(i * 10), 1L)).collect(Collectors.toList());
    val inputEntries = splitIteratorInputs(allEntries);
    // Do an full iteration as well.
    inputEntries.add(allEntries);
    // Check regular iteration.
    for (int i = 0; i < inputEntries.size(); i++) {
        val entryList = inputEntries.get(i);
        SegmentIteratorArgs args = SegmentIteratorArgs.builder().fromKey(entryList.get(0).getKey().getKey()).toKey(entryList.get(entryList.size() - 1).getKey().getKey()).maxItemsAtOnce(suggestedKeyCount).build();
        // We collect iterated items in this list.
        val actualItems = new ArrayList<T>();
        val itemsToReturn = entryList.iterator();
        val tableIterator = newIterator.apply(args);
        while (itemsToReturn.hasNext()) {
            val iteratorFuture = tableIterator.getNext();
            // Verify the wire command got sent as expected.
            val requestFromKey = getLastRequestFromKey.get();
            Assert.assertEquals("Unexpected fromKey sent.", args.getFromKey(), requestFromKey);
            val requestToKey = getLastRequestToKey.get();
            Assert.assertEquals("Unexpected toKey sent.", args.getToKey(), requestToKey);
            // Send a reply.
            val expectedResult = new ArrayList<T>();
            int count = suggestedKeyCount;
            while (itemsToReturn.hasNext() && count > 0) {
                val next = itemsToReturn.next();
                expectedResult.add(getItemFromEntry.apply(next));
                args = args.next(next.getKey().getKey());
                count--;
            }
            sendReply.accept(expectedResult);
            // Check the partial result.
            val iteratorResult = iteratorFuture.get(SHORT_TIMEOUT, TimeUnit.MILLISECONDS);
            AssertExtensions.assertListEquals("Unexpected partial result.", expectedResult, iteratorResult.getItems(), checkItemEquality);
            actualItems.addAll(iteratorResult.getItems());
        }
        // Then the final result.
        val expected = entryList.stream().map(getItemFromEntry).collect(Collectors.toList());
        AssertExtensions.assertListEquals("Unexpected result.", expected, actualItems, checkItemEquality);
    }
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) NoSuchKeyException(io.pravega.client.tables.NoSuchKeyException) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Cleanup(lombok.Cleanup) Unpooled(io.netty.buffer.Unpooled) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) IteratorItem(io.pravega.client.tables.IteratorItem) Request(io.pravega.shared.protocol.netty.Request) DelegationTokenProviderFactory(io.pravega.client.security.auth.DelegationTokenProviderFactory) Collectors(java.util.stream.Collectors) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) List(java.util.List) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) TestUtils(io.pravega.test.common.TestUtils) BadKeyVersionException(io.pravega.client.tables.BadKeyVersionException) IntStream(java.util.stream.IntStream) Segment(io.pravega.client.segment.impl.Segment) Setter(lombok.Setter) Getter(lombok.Getter) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Reply(io.pravega.shared.protocol.netty.Reply) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Deque(java.util.Deque) Append(io.pravega.shared.protocol.netty.Append) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) BiPredicate(java.util.function.BiPredicate) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) Charsets(com.google.common.base.Charsets) LongStream(java.util.stream.LongStream) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) lombok.val(lombok.val) AsyncIterator(io.pravega.common.util.AsyncIterator) Test(org.junit.Test) AuthenticationException(io.pravega.auth.AuthenticationException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WireCommand(io.pravega.shared.protocol.netty.WireCommand) AbstractMap(java.util.AbstractMap) KeyValueTableClientConfiguration(io.pravega.client.tables.KeyValueTableClientConfiguration) ArrayDeque(java.util.ArrayDeque) Assert(org.junit.Assert) Collections(java.util.Collections) ArrayList(java.util.ArrayList)

Example 19 with BiPredicate

use of java.util.function.BiPredicate in project cuba by cuba-platform.

the class AccessConstraintMethodPredicate method createBiPredicate.

private static BiPredicate<AccessGroupDefinition, Entity> createBiPredicate(String className, String methodName, String argClassName) {
    Class<?> clazz = ReflectionHelper.getClass(className);
    Class<?> argumentClazz = ReflectionHelper.getClass(argClassName);
    Method method = Arrays.stream(clazz.getMethods()).filter(m -> Objects.equals(m.getName(), methodName)).filter(m -> m.getParameterCount() == 1 && Objects.equals(m.getParameterTypes()[0], argumentClazz)).findFirst().orElseThrow(() -> new RuntimeException(String.format("Unable to find corresponding in-memory predicate method %s#%s", className, methodName)));
    try {
        MethodHandles.Lookup caller = MethodHandles.lookup();
        CallSite site = LambdaMetafactory.metafactory(caller, "test", MethodType.methodType(BiPredicate.class), MethodType.methodType(boolean.class, Object.class, Object.class), caller.findVirtual(clazz, method.getName(), MethodType.methodType(method.getReturnType(), argumentClazz)), MethodType.methodType(boolean.class, clazz, argumentClazz));
        // noinspection unchecked
        return (BiPredicate<AccessGroupDefinition, Entity>) site.getTarget().invoke();
    } catch (Throwable e) {
        throw new IllegalStateException(String.format("Can't create in-memory constraint predicate for method %s#%s", className, methodName), e);
    }
}
Also used : LambdaMetafactory(java.lang.invoke.LambdaMetafactory) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) ReflectionHelper(com.haulmont.bali.util.ReflectionHelper) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) AppBeans(com.haulmont.cuba.core.global.AppBeans) Objects(java.util.Objects) BiPredicate(java.util.function.BiPredicate) MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Entity(com.haulmont.cuba.core.entity.Entity) MethodHandles(java.lang.invoke.MethodHandles) CallSite(java.lang.invoke.CallSite) Method(java.lang.reflect.Method) BiPredicate(java.util.function.BiPredicate)

Example 20 with BiPredicate

use of java.util.function.BiPredicate in project ignite by apache.

the class GridSubqueryJoinOptimizer method remapColumns.

/**
 * Remap all columns that satisfy the predicate such they be referred to the given table.
 *
 * @param parent Tree where to search columns.
 * @param subSelect Tree where to search column aliases.
 * @param colPred Collection predicate.
 * @param tbl Table.
 */
private static void remapColumns(GridSqlAst parent, GridSqlAst subSelect, Predicate<GridSqlColumn> colPred, GridSqlAlias tbl) {
    ASTNodeFinder colFinder = new ASTNodeFinder(parent, (p, c) -> c instanceof GridSqlColumn && colPred.test((GridSqlColumn) c));
    ASTNodeFinder.Result res;
    while ((res = colFinder.findNext()) != null) {
        GridSqlColumn oldCol = res.getEl().child(res.getIdx());
        BiPredicate<GridSqlAst, GridSqlAst> constPred = (p, c) -> c != null && c.getSQL().equals(oldCol.columnName());
        BiPredicate<GridSqlAst, GridSqlAst> aliasPred = (p, c) -> c instanceof GridSqlAlias && ((GridSqlAlias) c).alias().equals(oldCol.columnName());
        ASTNodeFinder.Result aliasOrPred = findNode(subSelect, constPred.or(aliasPred));
        if (aliasOrPred != null)
            res.getEl().child(res.getIdx(), GridSqlAlias.unwrap(aliasOrPred.getEl().child(aliasOrPred.getIdx())));
        else {
            res.getEl().child(res.getIdx(), new GridSqlColumn(oldCol.column(), tbl, oldCol.schema(), tbl.alias(), oldCol.columnName()));
        }
    }
}
Also used : Arrays(java.util.Arrays) GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) GridSqlAggregateFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAggregateFunction) GridSqlSubquery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) Index(org.h2.index.Index) AND(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND) ArrayList(java.util.ArrayList) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) Column(org.h2.table.Column) BiPredicate(java.util.function.BiPredicate) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) EQUAL(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) F(org.apache.ignite.internal.util.typedef.F) GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlJoin(org.apache.ignite.internal.processors.query.h2.sql.GridSqlJoin) IN(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN) GridSqlArray(org.apache.ignite.internal.processors.query.h2.sql.GridSqlArray) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) GridH2ProxyIndex(org.apache.ignite.internal.processors.query.h2.opt.GridH2ProxyIndex) GridSqlQuery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery) GridSqlFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) EQUAL_NULL_SAFE(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL_NULL_SAFE) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) EXISTS(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EXISTS) NEGATE(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.NEGATE) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)

Aggregations

BiPredicate (java.util.function.BiPredicate)36 List (java.util.List)17 Collectors (java.util.stream.Collectors)16 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)12 Collections (java.util.Collections)12 Map (java.util.Map)11 Files (java.nio.file.Files)10 Objects (java.util.Objects)8 Set (java.util.Set)8 HashMap (java.util.HashMap)7 Paths (java.nio.file.Paths)6 Arrays (java.util.Arrays)6 HashSet (java.util.HashSet)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)6 Path (java.nio.file.Path)5 java.util (java.util)5 Collection (java.util.Collection)5 Function (java.util.function.Function)5