123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package leveldb
- import (
- "github.com/syndtr/goleveldb/leveldb/storage"
- "sync/atomic"
- )
- type iStorage struct {
- storage.Storage
- read uint64
- write uint64
- }
- func (c *iStorage) Open(fd storage.FileDesc) (storage.Reader, error) {
- r, err := c.Storage.Open(fd)
- return &iStorageReader{r, c}, err
- }
- func (c *iStorage) Create(fd storage.FileDesc) (storage.Writer, error) {
- w, err := c.Storage.Create(fd)
- return &iStorageWriter{w, c}, err
- }
- func (c *iStorage) reads() uint64 {
- return atomic.LoadUint64(&c.read)
- }
- func (c *iStorage) writes() uint64 {
- return atomic.LoadUint64(&c.write)
- }
- // newIStorage returns the given storage wrapped by iStorage.
- func newIStorage(s storage.Storage) *iStorage {
- return &iStorage{s, 0, 0}
- }
- type iStorageReader struct {
- storage.Reader
- c *iStorage
- }
- func (r *iStorageReader) Read(p []byte) (n int, err error) {
- n, err = r.Reader.Read(p)
- atomic.AddUint64(&r.c.read, uint64(n))
- return n, err
- }
- func (r *iStorageReader) ReadAt(p []byte, off int64) (n int, err error) {
- n, err = r.Reader.ReadAt(p, off)
- atomic.AddUint64(&r.c.read, uint64(n))
- return n, err
- }
- type iStorageWriter struct {
- storage.Writer
- c *iStorage
- }
- func (w *iStorageWriter) Write(p []byte) (n int, err error) {
- n, err = w.Writer.Write(p)
- atomic.AddUint64(&w.c.write, uint64(n))
- return n, err
- }
|