|
| 1 | +// Package serdes provides functions to (de)serialize databases. |
| 2 | +package serdes |
| 3 | + |
| 4 | +import ( |
| 5 | + "io" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/ncruces/go-sqlite3" |
| 9 | + "github.com/ncruces/go-sqlite3/vfs" |
| 10 | +) |
| 11 | + |
| 12 | +func init() { |
| 13 | + vfs.Register(vfsName, sliceVFS{}) |
| 14 | +} |
| 15 | + |
| 16 | +// Serialize backs up a database into a byte slice. |
| 17 | +// |
| 18 | +// https://sqlite.org/c3ref/serialize.html |
| 19 | +func Serialize(db *sqlite3.Conn, schema string) ([]byte, error) { |
| 20 | + var file sliceFile |
| 21 | + openMtx.Lock() |
| 22 | + openFile = &file |
| 23 | + err := db.Backup(schema, "file:db?vfs="+vfsName) |
| 24 | + return file.data, err |
| 25 | +} |
| 26 | + |
| 27 | +// Deserialize restores a database from a byte slice, |
| 28 | +// DESTROYING any contents previously stored in schema. |
| 29 | +// |
| 30 | +// To non-destructively open a database from a byte slice, |
| 31 | +// consider alternatives like the ["reader"] or ["memdb"] VFSes. |
| 32 | +// |
| 33 | +// This differs from the similarly named SQLite API |
| 34 | +// in that it DOES NOT disconnect from schema |
| 35 | +// to reopen as an in-memory database. |
| 36 | +// |
| 37 | +// https://sqlite.org/c3ref/deserialize.html |
| 38 | +// |
| 39 | +// ["memdb"]: https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/memdb |
| 40 | +// ["reader"]: https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/readervfs |
| 41 | +func Deserialize(db *sqlite3.Conn, schema string, data []byte) error { |
| 42 | + openMtx.Lock() |
| 43 | + openFile = &sliceFile{data} |
| 44 | + return db.Restore(schema, "file:db?vfs="+vfsName) |
| 45 | +} |
| 46 | + |
| 47 | +var ( |
| 48 | + openMtx sync.Mutex |
| 49 | + openFile *sliceFile |
| 50 | +) |
| 51 | + |
| 52 | +const vfsName = "github.com/ncruces/go-sqlite3/ext/deserialize.sliceVFS" |
| 53 | + |
| 54 | +type sliceVFS struct{} |
| 55 | + |
| 56 | +func (sliceVFS) Open(name string, flags vfs.OpenFlag) (vfs.File, vfs.OpenFlag, error) { |
| 57 | + if flags&vfs.OPEN_MAIN_DB == 0 { |
| 58 | + // notest // OPEN_MEMORY |
| 59 | + return nil, flags, sqlite3.CANTOPEN |
| 60 | + } |
| 61 | + |
| 62 | + file := openFile |
| 63 | + openFile = nil |
| 64 | + openMtx.Unlock() |
| 65 | + |
| 66 | + if file.data != nil { |
| 67 | + flags |= vfs.OPEN_READONLY |
| 68 | + } |
| 69 | + flags |= vfs.OPEN_MEMORY |
| 70 | + return file, flags, nil |
| 71 | +} |
| 72 | + |
| 73 | +func (sliceVFS) Delete(name string, dirSync bool) error { |
| 74 | + // notest // OPEN_MEMORY |
| 75 | + return sqlite3.IOERR_DELETE |
| 76 | +} |
| 77 | + |
| 78 | +func (sliceVFS) Access(name string, flag vfs.AccessFlag) (bool, error) { |
| 79 | + return name == "db", nil |
| 80 | +} |
| 81 | + |
| 82 | +func (sliceVFS) FullPathname(name string) (string, error) { |
| 83 | + return name, nil |
| 84 | +} |
| 85 | + |
| 86 | +type sliceFile struct{ data []byte } |
| 87 | + |
| 88 | +func (f *sliceFile) ReadAt(b []byte, off int64) (n int, err error) { |
| 89 | + if d := f.data; off < int64(len(d)) { |
| 90 | + n = copy(b, d[off:]) |
| 91 | + } |
| 92 | + if n == 0 { |
| 93 | + err = io.EOF |
| 94 | + } |
| 95 | + return |
| 96 | +} |
| 97 | + |
| 98 | +func (f *sliceFile) WriteAt(b []byte, off int64) (n int, err error) { |
| 99 | + if d := f.data; off > int64(len(d)) { |
| 100 | + f.data = append(d, make([]byte, off-int64(len(d)))...) |
| 101 | + } |
| 102 | + d := append(f.data[:off], b...) |
| 103 | + if len(d) > len(f.data) { |
| 104 | + f.data = d |
| 105 | + } |
| 106 | + return len(b), nil |
| 107 | +} |
| 108 | + |
| 109 | +func (f *sliceFile) Size() (int64, error) { |
| 110 | + return int64(len(f.data)), nil |
| 111 | +} |
| 112 | + |
| 113 | +func (f *sliceFile) Truncate(size int64) error { |
| 114 | + if d := f.data; size < int64(len(d)) { |
| 115 | + f.data = d[:size] |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func (f *sliceFile) SizeHint(size int64) error { |
| 121 | + if d := f.data; size > int64(len(d)) { |
| 122 | + f.data = append(d, make([]byte, size-int64(len(d)))...) |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func (*sliceFile) Close() error { return nil } |
| 128 | + |
| 129 | +func (*sliceFile) Sync(flag vfs.SyncFlag) error { return nil } |
| 130 | + |
| 131 | +func (*sliceFile) Lock(lock vfs.LockLevel) error { return nil } |
| 132 | + |
| 133 | +func (*sliceFile) Unlock(lock vfs.LockLevel) error { return nil } |
| 134 | + |
| 135 | +func (*sliceFile) CheckReservedLock() (bool, error) { |
| 136 | + // notest // OPEN_MEMORY |
| 137 | + return false, nil |
| 138 | +} |
| 139 | + |
| 140 | +func (*sliceFile) SectorSize() int { |
| 141 | + // notest // IOCAP_POWERSAFE_OVERWRITE |
| 142 | + return 0 |
| 143 | +} |
| 144 | + |
| 145 | +func (*sliceFile) DeviceCharacteristics() vfs.DeviceCharacteristic { |
| 146 | + return vfs.IOCAP_ATOMIC | |
| 147 | + vfs.IOCAP_SAFE_APPEND | |
| 148 | + vfs.IOCAP_SEQUENTIAL | |
| 149 | + vfs.IOCAP_POWERSAFE_OVERWRITE | |
| 150 | + vfs.IOCAP_SUBPAGE_READ |
| 151 | +} |
0 commit comments