syncstorage

package
v0.0.0-...-eac43e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2017 License: MPL-2.0 Imports: 13 Imported by: 3

Documentation

Index

Constants

View Source
const MaxTimestamp = 4070822400000

2099 ... somebody else's problem by then (I hope)

View Source
const SCHEMA_0 = `` /* 1573-byte string literal not displayed */
View Source
const SCHEMA_1 = `` /* 240-byte string literal not displayed */

Issue #72

Variables

View Source
var (
	ErrNotFound       = errors.New("Not Found")
	ErrNotImplemented = errors.New("Not Implemented")
	ErrNothingToDo    = errors.New("Nothing to do")

	ErrInvalidBSOId          = errors.New("Invalid BSO Id")
	ErrInvalidCollectionId   = errors.New("Invalid Collection Id")
	ErrInvalidCollectionName = errors.New("Invalid Collection Name")
	ErrInvalidPayload        = errors.New("Invalid Payload")
	ErrInvalidSortIndex      = errors.New("Invalid Sort Index")
	ErrInvalidTTL            = errors.New("Invalid TTL")

	ErrInvalidLimit  = errors.New("Invalid LIMIT")
	ErrInvalidOffset = errors.New("Invalid OFFSET")
	ErrInvalidNewer  = errors.New("Invalid NEWER than")
)
View Source
var (
	ErrBatchNotFound = errors.New("Batch Not Found")
)

Functions

func BSOIdOk

func BSOIdOk(bId string) bool

func CollectionNameOk

func CollectionNameOk(cName string) bool

func Int

func Int(u int) *int

func LimitOk

func LimitOk(limit int) bool

LimitOk checks limit is within the correct range. Acceptable values are -1 to max int. A limit of 0 would return 0 records

func ModifiedToString

func ModifiedToString(modified int) string

ModifiedToString turns the output of Now(), an integer of milliseconds since the epoch to the sync 1.5's seconds w/ two decimals format

func NewerOk

func NewerOk(newer int) bool

func Now

func Now() int

Now returns the number of millisecond since the unix epoch

func OffsetOk

func OffsetOk(offset int) bool

func SortIndexOk

func SortIndexOk(sortIndex int) bool

SortIndexOK validates a sortIndex int, the only rule is that it is 9 digits ... :\

func String

func String(s string) *string

func TTLOk

func TTLOk(ttl int) bool

func ValidateBSOId

func ValidateBSOId(ids ...string) bool

ValidateBSOIds checks if all provided Is are 12 characters long and only contain url safe base64 characters

Types

type BSO

type BSO struct {
	Id        string
	Modified  int
	Payload   string
	SortIndex int
	TTL       int
}

ref: https://docs.services.mozilla.com/storage/apis-1.5.html#basic-storage-object

func (BSO) MarshalJSON

func (b BSO) MarshalJSON() ([]byte, error)

MarshalJSON builds a custom json blob since there is no way good way of turning the Modified int (in milliseconds) into seconds with two decimal places which the api defines as the correct format. meh.

type BatchRecord

type BatchRecord struct {
	Id           int
	CollectionId int
	BSOS         string
	Modified     int
}

type CollectionInfo

type CollectionInfo struct {
	Name     string
	BSOs     int
	Storage  int
	Modified int
}

type Config

type Config struct {
	CacheSize int
}

type DB

type DB struct {
	sync.RWMutex

	// sqlite database path
	Path string
	// contains filtered or unexported fields
}

func NewDB

func NewDB(path string, conf *Config) (*DB, error)

func (*DB) BatchAppend

func (d *DB) BatchAppend(id, cId int, data string) (err error)

BatchAppend adds data to the BSOS column for a batch id

func (*DB) BatchCreate

func (d *DB) BatchCreate(cId int, data string) (int, error)

BatchCreate creates a new batch

func (*DB) BatchExists

func (d *DB) BatchExists(id, cId int) (bool, error)

BatchExists checks if a batch exists without loading all the data from disk

func (*DB) BatchLoad

func (d *DB) BatchLoad(id, cId int) (*BatchRecord, error)

func (*DB) BatchPurge

func (d *DB) BatchPurge(TTL int) (int, error)

func (*DB) BatchRemove

func (d *DB) BatchRemove(id int) error

func (*DB) Close

func (d *DB) Close()

func (*DB) CreateCollection

func (d *DB) CreateCollection(name string) (cId int, err error)

func (*DB) DeleteBSO

func (d *DB) DeleteBSO(cId int, bId string) (int, error)

DeleteBSO deletes a single BSO and returns the modified timestamp for the collection

func (*DB) DeleteBSOs

func (d *DB) DeleteBSOs(cId int, bIds ...string) (modified int, err error)

DeleteBSOs deletes multiple BSO. It returns the modified timestamp for the collection on success

func (*DB) DeleteCollection

func (d *DB) DeleteCollection(cId int) (int, error)

func (*DB) DeleteEverything

func (d *DB) DeleteEverything() (err error)

DeleteEverything will delete all BSOs, record when everything was deleted and vacuum to free up disk pages.

func (*DB) GetBSO

func (d *DB) GetBSO(cId int, bId string) (b *BSO, err error)

func (*DB) GetBSOModified

func (d *DB) GetBSOModified(cId int, bId string) (modified int, err error)

func (*DB) GetBSOs

func (d *DB) GetBSOs(
	cId int,
	ids []string,
	older int,
	newer int,

	sort SortType,
	limit int,
	offset int) (r *GetResults, err error)

func (*DB) GetCollectionId

func (d *DB) GetCollectionId(name string) (id int, err error)

func (*DB) GetCollectionModified

func (d *DB) GetCollectionModified(cId int) (modified int, err error)

func (*DB) GetKey

func (d *DB) GetKey(key string) (string, error)

GetKey returns a previous key in the database

func (*DB) InfoCollectionCounts

func (d *DB) InfoCollectionCounts() (map[string]int, error)

func (*DB) InfoCollectionUsage

func (d *DB) InfoCollectionUsage() (map[string]int, error)

func (*DB) InfoCollections

func (d *DB) InfoCollections() (map[string]int, error)

InfoCollections create a map of collection names to last modified times

func (*DB) InfoQuota

func (d *DB) InfoQuota() (used, quota int, err error)

func (*DB) LastModified

func (d *DB) LastModified() (int, error)

LastModified returns the top level last modified timestamp

func (*DB) Open

func (d *DB) Open() (err error)

func (*DB) OpenWithConfig

func (d *DB) OpenWithConfig(conf *Config) (err error)

func (*DB) Optimize

func (d *DB) Optimize(thresholdPercent int) (ItHappened bool, err error)

Optimize recovers disk space by removing empty db pages if the number of free pages makes up more than `threshold` percent of the total pages

func (*DB) PostBSOs

func (d *DB) PostBSOs(cId int, input PostBSOInput) (*PostResults, error)

func (*DB) PurgeExpired

func (d *DB) PurgeExpired() (removed int, err error)

PurgeExpired removes all BSOs that have expired out

func (*DB) PutBSO

func (d *DB) PutBSO(cId int, bId string, payload *string, sortIndex *int, ttl *int) (modified int, err error)

PutBSO creates or updates a BSO

func (*DB) SetKey

func (d *DB) SetKey(key, value string) error

SetKey inserts or replaces a key with the value

func (*DB) TouchCollection

func (d *DB) TouchCollection(cId, modified int) (err error)

func (*DB) Usage

func (d *DB) Usage() (stats *DBPageStats, err error)

func (*DB) Vacuum

func (d *DB) Vacuum() (err error)

Vacuum recovers free disk pages and reduces fragmentation of the data on disk. This could take a long time depending on the size of the database

type DBPageStats

type DBPageStats struct {
	Size  int
	Total int
	Free  int
}

func (*DBPageStats) FreePercent

func (s *DBPageStats) FreePercent() int

FreePercent calculates how much of total space is used up by free pages (empty of data)

type GetResults

type GetResults struct {
	BSOs   []*BSO
	More   bool
	Offset int
}

GetResults holds search results for BSOs, this is what getBSOs() returns

func (*GetResults) String

func (g *GetResults) String() string

type PostBSOInput

type PostBSOInput []*PutBSOInput

type PostResults

type PostResults struct {
	Modified int
	Success  []string
	Failed   map[string][]string
}

PostBSOs takes a set of BSO and performs an Insert or Update on each of them.

func NewPostResults

func NewPostResults(modified int) *PostResults

func (*PostResults) AddFailure

func (p *PostResults) AddFailure(bId string, reasons ...string)

func (*PostResults) AddSuccess

func (p *PostResults) AddSuccess(bId ...string)

type PutBSOInput

type PutBSOInput struct {
	Id        string  `json:"id"`
	Payload   *string `json:"payload,omitempty"`
	TTL       *int    `json:"ttl,omitempty"`
	SortIndex *int    `json:"sortindex,omitempty"`
}

func NewPutBSOInput

func NewPutBSOInput(id string, payload *string, sortIndex, ttl *int) *PutBSOInput

type SortType

type SortType int
const (
	SORT_NONE SortType = iota
	SORT_NEWEST
	SORT_OLDEST
	SORT_INDEX

	// The default TTL is to never expire. Use 100 years
	// which should be enough (in milliseconds)
	DEFAULT_BSO_TTL = 100 * 365 * 24 * 60 * 60 * 1000

	STORAGE_LAST_MODIFIED = "Storage Last Modified"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL