Java Code Examples for java.util.stream.LongStream#forEach()

The following examples show how to use java.util.stream.LongStream#forEach() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MinShouldMatchSumScorer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static long cost(LongStream costs, int numScorers, int minShouldMatch) {
  // the idea here is the following: a boolean query c1,c2,...cn with minShouldMatch=m
  // could be rewritten to:
  // (c1 AND (c2..cn|msm=m-1)) OR (!c1 AND (c2..cn|msm=m))
  // if we assume that clauses come in ascending cost, then
  // the cost of the first part is the cost of c1 (because the cost of a conjunction is
  // the cost of the least costly clause)
  // the cost of the second part is the cost of finding m matches among the c2...cn
  // remaining clauses
  // since it is a disjunction overall, the total cost is the sum of the costs of these
  // two parts

  // If we recurse infinitely, we find out that the cost of a msm query is the sum of the
  // costs of the num_scorers - minShouldMatch + 1 least costly scorers
  final PriorityQueue<Long> pq = new PriorityQueue<Long>(numScorers - minShouldMatch + 1) {
    @Override
    protected boolean lessThan(Long a, Long b) {
      return a > b;
    }
  };
  costs.forEach(pq::insertWithOverflow);
  return StreamSupport.stream(pq.spliterator(), false).mapToLong(Number::longValue).sum();
}
 
Example 2
Source File: BasicClusteredCacheOpsReplicationWithMultipleClientsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test(timeout=180000)
public void testCRUD() throws Exception {
  Random random = new Random();
  LongStream longStream = random.longs(1000);
  Set<Long> added = new HashSet<>();
  longStream.forEach(x -> {
    cache1.put(x, new BlobValue());
    added.add(x);
  });

  Set<Long> readKeysByCache2BeforeFailOver = new HashSet<>();
  added.forEach(x -> {
    if (cache2.get(x) != null) {
      readKeysByCache2BeforeFailOver.add(x);
    }
  });

  CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
  CLUSTER.getClusterControl().terminateActive();

  Set<Long> readKeysByCache1AfterFailOver = new HashSet<>();
  added.forEach(x -> {
    if (cache1.get(x) != null) {
      readKeysByCache1AfterFailOver.add(x);
    }
  });

  assertThat(readKeysByCache2BeforeFailOver.size(), greaterThanOrEqualTo(readKeysByCache1AfterFailOver.size()));

  readKeysByCache1AfterFailOver.stream().filter(readKeysByCache2BeforeFailOver::contains).forEach(y -> assertThat(cache2.get(y), notNullValue()));

}
 
Example 3
Source File: BasicClusteredCacheOpsReplicationWithMultipleClientsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test(timeout=180000)
@Ignore //TODO: FIXME: FIX THIS RANDOMLY FAILING TEST
public void testBulkOps() throws Exception {
  List<Cache<Long, BlobValue>> caches = new ArrayList<>();
  caches.add(cache1);
  caches.add(cache2);

  Map<Long, BlobValue> entriesMap = new HashMap<>();

  Random random = new Random();
  LongStream longStream = random.longs(1000);

  longStream.forEach(x -> entriesMap.put(x, new BlobValue()));
  caches.forEach(cache -> cache.putAll(entriesMap));

  Set<Long> keySet = entriesMap.keySet();

  Set<Long> readKeysByCache2BeforeFailOver = new HashSet<>();
  keySet.forEach(x -> {
    if (cache2.get(x) != null) {
      readKeysByCache2BeforeFailOver.add(x);
    }
  });

  CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
  CLUSTER.getClusterControl().terminateActive();

  Set<Long> readKeysByCache1AfterFailOver = new HashSet<>();
  keySet.forEach(x -> {
    if (cache1.get(x) != null) {
      readKeysByCache1AfterFailOver.add(x);
    }
  });

  assertThat(readKeysByCache2BeforeFailOver.size(), greaterThanOrEqualTo(readKeysByCache1AfterFailOver.size()));

  readKeysByCache1AfterFailOver.stream().filter(readKeysByCache2BeforeFailOver::contains).forEach(y -> assertThat(cache2.get(y), notNullValue()));

}
 
Example 4
Source File: BasicClusteredCacheOpsReplicationWithMultipleClientsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test(timeout=180000)
public void testClear() throws Exception {
  List<Cache<Long, BlobValue>> caches = new ArrayList<>();
  caches.add(cache1);
  caches.add(cache2);

  Map<Long, BlobValue> entriesMap = new HashMap<>();

  Random random = new Random();
  LongStream longStream = random.longs(1000);

  longStream.forEach(x -> entriesMap.put(x, new BlobValue()));
  caches.forEach(cache -> cache.putAll(entriesMap));

  Set<Long> keySet = entriesMap.keySet();

  Set<Long> readKeysByCache2BeforeFailOver = new HashSet<>();
  keySet.forEach(x -> {
    if (cache2.get(x) != null) {
      readKeysByCache2BeforeFailOver.add(x);
    }
  });

  cache1.clear();

  CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
  CLUSTER.getClusterControl().terminateActive();

  if (cacheConsistency == Consistency.STRONG) {
    readKeysByCache2BeforeFailOver.forEach(x -> assertThat(cache2.get(x), nullValue()));
  } else {
    readKeysByCache2BeforeFailOver.forEach(x -> assertThat(cache1.get(x), nullValue()));
  }

}
 
Example 5
Source File: Bounds.java    From morpheus-core with Apache License 2.0 4 votes vote down vote up
/**
 * Retruns the integer bounds of a stream of longs
 * @param stream    the stream to compute bounds on
 * @return          the bounds for stream, empty if no data in stream
 */
public static Optional<Bounds<Long>> ofLongs(LongStream stream) {
    final OfLongs calculator = new OfLongs();
    stream.forEach(calculator::add);
    return calculator.getBounds();
}
 
Example 6
Source File: LongColumn.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static LongColumn create(String name, LongStream stream) {
  LongArrayList list = new LongArrayList();
  stream.forEach(list::add);
  return new LongColumn(name, list);
}
 
Example 7
Source File: LongColumn.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static LongColumn create(String name, LongStream stream) {
  LongArrayList list = new LongArrayList();
  stream.forEach(list::add);
  return new LongColumn(name, list);
}
 
Example 8
Source File: FetchIdTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeAndDecode() throws Exception {
    LongStream longs = random().longs(50, 0, Long.MAX_VALUE);
    longs.forEach(this::assertEncodeDecodeMatches);
}