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

Examples of org.eclipse.jgit.internal.storage.pack.PackWriter$Statistics$ObjectType


              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

      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) {
          List<DfsPackDescription> remove = toPrune();
          if (remove.size() > 0)
            objdb.commitPack(
                Collections.<DfsPackDescription>emptyList(),
                remove);
          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 {
      rw = null;
      ctx.release();
    }
View Full Code Here

    if (db.getObjectDatabase() instanceof ObjectDirectory) {
      ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase();
      NullProgressMonitor m = NullProgressMonitor.INSTANCE;

      final File pack, idx;
      PackWriter pw = new PackWriter(db);
      try {
        Set<ObjectId> all = new HashSet<ObjectId>();
        for (Ref r : db.getAllRefs().values())
          all.add(r.getObjectId());
        pw.preparePack(m, all, Collections.<ObjectId> emptySet());

        final ObjectId name = pw.computeName();
        OutputStream out;

        pack = nameFor(odb, name, ".pack");
        out = new SafeBufferedOutputStream(new FileOutputStream(pack));
        try {
          pw.writePack(m, m, out);
        } finally {
          out.close();
        }
        pack.setReadOnly();

        idx = nameFor(odb, name, ".idx");
        out = new SafeBufferedOutputStream(new FileOutputStream(idx));
        try {
          pw.writeIndex(out);
        } finally {
          out.close();
        }
        idx.setReadOnly();
      } finally {
        pw.release();
      }

      odb.openPack(pack);
      updateServerInfo();
      prunePacked(odb);
View Full Code Here

   *
   * @throws IOException
   */
  @Test
  public void testContructor() throws IOException {
    writer = new PackWriter(config, db.newObjectReader());
    assertFalse(writer.isDeltaBaseAsOffset());
    assertTrue(config.isReuseDeltas());
    assertTrue(config.isReuseObjects());
    assertEquals(0, writer.getObjectCount());
  }
View Full Code Here

    config.setDeltaBaseAsOffset(false);
    assertFalse(config.isReuseDeltas());
    assertFalse(config.isReuseObjects());
    assertFalse(config.isDeltaBaseAsOffset());

    writer = new PackWriter(config, db.newObjectReader());
    writer.setDeltaBaseAsOffset(true);
    assertTrue(writer.isDeltaBaseAsOffset());
    assertFalse(config.isDeltaBaseAsOffset());
  }
View Full Code Here

  }

  private static PackIndex writePack(FileRepository repo,
      Set<? extends ObjectId> want, Set<ObjectIdSet> excludeObjects)
      throws IOException {
    PackWriter pw = new PackWriter(repo);
    pw.setDeltaBaseAsOffset(true);
    pw.setReuseDeltaCommits(false);
    for (ObjectIdSet idx : excludeObjects)
      pw.excludeObjects(idx);
    pw.preparePack(NullProgressMonitor.INSTANCE, want,
        Collections.<ObjectId> emptySet());
    String id = pw.computeName().getName();
    File packdir = new File(repo.getObjectsDirectory(), "pack");
    File packFile = new File(packdir, "pack-" + id + ".pack");
    FileOutputStream packOS = new FileOutputStream(packFile);
    pw.writePack(NullProgressMonitor.INSTANCE,
        NullProgressMonitor.INSTANCE, packOS);
    packOS.close();
    File idxFile = new File(packdir, "pack-" + id + ".idx");
    FileOutputStream idxOS = new FileOutputStream(idxFile);
    pw.writeIndex(idxOS);
    idxOS.close();
    pw.release();
    return PackIndex.open(idxFile);
  }
View Full Code Here

TOP

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

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.