LCOV - code coverage report
Current view: top level - server/git - InMemoryInserter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 30 42 71.4 %
Date: 2022-11-19 15:00:39 Functions: 15 20 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 static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.common.collect.ImmutableList;
      20             : import java.io.IOException;
      21             : import java.io.InputStream;
      22             : import java.util.Collection;
      23             : import java.util.HashSet;
      24             : import java.util.LinkedHashMap;
      25             : import java.util.Map;
      26             : import java.util.Set;
      27             : import org.eclipse.jgit.errors.IncorrectObjectTypeException;
      28             : import org.eclipse.jgit.lib.AbbreviatedObjectId;
      29             : import org.eclipse.jgit.lib.AnyObjectId;
      30             : import org.eclipse.jgit.lib.ObjectId;
      31             : import org.eclipse.jgit.lib.ObjectInserter;
      32             : import org.eclipse.jgit.lib.ObjectLoader;
      33             : import org.eclipse.jgit.lib.ObjectReader;
      34             : import org.eclipse.jgit.lib.Repository;
      35             : import org.eclipse.jgit.transport.PackParser;
      36             : 
      37             : public class InMemoryInserter extends ObjectInserter {
      38             :   private final ObjectReader reader;
      39         110 :   private final Map<ObjectId, InsertedObject> inserted = new LinkedHashMap<>();
      40             :   private final boolean closeReader;
      41             : 
      42         110 :   public InMemoryInserter(ObjectReader reader) {
      43         110 :     this.reader = requireNonNull(reader);
      44         110 :     closeReader = false;
      45         110 :   }
      46             : 
      47          18 :   public InMemoryInserter(Repository repo) {
      48          18 :     this.reader = repo.newObjectReader();
      49          18 :     closeReader = true;
      50          18 :   }
      51             : 
      52             :   @Override
      53             :   public ObjectId insert(int type, long length, InputStream in) throws IOException {
      54           7 :     return insert(InsertedObject.create(type, in));
      55             :   }
      56             : 
      57             :   @Override
      58             :   public ObjectId insert(int type, byte[] data) {
      59         103 :     return insert(type, data, 0, data.length);
      60             :   }
      61             : 
      62             :   @Override
      63             :   public ObjectId insert(int type, byte[] data, int off, int len) {
      64         103 :     return insert(InsertedObject.create(type, data, off, len));
      65             :   }
      66             : 
      67             :   public ObjectId insert(InsertedObject obj) {
      68         103 :     inserted.put(obj.id(), obj);
      69         103 :     return obj.id();
      70             :   }
      71             : 
      72             :   @Override
      73             :   public PackParser newPackParser(InputStream in) {
      74           0 :     throw new UnsupportedOperationException();
      75             :   }
      76             : 
      77             :   @Override
      78             :   public ObjectReader newReader() {
      79         110 :     return new Reader();
      80             :   }
      81             : 
      82             :   @Override
      83             :   public void flush() {
      84             :     // Do nothing; objects are not written to the repo.
      85           0 :   }
      86             : 
      87             :   @Override
      88             :   public void close() {
      89          51 :     if (closeReader) {
      90          18 :       reader.close();
      91             :     }
      92          51 :   }
      93             : 
      94             :   public ImmutableList<InsertedObject> getInsertedObjects() {
      95         109 :     return ImmutableList.copyOf(inserted.values());
      96             :   }
      97             : 
      98             :   public int getInsertedObjectCount() {
      99           0 :     return inserted.values().size();
     100             :   }
     101             : 
     102             :   public void clear() {
     103         109 :     inserted.clear();
     104         109 :   }
     105             : 
     106         110 :   private class Reader extends ObjectReader {
     107             :     @Override
     108             :     public ObjectReader newReader() {
     109           0 :       return new Reader();
     110             :     }
     111             : 
     112             :     @Override
     113             :     public Collection<ObjectId> resolve(AbbreviatedObjectId id) throws IOException {
     114           0 :       Set<ObjectId> result = new HashSet<>();
     115           0 :       for (ObjectId insId : inserted.keySet()) {
     116           0 :         if (id.prefixCompare(insId) == 0) {
     117           0 :           result.add(insId);
     118             :         }
     119           0 :       }
     120           0 :       result.addAll(reader.resolve(id));
     121           0 :       return result;
     122             :     }
     123             : 
     124             :     @Override
     125             :     public ObjectLoader open(AnyObjectId objectId, int typeHint) throws IOException {
     126         109 :       InsertedObject obj = inserted.get(objectId);
     127         109 :       if (obj == null) {
     128         109 :         return reader.open(objectId, typeHint);
     129             :       }
     130          23 :       if (typeHint != OBJ_ANY && obj.type() != typeHint) {
     131           0 :         throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
     132             :       }
     133          23 :       return obj.newLoader();
     134             :     }
     135             : 
     136             :     @Override
     137             :     public Set<ObjectId> getShallowCommits() throws IOException {
     138         109 :       return reader.getShallowCommits();
     139             :     }
     140             : 
     141             :     @Override
     142             :     public void close() {
     143             :       // Do nothing; this class owns no open resources.
     144         110 :     }
     145             : 
     146             :     @Override
     147             :     public ObjectInserter getCreatedFromInserter() {
     148         103 :       return InMemoryInserter.this;
     149             :     }
     150             :   }
     151             : }

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