-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed random order when entity primary key translation occurs
- Loading branch information
Showing
5 changed files
with
126 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...ngine/src/main/java/io/evitadb/core/query/sort/primaryKey/TranslatedPrimaryKeySorter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* | ||
* _ _ ____ ____ | ||
* _____ _(_) |_ __ _| _ \| __ ) | ||
* / _ \ \ / / | __/ _` | | | | _ \ | ||
* | __/\ V /| | || (_| | |_| | |_) | | ||
* \___| \_/ |_|\__\__,_|____/|____/ | ||
* | ||
* Copyright (c) 2023 | ||
* | ||
* Licensed under the Business Source License, Version 1.1 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/FgForrest/evitaDB/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.evitadb.core.query.sort.primaryKey; | ||
|
||
import io.evitadb.core.query.QueryContext; | ||
import io.evitadb.core.query.algebra.Formula; | ||
import io.evitadb.core.query.sort.Sorter; | ||
import io.evitadb.index.bitmap.Bitmap; | ||
import io.evitadb.utils.ArrayUtils; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* This sorter sorts translated primary keys according to original primary keys in ascending order. It is used with | ||
* the special implementation in case the entity is not known in the query. In such case the primary keys are translated | ||
* different ids and those ids are translated back at the end of the query. Unfortunately the order of the translated | ||
* keys might be different than the original order of the primary keys, so we need to sort them here according to | ||
* their original primary keys order in ascending fashion. | ||
* | ||
* @author Jan Novotný (novotny@fg.cz), FG Forrest a.s. (c) 2023 | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class TranslatedPrimaryKeySorter implements Sorter { | ||
public static final Sorter INSTANCE = new TranslatedPrimaryKeySorter(); | ||
|
||
@Nonnull | ||
@Override | ||
public Sorter cloneInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public Sorter andThen(Sorter sorterForUnknownRecords) { | ||
return INSTANCE; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Sorter getNextSorter() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int sortAndSlice(@Nonnull QueryContext queryContext, @Nonnull Formula input, int startIndex, int endIndex, @Nonnull int[] result, int peak) { | ||
final Bitmap translatedPrimaryKeysBitmap = input.compute(); | ||
final int[] translatedPrimaryKeys = translatedPrimaryKeysBitmap.getArray(); | ||
final int[] originalPrimaryKeys = translatedPrimaryKeysBitmap.stream().map(queryContext::translateToEntityPrimaryKey).toArray(); | ||
// initialize order array | ||
final int[] order = new int[originalPrimaryKeys.length]; | ||
for (int i = 0; i < order.length; i++) { | ||
order[i] = i; | ||
} | ||
|
||
ArrayUtils.sortSecondAlongFirstArray(originalPrimaryKeys, order); | ||
final int length = endIndex - startIndex; | ||
for (int i = peak; i < translatedPrimaryKeys.length && i < length; i++) { | ||
result[i] = translatedPrimaryKeys[order[i]]; | ||
} | ||
return peak + Math.min(translatedPrimaryKeys.length, length); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters