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.

85 lines
2.4 KiB

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