Categorygithub.com/gdbu/snapshotter
modulepackage
0.5.10
Repository: https://github.com/gdbu/snapshotter.git
Documentation: pkg.go.dev

# README

Snapshotter

Snapshotter is a database snapshot utility which snapshots and truncates for the configured values.

Usage

package main

import (
	"fmt"
	"log"
	"os"
	"path"
	"time"

	"github.com/gdbu/snapshotter"
	"github.com/gdbu/snapshotter/backends"
	"github.com/gdbu/snapshotter/frontends"

	"github.com/boltdb/bolt"
)

const (
	frontendDir = "./frontend"
	backendDir  = "./backend"
)

func main() {
	var (
		s   *snapshotter.Snapshotter
		db  *bolt.DB
		err error
	)

	// Create bolt database
	if db, err = createBoltDB(); err != nil {
		out.Errorf("error during Init: %v", err)
        return
	}

	// Populate bolt database
	if err = populateValues(db); err != nil {
		out.Errorf("error during Init: %v", err)
        return
	}

	// Initialize bolt front-end
	fe := frontends.NewBolt(db)

	// Initialize a new file backend
	be := backends.NewFilebackend(backendDir)

	// Create new configuration
	cfg := snapshotter.NewConfig("data", "db")
	// Interval represents our snapshot interval value. This is how often we will snapshot data. We will set
	// our configuration so that the snapshotter takes a snapshot once a second.
	cfg.Interval = snapshotter.Second
	// Truncate represents our snapshot truncate value. This determines which time value our snapshots will
	// truncate to. We will set our configuration so that the snapshotter truncates our time value to the minute.
	cfg.Truncate = snapshotter.Minute

	if s, err = snapshotter.New(fe, be, cfg); err != nil {
		out.Errorf("error during Init: %v", err)
        return
	}
	defer s.Close()

	// Give snapshotter time to take some snapshots
	time.Sleep(5 * time.Second)

	var latest string
	// Get latest key
	if latest, err = s.LatestKey(); err != nil {
		out.Errorf("error during Init: %v", err)
        return
	}

	fmt.Printf("Our latest key was: %s\n", latest)
}

func createBoltDB() (db *bolt.DB, err error) {
	// Ensure our frontend test directory has been created
	if err = os.MkdirAll(frontendDir, 0744); err != nil {
		out.Errorf("error during Init: %v", err)
        return
	}

	// Open a bolt database within the frontend test directory with the name of "bolt.db"
	return bolt.Open(path.Join(frontendDir, "bolt.db"), 0744, nil)
}

func populateValues(db *bolt.DB) (err error) {
	bucketKey := []byte("main")
	key := []byte("greeting")
	value := []byte("hello world")

	// Update bolt database
	err = db.Update(func(txn *bolt.Tx) (err error) {
		var bkt *bolt.Bucket
		// Create bucket with the key equaling our bucketKey
		if bkt, err = txn.CreateBucketIfNotExists(bucketKey); err != nil {
			return
		}

		// Put our value to the database
		return bkt.Put(key, value)
	})

	return
}

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

New returns a new instance of snapshotter.
NewConfig will return a new default Config with the given name and extension.

# Constants

Day represents a day.
ErrInvalidExtension is returned when an invalid extension is set.
ErrInvalidInterval is returned when an invalid interval duration is set.
ErrInvalidKey is returned when an invalid key is attempted to be parsed.
ErrInvalidName is returned when an invalid name is set.
ErrInvalidTruncate is returned when an invalid truncate duration is set.
ErrIsLatestKey is returned when a latest key is attempted to be parsed.
Hour represents an hour.
Minute represents a minute.
Month represents a month.
Second represents a second.
Year represents a year.

# Structs

Config are the basic configuration settings for snapshotter.
Snapshotter will manage a snapshotting service.

# Interfaces

Backend is the interface for values which can be stored and retrieved.
Frontend is the interface for values which can be used for snapshots.