aboutsummaryrefslogtreecommitdiff
path: root/mediaapi/storage
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2020-08-21 10:42:08 +0100
committerGitHub <noreply@github.com>2020-08-21 10:42:08 +0100
commit9d53351dc20283103bf2eec6b92831033d06c5a8 (patch)
tree653cf0ddca3f777bcdba188187fb78fe39ae2b02 /mediaapi/storage
parent5aaf32bbed4d704d5a22ad7dff79f7a68002a213 (diff)
Component-wide TransactionWriters (#1290)
* Offset updates take place using TransactionWriter * Refactor TransactionWriter in current state server * Refactor TransactionWriter in federation sender * Refactor TransactionWriter in key server * Refactor TransactionWriter in media API * Refactor TransactionWriter in server key API * Refactor TransactionWriter in sync API * Refactor TransactionWriter in user API * Fix deadlocking Sync API tests * Un-deadlock device database * Fix appservice API * Rename TransactionWriters to Writers * Move writers up a layer in sync API * Document sqlutil.Writer interface * Add note to Writer documentation
Diffstat (limited to 'mediaapi/storage')
-rw-r--r--mediaapi/storage/sqlite3/media_repository_table.go6
-rw-r--r--mediaapi/storage/sqlite3/sql.go8
-rw-r--r--mediaapi/storage/sqlite3/storage.go7
-rw-r--r--mediaapi/storage/sqlite3/thumbnail_table.go34
4 files changed, 34 insertions, 21 deletions
diff --git a/mediaapi/storage/sqlite3/media_repository_table.go b/mediaapi/storage/sqlite3/media_repository_table.go
index ff6ddf3d..dcc1b41e 100644
--- a/mediaapi/storage/sqlite3/media_repository_table.go
+++ b/mediaapi/storage/sqlite3/media_repository_table.go
@@ -62,14 +62,14 @@ SELECT content_type, file_size_bytes, creation_ts, upload_name, base64hash, user
type mediaStatements struct {
db *sql.DB
- writer sqlutil.TransactionWriter
+ writer sqlutil.Writer
insertMediaStmt *sql.Stmt
selectMediaStmt *sql.Stmt
}
-func (s *mediaStatements) prepare(db *sql.DB) (err error) {
+func (s *mediaStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
s.db = db
- s.writer = sqlutil.NewTransactionWriter()
+ s.writer = writer
_, err = db.Exec(mediaSchema)
if err != nil {
diff --git a/mediaapi/storage/sqlite3/sql.go b/mediaapi/storage/sqlite3/sql.go
index 9cd78b8e..245bd40c 100644
--- a/mediaapi/storage/sqlite3/sql.go
+++ b/mediaapi/storage/sqlite3/sql.go
@@ -17,6 +17,8 @@ package sqlite3
import (
"database/sql"
+
+ "github.com/matrix-org/dendrite/internal/sqlutil"
)
type statements struct {
@@ -24,11 +26,11 @@ type statements struct {
thumbnail thumbnailStatements
}
-func (s *statements) prepare(db *sql.DB) (err error) {
- if err = s.media.prepare(db); err != nil {
+func (s *statements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
+ if err = s.media.prepare(db, writer); err != nil {
return
}
- if err = s.thumbnail.prepare(db); err != nil {
+ if err = s.thumbnail.prepare(db, writer); err != nil {
return
}
diff --git a/mediaapi/storage/sqlite3/storage.go b/mediaapi/storage/sqlite3/storage.go
index a1e7fec7..d5c3031e 100644
--- a/mediaapi/storage/sqlite3/storage.go
+++ b/mediaapi/storage/sqlite3/storage.go
@@ -31,16 +31,19 @@ import (
type Database struct {
statements statements
db *sql.DB
+ writer sqlutil.Writer
}
// Open opens a postgres database.
func Open(dbProperties *config.DatabaseOptions) (*Database, error) {
- var d Database
+ d := Database{
+ writer: sqlutil.NewExclusiveWriter(),
+ }
var err error
if d.db, err = sqlutil.Open(dbProperties); err != nil {
return nil, err
}
- if err = d.statements.prepare(d.db); err != nil {
+ if err = d.statements.prepare(d.db, d.writer); err != nil {
return nil, err
}
return &d, nil
diff --git a/mediaapi/storage/sqlite3/thumbnail_table.go b/mediaapi/storage/sqlite3/thumbnail_table.go
index 432a1590..06b056b6 100644
--- a/mediaapi/storage/sqlite3/thumbnail_table.go
+++ b/mediaapi/storage/sqlite3/thumbnail_table.go
@@ -21,6 +21,7 @@ import (
"time"
"github.com/matrix-org/dendrite/internal"
+ "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/mediaapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
@@ -57,16 +58,20 @@ SELECT content_type, file_size_bytes, creation_ts, width, height, resize_method
`
type thumbnailStatements struct {
+ db *sql.DB
+ writer sqlutil.Writer
insertThumbnailStmt *sql.Stmt
selectThumbnailStmt *sql.Stmt
selectThumbnailsStmt *sql.Stmt
}
-func (s *thumbnailStatements) prepare(db *sql.DB) (err error) {
+func (s *thumbnailStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
_, err = db.Exec(thumbnailSchema)
if err != nil {
return
}
+ s.db = db
+ s.writer = writer
return statementList{
{&s.insertThumbnailStmt, insertThumbnailSQL},
@@ -79,18 +84,21 @@ func (s *thumbnailStatements) insertThumbnail(
ctx context.Context, thumbnailMetadata *types.ThumbnailMetadata,
) error {
thumbnailMetadata.MediaMetadata.CreationTimestamp = types.UnixMs(time.Now().UnixNano() / 1000000)
- _, err := s.insertThumbnailStmt.ExecContext(
- ctx,
- thumbnailMetadata.MediaMetadata.MediaID,
- thumbnailMetadata.MediaMetadata.Origin,
- thumbnailMetadata.MediaMetadata.ContentType,
- thumbnailMetadata.MediaMetadata.FileSizeBytes,
- thumbnailMetadata.MediaMetadata.CreationTimestamp,
- thumbnailMetadata.ThumbnailSize.Width,
- thumbnailMetadata.ThumbnailSize.Height,
- thumbnailMetadata.ThumbnailSize.ResizeMethod,
- )
- return err
+ return s.writer.Do(s.db, nil, func(txn *sql.Tx) error {
+ stmt := sqlutil.TxStmt(txn, s.insertThumbnailStmt)
+ _, err := stmt.ExecContext(
+ ctx,
+ thumbnailMetadata.MediaMetadata.MediaID,
+ thumbnailMetadata.MediaMetadata.Origin,
+ thumbnailMetadata.MediaMetadata.ContentType,
+ thumbnailMetadata.MediaMetadata.FileSizeBytes,
+ thumbnailMetadata.MediaMetadata.CreationTimestamp,
+ thumbnailMetadata.ThumbnailSize.Width,
+ thumbnailMetadata.ThumbnailSize.Height,
+ thumbnailMetadata.ThumbnailSize.ResizeMethod,
+ )
+ return err
+ })
}
func (s *thumbnailStatements) selectThumbnail(