Search in sources :

Example 1 with FieldBundle

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());
        }
    };
}
Also used : InternalGroup(com.google.gerrit.entities.InternalGroup) MergeabilityComputationBehavior(com.google.gerrit.server.change.MergeabilityComputationBehavior) IndexUtils(com.google.gerrit.server.index.IndexUtils) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) ProjectData(com.google.gerrit.index.project.ProjectData) ResultSet(com.google.gerrit.index.query.ResultSet) Assisted(com.google.inject.assistedinject.Assisted) Config(org.eclipse.jgit.lib.Config) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ListResultSet(com.google.gerrit.index.query.ListResultSet) ImmutableList(com.google.common.collect.ImmutableList) AccountIndex(com.google.gerrit.server.index.account.AccountIndex) Map(java.util.Map) FieldBundle(com.google.gerrit.index.query.FieldBundle) Change(com.google.gerrit.entities.Change) Predicate(com.google.gerrit.index.query.Predicate) AccountGroup(com.google.gerrit.entities.AccountGroup) FieldDef(com.google.gerrit.index.FieldDef) Schema(com.google.gerrit.index.Schema) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Account(com.google.gerrit.entities.Account) QueryOptions(com.google.gerrit.index.QueryOptions) Instant(java.time.Instant) Index(com.google.gerrit.index.Index) ChangeField(com.google.gerrit.server.index.change.ChangeField) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SitePaths(com.google.gerrit.server.config.SitePaths) Project(com.google.gerrit.entities.Project) DataSource(com.google.gerrit.index.query.DataSource) GroupIndex(com.google.gerrit.server.index.group.GroupIndex) AccountState(com.google.gerrit.server.account.AccountState) Comparator(java.util.Comparator) ChangeIndex(com.google.gerrit.server.index.change.ChangeIndex) ListResultSet(com.google.gerrit.index.query.ListResultSet) FieldBundle(com.google.gerrit.index.query.FieldBundle) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DataSource(com.google.gerrit.index.query.DataSource) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 2 with FieldBundle

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();
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) FieldBundle(com.google.gerrit.index.query.FieldBundle) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RefState(com.google.gerrit.index.RefState) Map(java.util.Map)

Example 3 with FieldBundle

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);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) FieldBundle(com.google.gerrit.index.query.FieldBundle)

Example 4 with FieldBundle

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);
}
Also used : FieldBundle(com.google.gerrit.index.query.FieldBundle) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountState(com.google.gerrit.server.account.AccountState) AccountExternalIdInfo(com.google.gerrit.extensions.common.AccountExternalIdInfo) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) Test(org.junit.Test)

Example 5 with FieldBundle

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());
}
Also used : AccountGroup(com.google.gerrit.entities.AccountGroup) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) FieldBundle(com.google.gerrit.index.query.FieldBundle) Test(org.junit.Test)

Aggregations

FieldBundle (com.google.gerrit.index.query.FieldBundle)10 Project (com.google.gerrit.entities.Project)4 FieldDef (com.google.gerrit.index.FieldDef)3 QueryOptions (com.google.gerrit.index.QueryOptions)3 ProjectIndex (com.google.gerrit.index.project.ProjectIndex)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Account (com.google.gerrit.entities.Account)2 AccountGroup (com.google.gerrit.entities.AccountGroup)2 StorageException (com.google.gerrit.exceptions.StorageException)2 IndexConfig (com.google.gerrit.index.IndexConfig)2 RefState (com.google.gerrit.index.RefState)2 ProjectData (com.google.gerrit.index.project.ProjectData)2 ProjectIndexCollection (com.google.gerrit.index.project.ProjectIndexCollection)2 Predicate (com.google.gerrit.index.query.Predicate)2 QueryParseException (com.google.gerrit.index.query.QueryParseException)2 ResultSet (com.google.gerrit.index.query.ResultSet)2 AccountState (com.google.gerrit.server.account.AccountState)2 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)2 Inject (com.google.inject.Inject)2