Package org.eclipse.jgit.storage.pack

Examples of org.eclipse.jgit.storage.pack.PackWriter


  private void writePack(final Map<String, RemoteRefUpdate> refUpdates,
      final ProgressMonitor monitor) throws IOException {
    Set<ObjectId> remoteObjects = new HashSet<ObjectId>();
    Set<ObjectId> newObjects = new HashSet<ObjectId>();

    final PackWriter writer = new PackWriter(transport.getPackConfig(),
        local.newObjectReader());
    try {

      for (final Ref r : getRefs())
        remoteObjects.add(r.getObjectId());
      remoteObjects.addAll(additionalHaves);
      for (final RemoteRefUpdate r : refUpdates.values()) {
        if (!ObjectId.zeroId().equals(r.getNewObjectId()))
          newObjects.add(r.getNewObjectId());
      }

      writer.setUseCachedPacks(true);
      writer.setThin(thinPack);
      writer.setReuseValidatingObjects(false);
      writer.setDeltaBaseAsOffset(capableOfsDelta);
      writer.preparePack(monitor, newObjects, remoteObjects);
      writer.writePack(monitor, monitor, out);
    } finally {
      writer.release();
    }
    packTransferTime = writer.getStatistics().getTimeWriting();
  }
View Full Code Here


  private void sendpack(final List<RemoteRefUpdate> updates,
      final ProgressMonitor monitor) throws TransportException {
    String pathPack = null;
    String pathIdx = null;

    final PackWriter writer = new PackWriter(transport.getPackConfig(),
        local.newObjectReader());
    try {
      final Set<ObjectId> need = new HashSet<ObjectId>();
      final Set<ObjectId> have = new HashSet<ObjectId>();
      for (final RemoteRefUpdate r : updates)
        need.add(r.getNewObjectId());
      for (final Ref r : getRefs()) {
        have.add(r.getObjectId());
        if (r.getPeeledObjectId() != null)
          have.add(r.getPeeledObjectId());
      }
      writer.preparePack(monitor, need, have);

      // We don't have to continue further if the pack will
      // be an empty pack, as the remote has all objects it
      // needs to complete this change.
      //
      if (writer.getObjectCount() == 0)
        return;

      packNames = new LinkedHashMap<String, String>();
      for (final String n : dest.getPackNames())
        packNames.put(n, n);

      final String base = "pack-" + writer.computeName().name();
      final String packName = base + ".pack";
      pathPack = "pack/" + packName;
      pathIdx = "pack/" + base + ".idx";

      if (packNames.remove(packName) != null) {
        // The remote already contains this pack. We should
        // remove the index before overwriting to prevent bad
        // offsets from appearing to clients.
        //
        dest.writeInfoPacks(packNames.keySet());
        dest.deleteFile(pathIdx);
      }

      // Write the pack file, then the index, as readers look the
      // other direction (index, then pack file).
      //
      final String wt = "Put " + base.substring(0, 12);
      OutputStream os = dest.writeFile(pathPack, monitor, wt + "..pack");
      try {
        os = new SafeBufferedOutputStream(os);
        writer.writePack(monitor, monitor, os);
      } finally {
        os.close();
      }

      os = dest.writeFile(pathIdx, monitor, wt + "..idx");
      try {
        os = new SafeBufferedOutputStream(os);
        writer.writeIndex(os);
      } finally {
        os.close();
      }

      // Record the pack at the start of the pack info list. This
      // way clients are likely to consult the newest pack first,
      // and discover the most recent objects there.
      //
      final ArrayList<String> infoPacks = new ArrayList<String>();
      infoPacks.add(packName);
      infoPacks.addAll(packNames.keySet());
      dest.writeInfoPacks(infoPacks);

    } catch (IOException err) {
      safeDelete(pathIdx);
      safeDelete(pathPack);

      throw new TransportException(uri, JGitText.get().cannotStoreObjects, err);
    } finally {
      writer.release();
    }
  }
View Full Code Here

    }

    PackConfig cfg = packConfig;
    if (cfg == null)
      cfg = new PackConfig(db);
    final PackWriter pw = new PackWriter(cfg, walk.getObjectReader());
    try {
      pw.setUseCachedPacks(true);
      pw.setReuseDeltaCommits(true);
      pw.setDeltaBaseAsOffset(options.contains(OPTION_OFS_DELTA));
      pw.setThin(options.contains(OPTION_THIN_PACK));
      pw.setReuseValidatingObjects(false);

      if (commonBase.isEmpty() && refs != null) {
        Set<ObjectId> tagTargets = new HashSet<ObjectId>();
        for (Ref ref : refs.values()) {
          if (ref.getPeeledObjectId() != null)
            tagTargets.add(ref.getPeeledObjectId());
          else if (ref.getObjectId() == null)
            continue;
          else if (ref.getName().startsWith(Constants.R_HEADS))
            tagTargets.add(ref.getObjectId());
        }
        pw.setTagTargets(tagTargets);
      }

      if (depth > 0)
        pw.setShallowPack(depth, unshallowCommits);

      RevWalk rw = walk;
      if (wantAll.isEmpty()) {
        pw.preparePack(pm, wantIds, commonBase);
      } else {
        walk.reset();

        ObjectWalk ow = walk.toObjectWalkWithSameObjects();
        pw.preparePack(pm, ow, wantAll, commonBase);
        rw = ow;
      }

      if (options.contains(OPTION_INCLUDE_TAG) && refs != null) {
        for (Ref ref : refs.values()) {
          ObjectId objectId = ref.getObjectId();

          // If the object was already requested, skip it.
          if (wantAll.isEmpty()) {
            if (wantIds.contains(objectId))
              continue;
          } else {
            RevObject obj = rw.lookupOrNull(objectId);
            if (obj != null && obj.has(WANT))
              continue;
          }

          if (!ref.isPeeled())
            ref = db.peel(ref);

          ObjectId peeledId = ref.getPeeledObjectId();
          if (peeledId == null)
            continue;

          objectId = ref.getObjectId();
          if (pw.willInclude(peeledId) && !pw.willInclude(objectId))
            pw.addObject(rw.parseAny(objectId));
        }
      }

      pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut);
      statistics = pw.getStatistics();

      if (msgOut != null) {
        String msg = pw.getStatistics().getMessage() + '\n';
        msgOut.write(Constants.encode(msg));
        msgOut.flush();
      }

    } finally {
      pw.release();
    }

    if (sideband)
      pckOut.end();
View Full Code Here

      pc.setIndexVersion(2);
      pc.setDeltaCompress(false);
      pc.setReuseDeltas(true);
      pc.setReuseObjects(true);

      PackWriter pw = new PackWriter(pc, ctx);
      try {
        pw.setDeltaBaseAsOffset(true);
        pw.setReuseDeltaCommits(false);

        addObjectsToPack(pw, ctx, pm);
        if (pw.getObjectCount() == 0)
          return;

        boolean rollback = true;
        DfsPackDescription pack = objdb.newPack(COMPACT);
        try {
          writePack(objdb, pack, pw, pm);
          writeIndex(objdb, pack, pw);

          PackWriter.Statistics stats = pw.getStatistics();
          pw.release();
          pw = null;

          pack.setPackStats(stats);
          objdb.commitPack(Collections.singletonList(pack), toPrune());
          newPacks.add(pack);
          newStats.add(stats);
          rollback = false;
        } finally {
          if (rollback)
            objdb.rollbackPack(Collections.singletonList(pack));
        }
      } finally {
        if (pw != null)
          pw.release();
      }
    } finally {
      ctx.release();
    }
  }
View Full Code Here

  private void writePack(final Map<String, RemoteRefUpdate> refUpdates,
      final ProgressMonitor monitor) throws IOException {
    List<ObjectId> remoteObjects = new ArrayList<ObjectId>(getRefs().size());
    List<ObjectId> newObjects = new ArrayList<ObjectId>(refUpdates.size());

    final PackWriter writer = new PackWriter(transport.getPackConfig(),
        local.newObjectReader());
    try {

      for (final Ref r : getRefs())
        remoteObjects.add(r.getObjectId());
      remoteObjects.addAll(additionalHaves);
      for (final RemoteRefUpdate r : refUpdates.values()) {
        if (!ObjectId.zeroId().equals(r.getNewObjectId()))
          newObjects.add(r.getNewObjectId());
      }

      writer.setUseCachedPacks(true);
      writer.setThin(thinPack);
      writer.setReuseValidatingObjects(false);
      writer.setDeltaBaseAsOffset(capableOfsDelta);
      writer.preparePack(monitor, newObjects, remoteObjects);
      writer.writePack(monitor, monitor, out);
    } finally {
      writer.release();
    }
    packTransferTime = writer.getStatistics().getTimeWriting();
  }
View Full Code Here

  private void sendpack(final List<RemoteRefUpdate> updates,
      final ProgressMonitor monitor) throws TransportException {
    String pathPack = null;
    String pathIdx = null;

    final PackWriter writer = new PackWriter(transport.getPackConfig(),
        local.newObjectReader());
    try {
      final List<ObjectId> need = new ArrayList<ObjectId>();
      final List<ObjectId> have = new ArrayList<ObjectId>();
      for (final RemoteRefUpdate r : updates)
        need.add(r.getNewObjectId());
      for (final Ref r : getRefs()) {
        have.add(r.getObjectId());
        if (r.getPeeledObjectId() != null)
          have.add(r.getPeeledObjectId());
      }
      writer.preparePack(monitor, need, have);

      // We don't have to continue further if the pack will
      // be an empty pack, as the remote has all objects it
      // needs to complete this change.
      //
      if (writer.getObjectCount() == 0)
        return;

      packNames = new LinkedHashMap<String, String>();
      for (final String n : dest.getPackNames())
        packNames.put(n, n);

      final String base = "pack-" + writer.computeName().name();
      final String packName = base + ".pack";
      pathPack = "pack/" + packName;
      pathIdx = "pack/" + base + ".idx";

      if (packNames.remove(packName) != null) {
        // The remote already contains this pack. We should
        // remove the index before overwriting to prevent bad
        // offsets from appearing to clients.
        //
        dest.writeInfoPacks(packNames.keySet());
        dest.deleteFile(pathIdx);
      }

      // Write the pack file, then the index, as readers look the
      // other direction (index, then pack file).
      //
      final String wt = "Put " + base.substring(0, 12);
      OutputStream os = dest.writeFile(pathPack, monitor, wt + "..pack");
      try {
        os = new BufferedOutputStream(os);
        writer.writePack(monitor, monitor, os);
      } finally {
        os.close();
      }

      os = dest.writeFile(pathIdx, monitor, wt + "..idx");
      try {
        os = new BufferedOutputStream(os);
        writer.writeIndex(os);
      } finally {
        os.close();
      }

      // Record the pack at the start of the pack info list. This
      // way clients are likely to consult the newest pack first,
      // and discover the most recent objects there.
      //
      final ArrayList<String> infoPacks = new ArrayList<String>();
      infoPacks.add(packName);
      infoPacks.addAll(packNames.keySet());
      dest.writeInfoPacks(infoPacks);

    } catch (IOException err) {
      safeDelete(pathIdx);
      safeDelete(pathPack);

      throw new TransportException(uri, JGitText.get().cannotStoreObjects, err);
    } finally {
      writer.release();
    }
  }
View Full Code Here

  public void writeBundle(ProgressMonitor monitor, OutputStream os)
      throws IOException {
    PackConfig pc = packConfig;
    if (pc == null)
      pc = new PackConfig(db);
    PackWriter packWriter = new PackWriter(pc, db.newObjectReader());
    try {
      final HashSet<ObjectId> inc = new HashSet<ObjectId>();
      final HashSet<ObjectId> exc = new HashSet<ObjectId>();
      inc.addAll(include.values());
      for (final RevCommit r : assume)
        exc.add(r.getId());
      packWriter.setDeltaBaseAsOffset(true);
      packWriter.setThin(exc.size() > 0);
      packWriter.setReuseValidatingObjects(false);
      if (exc.size() == 0)
        packWriter.setTagTargets(tagTargets);
      packWriter.preparePack(monitor, inc, exc);

      final Writer w = new OutputStreamWriter(os, Constants.CHARSET);
      w.write(TransportBundle.V2_BUNDLE_SIGNATURE);
      w.write('\n');

      final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
      for (final RevCommit a : assume) {
        w.write('-');
        a.copyTo(tmp, w);
        if (a.getRawBuffer() != null) {
          w.write(' ');
          w.write(a.getShortMessage());
        }
        w.write('\n');
      }
      for (final Map.Entry<String, ObjectId> e : include.entrySet()) {
        e.getValue().copyTo(tmp, w);
        w.write(' ');
        w.write(e.getKey());
        w.write('\n');
      }

      w.write('\n');
      w.flush();
      packWriter.writePack(monitor, monitor, os);
    } finally {
      packWriter.release();
    }
  }
View Full Code Here

    }

    PackConfig cfg = packConfig;
    if (cfg == null)
      cfg = new PackConfig(db);
    final PackWriter pw = new PackWriter(cfg, walk.getObjectReader());
    try {
      pw.setUseCachedPacks(true);
      pw.setReuseDeltaCommits(true);
      pw.setDeltaBaseAsOffset(options.contains(OPTION_OFS_DELTA));
      pw.setThin(options.contains(OPTION_THIN_PACK));
      pw.setReuseValidatingObjects(false);

      if (commonBase.isEmpty()) {
        Set<ObjectId> tagTargets = new HashSet<ObjectId>();
        for (Ref ref : refs.values()) {
          if (ref.getPeeledObjectId() != null)
            tagTargets.add(ref.getPeeledObjectId());
          else if (ref.getObjectId() == null)
            continue;
          else if (ref.getName().startsWith(Constants.R_HEADS))
            tagTargets.add(ref.getObjectId());
        }
        pw.setTagTargets(tagTargets);
      }

      RevWalk rw = walk;
      if (wantAll.isEmpty()) {
        pw.preparePack(pm, wantIds, commonBase);
      } else {
        walk.reset();

        ObjectWalk ow = walk.toObjectWalkWithSameObjects();
        pw.preparePack(pm, ow, wantAll, commonBase);
        rw = ow;
      }

      if (options.contains(OPTION_INCLUDE_TAG)) {
        for (Ref ref : refs.values()) {
          ObjectId objectId = ref.getObjectId();

          // If the object was already requested, skip it.
          if (wantAll.isEmpty()) {
            if (wantIds.contains(objectId))
              continue;
          } else {
            RevObject obj = rw.lookupOrNull(objectId);
            if (obj != null && obj.has(WANT))
              continue;
          }

          if (!ref.isPeeled())
            ref = db.peel(ref);

          ObjectId peeledId = ref.getPeeledObjectId();
          if (peeledId == null)
            continue;

          objectId = ref.getObjectId();
          if (pw.willInclude(peeledId) && !pw.willInclude(objectId))
            pw.addObject(rw.parseAny(objectId));
        }
      }

      pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut);
      statistics = pw.getStatistics();

      if (msgOut != null) {
        String msg = pw.getStatistics().getMessage() + '\n';
        msgOut.write(Constants.encode(msg));
        msgOut.flush();
      }

    } finally {
      pw.release();
    }

    if (sideband)
      pckOut.end();
View Full Code Here

  private PackFile writePack(Set<? extends ObjectId> want,
      Set<? extends ObjectId> have, Set<ObjectId> tagTargets,
      List<PackIndex> excludeObjects) throws IOException {
    File tmpPack = null;
    File tmpIdx = null;
    PackWriter pw = new PackWriter(repo);
    try {
      // prepare the PackWriter
      pw.setDeltaBaseAsOffset(true);
      pw.setReuseDeltaCommits(false);
      if (tagTargets != null)
        pw.setTagTargets(tagTargets);
      if (excludeObjects != null)
        for (PackIndex idx : excludeObjects)
          pw.excludeObjects(idx);
      pw.preparePack(pm, want, have);
      if (pw.getObjectCount() == 0)
        return null;

      // create temporary files
      String id = pw.computeName().getName();
      File packdir = new File(repo.getObjectsDirectory(), "pack");
      tmpPack = File.createTempFile("gc_", ".pack_tmp", packdir);
      tmpIdx = new File(packdir, tmpPack.getName().substring(0,
          tmpPack.getName().lastIndexOf('.'))
          + ".idx_tmp");

      if (!tmpIdx.createNewFile())
        throw new IOException(MessageFormat.format(
            JGitText.get().cannotCreateIndexfile, tmpIdx.getPath()));

      // write the packfile
      @SuppressWarnings("resource" /* java 7 */)
      FileChannel channel = new FileOutputStream(tmpPack).getChannel();
      OutputStream channelStream = Channels.newOutputStream(channel);
      try {
        pw.writePack(pm, pm, channelStream);
      } finally {
        channel.force(true);
        channelStream.close();
        channel.close();
      }

      // write the packindex
      @SuppressWarnings("resource")
      FileChannel idxChannel = new FileOutputStream(tmpIdx).getChannel();
      OutputStream idxStream = Channels.newOutputStream(idxChannel);
      try {
        pw.writeIndex(idxStream);
      } finally {
        idxChannel.force(true);
        idxStream.close();
        idxChannel.close();
      }

      // rename the temporary files to real files
      File realPack = nameFor(id, ".pack");
      tmpPack.setReadOnly();
      File realIdx = nameFor(id, ".idx");
      realIdx.setReadOnly();
      boolean delete = true;
      try {
        if (!tmpPack.renameTo(realPack))
          return null;
        delete = false;
        if (!tmpIdx.renameTo(realIdx)) {
          File newIdx = new File(realIdx.getParentFile(),
              realIdx.getName() + ".new");
          if (!tmpIdx.renameTo(newIdx))
            newIdx = tmpIdx;
          throw new IOException(MessageFormat.format(
              JGitText.get().panicCantRenameIndexFile, newIdx,
              realIdx));
        }
      } finally {
        if (delete && tmpPack.exists())
          tmpPack.delete();
        if (delete && tmpIdx.exists())
          tmpIdx.delete();
      }
      return repo.getObjectDatabase().openPack(realPack, realIdx);
    } finally {
      pw.release();
      if (tmpPack != null && tmpPack.exists())
        tmpPack.delete();
      if (tmpIdx != null && tmpIdx.exists())
        tmpIdx.delete();
    }
View Full Code Here

    }

    PackConfig cfg = packConfig;
    if (cfg == null)
      cfg = new PackConfig(db);
    final PackWriter pw = new PackWriter(cfg, walk.getObjectReader());
    try {
      pw.setUseCachedPacks(true);
      pw.setReuseDeltaCommits(true);
      pw.setDeltaBaseAsOffset(options.contains(OPTION_OFS_DELTA));
      pw.setThin(options.contains(OPTION_THIN_PACK));
      pw.setReuseValidatingObjects(false);

      if (commonBase.isEmpty() && refs != null) {
        Set<ObjectId> tagTargets = new HashSet<ObjectId>();
        for (Ref ref : refs.values()) {
          if (ref.getPeeledObjectId() != null)
            tagTargets.add(ref.getPeeledObjectId());
          else if (ref.getObjectId() == null)
            continue;
          else if (ref.getName().startsWith(Constants.R_HEADS))
            tagTargets.add(ref.getObjectId());
        }
        pw.setTagTargets(tagTargets);
      }

      if (depth > 0)
        pw.setShallowPack(depth, unshallowCommits);

      RevWalk rw = walk;
      if (wantAll.isEmpty()) {
        pw.preparePack(pm, wantIds, commonBase);
      } else {
        walk.reset();

        ObjectWalk ow = walk.toObjectWalkWithSameObjects();
        pw.preparePack(pm, ow, wantAll, commonBase);
        rw = ow;
      }

      if (options.contains(OPTION_INCLUDE_TAG) && refs != null) {
        for (Ref ref : refs.values()) {
          ObjectId objectId = ref.getObjectId();

          // If the object was already requested, skip it.
          if (wantAll.isEmpty()) {
            if (wantIds.contains(objectId))
              continue;
          } else {
            RevObject obj = rw.lookupOrNull(objectId);
            if (obj != null && obj.has(WANT))
              continue;
          }

          if (!ref.isPeeled())
            ref = db.peel(ref);

          ObjectId peeledId = ref.getPeeledObjectId();
          if (peeledId == null)
            continue;

          objectId = ref.getObjectId();
          if (pw.willInclude(peeledId) && !pw.willInclude(objectId))
            pw.addObject(rw.parseAny(objectId));
        }
      }

      pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut);
      statistics = pw.getStatistics();

      if (msgOut != null) {
        String msg = pw.getStatistics().getMessage() + '\n';
        msgOut.write(Constants.encode(msg));
        msgOut.flush();
      }

    } finally {
      pw.release();
    }

    if (sideband)
      pckOut.end();
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.storage.pack.PackWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.