-
Notifications
You must be signed in to change notification settings - Fork 2
chore: adding supporting decode for write logic #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,61 @@ | ||
package io.cloudquery.memdb; | ||
|
||
import io.cloudquery.messages.WriteMessage; | ||
import io.cloudquery.messages.WriteMigrateTable; | ||
import io.cloudquery.schema.ClientMeta; | ||
import io.cloudquery.schema.Table; | ||
import io.cloudquery.schema.TableColumnChange; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import org.apache.arrow.vector.VectorSchemaRoot; | ||
|
||
public class MemDBClient implements ClientMeta { | ||
private static final String id = "memdb"; | ||
|
||
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
private Map<String, Table> tables = new HashMap<>(); | ||
private Map<String, List<VectorSchemaRoot>> memDB = new HashMap<>(); | ||
|
||
public MemDBClient() {} | ||
|
||
@Override | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public void write(WriteMessage message) { | ||
if (message instanceof WriteMigrateTable migrateTable) { | ||
migrate(migrateTable); | ||
} | ||
} | ||
|
||
public void close() { | ||
// do nothing | ||
} | ||
|
||
private void migrate(WriteMigrateTable migrateTable) { | ||
lock.writeLock().lock(); | ||
try { | ||
Table table = migrateTable.getTable(); | ||
String tableName = table.getName(); | ||
if (!memDB.containsKey(tableName)) { | ||
memDB.put(tableName, new ArrayList<>()); | ||
tables.put(tableName, table); | ||
return; | ||
} | ||
|
||
List<TableColumnChange> changes = table.getChanges(tables.get(tableName)); | ||
if (changes.isEmpty()) { | ||
return; | ||
} | ||
memDB.put(tableName, new ArrayList<>()); | ||
tables.put(tableName, table); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package io.cloudquery.messages; | ||
|
||
public abstract class WriteMessage {} |
12 changes: 12 additions & 0 deletions
12
lib/src/main/java/io/cloudquery/messages/WriteMigrateTable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.cloudquery.messages; | ||
|
||
import io.cloudquery.schema.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class WriteMigrateTable extends WriteMessage { | ||
private Table table; | ||
private boolean migrateForce; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package io.cloudquery.schema; | ||
|
||
import io.cloudquery.messages.WriteMessage; | ||
|
||
public interface ClientMeta { | ||
String getId(); | ||
|
||
void write(WriteMessage message); | ||
} |
85 changes: 85 additions & 0 deletions
85
lib/src/test/java/io/cloudquery/helper/ArrowHelperTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.cloudquery.helper; | ||
|
||
import static io.cloudquery.helper.ArrowHelper.CQ_TABLE_DEPENDS_ON; | ||
import static io.cloudquery.helper.ArrowHelper.CQ_TABLE_DESCRIPTION; | ||
import static io.cloudquery.helper.ArrowHelper.CQ_TABLE_NAME; | ||
import static io.cloudquery.helper.ArrowHelper.CQ_TABLE_TITLE; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.google.protobuf.ByteString; | ||
import io.cloudquery.schema.Column; | ||
import io.cloudquery.schema.Table; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.arrow.vector.types.pojo.ArrowType; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ArrowHelperTest { | ||
|
||
public static final Table TEST_TABLE = | ||
Table.builder() | ||
.name("table1") | ||
.description("A simple test table") | ||
.title("Test table title") | ||
.parent(Table.builder().name("parent").build()) | ||
.columns( | ||
List.of( | ||
Column.builder().name("column1").type(ArrowType.Utf8.INSTANCE).build(), | ||
Column.builder().name("column2").type(ArrowType.Utf8.INSTANCE).build())) | ||
.build(); | ||
|
||
@Test | ||
public void testToArrowSchema() { | ||
Schema arrowSchema = ArrowHelper.toArrowSchema(TEST_TABLE); | ||
|
||
assertEquals(arrowSchema.getFields().get(0).getName(), "column1"); | ||
assertEquals(arrowSchema.getFields().get(1).getName(), "column2"); | ||
|
||
assertEquals( | ||
arrowSchema.getCustomMetadata(), | ||
Map.of( | ||
CQ_TABLE_NAME, "table1", | ||
CQ_TABLE_DESCRIPTION, "A simple test table", | ||
CQ_TABLE_TITLE, "Test table title", | ||
CQ_TABLE_DEPENDS_ON, "parent")); | ||
} | ||
|
||
@Test | ||
public void testFromArrowSchema() { | ||
List<Field> fields = | ||
List.of( | ||
Field.nullable("column1", ArrowType.Utf8.INSTANCE), | ||
Field.nullable("column2", ArrowType.Utf8.INSTANCE)); | ||
|
||
Schema schema = new Schema(fields, Map.of(CQ_TABLE_NAME, "table1")); | ||
|
||
Table table = ArrowHelper.fromArrowSchema(schema); | ||
|
||
assertEquals(table.getName(), "table1"); | ||
|
||
for (int i = 0; i < table.getColumns().size(); i++) { | ||
Column column = table.getColumns().get(i); | ||
assertEquals(column.getName(), fields.get(i).getName()); | ||
assertEquals(column.getType(), fields.get(i).getType()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRoundTrip() throws IOException { | ||
ByteString byteString = ArrowHelper.encode(TEST_TABLE); | ||
Table table = ArrowHelper.decode(byteString); | ||
|
||
assertEquals(table.getName(), TEST_TABLE.getName()); | ||
assertEquals(table.getDescription(), TEST_TABLE.getDescription()); | ||
assertEquals(table.getTitle(), TEST_TABLE.getTitle()); | ||
assertEquals(table.getParent().getName(), TEST_TABLE.getParent().getName()); | ||
|
||
for (int i = 0; i < TEST_TABLE.getColumns().size(); i++) { | ||
assertEquals(TEST_TABLE.getColumns().get(i).getName(), table.getColumns().get(i).getName()); | ||
assertEquals(TEST_TABLE.getColumns().get(i).getType(), table.getColumns().get(i).getType()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[non blocking] I think we can skip these
null
checks and pass everything to builder. If the values arenull
it won't change anything on the resulted table, WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes sense. I'll modify as part of the next PR.