cache.v

1.5 KB · raw

1//! The file cache: a machine-local shortcut so `status` and `save` can skip2//! files whose size, mtime and ctime are unchanged. It caches the working3//! tree, never history, and is safe to drop at any time.45module storage67pub struct CacheRow {8pub:9	path     string10	size     i6411	mtime_ns i6412	ctime_ns i6413	hash     string14}1516pub fn (s &Store) load_cache() !map[string]CacheRow {17	mut rows := map[string]CacheRow{}18	mut stmt := s.db.prepare('SELECT path, size, mtime_ns, ctime_ns, hash FROM file_cache')!19	defer {20		stmt.finalize()21	}22	for stmt.step()! {23		row := CacheRow{24			path:     stmt.text(0)25			size:     stmt.int(1)26			mtime_ns: stmt.int(2)27			ctime_ns: stmt.int(3)28			hash:     stmt.text(4)29		}30		rows[row.path] = row31	}32	return rows33}3435pub fn (s &Store) save_cache(row CacheRow) ! {36	mut stmt := s.db.prepare('INSERT INTO file_cache (path, size, mtime_ns, ctime_ns, hash)37		VALUES (?, ?, ?, ?, ?)38		ON CONFLICT(path) DO UPDATE SET size = excluded.size, mtime_ns = excluded.mtime_ns,39			ctime_ns = excluded.ctime_ns, hash = excluded.hash')!40	defer {41		stmt.finalize()42	}43	stmt.bind_text(1, row.path)!44	stmt.bind_int(2, row.size)!45	stmt.bind_int(3, row.mtime_ns)!46	stmt.bind_int(4, row.ctime_ns)!47	stmt.bind_text(5, row.hash)!48	stmt.step()!49}5051pub fn (s &Store) drop_cache() ! {52	s.db.exec('DELETE FROM file_cache')!53}5455pub fn (s &Store) cache_count() !i64 {56	mut stmt := s.db.prepare('SELECT COUNT(*) FROM file_cache')!57	defer {58		stmt.finalize()59	}60	if !stmt.step()! {61		return 062	}63	return stmt.int(0)64}