Skip to content
Snippets Groups Projects
Commit 81b56c39 authored by Travis Ralston's avatar Travis Ralston
Browse files

Use a cache for faster lookups to datastores

parent 386b968f
No related branches found
No related tags found
No related merge requests found
...@@ -12,9 +12,7 @@ import ( ...@@ -12,9 +12,7 @@ import (
) )
func GetOrCreateDatastore(ctx context.Context, log *logrus.Entry, basePath string) (*types.Datastore, error) { func GetOrCreateDatastore(ctx context.Context, log *logrus.Entry, basePath string) (*types.Datastore, error) {
logrus.Info("Init DS lookup")
mediaService := GetDatabase().GetMediaStore(ctx, log) mediaService := GetDatabase().GetMediaStore(ctx, log)
return getOrCreateDatastoreWithMediaService(mediaService, basePath) return getOrCreateDatastoreWithMediaService(mediaService, basePath)
} }
......
...@@ -22,6 +22,9 @@ const insertDatastore = "INSERT INTO datastores (datastore_id, ds_type, uri) VAL ...@@ -22,6 +22,9 @@ const insertDatastore = "INSERT INTO datastores (datastore_id, ds_type, uri) VAL
const selectMediaWithoutDatastore = "SELECT origin, media_id, upload_name, content_type, user_id, sha256_hash, size_bytes, datastore_id, location, creation_ts, quarantined FROM media WHERE datastore_id IS NULL OR datastore_id = '';" const selectMediaWithoutDatastore = "SELECT origin, media_id, upload_name, content_type, user_id, sha256_hash, size_bytes, datastore_id, location, creation_ts, quarantined FROM media WHERE datastore_id IS NULL OR datastore_id = '';"
const updateMediaDatastoreAndLocation = "UPDATE media SET location = $4, datastore_id = $3 WHERE origin = $1 AND media_id = $2;" const updateMediaDatastoreAndLocation = "UPDATE media SET location = $4, datastore_id = $3 WHERE origin = $1 AND media_id = $2;"
var dsCacheByPath = make(map[string]*types.Datastore)
var dsCacheById = make(map[string]*types.Datastore)
type mediaStoreStatements struct { type mediaStoreStatements struct {
selectMedia *sql.Stmt selectMedia *sql.Stmt
selectMediaByHash *sql.Stmt selectMediaByHash *sql.Stmt
...@@ -244,13 +247,30 @@ func (s *MediaStore) UpdateDatastoreAndLocation(media *types.Media) (error) { ...@@ -244,13 +247,30 @@ func (s *MediaStore) UpdateDatastoreAndLocation(media *types.Media) (error) {
} }
func (s *MediaStore) GetDatastore(id string) (*types.Datastore, error) { func (s *MediaStore) GetDatastore(id string) (*types.Datastore, error) {
if v, ok := dsCacheById[id]; ok {
return &types.Datastore{
DatastoreId: v.DatastoreId,
Type: v.Type,
Uri: v.Uri,
}, nil
}
d := &types.Datastore{} d := &types.Datastore{}
err := s.statements.selectDatastore.QueryRowContext(s.ctx, id).Scan( err := s.statements.selectDatastore.QueryRowContext(s.ctx, id).Scan(
&d.DatastoreId, &d.DatastoreId,
&d.Type, &d.Type,
&d.Uri, &d.Uri,
) )
return d, err if err != nil {
dsCacheById[d.DatastoreId] = d
dsCacheByPath[d.Uri] = d
}
return &types.Datastore{
DatastoreId: d.DatastoreId,
Type: d.Type,
Uri: d.Uri,
}, err
} }
func (s *MediaStore) InsertDatastore(datastore *types.Datastore) (error) { func (s *MediaStore) InsertDatastore(datastore *types.Datastore) (error) {
...@@ -260,17 +280,43 @@ func (s *MediaStore) InsertDatastore(datastore *types.Datastore) (error) { ...@@ -260,17 +280,43 @@ func (s *MediaStore) InsertDatastore(datastore *types.Datastore) (error) {
datastore.Type, datastore.Type,
datastore.Uri, datastore.Uri,
) )
if err != nil {
d := &types.Datastore{
DatastoreId: datastore.DatastoreId,
Type: datastore.Type,
Uri: datastore.Uri,
}
dsCacheById[d.DatastoreId] = d
dsCacheByPath[d.Uri] = d
}
return err return err
} }
func (s *MediaStore) GetDatastoreByUri(uri string) (*types.Datastore, error) { func (s *MediaStore) GetDatastoreByUri(uri string) (*types.Datastore, error) {
if v, ok := dsCacheByPath[uri]; ok {
return &types.Datastore{
DatastoreId: v.DatastoreId,
Type: v.Type,
Uri: v.Uri,
}, nil
}
d := &types.Datastore{} d := &types.Datastore{}
err := s.statements.selectDatastoreByUri.QueryRowContext(s.ctx, uri).Scan( err := s.statements.selectDatastoreByUri.QueryRowContext(s.ctx, uri).Scan(
&d.DatastoreId, &d.DatastoreId,
&d.Type, &d.Type,
&d.Uri, &d.Uri,
) )
return d, err if err != nil {
dsCacheById[d.DatastoreId] = d
dsCacheByPath[d.Uri] = d
}
return &types.Datastore{
DatastoreId: d.DatastoreId,
Type: d.Type,
Uri: d.Uri,
}, err
} }
func (s *MediaStore) GetAllWithoutDatastore() ([]*types.Media, error) { func (s *MediaStore) GetAllWithoutDatastore() ([]*types.Media, error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment