This commit is contained in:
Christopher Schnick 2021-12-18 03:21:03 +01:00
parent 562f02b607
commit a6bb824bd3
22 changed files with 149 additions and 151 deletions

View file

@ -61,7 +61,7 @@ public class DataStructureNodePointer {
return path; return path;
} }
public static interface Element { public interface Element {
DataStructureNode tryMatch(DataStructureNode n); DataStructureNode tryMatch(DataStructureNode n);
@ -70,7 +70,7 @@ public class DataStructureNodePointer {
} }
} }
public static final record NameElement(String name) implements Element { public record NameElement(String name) implements Element {
@Override @Override
public DataStructureNode tryMatch(DataStructureNode n) { public DataStructureNode tryMatch(DataStructureNode n) {
@ -88,7 +88,7 @@ public class DataStructureNodePointer {
} }
} }
public static final record IndexElement(int index) implements Element { public record IndexElement(int index) implements Element {
@Override @Override
public DataStructureNode tryMatch(DataStructureNode n) { public DataStructureNode tryMatch(DataStructureNode n) {
@ -104,7 +104,7 @@ public class DataStructureNodePointer {
} }
} }
public static final record SupplierElement(Supplier<String> keySupplier) implements Element { public record SupplierElement(Supplier<String> keySupplier) implements Element {
@Override @Override
public DataStructureNode tryMatch(DataStructureNode n) { public DataStructureNode tryMatch(DataStructureNode n) {
@ -126,7 +126,7 @@ public class DataStructureNodePointer {
} }
} }
public static final record FunctionElement(Function<DataStructureNode, String> keyFunc) implements Element { public record FunctionElement(Function<DataStructureNode, String> keyFunc) implements Element {
@Override @Override
public DataStructureNode tryMatch(DataStructureNode n) { public DataStructureNode tryMatch(DataStructureNode n) {
@ -148,7 +148,7 @@ public class DataStructureNodePointer {
} }
} }
public static final record SelectorElement(Predicate<DataStructureNode> selector) implements Element { public record SelectorElement(Predicate<DataStructureNode> selector) implements Element {
@Override @Override
public DataStructureNode tryMatch(DataStructureNode n) { public DataStructureNode tryMatch(DataStructureNode n) {

View file

@ -9,22 +9,21 @@ import java.util.List;
public class GenericArrayReader implements GenericAbstractReader { public class GenericArrayReader implements GenericAbstractReader {
public static GenericArrayReader newReader(int length) {
var ar = new GenericArrayReader();
ar.onArrayStart(length);
return ar;
}
private boolean initialized; private boolean initialized;
private List<DataStructureNode> nodes; private List<DataStructureNode> nodes;
private int length; private int length;
private int currentIndex = 0; private int currentIndex = 0;
private GenericAbstractReader currentReader; private GenericAbstractReader currentReader;
private DataStructureNode created; private DataStructureNode created;
public GenericArrayReader() { public GenericArrayReader() {
} }
public static GenericArrayReader newReader(int length) {
var ar = new GenericArrayReader();
ar.onArrayStart(length);
return ar;
}
private void init(int length) { private void init(int length) {
this.length = length; this.length = length;
this.nodes = new ArrayList<>(length); this.nodes = new ArrayList<>(length);

View file

@ -2,7 +2,8 @@ package io.xpipe.core.data.generic;
public interface GenericDataStreamCallback { public interface GenericDataStreamCallback {
default void onName(String name) {} default void onName(String name) {
}
default void onArrayStart(int length) { default void onArrayStart(int length) {
} }

View file

@ -19,7 +19,7 @@ public class GenericDataStreamParser {
public static List<DataStructureNode> readN(InputStream in, int n) throws IOException { public static List<DataStructureNode> readN(InputStream in, int n) throws IOException {
var list = new ArrayList<DataStructureNode>(); var list = new ArrayList<DataStructureNode>();
var reader = new GenericDataStructureNodeReader(); var reader = new GenericDataStructureNodeReader();
for (int i = 0; i < n ; i++) { for (int i = 0; i < n; i++) {
read(in, reader); read(in, reader);
list.add(reader.create()); list.add(reader.create());
} }

View file

@ -1,21 +1,15 @@
package io.xpipe.core.data.generic; package io.xpipe.core.data.generic;
import io.xpipe.core.data.DataStructureNode; import io.xpipe.core.data.DataStructureNode;
import io.xpipe.core.data.node.SimpleTupleNode;
import io.xpipe.core.data.node.TupleNode; import io.xpipe.core.data.node.TupleNode;
import io.xpipe.core.data.node.ValueNode; import io.xpipe.core.data.node.ValueNode;
import io.xpipe.core.data.node.SimpleTupleNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GenericTupleReader implements GenericAbstractReader { public class GenericTupleReader implements GenericAbstractReader {
public static GenericTupleReader newReader(int length) {
var tr = new GenericTupleReader();
tr.onTupleStart(length);
return tr;
}
private boolean initialized; private boolean initialized;
private int length; private int length;
private List<String> names; private List<String> names;
@ -23,10 +17,15 @@ public class GenericTupleReader implements GenericAbstractReader {
private int currentIndex = 0; private int currentIndex = 0;
private GenericAbstractReader currentReader; private GenericAbstractReader currentReader;
private DataStructureNode created; private DataStructureNode created;
public GenericTupleReader() { public GenericTupleReader() {
} }
public static GenericTupleReader newReader(int length) {
var tr = new GenericTupleReader();
tr.onTupleStart(length);
return tr;
}
private boolean hasReader() { private boolean hasReader() {
return currentReader != null; return currentReader != null;
} }

View file

@ -5,7 +5,10 @@ import io.xpipe.core.data.type.DataType;
import io.xpipe.core.data.type.TupleType; import io.xpipe.core.data.type.TupleType;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public class SimpleTupleNode extends TupleNode { public class SimpleTupleNode extends TupleNode {

View file

@ -10,38 +10,6 @@ import java.util.stream.Collectors;
public abstract class TupleNode extends DataStructureNode { public abstract class TupleNode extends DataStructureNode {
@Value
public static class KeyValue {
String key;
DataStructureNode value;
}
public static class Builder {
private final List<KeyValue> entries = new ArrayList<>();
public Builder add(String name, DataStructureNode node) {
Objects.requireNonNull(node);
entries.add(new KeyValue(name, node));
return this;
}
public Builder add(DataStructureNode node) {
Objects.requireNonNull(node);
entries.add(new KeyValue(null, node));
return this;
}
public TupleNode build() {
boolean hasKeys = entries.stream().anyMatch(kv -> kv.key != null);
return hasKeys ? TupleNode.wrap(
entries.stream().map(kv -> kv.key).toList(),
entries.stream().map(kv -> kv.value).toList()) :
TupleNode.wrap(entries.stream().map(kv -> kv.value).toList());
}
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -112,4 +80,36 @@ public abstract class TupleNode extends DataStructureNode {
public abstract List<String> getNames(); public abstract List<String> getNames();
public abstract List<DataStructureNode> getNodes(); public abstract List<DataStructureNode> getNodes();
@Value
public static class KeyValue {
String key;
DataStructureNode value;
}
public static class Builder {
private final List<KeyValue> entries = new ArrayList<>();
public Builder add(String name, DataStructureNode node) {
Objects.requireNonNull(node);
entries.add(new KeyValue(name, node));
return this;
}
public Builder add(DataStructureNode node) {
Objects.requireNonNull(node);
entries.add(new KeyValue(null, node));
return this;
}
public TupleNode build() {
boolean hasKeys = entries.stream().anyMatch(kv -> kv.key != null);
return hasKeys ? TupleNode.wrap(
entries.stream().map(kv -> kv.key).toList(),
entries.stream().map(kv -> kv.value).toList()) :
TupleNode.wrap(entries.stream().map(kv -> kv.value).toList());
}
}
} }

View file

@ -10,7 +10,10 @@ import java.nio.charset.StandardCharsets;
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public abstract class ValueNode extends DataStructureNode { public abstract class ValueNode extends DataStructureNode {
private static final byte[] NULL = new byte[] {0}; private static final byte[] NULL = new byte[]{0};
protected ValueNode() {
}
public static ValueNode immutable(byte[] data) { public static ValueNode immutable(byte[] data) {
return new ImmutableValueNode(data); return new ImmutableValueNode(data);
@ -40,9 +43,6 @@ public abstract class ValueNode extends DataStructureNode {
return mutable(o); return mutable(o);
} }
protected ValueNode() {
}
@Override @Override
public abstract DataStructureNode setRawData(byte[] data); public abstract DataStructureNode setRawData(byte[] data);

View file

@ -11,6 +11,12 @@ import java.util.List;
@EqualsAndHashCode @EqualsAndHashCode
public class ArrayType implements DataType { public class ArrayType implements DataType {
private final DataType sharedType;
public ArrayType(DataType sharedType) {
this.sharedType = sharedType;
}
public static ArrayType ofWildcard() { public static ArrayType ofWildcard() {
return new ArrayType(WildcardType.of()); return new ArrayType(WildcardType.of());
} }
@ -25,12 +31,6 @@ public class ArrayType implements DataType {
return new ArrayType(eq ? first : WildcardType.of()); return new ArrayType(eq ? first : WildcardType.of());
} }
private final DataType sharedType;
public ArrayType(DataType sharedType) {
this.sharedType = sharedType;
}
public boolean isSimple() { public boolean isSimple() {
return hasSharedType() && getSharedType().isValue(); return hasSharedType() && getSharedType().isValue();
} }

View file

@ -13,8 +13,8 @@ import java.util.List;
@EqualsAndHashCode @EqualsAndHashCode
public class TupleType implements DataType { public class TupleType implements DataType {
private List<String> names; private final List<String> names;
private List<DataType> types; private final List<DataType> types;
@JsonCreator @JsonCreator
private TupleType(List<String> names, List<DataType> types) { private TupleType(List<String> names, List<DataType> types) {

View file

@ -9,14 +9,14 @@ import lombok.EqualsAndHashCode;
@EqualsAndHashCode @EqualsAndHashCode
public class ValueType implements DataType { public class ValueType implements DataType {
public static ValueType of() {
return new ValueType();
}
private ValueType() { private ValueType() {
} }
public static ValueType of() {
return new ValueType();
}
@Override @Override
public String getName() { public String getName() {
return "value"; return "value";

View file

@ -5,14 +5,14 @@ import io.xpipe.core.data.type.callback.DataTypeCallback;
public class WildcardType implements DataType { public class WildcardType implements DataType {
public static WildcardType of() {
return new WildcardType();
}
private WildcardType() { private WildcardType() {
} }
public static WildcardType of() {
return new WildcardType();
}
@Override @Override
public String getName() { public String getName() {
return "wildcard"; return "wildcard";

View file

@ -9,7 +9,7 @@ import java.util.function.Consumer;
public interface DataTypeCallback { public interface DataTypeCallback {
public static DataTypeCallback flatten(Consumer<DataType> typeConsumer) { static DataTypeCallback flatten(Consumer<DataType> typeConsumer) {
return new DataTypeCallback() { return new DataTypeCallback() {
@Override @Override
public void onValue() { public void onValue() {

View file

@ -46,7 +46,7 @@ public class FlatArrayTypeCallback implements DataTypeCallback {
arrayDepth++; arrayDepth++;
} }
public static interface FlatCallback { public interface FlatCallback {
default void onValue() { default void onValue() {
} }

View file

@ -14,6 +14,13 @@ import java.util.function.Consumer;
public class TypedDataStreamParser { public class TypedDataStreamParser {
private final DataType dataType;
private GenericDataStructureNodeReader genericReader;
public TypedDataStreamParser(DataType dataType) {
this.dataType = dataType;
}
public boolean hasNext(InputStream in) throws IOException { public boolean hasNext(InputStream in) throws IOException {
var b = in.read(); var b = in.read();
if (b == -1) { if (b == -1) {
@ -98,13 +105,6 @@ public class TypedDataStreamParser {
cb.onTupleEnd(); cb.onTupleEnd();
} }
private DataType dataType;
private GenericDataStructureNodeReader genericReader;
public TypedDataStreamParser(DataType dataType) {
this.dataType = dataType;
}
private GenericDataStructureNodeReader getGenericReader() { private GenericDataStructureNodeReader getGenericReader() {
if (genericReader == null) { if (genericReader == null) {
genericReader = new GenericDataStructureNodeReader(); genericReader = new GenericDataStructureNodeReader();

View file

@ -23,11 +23,9 @@ public class TypedDataStreamWriter {
private static void write(OutputStream out, DataStructureNode node, DataType type) throws IOException { private static void write(OutputStream out, DataStructureNode node, DataType type) throws IOException {
if (type.isTuple() && node.isTuple()) { if (type.isTuple() && node.isTuple()) {
writeTuple(out, (SimpleTupleNode) node, (TupleType) type); writeTuple(out, (SimpleTupleNode) node, (TupleType) type);
} } else if (node.isArray() && type.isArray()) {
else if (node.isArray() && type.isArray()) {
writeArray(out, (ArrayNode) node, (ArrayType) type); writeArray(out, (ArrayNode) node, (ArrayType) type);
} } else if (node.isValue() && type.isValue()) {
else if (node.isValue() && type.isValue()) {
writeValue(out, (ValueNode) node); writeValue(out, (ValueNode) node);
} else { } else {
throw new AssertionError(); throw new AssertionError();

View file

@ -17,23 +17,14 @@ import java.util.Stack;
public class TypedDataStructureNodeReader implements TypedAbstractReader { public class TypedDataStructureNodeReader implements TypedAbstractReader {
public static TypedDataStructureNodeReader mutable(DataType type) {
return new TypedDataStructureNodeReader(type, false);
}
public static TypedDataStructureNodeReader immutable(DataType type) {
return new TypedDataStructureNodeReader(type, true);
}
private int currentDataTypeIndex;
private final List<DataType> flattened; private final List<DataType> flattened;
private final Stack<List<DataStructureNode>> children; private final Stack<List<DataStructureNode>> children;
private final Stack<DataStructureNode> nodes; private final Stack<DataStructureNode> nodes;
private final boolean makeImmutable;
private int currentDataTypeIndex;
private DataStructureNode readNode; private DataStructureNode readNode;
private boolean initialized; private boolean initialized;
private int arrayDepth; private int arrayDepth;
private final boolean makeImmutable;
private TypedDataStructureNodeReader(DataType type, boolean makeImmutable) { private TypedDataStructureNodeReader(DataType type, boolean makeImmutable) {
flattened = new ArrayList<>(); flattened = new ArrayList<>();
children = new Stack<>(); children = new Stack<>();
@ -42,6 +33,14 @@ public class TypedDataStructureNodeReader implements TypedAbstractReader {
this.makeImmutable = makeImmutable; this.makeImmutable = makeImmutable;
} }
public static TypedDataStructureNodeReader mutable(DataType type) {
return new TypedDataStructureNodeReader(type, false);
}
public static TypedDataStructureNodeReader immutable(DataType type) {
return new TypedDataStructureNodeReader(type, true);
}
@Override @Override
public void onNodeBegin() { public void onNodeBegin() {
if (nodes.size() != 0 || children.size() != 0) { if (nodes.size() != 0 || children.size() != 0) {

View file

@ -13,10 +13,10 @@ import java.util.Stack;
public class TypedReusableDataStructureNodeReader implements TypedAbstractReader { public class TypedReusableDataStructureNodeReader implements TypedAbstractReader {
private TypedDataStructureNodeReader initialReader;
private DataStructureNode node;
private final List<DataType> flattened; private final List<DataType> flattened;
private Stack<Integer> indices; private final TypedDataStructureNodeReader initialReader;
private DataStructureNode node;
private final Stack<Integer> indices;
private int arrayDepth; private int arrayDepth;
public TypedReusableDataStructureNodeReader(DataType type) { public TypedReusableDataStructureNodeReader(DataType type) {

View file

@ -7,7 +7,7 @@ import lombok.Getter;
/** /**
* Represents a reference to an XPipe data source. * Represents a reference to an XPipe data source.
* This reference consists out of a collection name and an entry name to allow for better organisation. * This reference consists out of a collection name and an entry name to allow for better organisation.
* * <p>
* To allow for a simple usage of data source ids, the collection and entry names are trimmed and * To allow for a simple usage of data source ids, the collection and entry names are trimmed and
* converted to lower case names when creating them. * converted to lower case names when creating them.
* The two names are separated by a colon and are therefore not allowed to contain colons themselves. * The two names are separated by a colon and are therefore not allowed to contain colons themselves.
@ -20,12 +20,19 @@ import lombok.Getter;
public class DataSourceId { public class DataSourceId {
public static final char SEPARATOR = ':'; public static final char SEPARATOR = ':';
private final String collectionName;
private final String entryName;
@JsonCreator
private DataSourceId(String collectionName, String entryName) {
this.collectionName = collectionName;
this.entryName = entryName;
}
/** /**
* Creates a new data source id from a collection name and an entry name. * Creates a new data source id from a collection name and an entry name.
* *
* @param collectionName the collection name, which must be not null and not empty * @param collectionName the collection name, which must be not null and not empty
* @param entryName the entry name, which must be not null and not empty * @param entryName the entry name, which must be not null and not empty
* @throws IllegalArgumentException if any name is not valid * @throws IllegalArgumentException if any name is not valid
*/ */
public static DataSourceId create(String collectionName, String entryName) { public static DataSourceId create(String collectionName, String entryName) {
@ -52,15 +59,6 @@ public class DataSourceId {
return new DataSourceId(collectionName, entryName); return new DataSourceId(collectionName, entryName);
} }
private final String collectionName;
private final String entryName;
@JsonCreator
private DataSourceId(String collectionName, String entryName) {
this.collectionName = collectionName;
this.entryName = entryName;
}
/** /**
* Creates a new data source id from a string representation. * Creates a new data source id from a string representation.
* The string must contain exactly one colon and non-empty names. * The string must contain exactly one colon and non-empty names.

View file

@ -1,6 +1,8 @@
package io.xpipe.core.store; package io.xpipe.core.store;
import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import java.io.IOException; import java.io.IOException;

View file

@ -20,40 +20,6 @@ import java.nio.file.Path;
public class CoreJacksonModule extends SimpleModule { public class CoreJacksonModule extends SimpleModule {
public static class CharsetSerializer extends JsonSerializer<Charset> {
@Override
public void serialize(Charset value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeString(value.name());
}
}
public static class CharsetDeserializer extends JsonDeserializer<Charset> {
@Override
public Charset deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return Charset.forName(p.getValueAsString());
}
}
public static class LocalPathSerializer extends JsonSerializer<Path> {
@Override
public void serialize(Path value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeString(value.toString());
}
}
public static class LocalPathDeserializer extends JsonDeserializer<Path> {
@Override
public Path deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return Path.of(p.getValueAsString());
}
}
@Override @Override
public void setupModule(SetupContext context) { public void setupModule(SetupContext context) {
context.registerSubtypes( context.registerSubtypes(
@ -69,4 +35,38 @@ public class CoreJacksonModule extends SimpleModule {
addSerializer(Path.class, new LocalPathSerializer()); addSerializer(Path.class, new LocalPathSerializer());
addDeserializer(Path.class, new LocalPathDeserializer()); addDeserializer(Path.class, new LocalPathDeserializer());
} }
public static class CharsetSerializer extends JsonSerializer<Charset> {
@Override
public void serialize(Charset value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeString(value.name());
}
}
public static class CharsetDeserializer extends JsonDeserializer<Charset> {
@Override
public Charset deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return Charset.forName(p.getValueAsString());
}
}
public static class LocalPathSerializer extends JsonSerializer<Path> {
@Override
public void serialize(Path value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeString(value.toString());
}
}
public static class LocalPathDeserializer extends JsonDeserializer<Path> {
@Override
public Path deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return Path.of(p.getValueAsString());
}
}
} }

View file

@ -28,8 +28,7 @@ public class JacksonHelper {
init = true; init = true;
} }
private static List<Module> findModules(ModuleLayer layer) private static List<Module> findModules(ModuleLayer layer) {
{
ArrayList<Module> modules = new ArrayList<Module>(); ArrayList<Module> modules = new ArrayList<Module>();
ServiceLoader<Module> loader = ServiceLoader.load(layer, Module.class); ServiceLoader<Module> loader = ServiceLoader.load(layer, Module.class);
for (Module module : loader) { for (Module module : loader) {