use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class AbstractFakeIndex method getSource.
@Override
public DataSource<V> getSource(Predicate<V> p, QueryOptions opts) {
List<V> results;
synchronized (indexedDocuments) {
results = indexedDocuments.values().stream().map(doc -> valueFor(doc)).filter(doc -> p.asMatchable().match(doc)).sorted(sortingComparator()).skip(opts.start()).limit(opts.limit()).collect(toImmutableList());
}
return new DataSource<>() {
@Override
public int getCardinality() {
return results.size();
}
@Override
public ResultSet<V> read() {
return new ListResultSet<>(results);
}
@Override
public ResultSet<FieldBundle> readRaw() {
ImmutableList.Builder<FieldBundle> fieldBundles = ImmutableList.builder();
for (V result : results) {
ImmutableListMultimap.Builder<String, Object> fields = ImmutableListMultimap.builder();
for (FieldDef<V, ?> field : getSchema().getFields().values()) {
if (field.get(result) == null) {
continue;
}
if (field.isRepeatable()) {
fields.putAll(field.getName(), (Iterable<?>) field.get(result));
} else {
fields.put(field.getName(), field.get(result));
}
}
fieldBundles.add(new FieldBundle(fields.build()));
}
return new ListResultSet<>(fieldBundles.build());
}
};
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class StalenessChecker method check.
public StalenessCheckResult check(Account.Id id) throws IOException {
AccountIndex i = indexes.getSearchIndex();
if (i == null) {
// No index; caller couldn't do anything if it is stale.
return StalenessCheckResult.notStale();
}
if (!i.getSchema().hasField(AccountField.REF_STATE) || !i.getSchema().hasField(AccountField.EXTERNAL_ID_STATE)) {
// Index version not new enough for this check.
return StalenessCheckResult.notStale();
}
boolean useLegacyNumericFields = i.getSchema().useLegacyNumericFields();
ImmutableSet<String> fields = useLegacyNumericFields ? FIELDS : FIELDS2;
Optional<FieldBundle> result = i.getRaw(id, QueryOptions.create(indexConfig, 0, 1, IndexUtils.accountFields(fields, useLegacyNumericFields)));
if (!result.isPresent()) {
// The document is missing in the index.
try (Repository repo = repoManager.openRepository(allUsersName)) {
Ref ref = repo.exactRef(RefNames.refsUsers(id));
// Stale if the account actually exists.
if (ref == null) {
return StalenessCheckResult.notStale();
}
return StalenessCheckResult.stale("Document missing in index, but found %s in the repo", ref);
}
}
for (Map.Entry<Project.NameKey, RefState> e : RefState.parseStates(result.get().getValue(AccountField.REF_STATE)).entries()) {
// Custom All-Users repository names are not indexed. Instead, the default name is used.
// Therefore, defer to the currently configured All-Users name.
Project.NameKey repoName = e.getKey().get().equals(AllUsersNameProvider.DEFAULT) ? allUsersName : e.getKey();
try (Repository repo = repoManager.openRepository(repoName)) {
if (!e.getValue().match(repo)) {
return StalenessCheckResult.stale("Ref was modified since the account was indexed (%s != %s)", e.getValue(), repo.exactRef(e.getValue().ref()));
}
}
}
Set<ExternalId> extIds = externalIds.byAccount(id);
ListMultimap<ObjectId, ObjectId> extIdStates = parseExternalIdStates(result.get().getValue(AccountField.EXTERNAL_ID_STATE));
if (extIdStates.size() != extIds.size()) {
return StalenessCheckResult.stale("External IDs of the account were modified since the account was indexed. (%s != %s)", extIdStates.size(), extIds.size());
}
for (ExternalId extId : extIds) {
if (!extIdStates.containsKey(extId.key().sha1())) {
return StalenessCheckResult.stale("External ID missing: %s", extId.key().sha1());
}
if (!extIdStates.containsEntry(extId.key().sha1(), extId.blobId())) {
return StalenessCheckResult.stale("External ID has unexpected value. (%s != %s)", extIdStates.get(extId.key().sha1()), extId.blobId());
}
}
return StalenessCheckResult.notStale();
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class StalenessChecker method check.
public StalenessCheckResult check(AccountGroup.UUID uuid) throws IOException {
GroupIndex i = indexes.getSearchIndex();
if (i == null) {
// No index; caller couldn't do anything if it is stale.
return StalenessCheckResult.notStale();
}
Optional<FieldBundle> result = i.getRaw(uuid, IndexedGroupQuery.createOptions(indexConfig, 0, 1, FIELDS));
if (!result.isPresent()) {
// The document is missing in the index.
try (Repository repo = repoManager.openRepository(allUsers)) {
Ref ref = repo.exactRef(RefNames.refsGroups(uuid));
// Stale if the group actually exists.
if (ref == null) {
return StalenessCheckResult.notStale();
}
return StalenessCheckResult.stale("Document missing in index, but found %s in the repo", ref);
}
}
try (Repository repo = repoManager.openRepository(allUsers)) {
Ref ref = repo.exactRef(RefNames.refsGroups(uuid));
ObjectId head = ref == null ? ObjectId.zeroId() : ref.getObjectId();
ObjectId idFromIndex = ObjectId.fromString(result.get().getValue(GroupField.REF_STATE), 0);
if (head.equals(idFromIndex)) {
return StalenessCheckResult.notStale();
}
return StalenessCheckResult.stale("Document has unexpected ref state (%s != %s)", head, idFromIndex);
}
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class AbstractQueryAccountsTest method rawDocument.
@Test
public void rawDocument() throws Exception {
AccountInfo userInfo = gApi.accounts().id(admin.getAccountId().get()).get();
Schema<AccountState> schema = indexes.getSearchIndex().getSchema();
Optional<FieldBundle> rawFields = indexes.getSearchIndex().getRaw(Account.id(userInfo._accountId), QueryOptions.create(IndexConfig.createDefault(), 0, 1, schema.getStoredFields().keySet()));
assertThat(rawFields).isPresent();
if (schema.useLegacyNumericFields()) {
assertThat(rawFields.get().getValue(AccountField.ID)).isEqualTo(userInfo._accountId);
} else {
assertThat(Integer.valueOf(rawFields.get().getValue(AccountField.ID_STR))).isEqualTo(userInfo._accountId);
}
// The field EXTERNAL_ID_STATE is only supported from schema version 6.
if (getSchemaVersion() < 6) {
return;
}
List<AccountExternalIdInfo> externalIdInfos = gApi.accounts().self().getExternalIds();
List<ByteArrayWrapper> blobs = new ArrayList<>();
for (AccountExternalIdInfo info : externalIdInfos) {
Optional<ExternalId> extId = externalIds.get(externalIdKeyFactory.parse(info.identity));
assertThat(extId).isPresent();
blobs.add(new ByteArrayWrapper(extId.get().toByteArray()));
}
assertThat(rawFields.get().getValue(AccountField.EXTERNAL_ID_STATE)).hasSize(blobs.size());
assertThat(Streams.stream(rawFields.get().getValue(AccountField.EXTERNAL_ID_STATE)).map(ByteArrayWrapper::new).collect(toList())).containsExactlyElementsIn(blobs);
}
use of com.google.gerrit.index.query.FieldBundle in project gerrit by GerritCodeReview.
the class AbstractQueryGroupsTest method rawDocument.
@Test
public void rawDocument() throws Exception {
GroupInfo group1 = createGroup(name("group1"));
AccountGroup.UUID uuid = AccountGroup.uuid(group1.id);
Optional<FieldBundle> rawFields = indexes.getSearchIndex().getRaw(uuid, QueryOptions.create(IndexConfig.createDefault(), 0, 10, indexes.getSearchIndex().getSchema().getStoredFields().keySet()));
assertThat(rawFields).isPresent();
assertThat(rawFields.get().getValue(GroupField.UUID)).isEqualTo(uuid.get());
}
Aggregations