com.google.gerrit.server.index.group.GroupField Maven / Gradle / Ivy
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.index.group;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.base.MoreObjects;
import com.google.gerrit.entities.Account;
import com.google.gerrit.entities.AccountGroup;
import com.google.gerrit.entities.InternalGroup;
import com.google.gerrit.git.ObjectIds;
import com.google.gerrit.index.IndexedField;
import com.google.gerrit.index.SchemaUtil;
import java.sql.Timestamp;
import org.eclipse.jgit.lib.ObjectId;
/**
 * Secondary index schemas for groups.
 *
 * Note that this class does not override {@link Object#equals(Object)}. It relies on instances
 * being singletons so that the default (i.e. reference) comparison works.
 */
public class GroupField {
  /** Legacy group ID. */
  public static final IndexedField ID_FIELD =
      IndexedField.integerBuilder("Id").required().build(g -> g.getId().get());
  public static final IndexedField.SearchSpec ID_FIELD_SPEC =
      ID_FIELD.integer("id");
  /** Group UUID. */
  public static final IndexedField UUID_FIELD =
      IndexedField.stringBuilder("UUID")
          .required()
          .stored()
          .build(g -> g.getGroupUUID().get());
  public static final IndexedField.SearchSpec UUID_FIELD_SPEC =
      UUID_FIELD.exact("uuid");
  /** Group owner UUID. */
  public static final IndexedField OWNER_UUID_FIELD =
      IndexedField.stringBuilder("OwnerUUID")
          .required()
          .build(g -> g.getOwnerGroupUUID().get());
  public static final IndexedField.SearchSpec OWNER_UUID_SPEC =
      OWNER_UUID_FIELD.exact("owner_uuid");
  /** Timestamp indicating when this group was created. */
  // TODO(issue-15518): Migrate type for timestamp index fields from Timestamp to Instant
  public static final IndexedField CREATED_ON_FIELD =
      IndexedField.timestampBuilder("CreatedOn")
          .required()
          .build(internalGroup -> Timestamp.from(internalGroup.getCreatedOn()));
  public static final IndexedField.SearchSpec CREATED_ON_SPEC =
      CREATED_ON_FIELD.timestamp("created_on");
  /** Group name. */
  public static final IndexedField NAME_FIELD =
      IndexedField.stringBuilder("Name")
          .required()
          .size(200)
          .build(InternalGroup::getName);
  public static final IndexedField.SearchSpec NAME_SPEC =
      NAME_FIELD.exact("name");
  /** Prefix match on group name parts. */
  public static final IndexedField> NAME_PART_FIELD =
      IndexedField.iterableStringBuilder("NamePart")
          .required()
          .size(200)
          .build(g -> SchemaUtil.getNameParts(g.getName()));
  public static final IndexedField>.SearchSpec NAME_PART_SPEC =
      NAME_PART_FIELD.prefix("name_part");
  /** Group description. */
  public static final IndexedField DESCRIPTION_FIELD =
      IndexedField.stringBuilder("Description").build(InternalGroup::getDescription);
  public static final IndexedField.SearchSpec DESCRIPTION_SPEC =
      DESCRIPTION_FIELD.fullText("description");
  /** Whether the group is visible to all users. */
  public static final IndexedField IS_VISIBLE_TO_ALL_FIELD =
      IndexedField.stringBuilder("IsVisibleToAll")
          .required()
          .size(1)
          .build(g -> g.isVisibleToAll() ? "1" : "0");
  public static final IndexedField.SearchSpec IS_VISIBLE_TO_ALL_SPEC =
      IS_VISIBLE_TO_ALL_FIELD.exact("is_visible_to_all");
  public static final IndexedField> MEMBER_FIELD =
      IndexedField.iterableIntegerBuilder("Member")
          .build(g -> g.getMembers().stream().map(Account.Id::get).collect(toImmutableList()));
  public static final IndexedField>.SearchSpec MEMBER_SPEC =
      MEMBER_FIELD.integer("member");
  public static final IndexedField> SUBGROUP_FIELD =
      IndexedField.iterableStringBuilder("Subgroup")
          .build(
              g ->
                  g.getSubgroups().stream().map(AccountGroup.UUID::get).collect(toImmutableList()));
  public static final IndexedField>.SearchSpec SUBGROUP_SPEC =
      SUBGROUP_FIELD.exact("subgroup");
  /** ObjectId of HEAD:refs/groups/. */
  public static final IndexedField REF_STATE_FIELD =
      IndexedField.byteArrayBuilder("RefState")
          .stored()
          .required()
          .build(
              g -> {
                byte[] a = new byte[ObjectIds.STR_LEN];
                MoreObjects.firstNonNull(g.getRefState(), ObjectId.zeroId()).copyTo(a, 0);
                return a;
              });
  public static final IndexedField.SearchSpec REF_STATE_SPEC =
      REF_STATE_FIELD.storedOnly("ref_state");
}