-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ChunkMetadata, added LocalChunkCache example
- Loading branch information
Showing
5 changed files
with
172 additions
and
33 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
de.zabuza.fastcdc4j.examples/src/de/zabuza/fastcdc4j/examples/LocalChunkCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package de.zabuza.fastcdc4j.examples; | ||
|
||
import de.zabuza.fastcdc4j.external.chunking.Chunk; | ||
import de.zabuza.fastcdc4j.external.chunking.ChunkerBuilder; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Class offering a {@link #main(String[])} method that chunks a given build and populates a local chunk cache. | ||
* | ||
* @author Daniel Tischner {@literal <[email protected]>} | ||
*/ | ||
@SuppressWarnings({ "UseOfSystemOutOrSystemErr", "ClassIndependentOfModule", "ClassOnlyUsedInOneModule" }) | ||
enum LocalChunkCache { | ||
; | ||
|
||
/** | ||
* Starts the application. | ||
* | ||
* @param args Two arguments, the path to the build and the path to the local chunk cache | ||
*/ | ||
public static void main(final String[] args) throws IOException { | ||
if (args.length != 2) { | ||
throw new IllegalArgumentException( | ||
"Expected two arguments buildPath and cachePath, where buildPath denotes the path to the build and cachePath the path to the local chunk cache."); | ||
} | ||
final Path buildPath = Path.of(args[0]); | ||
final Path cachePath = Path.of(args[1]); | ||
|
||
int cachedChunks = 0; | ||
int uncachedChunks = 0; | ||
|
||
final var chunker = new ChunkerBuilder().build(); | ||
final var chunks = chunker.chunk(buildPath); | ||
for (final Chunk chunk : chunks) { | ||
final Path chunkPath = cachePath.resolve(chunk.getHexHash()); | ||
if (Files.exists(chunkPath)) { | ||
cachedChunks++; | ||
} else { | ||
Files.write(chunkPath, chunk.getData()); | ||
uncachedChunks++; | ||
} | ||
} | ||
|
||
System.out.printf("%d cached chunks, %d uncached chunks%n", cachedChunks, uncachedChunks); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
de.zabuza.fastcdc4j/src/de/zabuza/fastcdc4j/external/chunking/ChunkMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.zabuza.fastcdc4j.external.chunking; | ||
|
||
/** | ||
* Interface representing metadata of a chunk as created by a {@link Chunker}. | ||
* <p> | ||
* Unlike a {@link Chunk}, metadata does not own their data. | ||
* | ||
* @author Daniel Tischner {@literal <[email protected]>} | ||
*/ | ||
public interface ChunkMetadata { | ||
/** | ||
* Gets the offset of this chunk, with respect to its source data stream. | ||
* | ||
* @return The offset | ||
*/ | ||
long getOffset(); | ||
|
||
/** | ||
* The length of this chunk, i.e. the amount of contained data. | ||
* | ||
* @return Gets the length | ||
*/ | ||
int getLength(); | ||
|
||
/** | ||
* A binary hash representation of the contained data. Using the algorithm specified during construction by the | ||
* {@link Chunker}. | ||
* | ||
* @return A binary hash representation | ||
*/ | ||
byte[] getHash(); | ||
|
||
/** | ||
* A hexadecimal hash representation of the contained data. Using the algorithm specified during construction by the | ||
* {@link Chunker}. | ||
* | ||
* @return A hexadecimal hash representation | ||
*/ | ||
String getHexHash(); | ||
} |
68 changes: 68 additions & 0 deletions
68
de.zabuza.fastcdc4j/src/de/zabuza/fastcdc4j/internal/chunking/SimpleChunkMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package de.zabuza.fastcdc4j.internal.chunking; | ||
|
||
import de.zabuza.fastcdc4j.external.chunking.ChunkMetadata; | ||
|
||
/** | ||
* Implementation of a simple chunk metadata, wrapping given data. | ||
* | ||
* @author Daniel Tischner {@literal <[email protected]>} | ||
*/ | ||
public final class SimpleChunkMetadata implements ChunkMetadata { | ||
/** | ||
* The offset of this chunk, with respect to its source data stream. | ||
*/ | ||
private final long offset; | ||
/** | ||
* The length of this chunk, i.e. the amount of contained data. | ||
*/ | ||
private final int length; | ||
/** | ||
* A binary hash representation of the contained data. Using the algorithm specified during construction by the | ||
* {@link de.zabuza.fastcdc4j.external.chunking.Chunker}. | ||
*/ | ||
private final byte[] hash; | ||
/** | ||
* A hexadecimal hash representation of the contained data. Using the algorithm specified during construction by the | ||
* {@link de.zabuza.fastcdc4j.external.chunking.Chunker}. | ||
*/ | ||
private final String hexHash; | ||
|
||
/** | ||
* Creates a new simple chunk. | ||
* | ||
* @param offset The offset of this chunk, with respect to its source data stream | ||
* @param length The length of this chunk, i.e. the amount of contained data | ||
* @param hash A binary hash representation of the contained data. Using the algorithm specified during | ||
* construction by the {@link de.zabuza.fastcdc4j.external.chunking.Chunker}. | ||
* @param hexHash A hexadecimal hash representation of the contained data. Using the algorithm specified during | ||
* construction by the {@link de.zabuza.fastcdc4j.external.chunking.Chunker}. | ||
*/ | ||
public SimpleChunkMetadata(final long offset, final int length, final byte[] hash, final String hexHash) { | ||
this.offset = offset; | ||
this.length = length; | ||
//noinspection AssignmentOrReturnOfFieldWithMutableType | ||
this.hash = hash; | ||
this.hexHash = hexHash; | ||
} | ||
|
||
@Override | ||
public long getOffset() { | ||
return offset; | ||
} | ||
|
||
@Override | ||
public int getLength() { | ||
return length; | ||
} | ||
|
||
@Override | ||
public byte[] getHash() { | ||
//noinspection AssignmentOrReturnOfFieldWithMutableType | ||
return hash; | ||
} | ||
|
||
@Override | ||
public String getHexHash() { | ||
return hexHash; | ||
} | ||
} |