Commit 7fba56df authored by Kirill Smelkov's avatar Kirill Smelkov

X zodb: NewDB += option to set live cache control on created connections

parent dc35035c
// Copyright (C) 2018-2019 Nexedi SA and Contributors. // Copyright (C) 2018-2021 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -105,7 +105,7 @@ func TestBTree(t *testing.T) { ...@@ -105,7 +105,7 @@ func TestBTree(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
db := zodb.NewDB(stor) db := zodb.NewDB(stor, &zodb.DBOptions{})
defer func() { defer func() {
err := db.Close(); X(err) err := db.Close(); X(err)
}() }()
......
// Copyright (C) 2018-2020 Nexedi SA and Contributors. // Copyright (C) 2018-2021 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -180,7 +180,7 @@ const ( ...@@ -180,7 +180,7 @@ const (
// newConnection creates new Connection associated with db. // newConnection creates new Connection associated with db.
func newConnection(db *DB, at Tid) *Connection { func newConnection(db *DB, at Tid) *Connection {
return &Connection{ conn := &Connection{
db: db, db: db,
at: at, at: at,
cache: LiveCache{ cache: LiveCache{
...@@ -188,6 +188,10 @@ func newConnection(db *DB, at Tid) *Connection { ...@@ -188,6 +188,10 @@ func newConnection(db *DB, at Tid) *Connection {
objtab: make(map[Oid]*weak.Ref), objtab: make(map[Oid]*weak.Ref),
}, },
} }
if cc := db.opt.CacheControl; cc != nil {
conn.cache.SetControl(cc)
}
return conn
} }
// DB returns database handle under which the connection was opened. // DB returns database handle under which the connection was opened.
......
// Copyright (C) 2018-2020 Nexedi SA and Contributors. // Copyright (C) 2018-2021 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -48,6 +48,8 @@ type DB struct { ...@@ -48,6 +48,8 @@ type DB struct {
stor IStorage stor IStorage
watchq chan Event // we are watching .stor via here watchq chan Event // we are watching .stor via here
opt *DBOptions
down chan struct{} // ready when DB is no longer operational down chan struct{} // ready when DB is no longer operational
downOnce sync.Once // shutdown may be due to both Close and IO error in watcher downOnce sync.Once // shutdown may be due to both Close and IO error in watcher
downErr error // reason for shutdown downErr error // reason for shutdown
...@@ -121,15 +123,23 @@ type DB struct { ...@@ -121,15 +123,23 @@ type DB struct {
// (so it is not duplicated many times for many DB case) // (so it is not duplicated many times for many DB case)
} }
// DBOptions describes options to NewDB.
type DBOptions struct {
// CacheControl, if !nil, is set as default live cache control for
// newly created connections.
CacheControl LiveCacheControl
}
// NewDB creates new database handle. // NewDB creates new database handle.
// //
// Created database handle must be closed when no longer needed. // Created database handle must be closed when no longer needed.
func NewDB(stor IStorage) *DB { func NewDB(stor IStorage, opt *DBOptions) *DB {
// XXX db options? // copy opts in case caller will change them later
opt_ := *opt
db := &DB{ db := &DB{
stor: stor, stor: stor,
watchq: make(chan Event), watchq: make(chan Event),
opt: &opt_,
down: make(chan struct{}), down: make(chan struct{}),
hwait: make(map[hwaiter]struct{}), hwait: make(map[hwaiter]struct{}),
......
// Copyright (C) 2018-2020 Nexedi SA and Contributors. // Copyright (C) 2018-2021 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -276,7 +276,7 @@ func (t *tDB) Reopen() { ...@@ -276,7 +276,7 @@ func (t *tDB) Reopen() {
ReadOnly: true, ReadOnly: true,
NoCache: !t.rawcache, NoCache: !t.rawcache,
}); X(err) }); X(err)
db := NewDB(stor) db := NewDB(stor, &DBOptions{})
t.stor = stor t.stor = stor
t.db = db t.db = db
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment