StorageOpts func(*storage.ResourcePiecesOpts)
}
-// A convenience function that creates a connection pool, resource provider, and a pieces storage
-// ClientImpl and returns them all with a Close attached.
-func NewPiecesStorage(opts NewPiecesStorageOpts) (_ storage.ClientImplCloser, err error) {
- conns, err := NewPool(opts.NewPoolOpts)
- if err != nil {
- return
- }
- if opts.PageSize == 0 {
- opts.PageSize = 1 << 14
- }
- err = initPoolDatabase(conns, opts.InitDbOpts)
- if err != nil {
- conns.Close()
- return
- }
- if opts.SetJournalMode == "" && !opts.Memory {
- opts.SetJournalMode = "wal"
- }
- err = initPoolConns(context.TODO(), conns, opts.InitConnOpts)
- if err != nil {
- conns.Close()
- return
- }
- provOpts := ProviderOpts{
- BatchWrites: conns.NumConns() > 1,
- }
- if f := opts.ProvOpts; f != nil {
- f(&provOpts)
- }
- prov, err := NewProvider(conns, provOpts)
- if err != nil {
- conns.Close()
- return
- }
- var (
- journalMode string
- )
- withPoolConn(conns, func(c conn) {
- err = sqlitex.Exec(c, "pragma journal_mode", func(stmt *sqlite.Stmt) error {
- journalMode = stmt.ColumnText(0)
- return nil
- })
- })
- if err != nil {
- err = fmt.Errorf("getting journal mode: %w", err)
- prov.Close()
- return
- }
- if journalMode == "" {
- err = errors.New("didn't get journal mode")
- prov.Close()
- return
- }
- storageOpts := storage.ResourcePiecesOpts{
- NoSizedPuts: journalMode != "wal" || conns.NumConns() == 1,
- }
- if f := opts.StorageOpts; f != nil {
- f(&storageOpts)
- }
- store := storage.NewResourcePiecesOpts(prov, storageOpts)
- return struct {
- storage.ClientImpl
- io.Closer
- }{
- store,
- prov,
- }, nil
-}
-
type NewPoolOpts struct {
NewConnOpts
InitConnOpts
})
}
})
- b.Run("ResourcePieces", func(b *testing.B) {
- for _, batchWrites := range []bool{false, true} {
- b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
- var opts NewPiecesStorageOpts
- opts.Path = filepath.Join(b.TempDir(), "storage.db")
- //b.Logf("storage db path: %q", dbPath)
- opts.Capacity = capacity
- opts.Memory = memory
- opts.ProvOpts = func(opts *ProviderOpts) {
- opts.BatchWrites = batchWrites
- }
- ci, err := NewPiecesStorage(opts)
- c.Assert(err, qt.IsNil)
- defer ci.Close()
- runBench(b, ci)
- })
- }
- })
})
}
}
testClientTransferSmallCache(t, false, -1)
}
-func sqliteClientStorageFactory(optsMaker func(dataDir string) sqliteStorage.NewPiecesStorageOpts) storageFactory {
- return func(dataDir string) storage.ClientImplCloser {
- opts := optsMaker(dataDir)
- //log.Printf("opening sqlite db: %#v", opts)
- ret, err := sqliteStorage.NewPiecesStorage(opts)
- if err != nil {
- panic(err)
- }
- return ret
- }
-}
-
type leecherStorageTestCase struct {
name string
f storageFactory
gomaxprocs int
}
-func sqliteLeecherStorageTestCase(numConns int) leecherStorageTestCase {
- return leecherStorageTestCase{
- fmt.Sprintf("SqliteFile,NumConns=%v", numConns),
- sqliteClientStorageFactory(func(dataDir string) (opts sqliteStorage.NewPiecesStorageOpts) {
- opts.Path = filepath.Join(dataDir, "sqlite.db")
- opts.NumConns = numConns
- return
- }),
- numConns,
- }
-}
-
func TestClientTransferVarious(t *testing.T) {
// Leecher storage
for _, ls := range []leecherStorageTestCase{
}
return cl
}, 0},
- sqliteLeecherStorageTestCase(1),
- sqliteLeecherStorageTestCase(2),
- // This should use a number of connections equal to the number of CPUs
- sqliteLeecherStorageTestCase(0),
- {"SqliteMemory", sqliteClientStorageFactory(func(dataDir string) (opts sqliteStorage.NewPiecesStorageOpts) {
- opts.Memory = true
- return
- }), 0},
} {
t.Run(fmt.Sprintf("LeecherStorage=%s", ls.name), func(t *testing.T) {
// Seeder storage