storage.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package leveldb
  2. import (
  3. "github.com/syndtr/goleveldb/leveldb/storage"
  4. "sync/atomic"
  5. )
  6. type iStorage struct {
  7. storage.Storage
  8. read uint64
  9. write uint64
  10. }
  11. func (c *iStorage) Open(fd storage.FileDesc) (storage.Reader, error) {
  12. r, err := c.Storage.Open(fd)
  13. return &iStorageReader{r, c}, err
  14. }
  15. func (c *iStorage) Create(fd storage.FileDesc) (storage.Writer, error) {
  16. w, err := c.Storage.Create(fd)
  17. return &iStorageWriter{w, c}, err
  18. }
  19. func (c *iStorage) reads() uint64 {
  20. return atomic.LoadUint64(&c.read)
  21. }
  22. func (c *iStorage) writes() uint64 {
  23. return atomic.LoadUint64(&c.write)
  24. }
  25. // newIStorage returns the given storage wrapped by iStorage.
  26. func newIStorage(s storage.Storage) *iStorage {
  27. return &iStorage{s, 0, 0}
  28. }
  29. type iStorageReader struct {
  30. storage.Reader
  31. c *iStorage
  32. }
  33. func (r *iStorageReader) Read(p []byte) (n int, err error) {
  34. n, err = r.Reader.Read(p)
  35. atomic.AddUint64(&r.c.read, uint64(n))
  36. return n, err
  37. }
  38. func (r *iStorageReader) ReadAt(p []byte, off int64) (n int, err error) {
  39. n, err = r.Reader.ReadAt(p, off)
  40. atomic.AddUint64(&r.c.read, uint64(n))
  41. return n, err
  42. }
  43. type iStorageWriter struct {
  44. storage.Writer
  45. c *iStorage
  46. }
  47. func (w *iStorageWriter) Write(p []byte) (n int, err error) {
  48. n, err = w.Writer.Write(p)
  49. atomic.AddUint64(&w.c.write, uint64(n))
  50. return n, err
  51. }