needle_map.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  29. type baseNeedleMapper struct {
  30. mapMetric
  31. indexFile *os.File
  32. indexFileAccessLock sync.Mutex
  33. }
  34. func (nm *baseNeedleMapper) IndexFileSize() uint64 {
  35. stat, err := nm.indexFile.Stat()
  36. if err == nil {
  37. return uint64(stat.Size())
  38. }
  39. return 0
  40. }
  41. func (nm *baseNeedleMapper) appendToIndexFile(key NeedleId, offset Offset, size uint32) error {
  42. bytes := needle_map.ToBytes(key, offset, size)
  43. nm.indexFileAccessLock.Lock()
  44. defer nm.indexFileAccessLock.Unlock()
  45. if _, err := nm.indexFile.Seek(0, 2); err != nil {
  46. return fmt.Errorf("cannot seek end of indexfile %s: %v",
  47. nm.indexFile.Name(), err)
  48. }
  49. _, err := nm.indexFile.Write(bytes)
  50. return err
  51. }