Search in sources :

Example 1 with ProjectIndex

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);
        }
    };
}
Also used : ProjectIndex(com.google.gerrit.index.project.ProjectIndex)

Example 2 with ProjectIndex

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());
    }
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ProjectQueryProcessor(com.google.gerrit.server.query.project.ProjectQueryProcessor) ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ProjectData(com.google.gerrit.index.project.ProjectData) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 3 with ProjectIndex

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);
            }
        }
    }
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ProjectState(com.google.gerrit.server.project.ProjectState) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) StorageException(com.google.gerrit.exceptions.StorageException) ProjectData(com.google.gerrit.index.project.ProjectData)

Example 4 with ProjectIndex

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);
}
Also used : RefState(com.google.gerrit.index.RefState) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableSet(com.google.common.collect.ImmutableSet) IndexConfig(com.google.gerrit.index.IndexConfig) ProjectCache(com.google.gerrit.server.project.ProjectCache) MultimapBuilder(com.google.common.collect.MultimapBuilder) ProjectField(com.google.gerrit.index.project.ProjectField) Inject(com.google.inject.Inject) QueryOptions(com.google.gerrit.index.QueryOptions) ProjectData(com.google.gerrit.index.project.ProjectData) SetMultimap(com.google.common.collect.SetMultimap) StalenessCheckResult(com.google.gerrit.server.index.StalenessCheckResult) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ProjectIndexCollection(com.google.gerrit.index.project.ProjectIndexCollection) Project(com.google.gerrit.entities.Project) RefNames(com.google.gerrit.entities.RefNames) FieldBundle(com.google.gerrit.index.query.FieldBundle) Optional(java.util.Optional) RefState(com.google.gerrit.index.RefState) FieldBundle(com.google.gerrit.index.query.FieldBundle) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ProjectData(com.google.gerrit.index.project.ProjectData)

Example 5 with ProjectIndex

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)));
}
Also used : Ref(org.eclipse.jgit.lib.Ref) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) FieldBundle(com.google.gerrit.index.query.FieldBundle) Collection(java.util.Collection) ProjectIndexCollection(com.google.gerrit.index.project.ProjectIndexCollection) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

ProjectIndex (com.google.gerrit.index.project.ProjectIndex)5 ProjectData (com.google.gerrit.index.project.ProjectData)3 ProjectIndexCollection (com.google.gerrit.index.project.ProjectIndexCollection)2 FieldBundle (com.google.gerrit.index.query.FieldBundle)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1 SetMultimap (com.google.common.collect.SetMultimap)1 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 Project (com.google.gerrit.entities.Project)1 RefNames (com.google.gerrit.entities.RefNames)1 StorageException (com.google.gerrit.exceptions.StorageException)1 ProjectInfo (com.google.gerrit.extensions.common.ProjectInfo)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 IndexConfig (com.google.gerrit.index.IndexConfig)1 QueryOptions (com.google.gerrit.index.QueryOptions)1 RefState (com.google.gerrit.index.RefState)1 ProjectField (com.google.gerrit.index.project.ProjectField)1 QueryParseException (com.google.gerrit.index.query.QueryParseException)1 StalenessCheckResult (com.google.gerrit.server.index.StalenessCheckResult)1