Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -24,9 +24,12 @@
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

public class MongoIndex
public record MongoIndex(List<MongodbIndexKey> keys)
{
private final List<MongodbIndexKey> keys;
public MongoIndex
{
keys = ImmutableList.copyOf(keys);
}

public static List<MongoIndex> parse(ListIndexesIterable<Document> indexes)
{
Expand All @@ -53,10 +56,11 @@ private static List<MongodbIndexKey> parseKey(Document key)
if (value instanceof Number) {
int order = ((Number) value).intValue();
checkState(order == 1 || order == -1, "Unknown index sort order");
builder.add(new MongodbIndexKey(name, order == 1 ? SortOrder.ASC_NULLS_LAST : SortOrder.DESC_NULLS_LAST));
SortOrder sortOrder = order == 1 ? SortOrder.ASC_NULLS_LAST : SortOrder.DESC_NULLS_LAST;
builder.add(new MongodbIndexKey(name, Optional.of(sortOrder)));
}
else if (value instanceof String) {
builder.add(new MongodbIndexKey(name, (String) value));
builder.add(new MongodbIndexKey(name, Optional.empty()));
}
else {
throw new UnsupportedOperationException("Unknown index type: " + value.toString());
Expand All @@ -66,52 +70,12 @@ else if (value instanceof String) {
return builder.build();
}

public MongoIndex(List<MongodbIndexKey> keys)
{
this.keys = keys;
}

public List<MongodbIndexKey> getKeys()
{
return keys;
}

public static class MongodbIndexKey
public record MongodbIndexKey(String name, Optional<SortOrder> sortOrder)
{
private final String name;
private final Optional<SortOrder> sortOrder;
private final Optional<String> type;

public MongodbIndexKey(String name, SortOrder sortOrder)
{
this(name, Optional.of(sortOrder), Optional.empty());
}

public MongodbIndexKey(String name, String type)
{
this(name, Optional.empty(), Optional.of(type));
}

public MongodbIndexKey(String name, Optional<SortOrder> sortOrder, Optional<String> type)
{
this.name = requireNonNull(name, "name is null");
this.sortOrder = sortOrder;
this.type = type;
}

public String getName()
{
return name;
}

public Optional<SortOrder> getSortOrder()
{
return sortOrder;
}

public Optional<String> getType()
public MongodbIndexKey
{
return type;
requireNonNull(name, "name is null");
requireNonNull(sortOrder, "sortOrder is null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,12 @@ public ConnectorTableProperties getTableProperties(ConnectorSession session, Con
Map<String, ColumnHandle> columns = getColumnHandles(session, tableHandle);

for (MongoIndex index : tableInfo.indexes()) {
for (MongodbIndexKey key : index.getKeys()) {
if (key.getSortOrder().isEmpty()) {
for (MongodbIndexKey key : index.keys()) {
if (key.sortOrder().isEmpty()) {
continue;
}
if (columns.get(key.getName()) != null) {
localProperties.add(new SortingProperty<>(columns.get(key.getName()), key.getSortOrder().get()));
if (columns.get(key.name()) != null) {
localProperties.add(new SortingProperty<>(columns.get(key.name()), key.sortOrder().get()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public boolean collectionExists(MongoDatabase db, String collectionName)
private boolean indexExists(MongoCollection<Document> schemaCollection)
{
return MongoIndex.parse(schemaCollection.listIndexes()).stream()
.anyMatch(index -> index.getKeys().size() == 1 && TABLE_NAME_KEY.equals(index.getKeys().get(0).getName()));
.anyMatch(index -> index.keys().size() == 1 && TABLE_NAME_KEY.equals(index.keys().get(0).name()));
}

private Set<String> getTableMetadataNames(String schemaName)
Expand Down