LCOV - code coverage report
Current view: top level - server/index/group - GroupField.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 69 70 98.6 %
Date: 2022-11-19 15:00:39 Functions: 10 11 90.9 %

          Line data    Source code
       1             : // Copyright (C) 2017 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.index.group;
      16             : 
      17             : import static com.google.common.collect.ImmutableList.toImmutableList;
      18             : 
      19             : import com.google.common.base.MoreObjects;
      20             : import com.google.gerrit.entities.Account;
      21             : import com.google.gerrit.entities.AccountGroup;
      22             : import com.google.gerrit.entities.InternalGroup;
      23             : import com.google.gerrit.git.ObjectIds;
      24             : import com.google.gerrit.index.IndexedField;
      25             : import com.google.gerrit.index.SchemaUtil;
      26             : import java.sql.Timestamp;
      27             : import org.eclipse.jgit.lib.ObjectId;
      28             : 
      29             : /**
      30             :  * Secondary index schemas for groups.
      31             :  *
      32             :  * <p>Note that this class does not override {@link Object#equals(Object)}. It relies on instances
      33             :  * being singletons so that the default (i.e. reference) comparison works.
      34             :  */
      35           0 : public class GroupField {
      36             :   /** Legacy group ID. */
      37         153 :   public static final IndexedField<InternalGroup, Integer> ID_FIELD =
      38         153 :       IndexedField.<InternalGroup>integerBuilder("Id").required().build(g -> g.getId().get());
      39             : 
      40         153 :   public static final IndexedField<InternalGroup, Integer>.SearchSpec ID_FIELD_SPEC =
      41         153 :       ID_FIELD.integer("id");
      42             : 
      43             :   /** Group UUID. */
      44         153 :   public static final IndexedField<InternalGroup, String> UUID_FIELD =
      45         153 :       IndexedField.<InternalGroup>stringBuilder("UUID")
      46         153 :           .required()
      47         153 :           .stored()
      48         153 :           .build(g -> g.getGroupUUID().get());
      49             : 
      50         153 :   public static final IndexedField<InternalGroup, String>.SearchSpec UUID_FIELD_SPEC =
      51         153 :       UUID_FIELD.exact("uuid");
      52             : 
      53             :   /** Group owner UUID. */
      54         153 :   public static final IndexedField<InternalGroup, String> OWNER_UUID_FIELD =
      55         153 :       IndexedField.<InternalGroup>stringBuilder("OwnerUUID")
      56         153 :           .required()
      57         153 :           .build(g -> g.getOwnerGroupUUID().get());
      58             : 
      59         153 :   public static final IndexedField<InternalGroup, String>.SearchSpec OWNER_UUID_SPEC =
      60         153 :       OWNER_UUID_FIELD.exact("owner_uuid");
      61             : 
      62             :   /** Timestamp indicating when this group was created. */
      63             :   // TODO(issue-15518): Migrate type for timestamp index fields from Timestamp to Instant
      64         153 :   public static final IndexedField<InternalGroup, Timestamp> CREATED_ON_FIELD =
      65         153 :       IndexedField.<InternalGroup>timestampBuilder("CreatedOn")
      66         153 :           .required()
      67         153 :           .build(internalGroup -> Timestamp.from(internalGroup.getCreatedOn()));
      68             : 
      69         153 :   public static final IndexedField<InternalGroup, Timestamp>.SearchSpec CREATED_ON_SPEC =
      70         153 :       CREATED_ON_FIELD.timestamp("created_on");
      71             : 
      72             :   /** Group name. */
      73         153 :   public static final IndexedField<InternalGroup, String> NAME_FIELD =
      74         153 :       IndexedField.<InternalGroup>stringBuilder("Name")
      75         153 :           .required()
      76         153 :           .size(200)
      77         153 :           .build(InternalGroup::getName);
      78             : 
      79         153 :   public static final IndexedField<InternalGroup, String>.SearchSpec NAME_SPEC =
      80         153 :       NAME_FIELD.exact("name");
      81             : 
      82             :   /** Prefix match on group name parts. */
      83         153 :   public static final IndexedField<InternalGroup, Iterable<String>> NAME_PART_FIELD =
      84         153 :       IndexedField.<InternalGroup>iterableStringBuilder("NamePart")
      85         153 :           .required()
      86         153 :           .size(200)
      87         153 :           .build(g -> SchemaUtil.getNameParts(g.getName()));
      88             : 
      89         153 :   public static final IndexedField<InternalGroup, Iterable<String>>.SearchSpec NAME_PART_SPEC =
      90         153 :       NAME_PART_FIELD.prefix("name_part");
      91             : 
      92             :   /** Group description. */
      93         153 :   public static final IndexedField<InternalGroup, String> DESCRIPTION_FIELD =
      94         153 :       IndexedField.<InternalGroup>stringBuilder("Description").build(InternalGroup::getDescription);
      95             : 
      96         153 :   public static final IndexedField<InternalGroup, String>.SearchSpec DESCRIPTION_SPEC =
      97         153 :       DESCRIPTION_FIELD.fullText("description");
      98             : 
      99             :   /** Whether the group is visible to all users. */
     100         153 :   public static final IndexedField<InternalGroup, String> IS_VISIBLE_TO_ALL_FIELD =
     101         153 :       IndexedField.<InternalGroup>stringBuilder("IsVisibleToAll")
     102         153 :           .required()
     103         153 :           .size(1)
     104         153 :           .build(g -> g.isVisibleToAll() ? "1" : "0");
     105             : 
     106         153 :   public static final IndexedField<InternalGroup, String>.SearchSpec IS_VISIBLE_TO_ALL_SPEC =
     107         153 :       IS_VISIBLE_TO_ALL_FIELD.exact("is_visible_to_all");
     108             : 
     109         153 :   public static final IndexedField<InternalGroup, Iterable<Integer>> MEMBER_FIELD =
     110         153 :       IndexedField.<InternalGroup>iterableIntegerBuilder("Member")
     111         153 :           .build(g -> g.getMembers().stream().map(Account.Id::get).collect(toImmutableList()));
     112             : 
     113         153 :   public static final IndexedField<InternalGroup, Iterable<Integer>>.SearchSpec MEMBER_SPEC =
     114         153 :       MEMBER_FIELD.integer("member");
     115             : 
     116         153 :   public static final IndexedField<InternalGroup, Iterable<String>> SUBGROUP_FIELD =
     117         153 :       IndexedField.<InternalGroup>iterableStringBuilder("Subgroup")
     118         153 :           .build(
     119             :               g ->
     120          26 :                   g.getSubgroups().stream().map(AccountGroup.UUID::get).collect(toImmutableList()));
     121             : 
     122         153 :   public static final IndexedField<InternalGroup, Iterable<String>>.SearchSpec SUBGROUP_SPEC =
     123         153 :       SUBGROUP_FIELD.exact("subgroup");
     124             : 
     125             :   /** ObjectId of HEAD:refs/groups/<UUID>. */
     126         153 :   public static final IndexedField<InternalGroup, byte[]> REF_STATE_FIELD =
     127         153 :       IndexedField.<InternalGroup>byteArrayBuilder("RefState")
     128         153 :           .stored()
     129         153 :           .required()
     130         153 :           .build(
     131             :               g -> {
     132           9 :                 byte[] a = new byte[ObjectIds.STR_LEN];
     133           9 :                 MoreObjects.firstNonNull(g.getRefState(), ObjectId.zeroId()).copyTo(a, 0);
     134           9 :                 return a;
     135             :               });
     136             : 
     137         153 :   public static final IndexedField<InternalGroup, byte[]>.SearchSpec REF_STATE_SPEC =
     138         153 :       REF_STATE_FIELD.storedOnly("ref_state");
     139             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750