Skip to content

Commit

Permalink
Recreated the PiecesTracker for optimal performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 3, 2015
1 parent 7d3b43f commit 7fd55fd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 115 deletions.
24 changes: 12 additions & 12 deletions src/com/frostwire/jlibtorrent/FileEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public String getPath() {

/**
* The path which this is a symlink to, or empty if this is
* not a symlink. This field is only used if the ``symlink_attribute`` is set.
* not a symlink. This field is only used if the {@code symlink_attribute} is set.
*
* @return
*/
Expand All @@ -50,7 +50,7 @@ public long getOffset() {
}

/**
* the size of the file (in bytes) and ``offset`` is the byte offset
* The size of the file (in bytes) and {@code offset} is the byte offset
* of the file within the torrent. i.e. the sum of all the sizes of the files
* before it in the list.
*
Expand All @@ -61,10 +61,10 @@ public long getSize() {
}

/**
* the offset in the file where the storage should start. The normal
* The offset in the file where the storage should start. The normal
* case is to have this set to 0, so that the storage starts saving data at the start
* if the file. In cases where multiple files are mapped into the same file though,
* the ``file_base`` should be set to an offset so that the different regions do
* the {@code file_base} should be set to an offset so that the different regions do
* not overlap. This is used when mapping "unselected" files into a so-called part
* file.
*
Expand All @@ -75,7 +75,7 @@ public long getFileBase() {
}

/**
* the modification time of this file specified in posix time.
* The modification time of this file specified in posix time.
*
* @return
*/
Expand All @@ -84,7 +84,7 @@ public int getMTime() {
}

/**
* a sha-1 hash of the content of the file, or zeroes, if no
* The sha-1 hash of the content of the file, or zeroes, if no
* file hash was present in the torrent file. It can be used to potentially
* find alternative sources for the file.
*
Expand All @@ -95,9 +95,9 @@ public Sha1Hash getFileHash() {
}

/**
* set to true for files that are not part of the data of the torrent.
* Set to true for files that are not part of the data of the torrent.
* They are just there to make sure the next file is aligned to a particular byte offset
* or piece boundry. These files should typically be hidden from an end user. They are
* or piece boundary. These files should typically be hidden from an end user. They are
* not written to disk.
*
* @return
Expand All @@ -107,7 +107,7 @@ public boolean isPadFile() {
}

/**
* true if the file was marked as hidden (on windows).
* True if the file was marked as hidden (on windows).
*
* @return
*/
Expand All @@ -116,7 +116,7 @@ public boolean hasHiddenAttribute() {
}

/**
* true if the file was marked as executable (posix)
* True if the file was marked as executable (posix)
*
* @return
*/
Expand All @@ -125,8 +125,8 @@ public boolean hasExecutableAttribute() {
}

/**
* true if the file was a symlink. If this is the case
* the ``symlink_index`` refers to a string which specifies the original location
* True if the file was a symlink. If this is the case
* the {@code symlink_index} refers to a string which specifies the original location
* where the data for this file was found.
*
* @return
Expand Down
80 changes: 0 additions & 80 deletions src/com/frostwire/jlibtorrent/FileSliceTracker.java

This file was deleted.

84 changes: 84 additions & 0 deletions src/com/frostwire/jlibtorrent/PiecesTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.frostwire.jlibtorrent;

import java.util.ArrayList;

/**
* @author gubatron
* @author aldenml
*/
public final class PiecesTracker {

private final int numFiles;
private final int numPieces;

private final int[][] files;
private final long[][] sizes;
private final boolean[] complete;

public PiecesTracker(TorrentInfo ti) {
this.numFiles = ti.getNumFiles();
this.numPieces = ti.getNumPieces();

this.files = new int[numFiles][];
this.sizes = new long[numFiles][];
this.complete = new boolean[numPieces];

for (int fileIndex = 0; fileIndex < numFiles; fileIndex++) {
FileEntry fe = ti.getFileAt(fileIndex);
long size = fe.getSize();
int firstPiece = ti.mapFile(fileIndex, 0, 1).getPiece();
int lastPiece = ti.mapFile(fileIndex, size - 1, 1).getPiece();

int numSlices = lastPiece - firstPiece + 1;
files[fileIndex] = new int[numSlices];
sizes[fileIndex] = new long[numSlices];

for (int pieceIndex = firstPiece; pieceIndex <= lastPiece; pieceIndex++) {
int index = pieceIndex - firstPiece;

files[fileIndex][index] = pieceIndex;

ArrayList<FileSlice> slices = ti.mapBlock(pieceIndex, 0, ti.getPieceSize(pieceIndex));
for (FileSlice slice : slices) {
if (slice.getFileIndex() == fileIndex) {
sizes[fileIndex][index] = slice.getSize();
}
}
}
}
}

public int getNumFiles() {
return numFiles;
}

public int getNumPieces() {
return numPieces;
}

public boolean isComplete(int pieceIndex) throws IllegalArgumentException {
return complete[pieceIndex];
}

public void setComplete(int pieceIndex, boolean complete) throws IllegalArgumentException {
this.complete[pieceIndex] = complete;
}

public long getSequentialDownloaded(int fileIndex) {
int[] pieces = files[fileIndex];

long downloaded = 0;

for (int i = 0; i < pieces.length; i++) {
int pieceIndex = pieces[i];

if (complete[pieceIndex]) {
downloaded += sizes[fileIndex][i];
} else {
break;
}
}

return downloaded;
}
}
30 changes: 7 additions & 23 deletions src/com/frostwire/jlibtorrent/demo/PieceMap.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.frostwire.jlibtorrent.demo;

import com.frostwire.jlibtorrent.FileSlice;
import com.frostwire.jlibtorrent.FileSliceTracker;
import com.frostwire.jlibtorrent.PiecesTracker;
import com.frostwire.jlibtorrent.TorrentInfo;

import java.io.File;
import java.util.ArrayList;

/**
* @author gubatron
Expand All @@ -23,33 +21,19 @@ public static void main(String[] args) throws Throwable {

TorrentInfo ti = new TorrentInfo(torrentFile);

int numFiles = ti.getNumFiles();
int numPieces = ti.getNumPieces();

System.out.println("Num Pieces: " + numPieces);

FileSliceTracker[] trackers = new FileSliceTracker[ti.getNumFiles()];
PiecesTracker tracker = new PiecesTracker(ti);

for (int i = 0; i < numPieces; i++) {
ArrayList<FileSlice> slices = ti.mapBlock(i, 0, ti.getPieceSize(i));
for (FileSlice slice : slices) {
int fileIndex = slice.getFileIndex();

if (trackers[fileIndex] == null) {
trackers[fileIndex] = new FileSliceTracker(fileIndex);
}

trackers[fileIndex].addSlice(i, slice);
}
for (int i = 0; i < numPieces / 2; i++) {
tracker.setComplete(i, true);
}

int totalSlices = 0;

for (FileSliceTracker tracker : trackers) {
int numSlices = tracker.getNumSlices();
System.out.println("File Index: " + tracker.getFileIndex() + ", slices: " + numSlices);
totalSlices = totalSlices + numSlices;
for (int i = 0; i < numFiles; i++) {
System.out.println("File index (seq)completed: " + tracker.getSequentialDownloaded(i));
}

System.out.println("Total Slices: " + totalSlices);
}
}

0 comments on commit 7fd55fd

Please sign in to comment.