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
44 changes: 44 additions & 0 deletions lib/src/main/java/io/cloudquery/schema/Table.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.cloudquery.schema;

import lombok.Builder;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Builder(toBuilder = true)
@Getter
public class Table {
public static List<Table> flattenTables(List<Table> tables) {
Map<String, Table> flattenMap = new HashMap<>();
for (Table table : tables) {
Table newTable = table.toBuilder().relations(Collections.emptyList()).build();
flattenMap.put(newTable.name, newTable);
for (Table child : flattenTables(table.getRelations())) {
flattenMap.put(child.name, child);
}
}
return flattenMap.values().stream().toList();
}

public static int maxDepth(List<Table> tables) {
int depth = 0;
if (tables.isEmpty()) {
return 0;
}
for (Table table : tables) {
int newDepth = 1 + maxDepth(table.getRelations());
if (newDepth > depth) {
depth = newDepth;
}
}
return depth;
}

private String name;

@Builder.Default
private List<Table> relations = Collections.emptyList();
}
52 changes: 52 additions & 0 deletions lib/src/test/java/io/cloudquery/schema/TableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.cloudquery.schema;

import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class TableTest {

public Table testTable;

@Before
public void setUp() {
testTable = Table.builder().
name("test").
relations(List.of(
Table.builder().name("test2").build(),
Table.builder().name("test3").build(),
Table.builder().name("test4").build()
)).build();
}

@Test
public void shouldFlattenTables() {
List<Table> srcTables = List.of(testTable);
List<Table> flattenedTables = Table.flattenTables(srcTables);

assertEquals(1, srcTables.size());
assertEquals(3, testTable.getRelations().size());
assertEquals(4, flattenedTables.size());
}

@Test
public void shouldFlattenTablesWithDuplicates() {
List<Table> srcTables = List.of(testTable, testTable, testTable);
List<Table> flattenedTables = Table.flattenTables(srcTables);

assertEquals(3, srcTables.size());
assertEquals(3, testTable.getRelations().size());
assertEquals(4, flattenedTables.size());
}

@Test
public void shouldReturnMaxDepth() {
assertEquals(0, Table.maxDepth(Collections.emptyList()));
assertEquals(2, Table.maxDepth(List.of(testTable)));
assertEquals(3, Table.maxDepth(List.of(testTable.toBuilder().relations(List.of(testTable)).build())));
}
}