summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandomChars <random@chars.jp>2021-10-17 12:20:25 +0900
committerRandomChars <random@chars.jp>2021-10-17 12:20:25 +0900
commit0bec98a43a63fc91cdc20a0024afab7ea5268ed2 (patch)
treed3e3fea930b5f61e37a742eb4fc8d56696e92243
parent2b855c42c33e91820f14720f699c92017826afe2 (diff)
not call pageDB during creation, check for zero when reducing total countv1.4.4
-rw-r--r--store/page.go36
1 files changed, 18 insertions, 18 deletions
diff --git a/store/page.go b/store/page.go
index 8a1a623..a247e70 100644
--- a/store/page.go
+++ b/store/page.go
@@ -12,27 +12,21 @@ const PageSize = 64
// pageDB returns leveldb of page variant and creates it as required.
func (s *Store) pageDB(variant string) *leveldb.DB {
- l := s.getLock("pageDB_get")
- l.RLock()
- defer l.RUnlock()
+ mutex := s.getLock("pageDB_get")
+ mutex.Lock()
+ defer mutex.Unlock()
- if s.pageldb[variant] != nil {
- return s.pageldb[variant]
+ if ldb := s.pageldb[variant]; ldb != nil {
+ return ldb
} else {
- l.RUnlock()
- l.Lock()
if db, err := leveldb.OpenFile(s.PageVariantPath(variant), nil); err != nil {
- l.Unlock()
- l.RLock()
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.pageSetTotalCountNoDestroy(variant, 0)
+ s.pageSetTotalCountNoDestroy(variant, 0, db)
}
- l.Unlock()
- l.RLock()
return db
}
}
@@ -68,9 +62,7 @@ func (s *Store) pageGetTotalCount(variant string) uint64 {
}
// pageSetTotalCountNoDestroy sets total count of a page variant.
-func (s *Store) pageSetTotalCountNoDestroy(variant string, value uint64) {
- db := s.pageDB(variant)
-
+func (s *Store) pageSetTotalCountNoDestroy(variant string, value uint64, db *leveldb.DB) {
payload := make([]byte, 8)
binary.LittleEndian.PutUint64(payload, value)
@@ -85,7 +77,7 @@ func (s *Store) pageSetTotalCount(variant string, value uint64) {
s.pageDBDestroy(variant)
return
}
- s.pageSetTotalCountNoDestroy(variant, value)
+ s.pageSetTotalCountNoDestroy(variant, value, s.pageDB(variant))
}
// pageAdvanceTotalCount advances total count of a page variant.
@@ -95,12 +87,20 @@ func (s *Store) pageAdvanceTotalCount(variant string) {
// pageReduceTotalCount reduces total count of a page variant.
func (s *Store) pageReduceTotalCount(variant string) {
- s.pageSetTotalCount(variant, s.pageGetTotalCount(variant)-1)
+ if total := s.pageGetTotalCount(variant); total == 0 {
+ return
+ } else {
+ s.pageSetTotalCount(variant, total-1)
+ }
}
// PageTotal returns total amount of pages.
func (s *Store) PageTotal(variant string) int {
- return (int(s.pageGetTotalCount(variant)) / PageSize) + 1
+ totalCount := int(s.pageGetTotalCount(variant))
+ if totalCount == 0 {
+ return 0
+ }
+ return (totalCount / PageSize) + 1
}
// Page returns all entries in a page.