Skip to content

Commit

Permalink
Automatic code cleanup.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 443787146
  • Loading branch information
Googler authored and copybara-github committed Apr 22, 2022
1 parent 14b5c41 commit 1f7affa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ public Splitter(int numberOfShards, int expectedSize) {
}

/**
* Forces mapping of the given entries to be that of the current shard.
* The estimated number of remaining entries to process is adjusted,
* by subtracting the number of as-of-yet unassigned entries from the
* filter.
* Forces mapping of the given entries to be that of the current shard. The estimated number of
* remaining entries to process is adjusted, by subtracting the number of as-of-yet unassigned
* entries from the filter.
*/
public void assign(Collection<String> filter) {
if (filter != null) {
Expand All @@ -64,33 +63,7 @@ public void assign(Collection<String> filter) {
}
}

/**
* Forces increment of the current shard. May be called externally.
* Typically right after calling {@link #assign(java.util.Collection)}.
*/
public void nextShard() {
if (shard < numberOfShards - 1) {
shard++;
size = 0;
addEntries(0);
}
}

/**
* Adjusts the number of estimated entries to be process by the given count.
*/
public void addEntries(int count) {
this.remaining += count;
idealSize = numberOfShards > shard ? remaining / (numberOfShards - shard) : remaining;
// Before you change this, please do the math.
// It's not always perfect, but designed to keep shards reasonably balanced in most cases.
int limit = Math.min(Math.min(10, (idealSize + 3) / 4), (int) Math.log(numberOfShards));
almostFull = idealSize - limit;
}

/**
* Assigns the given entry to an output file.
*/
/** Assigns the given entry to an output file. */
public int assign(String path) {
Preconditions.checkState(shard < numberOfShards, "Too many shards!");
Integer assignment = assigned.get(path);
Expand Down Expand Up @@ -135,4 +108,28 @@ public int assign(String path) {
size++;
return shard;
}

/**
* Forces increment of the current shard. May be called externally.
* Typically right after calling {@link #assign(java.util.Collection)}.
*/
public void nextShard() {
if (shard < numberOfShards - 1) {
shard++;
size = 0;
addEntries(0);
}
}

/**
* Adjusts the number of estimated entries to be process by the given count.
*/
public void addEntries(int count) {
this.remaining += count;
idealSize = numberOfShards > shard ? remaining / (numberOfShards - shard) : remaining;
// Before you change this, please do the math.
// It's not always perfect, but designed to keep shards reasonably balanced in most cases.
int limit = Math.min(Math.min(10, (idealSize + 3) / 4), (int) Math.log(numberOfShards));
almostFull = idealSize - limit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,11 @@ public ZipEntry entryAt(long offset) throws IOException {
/**
* Constructs a {@link ZipEntry} view of the entry at the location of the given header.
*
* @param header a previously located header. If (@code useDirectory} is set, this will
* attempt to lookup a corresponding directory entry. If there is none, and {@code ignoreDeleted}
* is also set, the return value will flag this entry with a
* {@code ZipEntry.Status.ENTRY_NOT_FOUND} status code.
*
* @return {@link ZipEntry} for the given location.
* @param header a previously located header. If (@code useDirectory} is set, this will attempt to
* lookup a corresponding directory entry. If there is none, and {@code ignoreDeleted} is also
* set, the return value will flag this entry with a {@code ZipEntry.Status.ENTRY_NOT_FOUND}
* status code.
* @return {@link ZipEntry} for the given location.
* @throws IOException
*/
public ZipEntry entryWith(LocalFileHeader header) throws IOException {
Expand All @@ -307,43 +306,6 @@ public ZipEntry entryWith(LocalFileHeader header) throws IOException {
return entryWith(header, dirEntry);
}

/**
* Scans for a data descriptor from a given offset.
*
* @param offset position where to start the search.
* @param dirEntry directory entry for validation, or {@code null}.
* @return A data descriptor view for the next position containing the data descriptor signature.
* @throws IOException
*/
public DataDescriptor descriptorFrom(final long offset, final DirectoryEntry dirEntry)
throws IOException {
int skipped = 0;
for (ByteBuffer buffer = getData(offset + skipped, MAX_HEADER_SIZE);
buffer.limit() >= 16; buffer = getData(offset + skipped, MAX_HEADER_SIZE)) {
int markerOffset = ScanUtil.scanTo(DATA_DESC_SIG, buffer);
if (markerOffset < 0) {
skipped += buffer.limit() - 3;
} else {
skipped += markerOffset;
return markerOffset == 0 ? descriptorIn(buffer, offset + skipped, dirEntry)
: descriptorAt(offset + skipped, dirEntry);
}
}
return null;
}

/**
* Creates a data descriptor view at a given offset.
*
* @param offset presumed location of data descriptor.
* @param dirEntry directory entry to use for validation, or {@code null}.
* @return a data descriptor view over the given file offset.
* @throws IOException
*/
public DataDescriptor descriptorAt(long offset, DirectoryEntry dirEntry) throws IOException {
return descriptorIn(getData(offset, 16), offset, dirEntry);
}

/**
* Constructs a zip entry object for the location of the given header, with the corresponding
* directory entry.
Expand Down Expand Up @@ -379,8 +341,10 @@ private ZipEntry entryWith(LocalFileHeader header, DirectoryEntry dirEntry) thro
content = getData(offset, sizeByDir);
DataDescriptor dataDesc = descriptorAt(offset + sizeByDir, dirEntry);
if (dataDesc != null) {
return zipEntry.withContent(content).withDescriptor(dataDesc).withCode(
ZipEntry.Status.ENTRY_OK);
return zipEntry
.withContent(content)
.withDescriptor(dataDesc)
.withCode(ZipEntry.Status.ENTRY_OK);
}
return zipEntry.withContent(content).withCode(ZipEntry.Status.NO_DATA_DESC);
}
Expand All @@ -404,13 +368,52 @@ private ZipEntry entryWith(LocalFileHeader header, DirectoryEntry dirEntry) thro
return zipEntry.withDescriptor(dataDesc).withCode(ZipEntry.Status.UNKNOWN_SIZE);
}
content = getData(offset, sizeByDesc);
return zipEntry.withContent(content).withDescriptor(dataDesc).withCode(
ZipEntry.Status.ENTRY_OK);
return zipEntry
.withContent(content)
.withDescriptor(dataDesc)
.withCode(ZipEntry.Status.ENTRY_OK);
}
}
return zipEntry.withCode(ZipEntry.Status.UNKNOWN_SIZE);
}

/**
* Scans for a data descriptor from a given offset.
*
* @param offset position where to start the search.
* @param dirEntry directory entry for validation, or {@code null}.
* @return A data descriptor view for the next position containing the data descriptor signature.
* @throws IOException
*/
public DataDescriptor descriptorFrom(final long offset, final DirectoryEntry dirEntry)
throws IOException {
int skipped = 0;
for (ByteBuffer buffer = getData(offset + skipped, MAX_HEADER_SIZE);
buffer.limit() >= 16; buffer = getData(offset + skipped, MAX_HEADER_SIZE)) {
int markerOffset = ScanUtil.scanTo(DATA_DESC_SIG, buffer);
if (markerOffset < 0) {
skipped += buffer.limit() - 3;
} else {
skipped += markerOffset;
return markerOffset == 0 ? descriptorIn(buffer, offset + skipped, dirEntry)
: descriptorAt(offset + skipped, dirEntry);
}
}
return null;
}

/**
* Creates a data descriptor view at a given offset.
*
* @param offset presumed location of data descriptor.
* @param dirEntry directory entry to use for validation, or {@code null}.
* @return a data descriptor view over the given file offset.
* @throws IOException
*/
public DataDescriptor descriptorAt(long offset, DirectoryEntry dirEntry) throws IOException {
return descriptorIn(getData(offset, 16), offset, dirEntry);
}

/**
* Constructs a local header view over a give byte buffer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void run() {
try {
fileChannel.write(buffer);
} catch (IOException ex) {
throw new IllegalStateException("Unexpected IOException writing to output channel");
throw new IllegalStateException("Unexpected IOException writing to output channel", ex);
}
}
}
Expand Down

0 comments on commit 1f7affa

Please sign in to comment.