第二节 对外接口
1.Open()创建数据库接口
1. // Open creates and opens a database at the given path.
2. // If the file does not exist then it will be created automatically.
3. // Passing in nil options will cause Bolt to open the database with the default options.
4. // 创建数据库接口
5. func Open(path string, mode os.FileMode, options *Options) (*DB, error)
2.View()查询接口
1. // View executes a function within the context of a managed read-only transaction.
2. // Any error that is returned from the function is returned from the View() method.
3. //
4. // Attempting to manually rollback within the function will cause a panic.
5. func (db *DB) View(fn func(*Tx) error) error
3.Update()更新接口
1. // Update executes a function within the context of a read-write managed transaction.
2. // If no error is returned from the function then the transaction is committed.
3. // If an error is returned then the entire transaction is rolled back.
4. // Any error that is returned from the function or returned from the commit is
5. // returned from the Update() method.
6. //
7. // Attempting to manually commit or rollback within the function will cause a panic.
8. func (db *DB) Update(fn func(*Tx) error) error
4.Batch()批量更新接口
1. // Batch calls fn as part of a batch. It behaves similar to Update,
2. // except:
3. //
4. // 1. concurrent Batch calls can be combined into a single Bolt
5. // transaction.
6. //
7. // 2. the function passed to Batch may be called multiple times,
8. // regardless of whether it returns error or not.
9. //
10. // This means that Batch function side effects must be idempotent and
11. // take permanent effect only after a successful return is seen in
12. // caller.
13. //
14. // The maximum batch size and delay can be adjusted with DB.MaxBatchSize
15. // and DB.MaxBatchDelay, respectively.
16. //
17. // Batch is only useful when there are multiple goroutines calling it.
18. func (db *DB) Batch(fn func(*Tx) error) error
5.Begin()开启事务接口
1. // Begin starts a new transaction.
2. // Multiple read-only transactions can be used concurrently but only one
3. // write transaction can be used at a time. Starting multiple write transactions
4. // will cause the calls to block and be serialized until the current write
5. // transaction finishes.
6. //
7. // Transactions should not be dependent on one another. Opening a read
8. // transaction and a write transaction in the same goroutine can cause the
9. // writer to deadlock because the database periodically needs to re-mmap itself
10. // as it grows and it cannot do that while a read transaction is open.
11. //
12. // If a long running read transaction (for example, a snapshot transaction) is
13. // needed, you might want to set DB.InitialMmapSize to a large enough value
14. // to avoid potential blocking of write transaction.
15. //
16. // IMPORTANT: You must close read-only transactions after you are finished or
17. // else the database will not reclaim old pages.
18. func (db *DB) Begin(writable bool) (*Tx, error)
备注:Begin()的实现分析,参见事务4.3节内容,下面不在做分析。
下面我们将对上述接口做一一分析
