Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
improved export function to only use a read transaction (instead of c…
Browse files Browse the repository at this point in the history
…losing entire database) and set gzip level to max compression, which reduces file size by 1/3 in quick tests
  • Loading branch information
thwd-duda committed Dec 19, 2017
1 parent 9340a78 commit 89b58c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
42 changes: 19 additions & 23 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,22 @@ func ExportHttpHandler(rw http.ResponseWriter, rq *http.Request) {
return
}

e := db.WhileClosed(func() {

f, e := os.Open(os.Getenv("DATA_FILE"))
e := func() error {
ow, e := gzip.NewWriterLevel(rw, gzip.BestCompression)
if e != nil {
rw.WriteHeader(http.StatusNotFound)
// ?
return
return e
}
ow := gzip.NewWriter(rw)
_, e = io.Copy(ow, f)
tx, e := dtbs.Begin(false)
if e != nil {
log.Panicln(e.Error())
return e
}
ow.Close()
f.Close()

})
defer tx.Rollback()
rw.Header().Set(`Content-Type`, `application/octet-stream`)
rw.Header().Set(`Content-Disposition`, `attachment; filename="`+os.Getenv("DATA_FILE")+`"`)
rw.Header().Set(`Content-Encoding`, `gzip`)
_, e = tx.WriteTo(ow)
return e
}()

if e != nil {
log.Println(e)
Expand Down Expand Up @@ -83,28 +82,25 @@ func ImportHttpHandler(rw http.ResponseWriter, rq *http.Request) {
return
}

e := db.WhileClosed(func() {
e := db.WhileClosed(func() error {

f, e := os.OpenFile(os.Getenv("DATA_FILE"), os.O_RDWR|os.O_TRUNC|os.O_CREATE, db.Perm)
if e != nil {
rw.WriteHeader(http.StatusNotFound)
// ?
return
return e
}
ir, e := gzip.NewReader(rq.Body)
if e != nil {
rw.WriteHeader(http.StatusBadRequest)
// ?
return
return e
}
_, e = io.Copy(f, ir)
if e != nil {
log.Panicln(e.Error())
}
ir.Close()
f.Close()
rq.Body.Close()
_ = ir.Close()
_ = f.Close()
_ = rq.Body.Close()
kvm.ClearCompilerCache()
return nil
})

if e != nil {
Expand Down
5 changes: 2 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Open() (*bolt.DB, error) {
return database, nil
}

func WhileClosed(f func()) error {
func WhileClosed(f func() error) error {

mutex.Lock()
defer mutex.Unlock()
Expand All @@ -57,9 +57,8 @@ func WhileClosed(f func()) error {
}
database = nil

f()
return f()

return nil
}

// reloads the underlying database from file
Expand Down

0 comments on commit 89b58c5

Please sign in to comment.