|
6 | 6 | * | __/\ V /| | || (_| | |_| | |_) |
|
7 | 7 | * \___| \_/ |_|\__\__,_|____/|____/
|
8 | 8 | *
|
9 |
| - * Copyright (c) 2023-2024 |
| 9 | + * Copyright (c) 2023-2025 |
10 | 10 | *
|
11 | 11 | * Licensed under the Business Source License, Version 1.1 (the "License");
|
12 | 12 | * you may not use this file except in compliance with the License.
|
|
60 | 60 | import io.evitadb.core.query.sort.translator.OrderByTranslator;
|
61 | 61 | import io.evitadb.core.query.sort.translator.OrderInScopeTranslator;
|
62 | 62 | import io.evitadb.core.query.sort.translator.ReferenceOrderingConstraintTranslator;
|
| 63 | +import io.evitadb.dataType.Scope; |
63 | 64 | import io.evitadb.exception.GenericEvitaInternalError;
|
64 | 65 | import io.evitadb.index.EntityIndex;
|
65 | 66 | import io.evitadb.index.EntityIndexKey;
|
|
73 | 74 |
|
74 | 75 | import javax.annotation.Nonnull;
|
75 | 76 | import javax.annotation.Nullable;
|
| 77 | +import java.util.ArrayDeque; |
| 78 | +import java.util.Deque; |
76 | 79 | import java.util.Map;
|
77 | 80 | import java.util.Objects;
|
78 | 81 | import java.util.Optional;
|
| 82 | +import java.util.Set; |
79 | 83 |
|
80 | 84 | import static io.evitadb.utils.Assert.isPremiseValid;
|
81 | 85 | import static java.util.Optional.ofNullable;
|
@@ -137,6 +141,10 @@ public class ReferenceOrderByVisitor implements ConstraintVisitor, FetchRequirem
|
137 | 141 | * This variable is internally updated during the traversal and comparison of constraints.
|
138 | 142 | */
|
139 | 143 | @Nullable private ChainIndex lastRetrievedChainIndex;
|
| 144 | + /** |
| 145 | + * Contemporary stack for scopes used on each level of the ordering query tree. |
| 146 | + */ |
| 147 | + private final Deque<Set<Scope>> scope = new ArrayDeque<>(16); |
140 | 148 |
|
141 | 149 | /**
|
142 | 150 | * Extracts {@link OrderingDescriptor} from the passed `orderBy` constraint using passed `queryContext` for
|
@@ -274,7 +282,7 @@ public NamedSchemaContract getAttributeSchemaOrSortableAttributeCompound(
|
274 | 282 | @Nonnull String attributeName
|
275 | 283 | ) {
|
276 | 284 | return attributeSchemaAccessor.getAttributeSchemaOrSortableAttributeCompound(
|
277 |
| - attributeName, this.getScopes() |
| 285 | + attributeName, this.scope.isEmpty() ? this.getScopes() : this.scope.peek() |
278 | 286 | );
|
279 | 287 | }
|
280 | 288 |
|
@@ -369,6 +377,24 @@ public Optional<ChainIndex> getChainIndex(
|
369 | 377 | }
|
370 | 378 | }
|
371 | 379 |
|
| 380 | + /** |
| 381 | + * Executes the given {@link Runnable} within the context of the specified {@link Scope scopes}. |
| 382 | + * The method temporarily pushes the provided scopes to the current scope stack, |
| 383 | + * executes the runnable, and ensures the stack is restored to its previous state |
| 384 | + * afterward, even if the runnable throws an exception. |
| 385 | + * |
| 386 | + * @param scopesToUse the set of scopes to apply during the execution of the runnable |
| 387 | + * @param runnable the code to be executed within the given scopes |
| 388 | + */ |
| 389 | + public void doWithScope(@Nonnull Set<Scope> scopesToUse, @Nonnull Runnable runnable) { |
| 390 | + try { |
| 391 | + this.scope.push(scopesToUse); |
| 392 | + runnable.run(); |
| 393 | + } finally { |
| 394 | + this.scope.pop(); |
| 395 | + } |
| 396 | + } |
| 397 | + |
372 | 398 | /**
|
373 | 399 | * DTO record enveloping comparators both for attributes on reference itself and the referenced entity.
|
374 | 400 | * Currently, the sorting allows to use only simple ordering constraints either on reference attributes or
|
|
0 commit comments