-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
467 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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,52 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"github.com/artie-labs/reader/constants" | ||
"github.com/artie-labs/transfer/lib/stringutil" | ||
) | ||
|
||
type MongoDB struct { | ||
Host string `yaml:"host"` | ||
Username string `yaml:"username"` | ||
Password string `yaml:"password"` | ||
Database string `yaml:"database"` | ||
|
||
Collections []Collection `yaml:"collections"` | ||
} | ||
|
||
type Collection struct { | ||
Name string `yaml:"name"` | ||
BatchSize int32 `yaml:"batchSize,omitempty"` | ||
// TODO: In the future, we should be able to support customers passing Start/End PK values. | ||
} | ||
|
||
func (c Collection) TopicSuffix(db string) string { | ||
return fmt.Sprintf("%s.%s", db, c.Name) | ||
} | ||
|
||
func (c Collection) GetBatchSize() int32 { | ||
if c.BatchSize == 0 { | ||
return constants.DefaultBatchSize | ||
} | ||
|
||
return c.BatchSize | ||
} | ||
|
||
func (m MongoDB) Validate() error { | ||
if stringutil.Empty(m.Host, m.Database, m.Username, m.Password) { | ||
return fmt.Errorf("one of the MongoDB settings is empty: host, username, password, database") | ||
} | ||
|
||
if len(m.Collections) == 0 { | ||
return fmt.Errorf("no collections passed in") | ||
} | ||
|
||
for _, collection := range m.Collections { | ||
if collection.Name == "" { | ||
return fmt.Errorf("collection name must be passed in") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,13 @@ | ||
source: mongodb | ||
mongodb: | ||
host: mongodb://localhost:27017 | ||
username: foo | ||
password: bar | ||
database: myFirstDatabase | ||
collections: | ||
- name: customers | ||
batchSize: 1 | ||
kafka: | ||
bootstrapServers: localhost:29092 | ||
topicPrefix: dbserver1 | ||
maxRequestSize: 4194304 |
This file contains 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 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 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 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 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,82 @@ | ||
package mongo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/artie-labs/reader/config" | ||
"github.com/artie-labs/reader/lib" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
type collectionScanner struct { | ||
db *mongo.Database | ||
cfg config.MongoDB | ||
collection config.Collection | ||
|
||
// mutable | ||
cursor *mongo.Cursor | ||
done bool | ||
} | ||
|
||
func newIterator(db *mongo.Database, collection config.Collection, cfg config.MongoDB) *collectionScanner { | ||
return &collectionScanner{ | ||
db: db, | ||
cfg: cfg, | ||
collection: collection, | ||
} | ||
} | ||
|
||
func (c *collectionScanner) HasNext() bool { | ||
return !c.done | ||
} | ||
|
||
func (c *collectionScanner) Next() ([]lib.RawMessage, error) { | ||
if !c.HasNext() { | ||
return nil, fmt.Errorf("no more rows to scan") | ||
} | ||
|
||
ctx := context.Background() | ||
if c.cursor == nil { | ||
findOptions := options.Find() | ||
findOptions.SetBatchSize(c.collection.GetBatchSize()) | ||
cursor, err := c.db.Collection(c.collection.Name).Find(ctx, bson.D{}, findOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to find documents: %w", err) | ||
} | ||
|
||
c.cursor = cursor | ||
} | ||
|
||
var rawMsgs []lib.RawMessage | ||
for c.collection.GetBatchSize() > int32(len(rawMsgs)) && c.cursor.Next(ctx) { | ||
var result bson.M | ||
if err := c.cursor.Decode(&result); err != nil { | ||
return nil, fmt.Errorf("failed to decode document: %w", err) | ||
} | ||
|
||
mgoMsg, err := parseMessage(result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse message: %w", err) | ||
} | ||
|
||
rawMsg, err := mgoMsg.toRawMessage(c.collection, c.cfg.Database) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create raw message: %w", err) | ||
} | ||
|
||
rawMsgs = append(rawMsgs, rawMsg) | ||
} | ||
|
||
if err := c.cursor.Err(); err != nil { | ||
return nil, fmt.Errorf("failed to iterate over documents: %w", err) | ||
} | ||
|
||
// If the number of fetched documents is less than the batch size, we are done | ||
if c.collection.GetBatchSize() > int32(len(rawMsgs)) { | ||
c.done = true | ||
} | ||
|
||
return rawMsgs, nil | ||
} |
Oops, something went wrong.