第一节 DB结构

DB在boltdb是一个结构体,里面封装了很多属性,部分属性添加了中文注释,其他部分属性,大家可以直接看英文注释,感觉英文表述的很通俗易懂。

  1. // The largest step that can be taken when remapping the mmap.
  2. const maxMmapStep = 1 << 30 // 1GB
  3. // The data file format version.
  4. const version = 2
  5. // Represents a marker value to indicate that a file is a Bolt DB.
  6. const magic uint32 = 0xED0CDAED
  7. // IgnoreNoSync specifies whether the NoSync field of a DB is ignored when
  8. // syncing changes to a file. This is required as some operating systems,
  9. // such as OpenBSD, do not have a unified buffer cache (UBC) and writes
  10. // must be synchronized using the msync(2) syscall.
  11. const IgnoreNoSync = runtime.GOOS == "openbsd"
  12. // Default values if not set in a DB instance.
  13. const (
  14. DefaultMaxBatchSize int =s 1000
  15. DefaultMaxBatchDelay = 10 * time.Millisecond
  16. // 16k
  17. DefaultAllocSize = 16 * 1024 * 1024
  18. )
  19. // default page size for db is set to the OS page size.
  20. var defaultPageSize = os.Getpagesize()
  21. // DB represents a collection of buckets persisted to a file on disk.
  22. // All data access is performed through transactions which can be obtained through the DB.
  23. // All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
  24. type DB struct {
  25. // When enabled, the database will perform a Check() after every commit.
  26. // A panic is issued if the database is in an inconsistent state. This
  27. // flag has a large performance impact so it should only be used for
  28. // debugging purposes.
  29. StrictMode bool
  30. // Setting the NoSync flag will cause the database to skip fsync()
  31. // calls after each commit. This can be useful when bulk loading data
  32. // into a database and you can restart the bulk load in the event of
  33. // a system failure or database corruption. Do not set this flag for
  34. // normal use.
  35. //
  36. // If the package global IgnoreNoSync constant is true, this value is
  37. // ignored. See the comment on that constant for more details.
  38. //
  39. // THIS IS UNSAFE. PLEASE USE WITH CAUTION.
  40. NoSync bool
  41. // When true, skips the truncate call when growing the database.
  42. // Setting this to true is only safe on non-ext3/ext4 systems.
  43. // Skipping truncation avoids preallocation of hard drive space and
  44. // bypasses a truncate() and fsync() syscall on remapping.
  45. //
  46. // https://github.com/boltdb/bolt/issues/284
  47. NoGrowSync bool
  48. // If you want to read the entire database fast, you can set MmapFlag to
  49. // syscall.MAP_POPULATE on Linux 2.6.23+ for sequential read-ahead.
  50. MmapFlags int
  51. // MaxBatchSize is the maximum size of a batch. Default value is
  52. // copied from DefaultMaxBatchSize in Open.
  53. //
  54. // If <=0, disables batching.
  55. //
  56. // Do not change concurrently with calls to Batch.
  57. MaxBatchSize int
  58. // MaxBatchDelay is the maximum delay before a batch starts.
  59. // Default value is copied from DefaultMaxBatchDelay in Open.
  60. //
  61. // If <=0, effectively disables batching.
  62. //
  63. // Do not change concurrently with calls to Batch.
  64. MaxBatchDelay time.Duration
  65. // AllocSize is the amount of space allocated when the database
  66. // needs to create new pages. This is done to amortize the cost
  67. // of truncate() and fsync() when growing the data file.
  68. AllocSize int
  69. path string
  70. file *os.File // 真实存储数据的磁盘文件
  71. lockfile *os.File // windows only
  72. dataref []byte // mmap'ed readonly, write throws SEGV
  73. // 通过mmap映射进来的地址
  74. data *[maxMapSize]byte
  75. datasz int
  76. filesz int // current on disk file size
  77. // 元数据
  78. meta0 *meta
  79. meta1 *meta
  80. pageSize int
  81. opened bool
  82. rwtx *Tx // 写事务锁
  83. txs []*Tx // 读事务数组
  84. freelist *freelist // 空闲列表
  85. stats Stats
  86. pagePool sync.Pool
  87. batchMu sync.Mutex
  88. batch *batch
  89. rwlock sync.Mutex // Allows only one writer at a time.
  90. metalock sync.Mutex // Protects meta page access.
  91. mmaplock sync.RWMutex // Protects mmap access during remapping.
  92. statlock sync.RWMutex // Protects stats access.
  93. ops struct {
  94. writeAt func(b []byte, off int64) (n int, err error)
  95. }
  96. // Read only mode.
  97. // When true, Update() and Begin(true) return ErrDatabaseReadOnly immediately.
  98. readOnly bool
  99. }