Search in sources :

Example 6 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class StarredChangesUtil method star.

public NavigableSet<String> star(Account.Id accountId, Project.NameKey project, Change.Id changeId, Set<String> labelsToAdd, Set<String> labelsToRemove) throws IllegalLabelException {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        String refName = RefNames.refsStarredChanges(changeId, accountId);
        StarRef old = readLabels(repo, refName);
        NavigableSet<String> labels = new TreeSet<>(old.labels());
        if (labelsToAdd != null) {
            labels.addAll(labelsToAdd);
        }
        if (labelsToRemove != null) {
            labels.removeAll(labelsToRemove);
        }
        if (labels.isEmpty()) {
            deleteRef(repo, refName, old.objectId());
        } else {
            checkMutuallyExclusiveLabels(labels);
            updateLabels(repo, refName, old.objectId(), labels);
        }
        indexer.index(project, changeId);
        return Collections.unmodifiableNavigableSet(labels);
    } catch (IOException e) {
        throw new StorageException(String.format("Star change %d for account %d failed", changeId.get(), accountId.get()), e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) TreeSet(java.util.TreeSet) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 7 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class StarredChangesUtil method byChange.

public ImmutableMap<Account.Id, StarRef> byChange(Change.Id changeId) {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        ImmutableMap.Builder<Account.Id, StarRef> builder = ImmutableMap.builder();
        for (String refPart : getRefNames(repo, RefNames.refsStarredChangesPrefix(changeId))) {
            Integer id = Ints.tryParse(refPart);
            if (id == null) {
                continue;
            }
            Account.Id accountId = Account.id(id);
            builder.put(accountId, readLabels(repo, RefNames.refsStarredChanges(changeId, accountId)));
        }
        return builder.build();
    } catch (IOException e) {
        throw new StorageException(String.format("Get accounts that starred change %d failed", changeId.get()), e);
    }
}
Also used : Account(com.google.gerrit.entities.Account) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class StarredChangesUtil method byAccountId.

public ImmutableSet<Change.Id> byAccountId(Account.Id accountId, String label) {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        ImmutableSet.Builder<Change.Id> builder = ImmutableSet.builder();
        for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_STARRED_CHANGES)) {
            Account.Id currentAccountId = Account.Id.fromRef(ref.getName());
            // Skip all refs that don't correspond with accountId.
            if (currentAccountId == null || !currentAccountId.equals(accountId)) {
                continue;
            }
            // Skip all refs that don't contain the required label.
            StarRef starRef = readLabels(repo, ref.getName());
            if (!starRef.labels().contains(label)) {
                continue;
            }
            // Skip invalid change ids.
            Change.Id changeId = Change.Id.fromAllUsersRef(ref.getName());
            if (changeId == null) {
                continue;
            }
            builder.add(changeId);
        }
        return builder.build();
    } catch (IOException e) {
        throw new StorageException(String.format("Get starred changes for account %d failed", accountId.get()), e);
    }
}
Also used : Account(com.google.gerrit.entities.Account) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) IOException(java.io.IOException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 9 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class QueryProcessor method query.

private List<QueryResult<T>> query(@Nullable List<String> queryStrings, List<Predicate<T>> queries) throws QueryParseException {
    long startNanos = System.nanoTime();
    checkState(!used.getAndSet(true), "%s has already been used", getClass().getSimpleName());
    int cnt = queries.size();
    if (queryStrings != null) {
        int qs = queryStrings.size();
        checkArgument(qs == cnt, "got %s query strings but %s predicates", qs, cnt);
    }
    if (cnt == 0) {
        return ImmutableList.of();
    }
    if (isDisabled()) {
        return disabledResults(queryStrings, queries);
    }
    logger.atFine().log("Executing %d %s index queries for %s", cnt, schemaDef.getName(), callerFinder.findCallerLazy());
    List<QueryResult<T>> out;
    try {
        // Parse and rewrite all queries.
        List<Integer> limits = new ArrayList<>(cnt);
        List<Predicate<T>> predicates = new ArrayList<>(cnt);
        List<DataSource<T>> sources = new ArrayList<>(cnt);
        int queryCount = 0;
        for (Predicate<T> q : queries) {
            checkSupportedForQueries(q);
            int limit = getEffectiveLimit(q);
            limits.add(limit);
            if (limit == getBackendSupportedLimit()) {
                limit--;
            }
            int page = (start / limit) + 1;
            if (page > indexConfig.maxPages()) {
                throw new QueryParseException("Cannot go beyond page " + indexConfig.maxPages() + " of results");
            }
            // Always bump limit by 1, even if this results in exceeding the permitted
            // max for this user. The only way to see if there are more entities is to
            // ask for one more result from the query.
            QueryOptions opts = createOptions(indexConfig, start, limit + 1, getRequestedFields());
            logger.atFine().log("Query options: %s", opts);
            Predicate<T> pred = rewriter.rewrite(q, opts);
            if (enforceVisibility) {
                pred = enforceVisibility(pred);
            }
            predicates.add(pred);
            logger.atFine().log("%s index query[%d]:\n%s", schemaDef.getName(), queryCount++, pred instanceof IndexedQuery ? pred.getChild(0) : pred);
            @SuppressWarnings("unchecked") DataSource<T> s = (DataSource<T>) pred;
            sources.add(s);
        }
        // Run each query asynchronously, if supported.
        List<ResultSet<T>> matches = new ArrayList<>(cnt);
        for (DataSource<T> s : sources) {
            matches.add(s.read());
        }
        out = new ArrayList<>(cnt);
        for (int i = 0; i < cnt; i++) {
            ImmutableList<T> matchesList = matches.get(i).toList();
            logger.atFine().log("Matches[%d]:\n%s", i, lazy(() -> matchesList.stream().map(this::formatForLogging).collect(toList())));
            out.add(QueryResult.create(queryStrings != null ? queryStrings.get(i) : null, predicates.get(i), limits.get(i), matchesList));
        }
        // Only measure successful queries that actually touched the index.
        metrics.executionTime.record(schemaDef.getName(), System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
    } catch (StorageException e) {
        Optional<QueryParseException> qpe = findQueryParseException(e);
        if (qpe.isPresent()) {
            throw new QueryParseException(qpe.get().getMessage(), e);
        }
        throw e;
    }
    return out;
}
Also used : Optional(java.util.Optional) ArrayList(java.util.ArrayList) QueryOptions(com.google.gerrit.index.QueryOptions) StorageException(com.google.gerrit.exceptions.StorageException)

Example 10 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class LuceneChangeIndex method replace.

@Override
public void replace(ChangeData cd) {
    Term id = LuceneChangeIndex.idTerm(idTerm, idField, cd);
    // toDocument is essentially static and doesn't depend on the specific
    // sub-index, so just pick one.
    Document doc = openIndex.toDocument(cd);
    try {
        if (cd.change().isNew()) {
            Futures.allAsList(closedIndex.delete(id), openIndex.replace(id, doc)).get();
        } else {
            Futures.allAsList(openIndex.delete(id), closedIndex.replace(id, doc)).get();
        }
    } catch (ExecutionException | InterruptedException e) {
        throw new StorageException(e);
    }
}
Also used : Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) ExecutionException(java.util.concurrent.ExecutionException) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

StorageException (com.google.gerrit.exceptions.StorageException)153 IOException (java.io.IOException)68 Change (com.google.gerrit.entities.Change)47 ObjectId (org.eclipse.jgit.lib.ObjectId)37 Repository (org.eclipse.jgit.lib.Repository)33 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)30 PatchSet (com.google.gerrit.entities.PatchSet)29 RevCommit (org.eclipse.jgit.revwalk.RevCommit)28 ArrayList (java.util.ArrayList)25 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)24 Project (com.google.gerrit.entities.Project)22 Ref (org.eclipse.jgit.lib.Ref)22 ChangeData (com.google.gerrit.server.query.change.ChangeData)21 RevWalk (org.eclipse.jgit.revwalk.RevWalk)21 Account (com.google.gerrit.entities.Account)20 Inject (com.google.inject.Inject)19 Map (java.util.Map)19 Test (org.junit.Test)19 List (java.util.List)18 BranchNameKey (com.google.gerrit.entities.BranchNameKey)17