Line data Source code
1 : // Copyright (C) 2010 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.query.change; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.common.collect.ImmutableList; 20 : import com.google.gerrit.entities.Change; 21 : import com.google.gerrit.index.query.FieldBundle; 22 : import com.google.gerrit.index.query.LazyResultSet; 23 : import com.google.gerrit.index.query.OrPredicate; 24 : import com.google.gerrit.index.query.Predicate; 25 : import com.google.gerrit.index.query.ResultSet; 26 : import java.util.Collection; 27 : import java.util.HashSet; 28 : import java.util.List; 29 : import java.util.Optional; 30 : import java.util.Set; 31 : 32 : public class OrSource extends OrPredicate<ChangeData> implements ChangeDataSource { 33 1 : private int cardinality = -1; 34 : 35 : public OrSource(Collection<? extends Predicate<ChangeData>> that) { 36 1 : super(that); 37 1 : Optional<Predicate<ChangeData>> nonChangeDataSource = 38 1 : getChildren().stream().filter(p -> !(p instanceof ChangeDataSource)).findAny(); 39 1 : if (nonChangeDataSource.isPresent()) { 40 1 : throw new IllegalArgumentException("No ChangeDataSource: " + nonChangeDataSource.get()); 41 : } 42 1 : } 43 : 44 : @Override 45 : public ResultSet<ChangeData> read() { 46 : // ResultSets are lazy. Calling #read here first and then dealing with ResultSets only when 47 : // requested allows the index to run asynchronous queries. 48 1 : List<ResultSet<ChangeData>> results = 49 1 : getChildren().stream().map(p -> ((ChangeDataSource) p).read()).collect(toImmutableList()); 50 1 : return new LazyResultSet<>( 51 : () -> { 52 1 : ImmutableList.Builder<ChangeData> r = ImmutableList.builder(); 53 1 : Set<Change.Id> have = new HashSet<>(); 54 1 : for (ResultSet<ChangeData> resultSet : results) { 55 0 : for (ChangeData result : resultSet) { 56 0 : if (have.add(result.getId())) { 57 0 : r.add(result); 58 : } 59 0 : } 60 0 : } 61 0 : return r.build(); 62 : }); 63 : } 64 : 65 : @Override 66 : public ResultSet<FieldBundle> readRaw() { 67 0 : throw new UnsupportedOperationException("not implemented"); 68 : } 69 : 70 : @Override 71 : public boolean hasChange() { 72 0 : for (Predicate<ChangeData> p : getChildren()) { 73 0 : if (!(p instanceof ChangeDataSource) || !((ChangeDataSource) p).hasChange()) { 74 0 : return false; 75 : } 76 0 : } 77 0 : return true; 78 : } 79 : 80 : @Override 81 : public int getCardinality() { 82 0 : if (cardinality < 0) { 83 0 : cardinality = 0; 84 0 : for (Predicate<ChangeData> p : getChildren()) { 85 0 : if (p instanceof ChangeDataSource) { 86 0 : cardinality += ((ChangeDataSource) p).getCardinality(); 87 : } 88 0 : } 89 : } 90 0 : return cardinality; 91 : } 92 : }