Skip to content

Commit ae42d97

Browse files
committed
temp debug
1 parent d57ab0c commit ae42d97

File tree

5 files changed

+113
-15
lines changed

5 files changed

+113
-15
lines changed

document-store/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins {
88

99
dependencies {
1010
api("com.typesafe:config:1.4.2")
11+
implementation("org.projectlombok:lombok:1.18.18")
1112
annotationProcessor("org.projectlombok:lombok:1.18.22")
1213
compileOnly("org.projectlombok:lombok:1.18.22")
1314
implementation("org.apache.commons:commons-collections4:4.4")

document-store/src/integrationTest/java/org/hypertrace/core/documentstore/DocStoreTest.java

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.List;
3333
import java.util.Map;
3434
import java.util.Objects;
35+
import java.util.Optional;
3536
import java.util.Set;
3637
import java.util.stream.Collectors;
3738
import java.util.stream.IntStream;
@@ -56,6 +57,7 @@
5657
import org.testcontainers.containers.GenericContainer;
5758
import org.testcontainers.containers.wait.strategy.Wait;
5859
import org.testcontainers.shaded.com.google.common.collect.Maps;
60+
import org.testcontainers.shaded.org.apache.commons.lang.StringEscapeUtils;
5961
import org.testcontainers.utility.DockerImageName;
6062

6163
public class DocStoreTest {
@@ -85,22 +87,25 @@ public static void init() {
8587
Datastore mongoDatastore = DatastoreProvider.getDatastore("Mongo", config);
8688
System.out.println(mongoDatastore.listCollections());
8789

88-
postgres =
89-
new GenericContainer<>(DockerImageName.parse("postgres:13.1"))
90-
.withEnv("POSTGRES_PASSWORD", "postgres")
91-
.withEnv("POSTGRES_USER", "postgres")
92-
.withExposedPorts(5432)
93-
.waitingFor(Wait.forListeningPort());
94-
postgres.start();
90+
// postgres =
91+
// new GenericContainer<>(DockerImageName.parse("postgres:13.1"))
92+
// .withEnv("POSTGRES_PASSWORD", "postgres")
93+
// .withEnv("POSTGRES_USER", "postgres")
94+
// .withExposedPorts(5432)
95+
// .waitingFor(Wait.forListeningPort());
96+
// postgres.start();
97+
98+
// String postgresConnectionUrl =
99+
// String.format("jdbc:postgresql://localhost:%s/", postgres.getMappedPort(5432));
95100

96101
String postgresConnectionUrl =
97-
String.format("jdbc:postgresql://localhost:%s/", postgres.getMappedPort(5432));
102+
String.format("jdbc:postgresql://localhost:%s/", "5432");
98103
DatastoreProvider.register("POSTGRES", PostgresDatastore.class);
99104

100105
Map<String, String> postgresConfig = new HashMap<>();
101106
postgresConfig.putIfAbsent("url", postgresConnectionUrl);
102-
postgresConfig.putIfAbsent("user", "postgres");
103-
postgresConfig.putIfAbsent("password", "postgres");
107+
postgresConfig.putIfAbsent("user", "keycloak");
108+
postgresConfig.putIfAbsent("password", "keycloak");
104109
Datastore postgresDatastore =
105110
DatastoreProvider.getDatastore("Postgres", ConfigFactory.parseMap(postgresConfig));
106111
System.out.println(postgresDatastore.listCollections());
@@ -139,6 +144,47 @@ private static Stream<Arguments> databaseContextMongo() {
139144
return Stream.of(Arguments.of(MONGO_STORE));
140145
}
141146

147+
@ParameterizedTest
148+
@MethodSource("databaseContextPostgres")
149+
public void debugSearch(String dataStoreName) throws Exception {
150+
/*
151+
SELECT document->'identifyingAttributes' AS "identifyingAttributes",
152+
document->'tenantId' AS "tenantId",
153+
document->'type' AS "type",id AS "id",
154+
document->'attributes' AS "attributes"
155+
FROM insights WHERE ((document->>'tenantId' = '14d8d0d8-c1a9-4100-83a4-97edfeb85606') AND (document->>'type' = 'API'))
156+
AND (document->'identifyingAttributes'->>'api_id' = '5e6f57d7-313d-34e1-a37e-a338c448c271')
157+
*/
158+
159+
Datastore datastore = datastoreMap.get(dataStoreName);
160+
Collection collection = datastore.getCollection("insights");
161+
162+
Query query = new Query();
163+
query.addSelection("identifyingAttributes");
164+
query.addSelection("tenantId");
165+
query.addSelection("id");
166+
query.addSelection("attributes");
167+
query.addSelection("type");
168+
169+
Filter f1 = Filter.eq("tenantId", "14d8d0d8-c1a9-4100-83a4-97edfeb85606");
170+
Filter f2 = f1.and(Filter.eq("type", "API"));
171+
Filter f3 = f2.and(Filter.eq("identifyingAttributes.api_id", "5e6f57d7-313d-34e1-a37e-a338c448c271"));
172+
query.setFilter(f3);
173+
174+
Iterator<Document> results = collection.search(query);
175+
List<Document> documents = new ArrayList<>();
176+
while (results.hasNext()) {
177+
Document document = results.next();
178+
String insightDocumentJson = document.toJson();
179+
String processed = StringEscapeUtils.unescapeJava(insightDocumentJson);
180+
documents.add(document);
181+
Optional<InsightDto> mayBeInsightDto =
182+
Optional.ofNullable(OBJECT_MAPPER.readValue(processed, InsightDto.class));
183+
Assertions.assertNotNull(mayBeInsightDto.get());
184+
}
185+
Assertions.assertFalse(documents.isEmpty());
186+
}
187+
142188
@ParameterizedTest
143189
@MethodSource("databaseContextProvider")
144190
public void testUpsert(String dataStoreName) throws Exception {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.hypertrace.core.documentstore;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import java.util.Map;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
@Data
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
@Builder
14+
public class InsightDto {
15+
public static final String ID_FIELD = "id";
16+
public static final String TYPE_FIELD = "type";
17+
public static final String ATTRIBUTES_FIELD = "attributes";
18+
public static final String IDENTIFYING_ATTRIBUTES_FIELD = "identifyingAttributes";
19+
public static final String TENANT_ID_FIELD = "tenantId";
20+
21+
@JsonProperty(value = ID_FIELD)
22+
private String id;
23+
24+
@JsonProperty(value = TYPE_FIELD)
25+
private String type;
26+
27+
@JsonProperty(value = ATTRIBUTES_FIELD)
28+
private Map<String, Object> attributes;
29+
30+
@JsonProperty(value = IDENTIFYING_ATTRIBUTES_FIELD)
31+
private Map<String, String> identifyingAttributes;
32+
33+
@JsonProperty(value = TENANT_ID_FIELD)
34+
private String tenantId;
35+
}

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/PostgresCollection.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hypertrace.core.documentstore.postgres;
22

33
import com.fasterxml.jackson.core.JsonParseException;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
45
import com.fasterxml.jackson.databind.JsonNode;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -1083,11 +1084,7 @@ protected Document prepareDocument() throws SQLException, IOException {
10831084
Map<String, Object> jsonNode = new HashMap();
10841085
for (int i = 1; i <= columnCount; i++) {
10851086
String columnName = resultSetMetaData.getColumnName(i);
1086-
int columnType = resultSetMetaData.getColumnType(i);
1087-
String columnValue =
1088-
columnType == Types.ARRAY
1089-
? MAPPER.writeValueAsString(resultSet.getArray(i).getArray())
1090-
: resultSet.getString(i);
1087+
String columnValue = getColumnValue(resultSetMetaData, columnName, i);
10911088
if (StringUtils.isNotEmpty(columnValue)) {
10921089
JsonNode leafNodeValue = MAPPER.readTree(columnValue);
10931090
if (PostgresUtils.isEncodedNestedField(columnName)) {
@@ -1101,6 +1098,23 @@ protected Document prepareDocument() throws SQLException, IOException {
11011098
return new JSONDocument(MAPPER.writeValueAsString(jsonNode));
11021099
}
11031100

1101+
private String getColumnValue(ResultSetMetaData resultSetMetaData, String columnName, int columnIndex)
1102+
throws SQLException, JsonProcessingException {
1103+
int columnType = resultSetMetaData.getColumnType(columnIndex);
1104+
// check for array
1105+
if (columnType == Types.ARRAY) {
1106+
return MAPPER.writeValueAsString(resultSet.getArray(columnIndex).getArray());
1107+
}
1108+
1109+
// check for ID column
1110+
if (PostgresUtils.OUTER_COLUMNS.contains(columnName) && columnName.equals(PostgresUtils.ID_COLUMN)) {
1111+
return MAPPER.writeValueAsString(resultSet.getString(columnIndex));
1112+
}
1113+
1114+
// rest of the columns
1115+
return resultSet.getString(columnIndex);
1116+
}
1117+
11041118
private void handleNestedField(
11051119
String columnName, Map<String, Object> rootNode, JsonNode leafNodeValue) {
11061120
List<String> keys = PostgresUtils.splitNestedField(columnName);

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/utils/PostgresUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class PostgresUtils {
2525

2626
public static final Set<String> OUTER_COLUMNS =
2727
new TreeSet<>(List.of(ID, CREATED_AT, UPDATED_AT));
28+
public static final String ID_COLUMN = ID;
29+
2830
public static final String DOCUMENT_COLUMN = DOCUMENT;
2931

3032
public enum Type {

0 commit comments

Comments
 (0)