LCOV - code coverage report
Current view: top level - server/account - GroupsSnapshotReader.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 15 15 100.0 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2020 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.account;
      16             : 
      17             : import com.google.auto.value.AutoValue;
      18             : import com.google.common.collect.ImmutableList;
      19             : import com.google.common.hash.Hashing;
      20             : import com.google.gerrit.entities.RefNames;
      21             : import com.google.gerrit.server.config.AllUsersName;
      22             : import com.google.gerrit.server.git.GitRepositoryManager;
      23             : import com.google.inject.Inject;
      24             : import com.google.inject.Singleton;
      25             : import java.io.IOException;
      26             : import java.nio.ByteBuffer;
      27             : import org.eclipse.jgit.lib.Constants;
      28             : import org.eclipse.jgit.lib.Ref;
      29             : import org.eclipse.jgit.lib.Repository;
      30             : 
      31             : /**
      32             :  * This class is used to compute a compound key that represents the state of the internal groups in
      33             :  * NoteDb.
      34             :  */
      35             : @Singleton
      36             : public class GroupsSnapshotReader {
      37             :   private final GitRepositoryManager repoManager;
      38             :   private final AllUsersName allUsersName;
      39             : 
      40             :   @Inject
      41         152 :   GroupsSnapshotReader(GitRepositoryManager repoManager, AllUsersName allUsersName) {
      42         152 :     this.repoManager = repoManager;
      43         152 :     this.allUsersName = allUsersName;
      44         152 :   }
      45             : 
      46             :   @AutoValue
      47          20 :   public abstract static class Snapshot {
      48             :     /**
      49             :      * 128-bit hash of all group ref {@link com.google.gerrit.git.ObjectIds}. To be used as cache
      50             :      * key.
      51             :      */
      52             :     public abstract String hash();
      53             : 
      54             :     /** Snapshot of the state of all relevant NoteDb group refs. */
      55             :     public abstract ImmutableList<Ref> groupsRefs();
      56             : 
      57             :     public static Snapshot create(String hash, ImmutableList<Ref> groupsRefs) {
      58          20 :       return new AutoValue_GroupsSnapshotReader_Snapshot(hash, groupsRefs);
      59             :     }
      60             :   }
      61             : 
      62             :   /** Retrieves a snapshot key of all internal groups refs from NoteDb. */
      63             :   public Snapshot getSnapshot() throws IOException {
      64          20 :     try (Repository repo = repoManager.openRepository(allUsersName)) {
      65          20 :       ImmutableList<Ref> groupsRefs =
      66          20 :           ImmutableList.copyOf(repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_GROUPS));
      67          20 :       ByteBuffer buf = ByteBuffer.allocate(groupsRefs.size() * Constants.OBJECT_ID_LENGTH);
      68          20 :       for (Ref groupRef : groupsRefs) {
      69          20 :         groupRef.getObjectId().copyRawTo(buf);
      70          20 :       }
      71          20 :       String hash = Hashing.murmur3_128().hashBytes(buf.array()).toString();
      72          20 :       return Snapshot.create(hash, groupsRefs);
      73             :     }
      74             :   }
      75             : }

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