Skip to content

Commit a2cbfec

Browse files
committed
Added local file cache for postgres data
1 parent 60b6bbe commit a2cbfec

File tree

8 files changed

+76
-14
lines changed

8 files changed

+76
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ openrun.toml
88
logs/
99
.DS_Store
1010
run/
11+
app_src/
1112
config/
1213
internal/server/appspecs/
1314
internal/server/appspecs/dummy/README

internal/metadata/filestore.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ type FileStore struct {
3939
db *sql.DB
4040
initTx types.Transaction // This is the transaction for the initial setup of the app, before it is committed to the database.
4141
// After app is committed to database, this is not used, auto-commit transactions are used for reads
42+
fileCache *FileCache
4243
}
4344

44-
func NewFileStore(appId types.AppId, version int, metadata *Metadata, tx types.Transaction) *FileStore {
45-
return &FileStore{appId: appId, version: version, metadata: metadata, db: metadata.db, initTx: tx}
45+
func NewFileStore(appId types.AppId, version int, metadata *Metadata, tx types.Transaction) (*FileStore, error) {
46+
var fileCache *FileCache
47+
var err error
48+
if metadata.dbType != system.DB_TYPE_SQLITE {
49+
fileCache, err = InitFileCache(metadata.Logger, metadata.config)
50+
if err != nil {
51+
return nil, fmt.Errorf("error initializing file cache: %w", err)
52+
}
53+
}
54+
return &FileStore{appId: appId, version: version, metadata: metadata, db: metadata.db, initTx: tx, fileCache: fileCache}, nil
4655
}
4756

4857
func (f *FileStore) IncrementAppVersion(ctx context.Context, tx types.Transaction, metadata *types.AppMetadata) error {
@@ -184,6 +193,17 @@ func (f *FileStore) GetFileBySha(sha string) ([]byte, string, error) {
184193
}
185194

186195
func (f *FileStore) GetFileByShaTx(ctx context.Context, tx types.Transaction, sha string) ([]byte, string, error) {
196+
if f.fileCache != nil {
197+
content, compressionType, err := f.fileCache.GetCachedFile(ctx, sha)
198+
if err == nil {
199+
f.metadata.Trace().Msgf("Got file from cache: %s", sha)
200+
return content, compressionType, nil
201+
}
202+
if err != nil && err != sql.ErrNoRows {
203+
return nil, "", fmt.Errorf("error getting cached file: %w", err)
204+
}
205+
}
206+
187207
stmt, err := tx.PrepareContext(ctx, system.RebindQuery(f.metadata.dbType, "SELECT compression_type, content FROM files where sha = ?"))
188208
if err != nil {
189209
return nil, "", fmt.Errorf("error preparing statement: %w", err)
@@ -197,6 +217,14 @@ func (f *FileStore) GetFileByShaTx(ctx context.Context, tx types.Transaction, sh
197217
return nil, "", fmt.Errorf("error querying file table: %w", err)
198218
}
199219

220+
if f.fileCache != nil {
221+
f.metadata.Trace().Msgf("Adding file to cache: %s", sha)
222+
err = f.fileCache.AddCache(ctx, sha, compressionType, content)
223+
if err != nil {
224+
return nil, "", fmt.Errorf("error adding file to cache: %w", err)
225+
}
226+
}
227+
200228
return content, compressionType, nil
201229
}
202230

internal/server/app_apis.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ func (s *Server) setupApp(appEntry *types.AppEntry, tx types.Transaction) (*app.
325325
var sourceFS *appfs.SourceFs
326326
if !appEntry.IsDev {
327327
// Prod mode, use DB as source
328-
fileStore := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
328+
fileStore, err := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
329+
if err != nil {
330+
return nil, err
331+
}
329332
dbFs, err := metadata.NewDbFs(s.Logger, fileStore, *appEntry.Metadata.SpecFiles)
330333
if err != nil {
331334
return nil, err
@@ -795,7 +798,10 @@ func (s *Server) loadSourceFromGit(ctx context.Context, tx types.Transaction, ap
795798

796799
s.Info().Msgf("Loading app sources from %s", checkoutFolder)
797800
// Walk the local directory and add all files to the database
798-
fileStore := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
801+
fileStore, err := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
802+
if err != nil {
803+
return err
804+
}
799805
highestVersion, err := fileStore.GetHighestVersion(ctx, tx, appEntry.Id)
800806
if err != nil {
801807
return err
@@ -820,7 +826,10 @@ func (s *Server) loadSourceFromDisk(ctx context.Context, tx types.Transaction, a
820826
appEntry.Settings.GitAuthName = ""
821827
appEntry.Metadata.VersionMetadata.GitMessage = ""
822828

823-
fileStore := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
829+
fileStore, err := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
830+
if err != nil {
831+
return err
832+
}
824833
highestVersion, err := fileStore.GetHighestVersion(ctx, tx, appEntry.Id)
825834
if err != nil {
826835
return fmt.Errorf("error getting highest version: %w", err)

internal/server/app_updates.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,11 @@ func (s *Server) StagedUpdateAppsTx(ctx context.Context, tx types.Transaction, a
264264
return nil, nil, nil, err
265265
}
266266

267-
stagingFileStore := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
268-
err := stagingFileStore.IncrementAppVersion(ctx, tx, &appEntry.Metadata)
267+
stagingFileStore, err := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
268+
if err != nil {
269+
return nil, nil, nil, fmt.Errorf("error initializing staging file store: %w", err)
270+
}
271+
err = stagingFileStore.IncrementAppVersion(ctx, tx, &appEntry.Metadata)
269272
if err != nil {
270273
return nil, nil, nil, fmt.Errorf("error incrementing app version: %w", err)
271274
}
@@ -368,8 +371,14 @@ func (s *Server) PromoteApps(ctx context.Context, appPathGlob string, dryRun boo
368371
}
369372

370373
func (s *Server) promoteApp(ctx context.Context, tx types.Transaction, stagingApp *types.AppEntry, prodApp *types.AppEntry) error {
371-
stagingFileStore := metadata.NewFileStore(stagingApp.Id, stagingApp.Metadata.VersionMetadata.Version, s.db, tx)
372-
prodFileStore := metadata.NewFileStore(prodApp.Id, prodApp.Metadata.VersionMetadata.Version, s.db, tx)
374+
stagingFileStore, err := metadata.NewFileStore(stagingApp.Id, stagingApp.Metadata.VersionMetadata.Version, s.db, tx)
375+
if err != nil {
376+
return fmt.Errorf("error initializing staging file store: %w", err)
377+
}
378+
prodFileStore, err := metadata.NewFileStore(prodApp.Id, prodApp.Metadata.VersionMetadata.Version, s.db, tx)
379+
if err != nil {
380+
return fmt.Errorf("error initializing prod file store: %w", err)
381+
}
373382
prevVersion := prodApp.Metadata.VersionMetadata.Version
374383
newVersion := stagingApp.Metadata.VersionMetadata.Version
375384

internal/server/apply.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,12 @@ func (s *Server) applyAppUpdate(ctx context.Context, tx types.Transaction, appPa
600600
promoteApp = len(reloadResult.PromoteResults) > 0
601601
} else if updated {
602602
// No reload, increment version and promote (if enabled)
603-
stagingFileStore := metadata.NewFileStore(liveApp.Id, liveApp.Metadata.VersionMetadata.Version, s.db, tx)
604-
err := stagingFileStore.IncrementAppVersion(ctx, tx, &liveApp.Metadata)
603+
stagingFileStore, err := metadata.NewFileStore(liveApp.Id, liveApp.Metadata.VersionMetadata.Version, s.db, tx)
604+
if err != nil {
605+
return nil, fmt.Errorf("error initializing staging file store: %w", err)
606+
}
607+
608+
err = stagingFileStore.IncrementAppVersion(ctx, tx, &liveApp.Metadata)
605609
if err != nil {
606610
return nil, fmt.Errorf("error incrementing app version: %w", err)
607611
}

internal/server/version_apis.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ func (s *Server) VersionList(ctx context.Context, mainAppPath string) (*types.Ap
3434
return nil, fmt.Errorf("version commands not supported for dev app")
3535
}
3636

37-
fileStore := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
37+
fileStore, err := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
38+
if err != nil {
39+
return nil, err
40+
}
3841
versions, err := fileStore.GetAppVersions(ctx, tx)
3942
if err != nil {
4043
return nil, err
@@ -80,7 +83,10 @@ func (s *Server) VersionFiles(ctx context.Context, mainAppPath, version string)
8083
}
8184
}
8285

83-
fileStore := metadata.NewFileStore(appEntry.Id, versionInt, s.db, tx)
86+
fileStore, err := metadata.NewFileStore(appEntry.Id, versionInt, s.db, tx)
87+
if err != nil {
88+
return nil, err
89+
}
8490
files, err := fileStore.GetAppFiles(ctx, tx)
8591
if err != nil {
8692
return nil, err
@@ -110,7 +116,10 @@ func (s *Server) VersionSwitch(ctx context.Context, mainAppPath string, dryRun b
110116
return nil, fmt.Errorf("version commands not supported for dev app")
111117
}
112118
var versionInt int
113-
fileStore := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
119+
fileStore, err := metadata.NewFileStore(appEntry.Id, appEntry.Metadata.VersionMetadata.Version, s.db, tx)
120+
if err != nil {
121+
return nil, err
122+
}
114123

115124
versionLower := strings.ToLower(version)
116125
switch versionLower {

internal/system/openrun.default.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ access_logging = true
5454
db_connection = "sqlite:$OPENRUN_HOME/metadata/clace_metadata.db"
5555
auto_upgrade = true
5656
audit_db_connection = "sqlite:$OPENRUN_HOME/metadata/clace_audit.db"
57+
file_cache_connection = "sqlite:$OPENRUN_HOME/metadata/file_cache.db"
5758

5859
[system]
5960
tailwindcss_command = "tailwindcss"

internal/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ type MetadataConfig struct {
198198
AutoUpgrade bool `toml:"auto_upgrade"`
199199
AuditDBConnection string `toml:"audit_db_connection"`
200200
IgnoreHigherVersion bool `toml:"ignore_higher_version"` // If true, ignore higher version of the metadata schema
201+
FileCacheConnection string `toml:"file_cache_connection"` // The connection string for the file cache database
201202
}
202203

203204
// LogConfig is the configuration for the Logger

0 commit comments

Comments
 (0)