Skip to content

Commit 1750aa1

Browse files
dfa1claude
andcommitted
refactor(encoding): split Encoding into Decoder + Encoder interfaces [Phase 1/6]
Encoding is now a marker that extends EncodingDecoder + EncodingEncoder. The two narrower interfaces carry the read-side and write-side method signatures (encodingId + accepts + decode for decoders; encodingId + accepts + encode + encodeCascade for encoders). Every existing Encoding implementation continues to satisfy both contracts without code changes, so the migration is transparent at this phase. Read-only call sites (ReadRegistry, FlatSegmentDecoder) can already type-narrow to EncodingDecoder; write-only call sites (VortexWriter, CascadingCompressor) can type-narrow to EncodingEncoder. Later phases lift one encoding family at a time into separate *EncodingDecoder and *EncodingEncoder implementations that live in the reader and writer modules respectively; once the last family is lifted, the bifunctional Encoding interface is removed. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent b6437ee commit 1750aa1

3 files changed

Lines changed: 77 additions & 41 deletions

File tree

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,11 @@
11
package io.github.dfa1.vortex.encoding;
22

3-
import io.github.dfa1.vortex.core.DType;
4-
import io.github.dfa1.vortex.core.array.Array;
5-
6-
/// Combines encode and decode for one encoding type.
7-
/// Register via [Registry] — implementations are discoverable via ServiceLoader.
8-
public interface Encoding {
9-
/// Returns the encoding id for this encoding.
10-
///
11-
/// @return encoding id
12-
EncodingId encodingId();
13-
14-
/// Decodes an array node from the file using the provided context.
15-
///
16-
/// @param ctx decoding context containing buffers, dtype, row count, and child registry
17-
/// @return decoded array
18-
Array decode(DecodeContext ctx);
19-
20-
/// Returns `true` if this encoding can encode the given dtype.
21-
///
22-
/// @param dtype the dtype to test
23-
/// @return `true` if this encoding accepts `dtype`
24-
boolean accepts(DType dtype);
25-
26-
/// Encodes {@code data} to bytes using the provided arena for output buffer allocation.
27-
///
28-
/// @param dtype logical type of the data
29-
/// @param data the data to encode (type depends on encoding; typically a primitive array)
30-
/// @param ctx encoding context supplying the arena for output buffer allocation
31-
/// @return encode result containing the root node, buffers, and optional stats
32-
EncodeResult encode(DType dtype, Object data, EncodeContext ctx);
33-
34-
/// Cascade-aware encode: returns a partial step with open child slots.
35-
/// Default wraps the terminal {@link #encode} result; override to expose children.
36-
///
37-
/// @param dtype the logical type of the data
38-
/// @param data the data to encode
39-
/// @param ctx encoding context supplying the arena, registry, and cascade parameters
40-
/// @return cascade step with optional open child slots
41-
default CascadeStep encodeCascade(DType dtype, Object data, EncodeContext ctx) {
42-
return CascadeStep.terminal(encode(dtype, data, ctx));
43-
}
3+
/// Bifunctional encoding interface that combines the read-side {@link EncodingDecoder}
4+
/// surface and the write-side {@link EncodingEncoder} surface. Existing implementations
5+
/// satisfy both contracts on a single class; ADR 0001 progressively peels these apart
6+
/// so that a read-only deployment carries only decoders.
7+
///
8+
/// <p>Register via {@link Registry} — implementations are discoverable via
9+
/// {@link java.util.ServiceLoader}.
10+
public interface Encoding extends EncodingDecoder, EncodingEncoder {
4411
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.dfa1.vortex.encoding;
2+
3+
import io.github.dfa1.vortex.core.DType;
4+
import io.github.dfa1.vortex.core.array.Array;
5+
6+
/// Read-side surface of an encoding. Exposes only the metadata required to dispatch
7+
/// a decode and the {@link #decode(DecodeContext)} entry point itself.
8+
///
9+
/// <p>ADR 0001 Phase 1: {@link Encoding} now extends this interface for backward
10+
/// compatibility while encoding implementations are still bifunctional. Later phases
11+
/// peel encoder implementations into a separate write runtime, at which point
12+
/// {@code EncodingDecoder} becomes the canonical read-side type and lives in the
13+
/// {@code reader} module.
14+
public interface EncodingDecoder {
15+
16+
/// @return the wire identifier of this encoding
17+
EncodingId encodingId();
18+
19+
/// @param dtype the dtype to test
20+
/// @return {@code true} if this encoding can decode arrays of {@code dtype}
21+
boolean accepts(DType dtype);
22+
23+
/// Decodes an array node from the file using the provided context.
24+
///
25+
/// @param ctx decoding context containing buffers, dtype, row count, and child registry
26+
/// @return decoded array
27+
Array decode(DecodeContext ctx);
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.github.dfa1.vortex.encoding;
2+
3+
import io.github.dfa1.vortex.core.DType;
4+
5+
/// Write-side surface of an encoding. Exposes only the metadata required to pick an
6+
/// encoder for a dtype and the {@link #encode(DType, Object, EncodeContext)} entry
7+
/// point itself.
8+
///
9+
/// <p>ADR 0001 Phase 1: {@link Encoding} now extends this interface for backward
10+
/// compatibility while encoding implementations are still bifunctional. Later phases
11+
/// peel encoder implementations into a separate write runtime, at which point
12+
/// {@code EncodingEncoder} becomes the canonical write-side type and lives in the
13+
/// {@code writer} module.
14+
public interface EncodingEncoder {
15+
16+
/// @return the wire identifier of this encoding
17+
EncodingId encodingId();
18+
19+
/// @param dtype the dtype to test
20+
/// @return {@code true} if this encoding can encode arrays of {@code dtype}
21+
boolean accepts(DType dtype);
22+
23+
/// Encodes {@code data} to bytes using the provided arena for output buffer allocation.
24+
///
25+
/// @param dtype logical type of the data
26+
/// @param data the data to encode (type depends on encoding; typically a primitive array)
27+
/// @param ctx encoding context supplying the arena for output buffer allocation
28+
/// @return encode result containing the root node, buffers, and optional stats
29+
EncodeResult encode(DType dtype, Object data, EncodeContext ctx);
30+
31+
/// Cascade-aware encode: returns a partial step with open child slots.
32+
/// Default wraps the terminal {@link #encode} result; override to expose children.
33+
///
34+
/// @param dtype the logical type of the data
35+
/// @param data the data to encode
36+
/// @param ctx encoding context supplying the arena, registry, and cascade parameters
37+
/// @return cascade step with optional open child slots
38+
default CascadeStep encodeCascade(DType dtype, Object data, EncodeContext ctx) {
39+
return CascadeStep.terminal(encode(dtype, data, ctx));
40+
}
41+
}

0 commit comments

Comments
 (0)