|
| 1 | +package com.datastax.astra.client.core.paging; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * Data API Java Client |
| 6 | + * -- |
| 7 | + * Copyright (C) 2024 DataStax |
| 8 | + * -- |
| 9 | + * Licensed under the Apache License, Version 2.0 |
| 10 | + * You may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import java.util.*; |
| 24 | +import java.util.function.Consumer; |
| 25 | +import java.util.function.Function; |
| 26 | +import java.util.NoSuchElementException; |
| 27 | + |
| 28 | +public class Cursor<T> implements Iterable<T> { |
| 29 | + |
| 30 | + private CursorState state; |
| 31 | + |
| 32 | + private final String keyspace; |
| 33 | + |
| 34 | + private final Collection collection; |
| 35 | + |
| 36 | + private final Map<String, Object> filter; |
| 37 | + |
| 38 | + private final Map<String, Object> projection; |
| 39 | + |
| 40 | + private final Map<String, Object> sort; |
| 41 | + |
| 42 | + private int limit; |
| 43 | + |
| 44 | + private int skip; |
| 45 | + |
| 46 | + private boolean includeSimilarity; |
| 47 | + |
| 48 | + private boolean includeSortVector; |
| 49 | + |
| 50 | + private Function<Document, T> mapFunction; |
| 51 | + |
| 52 | + /** Page. */ |
| 53 | + private final List<Document> buffer; |
| 54 | + |
| 55 | + private int consumedCount; |
| 56 | + |
| 57 | + // Constructor |
| 58 | + public Cursor(String keyspace, Collection collection) { |
| 59 | + this.state = CursorState.IDLE; |
| 60 | + this.keyspace = keyspace; |
| 61 | + this.collection = collection; |
| 62 | + this.filter = new HashMap<>(); |
| 63 | + this.projection = new HashMap<>(); |
| 64 | + this.sort = new HashMap<>(); |
| 65 | + this.limit = 0; |
| 66 | + this.skip = 0; |
| 67 | + this.includeSimilarity = false; |
| 68 | + this.includeSortVector = false; |
| 69 | + this.mapFunction = null; |
| 70 | + this.buffer = new ArrayList<>(); |
| 71 | + this.consumedCount = 0; |
| 72 | + } |
| 73 | + |
| 74 | + // Private constructor for cloning |
| 75 | + private Cursor(Cursor<T> cursor) { |
| 76 | + this.state = CursorState.IDLE; |
| 77 | + this.keyspace = cursor.keyspace; |
| 78 | + this.collection = cursor.collection; |
| 79 | + this.filter = new HashMap<>(cursor.filter); |
| 80 | + this.projection = new HashMap<>(cursor.projection); |
| 81 | + this.sort = new HashMap<>(cursor.sort); |
| 82 | + this.limit = cursor.limit; |
| 83 | + this.skip = cursor.skip; |
| 84 | + this.includeSimilarity = cursor.includeSimilarity; |
| 85 | + this.includeSortVector = cursor.includeSortVector; |
| 86 | + this.mapFunction = cursor.mapFunction; |
| 87 | + this.buffer = new ArrayList<>(); |
| 88 | + this.consumedCount = 0; |
| 89 | + } |
| 90 | + |
| 91 | + // Immutable methods that return a new Cursor<T> instance |
| 92 | + public Cursor<T> filter(Map<String, Object> newFilter) { |
| 93 | + checkIdleState(); |
| 94 | + Cursor<T> newCursor = this.clone(); |
| 95 | + newCursor.filter.putAll(newFilter); |
| 96 | + return newCursor; |
| 97 | + } |
| 98 | + |
| 99 | + public Cursor<T> project(Map<String, Object> newProjection) { |
| 100 | + checkIdleState(); |
| 101 | + if (this.mapFunction != null) { |
| 102 | + throw new CursorException("Cannot call project() after map().", state.toString()); |
| 103 | + } |
| 104 | + Cursor<T> newCursor = this.clone(); |
| 105 | + newCursor.projection.putAll(newProjection); |
| 106 | + return newCursor; |
| 107 | + } |
| 108 | + |
| 109 | + public Cursor<T> sort(Map<String, Object> newSort) { |
| 110 | + checkIdleState(); |
| 111 | + Cursor<T> newCursor = this.clone(); |
| 112 | + newCursor.sort.putAll(newSort); |
| 113 | + return newCursor; |
| 114 | + } |
| 115 | + |
| 116 | + public Cursor<T> limit(int newLimit) { |
| 117 | + checkIdleState(); |
| 118 | + Cursor<T> newCursor = this.clone(); |
| 119 | + newCursor.limit = newLimit; |
| 120 | + return newCursor; |
| 121 | + } |
| 122 | + |
| 123 | + public Cursor<T> skip(int newSkip) { |
| 124 | + checkIdleState(); |
| 125 | + Cursor<T> newCursor = this.clone(); |
| 126 | + newCursor.skip = newSkip; |
| 127 | + return newCursor; |
| 128 | + } |
| 129 | + |
| 130 | + public Cursor<T> includeSimilarity() { |
| 131 | + checkIdleState(); |
| 132 | + Cursor<T> newCursor = this.clone(); |
| 133 | + newCursor.includeSimilarity = true; |
| 134 | + return newCursor; |
| 135 | + } |
| 136 | + |
| 137 | + public Cursor<T> includeSortVector() { |
| 138 | + checkIdleState(); |
| 139 | + Cursor<T> newCursor = this.clone(); |
| 140 | + newCursor.includeSortVector = true; |
| 141 | + return newCursor; |
| 142 | + } |
| 143 | + |
| 144 | + public <R> Cursor<R> map(Function<Document, R> newMapFunction) { |
| 145 | + checkIdleState(); |
| 146 | + if (this.mapFunction != null) { |
| 147 | + throw new CursorException("Cannot call map() after map().", state.toString()); |
| 148 | + } |
| 149 | + Cursor<R> newCursor = new Cursor<>(this.keyspace, this.collection); |
| 150 | + newCursor.state = this.state; |
| 151 | + newCursor.filter.putAll(this.filter); |
| 152 | + newCursor.projection.putAll(this.projection); |
| 153 | + newCursor.sort.putAll(this.sort); |
| 154 | + newCursor.limit = this.limit; |
| 155 | + newCursor.skip = this.skip; |
| 156 | + newCursor.includeSimilarity = this.includeSimilarity; |
| 157 | + newCursor.includeSortVector = this.includeSortVector; |
| 158 | + newCursor.mapFunction = newMapFunction; |
| 159 | + return newCursor; |
| 160 | + } |
| 161 | + |
| 162 | + /** {@inheritDoc} */ |
| 163 | + @Override |
| 164 | + public Cursor<T> clone() { |
| 165 | + return new Cursor<>(this); |
| 166 | + } |
| 167 | + |
| 168 | + // State-changing methods |
| 169 | + public void close() { |
| 170 | + this.state = CursorState.CLOSED; |
| 171 | + // Close any open resources here |
| 172 | + } |
| 173 | + |
| 174 | + public void rewind() { |
| 175 | + this.state = CursorState.IDLE; |
| 176 | + this.buffer.clear(); |
| 177 | + this.consumedCount = 0; |
| 178 | + // Reset any other stateful components here |
| 179 | + } |
| 180 | + |
| 181 | + // Buffer consumption |
| 182 | + public List<Document> consumeBuffer(int n) { |
| 183 | + if (state == CursorState.CLOSED || state == CursorState.IDLE) { |
| 184 | + return Collections.emptyList(); |
| 185 | + } |
| 186 | + List<Document> result = new ArrayList<>(); |
| 187 | + int count = 0; |
| 188 | + while (!buffer.isEmpty() && count < n) { |
| 189 | + result.add(buffer.remove(0)); |
| 190 | + count++; |
| 191 | + } |
| 192 | + return result; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Validate that the cursor is in the IDLE state. |
| 197 | + */ |
| 198 | + private void checkIdleState() { |
| 199 | + if (state != CursorState.IDLE) { |
| 200 | + throw new CursorException("Cannot modify cursor after it has been started.", state.toString()); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // Iterator implementation |
| 205 | + @Override |
| 206 | + public Iterator<T> iterator() { |
| 207 | + return new CursorIterator(); |
| 208 | + } |
| 209 | + |
| 210 | + private class CursorIterator implements Iterator<T> { |
| 211 | + @Override |
| 212 | + public boolean hasNext() { |
| 213 | + if (state == CursorState.CLOSED) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + if (state == CursorState.IDLE) { |
| 217 | + state = CursorState.STARTED; |
| 218 | + } |
| 219 | + if (!buffer.isEmpty()) { |
| 220 | + return true; |
| 221 | + } |
| 222 | + // Fetch next batch of documents into buffer |
| 223 | + fetchNextBatch(); |
| 224 | + return !buffer.isEmpty(); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public T next() { |
| 229 | + if (!hasNext()) { |
| 230 | + throw new NoSuchElementException(); |
| 231 | + } |
| 232 | + Document rawDoc = buffer.remove(0); |
| 233 | + consumedCount++; |
| 234 | + if (mapFunction != null) { |
| 235 | + return mapFunction.apply(rawDoc); |
| 236 | + } else { |
| 237 | + // Unsafe cast; in practice, handle this properly |
| 238 | + return (T) rawDoc; |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + // Fetch next batch of documents |
| 244 | + private void fetchNextBatch() { |
| 245 | + // Implement logic to fetch next batch from database |
| 246 | + // and add to buffer. This is where you interact with the DB. |
| 247 | + } |
| 248 | + |
| 249 | + // Additional methods |
| 250 | + public boolean hasNext() { |
| 251 | + return iterator().hasNext(); |
| 252 | + } |
| 253 | + |
| 254 | + public void forEach(Consumer<? super T> action) { |
| 255 | + Objects.requireNonNull(action); |
| 256 | + try { |
| 257 | + for (T t : this) { |
| 258 | + action.accept(t); |
| 259 | + } |
| 260 | + } finally { |
| 261 | + close(); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + public List<T> toList() { |
| 266 | + List<T> result = new ArrayList<>(); |
| 267 | + try { |
| 268 | + forEach(result::add); |
| 269 | + } finally { |
| 270 | + close(); |
| 271 | + } |
| 272 | + return result; |
| 273 | + } |
| 274 | + |
| 275 | + public int getBufferedCount() { |
| 276 | + return buffer.size(); |
| 277 | + } |
| 278 | + |
| 279 | + public int getConsumedCount() { |
| 280 | + return consumedCount; |
| 281 | + } |
| 282 | + |
| 283 | + public String getKeyspace() { |
| 284 | + return keyspace; |
| 285 | + } |
| 286 | + |
| 287 | + public Collection getCollection() { |
| 288 | + return collection; |
| 289 | + } |
| 290 | + |
| 291 | + public String getDataSource() { |
| 292 | + // Implement as needed |
| 293 | + return null; |
| 294 | + } |
| 295 | + |
| 296 | + // Exception class |
| 297 | + public static class CursorException extends RuntimeException { |
| 298 | + private final String state; |
| 299 | + |
| 300 | + public CursorException(String message, String state) { |
| 301 | + super(message); |
| 302 | + this.state = state; |
| 303 | + } |
| 304 | + |
| 305 | + public String getState() { |
| 306 | + return state; |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + // Placeholder for Document class |
| 311 | + public static class Document { |
| 312 | + // Implement document fields and methods |
| 313 | + } |
| 314 | + |
| 315 | + // Placeholder for Collection class |
| 316 | + public static class Collection { |
| 317 | + // Implement collection fields and methods |
| 318 | + } |
| 319 | +} |
0 commit comments