第一节 DB结构

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

2. // The largest step that can be taken when remapping the mmap.
3. const maxMmapStep = 1 << 30 // 1GB

5. // The data file format version.
6. const version = 2

8. // Represents a marker value to indicate that a file is a Bolt DB.
9. const magic uint32 = 0xED0CDAED

11. // IgnoreNoSync specifies whether the NoSync field of a DB is ignored when
12. // syncing changes to a file.  This is required as some operating systems,
13. // such as OpenBSD, do not have a unified buffer cache (UBC) and writes
14. // must be synchronized using the msync(2) syscall.
15. const IgnoreNoSync = runtime.GOOS == "openbsd"

17. // Default values if not set in a DB instance.
18. const (
19. DefaultMaxBatchSize  int =s 1000
20. DefaultMaxBatchDelay     = 10 * time.Millisecond
21. // 16k
22. DefaultAllocSize = 16 * 1024 * 1024
23. )

25. // default page size for db is set to the OS page size.
26. var defaultPageSize = os.Getpagesize()

28. // DB represents a collection of buckets persisted to a file on disk.
29. // All data access is performed through transactions which can be obtained through the DB.
30. // All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
31. type DB struct {
32. // When enabled, the database will perform a Check() after every commit.
33. // A panic is issued if the database is in an inconsistent state. This
34. // flag has a large performance impact so it should only be used for
35. // debugging purposes.
36. StrictMode bool

38. // Setting the NoSync flag will cause the database to skip fsync()
39. // calls after each commit. This can be useful when bulk loading data
40. // into a database and you can restart the bulk load in the event of
41. // a system failure or database corruption. Do not set this flag for
42. // normal use.
43. //
44. // If the package global IgnoreNoSync constant is true, this value is
45. // ignored.  See the comment on that constant for more details.
46. //
47. // THIS IS UNSAFE. PLEASE USE WITH CAUTION.
48. NoSync bool

50. // When true, skips the truncate call when growing the database.
51. // Setting this to true is only safe on non-ext3/ext4 systems.
52. // Skipping truncation avoids preallocation of hard drive space and
53. // bypasses a truncate() and fsync() syscall on remapping.
54. //
55. // https://github.com/boltdb/bolt/issues/284
56. NoGrowSync bool

58. // If you want to read the entire database fast, you can set MmapFlag to
59. // syscall.MAP_POPULATE on Linux 2.6.23+ for sequential read-ahead.
60. MmapFlags int

62. // MaxBatchSize is the maximum size of a batch. Default value is
63. // copied from DefaultMaxBatchSize in Open.
64. //
65. // If <=0, disables batching.
66. //
67. // Do not change concurrently with calls to Batch.
68. MaxBatchSize int

70. // MaxBatchDelay is the maximum delay before a batch starts.
71. // Default value is copied from DefaultMaxBatchDelay in Open.
72. //
73. // If <=0, effectively disables batching.
74. //
75. // Do not change concurrently with calls to Batch.
76. MaxBatchDelay time.Duration

78. // AllocSize is the amount of space allocated when the database
79. // needs to create new pages. This is done to amortize the cost
80. // of truncate() and fsync() when growing the data file.
81. AllocSize int

83. path     string
84. file     *os.File  // 真实存储数据的磁盘文件
85. lockfile *os.File // windows only
86. dataref  []byte   // mmap'ed readonly, write throws SEGV
87. // 通过mmap映射进来的地址
88. data   *[maxMapSize]byte
89. datasz int
90. filesz int // current on disk file size
91. //  元数据
92. meta0 *meta
93. meta1 *meta

95. pageSize int
96. opened   bool
97. rwtx     *Tx  // 写事务锁
98. txs      []*Tx  // 读事务数组
99. freelist *freelist // 空闲列表
100. stats    Stats

102. pagePool sync.Pool

104. batchMu sync.Mutex
105. batch   *batch

107. rwlock   sync.Mutex   // Allows only one writer at a time.
108. metalock sync.Mutex   // Protects meta page access.
109. mmaplock sync.RWMutex // Protects mmap access during remapping.
110. statlock sync.RWMutex // Protects stats access.

112. ops struct {
113. writeAt func(b []byte, off int64) (n int, err error)
114. }

116. // Read only mode.
117. // When true, Update() and Begin(true) return ErrDatabaseReadOnly immediately.
118. readOnly bool
119. }