mirror of
https://github.com/syncthing/syncthing.git
synced 2025-12-23 22:18:14 -05:00
This changes the files table to use normalisation for the names and
versions. The idea is that these are often common between all remote
devices, and repeating an integer is more efficient than repeating a
long string. A new benchmark bears this out; for a database with 100k
files shared between 31 devices, with some worst case assumption on
version vector size, the database is reduced in size by 50% and the test
finishes quicker:
Current:
db_bench_test.go:322: Total size: 6263.70 MiB
--- PASS: TestBenchmarkSizeManyFilesRemotes (1084.89s)
New:
db_bench_test.go:326: Total size: 3049.95 MiB
--- PASS: TestBenchmarkSizeManyFilesRemotes (776.97s)
The other benchmarks end up about the same within the margin of
variability, with one possible exception being that RemoteNeed seems to
be a little slower on average:
old files/s new files/s
Update/n=RemoteNeed/size=1000-8 5.051k 4.654k
Update/n=RemoteNeed/size=2000-8 5.201k 4.384k
Update/n=RemoteNeed/size=4000-8 4.943k 4.242k
Update/n=RemoteNeed/size=8000-8 5.099k 3.527k
Update/n=RemoteNeed/size=16000-8 3.686k 3.847k
Update/n=RemoteNeed/size=30000-8 4.456k 3.482k
I'm not sure why, possibly that query can be optimised anyhow.
Signed-off-by: Jakob Borg <jakob@kastelo.net>
111 lines
2.7 KiB
Go
111 lines
2.7 KiB
Go
// Copyright (C) 2025 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package sqlite
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
)
|
|
|
|
type folderDB struct {
|
|
folderID string
|
|
*baseDB
|
|
|
|
localDeviceIdx int64
|
|
deleteRetention time.Duration
|
|
}
|
|
|
|
func openFolderDB(folder, path string, deleteRetention time.Duration) (*folderDB, error) {
|
|
pragmas := []string{
|
|
"journal_mode = WAL",
|
|
"optimize = 0x10002",
|
|
"auto_vacuum = INCREMENTAL",
|
|
fmt.Sprintf("application_id = %d", applicationIDFolder),
|
|
}
|
|
schemas := []string{
|
|
"sql/schema/common/*",
|
|
"sql/schema/folder/*",
|
|
}
|
|
migrations := []string{
|
|
"sql/migrations/common/*",
|
|
"sql/migrations/folder/*",
|
|
}
|
|
|
|
base, err := openBase(path, maxDBConns, pragmas, schemas, migrations)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fdb := &folderDB{
|
|
folderID: folder,
|
|
baseDB: base,
|
|
deleteRetention: deleteRetention,
|
|
}
|
|
|
|
_ = fdb.PutKV("folderID", []byte(folder))
|
|
|
|
// Touch device IDs that should always exist and have a low index
|
|
// numbers, and will never change
|
|
fdb.localDeviceIdx, _ = fdb.deviceIdxLocked(protocol.LocalDeviceID)
|
|
fdb.tplInput["LocalDeviceIdx"] = fdb.localDeviceIdx
|
|
|
|
return fdb, nil
|
|
}
|
|
|
|
// Open the database with options suitable for the migration inserts. This
|
|
// is not a safe mode of operation for normal processing, use only for bulk
|
|
// inserts with a close afterwards.
|
|
func openFolderDBForMigration(folder, path string, deleteRetention time.Duration) (*folderDB, error) {
|
|
pragmas := []string{
|
|
"journal_mode = OFF",
|
|
"foreign_keys = 0",
|
|
"synchronous = 0",
|
|
"locking_mode = EXCLUSIVE",
|
|
fmt.Sprintf("application_id = %d", applicationIDFolder),
|
|
}
|
|
schemas := []string{
|
|
"sql/schema/common/*",
|
|
"sql/schema/folder/*",
|
|
}
|
|
|
|
base, err := openBase(path, 1, pragmas, schemas, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fdb := &folderDB{
|
|
folderID: folder,
|
|
baseDB: base,
|
|
deleteRetention: deleteRetention,
|
|
}
|
|
|
|
// Touch device IDs that should always exist and have a low index
|
|
// numbers, and will never change
|
|
fdb.localDeviceIdx, _ = fdb.deviceIdxLocked(protocol.LocalDeviceID)
|
|
fdb.tplInput["LocalDeviceIdx"] = fdb.localDeviceIdx
|
|
|
|
return fdb, nil
|
|
}
|
|
|
|
func (s *folderDB) deviceIdxLocked(deviceID protocol.DeviceID) (int64, error) {
|
|
devStr := deviceID.String()
|
|
var idx int64
|
|
if err := s.stmt(`
|
|
INSERT INTO devices(device_id)
|
|
VALUES (?)
|
|
ON CONFLICT(device_id) DO UPDATE
|
|
SET device_id = excluded.device_id
|
|
RETURNING idx
|
|
`).Get(&idx, devStr); err != nil {
|
|
return 0, wrap(err)
|
|
}
|
|
|
|
return idx, nil
|
|
}
|