mirror of
https://github.com/xpipe-io/xpipe.git
synced 2026-05-25 00:38:28 -04:00
Add more data sources and refactor some parts
This commit is contained in:
@@ -13,7 +13,6 @@ import java.util.List;
|
||||
@Builder
|
||||
@Jacksonized
|
||||
public class DataSourceConfig {
|
||||
String description;
|
||||
|
||||
@Singular
|
||||
List<Option> options;
|
||||
|
||||
@@ -7,6 +7,10 @@ import io.xpipe.core.data.type.TupleType;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Value;
|
||||
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
/**
|
||||
* A data source info instances contains all required
|
||||
* essential information of a specific data source type.
|
||||
@@ -34,12 +38,86 @@ public abstract class DataSourceInfo {
|
||||
this.rowCount = rowCount;
|
||||
}
|
||||
|
||||
public OptionalInt getRowCountIfPresent() {
|
||||
return getRowCount() != -1 ? OptionalInt.of(getRowCount()) : OptionalInt.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.TABLE;
|
||||
}
|
||||
}
|
||||
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Value
|
||||
@JsonTypeName("structure")
|
||||
public static class Structure extends DataSourceInfo {
|
||||
|
||||
@JsonCreator
|
||||
public Structure() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.STRUCTURE;
|
||||
}
|
||||
}
|
||||
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Value
|
||||
@JsonTypeName("text")
|
||||
public static class Text extends DataSourceInfo {
|
||||
Charset encoding;
|
||||
|
||||
@JsonCreator
|
||||
public Text(Charset encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.TEXT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Value
|
||||
@JsonTypeName("raw")
|
||||
public static class Raw extends DataSourceInfo {
|
||||
int byteCount;
|
||||
ByteOrder byteOrder;
|
||||
|
||||
@JsonCreator
|
||||
public Raw(int byteCount, ByteOrder byteOrder) {
|
||||
this.byteCount = byteCount;
|
||||
this.byteOrder = byteOrder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.RAW;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Value
|
||||
@JsonTypeName("archive")
|
||||
public static class Archive extends DataSourceInfo {
|
||||
int contentCount;
|
||||
|
||||
@JsonCreator
|
||||
public Archive(int contentCount) {
|
||||
this.contentCount = contentCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts this instance to a table info.
|
||||
*/
|
||||
@@ -50,4 +128,37 @@ public abstract class DataSourceInfo {
|
||||
|
||||
return (Table) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts this instance to a structure info.
|
||||
*/
|
||||
public Structure asStructure() {
|
||||
if (!getType().equals(DataSourceType.STRUCTURE)) {
|
||||
throw new IllegalStateException("Not a structure");
|
||||
}
|
||||
|
||||
return (Structure) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts this instance to a text info.
|
||||
*/
|
||||
public Text asText() {
|
||||
if (!getType().equals(DataSourceType.TEXT)) {
|
||||
throw new IllegalStateException("Not a text");
|
||||
}
|
||||
|
||||
return (Text) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts this instance to a raw info.
|
||||
*/
|
||||
public Raw asRaw() {
|
||||
if (!getType().equals(DataSourceType.RAW)) {
|
||||
throw new IllegalStateException("Not raw");
|
||||
}
|
||||
|
||||
return (Raw) this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,56 @@ import lombok.Value;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a reference to an X-Pipe data source.
|
||||
* Using {@link DataSourceReference} instances instead of {@link DataSourceId}
|
||||
* instances is mainly done for user convenience purposes.
|
||||
*
|
||||
* While a {@link DataSourceId} represents a unique and canonical identifier for an X-Pipe data source,
|
||||
* there also exist easier and shorter ways to address a data source.
|
||||
* This convenience comes at the price of ambiguity and instability for other types of references.
|
||||
*/
|
||||
public interface DataSourceReference {
|
||||
|
||||
static DataSourceReference empty() {
|
||||
return new Empty();
|
||||
/**
|
||||
* Creates a reference that always refers to the latest data source.
|
||||
*
|
||||
* @see Latest
|
||||
*/
|
||||
static DataSourceReference latest() {
|
||||
return new Latest();
|
||||
}
|
||||
|
||||
public static DataSourceReference parse(String s) {
|
||||
/**
|
||||
* Creates a reference using only the data source name.
|
||||
*
|
||||
* @see Name
|
||||
*/
|
||||
static DataSourceReference name(String name) {
|
||||
return new Name(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for {@link #id(DataSourceId)}
|
||||
*
|
||||
* @see DataSourceId#fromString(String)
|
||||
*/
|
||||
static DataSourceReference id(String id) {
|
||||
return new Id(DataSourceId.fromString(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a reference by using a canonical data source id.
|
||||
*
|
||||
* @see Id
|
||||
*/
|
||||
static DataSourceReference id(DataSourceId id) {
|
||||
return new Id(id);
|
||||
}
|
||||
|
||||
static DataSourceReference parse(String s) {
|
||||
if (s == null || s.trim().length() == 0) {
|
||||
return new Empty();
|
||||
return new Latest();
|
||||
}
|
||||
|
||||
if (s.contains(":")) {
|
||||
@@ -27,15 +68,23 @@ public interface DataSourceReference {
|
||||
enum Type {
|
||||
ID,
|
||||
NAME,
|
||||
EMPTY
|
||||
LATEST
|
||||
}
|
||||
|
||||
Type getType();
|
||||
DataSourceId getId();
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Returns the internal string representation of this reference.
|
||||
*/
|
||||
String toRefString();
|
||||
|
||||
String toString();
|
||||
|
||||
/**
|
||||
* A wrapper class for {@link DataSourceId} instances.
|
||||
*/
|
||||
@Value
|
||||
@AllArgsConstructor
|
||||
static class Id implements DataSourceReference {
|
||||
@@ -81,6 +130,11 @@ public interface DataSourceReference {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Using only the data source name allows for a shorthand way of referring to data sources.
|
||||
* This works as long there are no two different data sources with the same name in different collections.
|
||||
* If this name reference is ambiguous, the data source referral fails.
|
||||
*/
|
||||
@Value
|
||||
@AllArgsConstructor
|
||||
static class Name implements DataSourceReference {
|
||||
@@ -126,7 +180,12 @@ public interface DataSourceReference {
|
||||
}
|
||||
}
|
||||
|
||||
static class Empty implements DataSourceReference {
|
||||
/**
|
||||
* Specifying the latest reference allows the user to always address the latest data source.
|
||||
* Data source referral this way is unstable however as adding or
|
||||
* removing data sources might change the referral behaviour and is therefore not recommended.
|
||||
*/
|
||||
static class Latest implements DataSourceReference {
|
||||
|
||||
@Override
|
||||
public String toRefString() {
|
||||
@@ -135,7 +194,7 @@ public interface DataSourceReference {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "none";
|
||||
return "latest";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -151,7 +210,7 @@ public interface DataSourceReference {
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return Type.EMPTY;
|
||||
return Type.LATEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.xpipe.core.source;
|
||||
|
||||
import io.xpipe.core.store.DataStore;
|
||||
|
||||
public class TextDataSourceDescriptor<DS extends DataStore> implements DataSourceDescriptor<DS> {
|
||||
|
||||
@Override
|
||||
public DataSourceInfo determineInfo(DS store) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.TEXT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.xpipe.core.source;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public interface TextReadConnection extends DataSourceConnection {
|
||||
|
||||
Charset getEncoding();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.xpipe.core.source;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
public interface TextWriteConnection extends DataSourceConnection {
|
||||
|
||||
OutputStream getOutputStream();
|
||||
}
|
||||
Reference in New Issue
Block a user