Skip to content

Stream documents in ExportCollection instead of buffering the whole collection - #170

Open
ChrisJr404 wants to merge 1 commit into
ostafen:v2from
ChrisJr404:export-collection-streaming
Open

ChrisJr404 wants to merge 1 commit into
ostafen:v2from
ChrisJr404:export-collection-streaming

Conversation

@ChrisJr404

Copy link
Copy Markdown

Fixes #130.

ExportCollection was calling FindAll and building a []map[string]interface{} of the entire collection before a single json.Marshal, so peak memory grew with the collection size. This switches it to IterateDocs and writes each document straight to a buffered writer, opening with [, joining docs with , and closing with ], so memory stays flat no matter how big the collection is.

The output is the same JSON array as before, so existing exports and ImportCollection keep working. Added a test that exports a populated collection and an empty one and checks both come back as well-formed JSON arrays with the expected element counts.

…ollection

ExportCollection loaded every document into memory and marshalled them
in one shot, which gets expensive for large collections. Iterate the
documents and write them to the file one at a time so memory stays flat
regardless of collection size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory efficient ExportCollection()

1 participant