-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Export document to jsonFile one by one #131
base: v2
Are you sure you want to change the base?
Changes from 3 commits
1b905e6
517fd8c
2bb9d36
fe14b77
6cd0197
fa7578e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package clover | |
import ( | ||
"bufio" | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
|
||
d "github.com/ostafen/clover/v2/document" | ||
|
@@ -19,23 +18,42 @@ func (db *DB) ExportCollection(collectionName string, exportPath string) error { | |
if !exists { | ||
return ErrCollectionNotExist | ||
} | ||
|
||
result, err := db.FindAll(query.NewQuery(collectionName)) | ||
q := query.NewQuery(collectionName) | ||
collectionCount, err := db.Count(q) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
docs := make([]map[string]interface{}, 0) | ||
for _, doc := range result { | ||
docs = append(docs, doc.AsMap()) | ||
f, err := os.Create(exportPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
if _, err = f.WriteString("["); err != nil { | ||
return err | ||
} | ||
|
||
jsonString, err := json.Marshal(docs) | ||
n := 0 | ||
err = db.ForEach(q, func(doc *d.Document) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
To prevent this issue, we can ensure the number of documents iterated by the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You remind me that is can also cause problems when documents are deleted between them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a suggestion for ForEach() function, let consumer() return error instead of bool. If there is any failure inside this function, we can get the error info directly
|
||
n++ | ||
jsonByte, err := json.Marshal(doc.AsMap()) | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be a good idea to recover this error in the outer scope. |
||
return false | ||
} | ||
jsonString := string(jsonByte) | ||
if n != collectionCount { | ||
jsonString += "," | ||
} | ||
_, err = f.WriteString(jsonString) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess same thing for this error |
||
return err == nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ioutil.WriteFile(exportPath, jsonString, os.ModePerm) | ||
if _, err = f.WriteString("]"); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When possible, like in this case, I prefer using the |
||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ImportCollection imports a collection from a JSON file. | ||
|
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.
I would just call this variable
count
, for simplicity