Skip to content

Commit

Permalink
Store database in file (#75)
Browse files Browse the repository at this point in the history
outofforest authored Oct 21, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7e571a2 commit fd80abb
Showing 2 changed files with 51 additions and 4 deletions.
24 changes: 20 additions & 4 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,9 @@ import (
"context"
"crypto/rand"
"fmt"
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
@@ -21,7 +24,7 @@ import (
func BenchmarkBalanceTransfer(b *testing.B) {
const (
spaceID = 0x00
numOfAddresses = 10_000_000
numOfAddresses = 50_000_000
txsPerCommit = 2000
balance = 100_000
)
@@ -38,7 +41,7 @@ func BenchmarkBalanceTransfer(b *testing.B) {

for bi := 0; bi < b.N; bi++ {
func() {
var size uint64 = 20 * 1024 * 1024 * 1024
var size uint64 = 70 * 1024 * 1024 * 1024
state, stateDeallocFunc, err := alloc.NewState(
size,
4*1024,
@@ -53,11 +56,24 @@ func BenchmarkBalanceTransfer(b *testing.B) {

pool := state.NewPool()

store, storeDeallocFunc, err := persistent.NewMemoryStore(size, true)
file, err := os.OpenFile(filepath.Join(b.TempDir(), "db.quantum"), os.O_CREATE|os.O_RDWR|os.O_TRUNC,
0o600)
if err != nil {
panic(err)
}
defer file.Close()

if _, err := file.Seek(int64(size-1), io.SeekStart); err != nil {
panic(err)
}
if _, err := file.Write([]byte{0x00}); err != nil {
panic(err)
}

store := persistent.NewFileStore(file)
if err != nil {
panic(err)
}
defer storeDeallocFunc()

db, err := New(Config{
State: state,
31 changes: 31 additions & 0 deletions persistent/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package persistent

import (
"io"
"os"

"github.com/pkg/errors"

"github.com/outofforest/quantum/types"
)

// NewFileStore creates new file-based store.
func NewFileStore(file *os.File) *FileStore {
return &FileStore{
file: file,
}
}

// FileStore defines persistent file-based store.
type FileStore struct {
file *os.File
}

// Write writes data to the store.
func (s *FileStore) Write(address types.PhysicalAddress, data []byte) error {
if _, err := s.file.Seek(int64(address), io.SeekStart); err != nil {
return errors.WithStack(err)
}
_, err := s.file.Write(data)
return errors.WithStack(err)
}

0 comments on commit fd80abb

Please sign in to comment.