Select Git revision
page.go 4.23 KiB
package store
import (
"encoding/binary"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/syndtr/goleveldb/leveldb"
"os"
)
const pageSize = 64
// pageDB returns leveldb of page variant and creates it as required.
func (s *Store) pageDB(variant string) *leveldb.DB {
if s.pageldb[variant] != nil {
return s.pageldb[variant]
} else {
if db, err := leveldb.OpenFile(s.PageVariantPath(variant), nil); err != nil {
s.fatalClose(fmt.Sprintf("Error opening leveldb for page variant %s, %s", variant, err))
} else {
s.pageldb[variant] = db
if _, err = db.Get([]byte("\000"), nil); err != nil {
log.Infof("Page variant %s created.", variant)
s.pageSetTotalCount(variant, 0)
}
return db
}
}
return nil
}
// pageDBDestroy destroys leveldb of page variant.
func (s *Store) pageDBDestroy(variant string) {
if err := s.pageDB(variant).Close(); err != nil {
s.fatalClose(fmt.Sprintf("Error closing leveldb for page variant %s, %s", variant, err))
} else {
delete(s.pageldb, variant)
}
if err := os.RemoveAll(s.PageVariantPath(variant)); err != nil {
s.fatalClose(fmt.Sprintf("Error destroying page variant %s, %s", variant, err))
}
log.Infof("Page variant %s destroyed.", variant)
}
// pageGetTotalCount gets total count of a page variant.
func (s *Store) pageGetTotalCount(variant string) uint64 {
db := s.pageDB(variant)
if payload, err := db.Get([]byte("\000"), nil); err != nil {
s.fatalClose(fmt.Sprintf("Error getting page variant %s total count, %s", variant, err))
} else {
return binary.LittleEndian.Uint64(payload)
}
return 0
}
// pageSetTotalCount sets total count of a page variant.
func (s *Store) pageSetTotalCount(variant string, value uint64) {
db := s.pageDB(variant)
payload := make([]byte, 8)
binary.LittleEndian.PutUint64(payload, value)
if err := db.Put([]byte("\000"), payload, nil); err != nil {
s.fatalClose(fmt.Sprintf("Error setting page variant %s total count, %s", variant, err))
}
}