zhangzengfei
2021-11-12 d7485b186f72742ee0a1bf34e7fc818b53f20870
api.go
@@ -1,18 +1,20 @@
package boltcache
import "strconv"
//添加一条日志
func (ls *LogStore) ApplyLog(logData []byte) {
   lastLogIndex := ls.getLastLog()
   ls.applyCh <- &Log {
      Index: lastLogIndex+1,
      Data: logData,
   ls.applyCh <- &Log{
      Index: lastLogIndex + 1,
      Data:  logData,
   }
   ls.setLastLog(lastLogIndex + 1)
}
type LogCon struct {
   conf *confLog
   Log *Log
   Log  *Log
}
//获取缓存的数据
@@ -24,21 +26,21 @@
func (ls *LogStore) Get() *LogCon {
   idx, _ := ls.store.FirstIndex(confBucket)
   cLog := &confLog{}
   if err := ls.store.GetConfLog(idx, cLog);err ==nil {
   if err := ls.store.GetConfLog(idx, cLog); err == nil {
      u, _ := ls.store.FirstIndex(cLog.BucketName)
      log := &Log{}
      if err = ls.store.GetLog(cLog.BucketName, u, log);err ==nil {
      if err = ls.store.GetLog(cLog.BucketName, u, log); err == nil {
         log.Index = u
         cLog.Index = int(idx)
         return &LogCon{
            conf:cLog,
            Log: log,
            conf: cLog,
            Log:  log,
         }
      } else {
         if size,_ := ls.store.Size(confBucket);size >1 {
         if size, _ := ls.store.Size(confBucket); size > 1 {
            ls.Delete(&LogCon{
               conf: cLog,
               Log: nil,
               Log:  nil,
            })
         }
         ls.printLog("Get log err:", err)
@@ -49,6 +51,28 @@
   return nil
}
// 遍历数据, 输出原始数据
func (ls *LogStore) ForEach(f func(v *LogCon) error) {
   ls.store.ForEach(f)
}
func (ls *LogStore) Size() int {
   start, _ := ls.store.FirstIndex(confBucket)
   end, _ := ls.store.LastIndex(confBucket)
   totalSize := 0
   for ; start <= end; start++ {
      bucketName := bucketPre + strconv.Itoa(int(start))
      bucketSize, err := ls.store.Size(bucketName)
      if err == nil {
         totalSize = totalSize + bucketSize
      }
   }
   return totalSize
}
//提供给外层使用,删除日志
func (ls *LogStore) Delete(lc *LogCon) error {
   return ls.store.Delete(lc)
@@ -56,4 +80,4 @@
func (ls *LogStore) Close() error {
   return ls.store.Close()
}
}