# wavesdb development targets.
#
# The repository previously had no Makefile, no CI and no -race configuration
# anywhere, despite group commit, a CLOCK block cache, a sharded table cache,
# lock-free skip lists, refcounted read views and 256 commit stripes. Several
# defects fixed in the ondadb parity audit were concurrency defects; `make race`
# exists so running the detector is the default habit rather than an afterthought.

GO ?= go

.PHONY: all build test race short vet fmt fmt-check fuzz bench cover clean check

all: check

build:
	$(GO) build ./...

# The full suite. -count=1 defeats the test cache so a green run means the tests
# actually ran.
test:
	$(GO) test -count=1 ./...

# The race detector needs a generous timeout: the suite exercises real flushes,
# compactions and crash recovery, and instrumentation makes that several times
# slower. The default 10m panics partway through.
race:
	$(GO) test -race -count=1 -timeout 30m ./...

# Quick pass for edit/compile loops.
short:
	$(GO) test -count=1 -short ./...

vet:
	$(GO) vet ./...

fmt:
	$(GO) fmt ./...

fmt-check:
	@out=$$(gofmt -l . | grep -v '^$$' || true); \
	if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi

# Short fuzz runs over the parsers that face untrusted bytes: WAL replay, SSTable
# block framing and varint decoding.
fuzz:
	$(GO) test -run '^$$' -fuzz=FuzzReplay -fuzztime=30s ./internal/wal
	$(GO) test -run '^$$' -fuzz=FuzzReadFramedBlock -fuzztime=30s ./internal/sst
	$(GO) test -run '^$$' -fuzz=FuzzUvarintRoundTrip -fuzztime=15s ./internal/encoding

bench:
	$(GO) test -run '^$$' -bench=. -benchmem ./...

cover:
	$(GO) test -count=1 -coverprofile=coverage.out ./...
	$(GO) tool cover -func=coverage.out | tail -1

# What to run before committing.
# The derived-scheme order-compatibility guard compiles only under the
# wavesdebug tag; without this target it and its test never run anywhere.
debug:
	$(GO) test -count=1 -tags wavesdebug -run 'OrderIncompatible|PartitionCut|Partition' .

check: fmt-check vet build test debug

clean:
	rm -f coverage.out
	$(GO) clean -testcache
