MCPcopy
hub / github.com/benbjohnson/litestream / openNewDatabase

Method openNewDatabase

vfs.go:1110–1163  ·  view source on GitHub ↗

openNewDatabase initializes the VFSFile for a brand new database with no existing data. This is called when write mode is enabled and no LTX files exist yet.

()

Source from the content-addressed store, hash-verified

1108// openNewDatabase initializes the VFSFile for a brand new database with no existing data.
1109// This is called when write mode is enabled and no LTX files exist yet.
1110func (f *VFSFile) openNewDatabase() error {
1111 f.logger.Debug("initializing new database")
1112
1113 // Use default page size for new databases
1114 f.pageSize = DefaultPageSize
1115
1116 // Initialize page cache
1117 cacheEntries := f.CacheSize / int(f.pageSize)
1118 if cacheEntries < 1 {
1119 cacheEntries = 1
1120 }
1121 cache, err := lru.New[uint32, []byte](cacheEntries)
1122 if err != nil {
1123 return fmt.Errorf("create page cache: %w", err)
1124 }
1125 f.cache = cache
1126
1127 // Initialize empty index - no pages exist yet
1128 f.index = make(map[uint32]ltx.PageIndexElem)
1129 f.pending = make(map[uint32]ltx.PageIndexElem)
1130 f.pos = ltx.Pos{TXID: 0}
1131 f.commit = 0
1132
1133 // Initialize write support for new database
1134 f.expectedTXID = 0
1135 f.pendingTXID = 1
1136 f.logger.Debug("write support enabled for new database", "expectedTXID", f.expectedTXID, "pendingTXID", f.pendingTXID)
1137
1138 // Initialize write buffer file for durability
1139 if err := f.initWriteBuffer(); err != nil {
1140 f.logger.Warn("failed to initialize write buffer", "error", err)
1141 }
1142
1143 // Start monitoring for new LTX files (in case another writer creates the database)
1144 f.wg.Add(1)
1145 go func() { defer f.wg.Done(); f.monitorReplicaClient(f.ctx) }()
1146
1147 // Start periodic sync goroutine
1148 if f.syncInterval > 0 {
1149 f.syncTicker = time.NewTicker(f.syncInterval)
1150 f.syncStop = make(chan struct{})
1151 stopCh := f.syncStop
1152 tickerCh := f.syncTicker.C
1153 f.wg.Add(1)
1154 go func() { defer f.wg.Done(); f.syncLoop(stopCh, tickerCh) }()
1155 }
1156
1157 // Start compaction monitors if enabled
1158 if f.compactor != nil && f.vfs != nil {
1159 f.startCompactionMonitors()
1160 }
1161
1162 return nil
1163}
1164
1165// SetTargetTime rebuilds the page index to view the database at a specific time.
1166func (f *VFSFile) SetTargetTime(ctx context.Context, timestamp time.Time) error {

Callers 1

OpenMethod · 0.95

Calls 4

initWriteBufferMethod · 0.95
monitorReplicaClientMethod · 0.95
syncLoopMethod · 0.95

Tested by

no test coverage detected