use of com.google.gerrit.index.project.ProjectIndex in project gerrit by GerritCodeReview.
the class AbstractDaemonTest method disableProjectIndex.
protected AutoCloseable disableProjectIndex() {
disableProjectIndexWrites();
ProjectIndex maybeDisabledSearchIndex = projectIndexes.getSearchIndex();
if (!(maybeDisabledSearchIndex instanceof DisabledProjectIndex)) {
projectIndexes.setSearchIndex(new DisabledProjectIndex(maybeDisabledSearchIndex), false);
}
return () -> {
enableProjectIndexWrites();
ProjectIndex maybeEnabledSearchIndex = projectIndexes.getSearchIndex();
if (maybeEnabledSearchIndex instanceof DisabledProjectIndex) {
projectIndexes.setSearchIndex(((DisabledProjectIndex) maybeEnabledSearchIndex).unwrap(), false);
}
};
}
use of com.google.gerrit.index.project.ProjectIndex in project gerrit by GerritCodeReview.
the class QueryProjects method apply.
public List<ProjectInfo> apply() throws BadRequestException, MethodNotAllowedException {
if (Strings.isNullOrEmpty(query)) {
throw new BadRequestException("missing query field");
}
ProjectIndex searchIndex = indexes.getSearchIndex();
if (searchIndex == null) {
throw new MethodNotAllowedException("no project index");
}
ProjectQueryProcessor queryProcessor = queryProcessorProvider.get();
if (start != 0) {
queryProcessor.setStart(start);
}
if (limit != 0) {
queryProcessor.setUserProvidedLimit(limit);
}
try {
QueryResult<ProjectData> result = queryProcessor.query(queryBuilder.parse(query));
List<ProjectData> pds = result.entities();
ArrayList<ProjectInfo> projectInfos = Lists.newArrayListWithCapacity(pds.size());
for (ProjectData pd : pds) {
projectInfos.add(json.format(pd.getProject()));
}
return projectInfos;
} catch (QueryParseException e) {
throw new BadRequestException(e.getMessage());
}
}
use of com.google.gerrit.index.project.ProjectIndex in project gerrit by GerritCodeReview.
the class ProjectIndexerImpl method index.
@Override
public void index(Project.NameKey nameKey) {
Optional<ProjectState> projectState = projectCache.get(nameKey);
if (projectState.isPresent()) {
logger.atFine().log("Replace project %s in index", nameKey.get());
ProjectData projectData = projectState.get().toProjectData();
for (ProjectIndex i : getWriteIndexes()) {
try (TraceTimer traceTimer = TraceContext.newTimer("Replacing project", Metadata.builder().projectName(nameKey.get()).indexVersion(i.getSchema().getVersion()).build())) {
i.replace(projectData);
} catch (RuntimeException e) {
throw new StorageException(String.format("Failed to replace project %s in index version %d", nameKey.get(), i.getSchema().getVersion()), e);
}
}
fireProjectIndexedEvent(nameKey.get());
} else {
logger.atFine().log("Delete project %s from index", nameKey.get());
for (ProjectIndex i : getWriteIndexes()) {
try (TraceTimer traceTimer = TraceContext.newTimer("Deleting project", Metadata.builder().projectName(nameKey.get()).indexVersion(i.getSchema().getVersion()).build())) {
i.delete(nameKey);
} catch (RuntimeException e) {
throw new StorageException(String.format("Failed to delete project %s from index version %d", nameKey.get(), i.getSchema().getVersion()), e);
}
}
}
}
use of com.google.gerrit.index.project.ProjectIndex 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.project.ProjectIndex 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)));
}
Aggregations