LCOV - code coverage report
Current view: top level - server/git - RepoRefCache.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 14 16 87.5 %
Date: 2022-11-19 15:00:39 Functions: 3 4 75.0 %

          Line data    Source code
       1             : // Copyright (C) 2016 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.git;
      16             : 
      17             : import java.io.IOException;
      18             : import java.util.Collections;
      19             : import java.util.HashMap;
      20             : import java.util.Map;
      21             : import java.util.Optional;
      22             : import org.eclipse.jgit.lib.ObjectId;
      23             : import org.eclipse.jgit.lib.Ref;
      24             : import org.eclipse.jgit.lib.RefDatabase;
      25             : import org.eclipse.jgit.lib.Repository;
      26             : 
      27             : /** {@link RefCache} backed directly by a repository. */
      28             : public class RepoRefCache implements RefCache {
      29             :   private final RefDatabase refdb;
      30             :   private final Map<String, Optional<ObjectId>> ids;
      31             :   private final Repository repo;
      32             : 
      33         110 :   public RepoRefCache(Repository repo) {
      34         110 :     repo.incrementOpen();
      35         110 :     this.repo = repo;
      36         110 :     this.refdb = repo.getRefDatabase();
      37         110 :     this.ids = new HashMap<>();
      38         110 :   }
      39             : 
      40             :   @Override
      41             :   public Optional<ObjectId> get(String refName) throws IOException {
      42         103 :     Optional<ObjectId> id = ids.get(refName);
      43         103 :     if (id != null) {
      44           5 :       return id;
      45             :     }
      46         103 :     Ref ref = refdb.exactRef(refName);
      47         103 :     id = Optional.ofNullable(ref).map(Ref::getObjectId);
      48         103 :     ids.put(refName, id);
      49         103 :     return id;
      50             :   }
      51             : 
      52             :   /** Returns an unmodifiable view of the refs that have been cached by this instance. */
      53             :   public Map<String, Optional<ObjectId>> getCachedRefs() {
      54          59 :     return Collections.unmodifiableMap(ids);
      55             :   }
      56             : 
      57             :   @Override
      58             :   public void close() {
      59           0 :     repo.close();
      60           0 :   }
      61             : }

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