|
43 | 43 | */
|
44 | 44 | public interface ReactiveKeyCommands {
|
45 | 45 |
|
| 46 | + /** |
| 47 | + * {@code MOVE} command parameters. |
| 48 | + * |
| 49 | + * @author Mark Paluch |
| 50 | + * @see <a href="https://redis.io/commands/move">Redis Documentation: MOVE</a> |
| 51 | + */ |
| 52 | + class CopyCommand extends KeyCommand { |
| 53 | + |
| 54 | + private final @Nullable ByteBuffer target; |
| 55 | + private final boolean replace; |
| 56 | + private final @Nullable Integer database; |
| 57 | + |
| 58 | + public CopyCommand(ByteBuffer key, @Nullable ByteBuffer target, boolean replace, @Nullable Integer database) { |
| 59 | + super(key); |
| 60 | + this.target = target; |
| 61 | + this.replace = replace; |
| 62 | + this.database = database; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new {@link CopyCommand} given a {@link ByteBuffer key}. |
| 67 | + * |
| 68 | + * @param key must not be {@literal null}. |
| 69 | + * @return a new {@link CopyCommand} for {@link ByteBuffer key}. |
| 70 | + */ |
| 71 | + public static CopyCommand key(ByteBuffer key) { |
| 72 | + |
| 73 | + Assert.notNull(key, "Key must not be null!"); |
| 74 | + |
| 75 | + return new CopyCommand(key, null, false, null); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Applies the {@link ByteBuffer targetKey}. Constructs a new command instance with all previously configured |
| 80 | + * properties. |
| 81 | + * |
| 82 | + * @param targetKey must not be {@literal null}. |
| 83 | + * @return a new {@link CopyCommand} with {@literal database} applied. |
| 84 | + */ |
| 85 | + public CopyCommand to(ByteBuffer targetKey) { |
| 86 | + |
| 87 | + Assert.notNull(targetKey, "Key must not be null!"); |
| 88 | + |
| 89 | + return new CopyCommand(getKey(), targetKey, isReplace(), database); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Applies {@code replace}. Constructs a new command instance with all previously configured properties. |
| 94 | + * |
| 95 | + * @param key must not be {@literal null}. |
| 96 | + * @return a new {@link CopyCommand} with {@literal replace} applied. |
| 97 | + */ |
| 98 | + public CopyCommand replace(boolean replace) { |
| 99 | + return new CopyCommand(getKey(), this.target, replace, database); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Applies the {@literal database} index. Constructs a new command instance with all previously configured |
| 104 | + * properties. |
| 105 | + * |
| 106 | + * @param database |
| 107 | + * @return a new {@link CopyCommand} with {@literal database} applied. |
| 108 | + */ |
| 109 | + public CopyCommand database(int database) { |
| 110 | + return new CopyCommand(getKey(), this.target, isReplace(), database); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return can be {@literal null}. |
| 115 | + */ |
| 116 | + @Nullable |
| 117 | + public ByteBuffer getTarget() { |
| 118 | + return target; |
| 119 | + } |
| 120 | + |
| 121 | + public boolean isReplace() { |
| 122 | + return replace; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @return can be {@literal null}. |
| 127 | + */ |
| 128 | + @Nullable |
| 129 | + public Integer getDatabase() { |
| 130 | + return database; |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Copy given {@code key} to a target {@code key}. |
| 137 | + * |
| 138 | + * @param sourceKey must not be {@literal null}. |
| 139 | + * @param targetKey must not be {@literal null}. |
| 140 | + * @param replace whether to replace existing keys. |
| 141 | + * @return |
| 142 | + * @see <a href="https://redis.io/commands/copy">Redis Documentation: COPY</a> |
| 143 | + * @since 2.6 |
| 144 | + */ |
| 145 | + default Mono<Boolean> copy(ByteBuffer sourceKey, ByteBuffer targetKey, boolean replace) { |
| 146 | + |
| 147 | + Assert.notNull(sourceKey, "Source key must not be null!"); |
| 148 | + Assert.notNull(targetKey, "Targetk ey must not be null!"); |
| 149 | + |
| 150 | + return copy(Mono.just(CopyCommand.key(sourceKey).to(targetKey).replace(replace))).next() |
| 151 | + .map(BooleanResponse::getOutput); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Copy keys one-by-one. |
| 156 | + * |
| 157 | + * @param commands must not be {@literal null}. |
| 158 | + * @return {@link Flux} of {@link BooleanResponse} holding the {@literal key} to move along with the copy result. |
| 159 | + * @see <a href="https://redis.io/commands/copy">Redis Documentation: COPY</a> |
| 160 | + * @since 2.6 |
| 161 | + */ |
| 162 | + Flux<BooleanResponse<CopyCommand>> copy(Publisher<CopyCommand> commands); |
| 163 | + |
46 | 164 | /**
|
47 | 165 | * Determine if given {@literal key} exists.
|
48 | 166 | *
|
@@ -670,7 +788,7 @@ private MoveCommand(ByteBuffer key, @Nullable Integer database) {
|
670 | 788 | * Creates a new {@link MoveCommand} given a {@link ByteBuffer key}.
|
671 | 789 | *
|
672 | 790 | * @param key must not be {@literal null}.
|
673 |
| - * @return a new {@link ExpireCommand} for {@link ByteBuffer key}. |
| 791 | + * @return a new {@link MoveCommand} for {@link ByteBuffer key}. |
674 | 792 | */
|
675 | 793 | public static MoveCommand key(ByteBuffer key) {
|
676 | 794 |
|
|
0 commit comments