Search in sources :

Example 11 with IntFunction

use of java.util.function.IntFunction in project neo4j by neo4j.

the class ServerPoliciesLoadBalancingIT method shouldSupportSeveralPolicies.

@Test
public void shouldSupportSeveralPolicies() throws Exception {
    Map<String, IntFunction<String>> instanceCoreParams = new HashMap<>();
    instanceCoreParams.put(CausalClusteringSettings.server_groups.name(), (id) -> "core" + id + ",core");
    Map<String, IntFunction<String>> instanceReplicaParams = new HashMap<>();
    instanceReplicaParams.put(CausalClusteringSettings.server_groups.name(), (id) -> "replica" + id + ",replica");
    String defaultPolicySpec = "groups(replica0,replica1)";
    String policyOneTwoSpec = "groups(replica1,replica2)";
    String policyZeroTwoSpec = "groups(replica0,replica2)";
    String policyAllReplicasSpec = "groups(replica); halt()";
    String allPolicySpec = "all()";
    Map<String, String> coreParams = stringMap(CausalClusteringSettings.cluster_allow_reads_on_followers.name(), "true", CausalClusteringSettings.load_balancing_config.name() + ".server_policies.all", allPolicySpec, CausalClusteringSettings.load_balancing_config.name() + ".server_policies.default", defaultPolicySpec, CausalClusteringSettings.load_balancing_config.name() + ".server_policies.policy_one_two", policyOneTwoSpec, CausalClusteringSettings.load_balancing_config.name() + ".server_policies.policy_zero_two", policyZeroTwoSpec, CausalClusteringSettings.load_balancing_config.name() + ".server_policies.policy_all_replicas", policyAllReplicasSpec, CausalClusteringSettings.multi_dc_license.name(), "true");
    cluster = new Cluster(testDir.directory("cluster"), 3, 3, new HazelcastDiscoveryServiceFactory(), coreParams, instanceCoreParams, emptyMap(), instanceReplicaParams, Standard.LATEST_NAME);
    cluster.start();
    assertGetServersEventuallyMatchesOnAllCores(new CountsMatcher(3, 1, 2, 3), policyContext("all"));
    for (CoreClusterMember core : cluster.coreMembers()) {
        CoreGraphDatabase db = core.database();
        assertThat(getServers(db, policyContext("default")), new SpecificReplicasMatcher(0, 1));
        assertThat(getServers(db, policyContext("policy_one_two")), new SpecificReplicasMatcher(1, 2));
        assertThat(getServers(db, policyContext("policy_zero_two")), new SpecificReplicasMatcher(0, 2));
        assertThat(getServers(db, policyContext("policy_all_replicas")), new SpecificReplicasMatcher(0, 1, 2));
    }
}
Also used : HazelcastDiscoveryServiceFactory(org.neo4j.causalclustering.discovery.HazelcastDiscoveryServiceFactory) HashMap(java.util.HashMap) CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) IntFunction(java.util.function.IntFunction) Cluster(org.neo4j.causalclustering.discovery.Cluster) CoreGraphDatabase(org.neo4j.causalclustering.core.CoreGraphDatabase) Test(org.junit.Test)

Example 12 with IntFunction

use of java.util.function.IntFunction in project jdk8u_jdk by JetBrains.

the class DistinctOps method makeRef.

/**
     * Appends a "distinct" operation to the provided stream, and returns the
     * new stream.
     *
     * @param <T> the type of both input and output elements
     * @param upstream a reference stream with element type T
     * @return the new stream
     */
static <T> ReferencePipeline<T, T> makeRef(AbstractPipeline<?, T, ?> upstream) {
    return new ReferencePipeline.StatefulOp<T, T>(upstream, StreamShape.REFERENCE, StreamOpFlag.IS_DISTINCT | StreamOpFlag.NOT_SIZED) {

        <P_IN> Node<T> reduce(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
            // If the stream is SORTED then it should also be ORDERED so the following will also
            // preserve the sort order
            TerminalOp<T, LinkedHashSet<T>> reduceOp = ReduceOps.<T, LinkedHashSet<T>>makeRef(LinkedHashSet::new, LinkedHashSet::add, LinkedHashSet::addAll);
            return Nodes.node(reduceOp.evaluateParallel(helper, spliterator));
        }

        @Override
        <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper, Spliterator<P_IN> spliterator, IntFunction<T[]> generator) {
            if (StreamOpFlag.DISTINCT.isKnown(helper.getStreamAndOpFlags())) {
                // No-op
                return helper.evaluate(spliterator, false, generator);
            } else if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
                return reduce(helper, spliterator);
            } else {
                // Holder of null state since ConcurrentHashMap does not support null values
                AtomicBoolean seenNull = new AtomicBoolean(false);
                ConcurrentHashMap<T, Boolean> map = new ConcurrentHashMap<>();
                TerminalOp<T, Void> forEachOp = ForEachOps.makeRef(t -> {
                    if (t == null)
                        seenNull.set(true);
                    else
                        map.putIfAbsent(t, Boolean.TRUE);
                }, false);
                forEachOp.evaluateParallel(helper, spliterator);
                // If null has been seen then copy the key set into a HashSet that supports null values
                // and add null
                Set<T> keys = map.keySet();
                if (seenNull.get()) {
                    // TODO Implement a more efficient set-union view, rather than copying
                    keys = new HashSet<>(keys);
                    keys.add(null);
                }
                return Nodes.node(keys);
            }
        }

        @Override
        <P_IN> Spliterator<T> opEvaluateParallelLazy(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
            if (StreamOpFlag.DISTINCT.isKnown(helper.getStreamAndOpFlags())) {
                // No-op
                return helper.wrapSpliterator(spliterator);
            } else if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
                // Not lazy, barrier required to preserve order
                return reduce(helper, spliterator).spliterator();
            } else {
                // Lazy
                return new StreamSpliterators.DistinctSpliterator<>(helper.wrapSpliterator(spliterator));
            }
        }

        @Override
        Sink<T> opWrapSink(int flags, Sink<T> sink) {
            Objects.requireNonNull(sink);
            if (StreamOpFlag.DISTINCT.isKnown(flags)) {
                return sink;
            } else if (StreamOpFlag.SORTED.isKnown(flags)) {
                return new Sink.ChainedReference<T, T>(sink) {

                    boolean seenNull;

                    T lastSeen;

                    @Override
                    public void begin(long size) {
                        seenNull = false;
                        lastSeen = null;
                        downstream.begin(-1);
                    }

                    @Override
                    public void end() {
                        seenNull = false;
                        lastSeen = null;
                        downstream.end();
                    }

                    @Override
                    public void accept(T t) {
                        if (t == null) {
                            if (!seenNull) {
                                seenNull = true;
                                downstream.accept(lastSeen = null);
                            }
                        } else if (lastSeen == null || !t.equals(lastSeen)) {
                            downstream.accept(lastSeen = t);
                        }
                    }
                };
            } else {
                return new Sink.ChainedReference<T, T>(sink) {

                    Set<T> seen;

                    @Override
                    public void begin(long size) {
                        seen = new HashSet<>();
                        downstream.begin(-1);
                    }

                    @Override
                    public void end() {
                        seen = null;
                        downstream.end();
                    }

                    @Override
                    public void accept(T t) {
                        if (!seen.contains(t)) {
                            seen.add(t);
                            downstream.accept(t);
                        }
                    }
                };
            }
        }
    };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Objects(java.util.Objects) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Spliterator(java.util.Spliterator) LinkedHashSet(java.util.LinkedHashSet) IntFunction(java.util.function.IntFunction) HashSet(java.util.HashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntFunction(java.util.function.IntFunction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Spliterator(java.util.Spliterator) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 13 with IntFunction

use of java.util.function.IntFunction in project lucene-solr by apache.

the class SimpleTextDocValuesReader method getBinary.

@Override
public synchronized BinaryDocValues getBinary(FieldInfo fieldInfo) throws IOException {
    final OneField field = fields.get(fieldInfo.name);
    // valid:
    assert field != null;
    final IndexInput in = data.clone();
    final BytesRefBuilder scratch = new BytesRefBuilder();
    final DecimalFormat decoder = new DecimalFormat(field.pattern, new DecimalFormatSymbols(Locale.ROOT));
    DocValuesIterator docsWithField = getBinaryDocsWithField(fieldInfo);
    IntFunction<BytesRef> values = new IntFunction<BytesRef>() {

        final BytesRefBuilder term = new BytesRefBuilder();

        @Override
        public BytesRef apply(int docID) {
            try {
                if (docID < 0 || docID >= maxDoc) {
                    throw new IndexOutOfBoundsException("docID must be 0 .. " + (maxDoc - 1) + "; got " + docID);
                }
                in.seek(field.dataStartFilePointer + (9 + field.pattern.length() + field.maxLength + 2) * docID);
                SimpleTextUtil.readLine(in, scratch);
                assert StringHelper.startsWith(scratch.get(), LENGTH);
                int len;
                try {
                    len = decoder.parse(new String(scratch.bytes(), LENGTH.length, scratch.length() - LENGTH.length, StandardCharsets.UTF_8)).intValue();
                } catch (ParseException pe) {
                    throw new CorruptIndexException("failed to parse int length", in, pe);
                }
                term.grow(len);
                term.setLength(len);
                in.readBytes(term.bytes(), 0, len);
                return term.get();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
    };
    return new BinaryDocValues() {

        @Override
        public int nextDoc() throws IOException {
            return docsWithField.nextDoc();
        }

        @Override
        public int docID() {
            return docsWithField.docID();
        }

        @Override
        public long cost() {
            return docsWithField.cost();
        }

        @Override
        public int advance(int target) throws IOException {
            return docsWithField.advance(target);
        }

        @Override
        public boolean advanceExact(int target) throws IOException {
            return docsWithField.advanceExact(target);
        }

        @Override
        public BytesRef binaryValue() throws IOException {
            return values.apply(docsWithField.docID());
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) IOException(java.io.IOException) IntFunction(java.util.function.IntFunction) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) IndexInput(org.apache.lucene.store.IndexInput) ParseException(java.text.ParseException) BytesRef(org.apache.lucene.util.BytesRef)

Example 14 with IntFunction

use of java.util.function.IntFunction in project lucene-solr by apache.

the class SimpleTextDocValuesReader method getNumericNonIterator.

IntFunction<Long> getNumericNonIterator(FieldInfo fieldInfo) throws IOException {
    final OneField field = fields.get(fieldInfo.name);
    assert field != null;
    // valid:
    assert field != null : "field=" + fieldInfo.name + " fields=" + fields;
    final IndexInput in = data.clone();
    final BytesRefBuilder scratch = new BytesRefBuilder();
    final DecimalFormat decoder = new DecimalFormat(field.pattern, new DecimalFormatSymbols(Locale.ROOT));
    decoder.setParseBigDecimal(true);
    return new IntFunction<Long>() {

        @Override
        public Long apply(int docID) {
            try {
                //System.out.println(Thread.currentThread().getName() + ": get docID=" + docID + " in=" + in);
                if (docID < 0 || docID >= maxDoc) {
                    throw new IndexOutOfBoundsException("docID must be 0 .. " + (maxDoc - 1) + "; got " + docID);
                }
                in.seek(field.dataStartFilePointer + (1 + field.pattern.length() + 2) * docID);
                SimpleTextUtil.readLine(in, scratch);
                //System.out.println("parsing delta: " + scratch.utf8ToString());
                BigDecimal bd;
                try {
                    bd = (BigDecimal) decoder.parse(scratch.get().utf8ToString());
                } catch (ParseException pe) {
                    throw new CorruptIndexException("failed to parse BigDecimal value", in, pe);
                }
                // read the line telling us if it's real or not
                SimpleTextUtil.readLine(in, scratch);
                return BigInteger.valueOf(field.minValue).add(bd.toBigIntegerExact()).longValue();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DecimalFormatSymbols(java.text.DecimalFormatSymbols) IntFunction(java.util.function.IntFunction) DecimalFormat(java.text.DecimalFormat) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) IndexInput(org.apache.lucene.store.IndexInput) ParseException(java.text.ParseException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal)

Aggregations

IntFunction (java.util.function.IntFunction)14 Test (org.junit.Test)6 BytesRef (org.apache.lucene.util.BytesRef)5 HashMap (java.util.HashMap)4 Cluster (org.neo4j.causalclustering.discovery.Cluster)4 HazelcastDiscoveryServiceFactory (org.neo4j.causalclustering.discovery.HazelcastDiscoveryServiceFactory)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IOException (java.io.IOException)2 DecimalFormat (java.text.DecimalFormat)2 DecimalFormatSymbols (java.text.DecimalFormatSymbols)2 ParseException (java.text.ParseException)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Objects (java.util.Objects)2 Set (java.util.Set)2 Spliterator (java.util.Spliterator)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)2 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)2 IndexInput (org.apache.lucene.store.IndexInput)2