diff --git a/block/block.go b/block/block.go index 3e30377..a4733ed 100644 --- a/block/block.go +++ b/block/block.go @@ -50,7 +50,11 @@ type BlockRepository interface { // storage space if not done automatically by the backend. Vacuum() error + // Count returns the total number of stored blocks in the map database. Count() (int64, error) + + // Close gracefully finishes the connection with the database backend. + Close() error } // NewBlockRepository initializes the connection with the appropriate database diff --git a/block/block_postgres.go b/block/block_postgres.go index 60d2c89..7a193f6 100644 --- a/block/block_postgres.go +++ b/block/block_postgres.go @@ -160,3 +160,7 @@ func (r *postgresBlockRepository) Import(z *zip.Reader) error { return nil } + +func (r *postgresBlockRepository) Close() error { + return r.db.Close() +} diff --git a/block/block_postgres_test.go b/block/block_postgres_test.go index 80ddf1b..372a7e3 100644 --- a/block/block_postgres_test.go +++ b/block/block_postgres_test.go @@ -24,12 +24,12 @@ func setupPostgress(t *testing.T) (block.BlockRepository, *sql.DB) { } func TestPostgresBlocksRepo(t *testing.T) { - blocks_repo, _ := setupPostgress(t) - testBlocksRepository(t, blocks_repo) + r, _ := setupPostgress(t) + testBlocksRepository(t, r) } func TestPostgresMaxConnections(t *testing.T) { - blocks_repo, db := setupPostgress(t) + r, db := setupPostgress(t) var maxConnections int row := db.QueryRow("show max_connections;") @@ -43,14 +43,14 @@ func TestPostgresMaxConnections(t *testing.T) { PosZ: 1, Data: []byte("test"), } - if err := blocks_repo.Update(&fakeBlock); err != nil { + if err := r.Update(&fakeBlock); err != nil { t.Fatalf("Error setting up test case: %v", err) } // Run more than max_connections query operations in a loop count := 0 for i := 0; i < maxConnections*2; i++ { - b, err := blocks_repo.GetByPos(1, 1, 1) + b, err := r.GetByPos(1, 1, 1) count++ if b != nil && count%10 == 0 { t.Logf("Executed %d operations. b=%v", count, b) diff --git a/block/block_sqlite.go b/block/block_sqlite.go index e498ea0..a016e00 100644 --- a/block/block_sqlite.go +++ b/block/block_sqlite.go @@ -63,6 +63,7 @@ func (repo *sqliteBlockRepository) GetByPos(x, y, z int) (*Block, error) { if err != nil { return nil, err } + defer rows.Close() if !rows.Next() { return nil, nil } @@ -212,3 +213,7 @@ func (r *sqliteBlockRepository) Import(z *zip.Reader) error { return nil } + +func (r *sqliteBlockRepository) Close() error { + return r.db.Close() +} diff --git a/block/block_sqlite_test.go b/block/block_sqlite_test.go index 447f20c..ee35fd1 100644 --- a/block/block_sqlite_test.go +++ b/block/block_sqlite_test.go @@ -28,22 +28,29 @@ func setupSqlite(t *testing.T) (block.BlockRepository, *sql.DB) { func TestSqliteBlockRepo(t *testing.T) { // open db - blocks_repo, _ := setupSqlite(t) - testBlocksRepository(t, blocks_repo) + r, _ := setupSqlite(t) + defer r.Close() + testBlocksRepository(t, r) } func TestSqliteIterator(t *testing.T) { - blocks_repo, _ := setupSqlite(t) - testBlocksRepositoryIterator(t, blocks_repo) + r, _ := setupSqlite(t) + defer r.Close() + testBlocksRepositoryIterator(t, r) } func TestSqliteIteratorErrorHandling(t *testing.T) { - blocks_repo, db := setupSqlite(t) + r, db := setupSqlite(t) defer db.Close() + defer r.Close() + + testIteratorErrorHandling(t, r, db, `UPDATE blocks SET pos = 18446744073709551615;`) +} - testIteratorErrorHandling(t, blocks_repo, db, ` - UPDATE blocks SET pos = 18446744073709551615; - `) +func TestSqliteIteratorCloser(t *testing.T) { + r, _ := setupSqlite(t) + defer r.Close() + testIteratorClose(t, r) } func TestCoordToPlain(t *testing.T) { @@ -72,8 +79,3 @@ func TestCoordToPlain(t *testing.T) { } } } - -func TestSqliteIteratorCloser(t *testing.T) { - r, _ := setupSqlite(t) - testIteratorClose(t, r) -}