Skip to content

mdbx: expose Env.Copy* with MDBX_CP_OVERWRITE and add Env.Defrag#223

Open
JkLondon wants to merge 8 commits into
masterfrom
jklondon/defrag-cp
Open

mdbx: expose Env.Copy* with MDBX_CP_OVERWRITE and add Env.Defrag#223
JkLondon wants to merge 8 commits into
masterfrom
jklondon/defrag-cp

Conversation

@JkLondon

Copy link
Copy Markdown
Member

Summary

  • Enables the long-commented Env.Copy / CopyFlag / CopyFD / CopyFDFlag methods and rewires them onto the modern mdbx_env_copy(env, dest, flags) / mdbx_env_copy2fd(env, fd, flags) entry points (the old mdbx_env_copy2 / mdbx_env_copyfd2 API the bindings used no longer exists).
  • Adds the missing copy flags as Go constants: CopyDefaults, CopyForceDynamicSize, CopyDontFlush, CopyThrottleMVCC, and CopyOverwrite (= MDBX_CP_OVERWRITE) so callers can clobber an existing target file.
  • Binds mdbx_env_defrag for explicit in-place defragmentation (no full copy needed): introduces Env.Defrag(DefragOptions) (*DefragResult, error), a DefragResult struct mirroring MDBX_defrag_result_t, and constants for MDBX_defrag_stopping_reasons_t (DefragStepSize, DefragLargeChunk, DefragLaggardReader, DefragEnoughThreshold, DefragTimeLimit, DefragAborted, DefragError, ...). The libmdbx-side progress callback is intentionally left out for now (always NULL); the final metrics are still returned.
  • Replaces the long-disabled TestEnv_Copy* skeletons with working tests and adds dedicated coverage for CopyOverwrite and Env.Defrag.

Windows portability

mdbx_filehandle_t is HANDLE (i.e. void*) on Windows and int on POSIX, so writing C.mdbx_filehandle_t(fd) over a Go uintptr does not compile under GOOS=windows. The fix is a tiny cgo wrapper mdbxgo_env_copy2fd(MDBX_env*, uintptr_t, MDBX_copy_flags_t) that performs the platform-specific cast in C, so the Go side stays identical on every target. Cross-checked with GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build and go test -c.

Test plan

  • go build ./...
  • go test -count=1 ./mdbx/... (darwin/arm64) — all green, including the new TestEnv_Copy, TestEnv_CopyFD, TestEnv_CopyFlag_Overwrite, TestEnv_Defrag
  • GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build ./mdbx/... — cross-compiles cleanly
  • go test -c for the same Windows target — produces a valid PE32+ test binary

JkLondon added 2 commits May 26, 2026 01:32
- Uncomment Env.Copy/CopyFlag/CopyFD/CopyFDFlag and rewire them through
  the unified mdbx_env_copy / mdbx_env_copy2fd entry points.
- Expose MDBX_CP_* flags (CopyDefaults, CopyForceDynamicSize,
  CopyDontFlush, CopyThrottleMVCC and the new CopyOverwrite for
  clobbering an existing target file).
- Bind mdbx_env_defrag via a thin cgo helper (mdbxgo_env_defrag), expose
  DefragOptions / DefragResult and the MDBX_defrag_* stopping reasons.
- Replace the long-commented Copy tests with working ones plus dedicated
  tests for the new CopyOverwrite flag and Env.Defrag.
On Windows mdbx_filehandle_t is HANDLE (void*), so the previous
C.mdbx_filehandle_t(fd) where fd is uintptr would not compile under
GOOS=windows. Route the call through a thin C helper that takes a
uintptr_t and performs the platform-specific cast in C, so the Go side
stays identical on every target. The CopyFD test now runs on Windows
as well.
@JkLondon JkLondon self-assigned this May 25, 2026
JkLondon added 5 commits May 26, 2026 01:42
r.spent_time_dot16 is already typed C.uint64_t, so the explicit
conversion is flagged by unconvert.
mdbx_env_defrag opens its own write transaction via txn_basal_start, and
on Windows it trips ERROR_LOCK_VIOLATION against the env's own LockFileEx
region when run on a handle that has just committed writes. The defrag
API binding itself is fine — only the test scenario hits the libmdbx
Windows locking interaction.
Keep the failure visible in CI until libmdbx clarifies the expected
mdbx_env_defrag calling sequence on Windows / fixes the LockFileEx
region conflict that ERROR_LOCK_VIOLATIONs on a freshly written env.
@AskAlexSharov AskAlexSharov requested a review from Copilot June 19, 2026 07:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants