-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GODT-1720, GODT-1722): Implement Database Wrapper
Implements database as discussed in the RFC. It makes distinctions between read and write operations. Currently, due to the use of SQLite we only support only one concurrent writer. This is enforced by a RWLock around the `Read` and `Write` functions. Updates to rest of the codebase will follow in future patches.
- Loading branch information
1 parent
d17a5f2
commit cc387f0
Showing
5 changed files
with
96 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package backend | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"sync" | ||
|
||
"entgo.io/ent/dialect" | ||
"github.com/ProtonMail/gluon/internal/backend/ent" | ||
) | ||
|
||
type DB struct { | ||
db *ent.Client | ||
lock sync.RWMutex | ||
} | ||
|
||
func (d *DB) Init(ctx context.Context) error { | ||
d.lock.Lock() | ||
defer d.lock.Unlock() | ||
|
||
return d.db.Schema.Create(ctx) | ||
} | ||
|
||
func (d *DB) Read(ctx context.Context, fn func(context.Context, *ent.Client) error) error { | ||
d.lock.RLock() | ||
defer d.lock.Unlock() | ||
|
||
return fn(ctx, d.db) | ||
} | ||
|
||
func (d *DB) Write(ctx context.Context, fn func(context.Context, *ent.Tx) error) error { | ||
d.lock.Lock() | ||
defer d.lock.Unlock() | ||
|
||
tx, err := d.db.Tx(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if v := recover(); v != nil { | ||
if err := tx.Rollback(); err != nil { | ||
panic(fmt.Errorf("rolling back while recovering (%v): %w", v, err)) | ||
} | ||
|
||
panic(v) | ||
} | ||
}() | ||
|
||
if err := fn(ctx, tx); err != nil { | ||
if rerr := tx.Rollback(); rerr != nil { | ||
return fmt.Errorf("rolling back transaction: %w", rerr) | ||
} | ||
|
||
return err | ||
} | ||
|
||
if err := tx.Commit(); err != nil { | ||
return fmt.Errorf("committing transaction: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *DB) Close() error { | ||
return d.db.Close() | ||
} | ||
|
||
func getDatabasePath(dataPath, userID string) string { | ||
return fmt.Sprintf("file:%v?cache=shared&_fk=1", filepath.Join(dataPath, fmt.Sprintf("%v.db", userID))) | ||
} | ||
|
||
func NewDB(dataPath, userID string) (*DB, error) { | ||
dbPath := getDatabasePath(dataPath, userID) | ||
client, err := ent.Open(dialect.SQLite, dbPath) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &DB{db: client}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters