Skip to content

Commit 6e5c01f

Browse files
committed
add size api
1 parent 143df17 commit 6e5c01f

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

db.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ type DB interface {
4141
// Any persistent changes to the database should be done by calling CRUD APIs on this connection.
4242
AcquireWriteConnection(ctx context.Context) (conn Conn, release func() error, err error)
4343

44+
// Size returns the size of the database in bytes.
45+
// It is currently implemented as sum of the size of all serving `.db` files.
46+
Size() int64
47+
4448
// CRUD APIs
4549

4650
// CreateTableAsSelect creates a new table by name from the results of the given SQL query.
@@ -272,10 +276,20 @@ func NewDB(ctx context.Context, dbIdentifier string, opts *DBOptions) (DB, error
272276
// create read handle
273277
db.readHandle, err = db.openDBAndAttach(ctx, true)
274278
if err != nil {
279+
// Check for using incompatible database files
280+
if strings.Contains(err.Error(), "Trying to read a database file with version number") {
281+
return nil, fmt.Errorf("database file was created with an older, incompatible version of Rill (please remove `tmp` directory and try again)")
282+
}
283+
275284
// Check for another process currently accessing the DB
276285
if strings.Contains(err.Error(), "Could not set lock on file") {
277286
return nil, fmt.Errorf("failed to open database (is Rill already running?): %w", err)
278287
}
288+
289+
if strings.Contains(err.Error(), "Symbol not found") {
290+
fmt.Printf("Your version of macOS is not supported. Please upgrade to the latest major release of macOS. See this link for details: https://support.apple.com/en-in/macos/upgrade")
291+
os.Exit(1)
292+
}
279293
return nil, err
280294
}
281295

@@ -828,6 +842,32 @@ func (d *db) syncRead(ctx context.Context) error {
828842
return nil
829843
}
830844

845+
func (d *db) Size() int64 {
846+
var paths []string
847+
entries, err := os.ReadDir(d.readPath)
848+
if err != nil { // ignore error
849+
return 0
850+
}
851+
for _, entry := range entries {
852+
if !entry.IsDir() {
853+
continue
854+
}
855+
// this is to avoid counting temp tables during source ingestion
856+
// in certain cases we only want to compute the size of the serving db files
857+
// TODO :: remove this when removing staged table concepts
858+
if strings.HasPrefix(entry.Name(), "__rill_tmp_") {
859+
continue
860+
}
861+
path := filepath.Join(d.readPath, entry.Name())
862+
version, exist, _ := d.readTableVersion(entry.Name())
863+
if !exist {
864+
continue
865+
}
866+
paths = append(paths, filepath.Join(path, fmt.Sprintf("%s.db", version)))
867+
}
868+
return fileSize(paths)
869+
}
870+
831871
// acquireWriteConn syncs the write database, initializes the write handle and returns a write connection.
832872
// The release function should be called to release the connection.
833873
// It should be called with the writeMu locked.
@@ -893,16 +933,6 @@ func (d *db) openDBAndAttach(ctx context.Context, read bool) (*sqlx.DB, error) {
893933
return nil
894934
})
895935
if err != nil {
896-
// Check for using incompatible database files
897-
if strings.Contains(err.Error(), "Trying to read a database file with version number") {
898-
return nil, fmt.Errorf("database file was created with an older, incompatible version of Rill (please remove `tmp` directory and try again)")
899-
}
900-
901-
// Check for another process currently accessing the DB
902-
if strings.Contains(err.Error(), "Could not set lock on file") {
903-
return nil, fmt.Errorf("failed to open database (is Rill already running?): %w", err)
904-
}
905-
906936
return nil, err
907937
}
908938

io.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ func copyFile(dst, src string) error {
6363
}
6464
return nil
6565
}
66+
67+
func fileSize(paths []string) int64 {
68+
var size int64
69+
for _, path := range paths {
70+
if info, err := os.Stat(path); err == nil { // ignoring error since only error possible is *PathError
71+
size += info.Size()
72+
}
73+
}
74+
return size
75+
}

singledb.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ func (s *singledb) alterTableColumn(ctx context.Context, conn *sqlx.Conn, tableN
302302
return err
303303
}
304304

305+
// TODO :: fix by calling pragma_database_size
306+
func (s *singledb) Size() int64 {
307+
return 0
308+
}
309+
305310
func isView(ctx context.Context, conn *sqlx.Conn, name string) (bool, error) {
306311
var view bool
307312
err := conn.QueryRowxContext(ctx, `

0 commit comments

Comments
 (0)