use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class StalenessChecker method check.
/**
* Returns a {@link StalenessCheckResult} with structured information about staleness of the
* provided {@link com.google.gerrit.entities.Project.NameKey}.
*/
public StalenessCheckResult check(Project.NameKey project) {
ProjectData projectData = projectCache.get(project).orElseThrow(illegalState(project)).toProjectData();
ProjectIndex i = indexes.getSearchIndex();
if (i == null) {
return StalenessCheckResult.notStale();
}
Optional<FieldBundle> result = i.getRaw(project, QueryOptions.create(indexConfig, 0, 1, FIELDS));
if (!result.isPresent()) {
return StalenessCheckResult.stale("Document %s missing from index", project);
}
SetMultimap<Project.NameKey, RefState> indexedRefStates = RefState.parseStates(result.get().getValue(ProjectField.REF_STATE));
SetMultimap<Project.NameKey, RefState> currentRefStates = MultimapBuilder.hashKeys().hashSetValues().build();
projectData.tree().stream().filter(p -> p.getProject().getConfigRefState() != null).forEach(p -> currentRefStates.put(p.getProject().getNameKey(), RefState.create(RefNames.REFS_CONFIG, p.getProject().getConfigRefState())));
if (currentRefStates.equals(indexedRefStates)) {
return StalenessCheckResult.notStale();
}
return StalenessCheckResult.stale("Document has unexpected ref states (%s != %s)", currentRefStates, indexedRefStates);
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class ReviewersUtil method suggestAccounts.
private List<Account.Id> suggestAccounts(SuggestReviewers suggestReviewers) throws BadRequestException {
try (Timer0.Context ctx = metrics.queryAccountsLatency.start()) {
// For performance reasons we don't use AccountQueryProvider as it would always load the
// complete account from the cache (or worse, from NoteDb) even though we only need the ID
// which we can directly get from the returned results.
Predicate<AccountState> pred = Predicate.and(AccountPredicates.isActive(), accountQueryBuilder.defaultQuery(suggestReviewers.getQuery()));
logger.atFine().log("accounts index query: %s", pred);
accountIndexRewriter.validateMaxTermsInQuery(pred);
boolean useLegacyNumericFields = accountIndexes.getSearchIndex().getSchema().useLegacyNumericFields();
FieldDef<AccountState, ?> idField = useLegacyNumericFields ? AccountField.ID : AccountField.ID_STR;
ResultSet<FieldBundle> result = accountIndexes.getSearchIndex().getSource(pred, QueryOptions.create(indexConfig, 0, suggestReviewers.getLimit(), ImmutableSet.of(idField.getName()))).readRaw();
List<Account.Id> matches = result.toList().stream().map(f -> fromIdField(f, useLegacyNumericFields)).collect(toList());
logger.atFine().log("Matches: %s", matches);
return matches;
} catch (TooManyTermsInQueryException e) {
throw new BadRequestException(e.getMessage());
} catch (QueryParseException e) {
logger.atWarning().withCause(e).log("Suggesting accounts failed, return empty result.");
return ImmutableList.of();
} catch (StorageException e) {
if (e.getCause() instanceof TooManyTermsInQueryException) {
throw new BadRequestException(e.getMessage());
}
if (e.getCause() instanceof QueryParseException) {
return ImmutableList.of();
}
throw e;
}
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class Index method getRaw.
/**
* Get a single raw document from the index.
*
* @param key document key.
* @param opts query options. Options that do not make sense in the context of a single document,
* such as start, will be ignored.
* @return an abstraction of a raw index document to retrieve fields from.
*/
default Optional<FieldBundle> getRaw(K key, QueryOptions opts) {
opts = opts.withStart(0).withLimit(2);
ImmutableList<FieldBundle> results;
try {
results = getSource(keyPredicate(key), opts).readRaw().toList();
} catch (QueryParseException e) {
throw new StorageException("Unexpected QueryParseException during get()", e);
}
if (results.size() > 1) {
throw new StorageException("Multiple results found in index for key " + key + ": " + results);
}
return results.stream().findFirst();
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class ProjectIndexerIT method indexProject_indexesRefStateOfProjectAndParents.
@Test
public void indexProject_indexesRefStateOfProjectAndParents() throws Exception {
projectIndexer.index(project);
ProjectIndex i = indexes.getSearchIndex();
assertThat(i.getSchema().hasField(ProjectField.REF_STATE)).isTrue();
Optional<FieldBundle> result = i.getRaw(project, QueryOptions.create(indexConfig, 0, 1, FIELDS));
assertThat(result).isPresent();
Iterable<byte[]> refState = result.get().getValue(ProjectField.REF_STATE);
assertThat(refState).isNotEmpty();
Map<Project.NameKey, Collection<RefState>> states = RefState.parseStates(refState).asMap();
fetch(testRepo, "refs/meta/config:refs/meta/config");
Ref projectConfigRef = testRepo.getRepository().exactRef("refs/meta/config");
TestRepository<InMemoryRepository> allProjectsRepo = cloneProject(allProjects, admin);
fetch(allProjectsRepo, "refs/meta/config:refs/meta/config");
Ref allProjectConfigRef = allProjectsRepo.getRepository().exactRef("refs/meta/config");
assertThat(states).containsExactly(project, ImmutableSet.of(RefState.of(projectConfigRef)), allProjects, ImmutableSet.of(RefState.of(allProjectConfigRef)));
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class AbstractLuceneIndex method toFieldBundle.
protected FieldBundle toFieldBundle(Document doc) {
Map<String, FieldDef<V, ?>> allFields = getSchema().getFields();
ListMultimap<String, Object> rawFields = ArrayListMultimap.create();
for (IndexableField field : doc.getFields()) {
checkArgument(allFields.containsKey(field.name()), "Unrecognized field " + field.name());
FieldType<?> type = allFields.get(field.name()).getType();
if (type == FieldType.EXACT || type == FieldType.FULL_TEXT || type == FieldType.PREFIX) {
rawFields.put(field.name(), field.stringValue());
} else if (type == FieldType.INTEGER || type == FieldType.INTEGER_RANGE) {
rawFields.put(field.name(), field.numericValue().intValue());
} else if (type == FieldType.LONG) {
rawFields.put(field.name(), field.numericValue().longValue());
} else if (type == FieldType.TIMESTAMP) {
rawFields.put(field.name(), new Timestamp(field.numericValue().longValue()));
} else if (type == FieldType.STORED_ONLY) {
rawFields.put(field.name(), field.binaryValue().bytes);
} else {
throw FieldType.badFieldType(type);
}
}
return new FieldBundle(rawFields);
}
Aggregations