Commit ec961c2c authored by Sheng Wang's avatar Sheng Wang
Browse files

Fixed the issues of cosmos bb query

parent f3bb644f
Pipeline #27670 failed with stages
in 12 minutes and 36 seconds
......@@ -14,14 +14,10 @@
package org.opengroup.osdu.wd.azure.cosmosdb;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.bson.Document;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;
import org.opengroup.osdu.wd.core.models.*;
import org.opengroup.osdu.wd.core.util.Common;
......
......@@ -20,7 +20,6 @@ import com.azure.cosmos.CosmosContainer;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.Data;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.client.model.http.AppException;
import org.opengroup.osdu.wd.core.models.Relationship;
......@@ -222,7 +221,7 @@ public class CosmosdbFacade {
}
public static CosmosEntity getLatestEntityVersionByRelatedId(CosmosContainer container, String relatedType, String relatedId) {
String query = String.format("SELECT top 1 %s FROM c join a in c.relationships WHERE c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s') ORDER BY c.version desc", selectSQL, relatedType, relatedId);
String query = String.format("SELECT top 1 %s FROM c join a in c.relationships WHERE c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s:') ORDER BY c.version desc", selectSQL, relatedType, relatedId);
List<CosmosEntity> entities = queryItems(container, query, CosmosEntity.class);
if (entities.size() < 1 || entities.get(0) == null)
return null;
......@@ -231,7 +230,7 @@ public class CosmosdbFacade {
}
public static CosmosEntity getSpecificEntityVersionByRelatedId(CosmosContainer container, long verison, String relatedType, String relatedId) {
String query = String.format("SELECT top 1 %s FROM c join a in c.relationships WHERE c.version = %d and c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s') ORDER BY c.version desc", selectSQL, verison, relatedType, relatedId);
String query = String.format("SELECT top 1 %s FROM c join a in c.relationships WHERE c.version = %d and c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s:') ORDER BY c.version desc", selectSQL, verison, relatedType, relatedId);
List<CosmosEntity> entities = queryItems(container, query, CosmosEntity.class);
if (entities.size() < 1 || entities.get(0) == null)
return null;
......@@ -240,17 +239,20 @@ public class CosmosdbFacade {
}
public static List<String> getEntityRelationshipsByRelatedId(CosmosContainer container, String relatedType, String relatedId) {
String query = String.format("SELECT c.entityId, max(_id) FROM c join a in c.relationships GROUP BY c.entityId WHERE c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s') ORDER BY c.version desc", relatedType, relatedId);
String query = String.format("SELECT c.entityId, c.id FROM c join a in c.relationships WHERE c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s:')", relatedType, relatedId);
List<ReturnRelationship> relationships = queryItems(container, query, ReturnRelationship.class);
List<String> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
for (ReturnRelationship relationship : relationships) {
list.add(relationship.getMax());
if (!map.containsKey(relationship.getEntityId()))
map.put(relationship.getEntityId(), relationship.getId());
else if (map.get(relationship.getEntityId()).compareTo(relationship.getId()) < 0)
map.replace(relationship.getEntityId(), relationship.getId());
}
return list;
return map.values().stream().collect(Collectors.toList());
}
public static List<Long> getVersionNumbersByRelatedId(CosmosContainer container, String relatedType, String relatedId) {
String query = String.format("SELECT %s FROM c join a in c.relationships WHERE c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s') ORDER BY c.version desc", selectSQL, relatedType, relatedId);
String query = String.format("SELECT %s FROM c join a in c.relationships WHERE c.deleted = false and a.entityType = '%s' and STARTSWITH(a.id, '%s:') ORDER BY c.version desc", selectSQL, relatedType, relatedId);
List<CosmosEntity> entities = queryItems(container, query, CosmosEntity.class);
List<Long> list = new ArrayList<>();
for (CosmosEntity entity : entities) {
......@@ -379,10 +381,4 @@ public class CosmosdbFacade {
public DefaultAzureCredential azureCredential() {
return new DefaultAzureCredentialBuilder().build();
}
@Data
public class ReturnRelationship {
String entityId;
String max;
}
}
// Copyright 2020 Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 org.opengroup.osdu.wd.azure.cosmosdb;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ReturnRelationship {
String entityId;
String id;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment