Skip to content
Snippets Groups Projects
Select Git revision
  • 2e6ace1a4e040f9dc28aaa6807f7008b7aa22d05
  • master default protected
  • v1.4.5
  • v1.4.4
  • v1.4.3
  • v1.4.2
  • v1.4.1
  • v1.4.0
  • v1.3.10
  • v1.3.9
  • v1.3.8
  • v1.3.7
  • v1.3.6
  • v1.3.5
  • v1.3.4
  • v1.3.3
  • v1.3.2
  • v1.3.1
  • v1.3.0
  • v1.2.9
  • v1.2.8
  • v1.2.7
22 results

page.go

Blame
  • 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))
    	}
    }