| | |
| | | } |
| | | |
| | | // 遍历数据, 输出原始数据 |
| | | func (ls *LogStore) ForEach(f func(v []byte) error) { |
| | | func (ls *LogStore) ForEach(f func(v *LogCon) error) { |
| | | ls.store.ForEach(f) |
| | | } |
| | | |
| | |
| | | import ( |
| | | "errors" |
| | | "github.com/boltdb/bolt" |
| | | "strconv" |
| | | "time" |
| | | ) |
| | | |
| | |
| | | return tx.Commit() |
| | | } |
| | | |
| | | func (b *BoltStore) ForEach(f func(v []byte) error) { |
| | | func (b *BoltStore) ForEach(f func(lc *LogCon) error) { |
| | | tx, err := b.conn.Begin(true) |
| | | if err != nil { |
| | | return |
| | |
| | | end, _ := b.LastIndex(confBucket) |
| | | |
| | | for ; start <= end; start++ { |
| | | bucketName := bucketPre + strconv.Itoa(int(start)) |
| | | cLog := &confLog{} |
| | | err := b.GetConfLog(start, cLog) |
| | | if err != nil { |
| | | return |
| | | } |
| | | |
| | | bucketName := cLog.BucketName |
| | | bucket := tx.Bucket([]byte(bucketName)) |
| | | if bucket == nil { |
| | | return |
| | |
| | | bucket.ForEach(func(k, v []byte) error { |
| | | log := &Log{} |
| | | err := decodeMsgPack(v, log) |
| | | f(log.Data) |
| | | if err == nil { |
| | | f(&LogCon{cLog, log}) |
| | | } |
| | | |
| | | return err |
| | | }) |
| | | } |
| | |
| | | package boltcache |
| | | |
| | | import ( |
| | | "strconv" |
| | | "testing" |
| | | "time" |
| | | ) |
| | |
| | | |
| | | //go consume(ls) |
| | | // |
| | | for i := 0; i < 100; i++ { |
| | | ls.ApplyLog([]byte("hello world " + strconv.Itoa(i))) |
| | | time.Sleep(time.Millisecond * 100) |
| | | } |
| | | //for i := 0; i < 10; i++ { |
| | | // ls.ApplyLog([]byte("hello world " + strconv.Itoa(i))) |
| | | // time.Sleep(time.Millisecond * 100) |
| | | //} |
| | | |
| | | ls.printLog("Size = ", ls.Size()) |
| | | |
| | | ls.ForEach(func(v []byte) error { |
| | | ls.printLog("val=:", string(v)) |
| | | var logs []*LogCon |
| | | ls.ForEach(func(log *LogCon) error { |
| | | ls.printLog("val=:", string(log.GetData())) |
| | | |
| | | logs = append(logs, log) |
| | | time.Sleep(time.Millisecond * 100) |
| | | |
| | | return nil |
| | | }) |
| | | |
| | | consume(ls) |
| | | for _, log := range logs { |
| | | ls.Delete(log) |
| | | } |
| | | //consume(ls) |
| | | } |
| | | |
| | | func consume(ls *LogStore) { |