Package org.eclipse.jgit.internal.storage.pack

Examples of org.eclipse.jgit.internal.storage.pack.PackWriter$State


  private void packHeads(ProgressMonitor pm) throws IOException {
    if (allHeads.isEmpty())
      return;

    PackWriter pw = newPackWriter();
    try {
      pw.setTagTargets(tagTargets);
      pw.preparePack(pm, allHeads, Collections.<ObjectId> emptySet());
      if (0 < pw.getObjectCount())
        writePack(GC, pw, pm);
    } finally {
      pw.release();
    }
  }
View Full Code Here


  private void packRest(ProgressMonitor pm) throws IOException {
    if (nonHeads.isEmpty())
      return;

    PackWriter pw = newPackWriter();
    try {
      for (PackWriter.ObjectIdSet packedObjs : newPackObj)
        pw.excludeObjects(packedObjs);
      pw.preparePack(pm, nonHeads, allHeads);
      if (0 < pw.getObjectCount())
        writePack(GC, pw, pm);
    } finally {
      pw.release();
    }
  }
View Full Code Here

    cfg.setReuseDeltas(true);
    cfg.setReuseObjects(true);
    cfg.setDeltaCompress(false);
    cfg.setBuildBitmaps(false);

    PackWriter pw = new PackWriter(cfg, ctx);
    pw.setDeltaBaseAsOffset(true);
    pw.setReuseDeltaCommits(true);
    try {
      RevWalk pool = new RevWalk(ctx);
      pm.beginTask("Finding garbage", objectsBefore());
      for (DfsPackFile oldPack : packsBefore) {
        PackIndex oldIdx = oldPack.getPackIndex(ctx);
        for (PackIndex.MutableEntry ent : oldIdx) {
          pm.update(1);
          ObjectId id = ent.toObjectId();
          if (pool.lookupOrNull(id) != null || anyPackHas(id))
            continue;

          int type = oldPack.getObjectType(ctx, ent.getOffset());
          pw.addObject(pool.lookupAny(id, type));
        }
      }
      pm.endTask();
      if (0 < pw.getObjectCount())
        writePack(UNREACHABLE_GARBAGE, pw, pm);
    } finally {
      pw.release();
    }
  }
View Full Code Here

      cnt += p.getPackDescription().getObjectCount();
    return cnt;
  }

  private PackWriter newPackWriter() {
    PackWriter pw = new PackWriter(packConfig, ctx);
    pw.setDeltaBaseAsOffset(true);
    pw.setReuseDeltaCommits(false);
    return pw;
  }
View Full Code Here

    }

    PackConfig cfg = packConfig;
    if (cfg == null)
      cfg = new PackConfig(db);
    final PackWriter pw = new PackWriter(cfg, walk.getObjectReader());
    try {
      pw.setIndexDisabled(true);
      pw.setUseCachedPacks(true);
      pw.setUseBitmaps(depth == 0 && clientShallowCommits.isEmpty());
      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);

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

    } finally {
      statistics = pw.getStatistics();
      if (statistics != null)
        logger.onPackStatistics(statistics);
      pw.release();
    }

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

              return -1;
            return Integer.signum(o1.hashCode() - o2.hashCode());
          }

        });
    PackWriter pw = new PackWriter((pconfig == null) ? new PackConfig(repo) : pconfig, repo.newObjectReader());
    try {
      // prepare the PackWriter
      pw.setDeltaBaseAsOffset(true);
      pw.setReuseDeltaCommits(false);
      if (tagTargets != null)
        pw.setTagTargets(tagTargets);
      if (excludeObjects != null)
        for (ObjectIdSet 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"); //$NON-NLS-1$
      tmpPack = File.createTempFile("gc_", ".pack_tmp", packdir); //$NON-NLS-1$ //$NON-NLS-2$
      final String tmpBase = tmpPack.getName()
          .substring(0, tmpPack.getName().lastIndexOf('.'));
      File tmpIdx = new File(packdir, tmpBase + ".idx_tmp"); //$NON-NLS-1$
      tmpExts.put(INDEX, tmpIdx);

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

      // write the packfile
      FileOutputStream fos = new FileOutputStream(tmpPack);
      FileChannel channel = fos.getChannel();
      OutputStream channelStream = Channels.newOutputStream(channel);
      try {
        pw.writePack(pm, pm, channelStream);
      } finally {
        channel.force(true);
        channelStream.close();
        fos.close();
      }

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

      if (pw.prepareBitmapIndex(pm)) {
        File tmpBitmapIdx = new File(packdir, tmpBase + ".bitmap_tmp"); //$NON-NLS-1$
        tmpExts.put(BITMAP_INDEX, tmpBitmapIdx);

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

        fos = new FileOutputStream(tmpBitmapIdx);
        idxChannel = fos.getChannel();
        idxStream = Channels.newOutputStream(idxChannel);
        try {
          pw.writeBitmapIndex(idxStream);
        } finally {
          idxChannel.force(true);
          idxStream.close();
          fos.close();
        }
      }

      // rename the temporary files to real files
      File realPack = nameFor(id, ".pack"); //$NON-NLS-1$

      // if the packfile already exists (because we are rewriting a
      // packfile for the same set of objects maybe with different
      // PackConfig) then make sure we get rid of all handles on the file.
      // Windows will not allow for rename otherwise.
      if (realPack.exists())
        for (PackFile p : repo.getObjectDatabase().getPacks())
          if (realPack.getPath().equals(p.getPackFile().getPath())) {
            p.close();
            break;
          }
      tmpPack.setReadOnly();
      boolean delete = true;
      try {
        FileUtils.rename(tmpPack, realPack);
        delete = false;
        for (Map.Entry<PackExt, File> tmpEntry : tmpExts.entrySet()) {
          File tmpExt = tmpEntry.getValue();
          tmpExt.setReadOnly();

          File realExt = nameFor(
              id, "." + tmpEntry.getKey().getExtension()); //$NON-NLS-1$
          try {
            FileUtils.rename(tmpExt, realExt);
          } catch (IOException e) {
            File newExt = new File(realExt.getParentFile(),
                realExt.getName() + ".new"); //$NON-NLS-1$
            if (!tmpExt.renameTo(newExt))
              newExt = tmpExt;
            throw new IOException(MessageFormat.format(
                JGitText.get().panicCantRenameIndexFile, newExt,
                realExt));
          }
        }

      } finally {
        if (delete) {
          if (tmpPack.exists())
            tmpPack.delete();
          for (File tmpExt : tmpExts.values()) {
            if (tmpExt.exists())
              tmpExt.delete();
          }
        }
      }
      return repo.getObjectDatabase().openPack(realPack);
    } finally {
      pw.release();
      if (tmpPack != null && tmpPack.exists())
        tmpPack.delete();
      for (File tmpExt : tmpExts.values()) {
        if (tmpExt.exists())
          tmpExt.delete();
View Full Code Here

  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()) {
        // only add objects that we actually have
        ObjectId oid = r.getObjectId();
        if (local.hasObject(oid))
          remoteObjects.add(oid);
      }
      remoteObjects.addAll(additionalHaves);
      for (final RemoteRefUpdate r : refUpdates.values()) {
        if (!ObjectId.zeroId().equals(r.getNewObjectId()))
          newObjects.add(r.getNewObjectId());
      }

      writer.setIndexDisabled(true);
      writer.setUseCachedPacks(true);
      writer.setUseBitmaps(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(); //$NON-NLS-1$
      final String packName = base + ".pack"; //$NON-NLS-1$
      pathPack = "pack/" + packName; //$NON-NLS-1$
      pathIdx = "pack/" + base + ".idx"; //$NON-NLS-1$ //$NON-NLS-2$

      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); //$NON-NLS-1$
      OutputStream os = dest.writeFile(pathPack, monitor, wt + "..pack"); //$NON-NLS-1$
      try {
        os = new SafeBufferedOutputStream(os);
        writer.writePack(monitor, monitor, os);
      } finally {
        os.close();
      }

      os = dest.writeFile(pathIdx, monitor, wt + "..idx"); //$NON-NLS-1$
      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

    // different pack under the same file name is partially broken. We
    // should also have a different file name because the list of objects
    // within the pack has been modified.
    //
    final RevObject o2 = writeBlob(eden, "o2");
    final PackWriter pw = new PackWriter(eden);
    pw.addObject(o2);
    pw.addObject(o1);
    write(out1, pw);
    pw.release();

    // Try the old name, then the new name. The old name should cause the
    // pack to reload when it opens and the index and pack mismatch.
    //
    assertEquals(o1.name(), parse(o1).name());
View Full Code Here

    return new RevWalk(db).parseAny(id);
  }

  private File[] pack(final Repository src, final RevObject... list)
      throws IOException {
    final PackWriter pw = new PackWriter(src);
    for (final RevObject o : list) {
      pw.addObject(o);
    }

    final ObjectId name = pw.computeName();
    final File packFile = fullPackFileName(name, ".pack");
    final File idxFile = fullPackFileName(name, ".idx");
    final File[] files = new File[] { packFile, idxFile };
    write(files, pw);
    pw.release();
    return files;
  }
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.internal.storage.pack.PackWriter$State

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.