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

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

Loading…
Cancel
Save