private JdbcIdentity getIdentity() {
      if (this.identity == null) {
        JdbcSecurityRealm.this
            .queryConfiguration
            .stream()
            .map(
                queryConfiguration -> {
                  return executePrincipalQuery(
                      queryConfiguration,
                      resultSet -> {
                        if (resultSet.next()) {
                          MapAttributes attributes = new MapAttributes();

                          do {
                            queryConfiguration
                                .getColumnMappers(AttributeMapper.class)
                                .forEach(
                                    attributeMapper -> {
                                      try {
                                        Object value = attributeMapper.map(resultSet);

                                        if (value != null) {
                                          attributes.addFirst(
                                              attributeMapper.getName(), value.toString());
                                        }
                                      } catch (SQLException cause) {
                                        throw log.ldapRealmFailedObtainAttributes(this.name, cause);
                                      }
                                    });
                          } while (resultSet.next());

                          return attributes;
                        }

                        return null;
                      });
                })
            .collect(
                Collectors.reducing(
                    (lAttribute, rAttribute) -> {
                      MapAttributes attributes = new MapAttributes(lAttribute);

                      for (Attributes.Entry rEntry : rAttribute.entries()) {
                        attributes.get(rEntry.getKey()).addAll(rEntry);
                      }

                      return attributes;
                    }))
            .ifPresent(attributes -> this.identity = new JdbcIdentity(attributes));
      }

      return this.identity;
    }
Example #2
0
  public static void main(String[] args) {
    final List<Person> people =
        Arrays.asList(
            new Person("Robert", 49),
            new Person("John", 20),
            new Person("Sara", 21),
            new Person("Chloe", 18),
            new Person("Jane", 21),
            new Person("Greg", 35));

    List<Person> olderThan20_a = new ArrayList<>();
    people.stream().filter(person -> person.age > 20).forEach(person -> olderThan20_a.add(person));
    printPeople("don't trust olderThan20_a", olderThan20_a);

    List<Person> olderThan20_b =
        people
            .stream()
            .filter(person -> person.age > 20)
            .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    printPeople("don't trust olderThan20_b", olderThan20_b);

    List<Person> olderThan20_c =
        people.stream().filter(person -> person.age > 20).collect(Collectors.toList());
    printPeople("don't trust olderThan20_c", olderThan20_c);

    Map<Integer, List<Person>> peopleByAge =
        people.stream().collect(Collectors.groupingBy(person -> person.age));
    System.out.println("Grouped by age: " + peopleByAge);

    Map<Integer, List<String>> nameOfPeopleByAge =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    person -> person.age,
                    Collectors.mapping(person -> person.name, Collectors.toList())));
    System.out.println("People grouped by age: " + nameOfPeopleByAge);

    // Group the names by their first char and then get the oldest person in
    // each group
    Comparator<Person> byAge = Comparator.comparing(person -> person.age);
    Map<Character, Optional<Person>> oldestPersonOfEachLetter =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    person -> person.name.charAt(0),
                    Collectors.reducing(BinaryOperator.maxBy(byAge))));
    System.out.println("Oldest person of each letter:");
    System.out.println(oldestPersonOfEachLetter);
  }
Example #3
0
  public static void main(String[] args) {
    final List<Person> people =
        Arrays.asList(
            new Person("John", 20),
            new Person("Sara", 21),
            new Person("Jane", 21),
            new Person("Greg", 35));

    List<Person> olderThan20 =
        people
            .stream()
            .filter(person -> person.getAge() > 20)
            .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    System.out.println("People older than 20: " + olderThan20);

    List<Person> olderThan20_1 =
        people.stream().filter(person -> person.getAge() > 20).collect(Collectors.toList());
    System.out.println("People older than 20: " + olderThan20_1);

    Map<Integer, List<Person>> peopleByAge =
        people.stream().collect(Collectors.groupingBy(Person::getAge));
    System.out.println("Grouped by age: " + peopleByAge);

    Map<Integer, List<String>> nameOfPeopleByAge =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    Person::getAge, Collectors.mapping(Person::getName, Collectors.toList())));
    System.out.println("People name grouped by age: " + nameOfPeopleByAge);

    Comparator<Person> byAge = Comparator.comparing(Person::getAge);
    Map<Character, Optional<Person>> oldestPersonOfEachCharacter =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    person -> person.getName().charAt(0),
                    Collectors.reducing(BinaryOperator.maxBy(byAge))));
    System.out.println("Oldest person of each letter: " + oldestPersonOfEachCharacter);
  }
  /**
   * 聚集计算
   *
   * @param dataList 待计算的数据
   * @param query 原始查询请求
   * @return LinkedList<ResultRecord> 计算后的数据
   */
  public static List<SearchIndexResultRecord> aggregate(
      List<SearchIndexResultRecord> dataList, int dimSize, List<QueryMeasure> queryMeasures) {

    if (CollectionUtils.isEmpty(queryMeasures) || CollectionUtils.isEmpty(dataList)) {
      LOGGER.info("no need to group.");
      return dataList;
    }

    Set<Integer> countIndex = Sets.newHashSet();
    for (int i = 0; i < queryMeasures.size(); i++) {
      if (queryMeasures.get(i).getAggregator().equals(Aggregator.DISTINCT_COUNT)) {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.info(queryMeasures.get(i) + " ============= begin print values ===== ====");
          final int tmp = i;
          dataList.forEach(rs -> LOGGER.info(rs.getField(tmp) + ""));
          LOGGER.info(" ============= end print measure values ==============");
        }
        countIndex.add(i);
      }
    }

    int arraySize = dataList.get(0).getFieldArraySize();

    long current = System.currentTimeMillis();
    Stream<SearchIndexResultRecord> stream =
        dataList.size() > 300000 ? dataList.parallelStream() : dataList.stream();

    int defaultSize = (int) (dataList.size() > 100 ? dataList.size() * 0.01 : dataList.size());

    final BinaryOperator<SearchIndexResultRecord> reduceOperation =
        (x, y) -> {
          if (!y.getGroupBy().equals(x.getGroupBy())) {
            x = SearchIndexResultRecord.of(arraySize);
            x.setGroupBy(y.getGroupBy());
            for (int i = 0; i < dimSize; i++) {
              x.setField(i, y.getField(i));
            }
          }
          try {
            int index = dimSize;
            for (int i = 0; i < queryMeasures.size(); i++) {
              QueryMeasure measure = queryMeasures.get(i);
              index = i + dimSize;
              if (measure.getAggregator().equals(Aggregator.DISTINCT_COUNT)) {
                if (!x.getDistinctMeasures().containsKey(i)) {
                  x.getDistinctMeasures().put(i, new HashSet<>(defaultSize));
                }

                if (y.getDistinctMeasures().containsKey(i)) {
                  x.getDistinctMeasures().get(i).addAll(y.getDistinctMeasures().get(i));
                } else if (y.getField(index) != null) {
                  x.getDistinctMeasures().get(i).add(y.getField(index));
                }

              } else {
                x.setField(
                    index, measure.getAggregator().aggregate(x.getField(index), y.getField(index)));
              }
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
          return x;
        };
    final Collector<SearchIndexResultRecord, ?, SearchIndexResultRecord> reducing =
        Collectors.reducing(SearchIndexResultRecord.of(arraySize), reduceOperation);
    Map<String, SearchIndexResultRecord> groupResult =
        stream.collect(
            Collectors.groupingByConcurrent(SearchIndexResultRecord::getGroupBy, reducing));

    if (CollectionUtils.isNotEmpty(countIndex)) {
      groupResult
          .values()
          .forEach(
              record -> {
                for (int index : countIndex) {
                  if (record.getDistinctMeasures() != null
                      && record.getDistinctMeasures().containsKey(index)) {
                    record.setField(
                        dimSize + index, record.getDistinctMeasures().get(index).size());
                  }
                }
              });
    }
    LOGGER.info(
        "group agg(sum) cost: {}ms, size:{}!",
        (System.currentTimeMillis() - current),
        groupResult.size());
    return new ArrayList<>(groupResult.values());
  }