needle_map.go 1.6 KB

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