1
0
Fork 0

Add GoDoc comments

master
Philip O'Toole 1 year ago
parent 307221d626
commit 9e0cb2a82c

@ -10,23 +10,27 @@ import (
"github.com/rqlite/rqlite/db"
)
// Snapshot represents a snapshot of the database state.
type Snapshot struct {
walData []byte
files []string
}
// NewWALSnapshot creates a new snapshot from a WAL.
func NewWALSnapshot(b []byte) *Snapshot {
return &Snapshot{
walData: b,
}
}
// NewFullSnapshot creates a new snapshot from a SQLite file and WALs.
func NewFullSnapshot(files ...string) *Snapshot {
return &Snapshot{
files: files,
}
}
// Persist writes the snapshot to the given sink.
func (s *Snapshot) Persist(sink raft.SnapshotSink) error {
stream, err := s.OpenStream()
if err != nil {
@ -38,8 +42,10 @@ func (s *Snapshot) Persist(sink raft.SnapshotSink) error {
return err
}
// Release is a no-op.
func (s *Snapshot) Release() {}
// OpenStream returns a stream for reading the snapshot.
func (s *Snapshot) OpenStream() (*Stream, error) {
if len(s.files) > 0 {
return NewFullStream(s.files...)
@ -47,6 +53,8 @@ func (s *Snapshot) OpenStream() (*Stream, error) {
return NewIncrementalStream(s.walData)
}
// ReplayDB reconstructs the database from the given reader, and writes it to
// the given path.
func ReplayDB(fullSnap *FullSnapshot, r io.Reader, path string) error {
dbInfo := fullSnap.GetDb()
if dbInfo == nil {

@ -16,12 +16,14 @@ const (
streamVersion = 1
)
// NewStreamHeader creates a new StreamHeader.
func NewStreamHeader() *StreamHeader {
return &StreamHeader{
Version: streamVersion,
}
}
// NewStreamHeaderFromReader reads a StreamHeader from the given reader.
func NewStreamHeaderFromReader(r io.Reader) (*StreamHeader, int64, error) {
var totalSizeRead int64
@ -51,6 +53,7 @@ func NewStreamHeaderFromReader(r io.Reader) (*StreamHeader, int64, error) {
return strHdr, totalSizeRead, nil
}
// FileSize returns the total size of the files in the snapshot.
func (s *StreamHeader) FileSize() int64 {
if fs := s.GetFullSnapshot(); fs != nil {
var size int64
@ -63,6 +66,7 @@ func (s *StreamHeader) FileSize() int64 {
return 0
}
// Stream is a stream of data that can be read from a snapshot.
type Stream struct {
headerLen int64
@ -71,6 +75,8 @@ type Stream struct {
totalFileSize int64
}
// NewIncrementalStream creates a new stream from a byte slice, presumably
// representing WAL data.
func NewIncrementalStream(data []byte) (*Stream, error) {
strHdr := NewStreamHeader()
strHdr.Payload = &StreamHeader_IncrementalSnapshot{
@ -92,6 +98,8 @@ func NewIncrementalStream(data []byte) (*Stream, error) {
}, nil
}
// NewFullStream creates a new stream from a SQLite file and 0 or more
// WAL files.
func NewFullStream(files ...string) (*Stream, error) {
if len(files) == 0 {
return nil, errors.New("no files provided")
@ -155,10 +163,13 @@ func NewFullStream(files ...string) (*Stream, error) {
}, nil
}
// Size returns the total number of bytes that will be read from the stream,
// if the stream is fully read.
func (s *Stream) Size() int64 {
return int64(strHeaderLenSize + int64(s.headerLen) + s.totalFileSize)
}
// Read reads from the stream.
func (s *Stream) Read(p []byte) (n int, err error) {
if s.readClosersIdx >= len(s.readClosers) {
return 0, io.EOF
@ -176,6 +187,7 @@ func (s *Stream) Read(p []byte) (n int, err error) {
return n, err
}
// Close closes the stream.
func (s *Stream) Close() error {
for _, r := range s.readClosers {
if err := r.Close(); err != nil {

Loading…
Cancel
Save