Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
public interface Document {

String toJson();

default DocumentType getDocumentType() {
return DocumentType.NESTED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.hypertrace.core.documentstore;

public enum DocumentType {
// FLAT documents contain individual columns for each attribute
FLAT,
// NESTED documents contains attributes as a nested JSON document
NESTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,40 @@ public class JSONDocument implements Document {

private static ObjectMapper mapper = new ObjectMapper();
private JsonNode node;
private DocumentType documentType = DocumentType.NESTED;

public JSONDocument(String json) throws IOException {
node = mapper.readTree(json);
}

public JSONDocument(String json, DocumentType documentType) throws IOException {
node = mapper.readTree(json);
this.documentType = documentType;
}

public JSONDocument(Object object) throws IOException {
node = mapper.readTree(mapper.writeValueAsString(object));
}

public JSONDocument(Object object, DocumentType documentType) throws IOException {
node = mapper.readTree(mapper.writeValueAsString(object));
this.documentType = documentType;
}

public JSONDocument(JsonNode node) {
this.node = node;
}

public JSONDocument(JsonNode node, DocumentType documentType) {
this.node = node;
this.documentType = documentType;
}

@Override
public DocumentType getDocumentType() {
return this.documentType;
}

@Override
public String toJson() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.hypertrace.core.documentstore.Collection;
import org.hypertrace.core.documentstore.CreateResult;
import org.hypertrace.core.documentstore.Document;
import org.hypertrace.core.documentstore.DocumentType;
import org.hypertrace.core.documentstore.Filter;
import org.hypertrace.core.documentstore.JSONDocument;
import org.hypertrace.core.documentstore.Key;
Expand Down Expand Up @@ -1264,6 +1265,10 @@ public PostgresResultIteratorWithBasicTypes(ResultSet resultSet) {
super(resultSet);
}

public PostgresResultIteratorWithBasicTypes(ResultSet resultSet, DocumentType documentType) {
super(resultSet, documentType);
}

@Override
public Document next() {
try {
Expand Down Expand Up @@ -1299,7 +1304,7 @@ protected Document prepareDocument() throws SQLException, IOException {
jsonNode.remove(DOCUMENT_ID);
}

return new JSONDocument(MAPPER.writeValueAsString(jsonNode));
return new JSONDocument(MAPPER.writeValueAsString(jsonNode), documentType);
}

private void addColumnToJsonNode(
Expand Down Expand Up @@ -1386,17 +1391,29 @@ static class PostgresResultIterator implements CloseableIterator<Document> {

protected final ObjectMapper MAPPER = new ObjectMapper();
protected ResultSet resultSet;
private final boolean removeDocumentId;
protected boolean cursorMovedForward = false;
protected boolean hasNext = false;

private final boolean removeDocumentId;
protected DocumentType documentType;

public PostgresResultIterator(ResultSet resultSet) {
this(resultSet, true);
}

PostgresResultIterator(ResultSet resultSet, boolean removeDocumentId) {
this(resultSet, removeDocumentId, DocumentType.NESTED);
}

public PostgresResultIterator(ResultSet resultSet, DocumentType documentType) {
this(resultSet, true, documentType);
}

PostgresResultIterator(
ResultSet resultSet, boolean removeDocumentId, DocumentType documentType) {
this.resultSet = resultSet;
this.removeDocumentId = removeDocumentId;
this.documentType = documentType;
}

@Override
Expand Down Expand Up @@ -1447,7 +1464,7 @@ protected Document prepareDocument() throws SQLException, IOException {
jsonNode.put(CREATED_AT, String.valueOf(createdAt));
jsonNode.put(UPDATED_AT, String.valueOf(updatedAt));

return new JSONDocument(MAPPER.writeValueAsString(jsonNode));
return new JSONDocument(MAPPER.writeValueAsString(jsonNode), documentType);
}

protected void closeResultSet() {
Expand Down Expand Up @@ -1508,7 +1525,7 @@ protected Document prepareDocument() throws SQLException, IOException {
}
}
}
return new JSONDocument(MAPPER.writeValueAsString(jsonNode));
return new JSONDocument(MAPPER.writeValueAsString(jsonNode), documentType);
}

private String getColumnValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.extern.slf4j.Slf4j;
import org.hypertrace.core.documentstore.CloseableIterator;
import org.hypertrace.core.documentstore.Document;
import org.hypertrace.core.documentstore.DocumentType;
import org.hypertrace.core.documentstore.postgres.PostgresCollection.PostgresResultIterator;
import org.hypertrace.core.documentstore.postgres.PostgresCollection.PostgresResultIteratorWithMetaData;
import org.hypertrace.core.documentstore.postgres.query.v1.transformer.PostgresQueryTransformer;
Expand All @@ -18,6 +19,7 @@
@Slf4j
@AllArgsConstructor
public class PostgresQueryExecutor {

private final PostgresTableIdentifier tableIdentifier;

public CloseableIterator<Document> execute(final Connection connection, final Query query) {
Expand All @@ -37,7 +39,8 @@ public CloseableIterator<Document> execute(
final ResultSet resultSet = preparedStatement.executeQuery();

if ((tableIdentifier.getTableName().equals(flatStructureCollectionName))) {
return new PostgresCollection.PostgresResultIteratorWithBasicTypes(resultSet);
return new PostgresCollection.PostgresResultIteratorWithBasicTypes(
resultSet, DocumentType.FLAT);
}
return query.getSelections().size() > 0
? new PostgresResultIteratorWithMetaData(resultSet)
Expand Down
Loading