You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

6 years ago
6 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "sync"
  7. "github.com/joeslay/seaweedfs/weed/storage/needle_map"
  8. . "github.com/joeslay/seaweedfs/weed/storage/types"
  9. )
  10. type NeedleMapType int
  11. const (
  12. NeedleMapInMemory NeedleMapType = iota
  13. NeedleMapLevelDb // small memory footprint, 4MB total, 1 write buffer, 3 block buffer
  14. NeedleMapLevelDbMedium // medium memory footprint, 8MB total, 3 write buffer, 5 block buffer
  15. NeedleMapLevelDbLarge // large memory footprint, 12MB total, 4write buffer, 8 block buffer
  16. )
  17. type NeedleMapper interface {
  18. Put(key NeedleId, offset Offset, size uint32) error
  19. Get(key NeedleId) (element *needle_map.NeedleValue, ok bool)
  20. Delete(key NeedleId, offset Offset) error
  21. Close()
  22. Destroy() error
  23. ContentSize() uint64
  24. DeletedSize() uint64
  25. FileCount() int
  26. DeletedCount() int
  27. MaxFileKey() NeedleId
  28. IndexFileSize() uint64
  29. IndexFileContent() ([]byte, error)
  30. IndexFileName() string
  31. }
  32. type baseNeedleMapper struct {
  33. mapMetric
  34. indexFile *os.File
  35. indexFileAccessLock sync.Mutex
  36. }
  37. func (nm *baseNeedleMapper) IndexFileSize() uint64 {
  38. stat, err := nm.indexFile.Stat()
  39. if err == nil {
  40. return uint64(stat.Size())
  41. }
  42. return 0
  43. }
  44. func (nm *baseNeedleMapper) IndexFileName() string {
  45. return nm.indexFile.Name()
  46. }
  47. func (nm *baseNeedleMapper) appendToIndexFile(key NeedleId, offset Offset, size uint32) error {
  48. bytes := needle_map.ToBytes(key, offset, size)
  49. nm.indexFileAccessLock.Lock()
  50. defer nm.indexFileAccessLock.Unlock()
  51. if _, err := nm.indexFile.Seek(0, 2); err != nil {
  52. return fmt.Errorf("cannot seek end of indexfile %s: %v",
  53. nm.indexFile.Name(), err)
  54. }
  55. _, err := nm.indexFile.Write(bytes)
  56. return err
  57. }
  58. func (nm *baseNeedleMapper) IndexFileContent() ([]byte, error) {
  59. nm.indexFileAccessLock.Lock()
  60. defer nm.indexFileAccessLock.Unlock()
  61. return ioutil.ReadFile(nm.indexFile.Name())
  62. }