Skip to content

Commit

Permalink
added PartialItem#findCached
Browse files Browse the repository at this point in the history
  • Loading branch information
orbyfied committed Dec 10, 2023
1 parent 0c73c60 commit 765ee02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ public FindOperation<K, T> find() {
return new FindOperation<>(datastore, null).completeSuccessfully(FindResult.CACHED, this);
}

@Override
public Optional<DataItem<K, T>> findCached() {
return Optional.of(this);
}

@Override
@SuppressWarnings("unchecked")
protected <V> V projectInterface(ProjectionInterface projectionInterface) {
Expand All @@ -300,7 +305,7 @@ protected <V> V projectInterface(ProjectionInterface projectionInterface) {
*
* @return The future.
*/
public CompletableFuture<DataItem<K, T>> findAsync() {
public CompletableFuture<DataItem<K, T>> fetchAsync() {
return datastore.getSourceTable()
.findOneAsync(Query.byKey(key))
.thenApply(result -> this.decode(result.input()).fetchedNow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.lang.reflect.Type;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -115,6 +116,14 @@ public FindOperation<K, T> 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<DataItem<K, T>> findCached();

/**
* Create an array of numbers easily comparable to other numbers
* of the same data which is to represent the order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -114,4 +112,9 @@ public double[] createFastOrderCoefficients(String[] fields, Sorting sorting) {
return arr;
}

@Override
public Optional<DataItem<K, T>> findCached() {
return Optional.ofNullable(assertQualified().findOneCached(Query.byKey(getKey())));
}

}

0 comments on commit 765ee02

Please sign in to comment.