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);
}
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations