Repository: polarsignals/frostdb
Pull request #568 (2023-10-23 01:09:14)

Add constant value to make the code more readable
Add constant value to make the code more readable. I believe it should be more elegant. 
Could you help review it when you have time? Thanks!


Patch:
diff --git a/db.go b/db.go
index f43f103..b1646bb 100644
--- a/db.go
+++ b/db.go
@@ -36,6 +36,14 @@ import (
 	"github.com/polarsignals/frostdb/wal"
 )
 
+const (
+	B   = 1
+	KiB = 1024 * B
+	MiB = 1024 * KiB
+	GiB = 1024 * MiB
+	TiB = 1024 * GiB
+)
+
 type ColumnStore struct {
 	mtx                 *sync.RWMutex
 	dbs                 map[string]*DB
@@ -89,8 +97,8 @@ func New(
 		indexConfig:      DefaultIndexConfig(),
 		indexDegree:      2,
 		splitSize:        2,
-		granuleSizeBytes: 1 * 1024 * 1024,   // 1MB granule size before splitting
-		activeMemorySize: 512 * 1024 * 1024, // 512MB
+		granuleSizeBytes: 1 * MiB,
+		activeMemorySize: 512 * MiB,
 	}
 
 	for _, option := range options {
diff --git a/store.go b/store.go
index 11ea334..795bcab 100644
--- a/store.go
+++ b/store.go
@@ -175,7 +175,7 @@ func (b *DefaultObjstoreBucket) openBlockFile(ctx context.Context, blockName str
 	file, err := parquet.OpenFile(
 		r,
 		size,
-		parquet.ReadBufferSize(5*1024*1024), // 5MB read buffers
+		parquet.ReadBufferSize(5*MiB), // 5MB read buffers
 		parquet.SkipBloomFilters(true),
 		parquet.FileReadMode(parquet.ReadModeAsync),
 	)


Test patch:
diff --git a/db_test.go b/db_test.go
index f748d1f..c230718 100644
--- a/db_test.go
+++ b/db_test.go
@@ -46,7 +46,7 @@ func TestDBWithWALAndBucket(t *testing.T) {
 		WithWAL(),
 		WithStoragePath(dir),
 		WithReadWriteStorage(sinksource),
-		WithActiveMemorySize(100*1024),
+		WithActiveMemorySize(100*KiB),
 	)
 	require.NoError(t, err)
 	db, err := c.DB(context.Background(), "test")
@@ -71,7 +71,7 @@ func TestDBWithWALAndBucket(t *testing.T) {
 		WithWAL(),
 		WithStoragePath(dir),
 		WithReadWriteStorage(sinksource),
-		WithActiveMemorySize(100*1024),
+		WithActiveMemorySize(100*KiB),
 	)
 	require.NoError(t, err)
 	defer c.Close()
@@ -1358,7 +1358,7 @@ func Test_DB_ReadOnlyQuery(t *testing.T) {
 		WithWAL(),
 		WithStoragePath(dir),
 		WithReadWriteStorage(sinksource),
-		WithActiveMemorySize(100*1024),
+		WithActiveMemorySize(100*KiB),
 	)
 	require.NoError(t, err)
 	db, err := c.DB(context.Background(), "test")
@@ -1383,7 +1383,7 @@ func Test_DB_ReadOnlyQuery(t *testing.T) {
 		WithWAL(),
 		WithStoragePath(dir),
 		WithReadWriteStorage(sinksource),
-		WithActiveMemorySize(100*1024),
+		WithActiveMemorySize(100*KiB),
 	)
 	require.NoError(t, err)
 	defer c.Close()
@@ -1867,7 +1867,7 @@ func Test_DB_DropStorage(t *testing.T) {
 		WithLogger(logger),
 		WithWAL(),
 		WithStoragePath(dir),
-		WithActiveMemorySize(1024*1024),
+		WithActiveMemorySize(1*MiB),
 	)
 	defer func() {
 		_ = c.Close()
@@ -1909,7 +1909,7 @@ func Test_DB_DropStorage(t *testing.T) {
 		WithLogger(logger),
 		WithWAL(),
 		WithStoragePath(dir),
-		WithActiveMemorySize(1024*1024),
+		WithActiveMemorySize(1*MiB),
 	)
 	defer func() {
 		_ = c.Close()
@@ -2009,8 +2009,8 @@ func Test_DB_SnapshotOnClose(t *testing.T) {
 		WithLogger(logger),
 		WithStoragePath(dir),
 		WithWAL(),
-		WithActiveMemorySize(1024*1024*1024),
-		WithSnapshotTriggerSize(1024*1024*1024),
+		WithActiveMemorySize(1*GiB),
+		WithSnapshotTriggerSize(1*GiB),
 		WithManualBlockRotation(),
 	)
 	require.NoError(t, err)
diff --git a/table_test.go b/table_test.go
index 328321e..a2895f3 100644
--- a/table_test.go
+++ b/table_test.go
@@ -1135,7 +1135,7 @@ func Test_Table_DynamicColumnNotDefined(t *testing.T) {
 func TestTableUniquePrimaryIndex(t *testing.T) {
 	c, err := New(WithGranuleSizeBytes(1), WithIndexConfig([]*index.LevelConfig{
 		{Level: index.L0, MaxSize: 180},
-		{Level: index.L1, MaxSize: 1024 * 1024 * 1024 * 1024},
+		{Level: index.L1, MaxSize: 1 * TiB},
 	}))
 	require.NoError(t, err)
 	defer c.Close()