Skip to content

Commit

Permalink
Logdb package and tests for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecreature committed Jul 13, 2024
1 parent 0d0f3d0 commit 77a3361
Show file tree
Hide file tree
Showing 476 changed files with 26,652 additions and 14,957 deletions.
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ linters:
# This file contains only configs which differ from defaults.
# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
linters-settings:
gosec:
exclude-rules:
- G404
nestif:
min-complexity: 8
cyclop:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ vendor:
## build/server: build cmd/server
build/server:
@echo 'Compiling server...'
go build -ldflags="-X main.serverName=${SERVER_NAME} -X main.port=${PORT} -X main.uri=${URI} -X main.db=${DB}" -o=./bin/pulse-server ./cmd/server
go build -o=./bin/pulse-server ./cmd/server
.PHONY:build/server

## build/client: build cmd/client
build/client:
@echo 'Compiling client...'
go build -ldflags="-X main.serverName=${SERVER_NAME} -X main.port=${PORT} -X main.hostname=${HOSTNAME}" -o=./bin/pulse-client ./cmd/client
go build -o=./bin/pulse-client ./cmd/client
.PHONY:build/client

## build: builds the server and client applications
Expand Down
31 changes: 14 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
module github.com/creativecreature/pulse

go 1.21.3
go 1.22.5

require (
github.com/charmbracelet/lipgloss v0.10.0
github.com/charmbracelet/lipgloss v0.12.1
github.com/charmbracelet/log v0.4.0
github.com/neovim/go-client v1.2.1
github.com/spf13/viper v1.19.0
go.mongodb.org/mongo-driver v1.11.7
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
go.mongodb.org/mongo-driver v1.16.0
golang.org/x/exp v0.0.0-20240707233637-46b078467d37
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
Expand All @@ -40,13 +38,12 @@ require (
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
82 changes: 26 additions & 56 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion filename.go → logdb/filename.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pulse
package logdb

import "strings"

Expand Down
8 changes: 4 additions & 4 deletions filename_test.go → logdb/filename_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pulse_test
package logdb_test

import (
"testing"

"github.com/creativecreature/pulse"
"github.com/creativecreature/pulse/logdb"
)

type filenameTest struct {
Expand All @@ -28,7 +28,7 @@ func TestFilename(t *testing.T) {
tc := tc
t.Run(tc.expected, func(t *testing.T) {
t.Parallel()
actual := pulse.Filename(tc.index)
actual := logdb.Filename(tc.index)
if actual != tc.expected {
t.Errorf("expected %s, got %s", tc.expected, actual)
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestIndex(t *testing.T) {
tc := tc
t.Run(tc.filename, func(t *testing.T) {
t.Parallel()
actual := pulse.Index(tc.filename)
actual := logdb.Index(tc.filename)
if actual != tc.expected {
t.Errorf("expected %d, got %d", tc.expected, actual)
}
Expand Down
5 changes: 3 additions & 2 deletions logdb.go → logdb/logdb.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pulse
package logdb

import (
"context"
Expand All @@ -8,6 +8,7 @@ import (

"github.com/charmbracelet/log"
"github.com/creativecreature/pulse/clock"
"github.com/creativecreature/pulse/logger"
)

// Record represents a key-value pair in our database.
Expand All @@ -29,7 +30,7 @@ type LogDB struct {

// NewDB creates a new log database.
func NewDB(dirPath string, segmentSizeKB int, c clock.Clock) *LogDB {
log := NewLogger()
log := logger.New()

// Create the directory if it doesn't exist.
if err := os.MkdirAll(dirPath, 0o755); err != nil {
Expand Down
20 changes: 10 additions & 10 deletions logdb_test.go → logdb/logdb_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pulse_test
package logdb_test

import (
"context"
Expand All @@ -11,8 +11,8 @@ import (
"testing"
"time"

"github.com/creativecreature/pulse"
"github.com/creativecreature/pulse/clock"
"github.com/creativecreature/pulse/logdb"
)

func copyFile(src, dst string) error {
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestConcurrentGetSet(t *testing.T) {
cpus := runtime.NumCPU()
writeCPUs, readCPUs := cpus/2, cpus/2
numIterations := 10_000
db := pulse.NewDB(t.TempDir(), 10, clock.New())
db := logdb.NewDB(t.TempDir(), 10, clock.New())

wg := sync.WaitGroup{}
wg.Add(numIterations * (writeCPUs + readCPUs))
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestUniqueValues(t *testing.T) {
}

mockClock := clock.NewMock(time.Now())
db := pulse.NewDB(path, 10, mockClock)
db := logdb.NewDB(path, 10, mockClock)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -130,7 +130,7 @@ func TestAggregation(t *testing.T) {
}

mockClock := clock.NewMock(time.Now())
db := pulse.NewDB(path, 10, mockClock)
db := logdb.NewDB(path, 10, mockClock)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -157,7 +157,7 @@ func TestCompaction(t *testing.T) {
}

mockClock := clock.NewMock(time.Now())
db := pulse.NewDB(path, 10, mockClock)
db := logdb.NewDB(path, 10, mockClock)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestAggregationAfterCompaction(t *testing.T) {
}

mockClock := clock.NewMock(time.Now())
db := pulse.NewDB(path, 10, mockClock)
db := logdb.NewDB(path, 10, mockClock)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestCompactionWritesAggregation(t *testing.T) {
}

mockClock := clock.NewMock(time.Now())
db := pulse.NewDB(path, 10, mockClock)
db := logdb.NewDB(path, 10, mockClock)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestAppendingCompactingWritesAggregation(t *testing.T) {
}

mockClock := clock.NewMock(time.Now())
db := pulse.NewDB(path, 10, mockClock)
db := logdb.NewDB(path, 10, mockClock)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestWritesCompacting(t *testing.T) {
t.Parallel()

mockClock := clock.NewMock(time.Now())
db := pulse.NewDB(t.TempDir(), 10, mockClock)
db := logdb.NewDB(t.TempDir(), 10, mockClock)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion restore.go → logdb/restore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pulse
package logdb

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion scan.go → logdb/scan.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pulse
package logdb

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion segment.go → logdb/segment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pulse
package logdb

import (
"encoding/json"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions logger.go → logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pulse
package logger

import (
"os"
Expand All @@ -9,7 +9,7 @@ import (

// New wraps the construction of a charmbracelet logger
// in order to achieve coherent styles and settings.
func NewLogger() *log.Logger {
func New() *log.Logger {
logger := log.New(os.Stdout)
logger.SetColorProfile(0)
logger.SetLevel(log.DebugLevel)
Expand Down
3 changes: 2 additions & 1 deletion mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/charmbracelet/log"
"github.com/creativecreature/pulse"
"github.com/creativecreature/pulse/logger"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -38,7 +39,7 @@ func New(uri, database string) *Client {
return &Client{
Client: client,
database: database,
log: pulse.NewLogger(),
log: logger.New(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/creativecreature/pulse"
)

const aggregationInterval = 10 * time.Minute
const aggregationInterval = 30 * time.Minute

// writeToRemote will write the session to the remote storage.
func (s *Server) writeToRemote(session pulse.CodingSession) {
Expand Down
8 changes: 5 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/creativecreature/pulse"
"github.com/creativecreature/pulse/clock"
"github.com/creativecreature/pulse/git"
"github.com/creativecreature/pulse/logdb"
"github.com/creativecreature/pulse/logger"
)

// SessionWriter is an abstraction for writing coding sessions to a permanent storage.
Expand All @@ -29,14 +31,14 @@ type Server struct {
name string
lastHeartbeat time.Time
sessionWriter SessionWriter
db *pulse.LogDB
db *logdb.LogDB
}

// New creates a new server.
func New(cfg *pulse.Config, segmentPath string, sessionWriter SessionWriter, opts ...Option) *Server {
s := &Server{
clock: clock.New(),
log: pulse.NewLogger(),
log: logger.New(),
name: cfg.Server.Name,
sessionWriter: sessionWriter,
}
Expand All @@ -45,7 +47,7 @@ func New(cfg *pulse.Config, segmentPath string, sessionWriter SessionWriter, opt
opt(s)
}

s.db = pulse.NewDB(segmentPath, cfg.Server.SegmentSizeKB, s.clock)
s.db = logdb.NewDB(segmentPath, cfg.Server.SegmentSizeKB, s.clock)

return s
}
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestServerMergesFiles(t *testing.T) {
OS: "Linux",
}, &reply)

mockClock.Add(time.Minute * 15)
mockClock.Add(time.Minute * 30)
mockClock.Add(time.Minute)
time.Sleep(200 * time.Millisecond)

Expand Down
Loading

0 comments on commit 77a3361

Please sign in to comment.