CacheScoredValue

This commit is contained in:
redkale
2023-09-12 08:21:15 +08:00
parent 42bf6ec73b
commit ddfabc0010
3 changed files with 85 additions and 92 deletions

View File

@@ -1585,7 +1585,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
return runFuture(() -> {
List<Object> list = new ArrayList<>();
for (CacheScoredValue v : values) {
list.add(new CacheScoredValue.NumberScoredValue(v));
list.add(new CacheScoredValue(v));
}
CacheEntry entry = find(key, CacheEntryType.ZSET);
if (entry == null) {
@@ -1627,10 +1627,10 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
entry.lock();
try {
Set<CacheScoredValue.NumberScoredValue> sets = entry.setValue;
CacheScoredValue.NumberScoredValue old = sets.stream().filter(v -> Objects.equals(v.getValue(), value.getValue())).findAny().orElse(null);
Set<CacheScoredValue> sets = entry.setValue;
CacheScoredValue old = sets.stream().filter(v -> Objects.equals(v.getValue(), value.getValue())).findAny().orElse(null);
if (old == null) {
sets.add(new CacheScoredValue.NumberScoredValue(value.getScore().doubleValue(), value.getValue()));
sets.add(new CacheScoredValue(value.getScore().doubleValue(), value.getValue()));
return (T) value.getScore();
} else {
Number ic = value.getScore();
@@ -1673,10 +1673,10 @@ public final class CacheMemorySource extends AbstractCacheSource {
if (entry == null) {
return null;
}
List<CacheScoredValue.NumberScoredValue> list = new ArrayList<>(entry.setValue);
List<CacheScoredValue> list = new ArrayList<>(entry.setValue);
Collections.sort(list);
long c = 0;
for (CacheScoredValue.NumberScoredValue v : list) {
for (CacheScoredValue v : list) {
if (Objects.equals(v.getValue(), member)) {
return c;
}
@@ -1693,10 +1693,10 @@ public final class CacheMemorySource extends AbstractCacheSource {
if (entry == null) {
return null;
}
List<CacheScoredValue.NumberScoredValue> list = new ArrayList<>(entry.setValue);
List<CacheScoredValue> list = new ArrayList<>(entry.setValue);
Collections.sort(list, Collections.reverseOrder());
long c = 0;
for (CacheScoredValue.NumberScoredValue v : list) {
for (CacheScoredValue v : list) {
if (Objects.equals(v.getValue(), member)) {
return c;
}
@@ -1727,13 +1727,13 @@ public final class CacheMemorySource extends AbstractCacheSource {
}
@Override
public CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanAsync(String key, Type scoreType, AtomicLong cursor, int limit, String pattern) {
public CompletableFuture<List<CacheScoredValue>> zscanAsync(String key, Type scoreType, AtomicLong cursor, int limit, String pattern) {
return supplyFuture(() -> {
CacheEntry entry = find(key, CacheEntryType.ZSET);
if (entry == null) {
return new ArrayList();
}
Set<CacheScoredValue.NumberScoredValue> sets = entry.setValue;
Set<CacheScoredValue> sets = entry.setValue;
if (Utility.isEmpty(pattern)) {
return sets.stream().collect(Collectors.toList());
} else {

View File

@@ -19,84 +19,77 @@ import org.redkale.convert.ConvertColumn;
*
* @since 2.8.0
*/
public interface CacheScoredValue<S extends Number> extends Serializable, Comparable<CacheScoredValue> {
public class CacheScoredValue implements Serializable, Comparable<CacheScoredValue> {
public S getScore();
@ConvertColumn(index = 1)
private Number score;
public String getValue();
@ConvertColumn(index = 2)
private String value;
public static NumberScoredValue create(Number score, String value) {
return new NumberScoredValue(score, value);
public CacheScoredValue() {
}
public static final class NumberScoredValue implements CacheScoredValue<Number> {
protected CacheScoredValue(Number score, String value) {
Objects.requireNonNull(score);
Objects.requireNonNull(value);
this.score = score;
this.value = value;
}
@ConvertColumn(index = 1)
private Number score;
protected CacheScoredValue(CacheScoredValue scoredValue) {
this.score = scoredValue.getScore();
this.value = scoredValue.getValue();
}
@ConvertColumn(index = 2)
private String value;
public static CacheScoredValue create(Number score, String value) {
return new CacheScoredValue(score, value);
}
public NumberScoredValue(Number score, String value) {
Objects.requireNonNull(score);
Objects.requireNonNull(value);
this.score = score;
this.value = value;
public Number getScore() {
return score;
}
public String getValue() {
return value;
}
public void setScore(Number score) {
this.score = score;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int compareTo(CacheScoredValue o) {
return ((Comparable) this.score).compareTo((Number) o.getScore());
}
@Override
public int hashCode() {
return Objects.hashCode(this.value);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
public NumberScoredValue(CacheScoredValue scoredValue) {
this.score = scoredValue.getScore();
this.value = scoredValue.getValue();
if (obj == null) {
return false;
}
@Override
public Number getScore() {
return score;
}
@Override
public String getValue() {
return value;
}
public void setScore(Number score) {
this.score = score;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int compareTo(CacheScoredValue o) {
return ((Comparable) this.score).compareTo((Number) o.getScore());
}
@Override
public int hashCode() {
return Objects.hashCode(this.value);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final NumberScoredValue other = (NumberScoredValue) obj;
return Objects.equals(this.value, other.value);
}
@Override
public String toString() {
return "{score:" + score + ", value:" + value + "}";
if (getClass() != obj.getClass()) {
return false;
}
final CacheScoredValue other = (CacheScoredValue) obj;
return Objects.equals(this.value, other.value);
}
@Override
public String toString() {
return "{score:" + score + ", value:" + value + "}";
}
}

View File

@@ -990,35 +990,35 @@ public interface CacheSource extends Resourcable {
return zrangeAsync(key, start, stop).join();
}
default List<CacheScoredValue.NumberScoredValue> zscan(String key, Type scoreType, AtomicLong cursor, int limit, String pattern) {
default List<CacheScoredValue> zscan(String key, Type scoreType, AtomicLong cursor, int limit, String pattern) {
return zscanAsync(key, scoreType, cursor, limit, pattern).join();
}
default List<CacheScoredValue.NumberScoredValue> zscanInteger(String key, AtomicLong cursor, int limit, String pattern) {
default List<CacheScoredValue> zscanInteger(String key, AtomicLong cursor, int limit, String pattern) {
return zscan(key, Integer.class, cursor, limit, pattern);
}
default List<CacheScoredValue.NumberScoredValue> zscanLong(String key, AtomicLong cursor, int limit, String pattern) {
default List<CacheScoredValue> zscanLong(String key, AtomicLong cursor, int limit, String pattern) {
return zscan(key, Long.class, cursor, limit, pattern);
}
default List<CacheScoredValue.NumberScoredValue> zscanDouble(String key, AtomicLong cursor, int limit, String pattern) {
default List<CacheScoredValue> zscanDouble(String key, AtomicLong cursor, int limit, String pattern) {
return zscan(key, Double.class, cursor, limit, pattern);
}
default List<CacheScoredValue.NumberScoredValue> zscan(String key, Type scoreType, AtomicLong cursor, int limit) {
default List<CacheScoredValue> zscan(String key, Type scoreType, AtomicLong cursor, int limit) {
return zscan(key, scoreType, cursor, limit, null);
}
default List<CacheScoredValue.NumberScoredValue> zscanInteger(String key, AtomicLong cursor, int limit) {
default List<CacheScoredValue> zscanInteger(String key, AtomicLong cursor, int limit) {
return zscan(key, Integer.class, cursor, limit, null);
}
default List<CacheScoredValue.NumberScoredValue> zscanLong(String key, AtomicLong cursor, int limit) {
default List<CacheScoredValue> zscanLong(String key, AtomicLong cursor, int limit) {
return zscan(key, Long.class, cursor, limit, null);
}
default List<CacheScoredValue.NumberScoredValue> zscanDouble(String key, AtomicLong cursor, int limit) {
default List<CacheScoredValue> zscanDouble(String key, AtomicLong cursor, int limit) {
return zscan(key, Double.class, cursor, limit, null);
}
@@ -1735,33 +1735,33 @@ public interface CacheSource extends Resourcable {
public CompletableFuture<List<String>> zrangeAsync(String key, int start, int stop);
public CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanAsync(String key, Type scoreType, AtomicLong cursor, int limit, String pattern);
public CompletableFuture<List<CacheScoredValue>> zscanAsync(String key, Type scoreType, AtomicLong cursor, int limit, String pattern);
default CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanIntegerAsync(String key, AtomicLong cursor, int limit, String pattern) {
default CompletableFuture<List<CacheScoredValue>> zscanIntegerAsync(String key, AtomicLong cursor, int limit, String pattern) {
return zscanAsync(key, Integer.class, cursor, limit, pattern);
}
default CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanLongAsync(String key, AtomicLong cursor, int limit, String pattern) {
default CompletableFuture<List<CacheScoredValue>> zscanLongAsync(String key, AtomicLong cursor, int limit, String pattern) {
return zscanAsync(key, Long.class, cursor, limit, pattern);
}
default CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanDoubleAsync(String key, AtomicLong cursor, int limit, String pattern) {
default CompletableFuture<List<CacheScoredValue>> zscanDoubleAsync(String key, AtomicLong cursor, int limit, String pattern) {
return zscanAsync(key, Double.class, cursor, limit, pattern);
}
default CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanAsync(String key, Type scoreType, AtomicLong cursor, int limit) {
default CompletableFuture<List<CacheScoredValue>> zscanAsync(String key, Type scoreType, AtomicLong cursor, int limit) {
return zscanAsync(key, scoreType, cursor, limit, null);
}
default CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanIntegerAsync(String key, AtomicLong cursor, int limit) {
default CompletableFuture<List<CacheScoredValue>> zscanIntegerAsync(String key, AtomicLong cursor, int limit) {
return zscanAsync(key, Integer.class, cursor, limit, null);
}
default CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanLongAsync(String key, AtomicLong cursor, int limit) {
default CompletableFuture<List<CacheScoredValue>> zscanLongAsync(String key, AtomicLong cursor, int limit) {
return zscanAsync(key, Long.class, cursor, limit, null);
}
default CompletableFuture<List<CacheScoredValue.NumberScoredValue>> zscanDoubleAsync(String key, AtomicLong cursor, int limit) {
default CompletableFuture<List<CacheScoredValue>> zscanDoubleAsync(String key, AtomicLong cursor, int limit) {
return zscanAsync(key, Double.class, cursor, limit, null);
}