forked from patrickxb/fgosqlite
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackup.go
124 lines (113 loc) · 3.45 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite
/*
#include <sqlite3.h>
#include <stdlib.h>
*/
import "C"
import (
"errors"
"time"
"unsafe"
)
// NewBackup initializes the backup/copy of the content of one database (source) to another (destination).
// The database name is "main", "temp", or the name specified in an ATTACH statement.
//
// (See http://sqlite.org/c3ref/backup_finish.html#sqlite3backupinit)
func NewBackup(dst *Conn, dstName string, src *Conn, srcName string) (*Backup, error) {
if dst == nil || src == nil {
return nil, errors.New("nil sqlite backup source or destination")
}
dname := C.CString(dstName)
sname := C.CString(srcName)
sb := C.sqlite3_backup_init(dst.db, dname, src.db, sname)
C.free(unsafe.Pointer(sname))
C.free(unsafe.Pointer(dname))
if sb == nil {
return nil, dst.error(C.sqlite3_errcode(dst.db), "backup init failed")
}
return &Backup{sb, dst, src}, nil
}
// The Backup object records state information about an ongoing online backup operation.
// (See http://sqlite.org/c3ref/backup.html)
type Backup struct {
sb *C.sqlite3_backup
dst, src *Conn
}
// Step copies up to N pages between the source and destination databases.
// (See http://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep)
func (b *Backup) Step(npage int32) error {
if b == nil {
return errors.New("nil sqlite backup")
}
rv := C.sqlite3_backup_step(b.sb, C.int(npage))
if rv == C.SQLITE_OK || Errno(rv&0xFF) == ErrBusy || Errno(rv&0xFF) == ErrLocked { // TODO Trace busy/locked errors
return nil
} else if rv == C.SQLITE_DONE {
return Errno(rv)
}
return b.dst.error(rv, "backup step failed")
}
// BackupStatus reports backup progression
type BackupStatus struct {
Remaining int
PageCount int
}
// Status returns the number of pages still to be backed up and the total number of pages in the source database file.
// (See http://sqlite.org/c3ref/backup_finish.html#sqlite3backupremaining)
func (b *Backup) Status() BackupStatus {
return BackupStatus{int(C.sqlite3_backup_remaining(b.sb)), int(C.sqlite3_backup_pagecount(b.sb))}
}
// Run starts the backup:
// - copying up to 'npage' pages between the source and destination at each step,
// - sleeping 'sleepNs' between steps,
// - notifying the caller of backup progress throw the channel 'c',
// - closing the backup when done or when an error happens.
// Sleeping is disabled if 'sleepNs' is zero or negative.
// Notification is disabled if 'c' is null.
// (See http://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep, sqlite3_backup_remaining and sqlite3_backup_pagecount)
func (b *Backup) Run(npage int32, sleepNs time.Duration, c chan<- BackupStatus) error {
var err error
for {
err = b.Step(npage)
if err != nil {
break
}
if c != nil {
c <- b.Status()
}
if sleepNs > 0 {
time.Sleep(sleepNs)
}
}
if err != Done {
_ = b.Close()
} else {
if c != nil {
c <- b.Status()
}
err = b.Close()
}
if err != nil && err != Done {
return err
}
return nil
}
// Close finishes/stops the backup.
// (See http://sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish)
func (b *Backup) Close() error {
if b == nil {
return errors.New("nil sqlite backup")
}
if b.sb == nil {
return nil
}
rv := C.sqlite3_backup_finish(b.sb) // must be called only once
b.sb = nil
if rv != C.SQLITE_OK {
return b.dst.error(rv, "backup finish failed")
}
return nil
}