Skip to content

Commit

Permalink
Define reader/writer interfaces with variant
Browse files Browse the repository at this point in the history
  • Loading branch information
RCHowell committed Oct 31, 2024
1 parent f0569bd commit 2d875c3
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 136 deletions.
21 changes: 21 additions & 0 deletions partiql-spi/src/main/java/org/partiql/spi/value/DatumReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.partiql.spi.value;

import org.jetbrains.annotations.Nullable;

/**
* The {@link DatumReader} interface is a low-level reader interface for reading streams of PartiQL data.
* <br>
* {@see java.io.Reader}
* <br>
* TODO
* - public void reset();
* - public void skip(long n);
*/
public interface DatumReader extends AutoCloseable {

/**
* @return next Datum or null.
*/
@Nullable
public Datum read();
}
16 changes: 16 additions & 0 deletions partiql-spi/src/main/java/org/partiql/spi/value/DatumWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.partiql.spi.value;

/**
* The {@link DatumWriter} interface is a low-level writer interface for writing streams of PartiQL data.
* <br>
* {@see java.io.Writer}
*/
public interface DatumWriter extends AutoCloseable {

/**
* Like java.io.Reader with combined `append` and `write` since this does not implement Appendable.
*
* @param datum to write.
*/
public DatumWriter write(Datum datum);
}
43 changes: 43 additions & 0 deletions partiql-spi/src/main/java/org/partiql/spi/value/Variant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.partiql.spi.value;

import org.jetbrains.annotations.NotNull;

import java.nio.charset.Charset;

/**
* Variant is a {@link Datum} with the ability to pack and unpack a value.
*
* @param <T>
*/
public interface Variant<T> extends Datum {

/**
* Unpack the inner variant value.
*
* @return T
*/
T unpack();

/**
* TODO move to writer?
* <br>
* Pack the variant into a byte array.
*
* @return byte[]
*/
default byte[] pack() {
throw new UnsupportedOperationException("variant does not have a byte[] encoding.");
}

/**
* TODO move to writer?
* <br>
* Pack the variant into a byte array with the given charset.
*
* @param charset charset
* @return byte[]
*/
default byte[] pack(@NotNull Charset charset) {
throw new UnsupportedOperationException("variant does not have an encoding for charset: " + charset.name());
}
}
Loading

0 comments on commit 2d875c3

Please sign in to comment.