diff --git a/.github/smoke/Smoke.java b/.github/smoke/Smoke.java index fee1f38..0bd294f 100644 --- a/.github/smoke/Smoke.java +++ b/.github/smoke/Smoke.java @@ -5,8 +5,8 @@ // can pin the released version and the per-arch native jar from the matrix. import io.github.dfa1.zstd.Zstd; -import io.github.dfa1.zstd.ZstdCompressCtx; -import io.github.dfa1.zstd.ZstdDecompressCtx; +import io.github.dfa1.zstd.ZstdCompressContext; +import io.github.dfa1.zstd.ZstdDecompressContext; import io.github.dfa1.zstd.ZstdDictionary; import java.util.ArrayList; @@ -38,8 +38,8 @@ public static void main(String[] args) { samples.add(("{\"id\":" + i + ",\"user\":\"user_" + (i % 100) + "\",\"event\":\"click\"}").getBytes()); } ZstdDictionary dict = ZstdDictionary.train(samples, 8 * 1024); - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] message = samples.get(7); byte[] dictCompressed = cctx.compress(message, dict); byte[] dictRestored = dctx.decompress(dictCompressed, message.length, dict); diff --git a/CHANGELOG.md b/CHANGELOG.md index 0798a3a..2e455d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,15 @@ All notable changes to this project are documented here. Format loosely follows [Keep a Changelog](https://keepachangelog.com/); versions are released as `v*` git tags, which trigger publication to Maven Central. +## [Unreleased] + +### Changed +- **Breaking:** renamed public types to spell out abbreviations, matching the + `Zstd` family and zstd's own prose + ("compression context", "dictionary"): `ZstdCompressCtx` → `ZstdCompressContext`, + `ZstdDecompressCtx` → `ZstdDecompressContext`, `ZstdCompressDict` → + `ZstdCompressDictionary`, `ZstdDecompressDict` → `ZstdDecompressDictionary`. + ## [0.6] - 2026-06-27 ### Added diff --git a/README.md b/README.md index 7e85a94..362520b 100644 --- a/README.md +++ b/README.md @@ -44,8 +44,8 @@ List samples = ...; // representative records ZstdDictionary dict = ZstdDictionary.train(samples, 8 * 1024); byte[] message = ...; -try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { +try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] frame = cctx.compress(message, dict); byte[] back = dctx.decompress(frame, message.length, dict); } @@ -58,8 +58,8 @@ import io.github.dfa1.zstd.*; import java.lang.foreign.*; try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment src = ...; // e.g. an mmap'd file slice MemorySegment frame = cctx.compress(arena, src); // off-heap → off-heap diff --git a/adr/0010-native-context-pool.md b/adr/0010-native-context-pool.md index 85fddae..c42f156 100644 --- a/adr/0010-native-context-pool.md +++ b/adr/0010-native-context-pool.md @@ -6,12 +6,12 @@ ## Context -`ZstdCompressCtx`/`ZstdDecompressCtx` wrap native CCtx/DCtx — expensive to +`ZstdCompressContext`/`ZstdDecompressContext` wrap native CCtx/DCtx — expensive to create (off-heap malloc + init) and **not thread-safe**. The library targets JDK 25, where virtual threads are the default concurrency model: many, cheap, short-lived. The question is how to reuse contexts across them. -`ThreadLocal` is the wrong answer under virtual threads: one +`ThreadLocal` is the wrong answer under virtual threads: one context per vthread means millions of native contexts (native-memory explosion), and short vthread lifetimes mean the cached context is never reused. Java's own guidance: **pool the scarce resource, not the thread.** diff --git a/benchmark/src/main/java/io/github/dfa1/zstd/bench/CompressBenchmark.java b/benchmark/src/main/java/io/github/dfa1/zstd/bench/CompressBenchmark.java index 68d5685..e8c445e 100644 --- a/benchmark/src/main/java/io/github/dfa1/zstd/bench/CompressBenchmark.java +++ b/benchmark/src/main/java/io/github/dfa1/zstd/bench/CompressBenchmark.java @@ -4,7 +4,7 @@ import io.airlift.compress.v3.zstd.ZstdJavaCompressor; import io.github.dfa1.zstd.Zstd; -import io.github.dfa1.zstd.ZstdCompressCtx; +import io.github.dfa1.zstd.ZstdCompressContext; import java.lang.foreign.Arena; import java.lang.foreign.MemorySegment; import java.util.concurrent.TimeUnit; @@ -43,7 +43,7 @@ public class CompressBenchmark { private byte[] src; - private ZstdCompressCtx ffmCtx; + private ZstdCompressContext ffmCtx; private byte[] ffmDst; private Arena arena; @@ -58,7 +58,7 @@ public void setup() { src = BenchData.generate(size); int bound = (int) Zstd.compressBound(size); - ffmCtx = new ZstdCompressCtx().level(level); + ffmCtx = new ZstdCompressContext().level(level); ffmDst = new byte[bound]; arena = Arena.ofConfined(); diff --git a/benchmark/src/main/java/io/github/dfa1/zstd/bench/DecompressBenchmark.java b/benchmark/src/main/java/io/github/dfa1/zstd/bench/DecompressBenchmark.java index 98fe97c..5a53697 100644 --- a/benchmark/src/main/java/io/github/dfa1/zstd/bench/DecompressBenchmark.java +++ b/benchmark/src/main/java/io/github/dfa1/zstd/bench/DecompressBenchmark.java @@ -4,7 +4,7 @@ import io.airlift.compress.v3.zstd.ZstdJavaDecompressor; import io.github.dfa1.zstd.Zstd; -import io.github.dfa1.zstd.ZstdDecompressCtx; +import io.github.dfa1.zstd.ZstdDecompressContext; import java.lang.foreign.Arena; import java.lang.foreign.MemorySegment; import java.util.concurrent.TimeUnit; @@ -41,7 +41,7 @@ public class DecompressBenchmark { private int originalSize; private byte[] frame; - private ZstdDecompressCtx ffmCtx; + private ZstdDecompressContext ffmCtx; private Arena arena; private MemorySegment frameSeg; @@ -55,7 +55,7 @@ public void setup() { originalSize = size; frame = Zstd.compress(BenchData.generate(size)); - ffmCtx = new ZstdDecompressCtx(); + ffmCtx = new ZstdDecompressContext(); arena = Arena.ofConfined(); frameSeg = arena.allocate(frame.length); diff --git a/benchmark/src/main/java/io/github/dfa1/zstd/bench/GoldenCorpusBenchmark.java b/benchmark/src/main/java/io/github/dfa1/zstd/bench/GoldenCorpusBenchmark.java index 9b79ebe..4b77968 100644 --- a/benchmark/src/main/java/io/github/dfa1/zstd/bench/GoldenCorpusBenchmark.java +++ b/benchmark/src/main/java/io/github/dfa1/zstd/bench/GoldenCorpusBenchmark.java @@ -3,8 +3,8 @@ import static java.lang.foreign.ValueLayout.JAVA_BYTE; import io.github.dfa1.zstd.Zstd; -import io.github.dfa1.zstd.ZstdCompressCtx; -import io.github.dfa1.zstd.ZstdDecompressCtx; +import io.github.dfa1.zstd.ZstdCompressContext; +import io.github.dfa1.zstd.ZstdDecompressContext; import java.io.UncheckedIOException; import java.io.IOException; import java.lang.foreign.Arena; @@ -61,8 +61,8 @@ public class GoldenCorpusBenchmark { private int bound; - private ZstdCompressCtx cctx; - private ZstdDecompressCtx dctx; + private ZstdCompressContext cctx; + private ZstdDecompressContext dctx; private byte[] compressDst; private Arena arena; @@ -88,8 +88,8 @@ public void setup() { srcSize = src.length; frame = Zstd.compress(src); - cctx = new ZstdCompressCtx().level(level); - dctx = new ZstdDecompressCtx(); + cctx = new ZstdCompressContext().level(level); + dctx = new ZstdDecompressContext(); bound = (int) Zstd.compressBound(srcSize); compressDst = new byte[bound]; diff --git a/docs/how-to.md b/docs/how-to.md index d29414c..4a9e728 100644 --- a/docs/how-to.md +++ b/docs/how-to.md @@ -8,8 +8,8 @@ Task-focused recipes. Each assumes you have the library on the classpath (see th Reuse a context to amortise native allocation across many calls: ```java -try (ZstdCompressCtx cctx = new ZstdCompressCtx().level(19); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { +try (ZstdCompressContext cctx = new ZstdCompressContext().level(19); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] packed = cctx.compress(message); byte[] restored = dctx.decompress(packed, message.length); } @@ -26,7 +26,7 @@ or to abort a half-written frame and start clean — without freeing and recreat it. Pick what to clear with `ZstdResetDirective`: ```java -try (ZstdCompressCtx cctx = new ZstdCompressCtx().level(19)) { +try (ZstdCompressContext cctx = new ZstdCompressContext().level(19)) { byte[] a = cctx.compress(first); // Cheap: drop any unflushed frame state, keep the level and parameters. @@ -39,7 +39,7 @@ try (ZstdCompressCtx cctx = new ZstdCompressCtx().level(19)) { } ``` -`ZstdDecompressCtx.reset(...)` works the same way. Reuse alone amortises +`ZstdDecompressContext.reset(...)` works the same way. Reuse alone amortises allocation; reset lets a long-lived or pooled context return to a known state without churning native memory. @@ -51,7 +51,7 @@ matching) set on the context. To combine the two, make the dictionary *sticky* with `loadDictionary` — then the normal `compress` path honours both: ```java -try (ZstdCompressCtx cctx = new ZstdCompressCtx().level(19).checksum(true)) { +try (ZstdCompressContext cctx = new ZstdCompressContext().level(19).checksum(true)) { cctx.loadDictionary(dict); // ZstdDictionary, or a native MemorySegment byte[] frame = cctx.compress(record); // dictionary + checksum, together } @@ -62,9 +62,9 @@ by reference — no per-call digesting, no copy. It pairs with `reset` for a pooled, recycled context: ```java -try (ZstdCompressDict cdict = dict.compressDict(19)) { +try (ZstdCompressDictionary cdict = dict.compressDict(19)) { // one cctx per pooled worker, all sharing the one digested dictionary - try (ZstdCompressCtx cctx = new ZstdCompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext()) { cctx.refDictionary(cdict); // borrowed; cdict must outlive cctx byte[] a = cctx.compress(first); cctx.reset(ZstdResetDirective.SESSION_ONLY); // recycle, keep the dictionary @@ -76,12 +76,12 @@ try (ZstdCompressDict cdict = dict.compressDict(19)) { `refDictionary` only borrows: the digested `cdict` is *not* tied to the context's lifetime, so it must be closed separately (hence its own try-with-resources). That is the price of sharing one digest across many contexts. If you have just **one** -context, don't build a `ZstdCompressDict` at all — `loadDictionary` above digests +context, don't build a `ZstdCompressDictionary` at all — `loadDictionary` above digests into the context and frees it for you, and a stray, never-closed -`ZstdCompressDict` is a native-memory leak. +`ZstdCompressDictionary` is a native-memory leak. A loaded or referenced dictionary stays until replaced, cleared with `null`, or -dropped by a parameter `reset`. `ZstdDecompressCtx` mirrors all of this. +dropped by a parameter `reset`. `ZstdDecompressContext` mirrors all of this. ## Compress many small payloads with a dictionary @@ -92,8 +92,8 @@ representative samples: ```java ZstdDictionary dict = ZstdDictionary.train(sampleRecords, 16 * 1024); -try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { +try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] packed = cctx.compress(record, dict); byte[] restored = dctx.decompress(packed, record.length, dict); } @@ -105,10 +105,10 @@ ZstdDictionary reloaded = ZstdDictionary.of(persisted); On a hot path, digest the dictionary once to skip per-call setup: ```java -try (ZstdCompressDict cdict = dict.compressDict(19); - ZstdDecompressDict ddict = dict.decompressDict(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { +try (ZstdCompressDictionary cdict = dict.compressDict(19); + ZstdDecompressDictionary ddict = dict.decompressDict(); + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] packed = cctx.compress(record, cdict); byte[] restored = dctx.decompress(packed, record.length, ddict); } @@ -122,7 +122,7 @@ hands zstd the segment address directly: no copy in, no copy out, no GC churn. ```java try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment frame = reader.mmapSlice(); // already native long n = Zstd.decompressedSize(frame); // read header, no copy MemorySegment out = arena.allocate(n); // becomes the backing buffer @@ -138,10 +138,10 @@ The segment-API map: | Operation | byte[] (convenience) | MemorySegment (boundary zero-copy) | |------------------------|-------------------------------------------------|----------------------------------------------------| -| compress | `ZstdCompressCtx.compress(byte[])` | `ZstdCompressCtx.compress(dst, src)` | -| compress + dict | `ZstdCompressCtx.compress(byte[], ZstdCompressDict)` | `ZstdCompressCtx.compress(dst, src, ZstdCompressDict)` | -| decompress | `ZstdDecompressCtx.decompress(byte[], int)` | `ZstdDecompressCtx.decompress(dst, src)` | -| decompress + dict | `ZstdDecompressCtx.decompress(byte[], int, ZstdDecompressDict)` | `ZstdDecompressCtx.decompress(dst, src, ZstdDecompressDict)` | +| compress | `ZstdCompressContext.compress(byte[])` | `ZstdCompressContext.compress(dst, src)` | +| compress + dict | `ZstdCompressContext.compress(byte[], ZstdCompressDictionary)` | `ZstdCompressContext.compress(dst, src, ZstdCompressDictionary)` | +| decompress | `ZstdDecompressContext.decompress(byte[], int)` | `ZstdDecompressContext.decompress(dst, src)` | +| decompress + dict | `ZstdDecompressContext.decompress(byte[], int, ZstdDecompressDictionary)` | `ZstdDecompressContext.decompress(dst, src, ZstdDecompressDictionary)` | | size output (no copy) | frame header via `Zstd.decompress(byte[])` | `Zstd.decompressedSize(MemorySegment)` | Size `dst` with `Zstd.compressBound(srcSize)` for compression, or @@ -177,7 +177,7 @@ direct buffer or a `byte[]` first. ```java try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext()) { ByteBuffer src = channel.map(READ_ONLY, 0, size, arena); // direct, off-heap MemorySegment in = MemorySegment.ofBuffer(src); // covers [position, limit) MemorySegment out = cctx.compress(arena, in); // arena-owned frame diff --git a/docs/reference.md b/docs/reference.md index 3f2ea26..e63fa7a 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -16,9 +16,9 @@ native artifact per platform: | Type | Role | |---|---| | `Zstd` | one-shot `compress` / `decompress`, level + version queries, `compressBound`, `decompressedSize` | -| `ZstdCompressCtx` / `ZstdDecompressCtx` | reusable contexts; `byte[]` and `MemorySegment` overloads, dictionary variants | +| `ZstdCompressContext` / `ZstdDecompressContext` | reusable contexts; `byte[]` and `MemorySegment` overloads, dictionary variants | | `ZstdDictionary` | train (`ZDICT`), load, persist, query dict id | -| `ZstdCompressDict` / `ZstdDecompressDict` | pre-digested dictionaries for hot paths | +| `ZstdCompressDictionary` / `ZstdDecompressDictionary` | pre-digested dictionaries for hot paths | | `ZstdFrame` | frame inspection: header, sizes, dict id, skippable frames | | `ZstdException` / `ZstdErrorCode` | typed errors mapped from zstd's sentinels | diff --git a/docs/supported.md b/docs/supported.md index 97e5f71..14be6f1 100644 --- a/docs/supported.md +++ b/docs/supported.md @@ -49,26 +49,26 @@ rather than the deprecated `ZSTD_getDecompressedSize`. | `ZSTD_versionNumber`, `ZSTD_versionString` | `Zstd.version` | | `ZSTD_isError`, `ZSTD_getErrorName` | internal error mapping in `Zstd` | | `ZSTD_getFrameContentSize` | `Zstd.decompress(byte[])`, `Zstd.decompressedSize` | -| `ZSTD_createCCtx`, `ZSTD_freeCCtx`, `ZSTD_compressCCtx` | `ZstdCompressCtx` | -| `ZSTD_createDCtx`, `ZSTD_freeDCtx`, `ZSTD_decompressDCtx` | `ZstdDecompressCtx` | -| `ZSTD_compress_usingDict` | `ZstdCompressCtx.compress(byte[], ZstdDictionary)` | -| `ZSTD_decompress_usingDict` | `ZstdDecompressCtx.decompress(byte[], int, ZstdDictionary)` | -| `ZSTD_createCDict`, `ZSTD_freeCDict`, `ZSTD_compress_usingCDict` | `ZstdCompressDict` | -| `ZSTD_createDDict`, `ZSTD_freeDDict`, `ZSTD_decompress_usingDDict` | `ZstdDecompressDict` | +| `ZSTD_createCCtx`, `ZSTD_freeCCtx`, `ZSTD_compressCCtx` | `ZstdCompressContext` | +| `ZSTD_createDCtx`, `ZSTD_freeDCtx`, `ZSTD_decompressDCtx` | `ZstdDecompressContext` | +| `ZSTD_compress_usingDict` | `ZstdCompressContext.compress(byte[], ZstdDictionary)` | +| `ZSTD_decompress_usingDict` | `ZstdDecompressContext.decompress(byte[], int, ZstdDictionary)` | +| `ZSTD_createCDict`, `ZSTD_freeCDict`, `ZSTD_compress_usingCDict` | `ZstdCompressDictionary` | +| `ZSTD_createDDict`, `ZSTD_freeDDict`, `ZSTD_decompress_usingDDict` | `ZstdDecompressDictionary` | | `ZDICT_trainFromBuffer` | `ZstdDictionary.train` | | `ZDICT_getDictID` | `ZstdDictionary.id` | | `ZDICT_isError`, `ZDICT_getErrorName` | internal error mapping in `ZstdDictionary` | | `ZSTD_compressStream2`, `ZSTD_CStreamInSize`, `ZSTD_CStreamOutSize`, `ZSTD_CCtx_setParameter` | `ZstdOutputStream` | | `ZSTD_decompressStream`, `ZSTD_DStreamInSize`, `ZSTD_DStreamOutSize` | `ZstdInputStream` | -| `ZSTD_compress2`, `ZSTD_CCtx_setParameter` | `ZstdCompressCtx.parameter` / `checksum` / `longDistanceMatching` / `windowLog` (all of `ZstdCompressParameter`) | -| `ZSTD_DCtx_setParameter` | `ZstdDecompressCtx.parameter` / `windowLogMax` (`ZstdDecompressParameter`) | +| `ZSTD_compress2`, `ZSTD_CCtx_setParameter` | `ZstdCompressContext.parameter` / `checksum` / `longDistanceMatching` / `windowLog` (all of `ZstdCompressParameter`) | +| `ZSTD_DCtx_setParameter` | `ZstdDecompressContext.parameter` / `windowLogMax` (`ZstdDecompressParameter`) | | `ZSTD_CCtx_setPledgedSrcSize` | `ZstdOutputStream.withPledgedSize` | -| `ZSTD_CCtx_reset`, `ZSTD_DCtx_reset` | `ZstdCompressCtx.reset` / `ZstdDecompressCtx.reset` (`ZstdResetDirective`) | -| `ZSTD_getDictID_fromCDict`, `ZSTD_getDictID_fromDDict` | `ZstdCompressDict.id()` / `ZstdDecompressDict.id()` | +| `ZSTD_CCtx_reset`, `ZSTD_DCtx_reset` | `ZstdCompressContext.reset` / `ZstdDecompressContext.reset` (`ZstdResetDirective`) | +| `ZSTD_getDictID_fromCDict`, `ZSTD_getDictID_fromDDict` | `ZstdCompressDictionary.id()` / `ZstdDecompressDictionary.id()` | | `ZSTD_getErrorString` | `ZstdErrorCode.description()` | | `ZSTD_cParam_getBounds`, `ZSTD_dParam_getBounds` | `ZstdCompressParameter.bounds()` / `ZstdDecompressParameter.bounds()` (`ZstdBounds`) | -| `ZSTD_CCtx_loadDictionary`, `ZSTD_DCtx_loadDictionary` | `ZstdCompressCtx.loadDictionary` / `ZstdDecompressCtx.loadDictionary`; `ZstdOutputStream` / `ZstdInputStream` dictionary constructors | -| `ZSTD_CCtx_refCDict`, `ZSTD_DCtx_refDDict` | `ZstdCompressCtx.refDictionary` / `ZstdDecompressCtx.refDictionary` | +| `ZSTD_CCtx_loadDictionary`, `ZSTD_DCtx_loadDictionary` | `ZstdCompressContext.loadDictionary` / `ZstdDecompressContext.loadDictionary`; `ZstdOutputStream` / `ZstdInputStream` dictionary constructors | +| `ZSTD_CCtx_refCDict`, `ZSTD_DCtx_refDDict` | `ZstdCompressContext.refDictionary` / `ZstdDecompressContext.refDictionary` | | `ZSTD_isFrame`, `ZSTD_findFrameCompressedSize`, `ZSTD_decompressBound`, `ZSTD_getDictID_fromFrame`, `ZSTD_getFrameHeader`, `ZSTD_isSkippableFrame`, `ZSTD_writeSkippableFrame`, `ZSTD_readSkippableFrame` | `ZstdFrame` (+ `ZstdFrameHeader`, `ZstdFrameType`, `ZstdSkippableContent`) | | `ZSTD_getErrorCode` | `ZstdException.code()` (+ `ZstdErrorCode`) | | `ZSTD_getFrameProgression` | `ZstdCompressStream.progress()` (`ZstdFrameProgression`) | diff --git a/docs/tutorial.md b/docs/tutorial.md index e0b29c7..cc2ded0 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -95,7 +95,7 @@ the `MemorySegment` overloads hand zstd the segment address directly, so there i ```java try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment frame = reader.mmapSlice(); // already native — never touches the heap long n = Zstd.decompressedSize(frame); // read the header, no copy MemorySegment out = arena.allocate(n); // this segment *is* the output buffer diff --git a/integration-tests/src/test/java/io/github/dfa1/zstd/it/GoldenCorpusTest.java b/integration-tests/src/test/java/io/github/dfa1/zstd/it/GoldenCorpusTest.java index 7e65733..e85a516 100644 --- a/integration-tests/src/test/java/io/github/dfa1/zstd/it/GoldenCorpusTest.java +++ b/integration-tests/src/test/java/io/github/dfa1/zstd/it/GoldenCorpusTest.java @@ -2,8 +2,8 @@ import io.github.dfa1.zstd.ZstdDictionaryId; import io.github.dfa1.zstd.Zstd; -import io.github.dfa1.zstd.ZstdCompressCtx; -import io.github.dfa1.zstd.ZstdDecompressCtx; +import io.github.dfa1.zstd.ZstdCompressContext; +import io.github.dfa1.zstd.ZstdDecompressContext; import io.github.dfa1.zstd.ZstdDictionary; import io.github.dfa1.zstd.ZstdException; import io.github.dfa1.zstd.ZstdFrame; @@ -209,7 +209,7 @@ void javaDictCompressJniDictDecompress(Path file) { byte[] data = payload(); ZstdDictionary dict = ZstdDictionary.of(raw); byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { frame = ctx.compress(data, dict); } @@ -231,7 +231,7 @@ void jniDictCompressJavaDictDecompress(Path file) { // When byte[] restored; - try (ZstdDecompressCtx ctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext ctx = new ZstdDecompressContext()) { restored = ctx.decompress(frame, data.length, ZstdDictionary.of(raw)); } @@ -246,7 +246,7 @@ void dictIdRidesWithFrame(Path file) { byte[] raw = read(file); ZstdDictionary dict = ZstdDictionary.of(raw); byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { frame = ctx.compress(payload(), dict); } diff --git a/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdInteropExtrasTest.java b/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdInteropExtrasTest.java index 456c244..3a9948f 100644 --- a/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdInteropExtrasTest.java +++ b/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdInteropExtrasTest.java @@ -69,7 +69,7 @@ class Checksum { void javaChecksumDecodedByJni() { // Given byte[] frame; - try (io.github.dfa1.zstd.ZstdCompressCtx ctx = new io.github.dfa1.zstd.ZstdCompressCtx()) { + try (io.github.dfa1.zstd.ZstdCompressContext ctx = new io.github.dfa1.zstd.ZstdCompressContext()) { frame = ctx.checksum(true).compress(data); } @@ -101,7 +101,7 @@ void jniChecksumDecodedByJava() { void javaRejectsCorruptedChecksum() { // Given byte[] frame; - try (io.github.dfa1.zstd.ZstdCompressCtx ctx = new io.github.dfa1.zstd.ZstdCompressCtx()) { + try (io.github.dfa1.zstd.ZstdCompressContext ctx = new io.github.dfa1.zstd.ZstdCompressContext()) { frame = ctx.checksum(true).compress(data); } frame[frame.length / 2] ^= 0x7F; diff --git a/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdJniInteropTest.java b/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdJniInteropTest.java index cc0e5ac..8ea9ad6 100644 --- a/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdJniInteropTest.java +++ b/integration-tests/src/test/java/io/github/dfa1/zstd/it/ZstdJniInteropTest.java @@ -3,9 +3,9 @@ import com.github.luben.zstd.ZstdDictCompress; import com.github.luben.zstd.ZstdDictDecompress; import io.github.dfa1.zstd.Zstd; -import io.github.dfa1.zstd.ZstdCompressCtx; -import io.github.dfa1.zstd.ZstdCompressDict; -import io.github.dfa1.zstd.ZstdDecompressCtx; +import io.github.dfa1.zstd.ZstdCompressContext; +import io.github.dfa1.zstd.ZstdCompressDictionary; +import io.github.dfa1.zstd.ZstdDecompressContext; import io.github.dfa1.zstd.ZstdDictionary; import io.github.dfa1.zstd.ZstdInputStream; import io.github.dfa1.zstd.ZstdOutputStream; @@ -103,7 +103,7 @@ void javaDictCompressJniDictDecompress() { byte[] sample = sample(11); byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { frame = ctx.compress(sample, dict); } ZstdDictDecompress jniDict = new ZstdDictDecompress(dict.toByteArray()); @@ -119,7 +119,7 @@ void jniDictCompressJavaDictDecompress() { byte[] frame = com.github.luben.zstd.Zstd.compress(sample, jniDict); byte[] restored; - try (ZstdDecompressCtx ctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext ctx = new ZstdDecompressContext()) { restored = ctx.decompress(frame, sample.length, dict); } assertThat(restored).isEqualTo(sample); @@ -134,7 +134,7 @@ void javaLoadedDictWithChecksumJniDictDecompress() { byte[] sample = sample(33); byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx().checksum(true)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().checksum(true)) { ctx.loadDictionary(dict); frame = ctx.compress(sample); } @@ -149,8 +149,8 @@ void javaReferencedDigestedDictJniDictDecompress() { byte[] sample = sample(44); byte[] frame; - try (ZstdCompressDict cdict = new ZstdCompressDict(dict, Zstd.defaultCompressionLevel()); - ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressDictionary cdict = new ZstdCompressDictionary(dict, Zstd.defaultCompressionLevel()); + ZstdCompressContext ctx = new ZstdCompressContext()) { ctx.refDictionary(cdict); frame = ctx.compress(sample); } diff --git a/zstd/src/main/java/io/github/dfa1/zstd/Zstd.java b/zstd/src/main/java/io/github/dfa1/zstd/Zstd.java index 7e5a035..fa3b463 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/Zstd.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/Zstd.java @@ -10,8 +10,8 @@ /// One-shot, stateless zstd compression and decompression over `byte[]`. /// /// Every method is thread-safe and allocates its own native scratch buffers. -/// For repeated calls on a hot path, reuse a [ZstdCompressCtx] / -/// [ZstdDecompressCtx] instead to avoid re-allocating per call. +/// For repeated calls on a hot path, reuse a [ZstdCompressContext] / +/// [ZstdDecompressContext] instead to avoid re-allocating per call. /// /// {@snippet : /// byte[] packed = Zstd.compress("hello world".getBytes()); @@ -112,7 +112,7 @@ public static byte[] decompress(byte[] compressed, int maxSize) { /// Decompressed size recorded in a zstd frame's header, read directly from a /// native [MemorySegment] with no copy — use it to size the destination - /// for the zero-copy [ZstdDecompressCtx#decompress(MemorySegment, MemorySegment)]. + /// for the zero-copy [ZstdDecompressContext#decompress(MemorySegment, MemorySegment)]. /// /// @param frame a complete zstd frame /// @return the decompressed length in bytes diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressContext.java similarity index 90% rename from zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java rename to zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressContext.java index ca31114..00bd34f 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressContext.java @@ -11,18 +11,18 @@ /// on hot paths. Not thread-safe: confine an instance to one thread or pool it. /// /// {@snippet : -/// try (ZstdCompressCtx ctx = new ZstdCompressCtx().level(19)) { +/// try (ZstdCompressContext ctx = new ZstdCompressContext().level(19)) { /// for (byte[] msg : messages) { /// sink.accept(ctx.compress(msg)); /// } /// } /// } -public final class ZstdCompressCtx extends NativeObject { +public final class ZstdCompressContext extends NativeObject { private int level; /// Creates a new compression context at the default level. - public ZstdCompressCtx() { + public ZstdCompressContext() { super(create()); this.level = Zstd.defaultCompressionLevel(); } @@ -35,7 +35,7 @@ private static MemorySegment create() { /// /// @param level the compression level to use /// @return `this`, for chaining - public ZstdCompressCtx level(int level) { + public ZstdCompressContext level(int level) { this.level = level; setParam(ZstdCompressParameter.COMPRESSION_LEVEL, level); return this; @@ -51,7 +51,7 @@ public ZstdCompressCtx level(int level) { /// @param value the value, validated natively against the parameter's bounds /// @return `this`, for chaining /// @throws ZstdException if the value is out of range for the parameter - public ZstdCompressCtx parameter(ZstdCompressParameter parameter, int value) { + public ZstdCompressContext parameter(ZstdCompressParameter parameter, int value) { setParam(parameter, value); return this; } @@ -61,7 +61,7 @@ public ZstdCompressCtx parameter(ZstdCompressParameter parameter, int value) { /// /// @param enabled whether to write a checksum /// @return `this`, for chaining - public ZstdCompressCtx checksum(boolean enabled) { + public ZstdCompressContext checksum(boolean enabled) { return parameter(ZstdCompressParameter.CHECKSUM_FLAG, enabled ? 1 : 0); } @@ -69,7 +69,7 @@ public ZstdCompressCtx checksum(boolean enabled) { /// /// @param enabled whether to enable long-distance matching /// @return `this`, for chaining - public ZstdCompressCtx longDistanceMatching(boolean enabled) { + public ZstdCompressContext longDistanceMatching(boolean enabled) { return parameter(ZstdCompressParameter.ENABLE_LONG_DISTANCE_MATCHING, enabled ? 1 : 0); } @@ -79,7 +79,7 @@ public ZstdCompressCtx longDistanceMatching(boolean enabled) { /// /// @param windowLog the base-2 log of the window size /// @return `this`, for chaining - public ZstdCompressCtx windowLog(int windowLog) { + public ZstdCompressContext windowLog(int windowLog) { return parameter(ZstdCompressParameter.WINDOW_LOG, windowLog); } @@ -98,7 +98,7 @@ public ZstdCompressCtx windowLog(int windowLog) { /// @param directive what to clear /// @return `this`, for chaining /// @throws ZstdException if the reset fails natively - public ZstdCompressCtx reset(ZstdResetDirective directive) { + public ZstdCompressContext reset(ZstdResetDirective directive) { Objects.requireNonNull(directive, "directive"); NativeCall.checkReturnValue(() -> (long) Bindings.CCTX_RESET.invokeExact(ptr(), directive.value())); if (directive != ZstdResetDirective.SESSION_ONLY) { @@ -120,12 +120,12 @@ public ZstdCompressCtx reset(ZstdResetDirective directive) { /// It stays loaded until replaced, cleared with [#loadDictionary(ZstdDictionary)] /// passing `null`, or dropped by a parameter [#reset(ZstdResetDirective)]. For /// a dictionary reused across many contexts, digest it once and attach it with - /// [#refDictionary(ZstdCompressDict)] instead. + /// [#refDictionary(ZstdCompressDictionary)] instead. /// /// @param dict the dictionary to load, or `null` to clear the loaded dictionary /// @return `this`, for chaining /// @throws ZstdException if the dictionary cannot be loaded - public ZstdCompressCtx loadDictionary(ZstdDictionary dict) { + public ZstdCompressContext loadDictionary(ZstdDictionary dict) { if (dict == null) { return loadDictionary(MemorySegment.NULL, 0L); } @@ -144,7 +144,7 @@ public ZstdCompressCtx loadDictionary(ZstdDictionary dict) { /// context), or `null` / [MemorySegment#NULL] to clear the loaded dictionary /// @return `this`, for chaining /// @throws ZstdException if the dictionary cannot be loaded - public ZstdCompressCtx loadDictionary(MemorySegment dict) { + public ZstdCompressContext loadDictionary(MemorySegment dict) { if (NativeCall.isNull(dict)) { return loadDictionary(MemorySegment.NULL, 0L); } @@ -152,7 +152,7 @@ public ZstdCompressCtx loadDictionary(MemorySegment dict) { return loadDictionary(dict, dict.byteSize()); } - private ZstdCompressCtx loadDictionary(MemorySegment dict, long size) { + private ZstdCompressContext loadDictionary(MemorySegment dict, long size) { NativeCall.checkReturnValue(() -> (long) Bindings.CCTX_LOAD_DICTIONARY.invokeExact(ptr(), dict, size)); return this; } @@ -161,7 +161,7 @@ private ZstdCompressCtx loadDictionary(MemorySegment dict, long size) { /// digesting and no copy. Subsequent [#compress(byte[])] / /// [#compress(MemorySegment, MemorySegment)] calls compress against it while /// honouring this context's advanced parameters; the compression level comes - /// from the [ZstdCompressDict]. This is the hot path for a pooled context + /// from the [ZstdCompressDictionary]. This is the hot path for a pooled context /// recycled with [#reset(ZstdResetDirective)] between frames. /// /// The reference is borrowed: `dict` must stay open for as long as this @@ -171,7 +171,7 @@ private ZstdCompressCtx loadDictionary(MemorySegment dict, long size) { /// @param dict the digested dictionary to reference, or `null` to clear it /// @return `this`, for chaining /// @throws ZstdException if the dictionary cannot be referenced - public ZstdCompressCtx refDictionary(ZstdCompressDict dict) { + public ZstdCompressContext refDictionary(ZstdCompressDictionary dict) { MemorySegment cdict = dict == null ? MemorySegment.NULL : dict.ptr(); NativeCall.checkReturnValue(() -> (long) Bindings.CCTX_REF_CDICT.invokeExact(ptr(), cdict)); return this; @@ -187,7 +187,7 @@ public ZstdCompressCtx refDictionary(ZstdCompressDict dict) { /// consumed by the next [#compress(MemorySegment, MemorySegment)] / /// [#compress(byte[])] — it does not stick across frames. The decompressor /// must reference the **same** prefix with - /// [ZstdDecompressCtx#refPrefix(MemorySegment)] to decode the frame. + /// [ZstdDecompressContext#refPrefix(MemorySegment)] to decode the frame. /// /// Because the prefix is referenced, `prefix` must stay valid until the next /// compression completes. Heap callers that cannot manage native lifetime @@ -196,7 +196,7 @@ public ZstdCompressCtx refDictionary(ZstdCompressDict dict) { /// @param prefix native prefix content, or `null` / [MemorySegment#NULL] to clear it /// @return `this`, for chaining /// @throws ZstdException if the prefix cannot be referenced - public ZstdCompressCtx refPrefix(MemorySegment prefix) { + public ZstdCompressContext refPrefix(MemorySegment prefix) { if (NativeCall.isNull(prefix)) { return refPrefix(MemorySegment.NULL, 0L); } @@ -204,7 +204,7 @@ public ZstdCompressCtx refPrefix(MemorySegment prefix) { return refPrefix(prefix, prefix.byteSize()); } - private ZstdCompressCtx refPrefix(MemorySegment prefix, long size) { + private ZstdCompressContext refPrefix(MemorySegment prefix, long size) { NativeCall.checkReturnValue(() -> (long) Bindings.CCTX_REF_PREFIX.invokeExact(ptr(), prefix, size)); return this; } @@ -233,8 +233,8 @@ private void setParam(ZstdCompressParameter parameter, int value) { /// Compresses `src` against `dict` at this context's level. /// /// The dictionary is re-digested on every call; for repeated compression - /// against the same dictionary, digest it once into a [ZstdCompressDict] - /// and use [#compress(byte[], ZstdCompressDict)]. + /// against the same dictionary, digest it once into a [ZstdCompressDictionary] + /// and use [#compress(byte[], ZstdCompressDictionary)]. /// /// @param src the bytes to compress /// @param dict the dictionary to compress against @@ -255,12 +255,12 @@ public byte[] compress(byte[] src, ZstdDictionary dict) { } /// Compresses `src` against a pre-digested `dict` (the level was - /// fixed when the [ZstdCompressDict] was built). + /// fixed when the [ZstdCompressDictionary] was built). /// /// @param src the bytes to compress /// @param dict the pre-digested compression dictionary /// @return a self-describing zstd frame - public byte[] compress(byte[] src, ZstdCompressDict dict) { + public byte[] compress(byte[] src, ZstdCompressDictionary dict) { Objects.requireNonNull(src, "src"); Objects.requireNonNull(dict, "dict"); try (Arena arena = Arena.ofConfined()) { @@ -299,7 +299,7 @@ public long compress(MemorySegment dst, MemorySegment src) { /// @param src the native source bytes to compress /// @param dict the pre-digested compression dictionary /// @return the number of bytes written into `dst` - public long compress(MemorySegment dst, MemorySegment src, ZstdCompressDict dict) { + public long compress(MemorySegment dst, MemorySegment src, ZstdCompressDictionary dict) { NativeCall.requireNative(dst, "dst"); NativeCall.requireNative(src, "src"); MemorySegment cdict = dict.ptr(); @@ -328,7 +328,7 @@ public MemorySegment compress(Arena arena, MemorySegment src) { /// @param src the native source bytes to compress /// @param dict the pre-digested compression dictionary /// @return the zstd frame, a slice of an `arena`-owned segment - public MemorySegment compress(Arena arena, MemorySegment src, ZstdCompressDict dict) { + public MemorySegment compress(Arena arena, MemorySegment src, ZstdCompressDictionary dict) { MemorySegment dst = arena.allocate(Zstd.compressBound(src.byteSize())); long written = compress(dst, src, dict); return dst.asSlice(0, written); diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDictionary.java similarity index 86% rename from zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java rename to zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDictionary.java index 83f1ded..f8e10bc 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDictionary.java @@ -6,14 +6,14 @@ /// A dictionary digested once for compression at a fixed level. /// -/// Building a `ZstdCompressDict` pre-processes the dictionary so that each -/// [ZstdCompressCtx#compress(byte[], ZstdCompressDict)] call skips that +/// Building a `ZstdCompressDictionary` pre-processes the dictionary so that each +/// [ZstdCompressContext#compress(byte[], ZstdCompressDictionary)] call skips that /// cost — the right choice when compressing many payloads against the same /// dictionary. The raw [ZstdDictionary] bytes are copied into native /// memory, so the source may be discarded afterwards. /// /// Immutable once built and safe to share across threads (the digested dictionary is read-only). -public final class ZstdCompressDict extends NativeObject { +public final class ZstdCompressDictionary extends NativeObject { private final int level; @@ -21,7 +21,7 @@ public final class ZstdCompressDict extends NativeObject { /// /// @param dict the dictionary to digest /// @param level the compression level to fix for this digested dictionary - public ZstdCompressDict(ZstdDictionary dict, int level) { + public ZstdCompressDictionary(ZstdDictionary dict, int level) { super(create(dict, level)); this.level = level; } @@ -29,7 +29,7 @@ public ZstdCompressDict(ZstdDictionary dict, int level) { /// Digests `dict` for compression at the library default level. /// /// @param dict the dictionary to digest - public ZstdCompressDict(ZstdDictionary dict) { + public ZstdCompressDictionary(ZstdDictionary dict) { this(dict, Zstd.defaultCompressionLevel()); } @@ -39,11 +39,11 @@ public ZstdCompressDict(ZstdDictionary dict) { /// `dict` must be a native (off-heap) [MemorySegment] — e.g. an mmap slice or /// an arena buffer. Its bytes are copied into the digested dictionary, so the /// segment may be released once the constructor returns. Heap-backed callers - /// should use [ZstdCompressDict(ZstdDictionary, int)] instead. + /// should use [ZstdCompressDictionary(ZstdDictionary, int)] instead. /// /// @param dict native dictionary content /// @param level the compression level to fix for this digested dictionary - public ZstdCompressDict(MemorySegment dict, int level) { + public ZstdCompressDictionary(MemorySegment dict, int level) { super(create(dict, level)); this.level = level; } @@ -52,7 +52,7 @@ public ZstdCompressDict(MemorySegment dict, int level) { /// level, without a heap copy. /// /// @param dict native dictionary content - public ZstdCompressDict(MemorySegment dict) { + public ZstdCompressDictionary(MemorySegment dict) { this(dict, Zstd.defaultCompressionLevel()); } diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressParameter.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressParameter.java index 8cf92ea..f2c1140 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressParameter.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressParameter.java @@ -1,15 +1,15 @@ package io.github.dfa1.zstd; -/// Advanced compression parameters settable on a [ZstdCompressCtx], mirroring +/// Advanced compression parameters settable on a [ZstdCompressContext], mirroring /// `ZSTD_cParameter` from the /// [official manual](https://facebook.github.io/zstd/doc/api_manual_latest.html). /// -/// Set them with [ZstdCompressCtx#parameter(ZstdCompressParameter, int)]; the -/// context applies them on its next [ZstdCompressCtx#compress(byte[])]. Values +/// Set them with [ZstdCompressContext#parameter(ZstdCompressParameter, int)]; the +/// context applies them on its next [ZstdCompressContext#compress(byte[])]. Values /// are validated natively — an out-of-range value raises a [ZstdException]. public enum ZstdCompressParameter { - /// Compression level. Prefer [ZstdCompressCtx#level(int)]. + /// Compression level. Prefer [ZstdCompressContext#level(int)]. COMPRESSION_LEVEL(100), /// Maximum back-reference distance, as a power of two (larger = better ratio, more memory). WINDOW_LOG(101), diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressContext.java similarity index 92% rename from zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java rename to zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressContext.java index 05e645c..23934f6 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressContext.java @@ -8,12 +8,12 @@ /// /// Reusing one context across many [#decompress] calls amortises native /// state allocation. Not thread-safe: confine an instance to one thread or pool it. -public final class ZstdDecompressCtx extends NativeObject { +public final class ZstdDecompressContext extends NativeObject { private static final String COMPRESSED = "compressed"; /// Creates a new decompression context. - public ZstdDecompressCtx() { + public ZstdDecompressContext() { super(create()); } @@ -27,7 +27,7 @@ private static MemorySegment create() { /// @param value the value, validated natively against the parameter's bounds /// @return `this`, for chaining /// @throws ZstdException if the value is out of range for the parameter - public ZstdDecompressCtx parameter(ZstdDecompressParameter parameter, int value) { + public ZstdDecompressContext parameter(ZstdDecompressParameter parameter, int value) { NativeCall.checkReturnValue(() -> (long) Bindings.DCTX_SET_PARAMETER.invokeExact(ptr(), parameter.value(), value)); return this; } @@ -37,7 +37,7 @@ public ZstdDecompressCtx parameter(ZstdDecompressParameter parameter, int value) /// /// @param windowLogMax the base-2 log of the maximum accepted window size /// @return `this`, for chaining - public ZstdDecompressCtx windowLogMax(int windowLogMax) { + public ZstdDecompressContext windowLogMax(int windowLogMax) { return parameter(ZstdDecompressParameter.WINDOW_LOG_MAX, windowLogMax); } @@ -55,7 +55,7 @@ public ZstdDecompressCtx windowLogMax(int windowLogMax) { /// @param directive what to clear /// @return `this`, for chaining /// @throws ZstdException if the reset fails natively - public ZstdDecompressCtx reset(ZstdResetDirective directive) { + public ZstdDecompressContext reset(ZstdResetDirective directive) { Objects.requireNonNull(directive, "directive"); NativeCall.checkReturnValue(() -> (long) Bindings.DCTX_RESET.invokeExact(ptr(), directive.value())); return this; @@ -70,12 +70,12 @@ public ZstdDecompressCtx reset(ZstdResetDirective directive) { /// It stays loaded until replaced, cleared with [#loadDictionary(ZstdDictionary)] /// passing `null`, or dropped by a parameter [#reset(ZstdResetDirective)]. For /// a dictionary reused across many contexts, digest it once and attach it with - /// [#refDictionary(ZstdDecompressDict)] instead. + /// [#refDictionary(ZstdDecompressDictionary)] instead. /// /// @param dict the dictionary to load, or `null` to clear the loaded dictionary /// @return `this`, for chaining /// @throws ZstdException if the dictionary cannot be loaded - public ZstdDecompressCtx loadDictionary(ZstdDictionary dict) { + public ZstdDecompressContext loadDictionary(ZstdDictionary dict) { if (dict == null) { return loadDictionary(MemorySegment.NULL, 0L); } @@ -94,7 +94,7 @@ public ZstdDecompressCtx loadDictionary(ZstdDictionary dict) { /// context), or `null` / [MemorySegment#NULL] to clear the loaded dictionary /// @return `this`, for chaining /// @throws ZstdException if the dictionary cannot be loaded - public ZstdDecompressCtx loadDictionary(MemorySegment dict) { + public ZstdDecompressContext loadDictionary(MemorySegment dict) { if (NativeCall.isNull(dict)) { return loadDictionary(MemorySegment.NULL, 0L); } @@ -102,7 +102,7 @@ public ZstdDecompressCtx loadDictionary(MemorySegment dict) { return loadDictionary(dict, dict.byteSize()); } - private ZstdDecompressCtx loadDictionary(MemorySegment dict, long size) { + private ZstdDecompressContext loadDictionary(MemorySegment dict, long size) { NativeCall.checkReturnValue(() -> (long) Bindings.DCTX_LOAD_DICTIONARY.invokeExact(ptr(), dict, size)); return this; } @@ -120,7 +120,7 @@ private ZstdDecompressCtx loadDictionary(MemorySegment dict, long size) { /// @param dict the digested dictionary to reference, or `null` to clear it /// @return `this`, for chaining /// @throws ZstdException if the dictionary cannot be referenced - public ZstdDecompressCtx refDictionary(ZstdDecompressDict dict) { + public ZstdDecompressContext refDictionary(ZstdDecompressDictionary dict) { MemorySegment ddict = dict == null ? MemorySegment.NULL : dict.ptr(); NativeCall.checkReturnValue(() -> (long) Bindings.DCTX_REF_DDICT.invokeExact(ptr(), ddict)); return this; @@ -128,7 +128,7 @@ public ZstdDecompressCtx refDictionary(ZstdDecompressDict dict) { /// References native `prefix` content as a single-use dictionary for decoding /// the **next** frame only — the decompression counterpart of - /// [ZstdCompressCtx#refPrefix(MemorySegment)]. It must be the **same** content + /// [ZstdCompressContext#refPrefix(MemorySegment)]. It must be the **same** content /// the frame was compressed against, or decoding fails. /// /// The prefix is referenced, not copied (no digest, no heap copy): `prefix` @@ -143,7 +143,7 @@ public ZstdDecompressCtx refDictionary(ZstdDecompressDict dict) { /// @param prefix native prefix content, or `null` / [MemorySegment#NULL] to clear it /// @return `this`, for chaining /// @throws ZstdException if the prefix cannot be referenced - public ZstdDecompressCtx refPrefix(MemorySegment prefix) { + public ZstdDecompressContext refPrefix(MemorySegment prefix) { if (NativeCall.isNull(prefix)) { return refPrefix(MemorySegment.NULL, 0L); } @@ -151,7 +151,7 @@ public ZstdDecompressCtx refPrefix(MemorySegment prefix) { return refPrefix(prefix, prefix.byteSize()); } - private ZstdDecompressCtx refPrefix(MemorySegment prefix, long size) { + private ZstdDecompressContext refPrefix(MemorySegment prefix, long size) { NativeCall.checkReturnValue(() -> (long) Bindings.DCTX_REF_PREFIX.invokeExact(ptr(), prefix, size)); return this; } @@ -175,8 +175,8 @@ public byte[] decompress(byte[] compressed, int maxSize) { /// Decompresses a frame that was compressed against `dict`. /// /// The dictionary is re-digested on every call; for repeated use digest it - /// once into a [ZstdDecompressDict] and use - /// [#decompress(byte[], int, ZstdDecompressDict)]. + /// once into a [ZstdDecompressDictionary] and use + /// [#decompress(byte[], int, ZstdDecompressDictionary)]. /// /// @param compressed a complete zstd frame /// @param maxSize upper bound on the decompressed length @@ -202,7 +202,7 @@ public byte[] decompress(byte[] compressed, int maxSize, ZstdDictionary dict) { /// @param maxSize upper bound on the decompressed length /// @param dict the pre-digested decompression dictionary /// @return the original bytes - public byte[] decompress(byte[] compressed, int maxSize, ZstdDecompressDict dict) { + public byte[] decompress(byte[] compressed, int maxSize, ZstdDecompressDictionary dict) { Objects.requireNonNull(compressed, COMPRESSED); Objects.requireNonNull(dict, "dict"); try (Arena arena = Arena.ofConfined()) { @@ -242,7 +242,7 @@ public long decompress(MemorySegment dst, MemorySegment src) { /// @param src the native source frame to decompress /// @param dict the pre-digested decompression dictionary /// @return the number of bytes written into `dst` - public long decompress(MemorySegment dst, MemorySegment src, ZstdDecompressDict dict) { + public long decompress(MemorySegment dst, MemorySegment src, ZstdDecompressDictionary dict) { NativeCall.requireNative(dst, "dst"); NativeCall.requireNative(src, "src"); MemorySegment ddict = dict.ptr(); @@ -277,7 +277,7 @@ public MemorySegment decompress(Arena arena, MemorySegment frame) { /// @param frame a complete zstd frame storing its decompressed size /// @param dict the pre-digested decompression dictionary /// @return a segment of exactly the decompressed length, allocated in `arena` - public MemorySegment decompress(Arena arena, MemorySegment frame, ZstdDecompressDict dict) { + public MemorySegment decompress(Arena arena, MemorySegment frame, ZstdDecompressDictionary dict) { long size = Zstd.decompressedSize(frame); MemorySegment out = arena.allocate(size); decompress(out, frame, dict); diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDictionary.java similarity index 88% rename from zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java rename to zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDictionary.java index 5696be8..af923a7 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDictionary.java @@ -7,17 +7,17 @@ /// A dictionary digested once for decompression. /// /// Pre-processes the dictionary so each -/// [ZstdDecompressCtx#decompress(byte[], int, ZstdDecompressDict)] call +/// [ZstdDecompressContext#decompress(byte[], int, ZstdDecompressDictionary)] call /// skips that cost. The raw [ZstdDictionary] bytes are copied into native /// memory, so the source may be discarded afterwards. /// /// Immutable once built and safe to share across threads (the digested dictionary is read-only). -public final class ZstdDecompressDict extends NativeObject { +public final class ZstdDecompressDictionary extends NativeObject { /// Digests `dict` for decompression. /// /// @param dict the dictionary to digest - public ZstdDecompressDict(ZstdDictionary dict) { + public ZstdDecompressDictionary(ZstdDictionary dict) { super(create(dict)); } @@ -26,10 +26,10 @@ public ZstdDecompressDict(ZstdDictionary dict) { /// `dict` must be a native (off-heap) [MemorySegment] — e.g. an mmap slice or /// an arena buffer. Its bytes are copied into the digested dictionary, so the /// segment may be released once the constructor returns. Heap-backed callers - /// should use [ZstdDecompressDict(ZstdDictionary)] instead. + /// should use [ZstdDecompressDictionary(ZstdDictionary)] instead. /// /// @param dict native dictionary content - public ZstdDecompressDict(MemorySegment dict) { + public ZstdDecompressDictionary(MemorySegment dict) { super(create(dict)); } diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressParameter.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressParameter.java index a248e1c..5049868 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressParameter.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressParameter.java @@ -1,10 +1,10 @@ package io.github.dfa1.zstd; -/// Advanced decompression parameters settable on a [ZstdDecompressCtx], mirroring +/// Advanced decompression parameters settable on a [ZstdDecompressContext], mirroring /// `ZSTD_dParameter` from the /// [official manual](https://facebook.github.io/zstd/doc/api_manual_latest.html). /// -/// Set them with [ZstdDecompressCtx#parameter(ZstdDecompressParameter, int)]. +/// Set them with [ZstdDecompressContext#parameter(ZstdDecompressParameter, int)]. public enum ZstdDecompressParameter { /// Largest back-reference window the decoder will accept, as a power of two. diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java index 0622dda..f237e8f 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java @@ -20,13 +20,13 @@ /// /// Obtain one by [training][#train(List, int)] on representative /// samples, or wrap dictionary bytes you already have with [#of(byte[])]. -/// Pass it to [ZstdCompressCtx] / [ZstdDecompressCtx] to compress and +/// Pass it to [ZstdCompressContext] / [ZstdDecompressContext] to compress and /// decompress against it. For a hot path, digest it once into a -/// [ZstdCompressDict] / [ZstdDecompressDict]. +/// [ZstdCompressDictionary] / [ZstdDecompressDictionary]. /// /// {@snippet : /// ZstdDictionary dict = ZstdDictionary.train(sampleRecords, 64 * 1024); -/// try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { +/// try (ZstdCompressContext ctx = new ZstdCompressContext()) { /// byte[] packed = ctx.compress(record, dict); /// } /// } @@ -298,38 +298,38 @@ public int size() { } /// Digests this dictionary once for compression at `level`, ready to share by - /// reference across contexts with [ZstdCompressCtx#refDictionary(ZstdCompressDict)]. + /// reference across contexts with [ZstdCompressContext#refDictionary(ZstdCompressDictionary)]. /// /// The returned dictionary owns native memory — close it when done (it is /// [AutoCloseable]). For a single context, prefer - /// [ZstdCompressCtx#loadDictionary(ZstdDictionary)], which the context digests, + /// [ZstdCompressContext#loadDictionary(ZstdDictionary)], which the context digests, /// owns, and frees for you. /// /// @param level the compression level to fix for the digested dictionary /// @return a digested compression dictionary the caller must close - public ZstdCompressDict compressDict(int level) { - return new ZstdCompressDict(this, level); + public ZstdCompressDictionary compressDict(int level) { + return new ZstdCompressDictionary(this, level); } /// Digests this dictionary for compression at the library default level. /// Otherwise identical to [#compressDict(int)]. /// /// @return a digested compression dictionary the caller must close - public ZstdCompressDict compressDict() { - return new ZstdCompressDict(this); + public ZstdCompressDictionary compressDict() { + return new ZstdCompressDictionary(this); } /// Digests this dictionary once for decompression, ready to share by reference - /// across contexts with [ZstdDecompressCtx#refDictionary(ZstdDecompressDict)]. + /// across contexts with [ZstdDecompressContext#refDictionary(ZstdDecompressDictionary)]. /// /// The returned dictionary owns native memory — close it when done (it is /// [AutoCloseable]). For a single context, prefer - /// [ZstdDecompressCtx#loadDictionary(ZstdDictionary)], which the context owns + /// [ZstdDecompressContext#loadDictionary(ZstdDictionary)], which the context owns /// and frees for you. /// /// @return a digested decompression dictionary the caller must close - public ZstdDecompressDict decompressDict() { - return new ZstdDecompressDict(this); + public ZstdDecompressDictionary decompressDict() { + return new ZstdDecompressDictionary(this); } /// Internal: direct view of the bytes for native calls. Not exposed. diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java index 715a33d..b11fa0b 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java @@ -13,7 +13,7 @@ /// /// Memory use is bounded and independent of the frame size, so this decodes /// arbitrarily large or incrementally-arriving compressed data. For a complete -/// in-memory frame, [Zstd#decompress(byte[])] or [ZstdDecompressCtx] +/// in-memory frame, [Zstd#decompress(byte[])] or [ZstdDecompressContext] /// is simpler. /// /// Closing closes the underlying stream. diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java index f6c8624..b99f010 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java @@ -14,7 +14,7 @@ /// Memory use is bounded and independent of the total payload size, so this is /// the way to compress data that does not fit in memory or arrives incrementally /// (files, sockets, serialization). For a whole in-memory payload of known size, -/// [Zstd#compress(byte[])] or [ZstdCompressCtx] is simpler. +/// [Zstd#compress(byte[])] or [ZstdCompressContext] is simpler. /// /// Closing finishes the frame and closes the underlying stream. /// diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdResetDirective.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdResetDirective.java index 591b48e..303f332 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdResetDirective.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdResetDirective.java @@ -2,8 +2,8 @@ /// Selects what a context reset clears, mirroring `ZSTD_ResetDirective`. /// -/// Use it with [ZstdCompressCtx#reset(ZstdResetDirective)] and -/// [ZstdDecompressCtx#reset(ZstdResetDirective)] to recycle a context for the +/// Use it with [ZstdCompressContext#reset(ZstdResetDirective)] and +/// [ZstdDecompressContext#reset(ZstdResetDirective)] to recycle a context for the /// next frame without freeing and recreating its native state. public enum ZstdResetDirective { diff --git a/zstd/src/test/java/io/github/dfa1/zstd/RefPrefixTest.java b/zstd/src/test/java/io/github/dfa1/zstd/RefPrefixTest.java index 8a24583..2b5c313 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/RefPrefixTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/RefPrefixTest.java @@ -20,8 +20,8 @@ void roundTripsWithMatchingPrefix() { byte[] prefixBytes = "the quick brown fox jumps over the lazy dog".getBytes(); byte[] dataBytes = "the quick brown fox jumps over the lazy cat".getBytes(); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment prefix = segmentOf(arena, prefixBytes); MemorySegment src = segmentOf(arena, dataBytes); MemorySegment frame = arena.allocate(Zstd.compressBound(dataBytes.length)); @@ -52,11 +52,11 @@ void prefixIsAppliedAndRequiredToDecode() { MemorySegment src = segmentOf(arena, random); long baseline; - try (ZstdCompressCtx cctx = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext cctx = new ZstdCompressContext().level(19)) { baseline = cctx.compress(arena, src).byteSize(); } byte[] frame; - try (ZstdCompressCtx cctx = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext cctx = new ZstdCompressContext().level(19)) { cctx.refPrefix(prefix); MemorySegment f = cctx.compress(arena, src); frame = bytesOf(f, (int) f.byteSize()); @@ -66,7 +66,7 @@ void prefixIsAppliedAndRequiredToDecode() { assertThat((long) frame.length).isLessThan(baseline / 10); // And — it round-trips with the same prefix - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment out = arena.allocate(random.length); dctx.refPrefix(prefix); long m = dctx.decompress(out, segmentOf(arena, frame)); @@ -75,7 +75,7 @@ void prefixIsAppliedAndRequiredToDecode() { // But — it cannot be recovered without the prefix boolean reproduced; - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment out = arena.allocate(random.length); long m = dctx.decompress(out, segmentOf(arena, frame)); reproduced = Arrays.equals(bytesOf(out, (int) m), random); @@ -91,7 +91,7 @@ void clearingPrefixWithNullCompressesPlainly() { // Given byte[] dataBytes = "the quick brown fox".getBytes(); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext()) { MemorySegment prefix = segmentOf(arena, "a prior version of the text".getBytes()); MemorySegment src = segmentOf(arena, dataBytes); MemorySegment frame = arena.allocate(Zstd.compressBound(dataBytes.length)); @@ -103,7 +103,7 @@ void clearingPrefixWithNullCompressesPlainly() { // Then — a plain decoder with no prefix decodes it byte[] restored; - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment out = arena.allocate(dataBytes.length); long m = dctx.decompress(out, frame.asSlice(0, n)); restored = bytesOf(out, (int) m); @@ -118,7 +118,7 @@ void prefixIsSingleUseAndDoesNotStickAcrossFrames() { // and one context kept open across two compressions with the prefix set once. byte[] random = randomBytes(0xCAFE, 16384); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx().level(19)) { + ZstdCompressContext cctx = new ZstdCompressContext().level(19)) { MemorySegment prefix = segmentOf(arena, random); MemorySegment src = segmentOf(arena, random); MemorySegment first = arena.allocate(Zstd.compressBound(random.length)); @@ -135,7 +135,7 @@ void prefixIsSingleUseAndDoesNotStickAcrossFrames() { // And — the second frame carries no prefix, so a plain decoder decodes it byte[] restored; - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment out = arena.allocate(random.length); long m = dctx.decompress(out, second.asSlice(0, n2)); restored = bytesOf(out, (int) m); @@ -151,7 +151,7 @@ void compressRefPrefixRejectsAHeapSegment() { // When ThrowingCallable result = () -> { - try (ZstdCompressCtx cctx = new ZstdCompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext()) { cctx.refPrefix(heap); } }; @@ -169,7 +169,7 @@ void decompressRefPrefixRejectsAHeapSegment() { // When ThrowingCallable result = () -> { - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { dctx.refPrefix(heap); } }; @@ -187,7 +187,7 @@ void compressLoadDictionaryRejectsAHeapSegment() { // When loaded as a zero-copy dictionary ThrowingCallable result = () -> { - try (ZstdCompressCtx cctx = new ZstdCompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext()) { cctx.loadDictionary(heap); } }; @@ -205,7 +205,7 @@ void decompressLoadDictionaryRejectsAHeapSegment() { // When loaded as a zero-copy dictionary ThrowingCallable result = () -> { - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { dctx.loadDictionary(heap); } }; @@ -220,15 +220,15 @@ void decompressLoadDictionaryRejectsAHeapSegment() { void refPrefixReturnsTheSameContextForChaining() { // Given both contexts and a native prefix try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment prefix = segmentOf(arena, "a prior version".getBytes()); // When setting and then clearing a prefix on both contexts - ZstdCompressCtx cSet = cctx.refPrefix(prefix); - ZstdCompressCtx cCleared = cctx.refPrefix((MemorySegment) null); - ZstdDecompressCtx dSet = dctx.refPrefix(prefix); - ZstdDecompressCtx dCleared = dctx.refPrefix((MemorySegment) null); + ZstdCompressContext cSet = cctx.refPrefix(prefix); + ZstdCompressContext cCleared = cctx.refPrefix((MemorySegment) null); + ZstdDecompressContext dSet = dctx.refPrefix(prefix); + ZstdDecompressContext dCleared = dctx.refPrefix((MemorySegment) null); // Then every call returns the same instance, for chaining assertThat(cSet).isSameAs(cctx); @@ -242,15 +242,15 @@ void refPrefixReturnsTheSameContextForChaining() { void loadDictionaryFromSegmentReturnsTheSameContextForChaining() { // Given both contexts and a native dictionary segment try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment dict = segmentOf(arena, "dictionary sample payload ".repeat(64).getBytes()); // When loading and then clearing a segment dictionary on both contexts - ZstdCompressCtx cSet = cctx.loadDictionary(dict); - ZstdCompressCtx cCleared = cctx.loadDictionary(MemorySegment.NULL); - ZstdDecompressCtx dSet = dctx.loadDictionary(dict); - ZstdDecompressCtx dCleared = dctx.loadDictionary(MemorySegment.NULL); + ZstdCompressContext cSet = cctx.loadDictionary(dict); + ZstdCompressContext cCleared = cctx.loadDictionary(MemorySegment.NULL); + ZstdDecompressContext dSet = dctx.loadDictionary(dict); + ZstdDecompressContext dCleared = dctx.loadDictionary(MemorySegment.NULL); // Then every call returns the same instance, for chaining assertThat(cSet).isSameAs(cctx); diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdDictionaryTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdDictionaryTest.java index d10a675..7e5d64d 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdDictionaryTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdDictionaryTest.java @@ -49,8 +49,8 @@ void fastCoverRoundTrips() { byte[] plain; byte[] withDict; byte[] restored; - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { plain = cctx.compress(sample); withDict = cctx.compress(sample, dict); restored = dctx.decompress(cctx.compress(sample, dict), sample.length, dict); @@ -66,8 +66,8 @@ void coverRoundTrips() { assertThat(dict.size()).isGreaterThan(0); byte[] sample = samples.get(5); - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] frame = cctx.compress(sample, dict); assertThat(dctx.decompress(frame, sample.length, dict)).isEqualTo(sample); } @@ -112,8 +112,8 @@ void finalizesRawContentIntoUsableDictionary() { assertThat(dict.headerSize()).isPositive(); byte[] sample = samples.get(3); - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] frame = cctx.compress(sample, dict); assertThat(dctx.decompress(frame, sample.length, dict)).isEqualTo(sample); } @@ -155,7 +155,7 @@ void beatsDictionarylessOnTinyPayload() { // When compressed with and without the dictionary byte[] plain; byte[] withDict; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { plain = ctx.compress(sample); withDict = ctx.compress(sample, sut); } @@ -201,8 +201,8 @@ void roundTripsRecord(int index) { // When compressed and decompressed against the raw dictionary byte[] restored; - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] frame = cctx.compress(sample, sut); restored = dctx.decompress(frame, sample.length, sut); } @@ -222,10 +222,10 @@ void roundTripsViaCDictAndDDict() { byte[] restored; int level; - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(sut, 19); - ZstdDecompressDict ddict = new ZstdDecompressDict(sut)) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(sut, 19); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(sut)) { // When round-tripped through the digested dictionaries byte[] frame = cctx.compress(sample, cdict); @@ -240,8 +240,8 @@ void roundTripsViaCDictAndDDict() { @Test void digestedDictionariesReportTheSameId() { - try (ZstdCompressDict cdict = new ZstdCompressDict(sut); - ZstdDecompressDict ddict = new ZstdDecompressDict(sut)) { + try (ZstdCompressDictionary cdict = new ZstdCompressDictionary(sut); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(sut)) { assertThat(cdict.id()).isEqualTo(sut.id()); assertThat(ddict.id()).isEqualTo(sut.id()); } @@ -253,9 +253,9 @@ void interoperatesWithRawPath() { byte[] sample = samples.get(2048); byte[] restored; - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdDecompressDict ddict = new ZstdDecompressDict(sut)) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(sut)) { byte[] frame = cctx.compress(sample, sut); // When decompressed with the digested dictionary @@ -273,7 +273,7 @@ class Factories { @Test void compressDictFixesTheRequestedLevel() { // When a digested compress dictionary is built via the factory - try (ZstdCompressDict cdict = sut.compressDict(19)) { + try (ZstdCompressDictionary cdict = sut.compressDict(19)) { // Then it carries that level and the dictionary's id assertThat(cdict.level()).isEqualTo(19); assertThat(cdict.id()).isEqualTo(sut.id()); @@ -283,7 +283,7 @@ void compressDictFixesTheRequestedLevel() { @Test void compressDictDefaultsToTheLibraryLevel() { // When built without a level - try (ZstdCompressDict cdict = sut.compressDict()) { + try (ZstdCompressDictionary cdict = sut.compressDict()) { // Then it uses the library default assertThat(cdict.level()).isEqualTo(Zstd.defaultCompressionLevel()); } @@ -292,7 +292,7 @@ void compressDictDefaultsToTheLibraryLevel() { @Test void decompressDictReportsTheDictionaryId() { // When a digested decompress dictionary is built via the factory - try (ZstdDecompressDict ddict = sut.decompressDict()) { + try (ZstdDecompressDictionary ddict = sut.decompressDict()) { // Then it carries the dictionary's id assertThat(ddict.id()).isEqualTo(sut.id()); } @@ -304,10 +304,10 @@ void factoryDictionariesRoundTrip() { byte[] sample = samples.get(123); byte[] restored; - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdCompressDict cdict = sut.compressDict(19); - ZstdDecompressDict ddict = sut.decompressDict()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdCompressDictionary cdict = sut.compressDict(19); + ZstdDecompressDictionary ddict = sut.decompressDict()) { // When round-tripped through them byte[] frame = cctx.compress(sample, cdict); @@ -333,8 +333,8 @@ void reloadedDictionaryKeepsIdentityAndDecodes() { // And a frame from the reload decodes against the original byte[] sample = samples.get(1); byte[] restored; - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { byte[] frame = cctx.compress(sample, reloaded); restored = dctx.decompress(frame, sample.length, sut); } @@ -355,10 +355,10 @@ void roundTripsViaSegmentBuiltCDictAndDDict() { byte[] restored; int level; try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(segmentOf(arena, raw), 19); - ZstdDecompressDict ddict = new ZstdDecompressDict(segmentOf(arena, raw))) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(segmentOf(arena, raw), 19); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(segmentOf(arena, raw))) { // When round-tripped through the segment-built dictionaries byte[] frame = cctx.compress(sample, cdict); @@ -378,8 +378,8 @@ void segmentBuiltDictionariesReportSameIdAsHeap() { // Then they carry the same id as the heap-built dictionary try (Arena arena = Arena.ofConfined(); - ZstdCompressDict cdict = new ZstdCompressDict(segmentOf(arena, raw)); - ZstdDecompressDict ddict = new ZstdDecompressDict(segmentOf(arena, raw))) { + ZstdCompressDictionary cdict = new ZstdCompressDictionary(segmentOf(arena, raw)); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(segmentOf(arena, raw))) { assertThat(cdict.id()).isEqualTo(sut.id()); assertThat(ddict.id()).isEqualTo(sut.id()); } @@ -393,10 +393,10 @@ void interoperatesWithHeapBuiltDictionaries() { byte[] restored; try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(segmentOf(arena, raw)); - ZstdDecompressDict ddict = new ZstdDecompressDict(sut)) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(segmentOf(arena, raw)); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(sut)) { byte[] frame = cctx.compress(sample, cdict); // When decompressed with a heap-built DDict @@ -413,7 +413,7 @@ void rejectsHeapDecompressDictSegment() { MemorySegment heap = MemorySegment.ofArray(sut.toByteArray()); // When digesting it as a decompress dictionary - ThrowingCallable result = () -> new ZstdDecompressDict(heap); + ThrowingCallable result = () -> new ZstdDecompressDictionary(heap); // Then it fails fast rather than dereferencing a heap address in C assertThatThrownBy(result).isInstanceOf(IllegalArgumentException.class); @@ -425,7 +425,7 @@ void rejectsHeapCompressDictSegment() { MemorySegment heap = MemorySegment.ofArray(sut.toByteArray()); // When digesting it as a compress dictionary - ThrowingCallable result = () -> new ZstdCompressDict(heap); + ThrowingCallable result = () -> new ZstdCompressDictionary(heap); // Then it fails fast assertThatThrownBy(result).isInstanceOf(IllegalArgumentException.class); @@ -441,19 +441,19 @@ void loadedDictionaryCombinesWithAdvancedParameters() { // combination the per-call compress(src, dict) overloads cannot give byte[] sample = samples.get(123); byte[] frame; - try (ZstdCompressCtx cctx = new ZstdCompressCtx().checksum(true)) { + try (ZstdCompressContext cctx = new ZstdCompressContext().checksum(true)) { cctx.loadDictionary(sut); frame = cctx.compress(sample); } byte[] plain; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { plain = ctx.compress(sample); } // Then the dictionary is honoured (smaller than dictionaryless) and decodes assertThat(frame).hasSizeLessThan(plain.length); byte[] restored; - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { dctx.loadDictionary(sut); restored = dctx.decompress(frame, sample.length); } @@ -467,10 +467,10 @@ void referencedDigestedDictionarySurvivesSessionReset() { byte[] second = samples.get(2); byte[] restoredFirst; byte[] restoredSecond; - try (ZstdCompressDict cdict = new ZstdCompressDict(sut, 19); - ZstdDecompressDict ddict = new ZstdDecompressDict(sut); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressDictionary cdict = new ZstdCompressDictionary(sut, 19); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(sut); + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { cctx.refDictionary(cdict); byte[] frameFirst = cctx.compress(first); cctx.reset(ZstdResetDirective.SESSION_ONLY); @@ -492,14 +492,14 @@ void parameterResetClearsTheLoadedDictionary() { // Given a context that loaded a dictionary, then cleared its parameters byte[] sample = samples.get(7); byte[] afterReset; - try (ZstdCompressCtx cctx = new ZstdCompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext()) { cctx.loadDictionary(sut); cctx.compress(sample); cctx.reset(ZstdResetDirective.SESSION_AND_PARAMETERS); afterReset = cctx.compress(sample); } byte[] noDict; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { noDict = ctx.compress(sample); } @@ -512,13 +512,13 @@ void nullClearsTheLoadedDictionary() { // Given a context whose loaded dictionary is then cleared with null byte[] sample = samples.get(7); byte[] cleared; - try (ZstdCompressCtx cctx = new ZstdCompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext()) { cctx.loadDictionary(sut); cctx.loadDictionary((ZstdDictionary) null); cleared = cctx.compress(sample); } byte[] noDict; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { noDict = ctx.compress(sample); } @@ -533,8 +533,8 @@ void loadsDictionaryFromNativeSegmentWithoutHeapCopy() { byte[] raw = sut.toByteArray(); byte[] restored; try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { cctx.loadDictionary(segmentOf(arena, raw)); byte[] frame = cctx.compress(sample); dctx.loadDictionary(segmentOf(arena, raw)); @@ -551,7 +551,7 @@ void rejectsHeapDictionarySegment() { MemorySegment heap = MemorySegment.ofArray(sut.toByteArray()); // When loaded into a context - try (ZstdCompressCtx cctx = new ZstdCompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext()) { ThrowingCallable result = () -> cctx.loadDictionary(heap); // Then it fails fast rather than handing C a heap address @@ -564,13 +564,13 @@ void nullNativeSegmentClearsTheLoadedDictionary() { // Given a context whose dictionary is cleared through the native overload byte[] sample = samples.get(7); byte[] cleared; - try (ZstdCompressCtx cctx = new ZstdCompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext()) { cctx.loadDictionary(sut); cctx.loadDictionary((MemorySegment) null); cleared = cctx.compress(sample); } byte[] noDict; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { noDict = ctx.compress(sample); } @@ -582,8 +582,8 @@ void nullNativeSegmentClearsTheLoadedDictionary() { void loadAndRefReturnTheSameCompressContext() { // Given a compress context and dictionaries to load and reference try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(sut, 19)) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(sut, 19)) { // Then every sticky-dictionary call returns the same context, for chaining assertThat(cctx.loadDictionary(sut)).isSameAs(cctx); @@ -598,8 +598,8 @@ void loadAndRefReturnTheSameCompressContext() { void loadAndRefReturnTheSameDecompressContext() { // Given a decompress context and dictionaries to load and reference try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdDecompressDict ddict = new ZstdDecompressDict(sut)) { + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(sut)) { // Then every sticky-dictionary call returns the same context, for chaining assertThat(dctx.loadDictionary(sut)).isSameAs(dctx); diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdErrorTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdErrorTest.java index fe399a6..6ad1612 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdErrorTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdErrorTest.java @@ -39,7 +39,7 @@ void carriesTheNativeErrorNameAsTheMessage() { @Test void reportsParameterOutOfBound() { - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { ZstdException ex = catchThrowableOfType(ZstdException.class, () -> ctx.parameter(ZstdCompressParameter.WINDOW_LOG, 99)); assertThat(ex.code()).isEqualTo(ZstdErrorCode.PARAMETER_OUT_OF_BOUND); diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java index 5e683fd..62ad638 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java @@ -236,7 +236,7 @@ void enablesInPlaceDecompression() { byte[] frame = Zstd.compress(PAYLOAD); long margin = ZstdFrame.decompressionMargin(frame); try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment buf = arena.allocate(PAYLOAD.length + margin); // and the compressed frame placed at the very end of that buffer @@ -285,7 +285,7 @@ void reportsContentSizeAndNoChecksumByDefault() { @Test void reflectsChecksumFlag() { byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx().checksum(true)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().checksum(true)) { frame = ctx.compress(PAYLOAD); } assertThat(ZstdFrame.header(frame).hasChecksum()).isTrue(); @@ -384,7 +384,7 @@ void matchesTheDictionaryUsed() { // Given a frame compressed with a dictionary ZstdDictionary dict = trainDictionary(3000); byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { frame = ctx.compress(PAYLOAD, dict); } @@ -397,7 +397,7 @@ void matchesThroughTheSegmentOverload() { // Given a dictionary frame in a native segment ZstdDictionary dict = trainDictionary(3000); byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { frame = ctx.compress(PAYLOAD, dict); } try (Arena arena = Arena.ofConfined()) { diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdMemoryTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdMemoryTest.java index cfe8147..bcedf27 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdMemoryTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdMemoryTest.java @@ -39,7 +39,7 @@ class LiveSizes { @Test void contextSizeGrowsAfterUse() { - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { long before = ctx.sizeOf(); ctx.compress(PAYLOAD); long after = ctx.sizeOf(); @@ -50,7 +50,7 @@ void contextSizeGrowsAfterUse() { @Test void decompressContextHasSize() { - try (ZstdDecompressCtx ctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext ctx = new ZstdDecompressContext()) { assertThat(ctx.sizeOf()).isPositive(); } } @@ -58,8 +58,8 @@ void decompressContextHasSize() { @Test void digestedDictionariesHaveSize() { ZstdDictionary dict = trainDictionary(2000); - try (ZstdCompressDict cdict = new ZstdCompressDict(dict); - ZstdDecompressDict ddict = new ZstdDecompressDict(dict)) { + try (ZstdCompressDictionary cdict = new ZstdCompressDictionary(dict); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(dict)) { assertThat(cdict.sizeOf()).isPositive(); assertThat(ddict.sizeOf()).isPositive(); } diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdParameterTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdParameterTest.java index e0088ad..6ebb4e8 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdParameterTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdParameterTest.java @@ -25,8 +25,8 @@ void addsFourByteTrailerAndStillRoundTrips() { // Given the same input compressed at the same level with and without a checksum byte[] plain; byte[] checksummed; - try (ZstdCompressCtx noSum = new ZstdCompressCtx().level(9); - ZstdCompressCtx withSum = new ZstdCompressCtx().level(9).checksum(true)) { + try (ZstdCompressContext noSum = new ZstdCompressContext().level(9); + ZstdCompressContext withSum = new ZstdCompressContext().level(9).checksum(true)) { plain = noSum.compress(PAYLOAD); checksummed = withSum.compress(PAYLOAD); } @@ -40,7 +40,7 @@ void addsFourByteTrailerAndStillRoundTrips() { void rejectsCorruptedChecksummedFrame() { // Given a checksummed frame with one body byte flipped byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx().checksum(true)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().checksum(true)) { frame = ctx.compress(PAYLOAD); } frame[frame.length / 2] ^= 0x01; @@ -59,7 +59,7 @@ class Ratio { @Test void longDistanceMatchingRoundTrips() { byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx().level(3).longDistanceMatching(true)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().level(3).longDistanceMatching(true)) { frame = ctx.compress(PAYLOAD); } assertThat(Zstd.decompress(frame)).isEqualTo(PAYLOAD); @@ -68,7 +68,7 @@ void longDistanceMatchingRoundTrips() { @Test void explicitWindowLogRoundTrips() { byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx().windowLog(24)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().windowLog(24)) { frame = ctx.compress(PAYLOAD); } assertThat(Zstd.decompress(frame)).isEqualTo(PAYLOAD); @@ -110,7 +110,7 @@ void settingAParameterAtItsLowerBoundRoundTrips(ZstdCompressParameter parameter) // Given a parameter set to a valid in-range value int value = Math.max(parameter.bounds().lowerBound(), 1); byte[] frame; - try (ZstdCompressCtx ctx = new ZstdCompressCtx().parameter(parameter, value)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().parameter(parameter, value)) { frame = ctx.compress(PAYLOAD); } // Then the frame still decompresses @@ -125,7 +125,7 @@ class Decompress { void windowLogMaxIsAccepted() { // Given a decompressor configured with a raised window limit byte[] frame = Zstd.compress(PAYLOAD); - try (ZstdDecompressCtx ctx = new ZstdDecompressCtx().windowLogMax(31)) { + try (ZstdDecompressContext ctx = new ZstdDecompressContext().windowLogMax(31)) { // Then normal frames still decode assertThat(ctx.decompress(frame, PAYLOAD.length)).isEqualTo(PAYLOAD); } @@ -134,7 +134,7 @@ void windowLogMaxIsAccepted() { @Test void rejectsOutOfRangeValue() { // Given a decompression context - try (ZstdDecompressCtx sut = new ZstdDecompressCtx()) { + try (ZstdDecompressContext sut = new ZstdDecompressContext()) { // When setting an absurd window-log-max ThrowingCallable result = () -> sut.parameter(ZstdDecompressParameter.WINDOW_LOG_MAX, 99); @@ -152,12 +152,12 @@ void sessionOnlyKeepsLevelAndParameters() { // Given a context used once, then reset for the session only byte[] reused; byte[] fresh; - try (ZstdCompressCtx sut = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext sut = new ZstdCompressContext().level(19)) { sut.compress(PAYLOAD); sut.reset(ZstdResetDirective.SESSION_ONLY); reused = sut.compress(PAYLOAD); } - try (ZstdCompressCtx ctx = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().level(19)) { fresh = ctx.compress(PAYLOAD); } @@ -171,12 +171,12 @@ void parameterResetRestoresTheDefaultLevel(ZstdResetDirective directive) { // Given a level-19 context reset with parameters cleared byte[] afterReset; byte[] atDefault; - try (ZstdCompressCtx sut = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext sut = new ZstdCompressContext().level(19)) { sut.compress(PAYLOAD); sut.reset(directive); afterReset = sut.compress(PAYLOAD); } - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { atDefault = ctx.compress(PAYLOAD); } @@ -190,7 +190,7 @@ void dictionaryRoundTripsAfterParameterReset() { ZstdDictionary dict = ZstdDictionary.of("dictionary sample payload ".repeat(64).getBytes(StandardCharsets.UTF_8)); byte[] frame; - try (ZstdCompressCtx sut = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext sut = new ZstdCompressContext().level(19)) { sut.compress(PAYLOAD, dict); sut.reset(ZstdResetDirective.SESSION_AND_PARAMETERS); @@ -199,7 +199,7 @@ void dictionaryRoundTripsAfterParameterReset() { } // Then the frame still round-trips through the same dictionary - try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext dctx = new ZstdDecompressContext()) { assertThat(dctx.decompress(frame, PAYLOAD.length, dict)).isEqualTo(PAYLOAD); } } @@ -208,7 +208,7 @@ void dictionaryRoundTripsAfterParameterReset() { void decompressContextStillDecodesAfterReset() { // Given a decompression context reset between frames byte[] frame = Zstd.compress(PAYLOAD); - try (ZstdDecompressCtx sut = new ZstdDecompressCtx()) { + try (ZstdDecompressContext sut = new ZstdDecompressContext()) { sut.decompress(frame, PAYLOAD.length); sut.reset(ZstdResetDirective.SESSION_AND_PARAMETERS); @@ -220,8 +220,8 @@ void decompressContextStillDecodesAfterReset() { @Test void resetReturnsTheSameContext() { // Given both contexts - try (ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + try (ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { // Then reset returns the same instance, for chaining assertThat(cctx.reset(ZstdResetDirective.SESSION_ONLY)).isSameAs(cctx); assertThat(dctx.reset(ZstdResetDirective.SESSION_ONLY)).isSameAs(dctx); @@ -235,13 +235,13 @@ void sessionOnlyKeepsTheCachedLevelForTheLegacyDictionaryPath() { ZstdDictionary dict = ZstdDictionary.of("dictionary sample payload ".repeat(64).getBytes(StandardCharsets.UTF_8)); byte[] afterSessionReset; - try (ZstdCompressCtx sut = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext sut = new ZstdCompressContext().level(19)) { sut.compress(PAYLOAD, dict); sut.reset(ZstdResetDirective.SESSION_ONLY); afterSessionReset = sut.compress(PAYLOAD, dict); } byte[] freshLevel19; - try (ZstdCompressCtx ctx = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext ctx = new ZstdCompressContext().level(19)) { freshLevel19 = ctx.compress(PAYLOAD, dict); } @@ -256,13 +256,13 @@ void parameterResetClearsTheCachedLevelForTheLegacyDictionaryPath() { ZstdDictionary dict = ZstdDictionary.of("dictionary sample payload ".repeat(64).getBytes(StandardCharsets.UTF_8)); byte[] afterParameterReset; - try (ZstdCompressCtx sut = new ZstdCompressCtx().level(19)) { + try (ZstdCompressContext sut = new ZstdCompressContext().level(19)) { sut.compress(PAYLOAD, dict); sut.reset(ZstdResetDirective.PARAMETERS); afterParameterReset = sut.compress(PAYLOAD, dict); } byte[] freshDefaultLevel; - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { + try (ZstdCompressContext ctx = new ZstdCompressContext()) { freshDefaultLevel = ctx.compress(PAYLOAD, dict); } @@ -273,7 +273,7 @@ void parameterResetClearsTheCachedLevelForTheLegacyDictionaryPath() { @Test void rejectsNullDirective() { // Given a compression context - try (ZstdCompressCtx sut = new ZstdCompressCtx()) { + try (ZstdCompressContext sut = new ZstdCompressContext()) { // When reset with a null directive ThrowingCallable result = () -> sut.reset(null); @@ -291,8 +291,8 @@ void levelViaParameterMatchesLevelMethod() { // Given the level set generically vs via level() byte[] viaParam; byte[] viaMethod; - try (ZstdCompressCtx a = new ZstdCompressCtx().parameter(ZstdCompressParameter.COMPRESSION_LEVEL, 17); - ZstdCompressCtx b = new ZstdCompressCtx().level(17)) { + try (ZstdCompressContext a = new ZstdCompressContext().parameter(ZstdCompressParameter.COMPRESSION_LEVEL, 17); + ZstdCompressContext b = new ZstdCompressContext().level(17)) { viaParam = a.compress(PAYLOAD); viaMethod = b.compress(PAYLOAD); } @@ -304,7 +304,7 @@ void levelViaParameterMatchesLevelMethod() { @Test void rejectsOutOfRangeValue() { // Given a compression context - try (ZstdCompressCtx sut = new ZstdCompressCtx()) { + try (ZstdCompressContext sut = new ZstdCompressContext()) { // When setting an absurd window log ThrowingCallable result = () -> sut.parameter(ZstdCompressParameter.WINDOW_LOG, 99); @@ -322,8 +322,8 @@ void levelActuallyAppliesToTheNativeCompressionPath() { byte[] atMax; // When compressing via level() at the minimum and the maximum level - try (ZstdCompressCtx low = new ZstdCompressCtx().level(Zstd.minCompressionLevel()); - ZstdCompressCtx high = new ZstdCompressCtx().level(Zstd.maxCompressionLevel())) { + try (ZstdCompressContext low = new ZstdCompressContext().level(Zstd.minCompressionLevel()); + ZstdCompressContext high = new ZstdCompressContext().level(Zstd.maxCompressionLevel())) { atMin = low.compress(data); atMax = high.compress(data); } diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdSegmentTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdSegmentTest.java index 186d8e7..c121e50 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdSegmentTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdSegmentTest.java @@ -25,8 +25,8 @@ void roundTripsNativeToNative() { byte[] original = "segment payload ".repeat(200).getBytes(StandardCharsets.UTF_8); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment src = segmentOf(arena, original); MemorySegment dst = arena.allocate(Zstd.compressBound(original.length)); @@ -55,8 +55,8 @@ void codecSizesAndAllocatesOutput() { byte[] original = "arena-sized payload ".repeat(100).getBytes(StandardCharsets.UTF_8); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment src = segmentOf(arena, original); @@ -81,10 +81,10 @@ void roundTripsWithDigestedDictionary() { byte[] sample = "{\"id\":42,\"user\":\"u\",\"active\":true}".getBytes(StandardCharsets.UTF_8); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(dict); - ZstdDecompressDict ddict = new ZstdDecompressDict(dict)) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(dict); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(dict)) { MemorySegment src = segmentOf(arena, sample); @@ -105,10 +105,10 @@ void arenaAllocatingDecompressSizesOutputFromTheDigestedDictionaryFrame() { byte[] sample = "{\"id\":99,\"user\":\"u\",\"active\":false}".getBytes(StandardCharsets.UTF_8); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx cctx = new ZstdCompressCtx(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(dict); - ZstdDecompressDict ddict = new ZstdDecompressDict(dict)) { + ZstdCompressContext cctx = new ZstdCompressContext(); + ZstdDecompressContext dctx = new ZstdDecompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(dict); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(dict)) { MemorySegment src = segmentOf(arena, sample); MemorySegment frame = cctx.compress(arena, src, cdict); @@ -130,7 +130,7 @@ class HeapSegmentGuard { @Test void compressRejectsHeapSource() { try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx sut = new ZstdCompressCtx()) { + ZstdCompressContext sut = new ZstdCompressContext()) { // Given a heap-backed source segment handed to the zero-copy API MemorySegment heapSrc = MemorySegment.ofArray(new byte[64]); MemorySegment dst = arena.allocate(64); @@ -148,7 +148,7 @@ void compressRejectsHeapSource() { @Test void decompressRejectsHeapDestination() { try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx sut = new ZstdDecompressCtx()) { + ZstdDecompressContext sut = new ZstdDecompressContext()) { // Given a heap-backed destination segment handed to the zero-copy API MemorySegment heapDst = MemorySegment.ofArray(new byte[64]); MemorySegment src = arena.allocate(64); @@ -166,7 +166,7 @@ void decompressRejectsHeapDestination() { @Test void compressRejectsHeapDestination() { try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx sut = new ZstdCompressCtx()) { + ZstdCompressContext sut = new ZstdCompressContext()) { // Given a heap-backed destination segment handed to the zero-copy API MemorySegment heapDst = MemorySegment.ofArray(new byte[64]); MemorySegment src = arena.allocate(64); @@ -184,7 +184,7 @@ void compressRejectsHeapDestination() { @Test void decompressRejectsHeapSource() { try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx sut = new ZstdDecompressCtx()) { + ZstdDecompressContext sut = new ZstdDecompressContext()) { // Given a heap-backed source segment handed to the zero-copy API MemorySegment heapSrc = MemorySegment.ofArray(new byte[64]); MemorySegment dst = arena.allocate(64); @@ -203,8 +203,8 @@ void decompressRejectsHeapSource() { void compressWithDictionaryRejectsHeapSource() { ZstdDictionary dict = trainDictionary(2000); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx sut = new ZstdCompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(dict)) { + ZstdCompressContext sut = new ZstdCompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(dict)) { // Given a heap-backed source handed to the dictionary zero-copy API MemorySegment heapSrc = MemorySegment.ofArray(new byte[64]); MemorySegment dst = arena.allocate(64); @@ -223,8 +223,8 @@ void compressWithDictionaryRejectsHeapSource() { void compressWithDictionaryRejectsHeapDestination() { ZstdDictionary dict = trainDictionary(2000); try (Arena arena = Arena.ofConfined(); - ZstdCompressCtx sut = new ZstdCompressCtx(); - ZstdCompressDict cdict = new ZstdCompressDict(dict)) { + ZstdCompressContext sut = new ZstdCompressContext(); + ZstdCompressDictionary cdict = new ZstdCompressDictionary(dict)) { // Given a heap-backed destination handed to the dictionary zero-copy API MemorySegment heapDst = MemorySegment.ofArray(new byte[64]); MemorySegment src = arena.allocate(64); @@ -243,8 +243,8 @@ void compressWithDictionaryRejectsHeapDestination() { void decompressWithDictionaryRejectsHeapSource() { ZstdDictionary dict = trainDictionary(2000); try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx sut = new ZstdDecompressCtx(); - ZstdDecompressDict ddict = new ZstdDecompressDict(dict)) { + ZstdDecompressContext sut = new ZstdDecompressContext(); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(dict)) { // Given a heap-backed source handed to the dictionary zero-copy API MemorySegment heapSrc = MemorySegment.ofArray(new byte[64]); MemorySegment dst = arena.allocate(64); @@ -263,8 +263,8 @@ void decompressWithDictionaryRejectsHeapSource() { void decompressWithDictionaryRejectsHeapDestination() { ZstdDictionary dict = trainDictionary(2000); try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx sut = new ZstdDecompressCtx(); - ZstdDecompressDict ddict = new ZstdDecompressDict(dict)) { + ZstdDecompressContext sut = new ZstdDecompressContext(); + ZstdDecompressDictionary ddict = new ZstdDecompressDictionary(dict)) { // Given a heap-backed destination handed to the dictionary zero-copy API MemorySegment heapDst = MemorySegment.ofArray(new byte[64]); MemorySegment src = arena.allocate(64); diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdStreamTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdStreamTest.java index e984560..4ba9cca 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdStreamTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdStreamTest.java @@ -137,7 +137,7 @@ void streamWithDictionaryDecodesWithOneShot() throws IOException { } // Then the one-shot context decodes it with the same dictionary - try (ZstdDecompressCtx ctx = new ZstdDecompressCtx()) { + try (ZstdDecompressContext ctx = new ZstdDecompressContext()) { assertThat(ctx.decompress(sink.toByteArray(), sample.length, dict)).isEqualTo(sample); } } @@ -229,7 +229,7 @@ void pledgedFrameDecodesZeroCopyIntoArenaInOneShot() throws IOException { // When it decodes straight into its arena and hands the result back as a ByteBuffer byte[] restored; try (Arena arena = Arena.ofConfined(); - ZstdDecompressCtx dctx = new ZstdDecompressCtx()) { + ZstdDecompressContext dctx = new ZstdDecompressContext()) { MemorySegment src = MemorySegment.ofBuffer(mmap); // zero-copy input view MemorySegment out = dctx.decompress(arena, src); // one allocation, zero copies ByteBuffer result = out.asByteBuffer(); // zero-copy hand-off out