DfsReftableStack.java

  1. /*
  2.  * Copyright (C) 2017, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.internal.storage.dfs;

  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.List;

  15. import org.eclipse.jgit.internal.storage.reftable.ReftableReader;

  16. /**
  17.  * Tracks multiple open
  18.  * {@link org.eclipse.jgit.internal.storage.reftable.ReftableReader} instances.
  19.  */
  20. public class DfsReftableStack implements AutoCloseable {
  21.     /**
  22.      * Opens a stack of tables for reading.
  23.      *
  24.      * @param ctx
  25.      *            context to read the tables with. This {@code ctx} will be
  26.      *            retained by the stack and each of the table readers.
  27.      * @param files
  28.      *            the tables to open.
  29.      * @return stack reference to close the tables.
  30.      * @throws java.io.IOException
  31.      *             a table could not be opened
  32.      */
  33.     public static DfsReftableStack open(DfsReader ctx, List<DfsReftable> files)
  34.             throws IOException {
  35.         DfsReftableStack stack = new DfsReftableStack(files.size());
  36.         boolean close = true;
  37.         try {
  38.             for (DfsReftable t : files) {
  39.                 stack.files.add(t);
  40.                 stack.tables.add(t.open(ctx));
  41.             }
  42.             close = false;
  43.             return stack;
  44.         } finally {
  45.             if (close) {
  46.                 stack.close();
  47.             }
  48.         }
  49.     }

  50.     private final List<DfsReftable> files;
  51.     private final List<ReftableReader> tables;

  52.     private DfsReftableStack(int tableCnt) {
  53.         this.files = new ArrayList<>(tableCnt);
  54.         this.tables = new ArrayList<>(tableCnt);
  55.     }

  56.     /**
  57.      * Get unmodifiable list of DfsRefatble files
  58.      *
  59.      * @return unmodifiable list of DfsRefatble files, in the same order the
  60.      *         files were passed to {@link #open(DfsReader, List)}.
  61.      */
  62.     public List<DfsReftable> files() {
  63.         return Collections.unmodifiableList(files);
  64.     }

  65.     /**
  66.      * Get unmodifiable list of tables
  67.      *
  68.      * @return unmodifiable list of tables, in the same order the files were
  69.      *         passed to {@link #open(DfsReader, List)}.
  70.      */
  71.     public List<ReftableReader> readers() {
  72.         return Collections.unmodifiableList(tables);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public void close() {
  77.         for (ReftableReader t : tables) {
  78.             try {
  79.                 t.close();
  80.             } catch (IOException e) {
  81.                 // Ignore close failures.
  82.             }
  83.         }
  84.     }
  85. }