diff --git a/inset-core/src/main/java/slatepowered/inset/datastore/DataItem.java b/inset-core/src/main/java/slatepowered/inset/datastore/DataItem.java index 2eef49e..206385c 100644 --- a/inset-core/src/main/java/slatepowered/inset/datastore/DataItem.java +++ b/inset-core/src/main/java/slatepowered/inset/datastore/DataItem.java @@ -284,6 +284,11 @@ public FindOperation find() { return new FindOperation<>(datastore, null).completeSuccessfully(FindResult.CACHED, this); } + @Override + public Optional> findCached() { + return Optional.of(this); + } + @Override @SuppressWarnings("unchecked") protected V projectInterface(ProjectionInterface projectionInterface) { @@ -300,7 +305,7 @@ protected V projectInterface(ProjectionInterface projectionInterface) { * * @return The future. */ - public CompletableFuture> findAsync() { + public CompletableFuture> fetchAsync() { return datastore.getSourceTable() .findOneAsync(Query.byKey(key)) .thenApply(result -> this.decode(result.input()).fetchedNow()); diff --git a/inset-core/src/main/java/slatepowered/inset/query/PartialItem.java b/inset-core/src/main/java/slatepowered/inset/query/PartialItem.java index a2ed99a..3580d13 100644 --- a/inset-core/src/main/java/slatepowered/inset/query/PartialItem.java +++ b/inset-core/src/main/java/slatepowered/inset/query/PartialItem.java @@ -12,6 +12,7 @@ import java.lang.reflect.Type; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.CompletableFuture; /** @@ -115,6 +116,14 @@ public FindOperation fetch() { return find().thenFetchIfCached(); } + /** + * Find a cached item for this partial item if present, + * otherwise return an empty optional. + * + * @return The cached item if present. + */ + public abstract Optional> findCached(); + /** * Create an array of numbers easily comparable to other numbers * of the same data which is to represent the order. diff --git a/inset-core/src/main/java/slatepowered/inset/source/SourceFoundItem.java b/inset-core/src/main/java/slatepowered/inset/source/SourceFoundItem.java index 570cc8e..9381b2a 100644 --- a/inset-core/src/main/java/slatepowered/inset/source/SourceFoundItem.java +++ b/inset-core/src/main/java/slatepowered/inset/source/SourceFoundItem.java @@ -6,12 +6,10 @@ import slatepowered.inset.datastore.Datastore; import slatepowered.inset.internal.ProjectionInterface; import slatepowered.inset.operation.Sorting; -import slatepowered.inset.query.FindAllOperation; -import slatepowered.inset.query.FindOperation; -import slatepowered.inset.query.FindResult; -import slatepowered.inset.query.PartialItem; +import slatepowered.inset.query.*; import java.lang.reflect.Type; +import java.util.Optional; /** * Represents a {@link PartialItem} retrieved from a data source. @@ -114,4 +112,9 @@ public double[] createFastOrderCoefficients(String[] fields, Sorting sorting) { return arr; } + @Override + public Optional> findCached() { + return Optional.ofNullable(assertQualified().findOneCached(Query.byKey(getKey()))); + } + }