[
  {
    "path": ".gitattributes",
    "content": "# ensure that line endings for Windows builds are properly formatted\n# see https://github.com/golangci/golangci-lint-action?tab=readme-ov-file#how-to-use\n# at \"Multiple OS Example\" section\n*.go text eol=lf\n"
  },
  {
    "path": ".github/dependabot.yml",
    "content": "version: 2\nupdates:\n  - package-ecosystem: github-actions\n    directory: /\n    schedule:\n      interval: weekly\n\n  - package-ecosystem: gomod\n    directory: /\n    schedule:\n      interval: weekly\n"
  },
  {
    "path": ".github/workflows/benchmark-pr.yaml",
    "content": "---\nname: Benchmarks on PRs (AMD64)\npermissions: read-all\non: [pull_request]\njobs:\n  amd64:\n    uses: ./.github/workflows/benchmark-template.yaml\n    with:\n      benchGitRef: ${{ github.event.pull_request.base.sha }}\n"
  },
  {
    "path": ".github/workflows/benchmark-releases.yaml",
    "content": "---\nname: Nightly Benchmarks against last release (AMD64)\npermissions: read-all\non:\n  schedule:\n    - cron: '10 5 * * *' # runs every day at 05:10 UTC\n  # workflow_dispatch enables manual testing of this job by maintainers\n  workflow_dispatch:\njobs:\n  amd64:\n    uses: ./.github/workflows/benchmark-template.yaml\n    with:\n      benchGitRef: release-1.3\n"
  },
  {
    "path": ".github/workflows/benchmark-template.yaml",
    "content": "---\nname: Reusable Benchmark Template\non:\n  workflow_call:\n    inputs:\n      # which git reference to benchmark against\n      benchGitRef:\n        required: true\n        type: string\n      maxAcceptableDifferencePercent:\n        required: false\n        type: number\n        default: 5\n      runs-on:\n        required: false\n        type: string\n        default: \"['ubuntu-latest']\"\npermissions: read-all\n\njobs:\n  benchmark:\n    runs-on: ${{ fromJson(inputs.runs-on) }}\n    steps:\n    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      with:\n        fetch-depth: 0\n    - id: goversion\n      run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n    - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n      with:\n        go-version: ${{ steps.goversion.outputs.goversion }}\n    - name: Run Benchmarks\n      run: |\n        BENCHSTAT_OUTPUT_FILE=result.txt make test-benchmark-compare REF=${{ inputs.benchGitRef }}\n    - run: |\n        echo \"\\`\\`\\`\" >> \"$GITHUB_STEP_SUMMARY\"\n        cat result.txt >> \"$GITHUB_STEP_SUMMARY\"\n        echo \"\\`\\`\\`\" >> \"$GITHUB_STEP_SUMMARY\"\n        cat <<EOL >> \"$GITHUB_STEP_SUMMARY\"\n        <hr />\n        The table shows the median and 90% confidence interval (CI) summaries for each benchmark comparing the HEAD and the BASE, and an A/B comparison under \"vs base\". The last column shows the statistical p-value with ten runs (n=10).\n        The last row has the Geometric Mean (geomean) for the given rows in the table.\n        Refer to [benchstat's documentation](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat) for more help.\n        EOL\n    - name: Validate results under acceptable limit\n      run: |\n        export MAX_ACCEPTABLE_DIFFERENCE=${{ inputs.maxAcceptableDifferencePercent }}\n        while IFS= read -r line; do\n          # Get fourth value, which is the comparison with the base.\n          value=\"$(echo \"$line\" | awk '{print $4}')\"\n          if [[ \"$value\" = +* ]] || [[ \"$value\" = -* ]]; then\n            if (( $(echo \"${value//[^0-9.]/}\"'>'\"$MAX_ACCEPTABLE_DIFFERENCE\" | bc -l) )); then\n              echo \"::error::$value is above the maximum acceptable difference ($MAX_ACCEPTABLE_DIFFERENCE)\"\n              exit 1\n            fi\n          fi\n        done < <(grep geomean result.txt)\n"
  },
  {
    "path": ".github/workflows/cross-arch-template.yaml",
    "content": "---\nname: Reusable Cross-Platform Build Workflow\non:\n  workflow_call:\n    inputs:\n      archs:\n        required: true\n        type: string\n      os:\n        required: true\n        type: string\npermissions: read-all\n\njobs:\n  cross-build:\n    strategy:\n      matrix:\n        arch: ${{ fromJSON(inputs.archs) }}\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n    - id: goversion\n      run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n    - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n      with:\n        go-version: ${{ steps.goversion.outputs.goversion }}\n    - name: Build for ${{ inputs.os }}/${{ matrix.arch }}\n      run: |\n        GOOS=${{ inputs.os }} GOARCH=${{ matrix.arch }} go build ./...\n"
  },
  {
    "path": ".github/workflows/cross-arch-test.yaml",
    "content": "---\nname: Cross-Platform Build Tests\npermissions: read-all\non: [push, pull_request]\n\njobs:\n  build-aix:\n    uses: ./.github/workflows/cross-arch-template.yaml\n    with:\n      os: aix\n      archs: \"['ppc64']\"\n  build-android:\n    uses: ./.github/workflows/cross-arch-template.yaml\n    with:\n      os: android\n      archs: \"['arm64']\"\n  build-linux:\n    uses: ./.github/workflows/cross-arch-template.yaml\n    with:\n      os: linux\n      archs: \"['386','amd64','arm','arm64','loong64','mips','mips64','mips64le','mipsle','ppc64','ppc64le','riscv64','s390x']\"\n  build-openbsd:\n    uses: ./.github/workflows/cross-arch-template.yaml\n    with:\n      os: openbsd\n      archs: \"['386','amd64','arm','arm64','ppc64','riscv64']\"\n  build-solaris:\n    uses: ./.github/workflows/cross-arch-template.yaml\n    with:\n      os: solaris\n      archs: \"['amd64']\"\n  build-windows:\n    uses: ./.github/workflows/cross-arch-template.yaml\n    with:\n      os: windows\n      archs: \"['386','amd64','arm64']\"\n"
  },
  {
    "path": ".github/workflows/failpoint_test.yaml",
    "content": "---\nname: Failpoint test\non: [push, pull_request]\npermissions: read-all\njobs:\n  test:\n    strategy:\n      matrix:\n        os: [ubuntu-latest]\n    runs-on: ${{ matrix.os }}\n    steps:\n      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      - id: goversion\n        run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n      - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n        with:\n          go-version: ${{ steps.goversion.outputs.goversion }}\n      - name: Run golangci-lint\n        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0\n        with:\n          version: v2.1.6\n      - run: |\n          make gofail-enable\n          make test-failpoint\n"
  },
  {
    "path": ".github/workflows/gh-workflow-approve.yaml",
    "content": "---\nname: Approve GitHub Workflows\npermissions: read-all\non:\n  pull_request_target:\n    types:\n      - labeled\n      - synchronize\n    branches:\n      - main\n      - release-1.3\n\njobs:\n  approve:\n    name: Approve ok-to-test\n    if: contains(github.event.pull_request.labels.*.name, 'ok-to-test')\n    runs-on: ubuntu-latest\n    permissions:\n      actions: write\n    steps:\n      - name: Update PR\n        uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0\n        continue-on-error: true\n        with:\n          github-token: ${{ secrets.GITHUB_TOKEN }}\n          debug: ${{ secrets.ACTIONS_RUNNER_DEBUG == 'true' }}\n          script: |\n            const result = await github.rest.actions.listWorkflowRunsForRepo({\n              owner: context.repo.owner,\n              repo: context.repo.repo,\n              event: \"pull_request\",\n              status: \"action_required\",\n              head_sha: context.payload.pull_request.head.sha,\n              per_page: 100\n            });\n            for (var run of result.data.workflow_runs) {\n              await github.rest.actions.approveWorkflowRun({\n                owner: context.repo.owner,\n                repo: context.repo.repo,\n                run_id: run.id\n              });\n            }\n"
  },
  {
    "path": ".github/workflows/robustness_nightly.yaml",
    "content": "---\nname: Robustness Nightly\npermissions: read-all\non:\n  schedule:\n    - cron: '25 9 * * *' # runs every day at 09:25 UTC\n  # workflow_dispatch enables manual testing of this job by maintainers\n  workflow_dispatch:\n\njobs:\n  amd64:\n    # GHA has a maximum amount of 6h execution time, we try to get done within 3h\n    uses: ./.github/workflows/robustness_template.yaml\n    with:\n      count: 100\n      testTimeout: 200m\n      runs-on: \"['ubuntu-latest']\"\n"
  },
  {
    "path": ".github/workflows/robustness_template.yaml",
    "content": "---\nname: Reusable Robustness Workflow\non:\n  workflow_call:\n    inputs:\n      count:\n        required: true\n        type: number\n      testTimeout:\n        required: false\n        type: string\n        default: '30m'\n      runs-on:\n        required: false\n        type: string\n        default: \"['ubuntu-latest']\"\npermissions: read-all\n\njobs:\n  test:\n    # this is to prevent the job to run at forked projects\n    if: github.repository == 'etcd-io/bbolt'\n    timeout-minutes: 210\n    runs-on: ${{ fromJson(inputs.runs-on) }}\n    steps:\n      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      - id: goversion\n        run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n      - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n        with:\n          go-version: ${{ steps.goversion.outputs.goversion }}\n      - name: Run golangci-lint\n        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0\n        with:\n          version: v2.1.6\n      - name: test-robustness\n        run: |\n          set -euo pipefail\n          sudo apt-get install -y dmsetup xfsprogs\n\n          ROBUSTNESS_TESTFLAGS=\"--count ${{ inputs.count }} --timeout ${{ inputs.testTimeout }} -failfast\" make test-robustness\n\n      - name: Host Status\n        if: always()\n        run: |\n          set -x\n          mount\n          df\n          losetup -l\n      - name: Kernel Message\n        if: failure()\n        run: |\n          sudo lsmod\n          sudo dmesg -T -f kern\n"
  },
  {
    "path": ".github/workflows/robustness_test.yaml",
    "content": "name: Robustness Test\non: [push, pull_request]\npermissions: read-all\njobs:\n  amd64:\n    uses: ./.github/workflows/robustness_template.yaml\n    with:\n      count: 10\n      testTimeout: 30m\n      runs-on: \"['ubuntu-latest']\"\n  arm64:\n    uses: ./.github/workflows/robustness_template.yaml\n    with:\n      count: 10\n      testTimeout: 30m\n      runs-on: \"['ubuntu-24.04-arm']\"\n"
  },
  {
    "path": ".github/workflows/stale.yaml",
    "content": "name: 'Close stale issues and PRs'\non:\n  schedule:\n    - cron: '0 0 * * *'  # every day at 00:00 UTC\n\npermissions:\n  issues: write\n  pull-requests: write\n\njobs:\n  stale:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/stale@b5d41d4e1d5dceea10e7104786b73624c18a190f # v10.2.0\n        with:\n          days-before-stale: 90\n          days-before-close: 21\n          stale-issue-label: stale\n          stale-pr-label: stale\n"
  },
  {
    "path": ".github/workflows/tests-template.yml",
    "content": "---\nname: Reusable unit test Workflow\non:\n  workflow_call:\n    inputs:\n      runs-on:\n        required: false\n        type: string\n        default: ubuntu-latest\n      targets:\n        required: false\n        type: string\n        default: \"['linux-unit-test-1-cpu','linux-unit-test-2-cpu','linux-unit-test-4-cpu']\"\npermissions: read-all\n\njobs:\n  test-linux:\n    strategy:\n      fail-fast: false\n      matrix:\n        target: ${{ fromJSON(inputs.targets) }}\n    runs-on: ${{ inputs.runs-on }}\n    steps:\n      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      - id: goversion\n        run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n      - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n        with:\n          go-version: ${{ steps.goversion.outputs.goversion }}\n      - run: make fmt\n      - env:\n          TARGET: ${{ matrix.target }}\n        run: |\n          case \"${TARGET}\" in\n            linux-unit-test-1-cpu)\n              CPU=1 make test\n              ;;\n            linux-unit-test-2-cpu)\n              CPU=2 make test\n              ;;\n            linux-unit-test-4-cpu)\n              CPU=4 make test\n              ;;\n            linux-unit-test-4-cpu-race)\n              CPU=4 ENABLE_RACE=true make test\n              ;;\n            *)\n              echo \"Failed to find target\"\n              exit 1\n              ;;\n          esac\n      - name: Run golangci-lint\n        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0\n        with:\n          version: v2.1.6\n"
  },
  {
    "path": ".github/workflows/tests_amd64.yaml",
    "content": "---\nname: Tests AMD64\npermissions: read-all\non: [push, pull_request]\njobs:\n  test-linux-amd64:\n    uses: ./.github/workflows/tests-template.yml\n  test-linux-amd64-race:\n    uses: ./.github/workflows/tests-template.yml\n    with:\n      runs-on: ubuntu-latest\n      targets: \"['linux-unit-test-4-cpu-race']\"\n\n  coverage:\n    needs:\n      - test-linux-amd64\n      - test-linux-amd64-race\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      - id: goversion\n        run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n      - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n        with:\n          go-version: ${{ steps.goversion.outputs.goversion }}\n      - name: Run golangci-lint\n        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0\n        with:\n          version: v2.1.6\n      - run: make coverage\n"
  },
  {
    "path": ".github/workflows/tests_arm64.yaml",
    "content": "---\nname: Tests ARM64\npermissions: read-all\non: [push, pull_request]\njobs:\n  test-linux-arm64:\n    uses: ./.github/workflows/tests-template.yml\n    with:\n      runs-on: ubuntu-24.04-arm\n  test-linux-arm64-race:\n    uses: ./.github/workflows/tests-template.yml\n    with:\n      runs-on: ubuntu-24.04-arm\n      targets: \"['linux-unit-test-4-cpu-race']\"\n\n  coverage:\n    needs:\n      - test-linux-arm64\n      - test-linux-arm64-race\n    runs-on: ubuntu-24.04-arm\n    steps:\n      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      - id: goversion\n        run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n      - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n        with:\n          go-version: ${{ steps.goversion.outputs.goversion }}\n      - name: Run golangci-lint\n        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0\n        with:\n          version: v2.1.6\n      - run: make coverage\n"
  },
  {
    "path": ".github/workflows/tests_windows.yml",
    "content": "---\nname: Tests\non: [push, pull_request]\npermissions: read-all\njobs:\n  test-windows:\n    strategy:\n      fail-fast: false\n      matrix:\n        target:\n          - windows-amd64-unit-test-4-cpu\n        # FIXME(fuweid):\n        #\n        # The windows will throws the following error when enable race.\n        # We skip it until we have solution.\n        #\n        #   ThreadSanitizer failed to allocate 0x000200000000 (8589934592) bytes at 0x0400c0000000 (error code: 1455)\n        #\n        # - windows-amd64-unit-test-4-cpu-race\n    runs-on: windows-latest\n    steps:\n      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      - id: goversion\n        run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n      - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n        with:\n          go-version: ${{ steps.goversion.outputs.goversion }}\n      - run: make fmt\n      - env:\n          TARGET: ${{ matrix.target }}\n        run: |\n          case \"${TARGET}\" in\n            windows-amd64-unit-test-4-cpu)\n              CPU=4 TIMEOUT=60m make test\n              ;;\n            *)\n              echo \"Failed to find target\"\n              exit 1\n              ;;\n          esac\n        shell: bash\n      - name: Run golangci-lint\n        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0\n        with:\n          version: v2.1.6\n\n  coverage:\n    needs: [\"test-windows\"]\n    runs-on: windows-latest\n    steps:\n      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2\n      - id: goversion\n        run: echo \"goversion=$(cat .go-version)\" >> \"$GITHUB_OUTPUT\"\n      - uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0\n        with:\n          go-version: ${{ steps.goversion.outputs.goversion }}\n      - name: Run golangci-lint\n        uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0\n        with:\n          version: v2.1.6\n      - run: make coverage\n        env:\n          TIMEOUT: 60m\n"
  },
  {
    "path": ".gitignore",
    "content": "*.prof\n*.test\n*.swp\n/bin/\ncover.out\ncover-*.out\n/.idea\n*.iml\n/bbolt\n/cmd/bbolt/bbolt\n.DS_Store\n\n"
  },
  {
    "path": ".go-version",
    "content": "1.24.13\n"
  },
  {
    "path": ".golangci.yaml",
    "content": "formatters:\n  enable:\n    - gci\n    - gofmt\n    - goimports\n  settings: # please keep this alphabetized\n    gci:\n      sections:\n        - standard\n        - default\n        - prefix(go.etcd.io)\n    goimports:\n      local-prefixes:\n        - go.etcd.io # Put imports beginning with prefix after 3rd-party packages.\nissues:\n  max-same-issues: 0\nlinters:\n  default: none\n  enable: # please keep this alphabetized\n    - errcheck\n    - govet\n    - ineffassign\n    - staticcheck\n    - unused\n  exclusions:\n    presets:\n      - comments\n      - common-false-positives\n      - legacy\n      - std-error-handling\n  settings: # please keep this alphabetized\n    staticcheck:\n      checks:\n        - all\n        - -QF1003 # Convert if/else-if chain to tagged switch\n        - -QF1010 # Convert slice of bytes to string when printing it\n        - -ST1003 # Poorly chosen identifier\n        - -ST1005 # Incorrectly formatted error string\n        - -ST1012 # Poorly chosen name for error variable\nversion: \"2\"\n"
  },
  {
    "path": "CHANGELOG/CHANGELOG-1.3.md",
    "content": "Note that we start to track changes starting from v1.3.7.\n\n<hr>\n\n## v1.3.12(2025-08-19)\n\n### BoltDB\n- [Add protection on meta page when it's being written](https://github.com/etcd-io/bbolt/pull/1006)\n- Fix [potential data corruption in `(*Tx)WriteTo` if underlying db file is overwritten](https://github.com/etcd-io/bbolt/pull/1059)\n\n<hr>\n\n## v1.3.11(2024-08-21)\n\n### BoltDB\n- Fix [the `freelist.allocs` isn't rollbacked when a tx is rollbacked](https://github.com/etcd-io/bbolt/pull/823).\n\n### CMD\n- Add [`-gobench-output` option for bench command to adapt to benchstat](https://github.com/etcd-io/bbolt/pull/802).\n\n### Other\n- [Bump go version to 1.22.x](https://github.com/etcd-io/bbolt/pull/822).\n- This patch also added `dmflakey` package, which can be reused by other projects. See https://github.com/etcd-io/bbolt/pull/812.\n\n<hr>\n\n## v1.3.10(2024-05-06)\n\n### BoltDB\n- [Remove deprecated `UnsafeSlice` and use `unsafe.Slice`](https://github.com/etcd-io/bbolt/pull/717)\n- [Stabilize the behaviour of Prev when the cursor already points to the first element](https://github.com/etcd-io/bbolt/pull/744)\n\n### Other\n- [Bump go version to 1.21.9](https://github.com/etcd-io/bbolt/pull/713)\n\n<hr>\n\n## v1.3.9(2024-02-24)\n\n### BoltDB\n- [Clone the key before operating data in bucket against the key](https://github.com/etcd-io/bbolt/pull/639)\n\n### CMD\n- [Fix `bbolt keys` and `bbolt get` to prevent them from panicking when no parameter provided](https://github.com/etcd-io/bbolt/pull/683)\n\n<hr>\n\n## v1.3.8(2023-10-26)\n\n### BoltDB\n- Fix [db.close() doesn't unlock the db file if db.munnmap() fails](https://github.com/etcd-io/bbolt/pull/439).\n- [Avoid syscall.Syscall use on OpenBSD](https://github.com/etcd-io/bbolt/pull/406).\n- Fix [rollback panicking after mlock failed or both meta pages corrupted](https://github.com/etcd-io/bbolt/pull/444).\n- Fix [bbolt panicking due to 64bit unaligned on arm32](https://github.com/etcd-io/bbolt/pull/584).\n\n### CMD\n- [Update the usage of surgery command](https://github.com/etcd-io/bbolt/pull/411).\n\n<hr>\n\n## v1.3.7(2023-01-31)\n\n### BoltDB\n- Add [recursive checker to confirm database consistency](https://github.com/etcd-io/bbolt/pull/225).\n- Add [support to get the page size from the second meta page if the first one is invalid](https://github.com/etcd-io/bbolt/pull/294).\n- Add [support for loong64 arch](https://github.com/etcd-io/bbolt/pull/303).\n- Add [internal iterator to Bucket that goes over buckets](https://github.com/etcd-io/bbolt/pull/356).\n- Add [validation on page read and write](https://github.com/etcd-io/bbolt/pull/358).\n- Add [PreLoadFreelist option to support loading free pages in readonly mode](https://github.com/etcd-io/bbolt/pull/381).\n- Add [(*Tx) CheckWithOption to support generating human-readable diagnostic messages](https://github.com/etcd-io/bbolt/pull/395).\n- Fix [Use `golang.org/x/sys/windows` for `FileLockEx`/`UnlockFileEx`](https://github.com/etcd-io/bbolt/pull/283).\n- Fix [readonly file mapping on windows](https://github.com/etcd-io/bbolt/pull/307).\n- Fix [the \"Last\" method might return no data due to not skipping the empty pages](https://github.com/etcd-io/bbolt/pull/341).\n- Fix [panic on db.meta when rollback](https://github.com/etcd-io/bbolt/pull/362).\n\n### CMD\n- Add [support for get keys in sub buckets in `bbolt get` command](https://github.com/etcd-io/bbolt/pull/295).\n- Add [support for `--format` flag for `bbolt keys` command](https://github.com/etcd-io/bbolt/pull/306).\n- Add [safeguards to bbolt CLI commands](https://github.com/etcd-io/bbolt/pull/354).\n- Add [`bbolt page` supports --all and --value-format=redacted formats](https://github.com/etcd-io/bbolt/pull/359).\n- Add [`bbolt surgery` commands](https://github.com/etcd-io/bbolt/issues/370).\n- Fix [open db file readonly mode for commands which shouldn't update the db file](https://github.com/etcd-io/bbolt/pull/365), see also [pull/292](https://github.com/etcd-io/bbolt/pull/292).\n\n### Other\n- [Build bbolt CLI tool, test and format the source code using golang 1.17.13](https://github.com/etcd-io/bbolt/pull/297).\n- [Bump golang.org/x/sys to v0.4.0](https://github.com/etcd-io/bbolt/pull/397).\n\n### Summary\nRelease v1.3.7 contains following critical fixes:\n- fix to problem that `Last` method might return incorrect value ([#341](https://github.com/etcd-io/bbolt/pull/341))\n- fix of potential panic when performing transaction's rollback ([#362](https://github.com/etcd-io/bbolt/pull/362))\n\nOther changes focused on defense-in-depth ([#358](https://github.com/etcd-io/bbolt/pull/358), [#294](https://github.com/etcd-io/bbolt/pull/294), [#225](https://github.com/etcd-io/bbolt/pull/225), [#395](https://github.com/etcd-io/bbolt/pull/395))\n\n`bbolt` command line tool was expanded to:\n- allow fixing simple corruptions by `bbolt surgery` ([#370](https://github.com/etcd-io/bbolt/pull/370))\n- be flexible about output formatting ([#306](https://github.com/etcd-io/bbolt/pull/306), [#359](https://github.com/etcd-io/bbolt/pull/359))\n- allow accessing data in subbuckets ([#295](https://github.com/etcd-io/bbolt/pull/295))\n"
  },
  {
    "path": "CHANGELOG/CHANGELOG-1.4.md",
    "content": "\n<hr>\n\n## v1.4.3(2025-08-19)\n\n### BoltDB\n- Fix [potential data corruption in `(*Tx)WriteTo` if underlying db file is overwritten](https://github.com/etcd-io/bbolt/pull/1058)\n\n<hr>\n\n## v1.4.2(2025-06-27)\n\n### BoltDB\n- [Fix the compilation issue on aix, android and solaris due to wrong use of `maxMapSize`](https://github.com/etcd-io/bbolt/pull/990)\n- [Add protection on meta page when it's being written](https://github.com/etcd-io/bbolt/pull/1005)\n\n<hr>\n\n## v1.4.1(2025-06-10)\n\n### BoltDB\n- [Correct the incorrect usage of debug method](https://github.com/etcd-io/bbolt/pull/905)\n- [Add clarification on the option `InitialMmapSize`](https://github.com/etcd-io/bbolt/pull/943)\n- [Fix the crash when writing huge values](https://github.com/etcd-io/bbolt/pull/978)\n\n<hr>\n\n## v1.4.0(2025-02-05)\nThere isn't any production code change since v1.4.0-beta.0. Only some dependencies\nare bumped, also updated some typos in comment and readme, and removed the legacy\nbuild tag `// +build` in https://github.com/etcd-io/bbolt/pull/879.\n\n<hr>\n\n## v1.4.0-beta.0(2024-11-04)\n\n### BoltDB\n- Reorganized the directory structure of freelist source code\n  - [Move array related freelist source code into a separate file](https://github.com/etcd-io/bbolt/pull/777)\n  - [Move method `freePages` into freelist.go](https://github.com/etcd-io/bbolt/pull/783)\n  - [Add an interface for freelist](https://github.com/etcd-io/bbolt/pull/775)\n- [Rollback alloc map when a transaction is rollbacked](https://github.com/etcd-io/bbolt/pull/819)\n- [No handling freelist as a special case when freeing a page](https://github.com/etcd-io/bbolt/pull/788)\n- [Ensure hashmap init method clears the data structures](https://github.com/etcd-io/bbolt/pull/794)\n- [Panicking when a write transaction tries to free a page allocated by itself](https://github.com/etcd-io/bbolt/pull/792)\n\n### CMD\n- [Add `-gobench-output` flag for `bbolt bench` command](https://github.com/etcd-io/bbolt/pull/765)\n\n### Other\n- [Bump go version to 1.23.x](https://github.com/etcd-io/bbolt/pull/821)\n\n<hr>\n\n## v1.4.0-alpha.1(2024-05-06)\n\n### BoltDB\n- [Enhance check functionality to support checking starting from a pageId](https://github.com/etcd-io/bbolt/pull/659)\n- [Optimize the logger performance for frequent called methods](https://github.com/etcd-io/bbolt/pull/741)\n- [Stabilize the behaviour of Prev when the cursor already points to the first element](https://github.com/etcd-io/bbolt/pull/734)\n\n### CMD\n- [Fix `bbolt keys` and `bbolt get` to prevent them from panicking when no parameter provided](https://github.com/etcd-io/bbolt/pull/682)\n- [Fix surgery freelist command in info logs](https://github.com/etcd-io/bbolt/pull/700)\n- [Remove txid references in surgery meta command's comment and description](https://github.com/etcd-io/bbolt/pull/703)\n- [Add rnd read capabilities to bbolt bench](https://github.com/etcd-io/bbolt/pull/711)\n- [Use `cobra.ExactArgs` to simplify the argument number check](https://github.com/etcd-io/bbolt/pull/728)\n- [Migrate `bbolt check` command to cobra style](https://github.com/etcd-io/bbolt/pull/723)\n- [Simplify the naming of cobra commands](https://github.com/etcd-io/bbolt/pull/732)\n- [Aggregate adding completed ops for read test of the `bbolt bench` command](https://github.com/etcd-io/bbolt/pull/721)\n- [Add `--from-page` flag to `bbolt check` command](https://github.com/etcd-io/bbolt/pull/737)\n\n### Document\n- [Add document for a known issue on the writing a value with a length of 0](https://github.com/etcd-io/bbolt/pull/730)\n\n### Test\n- [Enhance robustness test to cover XFS](https://github.com/etcd-io/bbolt/pull/707)\n\n### Other\n- [Bump go toolchain version to 1.22.2](https://github.com/etcd-io/bbolt/pull/712)\n\n<hr>\n\n## v1.4.0-alpha.0(2024-01-12)\n\n### BoltDB\n- [Improve the performance of hashmapGetFreePageIDs](https://github.com/etcd-io/bbolt/pull/419)\n- [Improve CreateBucketIfNotExists to avoid double searching the same key](https://github.com/etcd-io/bbolt/pull/532)\n- [Support Android platform](https://github.com/etcd-io/bbolt/pull/571)\n- [Record the count of free page to improve the performance of hashmapFreeCount](https://github.com/etcd-io/bbolt/pull/585)\n- [Add logger to bbolt](https://github.com/etcd-io/bbolt/issues/509)\n- [Support moving bucket inside the same db](https://github.com/etcd-io/bbolt/pull/635)\n- [Support inspecting database structure](https://github.com/etcd-io/bbolt/pull/674)\n\n### CMD\n- [Add `surgery clear-page-elements` command](https://github.com/etcd-io/bbolt/pull/417)\n- [Add `surgery abandon-freelist` command](https://github.com/etcd-io/bbolt/pull/443)\n- [Add `bbolt version` command](https://github.com/etcd-io/bbolt/pull/552)\n- [Add `bbolt inspect` command](https://github.com/etcd-io/bbolt/pull/674)\n- [Add `--no-sync` option to `bbolt compact` command](https://github.com/etcd-io/bbolt/pull/290)\n"
  },
  {
    "path": "CHANGELOG/CHANGELOG-1.5.md",
    "content": "<hr>\n\n## v1.5.0(TBD)\n\n### BoltDB\n- [Add support for data file size limit](https://github.com/etcd-io/bbolt/pull/929)\n- [Remove the unused txs list](https://github.com/etcd-io/bbolt/pull/973)\n- [Add option `NoStatistics` to make the statistics optional](https://github.com/etcd-io/bbolt/pull/977)\n- [Move panic handling from goroutine to the parent function](https://github.com/etcd-io/bbolt/pull/1153)\n- [Recover from panics in tx.check](https://github.com/etcd-io/bbolt/pull/1164)\n\n<hr>"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2013 Ben Johnson\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "BRANCH=`git rev-parse --abbrev-ref HEAD`\nCOMMIT=`git rev-parse --short HEAD`\nGOLDFLAGS=\"-X main.branch $(BRANCH) -X main.commit $(COMMIT)\"\nGOFILES = $(shell find . -name \\*.go)\n\nTESTFLAGS_RACE=-race=false\nifdef ENABLE_RACE\n\tTESTFLAGS_RACE=-race=true\nendif\n\nTESTFLAGS_CPU=\nifdef CPU\n\tTESTFLAGS_CPU=-cpu=$(CPU)\nendif\nTESTFLAGS = $(TESTFLAGS_RACE) $(TESTFLAGS_CPU) $(EXTRA_TESTFLAGS)\n\nTESTFLAGS_TIMEOUT=30m\nifdef TIMEOUT\n\tTESTFLAGS_TIMEOUT=$(TIMEOUT)\nendif\n\nTESTFLAGS_ENABLE_STRICT_MODE=false\nifdef ENABLE_STRICT_MODE\n\tTESTFLAGS_ENABLE_STRICT_MODE=$(ENABLE_STRICT_MODE)\nendif\n\n.EXPORT_ALL_VARIABLES:\nTEST_ENABLE_STRICT_MODE=${TESTFLAGS_ENABLE_STRICT_MODE}\n\n.PHONY: fmt\nfmt:\n\t@echo \"Verifying gofmt, failures can be fixed with ./scripts/fix.sh\"\n\t@!(gofmt -l -s -d ${GOFILES} | grep '[a-z]')\n\n\t@echo \"Verifying goimports, failures can be fixed with ./scripts/fix.sh\"\n\t@!(go tool golang.org/x/tools/cmd/goimports -l -d ${GOFILES} | grep '[a-z]')\n\n.PHONY: lint\nlint:\n\tgolangci-lint run ./...\n\n.PHONY: test\ntest:\n\t@echo \"hashmap freelist test\"\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=hashmap go test -v ${TESTFLAGS} -timeout ${TESTFLAGS_TIMEOUT}\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=hashmap go test -v ${TESTFLAGS} ./internal/...\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=hashmap go test -v ${TESTFLAGS} ./cmd/bbolt/...\n\n\t@echo \"array freelist test\"\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=array go test -v ${TESTFLAGS} -timeout ${TESTFLAGS_TIMEOUT}\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=array go test -v ${TESTFLAGS} ./internal/...\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=array go test -v ${TESTFLAGS} ./cmd/bbolt/...\n\n.PHONY: coverage\ncoverage:\n\t@echo \"hashmap freelist test\"\n\tTEST_FREELIST_TYPE=hashmap go test -v -timeout ${TESTFLAGS_TIMEOUT} \\\n\t\t-coverprofile cover-freelist-hashmap.out -covermode atomic\n\n\t@echo \"array freelist test\"\n\tTEST_FREELIST_TYPE=array go test -v -timeout ${TESTFLAGS_TIMEOUT} \\\n\t\t-coverprofile cover-freelist-array.out -covermode atomic\n\nBOLT_CMD=bbolt\n\nbuild:\n\tgo build -o bin/${BOLT_CMD} ./cmd/${BOLT_CMD}\n\n.PHONY: clean\nclean: # Clean binaries\n\trm -f ./bin/${BOLT_CMD}\n\n.PHONY: gofail-enable\ngofail-enable:\n\tgo tool go.etcd.io/gofail enable .\n\n.PHONY: gofail-disable\ngofail-disable:\n\tgo tool go.etcd.io/gofail disable .\n\n.PHONY: test-failpoint\ntest-failpoint:\n\t@echo \"[failpoint] hashmap freelist test\"\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=hashmap go test -v ${TESTFLAGS} -timeout 30m ./tests/failpoint\n\n\t@echo \"[failpoint] array freelist test\"\n\tBBOLT_VERIFY=all TEST_FREELIST_TYPE=array go test -v ${TESTFLAGS} -timeout 30m ./tests/failpoint\n\n.PHONY: test-robustness # Running robustness tests requires root permission for now\n# TODO: Remove sudo once we fully migrate to the prow infrastructure\ntest-robustness: gofail-enable build\n\tsudo env PATH=$$PATH go test -v ${TESTFLAGS} ./tests/dmflakey -test.root\n\tsudo env PATH=$(PWD)/bin:$$PATH go test -v ${TESTFLAGS} ${ROBUSTNESS_TESTFLAGS} ./tests/robustness -test.root\n\n.PHONY: test-benchmark-compare\n# Runs benchmark tests on the current git ref and the given REF, and compares\n# the two.\ntest-benchmark-compare:\n\t@git fetch\n\t./scripts/compare_benchmarks.sh $(REF)\n"
  },
  {
    "path": "OWNERS",
    "content": "# See the OWNERS docs at https://go.k8s.io/owners\n\napprovers:\n  - ahrtr           # Benjamin Wang <benjamin.ahrtr@gmail.com> <benjamin.wang@broadcom.com>\n  - serathius       # Marek Siarkowicz <siarkowicz@google.com> <marek.siarkowicz@gmail.com>\n  - ptabor          # Piotr Tabor <piotr.tabor@gmail.com>\n  - spzala          # Sahdev Zala <spzala@us.ibm.com>\nreviewers:\n  - elbehery        # Mustafa Elbehery <elbeherymustafa@gmail.com>\n  - fuweid          # Wei Fu <fuweid89@gmail.com>\n  - tjungblu        # Thomas Jungblut <tjungblu@redhat.com>\n"
  },
  {
    "path": "README.md",
    "content": "bbolt\n=====\n\n[![Go Report Card](https://goreportcard.com/badge/go.etcd.io/bbolt?style=flat-square)](https://goreportcard.com/report/go.etcd.io/bbolt)\n[![Go Reference](https://pkg.go.dev/badge/go.etcd.io/bbolt.svg)](https://pkg.go.dev/go.etcd.io/bbolt)\n[![Releases](https://img.shields.io/github/release/etcd-io/bbolt/all.svg?style=flat-square)](https://github.com/etcd-io/bbolt/releases)\n[![LICENSE](https://img.shields.io/github/license/etcd-io/bbolt.svg?style=flat-square)](https://github.com/etcd-io/bbolt/blob/master/LICENSE)\n\nbbolt is a fork of [Ben Johnson's][gh_ben] [Bolt][bolt] key/value\nstore. The purpose of this fork is to provide the Go community with an active\nmaintenance and development target for Bolt; the goal is improved reliability\nand stability. bbolt includes bug fixes, performance enhancements, and features\nnot found in Bolt while preserving backwards compatibility with the Bolt API.\n\nBolt is a pure Go key/value store inspired by [Howard Chu's][hyc_symas]\n[LMDB project][lmdb]. The goal of the project is to provide a simple,\nfast, and reliable database for projects that don't require a full database\nserver such as Postgres or MySQL.\n\nSince Bolt is meant to be used as such a low-level piece of functionality,\nsimplicity is key. The API will be small and only focus on getting values\nand setting values. That's it.\n\n[gh_ben]: https://github.com/benbjohnson\n[bolt]: https://github.com/boltdb/bolt\n[hyc_symas]: https://twitter.com/hyc_symas\n[lmdb]: https://www.symas.com/symas-embedded-database-lmdb\n\n## Project Status\n\nBolt is stable, the API is fixed, and the file format is fixed. Full unit\ntest coverage and randomized black box testing are used to ensure database\nconsistency and thread safety. Bolt is currently used in high-load production\nenvironments serving databases as large as 1TB. Many companies such as\nShopify and Heroku use Bolt-backed services every day.\n\n## Project versioning\n\nbbolt uses [semantic versioning](http://semver.org).\nAPI should not change between patch and minor releases.\nNew minor versions may add additional features to the API.\n\n## Table of Contents\n\n  - [Getting Started](#getting-started)\n    - [Installing](#installing)\n    - [Opening a database](#opening-a-database)\n    - [Transactions](#transactions)\n      - [Read-write transactions](#read-write-transactions)\n      - [Read-only transactions](#read-only-transactions)\n      - [Batch read-write transactions](#batch-read-write-transactions)\n      - [Managing transactions manually](#managing-transactions-manually)\n    - [Using buckets](#using-buckets)\n    - [Using key/value pairs](#using-keyvalue-pairs)\n    - [Autoincrementing integer for the bucket](#autoincrementing-integer-for-the-bucket)\n    - [Iterating over keys](#iterating-over-keys)\n      - [Prefix scans](#prefix-scans)\n      - [Range scans](#range-scans)\n      - [ForEach()](#foreach)\n    - [Nested buckets](#nested-buckets)\n    - [Database backups](#database-backups)\n    - [Statistics](#statistics)\n    - [Read-Only Mode](#read-only-mode)\n    - [Mobile Use (iOS/Android)](#mobile-use-iosandroid)\n  - [Resources](#resources)\n  - [Comparison with other databases](#comparison-with-other-databases)\n    - [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)\n    - [LevelDB, RocksDB](#leveldb-rocksdb)\n    - [LMDB](#lmdb)\n  - [Caveats & Limitations](#caveats--limitations)\n  - [Reading the Source](#reading-the-source)\n  - [Known Issues](#known-issues)\n  - [Other Projects Using Bolt](#other-projects-using-bolt)\n\n## Getting Started\n\n### Installing\n\nTo start using `bbolt`, install Go and run `go get`:\n```sh\n$ go get go.etcd.io/bbolt@latest\n```\n\nThis will retrieve the library and update your `go.mod` and `go.sum` files.\n\nTo run the command line utility, execute:\n```sh\n$ go run go.etcd.io/bbolt/cmd/bbolt@latest\n```\n\nRun `go install` to install the `bbolt` command line utility into\nyour `$GOBIN` path, which defaults to `$GOPATH/bin` or `$HOME/go/bin` if the\n`GOPATH` environment variable is not set.\n```sh\n$ go install go.etcd.io/bbolt/cmd/bbolt@latest\n```\n\n### Importing bbolt\n\nTo use bbolt as an embedded key-value store, import as:\n\n```go\nimport bolt \"go.etcd.io/bbolt\"\n\ndb, err := bolt.Open(path, 0600, nil)\nif err != nil {\n  return err\n}\ndefer db.Close()\n```\n\n\n### Opening a database\n\nThe top-level object in Bolt is a `DB`. It is represented as a single file on\nyour disk and represents a consistent snapshot of your data.\n\nTo open your database, simply use the `bolt.Open()` function:\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nfunc main() {\n\t// Open the my.db data file in your current directory.\n\t// It will be created if it doesn't exist.\n\tdb, err := bolt.Open(\"my.db\", 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer db.Close()\n\n\t...\n}\n```\n\nPlease note that Bolt obtains a file lock on the data file so multiple processes\ncannot open the same database at the same time. Opening an already open Bolt\ndatabase will cause it to hang until the other process closes it. To prevent\nan indefinite wait you can pass a timeout option to the `Open()` function:\n\n```go\ndb, err := bolt.Open(\"my.db\", 0600, &bolt.Options{Timeout: 1 * time.Second})\n```\n\n\n### Transactions\n\nBolt allows only one read-write transaction at a time but allows as many\nread-only transactions as you want at a time. Each transaction has a consistent\nview of the data as it existed when the transaction started.\n\nIndividual transactions and all objects created from them (e.g. buckets, keys)\nare not thread safe. To work with data in multiple goroutines you must start\na transaction for each one or use locking to ensure only one goroutine accesses\na transaction at a time. Creating transaction from the `DB` is thread safe.\n\nTransactions should not depend on one another and generally shouldn't be opened\nsimultaneously in the same goroutine. This can cause a deadlock as the read-write\ntransaction needs to periodically re-map the data file but it cannot do so while\nany read-only transaction is open. Even a nested read-only transaction can cause\na deadlock, as the child transaction can block the parent transaction from releasing\nits resources.\n\n#### Read-write transactions\n\nTo start a read-write transaction, you can use the `DB.Update()` function:\n\n```go\nerr := db.Update(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\n```\n\nInside the closure, you have a consistent view of the database. You commit the\ntransaction by returning `nil` at the end. You can also rollback the transaction\nat any point by returning an error. All database operations are allowed inside\na read-write transaction.\n\nAlways check the return error as it will report any disk failures that can cause\nyour transaction to not complete. If you return an error within your closure\nit will be passed through.\n\n\n#### Read-only transactions\n\nTo start a read-only transaction, you can use the `DB.View()` function:\n\n```go\nerr := db.View(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\n```\n\nYou also get a consistent view of the database within this closure, however,\nno mutating operations are allowed within a read-only transaction. You can only\nretrieve buckets, retrieve values, and copy the database within a read-only\ntransaction.\n\n\n#### Batch read-write transactions\n\nEach `DB.Update()` waits for disk to commit the writes. This overhead\ncan be minimized by combining multiple updates with the `DB.Batch()`\nfunction:\n\n```go\nerr := db.Batch(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\n```\n\nConcurrent Batch calls are opportunistically combined into larger\ntransactions. Batch is only useful when there are multiple goroutines\ncalling it.\n\nThe trade-off is that `Batch` can call the given\nfunction multiple times, if parts of the transaction fail. The\nfunction must be idempotent and side effects must take effect only\nafter a successful return from `DB.Batch()`.\n\nFor example: don't display messages from inside the function, instead\nset variables in the enclosing scope:\n\n```go\nvar id uint64\nerr := db.Batch(func(tx *bolt.Tx) error {\n\t// Find last key in bucket, decode as bigendian uint64, increment\n\t// by one, encode back to []byte, and add new key.\n\t...\n\tid = newValue\n\treturn nil\n})\nif err != nil {\n\treturn ...\n}\nfmt.Println(\"Allocated ID %d\", id)\n```\n\n\n#### Managing transactions manually\n\nThe `DB.View()` and `DB.Update()` functions are wrappers around the `DB.Begin()`\nfunction. These helper functions will start the transaction, execute a function,\nand then safely close your transaction if an error is returned. This is the\nrecommended way to use Bolt transactions.\n\nHowever, sometimes you may want to manually start and end your transactions.\nYou can use the `DB.Begin()` function directly but **please** be sure to close\nthe transaction.\n\n```go\n// Start a writable transaction.\ntx, err := db.Begin(true)\nif err != nil {\n    return err\n}\ndefer tx.Rollback()\n\n// Use the transaction...\n_, err := tx.CreateBucket([]byte(\"MyBucket\"))\nif err != nil {\n    return err\n}\n\n// Commit the transaction and check for error.\nif err := tx.Commit(); err != nil {\n    return err\n}\n```\n\nThe first argument to `DB.Begin()` is a boolean stating if the transaction\nshould be writable.\n\n\n### Using buckets\n\nBuckets are collections of key/value pairs within the database. All keys in a\nbucket must be unique. You can create a bucket using the `Tx.CreateBucket()`\nfunction:\n\n```go\ndb.Update(func(tx *bolt.Tx) error {\n\tb, err := tx.CreateBucket([]byte(\"MyBucket\"))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create bucket: %s\", err)\n\t}\n\treturn nil\n})\n```\n\nYou can retrieve an existing bucket using the `Tx.Bucket()` function:\n```go\ndb.Update(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\tif b == nil {\n\t\treturn errors.New(\"bucket does not exist\")\n\t}\n\treturn nil\n})\n```\n\nYou can also create a bucket only if it doesn't exist by using the\n`Tx.CreateBucketIfNotExists()` function. It's a common pattern to call this\nfunction for all your top-level buckets after you open your database so you can\nguarantee that they exist for future transactions.\n\nTo delete a bucket, simply call the `Tx.DeleteBucket()` function.\n\nYou can also iterate over all existing top-level buckets with `Tx.ForEach()`:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\ttx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\tfmt.Println(string(name))\n\t\treturn nil\n\t})\n\treturn nil\n})\n```\n\n### Using key/value pairs\n\nTo save a key/value pair to a bucket, use the `Bucket.Put()` function:\n\n```go\ndb.Update(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\terr := b.Put([]byte(\"answer\"), []byte(\"42\"))\n\treturn err\n})\n```\n\nThis will set the value of the `\"answer\"` key to `\"42\"` in the `MyBucket`\nbucket. To retrieve this value, we can use the `Bucket.Get()` function:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\tv := b.Get([]byte(\"answer\"))\n\tfmt.Printf(\"The answer is: %s\\n\", v)\n\treturn nil\n})\n```\n\nThe `Get()` function does not return an error because its operation is\nguaranteed to work (unless there is some kind of system failure). If the key\nexists then it will return its byte slice value. If it doesn't exist then it\nwill return `nil`. It's important to note that you can have a zero-length value\nset to a key which is different than the key not existing.\n\nUse the `Bucket.Delete()` function to delete a key from the bucket:\n\n```go\ndb.Update(func (tx *bolt.Tx) error {\n    b := tx.Bucket([]byte(\"MyBucket\"))\n    err := b.Delete([]byte(\"answer\"))\n    return err\n})\n```\n\nThis will delete the key `answers` from the bucket `MyBucket`.\n\nPlease note that values returned from `Get()` are only valid while the\ntransaction is open. If you need to use a value outside of the transaction\nthen you must use `copy()` to copy it to another byte slice.\n\n\n### Autoincrementing integer for the bucket\nBy using the `NextSequence()` function, you can let Bolt determine a sequence\nwhich can be used as the unique identifier for your key/value pairs. See the\nexample below.\n\n```go\n// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.\nfunc (s *Store) CreateUser(u *User) error {\n    return s.db.Update(func(tx *bolt.Tx) error {\n        // Retrieve the users bucket.\n        // This should be created when the DB is first opened.\n        b := tx.Bucket([]byte(\"users\"))\n\n        // Generate ID for the user.\n        // This returns an error only if the Tx is closed or not writeable.\n        // That can't happen in an Update() call so I ignore the error check.\n        id, _ := b.NextSequence()\n        u.ID = int(id)\n\n        // Marshal user data into bytes.\n        buf, err := json.Marshal(u)\n        if err != nil {\n            return err\n        }\n\n        // Persist bytes to users bucket.\n        return b.Put(itob(u.ID), buf)\n    })\n}\n\n// itob returns an 8-byte big endian representation of v.\nfunc itob(v int) []byte {\n    b := make([]byte, 8)\n    binary.BigEndian.PutUint64(b, uint64(v))\n    return b\n}\n\ntype User struct {\n    ID int\n    ...\n}\n```\n\n### Iterating over keys\n\nBolt stores its keys in byte-sorted order within a bucket. This makes sequential\niteration over these keys extremely fast. To iterate over keys we'll use a\n`Cursor`:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\n\tc := b.Cursor()\n\n\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\tfmt.Printf(\"key=%s, value=%s\\n\", k, v)\n\t}\n\n\treturn nil\n})\n```\n\nThe cursor allows you to move to a specific point in the list of keys and move\nforward or backward through the keys one at a time.\n\nThe following functions are available on the cursor:\n\n```\nFirst()  Move to the first key.\nLast()   Move to the last key.\nSeek()   Move to a specific key.\nNext()   Move to the next key.\nPrev()   Move to the previous key.\n```\n\nEach of those functions has a return signature of `(key []byte, value []byte)`.\nYou must seek to a position using `First()`, `Last()`, or `Seek()` before calling\n`Next()` or `Prev()`. If you do not seek to a position then these functions will\nreturn a `nil` key.\n\nWhen you have iterated to the end of the cursor, then `Next()` will return a\n`nil` key and the cursor still points to the last element if present. When you\nhave iterated to the beginning of the cursor, then `Prev()` will return a `nil`\nkey and the cursor still points to the first element if present.\n\nIf you remove key/value pairs during iteration, the cursor may automatically\nmove to the next position if present in current node each time removing a key.\nWhen you call `c.Next()` after removing a key, it may skip one key/value pair.\nRefer to [pull/611](https://github.com/etcd-io/bbolt/pull/611) to get more detailed info.\n\nDuring iteration, if the key is non-`nil` but the value is `nil`, that means\nthe key refers to a bucket rather than a value.  Use `Bucket.Bucket()` to\naccess the sub-bucket.\n\n\n#### Prefix scans\n\nTo iterate over a key prefix, you can combine `Seek()` and `bytes.HasPrefix()`:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tc := tx.Bucket([]byte(\"MyBucket\")).Cursor()\n\n\tprefix := []byte(\"1234\")\n\tfor k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {\n\t\tfmt.Printf(\"key=%s, value=%s\\n\", k, v)\n\t}\n\n\treturn nil\n})\n```\n\n#### Range scans\n\nAnother common use case is scanning over a range such as a time range. If you\nuse a sortable time encoding such as RFC3339 then you can query a specific\ndate range like this:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume our events bucket exists and has RFC3339 encoded time keys.\n\tc := tx.Bucket([]byte(\"Events\")).Cursor()\n\n\t// Our time range spans the 90's decade.\n\tmin := []byte(\"1990-01-01T00:00:00Z\")\n\tmax := []byte(\"2000-01-01T00:00:00Z\")\n\n\t// Iterate over the 90's.\n\tfor k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {\n\t\tfmt.Printf(\"%s: %s\\n\", k, v)\n\t}\n\n\treturn nil\n})\n```\n\nNote that, while RFC3339 is sortable, the Golang implementation of RFC3339Nano does not use a fixed number of digits after the decimal point and is therefore not sortable.\n\n\n#### ForEach()\n\nYou can also use the function `ForEach()` if you know you'll be iterating over\nall the keys in a bucket:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\n\tb.ForEach(func(k, v []byte) error {\n\t\tfmt.Printf(\"key=%s, value=%s\\n\", k, v)\n\t\treturn nil\n\t})\n\treturn nil\n})\n```\n\nPlease note that keys and values in `ForEach()` are only valid while\nthe transaction is open. If you need to use a key or value outside of\nthe transaction, you must use `copy()` to copy it to another byte\nslice.\n\n### Nested buckets\n\nYou can also store a bucket in a key to create nested buckets. The API is the\nsame as the bucket management API on the `DB` object:\n\n```go\nfunc (*Bucket) CreateBucket(key []byte) (*Bucket, error)\nfunc (*Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)\nfunc (*Bucket) DeleteBucket(key []byte) error\n```\n\nSay you had a multi-tenant application where the root level bucket was the account bucket. Inside of this bucket was a sequence of accounts which themselves are buckets. And inside the sequence bucket you could have many buckets pertaining to the Account itself (Users, Notes, etc) isolating the information into logical groupings.\n\n```go\n\n// createUser creates a new user in the given account.\nfunc createUser(accountID int, u *User) error {\n    // Start the transaction.\n    tx, err := db.Begin(true)\n    if err != nil {\n        return err\n    }\n    defer tx.Rollback()\n\n    // Retrieve the root bucket for the account.\n    // Assume this has already been created when the account was set up.\n    root := tx.Bucket([]byte(strconv.FormatUint(accountID, 10)))\n\n    // Setup the users bucket.\n    bkt, err := root.CreateBucketIfNotExists([]byte(\"USERS\"))\n    if err != nil {\n        return err\n    }\n\n    // Generate an ID for the new user.\n    userID, err := bkt.NextSequence()\n    if err != nil {\n        return err\n    }\n    u.ID = userID\n\n    // Marshal and save the encoded user.\n    if buf, err := json.Marshal(u); err != nil {\n        return err\n    } else if err := bkt.Put([]byte(strconv.FormatUint(u.ID, 10)), buf); err != nil {\n        return err\n    }\n\n    // Commit the transaction.\n    if err := tx.Commit(); err != nil {\n        return err\n    }\n\n    return nil\n}\n\n```\n\n\n\n\n### Database backups\n\nBolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()`\nfunction to write a consistent view of the database to a writer. If you call\nthis from a read-only transaction, it will perform a hot backup and not block\nyour other database reads and writes.\n\nBy default, it will use a regular file handle which will utilize the operating\nsystem's page cache. See the [`Tx`](https://godoc.org/go.etcd.io/bbolt#Tx)\ndocumentation for information about optimizing for larger-than-RAM datasets.\n\nOne common use case is to backup over HTTP so you can use tools like `cURL` to\ndo database backups:\n\n```go\nfunc BackupHandleFunc(w http.ResponseWriter, req *http.Request) {\n\terr := db.View(func(tx *bolt.Tx) error {\n\t\tw.Header().Set(\"Content-Type\", \"application/octet-stream\")\n\t\tw.Header().Set(\"Content-Disposition\", `attachment; filename=\"my.db\"`)\n\t\tw.Header().Set(\"Content-Length\", strconv.Itoa(int(tx.Size())))\n\t\t_, err := tx.WriteTo(w)\n\t\treturn err\n\t})\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t}\n}\n```\n\nThen you can backup using this command:\n\n```sh\n$ curl http://localhost/backup > my.db\n```\n\nOr you can open your browser to `http://localhost/backup` and it will download\nautomatically.\n\nIf you want to backup to another file you can use the `Tx.CopyFile()` helper\nfunction.\n\n\n### Statistics\n\nThe database keeps a running count of many of the internal operations it\nperforms so you can better understand what's going on. By grabbing a snapshot\nof these stats at two points in time we can see what operations were performed\nin that time range.\n\nFor example, we could start a goroutine to log stats every 10 seconds:\n\n```go\ngo func() {\n\t// Grab the initial stats.\n\tprev := db.Stats()\n\n\tfor {\n\t\t// Wait for 10s.\n\t\ttime.Sleep(10 * time.Second)\n\n\t\t// Grab the current stats and diff them.\n\t\tstats := db.Stats()\n\t\tdiff := stats.Sub(&prev)\n\n\t\t// Encode stats to JSON and print to STDERR.\n\t\tjson.NewEncoder(os.Stderr).Encode(diff)\n\n\t\t// Save stats for the next loop.\n\t\tprev = stats\n\t}\n}()\n```\n\nIt's also useful to pipe these stats to a service such as statsd for monitoring\nor to provide an HTTP endpoint that will perform a fixed-length sample.\n\n\n### Read-Only Mode\n\nSometimes it is useful to create a shared, read-only Bolt database. To this,\nset the `Options.ReadOnly` flag when opening your database. Read-only mode\nuses a shared lock to allow multiple processes to read from the database but\nit will block any processes from opening the database in read-write mode.\n\n```go\ndb, err := bolt.Open(\"my.db\", 0600, &bolt.Options{ReadOnly: true})\nif err != nil {\n\tlog.Fatal(err)\n}\n```\n\n### Mobile Use (iOS/Android)\n\nBolt is able to run on mobile devices by leveraging the binding feature of the\n[gomobile](https://github.com/golang/mobile) tool. Create a struct that will\ncontain your database logic and a reference to a `*bolt.DB` with a initializing\nconstructor that takes in a filepath where the database file will be stored.\nNeither Android nor iOS require extra permissions or cleanup from using this method.\n\n```go\nfunc NewBoltDB(filepath string) *BoltDB {\n\tdb, err := bolt.Open(filepath+\"/demo.db\", 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\treturn &BoltDB{db}\n}\n\ntype BoltDB struct {\n\tdb *bolt.DB\n\t...\n}\n\nfunc (b *BoltDB) Path() string {\n\treturn b.db.Path()\n}\n\nfunc (b *BoltDB) Close() {\n\tb.db.Close()\n}\n```\n\nDatabase logic should be defined as methods on this wrapper struct.\n\nTo initialize this struct from the native language (both platforms now sync\ntheir local storage to the cloud. These snippets disable that functionality for the\ndatabase file):\n\n#### Android\n\n```java\nString path;\nif (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.LOLLIPOP){\n    path = getNoBackupFilesDir().getAbsolutePath();\n} else{\n    path = getFilesDir().getAbsolutePath();\n}\nBoltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path)\n```\n\n#### iOS\n\n```objc\n- (void)demo {\n    NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,\n                                                          NSUserDomainMask,\n                                                          YES) objectAtIndex:0];\n\tGoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path);\n\t[self addSkipBackupAttributeToItemAtPath:demo.path];\n\t//Some DB Logic would go here\n\t[demo close];\n}\n\n- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString\n{\n    NSURL* URL= [NSURL fileURLWithPath: filePathString];\n    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);\n\n    NSError *error = nil;\n    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]\n                                  forKey: NSURLIsExcludedFromBackupKey error: &error];\n    if(!success){\n        NSLog(@\"Error excluding %@ from backup %@\", [URL lastPathComponent], error);\n    }\n    return success;\n}\n\n```\n\n## Resources\n\nFor more information on getting started with Bolt, check out the following articles:\n\n* [Intro to BoltDB: Painless Performant Persistence](http://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/) by [Nate Finch](https://github.com/natefinch).\n* [Bolt -- an embedded key/value database for Go](https://www.progville.com/go/bolt-embedded-db-golang/) by Progville\n\n\n## Comparison with other databases\n\n### Postgres, MySQL, & other relational databases\n\nRelational databases structure data into rows and are only accessible through\nthe use of SQL. This approach provides flexibility in how you store and query\nyour data but also incurs overhead in parsing and planning SQL statements. Bolt\naccesses all data by a byte slice key. This makes Bolt fast to read and write\ndata by key but provides no built-in support for joining values together.\n\nMost relational databases (with the exception of SQLite) are standalone servers\nthat run separately from your application. This gives your systems\nflexibility to connect multiple application servers to a single database\nserver but also adds overhead in serializing and transporting data over the\nnetwork. Bolt runs as a library included in your application so all data access\nhas to go through your application's process. This brings data closer to your\napplication but limits multi-process access to the data.\n\n\n### LevelDB, RocksDB\n\nLevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that\nthey are libraries bundled into the application, however, their underlying\nstructure is a log-structured merge-tree (LSM tree). An LSM tree optimizes\nrandom writes by using a write ahead log and multi-tiered, sorted files called\nSSTables. Bolt uses a B+tree internally and only a single file. Both approaches\nhave trade-offs.\n\nIf you require a high random write throughput (>10,000 w/sec) or you need to use\nspinning disks then LevelDB could be a good choice. If your application is\nread-heavy or does a lot of range scans then Bolt could be a good choice.\n\nOne other important consideration is that LevelDB does not have transactions.\nIt supports batch writing of key/values pairs and it supports read snapshots\nbut it will not give you the ability to do a compare-and-swap operation safely.\nBolt supports fully serializable ACID transactions.\n\n\n### LMDB\n\nBolt was originally a port of LMDB so it is architecturally similar. Both use\na B+tree, have ACID semantics with fully serializable transactions, and support\nlock-free MVCC using a single writer and multiple readers.\n\nThe two projects have somewhat diverged. LMDB heavily focuses on raw performance\nwhile Bolt has focused on simplicity and ease of use. For example, LMDB allows\nseveral unsafe actions such as direct writes for the sake of performance. Bolt\nopts to disallow actions which can leave the database in a corrupted state. The\nonly exception to this in Bolt is `DB.NoSync`.\n\nThere are also a few differences in API. LMDB requires a maximum mmap size when\nopening an `mdb_env` whereas Bolt will handle incremental mmap resizing\nautomatically. LMDB overloads the getter and setter functions with multiple\nflags whereas Bolt splits these specialized cases into their own functions.\n\n\n## Caveats & Limitations\n\nIt's important to pick the right tool for the job and Bolt is no exception.\nHere are a few things to note when evaluating and using Bolt:\n\n* Bolt is good for read intensive workloads. Sequential write performance is\n  also fast but random writes can be slow. You can use `DB.Batch()` or add a\n  write-ahead log to help mitigate this issue.\n\n* Bolt uses a B+tree internally so there can be a lot of random page access.\n  SSDs provide a significant performance boost over spinning disks.\n\n* Try to avoid long running read transactions. Bolt uses copy-on-write so\n  old pages cannot be reclaimed while an old transaction is using them.\n\n* Byte slices returned from Bolt are only valid during a transaction. Once the\n  transaction has been committed or rolled back then the memory they point to\n  can be reused by a new page or can be unmapped from virtual memory and you'll\n  see an `unexpected fault address` panic when accessing it.\n\n* Bolt uses an exclusive write lock on the database file so it cannot be\n  shared by multiple processes.\n\n* Be careful when using `Bucket.FillPercent`. Setting a high fill percent for\n  buckets that have random inserts will cause your database to have very poor\n  page utilization.\n\n* Use larger buckets in general. Smaller buckets causes poor page utilization\n  once they become larger than the page size (typically 4KB).\n\n* Bulk loading a lot of random writes into a new bucket can be slow as the\n  page will not split until the transaction is committed. Randomly inserting\n  more than 100,000 key/value pairs into a single new bucket in a single\n  transaction is not advised.\n\n* Bolt uses a memory-mapped file so the underlying operating system handles the\n  caching of the data. Typically, the OS will cache as much of the file as it\n  can in memory and will release memory as needed to other processes. This means\n  that Bolt can show very high memory usage when working with large databases.\n  However, this is expected and the OS will release memory as needed. Bolt can\n  handle databases much larger than the available physical RAM, provided its\n  memory-map fits in the process virtual address space. It may be problematic\n  on 32-bits systems.\n\n* The data structures in the Bolt database are memory mapped so the data file\n  will be endian specific. This means that you cannot copy a Bolt file from a\n  little endian machine to a big endian machine and have it work. For most\n  users this is not a concern since most modern CPUs are little endian.\n\n* Because of the way pages are laid out on disk, Bolt cannot truncate data files\n  and return free pages back to the disk. Instead, Bolt maintains a free list\n  of unused pages within its data file. These free pages can be reused by later\n  transactions. This works well for many use cases as databases generally tend\n  to grow. However, it's important to note that deleting large chunks of data\n  will not allow you to reclaim that space on disk.\n\n  For more information on page allocation, [see this comment][page-allocation].\n\n* Removing key/values pairs in a bucket during iteration on the bucket using\n  cursor may not work properly. Each time when removing a key/value pair, the\n  cursor may automatically move to the next position if present. When users\n  call `c.Next()` after removing a key, it may skip one key/value pair.\n  Refer to https://github.com/etcd-io/bbolt/pull/611 for more detailed info.\n\n* Bolt db can be corrupted during the initialization phase due to abrupt power failure.\n  - Please note: This issue can only be reproduced during the very first initialization phase, when there is\n  no existing data in bolt database.\n  - In normal production environment, it is difficult to reproduce this. Once the database file has been initialized, it can no longer occur.\n  - Please refer to this issue for more details: https://github.com/etcd-io/etcd/issues/16596.\n\n[page-allocation]: https://github.com/boltdb/bolt/issues/308#issuecomment-74811638\n\n\n## Reading the Source\n\nBolt is a relatively small code base (<5KLOC) for an embedded, serializable,\ntransactional key/value database so it can be a good starting point for people\ninterested in how databases work.\n\nThe best places to start are the main entry points into Bolt:\n\n- `Open()` - Initializes the reference to the database. It's responsible for\n  creating the database if it doesn't exist, obtaining an exclusive lock on the\n  file, reading the meta pages, & memory-mapping the file.\n\n- `DB.Begin()` - Starts a read-only or read-write transaction depending on the\n  value of the `writable` argument. This requires briefly obtaining the \"meta\"\n  lock to keep track of open transactions. Only one read-write transaction can\n  exist at a time so the \"rwlock\" is acquired during the life of a read-write\n  transaction.\n\n- `Bucket.Put()` - Writes a key/value pair into a bucket. After validating the\n  arguments, a cursor is used to traverse the B+tree to the page and position\n  where the key & value will be written. Once the position is found, the bucket\n  materializes the underlying page and the page's parent pages into memory as\n  \"nodes\". These nodes are where mutations occur during read-write transactions.\n  These changes get flushed to disk during commit.\n\n- `Bucket.Get()` - Retrieves a key/value pair from a bucket. This uses a cursor\n  to move to the page & position of a key/value pair. During a read-only\n  transaction, the key and value data is returned as a direct reference to the\n  underlying mmap file so there's no allocation overhead. For read-write\n  transactions, this data may reference the mmap file or one of the in-memory\n  node values.\n\n- `Cursor` - This object is simply for traversing the B+tree of on-disk pages\n  or in-memory nodes. It can seek to a specific key, move to the first or last\n  value, or it can move forward or backward. The cursor handles the movement up\n  and down the B+tree transparently to the end user.\n\n- `Tx.Commit()` - Converts the in-memory dirty nodes and the list of free pages\n  into pages to be written to disk. Writing to disk then occurs in two phases.\n  First, the dirty pages are written to disk and an `fsync()` occurs. Second, a\n  new meta page with an incremented transaction ID is written and another\n  `fsync()` occurs. This two phase write ensures that partially written data\n  pages are ignored in the event of a crash since the meta page pointing to them\n  is never written. Partially written meta pages are invalidated because they\n  are written with a checksum.\n\nIf you have additional notes that could be helpful for others, please submit\nthem via pull request.\n\n## Known Issues\n\n- bbolt might run into data corruption issue on Linux when the feature\n  [ext4: fast commit](https://lwn.net/Articles/842385/), which was introduced in\n  linux kernel version v5.10, is enabled. The fixes to the issue are included in\n  stable LTS patchlevels 5.10.94+ and 5.15.17+ (ftruncate tracking), plus\n  5.15.27+ (ineligible-commit fallback). Linux 5.17 includes these fixes as\n  well, but 5.17 is not an LTS release. Please refer to links below,\n\n  * [ext4: fast commit may miss tracking unwritten range during ftruncate](https://lore.kernel.org/linux-ext4/20211223032337.5198-3-yinxin.x@bytedance.com/)\n    * [5.10.94 stable backport](https://lore.kernel.org/stable/20220124184041.063143682@linuxfoundation.org/)\n    * [5.15.17 stable backport](https://lore.kernel.org/stable/20220124184125.887304707@linuxfoundation.org/)\n  * [ext4: fast commit may not fallback for ineligible commit](https://lore.kernel.org/lkml/202201091544.W5HHEXAp-lkp@intel.com/T/#ma0768815e4b5f671e9e451d578256ef9a76fe30e)\n    * [5.15.27 stable backport](https://lore.kernel.org/stable/20220307091703.544901888@linuxfoundation.org/)\n  * [ext4 updates for 5.17](https://lore.kernel.org/lkml/YdyxjTFaLWif6BCM@mit.edu/)\n\n  Please also refer to the discussion in https://github.com/etcd-io/bbolt/issues/562.\n\n- Writing a value with a length of 0 will always result in reading back an empty `[]byte{}` value.\n  Please refer to [issues/726#issuecomment-2061694802](https://github.com/etcd-io/bbolt/issues/726#issuecomment-2061694802).\n\n## Other Projects Using Bolt\n\nBelow is a list of public, open source projects that use Bolt:\n\n* [Algernon](https://github.com/xyproto/algernon) - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.\n* [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside.\n* [bolter](https://github.com/hasit/bolter) - Command-line app for viewing BoltDB file in your terminal.\n* [boltcli](https://github.com/spacewander/boltcli) - the redis-cli for boltdb with Lua script support.\n* [BoltHold](https://github.com/timshannon/bolthold) - An embeddable NoSQL store for Go types built on BoltDB\n* [BoltStore](https://github.com/yosssi/boltstore) - Session store using Bolt.\n* [Boltdb Boilerplate](https://github.com/bobintornado/boltdb-boilerplate) - Boilerplate wrapper around bolt aiming to make simple calls one-liners.\n* [BoltDbWeb](https://github.com/evnix/boltdbweb) - A web based GUI for BoltDB files.\n* [BoltDB Viewer](https://github.com/zc310/rich_boltdb) - A BoltDB Viewer Can run on Windows、Linux、Android system.\n* [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.\n* [bstore](https://github.com/mjl-/bstore) - Database library storing Go values, with referential/unique/nonzero constraints, indices, automatic schema management with struct tags, and a query API.\n* [btcwallet](https://github.com/btcsuite/btcwallet) - A bitcoin wallet.\n* [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining\n  simple tx and key scans.\n* [Buildkit](https://github.com/moby/buildkit) - concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit\n* [cayley](https://github.com/google/cayley) - Cayley is an open-source graph database using Bolt as optional backend.\n* [ChainStore](https://github.com/pressly/chainstore) - Simple key-value interface to a variety of storage engines organized as a chain of operations.\n* [🌰 Chestnut](https://github.com/jrapoport/chestnut) - Chestnut is encrypted storage for Go.\n* [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.\n* [Containerd](https://github.com/containerd/containerd) - An open and reliable container runtime\n* [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.\n* [dcrwallet](https://github.com/decred/dcrwallet) - A wallet for the Decred cryptocurrency.\n* [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \\*NIX operating systems.\n* [event-shuttle](https://github.com/sclasen/event-shuttle) - A Unix system service to collect and reliably deliver messages to Kafka.\n* [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data.\n* [Go Report Card](https://goreportcard.com/) - Go code quality report cards as a (free and open source) service.\n* [GoWebApp](https://github.com/josephspurrier/gowebapp) - A basic MVC web application in Go using BoltDB.\n* [GoShort](https://github.com/pankajkhairnar/goShort) - GoShort is a URL shortener written in Golang and BoltDB for persistent key/value storage and for routing it's using high performent HTTPRouter.\n* [gopherpit](https://github.com/gopherpit/gopherpit) - A web service to manage Go remote import paths with custom domains\n* [gokv](https://github.com/philippgille/gokv) - Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)\n* [goraphdb](https://github.com/mstrYoda/goraphdb) - A graph database provides Cypher query, fluent builder and management UI.\n* [Gitchain](https://github.com/gitchain/gitchain) - Decentralized, peer-to-peer Git repositories aka \"Git meets Bitcoin\".\n* [InfluxDB](https://influxdata.com) - Scalable datastore for metrics, events, and real-time analytics.\n* [ipLocator](https://github.com/AndreasBriese/ipLocator) - A fast ip-geo-location-server using bolt with bloom filters.\n* [ipxed](https://github.com/kelseyhightower/ipxed) - Web interface and api for ipxed.\n* [Ironsmith](https://github.com/timshannon/ironsmith) - A simple, script-driven continuous integration (build - > test -> release) tool, with no external dependencies\n* [Kala](https://github.com/ajvb/kala) - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.\n* [Key Value Access Language (KVAL)](https://github.com/kval-access-language) - A proposed grammar for key-value datastores offering a bbolt binding.\n* [LedisDB](https://github.com/siddontang/ledisdb) - A high performance NoSQL, using Bolt as optional storage.\n* [lru](https://github.com/crowdriff/lru) - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.\n* [mbuckets](https://github.com/abhigupta912/mbuckets) - A Bolt wrapper that allows easy operations on multi level (nested) buckets.\n* [MetricBase](https://github.com/msiebuhr/MetricBase) - Single-binary version of Graphite.\n* [MuLiFS](https://github.com/dankomiocevic/mulifs) - Music Library Filesystem creates a filesystem to organise your music files.\n* [NATS](https://github.com/nats-io/nats-streaming-server) - NATS Streaming uses bbolt for message and metadata storage.\n* [Portainer](https://github.com/portainer/portainer) - A lightweight service delivery platform for containerized applications that can be used to manage Docker, Swarm, Kubernetes and ACI environments.\n* [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system.\n* [Rain](https://github.com/cenkalti/rain) - BitTorrent client and library.\n* [reef-pi](https://github.com/reef-pi/reef-pi) - reef-pi is an award winning, modular, DIY reef tank controller using easy to learn electronics based on a Raspberry Pi.\n* [Request Baskets](https://github.com/darklynx/request-baskets) - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to [RequestBin](http://requestb.in/) service\n* [Seaweed File System](https://github.com/chrislusf/seaweedfs) - Highly scalable distributed key~file system with O(1) disk read.\n* [stow](https://github.com/djherbis/stow) -  a persistence manager for objects\n  backed by boltdb.\n* [Storm](https://github.com/asdine/storm) - Simple and powerful ORM for BoltDB.\n* [SimpleBolt](https://github.com/xyproto/simplebolt) - A simple way to use BoltDB. Deals mainly with strings.\n* [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics.\n* [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects.\n* [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.\n* [torrent](https://github.com/anacrolix/torrent) - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development.\n* [Wiki](https://github.com/peterhellberg/wiki) - A tiny wiki using Goji, BoltDB and Blackfriday.\n\nIf you are using Bolt in a project please send a pull request to add it to the list.\n"
  },
  {
    "path": "allocate_test.go",
    "content": "package bbolt\n\nimport (\n\t\"testing\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/freelist\"\n)\n\nfunc TestTx_allocatePageStats(t *testing.T) {\n\tfor n, f := range map[string]freelist.Interface{\"hashmap\": freelist.NewHashMapFreelist(), \"array\": freelist.NewArrayFreelist()} {\n\t\tt.Run(n, func(t *testing.T) {\n\t\t\tids := []common.Pgid{2, 3}\n\t\t\tf.Init(ids)\n\n\t\t\ttx := &Tx{\n\t\t\t\tdb: &DB{\n\t\t\t\t\tfreelist: f,\n\t\t\t\t\tpageSize: common.DefaultPageSize,\n\t\t\t\t},\n\t\t\t\tmeta:  &common.Meta{},\n\t\t\t\tpages: make(map[common.Pgid]*common.Page),\n\t\t\t}\n\n\t\t\ttxStats := tx.Stats()\n\t\t\tprePageCnt := txStats.GetPageCount()\n\t\t\tallocateCnt := f.FreeCount()\n\n\t\t\tif _, err := tx.allocate(allocateCnt); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\ttxStats = tx.Stats()\n\t\t\tif txStats.GetPageCount() != prePageCnt+int64(allocateCnt) {\n\t\t\t\tt.Errorf(\"Allocated %d but got %d page in stats\", allocateCnt, txStats.GetPageCount())\n\t\t\t}\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "bolt_aix.go",
    "content": "//go:build aix\n\npackage bbolt\n\nimport (\n\t\"fmt\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"golang.org/x/sys/unix\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, exclusive bool, timeout time.Duration) error {\n\tvar t time.Time\n\tif timeout != 0 {\n\t\tt = time.Now()\n\t}\n\tfd := db.file.Fd()\n\tvar lockType int16\n\tif exclusive {\n\t\tlockType = syscall.F_WRLCK\n\t} else {\n\t\tlockType = syscall.F_RDLCK\n\t}\n\tfor {\n\t\t// Attempt to obtain an exclusive lock.\n\t\tlock := syscall.Flock_t{Type: lockType}\n\t\terr := syscall.FcntlFlock(fd, syscall.F_SETLK, &lock)\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != syscall.EAGAIN {\n\t\t\treturn err\n\t\t}\n\n\t\t// If we timed out then return an error.\n\t\tif timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {\n\t\t\treturn ErrTimeout\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(flockRetryTimeout)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\tvar lock syscall.Flock_t\n\tlock.Start = 0\n\tlock.Len = 0\n\tlock.Type = syscall.F_UNLCK\n\tlock.Whence = 0\n\treturn syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)\n}\n\n// mmap memory maps a DB's data file.\nfunc mmap(db *DB, sz int) error {\n\t// Map the data file to memory.\n\tb, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Advise the kernel that the mmap is accessed randomly.\n\tif err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {\n\t\treturn fmt.Errorf(\"madvise: %s\", err)\n\t}\n\n\t// Save the original byte slice and convert to a byte array pointer.\n\tdb.dataref = b\n\tdb.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0]))\n\tdb.datasz = sz\n\treturn nil\n}\n\n// munmap unmaps a DB's data file from memory.\nfunc munmap(db *DB) error {\n\t// Ignore the unmap if we have no mapped data.\n\tif db.dataref == nil {\n\t\treturn nil\n\t}\n\n\t// Unmap using the original byte slice.\n\terr := unix.Munmap(db.dataref)\n\tdb.dataref = nil\n\tdb.data = nil\n\tdb.datasz = 0\n\treturn err\n}\n"
  },
  {
    "path": "bolt_android.go",
    "content": "package bbolt\n\nimport (\n\t\"fmt\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"golang.org/x/sys/unix\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, exclusive bool, timeout time.Duration) error {\n\tvar t time.Time\n\tif timeout != 0 {\n\t\tt = time.Now()\n\t}\n\tfd := db.file.Fd()\n\tvar lockType int16\n\tif exclusive {\n\t\tlockType = syscall.F_WRLCK\n\t} else {\n\t\tlockType = syscall.F_RDLCK\n\t}\n\tfor {\n\t\t// Attempt to obtain an exclusive lock.\n\t\tlock := syscall.Flock_t{Type: lockType}\n\t\terr := syscall.FcntlFlock(fd, syscall.F_SETLK, &lock)\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != syscall.EAGAIN {\n\t\t\treturn err\n\t\t}\n\n\t\t// If we timed out then return an error.\n\t\tif timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {\n\t\t\treturn ErrTimeout\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(flockRetryTimeout)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\tvar lock syscall.Flock_t\n\tlock.Start = 0\n\tlock.Len = 0\n\tlock.Type = syscall.F_UNLCK\n\tlock.Whence = 0\n\treturn syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)\n}\n\n// mmap memory maps a DB's data file.\nfunc mmap(db *DB, sz int) error {\n\t// Map the data file to memory.\n\tb, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Advise the kernel that the mmap is accessed randomly.\n\terr = unix.Madvise(b, syscall.MADV_RANDOM)\n\tif err != nil && err != syscall.ENOSYS {\n\t\t// Ignore not implemented error in kernel because it still works.\n\t\treturn fmt.Errorf(\"madvise: %s\", err)\n\t}\n\n\t// Save the original byte slice and convert to a byte array pointer.\n\tdb.dataref = b\n\tdb.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0]))\n\tdb.datasz = sz\n\treturn nil\n}\n\n// munmap unmaps a DB's data file from memory.\nfunc munmap(db *DB) error {\n\t// Ignore the unmap if we have no mapped data.\n\tif db.dataref == nil {\n\t\treturn nil\n\t}\n\n\t// Unmap using the original byte slice.\n\terr := unix.Munmap(db.dataref)\n\tdb.dataref = nil\n\tdb.data = nil\n\tdb.datasz = 0\n\treturn err\n}\n"
  },
  {
    "path": "bolt_linux.go",
    "content": "package bbolt\n\nimport (\n\t\"syscall\"\n)\n\n// fdatasync flushes written data to a file descriptor.\nfunc fdatasync(db *DB) error {\n\treturn syscall.Fdatasync(int(db.file.Fd()))\n}\n"
  },
  {
    "path": "bolt_openbsd.go",
    "content": "package bbolt\n\nimport (\n\t\"golang.org/x/sys/unix\"\n)\n\nfunc msync(db *DB) error {\n\treturn unix.Msync(db.data[:db.datasz], unix.MS_INVALIDATE)\n}\n\nfunc fdatasync(db *DB) error {\n\tif db.data != nil {\n\t\treturn msync(db)\n\t}\n\treturn db.file.Sync()\n}\n"
  },
  {
    "path": "bolt_solaris.go",
    "content": "package bbolt\n\nimport (\n\t\"fmt\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"golang.org/x/sys/unix\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, exclusive bool, timeout time.Duration) error {\n\tvar t time.Time\n\tif timeout != 0 {\n\t\tt = time.Now()\n\t}\n\tfd := db.file.Fd()\n\tvar lockType int16\n\tif exclusive {\n\t\tlockType = syscall.F_WRLCK\n\t} else {\n\t\tlockType = syscall.F_RDLCK\n\t}\n\tfor {\n\t\t// Attempt to obtain an exclusive lock.\n\t\tlock := syscall.Flock_t{Type: lockType}\n\t\terr := syscall.FcntlFlock(fd, syscall.F_SETLK, &lock)\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != syscall.EAGAIN {\n\t\t\treturn err\n\t\t}\n\n\t\t// If we timed out then return an error.\n\t\tif timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {\n\t\t\treturn ErrTimeout\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(flockRetryTimeout)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\tvar lock syscall.Flock_t\n\tlock.Start = 0\n\tlock.Len = 0\n\tlock.Type = syscall.F_UNLCK\n\tlock.Whence = 0\n\treturn syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)\n}\n\n// mmap memory maps a DB's data file.\nfunc mmap(db *DB, sz int) error {\n\t// Map the data file to memory.\n\tb, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Advise the kernel that the mmap is accessed randomly.\n\tif err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {\n\t\treturn fmt.Errorf(\"madvise: %s\", err)\n\t}\n\n\t// Save the original byte slice and convert to a byte array pointer.\n\tdb.dataref = b\n\tdb.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0]))\n\tdb.datasz = sz\n\treturn nil\n}\n\n// munmap unmaps a DB's data file from memory.\nfunc munmap(db *DB) error {\n\t// Ignore the unmap if we have no mapped data.\n\tif db.dataref == nil {\n\t\treturn nil\n\t}\n\n\t// Unmap using the original byte slice.\n\terr := unix.Munmap(db.dataref)\n\tdb.dataref = nil\n\tdb.data = nil\n\tdb.datasz = 0\n\treturn err\n}\n"
  },
  {
    "path": "bolt_unix.go",
    "content": "//go:build !windows && !plan9 && !solaris && !aix && !android\n\npackage bbolt\n\nimport (\n\t\"fmt\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"golang.org/x/sys/unix\"\n\n\t\"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, exclusive bool, timeout time.Duration) error {\n\tvar t time.Time\n\tif timeout != 0 {\n\t\tt = time.Now()\n\t}\n\tfd := db.file.Fd()\n\tflag := syscall.LOCK_NB\n\tif exclusive {\n\t\tflag |= syscall.LOCK_EX\n\t} else {\n\t\tflag |= syscall.LOCK_SH\n\t}\n\tfor {\n\t\t// Attempt to obtain an exclusive lock.\n\t\terr := syscall.Flock(int(fd), flag)\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != syscall.EWOULDBLOCK {\n\t\t\treturn err\n\t\t}\n\n\t\t// If we timed out then return an error.\n\t\tif timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {\n\t\t\treturn errors.ErrTimeout\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(flockRetryTimeout)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\treturn syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)\n}\n\n// mmap memory maps a DB's data file.\nfunc mmap(db *DB, sz int) error {\n\t// Map the data file to memory.\n\tb, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Advise the kernel that the mmap is accessed randomly.\n\terr = unix.Madvise(b, syscall.MADV_RANDOM)\n\tif err != nil && err != syscall.ENOSYS {\n\t\t// Ignore not implemented error in kernel because it still works.\n\t\treturn fmt.Errorf(\"madvise: %s\", err)\n\t}\n\n\t// Save the original byte slice and convert to a byte array pointer.\n\tdb.dataref = b\n\tdb.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0]))\n\tdb.datasz = sz\n\treturn nil\n}\n\n// munmap unmaps a DB's data file from memory.\nfunc munmap(db *DB) error {\n\t// Ignore the unmap if we have no mapped data.\n\tif db.dataref == nil {\n\t\treturn nil\n\t}\n\n\t// Unmap using the original byte slice.\n\terr := unix.Munmap(db.dataref)\n\tdb.dataref = nil\n\tdb.data = nil\n\tdb.datasz = 0\n\treturn err\n}\n"
  },
  {
    "path": "bolt_windows.go",
    "content": "package bbolt\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"golang.org/x/sys/windows\"\n\n\t\"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// fdatasync flushes written data to a file descriptor.\nfunc fdatasync(db *DB) error {\n\treturn db.file.Sync()\n}\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, exclusive bool, timeout time.Duration) error {\n\tvar t time.Time\n\tif timeout != 0 {\n\t\tt = time.Now()\n\t}\n\tvar flags uint32 = windows.LOCKFILE_FAIL_IMMEDIATELY\n\tif exclusive {\n\t\tflags |= windows.LOCKFILE_EXCLUSIVE_LOCK\n\t}\n\tfor {\n\t\t// Fix for https://github.com/etcd-io/bbolt/issues/121. Use byte-range\n\t\t// -1..0 as the lock on the database file.\n\t\tvar m1 uint32 = (1 << 32) - 1 // -1 in a uint32\n\t\terr := windows.LockFileEx(windows.Handle(db.file.Fd()), flags, 0, 1, 0, &windows.Overlapped{\n\t\t\tOffset:     m1,\n\t\t\tOffsetHigh: m1,\n\t\t})\n\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != windows.ERROR_LOCK_VIOLATION {\n\t\t\treturn err\n\t\t}\n\n\t\t// If we timed oumercit then return an error.\n\t\tif timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {\n\t\t\treturn errors.ErrTimeout\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(flockRetryTimeout)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\tvar m1 uint32 = (1 << 32) - 1 // -1 in a uint32\n\treturn windows.UnlockFileEx(windows.Handle(db.file.Fd()), 0, 1, 0, &windows.Overlapped{\n\t\tOffset:     m1,\n\t\tOffsetHigh: m1,\n\t})\n}\n\n// mmap memory maps a DB's data file.\n// Based on: https://github.com/edsrzf/mmap-go\nfunc mmap(db *DB, sz int) error {\n\tvar sizelo, sizehi uint32\n\n\tif !db.readOnly {\n\t\tif db.MaxSize > 0 && sz > db.MaxSize {\n\t\t\t// The max size only limits future writes; however, we don’t block opening\n\t\t\t// and mapping the database if it already exceeds the limit.\n\t\t\tfileSize, err := db.fileSize()\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"could not check existing db file size: %s\", err)\n\t\t\t}\n\n\t\t\tif sz > fileSize {\n\t\t\t\treturn errors.ErrMaxSizeReached\n\t\t\t}\n\t\t}\n\n\t\t// Truncate the database to the size of the mmap.\n\t\tif err := db.file.Truncate(int64(sz)); err != nil {\n\t\t\treturn fmt.Errorf(\"truncate: %s\", err)\n\t\t}\n\t\tsizehi = uint32(sz >> 32)\n\t\tsizelo = uint32(sz)\n\t}\n\n\t// Open a file mapping handle.\n\th, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizehi, sizelo, nil)\n\tif h == 0 {\n\t\treturn os.NewSyscallError(\"CreateFileMapping\", errno)\n\t}\n\n\t// Create the memory map.\n\taddr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0)\n\tif addr == 0 {\n\t\t// Do our best and report error returned from MapViewOfFile.\n\t\t_ = syscall.CloseHandle(h)\n\t\treturn os.NewSyscallError(\"MapViewOfFile\", errno)\n\t}\n\n\t// Close mapping handle.\n\tif err := syscall.CloseHandle(syscall.Handle(h)); err != nil {\n\t\treturn os.NewSyscallError(\"CloseHandle\", err)\n\t}\n\n\t// Convert to a byte array.\n\tdb.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(addr))\n\tdb.datasz = sz\n\n\treturn nil\n}\n\n// munmap unmaps a pointer from a file.\n// Based on: https://github.com/edsrzf/mmap-go\nfunc munmap(db *DB) error {\n\tif db.data == nil {\n\t\treturn nil\n\t}\n\n\taddr := (uintptr)(unsafe.Pointer(&db.data[0]))\n\tvar err1 error\n\tif err := syscall.UnmapViewOfFile(addr); err != nil {\n\t\terr1 = os.NewSyscallError(\"UnmapViewOfFile\", err)\n\t}\n\tdb.data = nil\n\tdb.datasz = 0\n\treturn err1\n}\n"
  },
  {
    "path": "boltsync_unix.go",
    "content": "//go:build !windows && !plan9 && !linux && !openbsd\n\npackage bbolt\n\n// fdatasync flushes written data to a file descriptor.\nfunc fdatasync(db *DB) error {\n\treturn db.file.Sync()\n}\n"
  },
  {
    "path": "bucket.go",
    "content": "package bbolt\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"unsafe\"\n\n\t\"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\nconst (\n\t// MaxKeySize is the maximum length of a key, in bytes.\n\tMaxKeySize = 32768\n\n\t// MaxValueSize is the maximum length of a value, in bytes.\n\tMaxValueSize = (1 << 31) - 2\n)\n\nconst (\n\tminFillPercent = 0.1\n\tmaxFillPercent = 1.0\n)\n\n// DefaultFillPercent is the percentage that split pages are filled.\n// This value can be changed by setting Bucket.FillPercent.\nconst DefaultFillPercent = 0.5\n\n// Bucket represents a collection of key/value pairs inside the database.\ntype Bucket struct {\n\t*common.InBucket\n\ttx       *Tx                   // the associated transaction\n\tbuckets  map[string]*Bucket    // subbucket cache\n\tpage     *common.Page          // inline page reference\n\trootNode *node                 // materialized node for the root page.\n\tnodes    map[common.Pgid]*node // node cache\n\n\t// Sets the threshold for filling nodes when they split. By default,\n\t// the bucket will fill to 50% but it can be useful to increase this\n\t// amount if you know that your write workloads are mostly append-only.\n\t//\n\t// This is non-persisted across transactions so it must be set in every Tx.\n\tFillPercent float64\n}\n\n// newBucket returns a new bucket associated with a transaction.\nfunc newBucket(tx *Tx) Bucket {\n\tvar b = Bucket{tx: tx, FillPercent: DefaultFillPercent}\n\tif tx.writable {\n\t\tb.buckets = make(map[string]*Bucket)\n\t\tb.nodes = make(map[common.Pgid]*node)\n\t}\n\treturn b\n}\n\n// Tx returns the tx of the bucket.\nfunc (b *Bucket) Tx() *Tx {\n\treturn b.tx\n}\n\n// Root returns the root of the bucket.\nfunc (b *Bucket) Root() common.Pgid {\n\treturn b.RootPage()\n}\n\n// Writable returns whether the bucket is writable.\nfunc (b *Bucket) Writable() bool {\n\treturn b.tx.writable\n}\n\n// Cursor creates a cursor associated with the bucket.\n// The cursor is only valid as long as the transaction is open.\n// Do not use a cursor after the transaction is closed.\nfunc (b *Bucket) Cursor() *Cursor {\n\t// Update transaction statistics.\n\tb.tx.stats.IncCursorCount(1)\n\n\t// Allocate and return a cursor.\n\treturn &Cursor{\n\t\tbucket: b,\n\t\tstack:  make([]elemRef, 0),\n\t}\n}\n\n// Bucket retrieves a nested bucket by name.\n// Returns nil if the bucket does not exist.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (b *Bucket) Bucket(name []byte) *Bucket {\n\tif b.buckets != nil {\n\t\tif child := b.buckets[string(name)]; child != nil {\n\t\t\treturn child\n\t\t}\n\t}\n\n\t// Move cursor to key.\n\tc := b.Cursor()\n\tk, v, flags := c.seek(name)\n\n\t// Return nil if the key doesn't exist or it is not a bucket.\n\tif !bytes.Equal(name, k) || (flags&common.BucketLeafFlag) == 0 {\n\t\treturn nil\n\t}\n\n\t// Otherwise create a bucket and cache it.\n\tvar child = b.openBucket(v)\n\tif b.buckets != nil {\n\t\tb.buckets[string(name)] = child\n\t}\n\n\treturn child\n}\n\n// Helper method that re-interprets a sub-bucket value\n// from a parent into a Bucket\nfunc (b *Bucket) openBucket(value []byte) *Bucket {\n\tvar child = newBucket(b.tx)\n\n\t// Unaligned access requires a copy to be made.\n\tconst unalignedMask = unsafe.Alignof(struct {\n\t\tcommon.InBucket\n\t\tcommon.Page\n\t}{}) - 1\n\tunaligned := uintptr(unsafe.Pointer(&value[0]))&unalignedMask != 0\n\tif unaligned {\n\t\tvalue = cloneBytes(value)\n\t}\n\n\t// If this is a writable transaction then we need to copy the bucket entry.\n\t// Read-only transactions can point directly at the mmap entry.\n\tif b.tx.writable && !unaligned {\n\t\tchild.InBucket = &common.InBucket{}\n\t\t*child.InBucket = *(*common.InBucket)(unsafe.Pointer(&value[0]))\n\t} else {\n\t\tchild.InBucket = (*common.InBucket)(unsafe.Pointer(&value[0]))\n\t}\n\n\t// Save a reference to the inline page if the bucket is inline.\n\tif child.RootPage() == 0 {\n\t\tchild.page = (*common.Page)(unsafe.Pointer(&value[common.BucketHeaderSize]))\n\t}\n\n\treturn &child\n}\n\n// CreateBucket creates a new bucket at the given key and returns the new bucket.\n// Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (b *Bucket) CreateBucket(key []byte) (rb *Bucket, err error) {\n\tif lg := b.tx.db.Logger(); lg != discardLogger {\n\t\tlg.Debugf(\"Creating bucket %q\", key)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Creating bucket %q failed: %v\", key, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Creating bucket %q successfully\", key)\n\t\t\t}\n\t\t}()\n\t}\n\tif b.tx.db == nil {\n\t\treturn nil, errors.ErrTxClosed\n\t} else if !b.tx.writable {\n\t\treturn nil, errors.ErrTxNotWritable\n\t} else if len(key) == 0 {\n\t\treturn nil, errors.ErrBucketNameRequired\n\t}\n\n\t// Insert into node.\n\t// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent\n\t// it from being marked as leaking, and accordingly cannot be allocated on stack.\n\tnewKey := cloneBytes(key)\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, _, flags := c.seek(newKey)\n\n\t// Return an error if there is an existing key.\n\tif bytes.Equal(newKey, k) {\n\t\tif (flags & common.BucketLeafFlag) != 0 {\n\t\t\treturn nil, errors.ErrBucketExists\n\t\t}\n\t\treturn nil, errors.ErrIncompatibleValue\n\t}\n\n\t// Create empty, inline bucket.\n\tvar bucket = Bucket{\n\t\tInBucket:    &common.InBucket{},\n\t\trootNode:    &node{isLeaf: true},\n\t\tFillPercent: DefaultFillPercent,\n\t}\n\tvar value = bucket.write()\n\n\tc.node().put(newKey, newKey, value, 0, common.BucketLeafFlag)\n\n\t// Since subbuckets are not allowed on inline buckets, we need to\n\t// dereference the inline page, if it exists. This will cause the bucket\n\t// to be treated as a regular, non-inline bucket for the rest of the tx.\n\tb.page = nil\n\n\treturn b.Bucket(newKey), nil\n}\n\n// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.\n// Returns an error if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (b *Bucket) CreateBucketIfNotExists(key []byte) (rb *Bucket, err error) {\n\tif lg := b.tx.db.Logger(); lg != discardLogger {\n\t\tlg.Debugf(\"Creating bucket if not exist %q\", key)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Creating bucket if not exist %q failed: %v\", key, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Creating bucket if not exist %q successfully\", key)\n\t\t\t}\n\t\t}()\n\t}\n\n\tif b.tx.db == nil {\n\t\treturn nil, errors.ErrTxClosed\n\t} else if !b.tx.writable {\n\t\treturn nil, errors.ErrTxNotWritable\n\t} else if len(key) == 0 {\n\t\treturn nil, errors.ErrBucketNameRequired\n\t}\n\n\t// Insert into node.\n\t// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent\n\t// it from being marked as leaking, and accordingly cannot be allocated on stack.\n\tnewKey := cloneBytes(key)\n\n\tif b.buckets != nil {\n\t\tif child := b.buckets[string(newKey)]; child != nil {\n\t\t\treturn child, nil\n\t\t}\n\t}\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, v, flags := c.seek(newKey)\n\n\t// Return an error if there is an existing non-bucket key.\n\tif bytes.Equal(newKey, k) {\n\t\tif (flags & common.BucketLeafFlag) != 0 {\n\t\t\tvar child = b.openBucket(v)\n\t\t\tif b.buckets != nil {\n\t\t\t\tb.buckets[string(newKey)] = child\n\t\t\t}\n\n\t\t\treturn child, nil\n\t\t}\n\t\treturn nil, errors.ErrIncompatibleValue\n\t}\n\n\t// Create empty, inline bucket.\n\tvar bucket = Bucket{\n\t\tInBucket:    &common.InBucket{},\n\t\trootNode:    &node{isLeaf: true},\n\t\tFillPercent: DefaultFillPercent,\n\t}\n\tvar value = bucket.write()\n\n\tc.node().put(newKey, newKey, value, 0, common.BucketLeafFlag)\n\n\t// Since subbuckets are not allowed on inline buckets, we need to\n\t// dereference the inline page, if it exists. This will cause the bucket\n\t// to be treated as a regular, non-inline bucket for the rest of the tx.\n\tb.page = nil\n\n\treturn b.Bucket(newKey), nil\n}\n\n// DeleteBucket deletes a bucket at the given key.\n// Returns an error if the bucket does not exist, or if the key represents a non-bucket value.\nfunc (b *Bucket) DeleteBucket(key []byte) (err error) {\n\tif lg := b.tx.db.Logger(); lg != discardLogger {\n\t\tlg.Debugf(\"Deleting bucket %q\", key)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Deleting bucket %q failed: %v\", key, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Deleting bucket %q successfully\", key)\n\t\t\t}\n\t\t}()\n\t}\n\n\tif b.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn errors.ErrTxNotWritable\n\t}\n\n\tnewKey := cloneBytes(key)\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, _, flags := c.seek(newKey)\n\n\t// Return an error if bucket doesn't exist or is not a bucket.\n\tif !bytes.Equal(newKey, k) {\n\t\treturn errors.ErrBucketNotFound\n\t} else if (flags & common.BucketLeafFlag) == 0 {\n\t\treturn errors.ErrIncompatibleValue\n\t}\n\n\t// Recursively delete all child buckets.\n\tchild := b.Bucket(newKey)\n\terr = child.ForEachBucket(func(k []byte) error {\n\t\tif err := child.DeleteBucket(k); err != nil {\n\t\t\treturn fmt.Errorf(\"delete bucket: %s\", err)\n\t\t}\n\t\treturn nil\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Remove cached copy.\n\tdelete(b.buckets, string(newKey))\n\n\t// Release all bucket pages to freelist.\n\tchild.nodes = nil\n\tchild.rootNode = nil\n\tchild.free()\n\n\t// Delete the node if we have a matching key.\n\tc.node().del(newKey)\n\n\treturn nil\n}\n\n// MoveBucket moves a sub-bucket from the source bucket to the destination bucket.\n// Returns an error if\n//  1. the sub-bucket cannot be found in the source bucket;\n//  2. or the key already exists in the destination bucket;\n//  3. or the key represents a non-bucket value;\n//  4. the source and destination buckets are the same.\nfunc (b *Bucket) MoveBucket(key []byte, dstBucket *Bucket) (err error) {\n\tlg := b.tx.db.Logger()\n\tif lg != discardLogger {\n\t\tlg.Debugf(\"Moving bucket %q\", key)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Moving bucket %q failed: %v\", key, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Moving bucket %q successfully\", key)\n\t\t\t}\n\t\t}()\n\t}\n\n\tif b.tx.db == nil || dstBucket.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t} else if !b.Writable() || !dstBucket.Writable() {\n\t\treturn errors.ErrTxNotWritable\n\t}\n\n\tif b.tx.db.Path() != dstBucket.tx.db.Path() || b.tx != dstBucket.tx {\n\t\tlg.Errorf(\"The source and target buckets are not in the same db file, source bucket in %s and target bucket in %s\", b.tx.db.Path(), dstBucket.tx.db.Path())\n\t\treturn errors.ErrDifferentDB\n\t}\n\n\tnewKey := cloneBytes(key)\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, v, flags := c.seek(newKey)\n\n\t// Return an error if bucket doesn't exist or is not a bucket.\n\tif !bytes.Equal(newKey, k) {\n\t\treturn errors.ErrBucketNotFound\n\t} else if (flags & common.BucketLeafFlag) == 0 {\n\t\tlg.Errorf(\"An incompatible key %s exists in the source bucket\", newKey)\n\t\treturn errors.ErrIncompatibleValue\n\t}\n\n\t// Do nothing (return true directly) if the source bucket and the\n\t// destination bucket are actually the same bucket.\n\tif b == dstBucket || (b.RootPage() == dstBucket.RootPage() && b.RootPage() != 0) {\n\t\tlg.Errorf(\"The source bucket (%s) and the target bucket (%s) are the same bucket\", b, dstBucket)\n\t\treturn errors.ErrSameBuckets\n\t}\n\n\t// check whether the key already exists in the destination bucket\n\tcurDst := dstBucket.Cursor()\n\tk, _, flags = curDst.seek(newKey)\n\n\t// Return an error if there is an existing key in the destination bucket.\n\tif bytes.Equal(newKey, k) {\n\t\tif (flags & common.BucketLeafFlag) != 0 {\n\t\t\treturn errors.ErrBucketExists\n\t\t}\n\t\tlg.Errorf(\"An incompatible key %s exists in the target bucket\", newKey)\n\t\treturn errors.ErrIncompatibleValue\n\t}\n\n\t// remove the sub-bucket from the source bucket\n\tdelete(b.buckets, string(newKey))\n\tc.node().del(newKey)\n\n\t// add te sub-bucket to the destination bucket\n\tnewValue := cloneBytes(v)\n\tcurDst.node().put(newKey, newKey, newValue, 0, common.BucketLeafFlag)\n\n\treturn nil\n}\n\n// Inspect returns the structure of the bucket.\nfunc (b *Bucket) Inspect() BucketStructure {\n\treturn b.recursivelyInspect([]byte(\"root\"))\n}\n\nfunc (b *Bucket) recursivelyInspect(name []byte) BucketStructure {\n\tbs := BucketStructure{Name: string(name)}\n\n\tkeyN := 0\n\tc := b.Cursor()\n\tfor k, _, flags := c.first(); k != nil; k, _, flags = c.next() {\n\t\tif flags&common.BucketLeafFlag != 0 {\n\t\t\tchildBucket := b.Bucket(k)\n\t\t\tchildBS := childBucket.recursivelyInspect(k)\n\t\t\tbs.Children = append(bs.Children, childBS)\n\t\t} else {\n\t\t\tkeyN++\n\t\t}\n\t}\n\tbs.KeyN = keyN\n\n\treturn bs\n}\n\n// Get retrieves the value for a key in the bucket.\n// Returns a nil value if the key does not exist or if the key is a nested bucket.\n// The returned value is only valid for the life of the transaction.\n// The returned memory is owned by bbolt and must never be modified; writing to this memory might corrupt the database.\nfunc (b *Bucket) Get(key []byte) []byte {\n\tk, v, flags := b.Cursor().seek(key)\n\n\t// Return nil if this is a bucket.\n\tif (flags & common.BucketLeafFlag) != 0 {\n\t\treturn nil\n\t}\n\n\t// If our target node isn't the same key as what's passed in then return nil.\n\tif !bytes.Equal(key, k) {\n\t\treturn nil\n\t}\n\treturn v\n}\n\n// Put sets the value for a key in the bucket.\n// If the key exist then its previous value will be overwritten.\n// Supplied value must remain valid for the life of the transaction.\n// Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large.\nfunc (b *Bucket) Put(key []byte, value []byte) (err error) {\n\tif lg := b.tx.db.Logger(); lg != discardLogger {\n\t\tlg.Debugf(\"Putting key %q\", key)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Putting key %q failed: %v\", key, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Putting key %q successfully\", key)\n\t\t\t}\n\t\t}()\n\t}\n\tif b.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn errors.ErrTxNotWritable\n\t} else if len(key) == 0 {\n\t\treturn errors.ErrKeyRequired\n\t} else if len(key) > MaxKeySize {\n\t\treturn errors.ErrKeyTooLarge\n\t} else if int64(len(value)) > MaxValueSize {\n\t\treturn errors.ErrValueTooLarge\n\t}\n\n\t// Insert into node.\n\t// Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent\n\t// it from being marked as leaking, and accordingly cannot be allocated on stack.\n\tnewKey := cloneBytes(key)\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, _, flags := c.seek(newKey)\n\n\t// Return an error if there is an existing key with a bucket value.\n\tif bytes.Equal(newKey, k) && (flags&common.BucketLeafFlag) != 0 {\n\t\treturn errors.ErrIncompatibleValue\n\t}\n\n\t// gofail: var beforeBucketPut struct{}\n\n\tc.node().put(newKey, newKey, value, 0, 0)\n\n\treturn nil\n}\n\n// Delete removes a key from the bucket.\n// If the key does not exist then nothing is done and a nil error is returned.\n// Returns an error if the bucket was created from a read-only transaction.\nfunc (b *Bucket) Delete(key []byte) (err error) {\n\tif lg := b.tx.db.Logger(); lg != discardLogger {\n\t\tlg.Debugf(\"Deleting key %q\", key)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Deleting key %q failed: %v\", key, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Deleting key %q successfully\", key)\n\t\t\t}\n\t\t}()\n\t}\n\n\tif b.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn errors.ErrTxNotWritable\n\t}\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, _, flags := c.seek(key)\n\n\t// Return nil if the key doesn't exist.\n\tif !bytes.Equal(key, k) {\n\t\treturn nil\n\t}\n\n\t// Return an error if there is already existing bucket value.\n\tif (flags & common.BucketLeafFlag) != 0 {\n\t\treturn errors.ErrIncompatibleValue\n\t}\n\n\t// Delete the node if we have a matching key.\n\tc.node().del(key)\n\n\treturn nil\n}\n\n// Sequence returns the current integer for the bucket without incrementing it.\nfunc (b *Bucket) Sequence() uint64 {\n\treturn b.InSequence()\n}\n\n// SetSequence updates the sequence number for the bucket.\nfunc (b *Bucket) SetSequence(v uint64) error {\n\tif b.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn errors.ErrTxNotWritable\n\t}\n\n\t// Materialize the root node if it hasn't been already so that the\n\t// bucket will be saved during commit.\n\tif b.rootNode == nil {\n\t\t_ = b.node(b.RootPage(), nil)\n\t}\n\n\t// Set the sequence.\n\tb.SetInSequence(v)\n\treturn nil\n}\n\n// NextSequence returns an autoincrementing integer for the bucket.\nfunc (b *Bucket) NextSequence() (uint64, error) {\n\tif b.tx.db == nil {\n\t\treturn 0, errors.ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn 0, errors.ErrTxNotWritable\n\t}\n\n\t// Materialize the root node if it hasn't been already so that the\n\t// bucket will be saved during commit.\n\tif b.rootNode == nil {\n\t\t_ = b.node(b.RootPage(), nil)\n\t}\n\n\t// Increment and return the sequence.\n\tb.IncSequence()\n\treturn b.Sequence(), nil\n}\n\n// ForEach executes a function for each key/value pair in a bucket.\n// Because ForEach uses a Cursor, the iteration over keys is in lexicographical order.\n// If the provided function returns an error then the iteration is stopped and\n// the error is returned to the caller. The provided function must not modify\n// the bucket; this will result in undefined behavior.\nfunc (b *Bucket) ForEach(fn func(k, v []byte) error) error {\n\tif b.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t}\n\tc := b.Cursor()\n\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\tif err := fn(k, v); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (b *Bucket) ForEachBucket(fn func(k []byte) error) error {\n\tif b.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t}\n\tc := b.Cursor()\n\tfor k, _, flags := c.first(); k != nil; k, _, flags = c.next() {\n\t\tif flags&common.BucketLeafFlag != 0 {\n\t\t\tif err := fn(k); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// Stats returns stats on a bucket.\nfunc (b *Bucket) Stats() BucketStats {\n\tvar s, subStats BucketStats\n\tpageSize := b.tx.db.pageSize\n\ts.BucketN += 1\n\tif b.RootPage() == 0 {\n\t\ts.InlineBucketN += 1\n\t}\n\tb.forEachPage(func(p *common.Page, depth int, pgstack []common.Pgid) {\n\t\tif p.IsLeafPage() {\n\t\t\ts.KeyN += int(p.Count())\n\n\t\t\t// used totals the used bytes for the page\n\t\t\tused := common.PageHeaderSize\n\n\t\t\tif p.Count() != 0 {\n\t\t\t\t// If page has any elements, add all element headers.\n\t\t\t\tused += common.LeafPageElementSize * uintptr(p.Count()-1)\n\n\t\t\t\t// Add all element key, value sizes.\n\t\t\t\t// The computation takes advantage of the fact that the position\n\t\t\t\t// of the last element's key/value equals to the total of the sizes\n\t\t\t\t// of all previous elements' keys and values.\n\t\t\t\t// It also includes the last element's header.\n\t\t\t\tlastElement := p.LeafPageElement(p.Count() - 1)\n\t\t\t\tused += uintptr(lastElement.Pos() + lastElement.Ksize() + lastElement.Vsize())\n\t\t\t}\n\n\t\t\tif b.RootPage() == 0 {\n\t\t\t\t// For inlined bucket just update the inline stats\n\t\t\t\ts.InlineBucketInuse += int(used)\n\t\t\t} else {\n\t\t\t\t// For non-inlined bucket update all the leaf stats\n\t\t\t\ts.LeafPageN++\n\t\t\t\ts.LeafInuse += int(used)\n\t\t\t\ts.LeafOverflowN += int(p.Overflow())\n\n\t\t\t\t// Collect stats from sub-buckets.\n\t\t\t\t// Do that by iterating over all element headers\n\t\t\t\t// looking for the ones with the bucketLeafFlag.\n\t\t\t\tfor i := uint16(0); i < p.Count(); i++ {\n\t\t\t\t\te := p.LeafPageElement(i)\n\t\t\t\t\tif (e.Flags() & common.BucketLeafFlag) != 0 {\n\t\t\t\t\t\t// For any bucket element, open the element value\n\t\t\t\t\t\t// and recursively call Stats on the contained bucket.\n\t\t\t\t\t\tsubStats.Add(b.openBucket(e.Value()).Stats())\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t} else if p.IsBranchPage() {\n\t\t\ts.BranchPageN++\n\t\t\tlastElement := p.BranchPageElement(p.Count() - 1)\n\n\t\t\t// used totals the used bytes for the page\n\t\t\t// Add header and all element headers.\n\t\t\tused := common.PageHeaderSize + (common.BranchPageElementSize * uintptr(p.Count()-1))\n\n\t\t\t// Add size of all keys and values.\n\t\t\t// Again, use the fact that last element's position equals to\n\t\t\t// the total of key, value sizes of all previous elements.\n\t\t\tused += uintptr(lastElement.Pos() + lastElement.Ksize())\n\t\t\ts.BranchInuse += int(used)\n\t\t\ts.BranchOverflowN += int(p.Overflow())\n\t\t}\n\n\t\t// Keep track of maximum page depth.\n\t\tif depth+1 > s.Depth {\n\t\t\ts.Depth = depth + 1\n\t\t}\n\t})\n\n\t// Alloc stats can be computed from page counts and pageSize.\n\ts.BranchAlloc = (s.BranchPageN + s.BranchOverflowN) * pageSize\n\ts.LeafAlloc = (s.LeafPageN + s.LeafOverflowN) * pageSize\n\n\t// Add the max depth of sub-buckets to get total nested depth.\n\ts.Depth += subStats.Depth\n\t// Add the stats for all sub-buckets\n\ts.Add(subStats)\n\treturn s\n}\n\n// forEachPage iterates over every page in a bucket, including inline pages.\nfunc (b *Bucket) forEachPage(fn func(*common.Page, int, []common.Pgid)) {\n\t// If we have an inline page then just use that.\n\tif b.page != nil {\n\t\tfn(b.page, 0, []common.Pgid{b.RootPage()})\n\t\treturn\n\t}\n\n\t// Otherwise traverse the page hierarchy.\n\tb.tx.forEachPage(b.RootPage(), fn)\n}\n\n// forEachPageNode iterates over every page (or node) in a bucket.\n// This also includes inline pages.\nfunc (b *Bucket) forEachPageNode(fn func(*common.Page, *node, int)) {\n\t// If we have an inline page or root node then just use that.\n\tif b.page != nil {\n\t\tfn(b.page, nil, 0)\n\t\treturn\n\t}\n\tb._forEachPageNode(b.RootPage(), 0, fn)\n}\n\nfunc (b *Bucket) _forEachPageNode(pgId common.Pgid, depth int, fn func(*common.Page, *node, int)) {\n\tvar p, n = b.pageNode(pgId)\n\n\t// Execute function.\n\tfn(p, n, depth)\n\n\t// Recursively loop over children.\n\tif p != nil {\n\t\tif p.IsBranchPage() {\n\t\t\tfor i := 0; i < int(p.Count()); i++ {\n\t\t\t\telem := p.BranchPageElement(uint16(i))\n\t\t\t\tb._forEachPageNode(elem.Pgid(), depth+1, fn)\n\t\t\t}\n\t\t}\n\t} else {\n\t\tif !n.isLeaf {\n\t\t\tfor _, inode := range n.inodes {\n\t\t\t\tb._forEachPageNode(inode.Pgid(), depth+1, fn)\n\t\t\t}\n\t\t}\n\t}\n}\n\n// spill writes all the nodes for this bucket to dirty pages.\nfunc (b *Bucket) spill() error {\n\t// Spill all child buckets first.\n\tfor name, child := range b.buckets {\n\t\t// If the child bucket is small enough and it has no child buckets then\n\t\t// write it inline into the parent bucket's page. Otherwise spill it\n\t\t// like a normal bucket and make the parent value a pointer to the page.\n\t\tvar value []byte\n\t\tif child.inlineable() {\n\t\t\tchild.free()\n\t\t\tvalue = child.write()\n\t\t} else {\n\t\t\tif err := child.spill(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// Update the child bucket header in this bucket.\n\t\t\tvalue = make([]byte, unsafe.Sizeof(common.InBucket{}))\n\t\t\tvar bucket = (*common.InBucket)(unsafe.Pointer(&value[0]))\n\t\t\t*bucket = *child.InBucket\n\t\t}\n\n\t\t// Skip writing the bucket if there are no materialized nodes.\n\t\tif child.rootNode == nil {\n\t\t\tcontinue\n\t\t}\n\n\t\t// Update parent node.\n\t\tvar c = b.Cursor()\n\t\tk, _, flags := c.seek([]byte(name))\n\t\tif !bytes.Equal([]byte(name), k) {\n\t\t\tpanic(fmt.Sprintf(\"misplaced bucket header: %x -> %x\", []byte(name), k))\n\t\t}\n\t\tif flags&common.BucketLeafFlag == 0 {\n\t\t\tpanic(fmt.Sprintf(\"unexpected bucket header flag: %x\", flags))\n\t\t}\n\t\tc.node().put([]byte(name), []byte(name), value, 0, common.BucketLeafFlag)\n\t}\n\n\t// Ignore if there's not a materialized root node.\n\tif b.rootNode == nil {\n\t\treturn nil\n\t}\n\n\t// Spill nodes.\n\tif err := b.rootNode.spill(); err != nil {\n\t\treturn err\n\t}\n\tb.rootNode = b.rootNode.root()\n\n\t// Update the root node for this bucket.\n\tif b.rootNode.pgid >= b.tx.meta.Pgid() {\n\t\tpanic(fmt.Sprintf(\"pgid (%d) above high water mark (%d)\", b.rootNode.pgid, b.tx.meta.Pgid()))\n\t}\n\tb.SetRootPage(b.rootNode.pgid)\n\n\treturn nil\n}\n\n// inlineable returns true if a bucket is small enough to be written inline\n// and if it contains no subbuckets. Otherwise, returns false.\nfunc (b *Bucket) inlineable() bool {\n\tvar n = b.rootNode\n\n\t// Bucket must only contain a single leaf node.\n\tif n == nil || !n.isLeaf {\n\t\treturn false\n\t}\n\n\t// Bucket is not inlineable if it contains subbuckets or if it goes beyond\n\t// our threshold for inline bucket size.\n\tvar size = common.PageHeaderSize\n\tfor _, inode := range n.inodes {\n\t\tsize += common.LeafPageElementSize + uintptr(len(inode.Key())) + uintptr(len(inode.Value()))\n\n\t\tif inode.Flags()&common.BucketLeafFlag != 0 {\n\t\t\treturn false\n\t\t} else if size > b.maxInlineBucketSize() {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n\n// Returns the maximum total size of a bucket to make it a candidate for inlining.\nfunc (b *Bucket) maxInlineBucketSize() uintptr {\n\treturn uintptr(b.tx.db.pageSize / 4)\n}\n\n// write allocates and writes a bucket to a byte slice.\nfunc (b *Bucket) write() []byte {\n\t// Allocate the appropriate size.\n\tvar n = b.rootNode\n\tvar value = make([]byte, common.BucketHeaderSize+n.size())\n\n\t// Write a bucket header.\n\tvar bucket = (*common.InBucket)(unsafe.Pointer(&value[0]))\n\t*bucket = *b.InBucket\n\n\t// Convert byte slice to a fake page and write the root node.\n\tvar p = (*common.Page)(unsafe.Pointer(&value[common.BucketHeaderSize]))\n\tn.write(p)\n\n\treturn value\n}\n\n// rebalance attempts to balance all nodes.\nfunc (b *Bucket) rebalance() {\n\tfor _, n := range b.nodes {\n\t\tn.rebalance()\n\t}\n\tfor _, child := range b.buckets {\n\t\tchild.rebalance()\n\t}\n}\n\n// node creates a node from a page and associates it with a given parent.\nfunc (b *Bucket) node(pgId common.Pgid, parent *node) *node {\n\tcommon.Assert(b.nodes != nil, \"nodes map expected\")\n\n\t// Retrieve node if it's already been created.\n\tif n := b.nodes[pgId]; n != nil {\n\t\treturn n\n\t}\n\n\t// Otherwise create a node and cache it.\n\tn := &node{bucket: b, parent: parent}\n\tif parent == nil {\n\t\tb.rootNode = n\n\t} else {\n\t\tparent.children = append(parent.children, n)\n\t}\n\n\t// Use the inline page if this is an inline bucket.\n\tvar p = b.page\n\tif p == nil {\n\t\tp = b.tx.page(pgId)\n\t} else {\n\t\t// if p isn't nil, then it's an inline bucket.\n\t\t// The pgId must be 0 in this case.\n\t\tcommon.Verify(func() {\n\t\t\tcommon.Assert(pgId == 0, \"The page ID (%d) isn't 0 for an inline bucket\", pgId)\n\t\t})\n\t}\n\n\t// Read the page into the node and cache it.\n\tn.read(p)\n\tb.nodes[pgId] = n\n\n\t// Update statistics.\n\tb.tx.stats.IncNodeCount(1)\n\n\treturn n\n}\n\n// free recursively frees all pages in the bucket.\nfunc (b *Bucket) free() {\n\tif b.RootPage() == 0 {\n\t\treturn\n\t}\n\n\tvar tx = b.tx\n\tb.forEachPageNode(func(p *common.Page, n *node, _ int) {\n\t\tif p != nil {\n\t\t\ttx.db.freelist.Free(tx.meta.Txid(), p)\n\t\t} else {\n\t\t\tn.free()\n\t\t}\n\t})\n\tb.SetRootPage(0)\n}\n\n// dereference removes all references to the old mmap.\nfunc (b *Bucket) dereference() {\n\tif b.rootNode != nil {\n\t\tb.rootNode.root().dereference()\n\t}\n\n\tfor _, child := range b.buckets {\n\t\tchild.dereference()\n\t}\n}\n\n// pageNode returns the in-memory node, if it exists.\n// Otherwise, returns the underlying page.\nfunc (b *Bucket) pageNode(id common.Pgid) (*common.Page, *node) {\n\t// Inline buckets have a fake page embedded in their value so treat them\n\t// differently. We'll return the rootNode (if available) or the fake page.\n\tif b.RootPage() == 0 {\n\t\tif id != 0 {\n\t\t\tpanic(fmt.Sprintf(\"inline bucket non-zero page access(2): %d != 0\", id))\n\t\t}\n\t\tif b.rootNode != nil {\n\t\t\treturn nil, b.rootNode\n\t\t}\n\t\treturn b.page, nil\n\t}\n\n\t// Check the node cache for non-inline buckets.\n\tif b.nodes != nil {\n\t\tif n := b.nodes[id]; n != nil {\n\t\t\treturn nil, n\n\t\t}\n\t}\n\n\t// Finally lookup the page from the transaction if no node is materialized.\n\treturn b.tx.page(id), nil\n}\n\n// BucketStats records statistics about resources used by a bucket.\ntype BucketStats struct {\n\t// Page count statistics.\n\tBranchPageN     int // number of logical branch pages\n\tBranchOverflowN int // number of physical branch overflow pages\n\tLeafPageN       int // number of logical leaf pages\n\tLeafOverflowN   int // number of physical leaf overflow pages\n\n\t// Tree statistics.\n\tKeyN  int // number of keys/value pairs\n\tDepth int // number of levels in B+tree\n\n\t// Page size utilization.\n\tBranchAlloc int // bytes allocated for physical branch pages\n\tBranchInuse int // bytes actually used for branch data\n\tLeafAlloc   int // bytes allocated for physical leaf pages\n\tLeafInuse   int // bytes actually used for leaf data\n\n\t// Bucket statistics\n\tBucketN           int // total number of buckets including the top bucket\n\tInlineBucketN     int // total number on inlined buckets\n\tInlineBucketInuse int // bytes used for inlined buckets (also accounted for in LeafInuse)\n}\n\nfunc (s *BucketStats) Add(other BucketStats) {\n\ts.BranchPageN += other.BranchPageN\n\ts.BranchOverflowN += other.BranchOverflowN\n\ts.LeafPageN += other.LeafPageN\n\ts.LeafOverflowN += other.LeafOverflowN\n\ts.KeyN += other.KeyN\n\tif s.Depth < other.Depth {\n\t\ts.Depth = other.Depth\n\t}\n\ts.BranchAlloc += other.BranchAlloc\n\ts.BranchInuse += other.BranchInuse\n\ts.LeafAlloc += other.LeafAlloc\n\ts.LeafInuse += other.LeafInuse\n\n\ts.BucketN += other.BucketN\n\ts.InlineBucketN += other.InlineBucketN\n\ts.InlineBucketInuse += other.InlineBucketInuse\n}\n\n// cloneBytes returns a copy of a given slice.\nfunc cloneBytes(v []byte) []byte {\n\tvar clone = make([]byte, len(v))\n\tcopy(clone, v)\n\treturn clone\n}\n\ntype BucketStructure struct {\n\tName     string            `json:\"name\"`              // name of the bucket\n\tKeyN     int               `json:\"keyN\"`              // number of key/value pairs\n\tChildren []BucketStructure `json:\"buckets,omitempty\"` // child buckets\n}\n"
  },
  {
    "path": "bucket_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"math/rand\"\n\t\"os\"\n\t\"strconv\"\n\t\"strings\"\n\t\"testing\"\n\t\"testing/quick\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\tberrors \"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// Ensure that a bucket that gets a non-existent key returns nil.\nfunc TestBucket_Get_NonExistent(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatal(\"expected nil value\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can read a value that is not flushed yet.\nfunc TestBucket_Get_FromNode(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); !bytes.Equal(v, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket retrieved via Get() returns a nil.\nfunc TestBucket_Get_IncompatibleValue(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := tx.Bucket([]byte(\"widgets\")).CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\")) != nil {\n\t\t\tt.Fatal(\"expected nil value\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a slice returned from a bucket has a capacity equal to its length.\n// This also allows slices to be appended to since it will require a realloc by Go.\n//\n// https://github.com/boltdb/bolt/issues/544\nfunc TestBucket_Get_Capacity(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Write key to a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"bucket\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn b.Put([]byte(\"key\"), []byte(\"val\"))\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Retrieve value and attempt to append to it.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tk, v := tx.Bucket([]byte(\"bucket\")).Cursor().First()\n\n\t\t// Verify capacity.\n\t\tif len(k) != cap(k) {\n\t\t\tt.Fatalf(\"unexpected key slice capacity: %d\", cap(k))\n\t\t} else if len(v) != cap(v) {\n\t\t\tt.Fatalf(\"unexpected value slice capacity: %d\", cap(v))\n\t\t}\n\n\t\t// Ensure slice can be appended to without a segfault.\n\t\tk = append(k, []byte(\"123\")...)\n\t\tv = append(v, []byte(\"123\")...)\n\t\t_, _ = k, v // to pass ineffassign\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can write a key/value.\nfunc TestBucket_Put(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tv := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tif !bytes.Equal([]byte(\"bar\"), v) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can rewrite a key in the same transaction.\nfunc TestBucket_Put_Repeat(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"baz\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tif !bytes.Equal([]byte(\"baz\"), value) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", value)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can write a bunch of large values.\nfunc TestBucket_Put_Large(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tcount, factor := 100, 200\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 1; i < count; i++ {\n\t\t\tif err := b.Put([]byte(strings.Repeat(\"0\", i*factor)), []byte(strings.Repeat(\"X\", (count-i)*factor))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 1; i < count; i++ {\n\t\t\tvalue := b.Get([]byte(strings.Repeat(\"0\", i*factor)))\n\t\t\tif !bytes.Equal(value, []byte(strings.Repeat(\"X\", (count-i)*factor))) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", value)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a database can perform multiple large appends safely.\nfunc TestDB_Put_VeryLarge(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tn, batchN := 400000, 200000\n\tksize, vsize := 8, 500\n\n\tdb := btesting.MustCreateDB(t)\n\n\tfor i := 0; i < n; i += batchN {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"widgets\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tfor j := 0; j < batchN; j++ {\n\t\t\t\tk, v := make([]byte, ksize), make([]byte, vsize)\n\t\t\t\tbinary.BigEndian.PutUint32(k, uint32(i+j))\n\t\t\t\tif err := b.Put(k, v); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n}\n\n// Ensure that a setting a value on a key with a bucket value returns an error.\nfunc TestBucket_Put_IncompatibleValue(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb0, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := tx.Bucket([]byte(\"widgets\")).CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b0.Put([]byte(\"foo\"), []byte(\"bar\")); err != berrors.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a setting a value while the transaction is closed returns an error.\nfunc TestBucket_Put_Closed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != berrors.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that setting a value on a read-only bucket returns an error.\nfunc TestBucket_Put_ReadOnly(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != berrors.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can delete an existing key.\nfunc TestBucket_Delete(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Delete([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a large set of keys will work correctly.\nfunc TestBucket_Delete_Large(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strings.Repeat(\"*\", 1024))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Delete([]byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif v := b.Get([]byte(strconv.Itoa(i))); v != nil {\n\t\t\t\tt.Fatalf(\"unexpected value: %v, i=%d\", v, i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Deleting a very large list of keys will cause the freelist to use overflow.\nfunc TestBucket_Delete_FreelistOverflow(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tdb := btesting.MustCreateDB(t)\n\n\tk := make([]byte, 16)\n\t// The bigger the pages - the more values we need to write.\n\tfor i := uint64(0); i < 2*uint64(db.Info().PageSize); i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"0\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatalf(\"bucket error: %s\", err)\n\t\t\t}\n\n\t\t\tfor j := uint64(0); j < 1000; j++ {\n\t\t\t\tbinary.BigEndian.PutUint64(k[:8], i)\n\t\t\t\tbinary.BigEndian.PutUint64(k[8:], j)\n\t\t\t\tif err := b.Put(k, nil); err != nil {\n\t\t\t\t\tt.Fatalf(\"put error: %s\", err)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Delete all of them in one large transaction\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"0\"))\n\t\tc := b.Cursor()\n\t\tfor k, _ := c.First(); k != nil; k, _ = c.Next() {\n\t\t\tif err := c.Delete(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Check more than an overflow's worth of pages are freed.\n\tstats := db.Stats()\n\tfreePages := stats.FreePageN + stats.PendingPageN\n\tif freePages <= 0xFFFF {\n\t\tt.Fatalf(\"expected more than 0xFFFF free pages, got %v\", freePages)\n\t}\n\n\t// Free page count should be preserved on reopen.\n\tdb.MustClose()\n\tdb.MustReopen()\n\tif reopenFreePages := db.Stats().FreePageN; freePages != reopenFreePages {\n\t\tt.Fatalf(\"expected %d free pages, got %+v\", freePages, db.Stats())\n\t}\n\tif reopenPendingPages := db.Stats().PendingPageN; reopenPendingPages != 0 {\n\t\tt.Fatalf(\"expected no pending pages, got %+v\", db.Stats())\n\t}\n}\n\n// Ensure that deleting of non-existing key is a no-op.\nfunc TestBucket_Delete_NonExisting(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err = b.CreateBucket([]byte(\"nested\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tif err := b.Delete([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif b.Bucket([]byte(\"nested\")) == nil {\n\t\t\tt.Fatal(\"nested bucket has been deleted\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that accessing and updating nested buckets is ok across transactions.\nfunc TestBucket_Nested(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a widgets bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Create a widgets/foo bucket.\n\t\t_, err = b.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Create a widgets/bar key.\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Update widgets/bar.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"xxxx\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Cause a split.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tvar b = tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 10000; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Insert into widgets/foo/baz.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tvar b = tx.Bucket([]byte(\"widgets\"))\n\t\tif err := b.Bucket([]byte(\"foo\")).Put([]byte(\"baz\"), []byte(\"yyyy\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Verify.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvar b = tx.Bucket([]byte(\"widgets\"))\n\t\tif v := b.Bucket([]byte(\"foo\")).Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"yyyy\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tif v := b.Get([]byte(\"bar\")); !bytes.Equal(v, []byte(\"xxxx\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tfor i := 0; i < 10000; i++ {\n\t\t\tif v := b.Get([]byte(strconv.Itoa(i))); !bytes.Equal(v, []byte(strconv.Itoa(i))) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket using Delete() returns an error.\nfunc TestBucket_Delete_Bucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Delete([]byte(\"foo\")); err != berrors.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a key on a read-only bucket returns an error.\nfunc TestBucket_Delete_ReadOnly(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif err := tx.Bucket([]byte(\"widgets\")).Delete([]byte(\"foo\")); err != berrors.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a deleting value while the transaction is closed returns an error.\nfunc TestBucket_Delete_Closed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := b.Delete([]byte(\"foo\")); err != berrors.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that deleting a bucket causes nested buckets to be deleted.\nfunc TestBucket_DeleteBucket_Nested(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfoo, err := widgets.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tbar, err := foo.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := bar.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := tx.Bucket([]byte(\"widgets\")).DeleteBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket causes nested buckets to be deleted after they have been committed.\nfunc TestBucket_DeleteBucket_Nested2(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfoo, err := widgets.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tbar, err := foo.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := bar.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets := tx.Bucket([]byte(\"widgets\"))\n\t\tif widgets == nil {\n\t\t\tt.Fatal(\"expected widgets bucket\")\n\t\t}\n\n\t\tfoo := widgets.Bucket([]byte(\"foo\"))\n\t\tif foo == nil {\n\t\t\tt.Fatal(\"expected foo bucket\")\n\t\t}\n\n\t\tbar := foo.Bucket([]byte(\"bar\"))\n\t\tif bar == nil {\n\t\t\tt.Fatal(\"expected bar bucket\")\n\t\t}\n\n\t\tif v := bar.Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"bat\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) != nil {\n\t\t\tt.Fatal(\"expected bucket to be deleted\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a child bucket with multiple pages causes all pages to get collected.\n// NOTE: Consistency check in bolt_test.DB.Close() will panic if pages not freed properly.\nfunc TestBucket_DeleteBucket_Large(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfoo, err := widgets.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < 1000; i++ {\n\t\t\tif err := foo.Put([]byte(fmt.Sprintf(\"%d\", i)), []byte(fmt.Sprintf(\"%0100d\", i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a simple value retrieved via Bucket() returns a nil.\nfunc TestBucket_Bucket_IncompatibleValue(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := widgets.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif b := tx.Bucket([]byte(\"widgets\")).Bucket([]byte(\"foo\")); b != nil {\n\t\t\tt.Fatal(\"expected nil bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that creating a bucket on an existing non-bucket key returns an error.\nfunc TestBucket_CreateBucket_IncompatibleValue(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := widgets.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := widgets.CreateBucket([]byte(\"foo\")); err != berrors.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket on an existing non-bucket key returns an error.\nfunc TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := widgets.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := tx.Bucket([]byte(\"widgets\")).DeleteBucket([]byte(\"foo\")); err != berrors.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure bucket can set and update its sequence number.\nfunc TestBucket_Sequence(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tbkt, err := tx.CreateBucket([]byte(\"0\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Retrieve sequence.\n\t\tif v := bkt.Sequence(); v != 0 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", v)\n\t\t}\n\n\t\t// Update sequence.\n\t\tif err := bkt.SetSequence(1000); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Read sequence again.\n\t\tif v := bkt.Sequence(); v != 1000 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Verify sequence in separate transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif v := tx.Bucket([]byte(\"0\")).Sequence(); v != 1000 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can return an autoincrementing sequence.\nfunc TestBucket_NextSequence(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\twoojits, err := tx.CreateBucket([]byte(\"woojits\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Make sure sequence increments.\n\t\tif seq, err := widgets.NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if seq != 1 {\n\t\t\tt.Fatalf(\"unexpecte sequence: %d\", seq)\n\t\t}\n\n\t\tif seq, err := widgets.NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if seq != 2 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", seq)\n\t\t}\n\n\t\t// Buckets should be separate.\n\t\tif seq, err := woojits.NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if seq != 1 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", 1)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket will persist an autoincrementing sequence even if its\n// the only thing updated on the bucket.\n// https://github.com/boltdb/bolt/issues/296\nfunc TestBucket_NextSequence_Persist(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.Bucket([]byte(\"widgets\")).NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tseq, err := tx.Bucket([]byte(\"widgets\")).NextSequence()\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t} else if seq != 2 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", seq)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that retrieving the next sequence on a read-only bucket returns an error.\nfunc TestBucket_NextSequence_ReadOnly(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t_, err := tx.Bucket([]byte(\"widgets\")).NextSequence()\n\t\tif err != berrors.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that retrieving the next sequence for a bucket on a closed database return an error.\nfunc TestBucket_NextSequence_Closed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif _, err := b.NextSequence(); err != berrors.ErrTxClosed {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a user can loop over all key/value pairs in a bucket.\nfunc TestBucket_ForEach(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\ttype kv struct {\n\t\tk []byte\n\t\tv []byte\n\t}\n\n\texpectedItems := []kv{\n\t\t{k: []byte(\"bar\"), v: []byte(\"0002\")},\n\t\t{k: []byte(\"baz\"), v: []byte(\"0001\")},\n\t\t{k: []byte(\"csubbucket\"), v: nil},\n\t\t{k: []byte(\"foo\"), v: []byte(\"0000\")},\n\t}\n\n\tverifyReads := func(b *bolt.Bucket) {\n\t\tvar items []kv\n\t\terr := b.ForEach(func(k, v []byte) error {\n\t\t\titems = append(items, kv{k: k, v: v})\n\t\t\treturn nil\n\t\t})\n\t\tassert.NoErrorf(t, err, \"b.ForEach failed\")\n\t\tassert.Equal(t, expectedItems, items, \"what we iterated (ForEach) is not what we put\")\n\t}\n\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\trequire.NoError(t, err, \"bucket creation failed\")\n\n\t\trequire.NoErrorf(t, b.Put([]byte(\"foo\"), []byte(\"0000\")), \"put 'foo' failed\")\n\t\trequire.NoErrorf(t, b.Put([]byte(\"baz\"), []byte(\"0001\")), \"put 'baz' failed\")\n\t\trequire.NoErrorf(t, b.Put([]byte(\"bar\"), []byte(\"0002\")), \"put 'bar' failed\")\n\t\t_, err = b.CreateBucket([]byte(\"csubbucket\"))\n\t\trequire.NoErrorf(t, err, \"creation of subbucket failed\")\n\n\t\tverifyReads(b)\n\n\t\treturn nil\n\t})\n\trequire.NoErrorf(t, err, \"db.Update failed\")\n\terr = db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\trequire.NotNil(t, b, \"bucket opening failed\")\n\t\tverifyReads(b)\n\t\treturn nil\n\t})\n\tassert.NoErrorf(t, err, \"db.View failed\")\n}\n\nfunc TestBucket_ForEachBucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\texpectedItems := [][]byte{\n\t\t[]byte(\"csubbucket\"),\n\t\t[]byte(\"zsubbucket\"),\n\t}\n\n\tverifyReads := func(b *bolt.Bucket) {\n\t\tvar items [][]byte\n\t\terr := b.ForEachBucket(func(k []byte) error {\n\t\t\titems = append(items, k)\n\t\t\treturn nil\n\t\t})\n\t\tassert.NoErrorf(t, err, \"b.ForEach failed\")\n\t\tassert.Equal(t, expectedItems, items, \"what we iterated (ForEach) is not what we put\")\n\t}\n\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\trequire.NoError(t, err, \"bucket creation failed\")\n\n\t\trequire.NoErrorf(t, b.Put([]byte(\"foo\"), []byte(\"0000\")), \"put 'foo' failed\")\n\t\t_, err = b.CreateBucket([]byte(\"zsubbucket\"))\n\t\trequire.NoErrorf(t, err, \"creation of subbucket failed\")\n\t\trequire.NoErrorf(t, b.Put([]byte(\"baz\"), []byte(\"0001\")), \"put 'baz' failed\")\n\t\trequire.NoErrorf(t, b.Put([]byte(\"bar\"), []byte(\"0002\")), \"put 'bar' failed\")\n\t\t_, err = b.CreateBucket([]byte(\"csubbucket\"))\n\t\trequire.NoErrorf(t, err, \"creation of subbucket failed\")\n\n\t\tverifyReads(b)\n\n\t\treturn nil\n\t})\n\tassert.NoErrorf(t, err, \"db.Update failed\")\n\terr = db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\trequire.NotNil(t, b, \"bucket opening failed\")\n\t\tverifyReads(b)\n\t\treturn nil\n\t})\n\tassert.NoErrorf(t, err, \"db.View failed\")\n}\n\nfunc TestBucket_ForEachBucket_NoBuckets(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tverifyReads := func(b *bolt.Bucket) {\n\t\tvar items [][]byte\n\t\terr := b.ForEachBucket(func(k []byte) error {\n\t\t\titems = append(items, k)\n\t\t\treturn nil\n\t\t})\n\t\tassert.NoErrorf(t, err, \"b.ForEach failed\")\n\t\tassert.Emptyf(t, items, \"what we iterated (ForEach) is not what we put\")\n\t}\n\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\trequire.NoError(t, err, \"bucket creation failed\")\n\n\t\trequire.NoErrorf(t, b.Put([]byte(\"foo\"), []byte(\"0000\")), \"put 'foo' failed\")\n\t\trequire.NoErrorf(t, err, \"creation of subbucket failed\")\n\t\trequire.NoErrorf(t, b.Put([]byte(\"baz\"), []byte(\"0001\")), \"put 'baz' failed\")\n\t\trequire.NoErrorf(t, err, \"creation of subbucket failed\")\n\n\t\tverifyReads(b)\n\n\t\treturn nil\n\t})\n\trequire.NoErrorf(t, err, \"db.Update failed\")\n\n\terr = db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\trequire.NotNil(t, b, \"bucket opening failed\")\n\t\tverifyReads(b)\n\t\treturn nil\n\t})\n\tassert.NoErrorf(t, err, \"db.View failed\")\n}\n\n// Ensure a database can stop iteration early.\nfunc TestBucket_ForEach_ShortCircuit(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tvar index int\n\t\tif err := tx.Bucket([]byte(\"widgets\")).ForEach(func(k, v []byte) error {\n\t\t\tindex++\n\t\t\tif bytes.Equal(k, []byte(\"baz\")) {\n\t\t\t\treturn errors.New(\"marker\")\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err == nil || err.Error() != \"marker\" {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\tif index != 2 {\n\t\t\tt.Fatalf(\"unexpected index: %d\", index)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that looping over a bucket on a closed database returns an error.\nfunc TestBucket_ForEach_Closed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := b.ForEach(func(k, v []byte) error { return nil }); err != berrors.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that an error is returned when inserting with an empty key.\nfunc TestBucket_Put_EmptyKey(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"\"), []byte(\"bar\")); err != berrors.ErrKeyRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\tif err := b.Put(nil, []byte(\"bar\")); err != berrors.ErrKeyRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that an error is returned when inserting with a key that's too large.\nfunc TestBucket_Put_KeyTooLarge(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put(make([]byte, 32769), []byte(\"bar\")); err != berrors.ErrKeyTooLarge {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that an error is returned when inserting a value that's too large.\nfunc TestBucket_Put_ValueTooLarge(t *testing.T) {\n\t// Skip this test on DroneCI because the machine is resource constrained.\n\tif os.Getenv(\"DRONE\") == \"true\" {\n\t\tt.Skip(\"not enough RAM for test\")\n\t}\n\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), make([]byte, bolt.MaxValueSize+1)); err != berrors.ErrValueTooLarge {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket can calculate stats.\nfunc TestBucket_Stats(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode\")\n\t}\n\n\tdb := btesting.MustCreateDB(t)\n\n\t// Add bucket with fewer keys but one big value.\n\tbigKey := []byte(\"really-big-value\")\n\tfor i := 0; i < 500; i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"woojits\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"%03d\", i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\tlongKeyLength := 10*db.Info().PageSize + 17\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.Bucket([]byte(\"woojits\")).Put(bigKey, []byte(strings.Repeat(\"*\", longKeyLength))); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tpageSize2stats := map[int]bolt.BucketStats{\n\t\t4096: {\n\t\t\tBranchPageN:     1,\n\t\t\tBranchOverflowN: 0,\n\t\t\tLeafPageN:       7,\n\t\t\tLeafOverflowN:   10,\n\t\t\tKeyN:            501,\n\t\t\tDepth:           2,\n\t\t\tBranchAlloc:     4096,\n\t\t\tBranchInuse:     149,\n\t\t\tLeafAlloc:       69632,\n\t\t\tLeafInuse: 0 +\n\t\t\t\t7*16 + // leaf page header (x LeafPageN)\n\t\t\t\t501*16 + // leaf elements\n\t\t\t\t500*3 + len(bigKey) + // leaf keys\n\t\t\t\t1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ...\n\t\t\tBucketN:           1,\n\t\t\tInlineBucketN:     0,\n\t\t\tInlineBucketInuse: 0},\n\t\t16384: {\n\t\t\tBranchPageN:     1,\n\t\t\tBranchOverflowN: 0,\n\t\t\tLeafPageN:       3,\n\t\t\tLeafOverflowN:   10,\n\t\t\tKeyN:            501,\n\t\t\tDepth:           2,\n\t\t\tBranchAlloc:     16384,\n\t\t\tBranchInuse:     73,\n\t\t\tLeafAlloc:       212992,\n\t\t\tLeafInuse: 0 +\n\t\t\t\t3*16 + // leaf page header (x LeafPageN)\n\t\t\t\t501*16 + // leaf elements\n\t\t\t\t500*3 + len(bigKey) + // leaf keys\n\t\t\t\t1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ...\n\t\t\tBucketN:           1,\n\t\t\tInlineBucketN:     0,\n\t\t\tInlineBucketInuse: 0},\n\t\t65536: {\n\t\t\tBranchPageN:     1,\n\t\t\tBranchOverflowN: 0,\n\t\t\tLeafPageN:       2,\n\t\t\tLeafOverflowN:   10,\n\t\t\tKeyN:            501,\n\t\t\tDepth:           2,\n\t\t\tBranchAlloc:     65536,\n\t\t\tBranchInuse:     54,\n\t\t\tLeafAlloc:       786432,\n\t\t\tLeafInuse: 0 +\n\t\t\t\t2*16 + // leaf page header (x LeafPageN)\n\t\t\t\t501*16 + // leaf elements\n\t\t\t\t500*3 + len(bigKey) + // leaf keys\n\t\t\t\t1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ...\n\t\t\tBucketN:           1,\n\t\t\tInlineBucketN:     0,\n\t\t\tInlineBucketInuse: 0},\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"woojits\")).Stats()\n\t\tt.Logf(\"Stats: %#v\", stats)\n\t\tif expected, ok := pageSize2stats[db.Info().PageSize]; ok {\n\t\t\tassert.EqualValues(t, expected, stats, \"stats differs from expectations\")\n\t\t} else {\n\t\t\tt.Skipf(\"No expectations for page size: %d\", db.Info().PageSize)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket with random insertion utilizes fill percentage correctly.\nfunc TestBucket_Stats_RandomFill(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t} else if os.Getpagesize() != 4096 {\n\t\tt.Skip(\"invalid page size for test\")\n\t}\n\n\tdb := btesting.MustCreateDB(t)\n\n\t// Add a set of values in random order. It will be the same random\n\t// order so we can maintain consistency between test runs.\n\tvar count int\n\trand := rand.New(rand.NewSource(42))\n\tfor _, i := range rand.Perm(1000) {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"woojits\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tb.FillPercent = 0.9\n\t\t\tfor _, j := range rand.Perm(100) {\n\t\t\t\tindex := (j * 10000) + i\n\t\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"%d000000000000000\", index)), []byte(\"0000000000\")); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t\tcount++\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"woojits\")).Stats()\n\t\tif stats.KeyN != 100000 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t}\n\n\t\tif stats.BranchPageN != 98 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.BranchInuse != 130984 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t} else if stats.BranchAlloc != 401408 {\n\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t}\n\n\t\tif stats.LeafPageN != 3412 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.LeafInuse != 4742482 {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t} else if stats.LeafAlloc != 13975552 {\n\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket can calculate stats.\nfunc TestBucket_Stats_Small(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Add a bucket that fits on a single root leaf.\n\t\tb, err := tx.CreateBucket([]byte(\"whozawhats\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"whozawhats\"))\n\t\tstats := b.Stats()\n\t\tif stats.BranchPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 1 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 1 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t} else if stats.BranchInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t} else if stats.LeafInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\tif db.Info().PageSize == 4096 {\n\t\t\tif stats.BranchAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != 16+16+6 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestBucket_Stats_EmptyBucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Add a bucket that fits on a single root leaf.\n\t\tif _, err := tx.CreateBucket([]byte(\"whozawhats\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"whozawhats\"))\n\t\tstats := b.Stats()\n\t\tif stats.BranchPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 0 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 1 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t} else if stats.BranchInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t} else if stats.LeafInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\tif db.Info().PageSize == 4096 {\n\t\t\tif stats.BranchAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != 16 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket can calculate stats.\nfunc TestBucket_Stats_Nested(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"%02d\", i)), []byte(fmt.Sprintf(\"%02d\", i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tbar, err := b.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tif err := bar.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tbaz, err := bar.CreateBucket([]byte(\"baz\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tif err := baz.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"foo\"))\n\t\tstats := b.Stats()\n\t\tif stats.BranchPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 2 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 122 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 3 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t} else if stats.BranchInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t}\n\n\t\tfoo := 16            // foo (pghdr)\n\t\tfoo += 101 * 16      // foo leaf elements\n\t\tfoo += 100*2 + 100*2 // foo leaf key/values\n\t\tfoo += 3 + 16        // foo -> bar key/value\n\n\t\tbar := 16      // bar (pghdr)\n\t\tbar += 11 * 16 // bar leaf elements\n\t\tbar += 10 + 10 // bar leaf key/values\n\t\tbar += 3 + 16  // bar -> baz key/value\n\n\t\tbaz := 16      // baz (inline) (pghdr)\n\t\tbaz += 10 * 16 // baz leaf elements\n\t\tbaz += 10 + 10 // baz leaf key/values\n\n\t\tif stats.LeafInuse != foo+bar+baz {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\tif db.Info().PageSize == 4096 {\n\t\t\tif stats.BranchAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 8192 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 3 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != baz {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestBucket_Inspect(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\texpectedStructure := bolt.BucketStructure{\n\t\tName: \"root\",\n\t\tKeyN: 0,\n\t\tChildren: []bolt.BucketStructure{\n\t\t\t{\n\t\t\t\tName: \"b1\",\n\t\t\t\tKeyN: 3,\n\t\t\t\tChildren: []bolt.BucketStructure{\n\t\t\t\t\t{\n\t\t\t\t\t\tName: \"b1_1\",\n\t\t\t\t\t\tKeyN: 6,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tName: \"b1_2\",\n\t\t\t\t\t\tKeyN: 7,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tName: \"b1_3\",\n\t\t\t\t\t\tKeyN: 8,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tName: \"b2\",\n\t\t\t\tKeyN: 4,\n\t\t\t\tChildren: []bolt.BucketStructure{\n\t\t\t\t\t{\n\t\t\t\t\t\tName: \"b2_1\",\n\t\t\t\t\t\tKeyN: 10,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tName: \"b2_2\",\n\t\t\t\t\t\tKeyN: 12,\n\t\t\t\t\t\tChildren: []bolt.BucketStructure{\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tName: \"b2_2_1\",\n\t\t\t\t\t\t\t\tKeyN: 2,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tName: \"b2_2_2\",\n\t\t\t\t\t\t\t\tKeyN: 3,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tName: \"b2_3\",\n\t\t\t\t\t\tKeyN: 11,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t}\n\n\ttype bucketItem struct {\n\t\tb  *bolt.Bucket\n\t\tbs bolt.BucketStructure\n\t}\n\n\tt.Log(\"Populating the database\")\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tqueue := []bucketItem{\n\t\t\t{\n\t\t\t\tb:  nil,\n\t\t\t\tbs: expectedStructure,\n\t\t\t},\n\t\t}\n\n\t\tfor len(queue) > 0 {\n\t\t\titem := queue[0]\n\t\t\tqueue = queue[1:]\n\n\t\t\tif item.b != nil {\n\t\t\t\tfor i := 0; i < item.bs.KeyN; i++ {\n\t\t\t\t\terr := item.b.Put([]byte(fmt.Sprintf(\"%02d\", i)), []byte(fmt.Sprintf(\"%02d\", i)))\n\t\t\t\t\trequire.NoError(t, err)\n\t\t\t\t}\n\n\t\t\t\tfor _, child := range item.bs.Children {\n\t\t\t\t\tchildBucket, err := item.b.CreateBucket([]byte(child.Name))\n\t\t\t\t\trequire.NoError(t, err)\n\t\t\t\t\tqueue = append(queue, bucketItem{b: childBucket, bs: child})\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfor _, child := range item.bs.Children {\n\t\t\t\t\tchildBucket, err := tx.CreateBucket([]byte(child.Name))\n\t\t\t\t\trequire.NoError(t, err)\n\t\t\t\t\tqueue = append(queue, bucketItem{b: childBucket, bs: child})\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\n\tt.Log(\"Inspecting the database\")\n\t_ = db.View(func(tx *bolt.Tx) error {\n\t\tactualStructure := tx.Inspect()\n\t\tassert.Equal(t, expectedStructure, actualStructure)\n\t\treturn nil\n\t})\n}\n\n// Ensure a large bucket can calculate stats.\nfunc TestBucket_Stats_Large(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tdb := btesting.MustCreateDB(t)\n\n\tvar index int\n\tfor i := 0; i < 100; i++ {\n\t\t// Add bucket with lots of keys.\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"widgets\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tfor i := 0; i < 1000; i++ {\n\t\t\t\tif err := b.Put([]byte(strconv.Itoa(index)), []byte(strconv.Itoa(index))); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t\tindex++\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tdb.MustCheck()\n\n\tpageSize2stats := map[int]bolt.BucketStats{\n\t\t4096: {\n\t\t\tBranchPageN:       13,\n\t\t\tBranchOverflowN:   0,\n\t\t\tLeafPageN:         1196,\n\t\t\tLeafOverflowN:     0,\n\t\t\tKeyN:              100000,\n\t\t\tDepth:             3,\n\t\t\tBranchAlloc:       53248,\n\t\t\tBranchInuse:       25257,\n\t\t\tLeafAlloc:         4898816,\n\t\t\tLeafInuse:         2596916,\n\t\t\tBucketN:           1,\n\t\t\tInlineBucketN:     0,\n\t\t\tInlineBucketInuse: 0},\n\t\t16384: {\n\t\t\tBranchPageN:       1,\n\t\t\tBranchOverflowN:   0,\n\t\t\tLeafPageN:         292,\n\t\t\tLeafOverflowN:     0,\n\t\t\tKeyN:              100000,\n\t\t\tDepth:             2,\n\t\t\tBranchAlloc:       16384,\n\t\t\tBranchInuse:       6094,\n\t\t\tLeafAlloc:         4784128,\n\t\t\tLeafInuse:         2582452,\n\t\t\tBucketN:           1,\n\t\t\tInlineBucketN:     0,\n\t\t\tInlineBucketInuse: 0},\n\t\t65536: {\n\t\t\tBranchPageN:       1,\n\t\t\tBranchOverflowN:   0,\n\t\t\tLeafPageN:         73,\n\t\t\tLeafOverflowN:     0,\n\t\t\tKeyN:              100000,\n\t\t\tDepth:             2,\n\t\t\tBranchAlloc:       65536,\n\t\t\tBranchInuse:       1534,\n\t\t\tLeafAlloc:         4784128,\n\t\t\tLeafInuse:         2578948,\n\t\t\tBucketN:           1,\n\t\t\tInlineBucketN:     0,\n\t\t\tInlineBucketInuse: 0},\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"widgets\")).Stats()\n\t\tt.Logf(\"Stats: %#v\", stats)\n\t\tif expected, ok := pageSize2stats[db.Info().PageSize]; ok {\n\t\t\tassert.EqualValues(t, expected, stats, \"stats differs from expectations\")\n\t\t} else {\n\t\t\tt.Skipf(\"No expectations for page size: %d\", db.Info().PageSize)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can write random keys and values across multiple transactions.\nfunc TestBucket_Put_Single(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tindex := 0\n\tif err := quick.Check(func(items testdata) bool {\n\t\tdb := btesting.MustCreateDB(t)\n\t\tdefer db.MustClose()\n\n\t\tm := make(map[string][]byte)\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor _, item := range items {\n\t\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tif err := tx.Bucket([]byte(\"widgets\")).Put(item.Key, item.Value); err != nil {\n\t\t\t\t\tpanic(\"put error: \" + err.Error())\n\t\t\t\t}\n\t\t\t\tm[string(item.Key)] = item.Value\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\t// Verify all key/values so far.\n\t\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\t\ti := 0\n\t\t\t\tfor k, v := range m {\n\t\t\t\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(k))\n\t\t\t\t\tif !bytes.Equal(value, v) {\n\t\t\t\t\t\tt.Logf(\"value mismatch [run %d] (%d of %d):\\nkey: %x\\ngot: %x\\nexp: %x\", index, i, len(m), []byte(k), value, v)\n\t\t\t\t\t\tdb.CopyTempFile()\n\t\t\t\t\t\tt.FailNow()\n\t\t\t\t\t}\n\t\t\t\t\ti++\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tindex++\n\t\treturn true\n\t}, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a transaction can insert multiple key/value pairs at once.\nfunc TestBucket_Put_Multiple(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tif err := quick.Check(func(items testdata) bool {\n\t\tdb := btesting.MustCreateDB(t)\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\t\tfor _, item := range items {\n\t\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Verify all items exist.\n\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\t\tfor _, item := range items {\n\t\t\t\tvalue := b.Get(item.Key)\n\t\t\t\tif !bytes.Equal(item.Value, value) {\n\t\t\t\t\tdb.CopyTempFile()\n\t\t\t\t\tt.Fatalf(\"exp=%x; got=%x\", item.Value, value)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a transaction can delete all key/value pairs and return to a single leaf page.\nfunc TestBucket_Delete_Quick(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tif err := quick.Check(func(items testdata) bool {\n\t\tdb := btesting.MustCreateDB(t)\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\t\tfor _, item := range items {\n\t\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Remove items one at a time and check consistency.\n\t\tfor _, item := range items {\n\t\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\treturn tx.Bucket([]byte(\"widgets\")).Delete(item.Key)\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\t// Anything before our deletion index should be nil.\n\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\tif err := tx.Bucket([]byte(\"widgets\")).ForEach(func(k, v []byte) error {\n\t\t\t\tt.Fatalf(\"bucket should be empty; found: %06x\", trunc(k, 3))\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\nfunc BenchmarkBucket_CreateBucketIfNotExists(b *testing.B) {\n\tdb := btesting.MustCreateDB(b)\n\tdefer db.MustClose()\n\n\tconst bucketCount = 1_000_000\n\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tfor i := 0; i < bucketCount; i++ {\n\t\t\tbucketName := fmt.Sprintf(\"bucket_%d\", i)\n\t\t\t_, berr := tx.CreateBucket([]byte(bucketName))\n\t\t\trequire.NoError(b, berr)\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(b, err)\n\n\tb.ResetTimer()\n\tb.ReportAllocs()\n\n\tfor i := 0; i < b.N; i++ {\n\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t_, berr := tx.CreateBucketIfNotExists([]byte(\"bucket_100\"))\n\t\t\treturn berr\n\t\t})\n\t\trequire.NoError(b, err)\n\t}\n}\n\nfunc ExampleBucket_Put() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Set the value \"bar\" for the key \"foo\".\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Read value back in a different read-only transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value of 'foo' is: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value of 'foo' is: bar\n}\n\nfunc ExampleBucket_Delete() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Set the value \"bar\" for the key \"foo\".\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Retrieve the key back from the database and verify it.\n\t\tvalue := b.Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value of 'foo' was: %s\\n\", value)\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Delete the key in a different write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\treturn tx.Bucket([]byte(\"widgets\")).Delete([]byte(\"foo\"))\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Retrieve the key again.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tif value == nil {\n\t\t\tfmt.Printf(\"The value of 'foo' is now: nil\\n\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value of 'foo' was: bar\n\t// The value of 'foo' is now: nil\n}\n\nfunc ExampleBucket_ForEach() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Insert data into a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"animals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif err := b.Put([]byte(\"dog\"), []byte(\"fun\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"cat\"), []byte(\"lame\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"liger\"), []byte(\"awesome\")); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Iterate over items in sorted key order.\n\t\tif err := b.ForEach(func(k, v []byte) error {\n\t\t\tfmt.Printf(\"A %s is %s.\\n\", k, v)\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// A cat is lame.\n\t// A dog is fun.\n\t// A liger is awesome.\n}\n"
  },
  {
    "path": "cmd/bbolt/OWNERS",
    "content": "# See the OWNERS docs at https://go.k8s.io/owners\n\napprovers:\n  - ahrtr           # Benjamin Wang <benjamin.ahrtr@gmail.com> <benjamin.wang@broadcom.com>\n  - elbehery        # Mustafa Elbehery <elbeherymustafa@gmail.com>\n  - fuweid          # Wei Fu <fuweid89@gmail.com>\n  - serathius       # Marek Siarkowicz <siarkowicz@google.com> <marek.siarkowicz@gmail.com>\n  - ptabor          # Piotr Tabor <piotr.tabor@gmail.com>\n  - spzala          # Sahdev Zala <spzala@us.ibm.com>\n  - tjungblu        # Thomas Jungblut <tjungblu@redhat.com>\nreviewers:\n  - ivanvc          # Ivan Valdes <ivan@vald.es> \n"
  },
  {
    "path": "cmd/bbolt/README.md",
    "content": "# Introduction to bbolt command line\n\n`bbolt` provides a command line utility for inspecting and manipulating bbolt database files. To install bbolt command-line please refer [here](https://github.com/etcd-io/bbolt#installing)\n\n**Note**: [etcd](https://github.com/etcd-io/etcd) uses bbolt as its backend storage engine. In this document, we take etcd as an example to demonstrate the usage of bbolt commands. Refer to [install etcd](https://etcd.io/docs/v3.5/install/) for installing etcd.\n\n1. Start a single member etcd cluster with this command below:\n\n    ```bash\n    $etcd\n    ```\n\n    It will create a directory `default.etcd` by default under current working directory, and the directory structure will look like this:\n\n    ```bash\n    $tree default.etcd\n    default.etcd\n    └── member\n        ├── snap\n        │   └── db // this is bbolt database file\n        └── wal\n            └── 0000000000000000-0000000000000000.wal\n\n    3 directories, 2 files\n    ```\n\n2. Put some dummy data using [etcdctl](https://github.com/etcd-io/etcd/tree/main/etcdctl).\n3. Stop the etcd instance. Note a bbolt database file can only be opened by one read-write process, because it is exclusively locked when opened.\n\n## Usage\n\n- `bbolt command [arguments]`\n\n### help\n\n- help will print information about that command\n\n  ```bash\n  $bbolt help\n\n  The commands are:\n\n      version     prints the current version of bbolt\n      bench       run synthetic benchmark against bbolt\n      buckets     print a list of buckets\n      check       verifies integrity of bbolt database\n      compact     copies a bbolt database, compacting it in the process\n      dump        print a hexadecimal dump of a single page\n      get         print the value of a key in a bucket\n      info        print basic info\n      keys        print a list of keys in a bucket\n      help        print this screen\n      page        print one or more pages in human readable format\n      pages       print list of pages with their types\n      page-item   print the key and value of a page item.\n      stats       iterate over all pages and generate usage stats\n      surgery     perform surgery on bbolt database\n  ```\n\n- you can use `help` with any command: `bbolt [command] -h` for more information about command.\n\n## Analyse bbolt database with bbolt command line\n\n### version\n\n- `version` print the current version information of bbolt command-line.\n- usage:\n  `bbolt version`\n\n  Example:\n  \n  ```bash\n  $bbolt version\n  bbolt version: 1.3.7\n  Go Version: go1.21.6\n  Go OS/Arch: darwin/arm64\n  ```\n\n### info\n\n- `info` print the basic information about the given Bbolt database.\n- usage:\n  `bbolt info [path to the bbolt database]`\n\n    Example:\n\n    ```bash\n    $bbolt info ~/default.etcd/member/snap/db\n    Page Size: 4096\n    ```\n\n  - **note**: page size is given in bytes\n  - Bbolt database is using page size of 4KB\n\n### buckets\n\n- `buckets` print a list of buckets of Bbolt database is currently having. Find more information on buckets [here](https://github.com/etcd-io/bbolt#using-buckets)\n- usage:\n  `bbolt buckets [path to the bbolt database]`\n\n    Example:\n\n    ```bash\n    $bbolt buckets ~/default.etcd/member/snap/db\n    alarm\n    auth\n    authRoles\n    authUsers\n    cluster\n    key\n    lease\n    members\n    members_removed\n    meta\n    ```\n\n  - It means when you start an etcd, it creates these `10` buckets using bbolt database.\n\n### check\n\n- `check` opens a database at a given `[PATH]` and runs an exhaustive check to verify that all pages are accessible or are marked as freed. It also verifies that no pages are double referenced.\n- usage:\n  `bbolt check [path to the bbolt database]`\n\n    Example:\n\n    ```bash\n    $bbolt check ~/default.etcd/member/snap/db\n    ok\n    ```\n\n  - It returns `ok` as our database file `db` is not corrupted.\n\n### stats\n\n- To gather essential statistics about the bbolt database: `stats` performs an extensive search of the database to track every page reference. It starts at the current meta page and recursively iterates through every accessible bucket.\n- usage:\n  `bbolt stats [path to the bbolt database]`\n\n  Example:\n\n  ```bash\n  $bbolt stats ~/default.etcd/member/snap/db\n  Aggregate statistics for 10 buckets\n\n  Page count statistics\n      Number of logical branch pages: 0\n      Number of physical branch overflow pages: 0\n      Number of logical leaf pages: 0\n      Number of physical leaf overflow pages: 0\n  Tree statistics\n      Number of keys/value pairs: 11\n      Number of levels in B+tree: 1\n  Page size utilization\n      Bytes allocated for physical branch pages: 0\n      Bytes actually used for branch data: 0 (0%)\n      Bytes allocated for physical leaf pages: 0\n      Bytes actually used for leaf data: 0 (0%)\n  Bucket statistics\n      Total number of buckets: 10\n      Total number on inlined buckets: 10 (100%)\n      Bytes used for inlined buckets: 780 (0%)\n  ```\n\n### inspect\n- `inspect` inspect the structure of the database.\n- Usage: `bbolt inspect [path to the bbolt database]`\n\n  Example:\n```bash\n$ ./bbolt inspect ~/default.etcd/member/snap/db\n{\n    \"name\": \"root\",\n    \"keyN\": 0,\n    \"buckets\": [\n        {\n            \"name\": \"alarm\",\n            \"keyN\": 0\n        },\n        {\n            \"name\": \"auth\",\n            \"keyN\": 2\n        },\n        {\n            \"name\": \"authRoles\",\n            \"keyN\": 1\n        },\n        {\n            \"name\": \"authUsers\",\n            \"keyN\": 1\n        },\n        {\n            \"name\": \"cluster\",\n            \"keyN\": 1\n        },\n        {\n            \"name\": \"key\",\n            \"keyN\": 1285\n        },\n        {\n            \"name\": \"lease\",\n            \"keyN\": 2\n        },\n        {\n            \"name\": \"members\",\n            \"keyN\": 1\n        },\n        {\n            \"name\": \"members_removed\",\n            \"keyN\": 0\n        },\n        {\n            \"name\": \"meta\",\n            \"keyN\": 3\n        }\n    ]\n}\n```\n\n### pages\n\n- Pages prints a table of pages with their type (meta, leaf, branch, freelist).\n- The `meta` will store the metadata information of database.\n- The `leaf` and `branch` pages will show a key count in the `items` column.\n- The `freelist` will show the number of free pages, which are free for writing again.\n- The `overflow` column shows the number of blocks that the page spills over into.\n- usage:\n  `bbolt pages [path to the bbolt database]`\n\n  Example:\n\n  ```bash\n  $bbolt pages ~/default.etcd/member/snap/db\n  ID       TYPE       ITEMS  OVRFLW\n  ======== ========== ====== ======\n  0        meta       0\n  1        meta       0\n  2        free\n  3        leaf       10\n  4        freelist   2\n  5        free\n  ```\n\n### page\n\n- Page prints one or more pages in human readable format.\n- usage:\n\n  ```bash\n  bolt page [path to the bbolt database] pageid [pageid...]\n  or: bolt page --all [path to the bbolt database]\n\n  Additional options include:\n\n  --all\n    prints all pages (only skips pages that were considered successful overflow pages)\n  --format-value=auto|ascii-encoded|hex|bytes|redacted (default: auto)\n    prints values (on the leaf page) using the given format\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt page ~/default.etcd/member/snap/db 3\n  Page ID:    3\n  Page Type:  leaf\n  Total Size: 4096 bytes\n  Overflow pages: 0\n  Item Count: 10\n\n  \"alarm\": <pgid=0,seq=0>\n  \"auth\": <pgid=0,seq=0>\n  \"authRoles\": <pgid=0,seq=0>\n  \"authUsers\": <pgid=0,seq=0>\n  \"cluster\": <pgid=0,seq=0>\n  \"key\": <pgid=0,seq=0>\n  \"lease\": <pgid=0,seq=0>\n  \"members\": <pgid=0,seq=0>\n  \"members_removed\": <pgid=0,seq=0>\n  \"meta\": <pgid=0,seq=0>\n  ```\n\n  - It prints information of page `page ID: 3`\n\n### page-item\n\n- page-item prints a page item's key and value.\n- usage:\n\n  ```bash\n  bolt page-item [options] [path to the bbolt database] <pageId> <itemId>\n  Additional options include:\n\n      --key-only\n          Print only the key\n      --value-only\n          Print only the value\n      --format\n          Output format. One of: auto|ascii-encoded|hex|bytes|redacted (default=auto)\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt page-item --key-only ~/default.etcd/member/snap/db 3 7\n  \"members\"\n  ```\n\n  - It returns the key as `--key-only` flag is passed of `pageID: 3` and `itemID: 7`\n\n### dump\n\n- Dump prints a hexadecimal dump of one or more given pages.\n- usage:\n  `bolt dump [path to the bbolt database] [pageid...]`\n\n### keys\n\n- Print a list of keys in the given bucket.\n- usage:\n\n  ```bash\n  bolt keys [path to the bbolt database] [BucketName]\n\n  Additional options include:\n  --format\n    Output format. One of: auto|ascii-encoded|hex|bytes|redacted (default=auto)\n  ```\n\n  Example 1:\n\n  ```bash\n  $bbolt keys ~/default.etcd/member/snap/db meta\n  confState\n  consistent_index\n  term\n  ```\n\n  - It list all the keys in bucket: `meta`\n\n  Example 2:\n\n  ```bash\n  $bbolt keys ~/default.etcd/member/snap/db members\n  8e9e05c52164694d\n  ```\n\n  - It list all the keys in `members` bucket which is a `memberId` of etcd cluster member.\n  - In this case we are running a single member etcd cluster, hence only `one memberId` is present. If we would have run a `3` member etcd cluster then it will return a `3 memberId` as `3 cluster members` would have been present in `members` bucket.\n\n### get\n\n- Print the value of the given key in the given bucket.\n- usage:\n  \n  ```bash\n  bolt get [path to the bbolt database] [BucketName] [Key]\n\n  Additional options include:\n  --format\n    Output format. One of: auto|ascii-encoded|hex|bytes|redacted (default=auto)\n  --parse-format\n    Input format (of key). One of: ascii-encoded|hex (default=ascii-encoded)\"\n  ```\n\n  Example 1:\n\n  ```bash\n  $bbolt get --format=hex ~/default.etcd/member/snap/db meta term\n  0000000000000004\n  ```\n\n  - It returns the value present in bucket: `meta` for key: `term` in hexadecimal format.\n\n  Example 2:\n\n  ```bash\n  $bbolt get ~/default.etcd/member/snap/db members 8e9e05c52164694d\n  {\"id\":10276657743932975437,\"peerURLs\":[\"http://localhost:2380\"],\"name\":\"default\",\"clientURLs\":[\"http://localhost:2379\"]}\n  ```\n\n  - It returns the value present in bucket: `members` for key: `8e9e05c52164694d`.\n\n### compact\n\n- Compact opens a database at given `[Source Path]` and walks it recursively, copying keys as they are found from all buckets, to a newly created database at `[Destination Path]`. The original database is left untouched.\n- usage:\n\n  ```bash\n  bbolt compact [options] -o [Destination Path] [Source Path]\n\n  Additional options include:\n\n  -tx-max-size NUM\n    Specifies the maximum size of individual transactions.\n    Defaults to 64KB\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt compact -o ~/db.compact ~/default.etcd/member/snap/db\n  16805888 -> 32768 bytes (gain=512.88x)\n  ```\n\n  - It will create a compacted database file: `db.compact` at given path.\n\n### bench\n\n- run synthetic benchmark against bbolt database.\n- usage:\n\n    ```bash\n    Usage:\n    -batch-size int\n\n    -blockprofile string\n\n    -count int\n            (default 1000)\n    -cpuprofile string\n\n    -fill-percent float\n            (default 0.5)\n    -key-size int\n            (default 8)\n    -memprofile string\n\n    -no-sync\n\n    -path string\n\n    -profile-mode string\n            (default \"rw\")\n    -read-mode string\n            (default \"seq\")\n    -value-size int\n            (default 32)\n    -work\n\n    -write-mode string\n            (default \"seq\")\n    ```\n\n    Example:\n\n    ```bash\n    $bbolt bench ~/default.etcd/member/snap/db -batch-size 400 -key-size 16\n    # Write\t68.523572ms\t(68.523µs/op)\t(14593 op/sec)\n    # Read\t1.000015152s\t(11ns/op)\t(90909090 op/sec)\n    ```\n\n  - It runs a benchmark with batch size of `400` and with key size of `16` while for others parameters default value is taken.\n\n### surgery\n\n- `surgery` perform surgery on bbolt database for repair and recovery operations.\n- usage:\n  `bbolt surgery <subcommand> [arguments]`\n\n  The surgery subcommands are:\n  - `revert-meta-page` - revert to previous transaction state\n  - `copy-page` - copy page from source to destination\n  - `clear-page` - clear all elements from a page\n  - `clear-page-elements` - clear specific elements from a page\n  - `freelist` - freelist related surgery commands\n  - `meta` - meta page related surgery commands\n\n#### surgery revert-meta-page\n\n- `surgery revert-meta-page` reverts to the previous transaction state by replacing the active meta page with the inactive one.\n- usage:\n\n  ```bash\n  bbolt surgery revert-meta-page [path to the bbolt database] --output [output-file]\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt surgery revert-meta-page ~/default.etcd/member/snap/db --output reverted.db\n  The meta page is reverted.\n  ```\n\n  - This is particularly useful when the most recent transaction has corrupted the database and you need to roll back to the previous consistent state.\n\n#### surgery copy-page\n\n- `surgery copy-page` copies content from one page to another.\n- usage:\n\n  ```bash\n  bbolt surgery copy-page [path to the bbolt database] --output [output-file] --from-page [source-id] --to-page [destination-id]\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt surgery copy-page ~/default.etcd/member/snap/db --output copied.db --from-page 3 --to-page 2\n  The page 3 was successfully copied to page 2\n  WARNING: the free list might have changed.\n  Please consider executing `./bbolt surgery freelist abandon ...`\n  ```\n\n  - This command is useful for recovering data from damaged pages or creating page backups.\n\n#### surgery clear-page\n\n- `surgery clear-page` removes all elements from a branch or leaf page.\n- usage:\n\n  ```bash\n  bbolt surgery clear-page [path to the bbolt database] --output [output-file] --pageId [page-id]\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt surgery clear-page ~/default.etcd/member/snap/db --output cleared.db --pageId 3\n  The page (3) was cleared\n  WARNING: The clearing has abandoned some pages that are not yet referenced from free list.\n  Please consider executing `./bbolt surgery freelist abandon ...`\n  ```\n\n  - The pageId must be at least 2 (meta pages 0 and 1 cannot be cleared).\n\n#### surgery clear-page-elements\n\n- `surgery clear-page-elements` removes specific elements from a branch or leaf page by index range.\n- usage:\n\n  ```bash\n  bbolt surgery clear-page-elements [path to the bbolt database] --output [output-file] --pageId [page-id] --from-index [start] --to-index [end]\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt surgery clear-page-elements ~/default.etcd/member/snap/db --output partial.db --pageId 3 --from-index 1 --to-index 4\n  All elements in [1, 4) in page 3 were cleared\n  WARNING: The clearing has abandoned some pages that are not yet referenced from free list.\n  Please consider executing `./bbolt surgery freelist abandon ...`\n  ```\n\n  - Use `--to-index -1` to clear elements to the end of the page.\n\n#### surgery freelist\n\n- `surgery freelist` provides commands for managing the database's free page list.\n- usage:\n\n  ```bash\n  bbolt surgery freelist <subcommand> [arguments]\n  ```\n\n  The freelist subcommands are:\n\n  - `abandon` - remove freelist references from meta pages\n  - `rebuild` - rebuild the freelist by scanning the database\n\n##### surgery freelist abandon\n\n- `surgery freelist abandon` removes freelist references from both meta pages, forcing Bbolt to reconstruct the freelist when the database is next opened.\n- usage:\n\n  ```bash\n  bbolt surgery freelist abandon [path to the bbolt database] --output [output-file]\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt surgery freelist abandon ~/default.etcd/member/snap/db --output abandoned.db\n  The freelist was abandoned in both meta pages.\n  It may cause some delay on next startup because bbolt needs to scan the whole db to reconstruct the free list.\n  ```\n\n  - This sets the freelist page ID to a special value indicating the freelist is not present.\n\n##### surgery freelist rebuild\n\n- `surgery freelist rebuild` rebuilds the freelist in a database where the freelist has been abandoned.\n- usage:\n\n  ```bash\n  bbolt surgery freelist rebuild [path to the bbolt database] --output [output-file]\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt surgery freelist rebuild ~/default.etcd/member/snap/db --output rebuilt.db\n  The freelist was successfully rebuilt.\n  ```\n\n  - This command opens the database and lets Bbolt automatically reconstruct and sync the freelist.\n\n#### surgery meta\n\n- `surgery meta` provides commands for working with database metadata pages.\n- usage:\n\n  ```bash\n  bbolt surgery meta <subcommand> [arguments]\n  ```\n  \n  The meta subcommands are:\n  - `validate` - validate integrity of meta pages\n  - `update` - update specific fields in meta pages\n\n##### surgery meta validate\n\n- `surgery meta validate` validates the integrity of both meta pages in the database.\n- usage:\n\n  ```bash\n  bbolt surgery meta validate [path to the bbolt database]\n  ```\n\n  Example:\n\n  ```bash\n  $bbolt surgery meta validate ~/default.etcd/member/snap/db\n  The meta page 0 is valid!\n  The meta page 1 is valid!\n  ```\n\n  - It checks magic number values, version compatibility, checksum integrity, and general metadata validity for both meta pages (0 and 1).\n\n##### surgery meta update\n\n- `surgery meta update` updates specific fields in a meta page for manual repair of corrupted metadata.\n- usage:\n\n  ```bash\n  bbolt surgery meta update [path to the bbolt database] --output [output-file] --meta-page [0|1] --fields [field:value,...]\n  ```\n\n  Supported fields:\n  - `pageSize` - Size of database pages\n  - `root` - Root bucket page ID\n  - `freelist` - Freelist page ID\n  - `pgid` - Next page ID to allocate\n\n  Example:\n\n  ```bash\n  $bbolt surgery meta update ~/default.etcd/member/snap/db --output fixed.db --meta-page 0 --fields root:16,freelist:8\n  The meta page 0 has been updated!\n  ```\n\n  - It updates the specified meta page and automatically updates magic number, version, flags, and checksum to ensure consistency.\n"
  },
  {
    "path": "cmd/bbolt/command/command_bench.go",
    "content": "package command\n\nimport (\n\t\"encoding/binary\"\n\t\"fmt\"\n\t\"io\"\n\t\"math\"\n\t\"math/rand\"\n\t\"os\"\n\t\"runtime\"\n\t\"runtime/pprof\"\n\t\"sync/atomic\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\nvar benchBucketName = []byte(\"bench\")\n\ntype benchOptions struct {\n\tprofileMode     string\n\twriteMode       string\n\treadMode        string\n\titerations      int64\n\tbatchSize       int64\n\tkeySize         int\n\tvalueSize       int\n\tcpuProfile      string\n\tmemProfile      string\n\tblockProfile    string\n\tfillPercent     float64\n\tnoSync          bool\n\twork            bool\n\tpath            string\n\tgoBenchOutput   bool\n\tpageSize        int\n\tinitialMmapSize int\n\tdeleteFraction  float64 // Fraction of keys of last tx to delete during writes. works only with \"seq-del\" write mode.\n}\n\nfunc (o *benchOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.StringVar(&o.profileMode, \"profile-mode\", \"rw\", \"\")\n\tfs.StringVar(&o.writeMode, \"write-mode\", \"seq\", \"\")\n\tfs.StringVar(&o.readMode, \"read-mode\", \"seq\", \"\")\n\tfs.Int64Var(&o.iterations, \"count\", 1000, \"\")\n\tfs.Int64Var(&o.batchSize, \"batch-size\", 0, \"\")\n\tfs.IntVar(&o.keySize, \"key-size\", 8, \"\")\n\tfs.IntVar(&o.valueSize, \"value-size\", 32, \"\")\n\tfs.StringVar(&o.cpuProfile, \"cpuprofile\", \"\", \"\")\n\tfs.StringVar(&o.memProfile, \"memprofile\", \"\", \"\")\n\tfs.StringVar(&o.blockProfile, \"blockprofile\", \"\", \"\")\n\tfs.Float64Var(&o.fillPercent, \"fill-percent\", bolt.DefaultFillPercent, \"\")\n\tfs.BoolVar(&o.noSync, \"no-sync\", false, \"\")\n\tfs.BoolVar(&o.work, \"work\", false, \"\")\n\tfs.StringVar(&o.path, \"path\", \"\", \"\")\n\tfs.BoolVar(&o.goBenchOutput, \"gobench-output\", false, \"\")\n\tfs.IntVar(&o.pageSize, \"page-size\", common.DefaultPageSize, \"Set page size in bytes.\")\n\tfs.IntVar(&o.initialMmapSize, \"initial-mmap-size\", 0, \"Set initial mmap size in bytes for database file.\")\n}\n\n// Returns an error if `bench` options are not valid.\nfunc (o *benchOptions) Validate() error {\n\t// Require that batch size can be evenly divided by the iteration count if set.\n\tif o.batchSize > 0 && o.iterations%o.batchSize != 0 {\n\t\treturn ErrBatchNonDivisibleBatchSize\n\t}\n\n\tswitch o.writeMode {\n\tcase \"seq\", \"rnd\", \"seq-nest\", \"rnd-nest\":\n\tdefault:\n\t\treturn ErrBatchInvalidWriteMode\n\t}\n\n\t// Generate temp path if one is not passed in.\n\tif o.path == \"\" {\n\t\tf, err := os.CreateTemp(\"\", \"bolt-bench-\")\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"temp file: %s\", err)\n\t\t}\n\t\tf.Close()\n\t\tos.Remove(f.Name())\n\t\to.path = f.Name()\n\t}\n\n\treturn nil\n}\n\n// Sets the `bench` option values that are dependent on other options.\nfunc (o *benchOptions) SetOptionValues() error {\n\t// Generate temp path if one is not passed in.\n\tif o.path == \"\" {\n\t\tf, err := os.CreateTemp(\"\", \"bolt-bench-\")\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error creating temp file: %s\", err)\n\t\t}\n\t\tf.Close()\n\t\tos.Remove(f.Name())\n\t\to.path = f.Name()\n\t}\n\n\t// Set batch size to iteration size if not set.\n\tif o.batchSize == 0 {\n\t\to.batchSize = o.iterations\n\t}\n\n\treturn nil\n}\n\nfunc newBenchCommand() *cobra.Command {\n\tvar o benchOptions\n\n\tbenchCmd := &cobra.Command{\n\t\tUse:   \"bench\",\n\t\tShort: \"run synthetic benchmark against bbolt\",\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := o.SetOptionValues(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn benchFunc(cmd, &o)\n\t\t},\n\t}\n\n\to.AddFlags(benchCmd.Flags())\n\n\treturn benchCmd\n}\n\nfunc benchFunc(cmd *cobra.Command, options *benchOptions) error {\n\t// Remove path if \"-work\" is not set. Otherwise keep path.\n\tif options.work {\n\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"work: %s\\n\", options.path)\n\t} else {\n\t\tdefer os.Remove(options.path)\n\t}\n\n\t// Create database.\n\tdbOptions := *bolt.DefaultOptions\n\tdbOptions.PageSize = options.pageSize\n\tdbOptions.InitialMmapSize = options.initialMmapSize\n\tdb, err := bolt.Open(options.path, 0600, &dbOptions)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdb.NoSync = options.noSync\n\tdefer db.Close()\n\n\tr := rand.New(rand.NewSource(time.Now().UnixNano()))\n\n\tvar writeResults benchResults\n\n\tfmt.Fprintf(cmd.ErrOrStderr(), \"starting write benchmark.\\n\")\n\tkeys, err := runWrites(cmd, db, options, &writeResults, r)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write: %v\", err)\n\t}\n\n\tif keys != nil {\n\t\tr.Shuffle(len(keys), func(i, j int) {\n\t\t\tkeys[i], keys[j] = keys[j], keys[i]\n\t\t})\n\t}\n\n\tvar readResults benchResults\n\tfmt.Fprintf(cmd.ErrOrStderr(), \"starting read benchmark.\\n\")\n\t// Read from the database.\n\tif err := runReads(cmd, db, options, &readResults, keys); err != nil {\n\t\treturn fmt.Errorf(\"bench: read: %s\", err)\n\t}\n\n\t// Print results.\n\tif options.goBenchOutput {\n\t\t// below replicates the output of testing.B benchmarks, e.g. for external tooling\n\t\tbenchWriteName := \"BenchmarkWrite\"\n\t\tbenchReadName := \"BenchmarkRead\"\n\t\tmaxLen := max(len(benchReadName), len(benchWriteName))\n\t\tprintGoBenchResult(cmd.OutOrStdout(), writeResults, maxLen, benchWriteName)\n\t\tprintGoBenchResult(cmd.OutOrStdout(), readResults, maxLen, benchReadName)\n\t} else {\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"# Write\\t%v(ops)\\t%v\\t(%v/op)\\t(%v op/sec)\\n\", writeResults.getCompletedOps(), writeResults.getDuration(), writeResults.opDuration(), writeResults.opsPerSecond())\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"# Read\\t%v(ops)\\t%v\\t(%v/op)\\t(%v op/sec)\\n\", readResults.getCompletedOps(), readResults.getDuration(), readResults.opDuration(), readResults.opsPerSecond())\n\t}\n\tfmt.Fprintln(cmd.OutOrStdout(), \"\")\n\n\treturn nil\n}\n\nfunc runWrites(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults, r *rand.Rand) ([]nestedKey, error) {\n\t// Start profiling for writes.\n\tif options.profileMode == \"rw\" || options.profileMode == \"w\" {\n\t\tstartProfiling(cmd, options)\n\t}\n\n\tfinishChan := make(chan interface{})\n\tgo checkProgress(results, finishChan, cmd.ErrOrStderr())\n\tdefer close(finishChan)\n\n\tt := time.Now()\n\n\tvar keys []nestedKey\n\tvar err error\n\tswitch options.writeMode {\n\tcase \"seq\":\n\t\tkeys, err = runWritesSequential(cmd, db, options, results)\n\tcase \"rnd\":\n\t\tkeys, err = runWritesRandom(cmd, db, options, results, r)\n\tcase \"seq-nest\":\n\t\tkeys, err = runWritesSequentialNested(cmd, db, options, results)\n\tcase \"rnd-nest\":\n\t\tkeys, err = runWritesRandomNested(cmd, db, options, results, r)\n\tcase \"seq-del\":\n\t\toptions.deleteFraction = 0.1\n\t\tkeys, err = runWritesSequentialAndDelete(cmd, db, options, results)\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"invalid write mode: %s\", options.writeMode)\n\t}\n\n\t// Save time to write.\n\tresults.setDuration(time.Since(t))\n\n\t// Stop profiling for writes only.\n\tif options.profileMode == \"w\" {\n\t\tstopProfiling(cmd)\n\t}\n\n\treturn keys, err\n}\n\nfunc runWritesSequential(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults) ([]nestedKey, error) {\n\tvar i = uint32(0)\n\treturn runWritesWithSource(cmd, db, options, results, func() uint32 { i++; return i })\n}\n\nfunc runWritesSequentialAndDelete(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults) ([]nestedKey, error) {\n\tvar i = uint32(0)\n\treturn runWritesDeletesWithSource(cmd, db, options, results, func() uint32 { i++; return i })\n}\n\nfunc runWritesRandom(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults, r *rand.Rand) ([]nestedKey, error) {\n\treturn runWritesWithSource(cmd, db, options, results, func() uint32 { return r.Uint32() })\n}\n\nfunc runWritesSequentialNested(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults) ([]nestedKey, error) {\n\tvar i = uint32(0)\n\treturn runWritesNestedWithSource(cmd, db, options, results, func() uint32 { i++; return i })\n}\n\nfunc runWritesRandomNested(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults, r *rand.Rand) ([]nestedKey, error) {\n\treturn runWritesNestedWithSource(cmd, db, options, results, func() uint32 { return r.Uint32() })\n}\n\nfunc runWritesWithSource(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults, keySource func() uint32) ([]nestedKey, error) {\n\tvar keys []nestedKey\n\tif options.readMode == \"rnd\" {\n\t\tkeys = make([]nestedKey, 0, options.iterations)\n\t}\n\n\tfor i := int64(0); i < options.iterations; i += options.batchSize {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, _ := tx.CreateBucketIfNotExists(benchBucketName)\n\t\t\tb.FillPercent = options.fillPercent\n\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Starting write iteration %d\\n\", i)\n\t\t\tfor j := int64(0); j < options.batchSize; j++ {\n\t\t\t\tkey := make([]byte, options.keySize)\n\t\t\t\tvalue := make([]byte, options.valueSize)\n\n\t\t\t\t// Write key as uint32.\n\t\t\t\tbinary.BigEndian.PutUint32(key, keySource())\n\n\t\t\t\t// Insert key/value.\n\t\t\t\tif err := b.Put(key, value); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tif keys != nil {\n\t\t\t\t\tkeys = append(keys, nestedKey{nil, key})\n\t\t\t\t}\n\t\t\t\tresults.addCompletedOps(1)\n\t\t\t}\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Finished write iteration %d\\n\", i)\n\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\treturn keys, nil\n}\n\nfunc runWritesDeletesWithSource(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults, keySource func() uint32) ([]nestedKey, error) {\n\tvar keys []nestedKey\n\tdeleteSize := int64(math.Ceil(float64(options.batchSize) * options.deleteFraction))\n\tvar InsertedKeys [][]byte\n\n\tfor i := int64(0); i < options.iterations; i += options.batchSize {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, _ := tx.CreateBucketIfNotExists(benchBucketName)\n\t\t\tb.FillPercent = options.fillPercent\n\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Starting delete iteration %d, deleteSize: %d\\n\", i, deleteSize)\n\t\t\tfor i := int64(0); i < deleteSize && i < int64(len(InsertedKeys)); i++ {\n\t\t\t\tif err := b.Delete(InsertedKeys[i]); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\t\t\tInsertedKeys = InsertedKeys[:0]\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Finished delete iteration %d\\n\", i)\n\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Starting write iteration %d\\n\", i)\n\t\t\tfor j := int64(0); j < options.batchSize; j++ {\n\n\t\t\t\tkey := make([]byte, options.keySize)\n\t\t\t\tvalue := make([]byte, options.valueSize)\n\n\t\t\t\t// Write key as uint32.\n\t\t\t\tbinary.BigEndian.PutUint32(key, keySource())\n\t\t\t\tInsertedKeys = append(InsertedKeys, key)\n\n\t\t\t\t// Insert key/value.\n\t\t\t\tif err := b.Put(key, value); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tif keys != nil {\n\t\t\t\t\tkeys = append(keys, nestedKey{nil, key})\n\t\t\t\t}\n\t\t\t\tresults.addCompletedOps(1)\n\t\t\t}\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Finished write iteration %d\\n\", i)\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\treturn keys, nil\n}\n\nfunc runWritesNestedWithSource(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults, keySource func() uint32) ([]nestedKey, error) {\n\tvar keys []nestedKey\n\tif options.readMode == \"rnd\" {\n\t\tkeys = make([]nestedKey, 0, options.iterations)\n\t}\n\n\tfor i := int64(0); i < options.iterations; i += options.batchSize {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\ttop, err := tx.CreateBucketIfNotExists(benchBucketName)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\ttop.FillPercent = options.fillPercent\n\n\t\t\t// Create bucket key.\n\t\t\tname := make([]byte, options.keySize)\n\t\t\tbinary.BigEndian.PutUint32(name, keySource())\n\n\t\t\t// Create bucket.\n\t\t\tb, err := top.CreateBucketIfNotExists(name)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tb.FillPercent = options.fillPercent\n\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Starting write iteration %d\\n\", i)\n\t\t\tfor j := int64(0); j < options.batchSize; j++ {\n\t\t\t\tvar key = make([]byte, options.keySize)\n\t\t\t\tvar value = make([]byte, options.valueSize)\n\n\t\t\t\t// Generate key as uint32.\n\t\t\t\tbinary.BigEndian.PutUint32(key, keySource())\n\n\t\t\t\t// Insert value into subbucket.\n\t\t\t\tif err := b.Put(key, value); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tif keys != nil {\n\t\t\t\t\tkeys = append(keys, nestedKey{name, key})\n\t\t\t\t}\n\t\t\t\tresults.addCompletedOps(1)\n\t\t\t}\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"Finished write iteration %d\\n\", i)\n\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\treturn keys, nil\n}\n\nfunc runReads(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults, keys []nestedKey) error {\n\t// Start profiling for reads.\n\tif options.profileMode == \"r\" {\n\t\tstartProfiling(cmd, options)\n\t}\n\n\tfinishChan := make(chan interface{})\n\tgo checkProgress(results, finishChan, cmd.ErrOrStderr())\n\tdefer close(finishChan)\n\n\tt := time.Now()\n\n\tvar err error\n\tswitch options.readMode {\n\tcase \"seq\":\n\t\tswitch options.writeMode {\n\t\tcase \"seq-nest\", \"rnd-nest\":\n\t\t\terr = runReadsSequentialNested(cmd, db, options, results)\n\t\tdefault:\n\t\t\terr = runReadsSequential(cmd, db, options, results)\n\t\t}\n\tcase \"rnd\":\n\t\tswitch options.writeMode {\n\t\tcase \"seq-nest\", \"rnd-nest\":\n\t\t\terr = runReadsRandomNested(cmd, db, options, keys, results)\n\t\tdefault:\n\t\t\terr = runReadsRandom(cmd, db, options, keys, results)\n\t\t}\n\tdefault:\n\t\treturn fmt.Errorf(\"invalid read mode: %s\", options.readMode)\n\t}\n\n\t// Save read time.\n\tresults.setDuration(time.Since(t))\n\n\t// Stop profiling for reads.\n\tif options.profileMode == \"rw\" || options.profileMode == \"r\" {\n\t\tstopProfiling(cmd)\n\t}\n\n\treturn err\n}\n\ntype nestedKey struct{ bucket, key []byte }\n\nfunc runReadsSequential(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tt := time.Now()\n\n\t\tfor {\n\t\t\tnumReads := int64(0)\n\t\t\terr := func() error {\n\t\t\t\tdefer func() { results.addCompletedOps(numReads) }()\n\n\t\t\t\tc := tx.Bucket(benchBucketName).Cursor()\n\t\t\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\t\t\tnumReads++\n\t\t\t\t\tif v == nil {\n\t\t\t\t\t\treturn ErrInvalidValue\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn nil\n\t\t\t}()\n\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tif options.writeMode == \"seq\" && numReads != options.iterations {\n\t\t\t\treturn fmt.Errorf(\"read seq: iter mismatch: expected %d, got %d\", options.iterations, numReads)\n\t\t\t}\n\n\t\t\t// Make sure we do this for at least a second.\n\t\t\tif time.Since(t) >= time.Second {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n}\n\nfunc runReadsRandom(cmd *cobra.Command, db *bolt.DB, options *benchOptions, keys []nestedKey, results *benchResults) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tt := time.Now()\n\n\t\tfor {\n\t\t\tnumReads := int64(0)\n\t\t\terr := func() error {\n\t\t\t\tdefer func() { results.addCompletedOps(numReads) }()\n\n\t\t\t\tb := tx.Bucket(benchBucketName)\n\t\t\t\tfor _, key := range keys {\n\t\t\t\t\tv := b.Get(key.key)\n\t\t\t\t\tnumReads++\n\t\t\t\t\tif v == nil {\n\t\t\t\t\t\treturn ErrInvalidValue\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn nil\n\t\t\t}()\n\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tif options.writeMode == \"seq\" && numReads != options.iterations {\n\t\t\t\treturn fmt.Errorf(\"read seq: iter mismatch: expected %d, got %d\", options.iterations, numReads)\n\t\t\t}\n\n\t\t\t// Make sure we do this for at least a second.\n\t\t\tif time.Since(t) >= time.Second {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n}\n\nfunc runReadsSequentialNested(cmd *cobra.Command, db *bolt.DB, options *benchOptions, results *benchResults) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tt := time.Now()\n\n\t\tfor {\n\t\t\tnumReads := int64(0)\n\t\t\tvar top = tx.Bucket(benchBucketName)\n\t\t\tif err := top.ForEach(func(name, _ []byte) error {\n\t\t\t\tdefer func() { results.addCompletedOps(numReads) }()\n\t\t\t\tif b := top.Bucket(name); b != nil {\n\t\t\t\t\tc := b.Cursor()\n\t\t\t\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\t\t\t\tnumReads++\n\t\t\t\t\t\tif v == nil {\n\t\t\t\t\t\t\treturn ErrInvalidValue\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tif options.writeMode == \"seq-nest\" && numReads != options.iterations {\n\t\t\t\treturn fmt.Errorf(\"read seq-nest: iter mismatch: expected %d, got %d\", options.iterations, numReads)\n\t\t\t}\n\n\t\t\t// Make sure we do this for at least a second.\n\t\t\tif time.Since(t) >= time.Second {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n}\n\nfunc runReadsRandomNested(cmd *cobra.Command, db *bolt.DB, options *benchOptions, nestedKeys []nestedKey, results *benchResults) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tt := time.Now()\n\n\t\tfor {\n\t\t\tnumReads := int64(0)\n\t\t\terr := func() error {\n\t\t\t\tdefer func() { results.addCompletedOps(numReads) }()\n\n\t\t\t\tvar top = tx.Bucket(benchBucketName)\n\t\t\t\tfor _, nestedKey := range nestedKeys {\n\t\t\t\t\tif b := top.Bucket(nestedKey.bucket); b != nil {\n\t\t\t\t\t\tv := b.Get(nestedKey.key)\n\t\t\t\t\t\tnumReads++\n\t\t\t\t\t\tif v == nil {\n\t\t\t\t\t\t\treturn ErrInvalidValue\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn nil\n\t\t\t}()\n\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tif options.writeMode == \"seq-nest\" && numReads != options.iterations {\n\t\t\t\treturn fmt.Errorf(\"read seq-nest: iter mismatch: expected %d, got %d\", options.iterations, numReads)\n\t\t\t}\n\n\t\t\t// Make sure we do this for at least a second.\n\t\t\tif time.Since(t) >= time.Second {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n}\n\nfunc checkProgress(results *benchResults, finishChan chan interface{}, stderr io.Writer) {\n\tticker := time.Tick(time.Second)\n\tlastCompleted, lastTime := int64(0), time.Now()\n\tfor {\n\t\tselect {\n\t\tcase <-finishChan:\n\t\t\treturn\n\t\tcase t := <-ticker:\n\t\t\tcompleted, taken := results.getCompletedOps(), t.Sub(lastTime)\n\t\t\tfmt.Fprintf(stderr, \"Completed %d requests, %d/s \\n\",\n\t\t\t\tcompleted, ((completed-lastCompleted)*int64(time.Second))/int64(taken),\n\t\t\t)\n\t\t\tlastCompleted, lastTime = completed, t\n\t\t}\n\t}\n}\n\nvar cpuprofile, memprofile, blockprofile *os.File\n\nfunc startProfiling(cmd *cobra.Command, options *benchOptions) {\n\tvar err error\n\n\t// Start CPU profiling.\n\tif options.cpuProfile != \"\" {\n\t\tcpuprofile, err = os.Create(options.cpuProfile)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"bench: could not create cpu profile %q: %v\\n\", options.cpuProfile, err)\n\t\t\tos.Exit(1)\n\t\t}\n\t\terr = pprof.StartCPUProfile(cpuprofile)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"bench: could not start cpu profile %q: %v\\n\", options.cpuProfile, err)\n\t\t\tos.Exit(1)\n\t\t}\n\t}\n\n\t// Start memory profiling.\n\tif options.memProfile != \"\" {\n\t\tmemprofile, err = os.Create(options.memProfile)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"bench: could not create memory profile %q: %v\\n\", options.memProfile, err)\n\t\t\tos.Exit(1)\n\t\t}\n\t\truntime.MemProfileRate = 4096\n\t}\n\n\t// Start fatal profiling.\n\tif options.blockProfile != \"\" {\n\t\tblockprofile, err = os.Create(options.blockProfile)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"bench: could not create block profile %q: %v\\n\", options.blockProfile, err)\n\t\t\tos.Exit(1)\n\t\t}\n\t\truntime.SetBlockProfileRate(1)\n\t}\n}\n\nfunc stopProfiling(cmd *cobra.Command) {\n\tif cpuprofile != nil {\n\t\tpprof.StopCPUProfile()\n\t\tcpuprofile.Close()\n\t\tcpuprofile = nil\n\t}\n\n\tif memprofile != nil {\n\t\terr := pprof.Lookup(\"heap\").WriteTo(memprofile, 0)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"bench: could not write mem profile\")\n\t\t}\n\t\tmemprofile.Close()\n\t\tmemprofile = nil\n\t}\n\n\tif blockprofile != nil {\n\t\terr := pprof.Lookup(\"block\").WriteTo(blockprofile, 0)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.ErrOrStderr(), \"bench: could not write block profile\")\n\t\t}\n\t\tblockprofile.Close()\n\t\tblockprofile = nil\n\t\truntime.SetBlockProfileRate(0)\n\t}\n}\n\n// benchResults represents the performance results of the benchmark and is thread-safe.\ntype benchResults struct {\n\tcompletedOps int64\n\tduration     int64\n}\n\nfunc (r *benchResults) addCompletedOps(amount int64) {\n\tatomic.AddInt64(&r.completedOps, amount)\n}\n\nfunc (r *benchResults) getCompletedOps() int64 {\n\treturn atomic.LoadInt64(&r.completedOps)\n}\n\nfunc (r *benchResults) setDuration(dur time.Duration) {\n\tatomic.StoreInt64(&r.duration, int64(dur))\n}\n\nfunc (r *benchResults) getDuration() time.Duration {\n\treturn time.Duration(atomic.LoadInt64(&r.duration))\n}\n\n// opDuration returns the duration for a single read/write operation.\nfunc (r *benchResults) opDuration() time.Duration {\n\tif r.getCompletedOps() == 0 {\n\t\treturn 0\n\t}\n\treturn r.getDuration() / time.Duration(r.getCompletedOps())\n}\n\n// opsPerSecond returns average number of read/write operations that can be performed per second.\nfunc (r *benchResults) opsPerSecond() int {\n\tvar op = r.opDuration()\n\tif op == 0 {\n\t\treturn 0\n\t}\n\treturn int(time.Second) / int(op)\n}\n\nfunc printGoBenchResult(w io.Writer, r benchResults, maxLen int, benchName string) {\n\tgobenchResult := testing.BenchmarkResult{}\n\tgobenchResult.T = r.getDuration()\n\tgobenchResult.N = int(r.getCompletedOps())\n\tfmt.Fprintf(w, \"%-*s\\t%s\\n\", maxLen, benchName, gobenchResult.String())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_bench_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"strings\"\n\t\"sync\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n)\n\ntype safeWriter struct {\n\tbuf *bytes.Buffer\n\tmu  sync.Mutex\n}\n\nfunc (w *safeWriter) Write(p []byte) (n int, err error) {\n\tw.mu.Lock()\n\tdefer w.mu.Unlock()\n\treturn w.buf.Write(p)\n}\n\nfunc (w *safeWriter) String() string {\n\tw.mu.Lock()\n\tdefer w.mu.Unlock()\n\treturn w.buf.String()\n}\n\nfunc newSafeWriter() *safeWriter {\n\treturn &safeWriter{buf: bytes.NewBufferString(\"\")}\n}\n\n// Ensure the \"bench\" command runs and exits without errors\nfunc TestBenchCommand_Run(t *testing.T) {\n\ttests := map[string]struct {\n\t\targs []string\n\t}{\n\t\t\"no-args\":    {},\n\t\t\"100k count\": {[]string{\"--count\", \"100000\"}},\n\t}\n\n\tfor name, test := range tests {\n\t\tt.Run(name, func(t *testing.T) {\n\t\t\t// Run the command.\n\t\t\trootCmd := command.NewRootCommand()\n\n\t\t\toutputWriter := newSafeWriter()\n\t\t\trootCmd.SetOut(outputWriter)\n\n\t\t\terrorWriter := newSafeWriter()\n\t\t\trootCmd.SetErr(errorWriter)\n\n\t\t\targs := append([]string{\"bench\"}, test.args...)\n\t\t\trootCmd.SetArgs(args)\n\n\t\t\terr := rootCmd.Execute()\n\t\t\trequire.NoError(t, err)\n\n\t\t\toutStr := outputWriter.String()\n\t\t\terrStr := errorWriter.String()\n\n\t\t\tif !strings.Contains(errStr, \"starting write benchmark.\") || !strings.Contains(errStr, \"starting read benchmark.\") {\n\t\t\t\tt.Fatal(fmt.Errorf(\"benchmark result does not contain read/write start output:\\n%s\", outStr))\n\t\t\t}\n\n\t\t\tif strings.Contains(errStr, \"iter mismatch\") {\n\t\t\t\tt.Fatal(fmt.Errorf(\"found iter mismatch in stdout:\\n%s\", outStr))\n\t\t\t}\n\n\t\t\tif !strings.Contains(outStr, \"# Write\") || !strings.Contains(outStr, \"# Read\") {\n\t\t\t\tt.Fatal(fmt.Errorf(\"benchmark result does not contain read/write output:\\n%s\", outStr))\n\t\t\t}\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_buckets.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/spf13/cobra\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nfunc newBucketsCommand() *cobra.Command {\n\tbucketsCmd := &cobra.Command{\n\t\tUse:   \"buckets <bbolt-file>\",\n\t\tShort: \"print a list of buckets in bbolt database\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn bucketsFunc(cmd, args[0])\n\t\t},\n\t}\n\n\treturn bucketsCmd\n}\n\nfunc bucketsFunc(cmd *cobra.Command, dbPath string) error {\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\t// Open database.\n\tdb, err := bolt.Open(dbPath, 0600, &bolt.Options{\n\t\tReadOnly:        true,\n\t\tPreLoadFreelist: true,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\t// Print buckets.\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.ForEach(func(name []byte, _ *bolt.Bucket) error {\n\t\t\tfmt.Fprintln(cmd.OutOrStdout(), string(name))\n\t\t\treturn nil\n\t\t})\n\t})\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_buckets_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"io\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// Ensure the \"buckets\" command can print a list of buckets.\nfunc TestBucketsCommand_Run(t *testing.T) {\n\n\ttestCases := []struct {\n\t\tname      string\n\t\targs      []string\n\t\texpErr    error\n\t\texpOutput string\n\t}{\n\t\t{\n\t\t\tname:      \"buckets all buckets in bbolt database\",\n\t\t\targs:      []string{\"buckets\", \"path\"},\n\t\t\texpErr:    nil,\n\t\t\texpOutput: \"bar\\nbaz\\nfoo\\n\",\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\n\t\t\tt.Log(\"Creating sample DB\")\n\t\t\tdb := btesting.MustCreateDB(t)\n\t\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tfor _, name := range []string{\"foo\", \"bar\", \"baz\"} {\n\t\t\t\t\t_, err := tx.CreateBucket([]byte(name))\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tdb.Close()\n\t\t\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t\t\tt.Log(\"Running buckets cmd\")\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\toutputBuf := bytes.NewBufferString(\"\")\n\t\t\trootCmd.SetOut(outputBuf)\n\n\t\t\ttc.args[1] = db.Path()\n\t\t\trootCmd.SetArgs(tc.args)\n\t\t\terr := rootCmd.Execute()\n\t\t\trequire.Equal(t, tc.expErr, err)\n\n\t\t\tt.Log(\"Checking output\")\n\t\t\toutput, err := io.ReadAll(outputBuf)\n\t\t\trequire.NoError(t, err)\n\t\t\trequire.Containsf(t, string(output), tc.expOutput, \"unexpected stdout:\\n\\n%s\", string(output))\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_check.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\ntype checkOptions struct {\n\tfromPageID uint64\n}\n\nfunc (o *checkOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.Uint64VarP(&o.fromPageID, \"from-page\", \"\", o.fromPageID, \"check db integrity starting from the given page ID\")\n}\n\nfunc newCheckCommand() *cobra.Command {\n\tvar o checkOptions\n\tcheckCmd := &cobra.Command{\n\t\tUse:   \"check <bbolt-file>\",\n\t\tShort: \"verify integrity of bbolt database data\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn checkFunc(cmd, args[0], o)\n\t\t},\n\t}\n\n\to.AddFlags(checkCmd.Flags())\n\treturn checkCmd\n}\n\nfunc checkFunc(cmd *cobra.Command, dbPath string, cfg checkOptions) error {\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\t// Open database.\n\tdb, err := bolt.Open(dbPath, 0600, &bolt.Options{\n\t\tReadOnly:        true,\n\t\tPreLoadFreelist: true,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\topts := []bolt.CheckOption{bolt.WithKVStringer(CmdKvStringer())}\n\tif cfg.fromPageID != 0 {\n\t\topts = append(opts, bolt.WithPageId(cfg.fromPageID))\n\t}\n\t// Perform consistency check.\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tvar count int\n\t\tfor err := range tx.Check(opts...) {\n\t\t\tfmt.Fprintln(cmd.OutOrStdout(), err)\n\t\t\tcount++\n\t\t}\n\n\t\t// Print summary of errors.\n\t\tif count > 0 {\n\t\t\tfmt.Fprintf(cmd.OutOrStdout(), \"%d errors found\\n\", count)\n\t\t\treturn guts_cli.ErrCorrupt\n\t\t}\n\n\t\t// Notify user that database is valid.\n\t\tfmt.Fprintln(cmd.OutOrStdout(), \"OK\")\n\t\treturn nil\n\t})\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_check_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"io\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\nfunc TestCheckCommand_Run(t *testing.T) {\n\ttestCases := []struct {\n\t\tname      string\n\t\targs      []string\n\t\texpErr    error\n\t\texpOutput string\n\t}{\n\t\t{\n\t\t\tname:      \"check whole db\",\n\t\t\targs:      []string{\"check\", \"path\"},\n\t\t\texpErr:    nil,\n\t\t\texpOutput: \"OK\\n\",\n\t\t},\n\t\t{\n\t\t\tname:      \"check valid pageId\",\n\t\t\targs:      []string{\"check\", \"path\", \"--from-page\", \"3\"},\n\t\t\texpErr:    nil,\n\t\t\texpOutput: \"OK\\n\",\n\t\t},\n\t\t{\n\t\t\tname:      \"check invalid pageId\",\n\t\t\targs:      []string{\"check\", \"path\", \"--from-page\", \"1\"},\n\t\t\texpErr:    guts_cli.ErrCorrupt,\n\t\t\texpOutput: \"page ID (1) out of range [2, 4)\",\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\n\t\t\tt.Log(\"Creating sample DB\")\n\t\t\tdb := btesting.MustCreateDB(t)\n\t\t\tdb.Close()\n\t\t\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t\t\tt.Log(\"Running check cmd\")\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\toutputBuf := bytes.NewBufferString(\"\") // capture output for assertion\n\t\t\trootCmd.SetOut(outputBuf)\n\n\t\t\ttc.args[1] = db.Path() // path to be replaced with db.Path()\n\t\t\trootCmd.SetArgs(tc.args)\n\t\t\terr := rootCmd.Execute()\n\t\t\trequire.Equal(t, tc.expErr, err)\n\n\t\t\tt.Log(\"Checking output\")\n\t\t\toutput, err := io.ReadAll(outputBuf)\n\t\t\trequire.NoError(t, err)\n\t\t\trequire.Containsf(t, string(output), tc.expOutput, \"unexpected stdout:\\n\\n%s\", string(output))\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_compact.go",
    "content": "package command\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\ntype compactOptions struct {\n\tdstPath   string\n\ttxMaxSize int64\n\tdstNoSync bool\n}\n\nfunc newCompactCommand() *cobra.Command {\n\tvar o compactOptions\n\tvar compactCmd = &cobra.Command{\n\t\tUse:   \"compact [options] -o <dst-bbolt-file> <src-bbolt-file>\",\n\t\tShort: \"creates a compacted copy of the database from source path to the destination path, preserving the original.\",\n\t\tLong: `compact opens a database at source path and walks it recursively, copying keys\nas they are found from all buckets, to a newly created database at the destination path.\nThe original database is left untouched.`,\n\t\tArgs: cobra.MinimumNArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(args[0]); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn o.Run(cmd, args[0])\n\t\t},\n\t}\n\to.AddFlags(compactCmd.Flags())\n\n\treturn compactCmd\n}\n\nfunc (o *compactOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.StringVarP(&o.dstPath, \"output\", \"o\", \"\", \"\")\n\tfs.Int64Var(&o.txMaxSize, \"tx-max-size\", 65536, \"\")\n\tfs.BoolVar(&o.dstNoSync, \"no-sync\", false, \"\")\n\t_ = cobra.MarkFlagRequired(fs, \"output\")\n}\n\nfunc (o *compactOptions) Validate(srcPath string) (err error) {\n\tif o.dstPath == \"\" {\n\t\treturn errors.New(\"output file required\")\n\t}\n\n\treturn\n}\n\nfunc (o *compactOptions) Run(cmd *cobra.Command, srcPath string) (err error) {\n\n\t// ensure source file exists.\n\tfi, err := checkSourceDBPath(srcPath)\n\tif err != nil {\n\t\treturn err\n\t}\n\tinitialSize := fi.Size()\n\n\t// open source database.\n\tsrc, err := bolt.Open(srcPath, 0400, &bolt.Options{ReadOnly: true})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer src.Close()\n\n\t// open destination database.\n\tdst, err := bolt.Open(o.dstPath, fi.Mode(), &bolt.Options{NoSync: o.dstNoSync})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer dst.Close()\n\n\t// run compaction.\n\tif err := bolt.Compact(dst, src, o.txMaxSize); err != nil {\n\t\treturn err\n\t}\n\n\t// report stats on new size.\n\tfi, err = os.Stat(o.dstPath)\n\tif err != nil {\n\t\treturn err\n\t} else if fi.Size() == 0 {\n\t\treturn fmt.Errorf(\"zero db size\")\n\t}\n\tfmt.Fprintf(cmd.OutOrStdout(), \"%d -> %d bytes (gain=%.2fx)\\n\", initialSize, fi.Size(), float64(initialSize)/float64(fi.Size()))\n\n\treturn\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_compact_test.go",
    "content": "package command_test\n\nimport (\n\tcrypto \"crypto/rand\"\n\t\"errors\"\n\t\"fmt\"\n\t\"math/rand\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// Ensure the \"compact\" command can print a list of buckets.\nfunc TestCompactCommand_Run(t *testing.T) {\n\tdstdb := btesting.MustCreateDB(t)\n\tdstdb.Close()\n\n\tt.Log(\"Creating sample DB\")\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tn := 2 + rand.Intn(5)\n\t\tfor i := 0; i < n; i++ {\n\t\t\tk := []byte(fmt.Sprintf(\"b%d\", i))\n\t\t\tb, err := tx.CreateBucketIfNotExists(k)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := b.SetSequence(uint64(i)); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := fillBucket(b, append(k, '.')); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// make the db grow by adding large values, and delete them.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"large_vals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tn := 5 + rand.Intn(5)\n\t\tfor i := 0; i < n; i++ {\n\t\t\tv := make([]byte, 1000*1000*(1+rand.Intn(5)))\n\t\t\t_, err := crypto.Read(v)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"l%d\", i)), v); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"large_vals\")).Cursor()\n\t\tfor k, _ := c.First(); k != nil; k, _ = c.Next() {\n\t\t\tif err := c.Delete(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn tx.DeleteBucket([]byte(\"large_vals\"))\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.Close()\n\tdbChk, err := chkdb(db.Path())\n\trequire.NoError(t, err)\n\n\tt.Log(\"Running compact cmd\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"compact\", \"-o\", dstdb.Path(), db.Path()})\n\terr = rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tt.Log(\"Checking output\")\n\tdbChkAfterCompact, err := chkdb(db.Path())\n\trequire.NoError(t, err)\n\tdstdbChk, err := chkdb(dstdb.Path())\n\trequire.NoError(t, err)\n\trequire.Equal(t, dbChk, dbChkAfterCompact, \"the original db has been touched\")\n\trequire.Equal(t, dbChk, dstdbChk, \"the compacted db data isn't the same than the original db\")\n}\n\nfunc TestCompactCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"requires at least 1 arg(s), only received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"compact\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_dump.go",
    "content": "package command\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\n\t\"github.com/spf13/cobra\"\n\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\nfunc newDumpCommand() *cobra.Command {\n\tdumpCmd := &cobra.Command{\n\t\tUse:   \"dump <bbolt-file> pageid [pageid...]\",\n\t\tShort: \"prints a hexadecimal dump of one or more pages of bbolt database.\",\n\t\tArgs:  cobra.MinimumNArgs(2),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tdbPath := args[0]\n\t\t\tpageIDs, err := stringToPages(args[1:])\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t} else if len(pageIDs) == 0 {\n\t\t\t\treturn ErrPageIDRequired\n\t\t\t}\n\t\t\treturn dumpFunc(cmd, dbPath, pageIDs)\n\t\t},\n\t}\n\n\treturn dumpCmd\n}\n\nfunc dumpFunc(cmd *cobra.Command, dbPath string, pageIDs []uint64) (err error) {\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\t// open database to retrieve page size.\n\tpageSize, _, err := guts_cli.ReadPageAndHWMSize(dbPath)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// open database file handler.\n\tf, err := os.Open(dbPath)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() { _ = f.Close() }()\n\n\t// print each page listed.\n\tfor i, pageID := range pageIDs {\n\t\t// print a separator.\n\t\tif i > 0 {\n\t\t\tfmt.Fprintln(cmd.OutOrStdout(), \"===============================================\")\n\t\t}\n\n\t\t// print page to stdout.\n\t\tif err := dumpPage(cmd.OutOrStdout(), f, pageID, uint64(pageSize)); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn\n}\n\nfunc dumpPage(w io.Writer, r io.ReaderAt, pageID uint64, pageSize uint64) error {\n\tconst bytesPerLineN = 16\n\n\t// read page into buffer.\n\tbuf := make([]byte, pageSize)\n\taddr := pageID * uint64(pageSize)\n\tif n, err := r.ReadAt(buf, int64(addr)); err != nil {\n\t\treturn err\n\t} else if uint64(n) != pageSize {\n\t\treturn io.ErrUnexpectedEOF\n\t}\n\n\t// write out to writer in 16-byte lines.\n\tvar prev []byte\n\tvar skipped bool\n\tfor offset := uint64(0); offset < pageSize; offset += bytesPerLineN {\n\t\t// retrieve current 16-byte line.\n\t\tline := buf[offset : offset+bytesPerLineN]\n\t\tisLastLine := (offset == (pageSize - bytesPerLineN))\n\n\t\t// if it's the same as the previous line then print a skip.\n\t\tif bytes.Equal(line, prev) && !isLastLine {\n\t\t\tif !skipped {\n\t\t\t\tfmt.Fprintf(w, \"%07x *\\n\", addr+offset)\n\t\t\t\tskipped = true\n\t\t\t}\n\t\t} else {\n\t\t\t// print line as hexadecimal in 2-byte groups.\n\t\t\tfmt.Fprintf(w, \"%07x %04x %04x %04x %04x %04x %04x %04x %04x\\n\", addr+offset,\n\t\t\t\tline[0:2], line[2:4], line[4:6], line[6:8],\n\t\t\t\tline[8:10], line[10:12], line[12:14], line[14:16],\n\t\t\t)\n\n\t\t\tskipped = false\n\t\t}\n\n\t\t// save the previous line.\n\t\tprev = line\n\t}\n\tfmt.Fprint(w, \"\\n\")\n\n\treturn nil\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_dump_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"io\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\nfunc TestDumpCommand_Run(t *testing.T) {\n\tt.Log(\"Creating database\")\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})\n\trequire.NoError(t, db.Close())\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\tt.Log(\"Running dump command\")\n\trootCmd := command.NewRootCommand()\n\toutputBuf := bytes.NewBufferString(\"\")\n\trootCmd.SetOut(outputBuf)\n\trootCmd.SetArgs([]string{\"dump\", db.Path(), \"0\"})\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tt.Log(\"Checking output\")\n\texp := `0000010 edda 0ced 0200 0000 0010 0000 0000 0000`\n\toutput, err := io.ReadAll(outputBuf)\n\trequire.NoError(t, err)\n\trequire.True(t, strings.Contains(string(output), exp), \"unexpected stdout:\", string(output))\n}\n\nfunc TestDumpCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"requires at least 2 arg(s), only received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"dump\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_get.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/errors\"\n)\n\ntype getOptions struct {\n\tparseFormat string\n\tformat      string\n}\n\nfunc newGetCommand() *cobra.Command {\n\tvar opts getOptions\n\n\tcmd := &cobra.Command{\n\t\tUse:   \"get PATH [BUCKET..] KEY\",\n\t\tShort: \"get the value of a key from a (sub)bucket in a bbolt database\",\n\t\tArgs:  cobra.MinimumNArgs(3),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tpath := args[0]\n\t\t\tif path == \"\" {\n\t\t\t\treturn ErrPathRequired\n\t\t\t}\n\n\t\t\tbuckets := args[1 : len(args)-1]\n\t\t\tkeyStr := args[len(args)-1]\n\n\t\t\t// validate input parameters\n\t\t\tif len(buckets) == 0 {\n\t\t\t\treturn fmt.Errorf(\"bucket is required: %w\", ErrBucketRequired)\n\t\t\t}\n\n\t\t\tkey, err := parseBytes(keyStr, opts.parseFormat)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tif len(key) == 0 {\n\t\t\t\treturn fmt.Errorf(\"key is required: %w\", errors.ErrKeyRequired)\n\t\t\t}\n\n\t\t\treturn getFunc(cmd, path, buckets, key, opts)\n\t\t},\n\t}\n\topts.AddFlags(cmd.Flags())\n\n\treturn cmd\n}\n\nfunc (o *getOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.StringVar(&o.parseFormat, \"parse-format\", \"ascii-encoded\", \"Input format one of: ascii-encoded|hex\")\n\tfs.StringVar(&o.format, \"format\", \"auto\", \"Output format one of: \"+FORMAT_MODES+\" (default: auto)\")\n}\n\n// getFunc opens the given bbolt db file and retrieves the key value from the bucket path.\nfunc getFunc(cmd *cobra.Command, path string, buckets []string, key []byte, opts getOptions) error {\n\t// check if the source DB path is valid\n\tif _, err := checkSourceDBPath(path); err != nil {\n\t\treturn err\n\t}\n\n\t// open the database\n\tdb, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\t// access the database and get the value\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tlastBucket, err := findLastBucket(tx, buckets)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tval := lastBucket.Get(key)\n\t\tif val == nil {\n\t\t\treturn fmt.Errorf(\"Error %w for key: %q hex: \\\"%x\\\"\", ErrKeyNotFound, key, string(key))\n\t\t}\n\t\treturn writelnBytes(cmd.OutOrStdout(), val, opts.format)\n\t})\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_get_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/hex\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\nfunc TestGetCommand_Run(t *testing.T) {\n\ttestCases := []struct {\n\t\tname          string\n\t\tprintable     bool\n\t\ttestBucket    string\n\t\ttestKey       string\n\t\texpectedValue string\n\t}{\n\t\t{\n\t\t\tname:          \"printable data\",\n\t\t\tprintable:     true,\n\t\t\ttestBucket:    \"foo\",\n\t\t\ttestKey:       \"foo-1\",\n\t\t\texpectedValue: \"value-foo-1\\n\",\n\t\t},\n\t\t{\n\t\t\tname:          \"non printable data\",\n\t\t\tprintable:     false,\n\t\t\ttestBucket:    \"bar\",\n\t\t\ttestKey:       \"100001\",\n\t\t\texpectedValue: hex.EncodeToString(convertInt64IntoBytes(100001)) + \"\\n\",\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tt.Logf(\"Creating test database for subtest '%s'\", tc.name)\n\t\t\tdb := btesting.MustCreateDB(t)\n\n\t\t\tt.Log(\"Inserting test data\")\n\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(tc.testBucket))\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"create bucket %q: %w\", tc.testBucket, err)\n\t\t\t\t}\n\n\t\t\t\tif tc.printable {\n\t\t\t\t\treturn b.Put([]byte(tc.testKey), []byte(\"value-\"+tc.testKey))\n\t\t\t\t}\n\n\t\t\t\treturn b.Put([]byte(tc.testKey), convertInt64IntoBytes(100001))\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\t\t\tdb.Close()\n\t\t\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t\t\tt.Log(\"Running get command\")\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\toutputBuf := bytes.NewBufferString(\"\")\n\t\t\trootCmd.SetOut(outputBuf)\n\t\t\trootCmd.SetArgs([]string{\"get\", db.Path(), tc.testBucket, tc.testKey})\n\t\t\terr = rootCmd.Execute()\n\t\t\trequire.NoError(t, err)\n\n\t\t\tt.Log(\"Checking output\")\n\t\t\toutput, err := io.ReadAll(outputBuf)\n\t\t\trequire.NoError(t, err)\n\t\t\trequire.Equalf(t, tc.expectedValue, string(output), \"unexpected stdout:\\n\\n%s\", string(output))\n\t\t})\n\t}\n}\n\nfunc TestGetCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"requires at least 3 arg(s), only received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"get\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_info.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/spf13/cobra\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nfunc newInfoCommand() *cobra.Command {\n\tinfoCmd := &cobra.Command{\n\t\tUse:   \"info <bbolt-file>\",\n\t\tShort: \"prints basic information about the bbolt database.\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn infoFunc(cmd, args[0])\n\t\t},\n\t}\n\n\treturn infoCmd\n}\n\nfunc infoFunc(cmd *cobra.Command, dbPath string) error {\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\t// Open database.\n\tdb, err := bolt.Open(dbPath, 0600, &bolt.Options{\n\t\tReadOnly: true,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\t// Print basic database info.\n\tinfo := db.Info()\n\tfmt.Fprintf(cmd.OutOrStdout(), \"Page Size: %d\\n\", info.PageSize)\n\n\treturn nil\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_info_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"io\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// Ensure the \"info\" command can print information about a database.\nfunc TestInfoCommand_Run(t *testing.T) {\n\tt.Log(\"Creating sample DB\")\n\tdb := btesting.MustCreateDB(t)\n\tdb.Close()\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\tt.Log(\"Running info cmd\")\n\trootCmd := command.NewRootCommand()\n\toutputBuf := bytes.NewBufferString(\"\")\n\trootCmd.SetOut(outputBuf)\n\n\trootCmd.SetArgs([]string{\"info\", db.Path()})\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tt.Log(\"Checking output\")\n\t_, err = io.ReadAll(outputBuf)\n\trequire.NoError(t, err)\n}\n\nfunc TestInfoCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"accepts 1 arg(s), received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"info\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_inspect.go",
    "content": "package command\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/spf13/cobra\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nfunc newInspectCommand() *cobra.Command {\n\tinspectCmd := &cobra.Command{\n\t\tUse:   \"inspect <bbolt-file>\",\n\t\tShort: \"inspect the structure of the database\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn inspectFunc(args[0])\n\t\t},\n\t}\n\n\treturn inspectCmd\n}\n\nfunc inspectFunc(srcDBPath string) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tdb, err := bolt.Open(srcDBPath, 0600, &bolt.Options{ReadOnly: true})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tbs := tx.Inspect()\n\t\tout, err := json.MarshalIndent(bs, \"\", \"    \")\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfmt.Fprintln(os.Stdout, string(out))\n\t\treturn nil\n\t})\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_inspect_test.go",
    "content": "package command_test\n\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\nfunc TestInspect(t *testing.T) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\tdb.Close()\n\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\n\t\t\"inspect\", srcPath,\n\t})\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_keys.go",
    "content": "package command\n\nimport (\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\ntype keysOptions struct {\n\tformat string\n}\n\nfunc (o *keysOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.StringVarP(&o.format, \"format\", \"f\", \"auto\", \"Output format one of: \"+FORMAT_MODES)\n}\n\nfunc newKeysCommand() *cobra.Command {\n\tvar o keysOptions\n\n\tkeysCmd := &cobra.Command{\n\t\tUse:   \"keys <bbolt-file> <buckets>\",\n\t\tShort: \"print a list of keys in the given (sub)bucket in bbolt database\",\n\t\tArgs:  cobra.MinimumNArgs(2),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn keysFunc(cmd, o, args[0], args[1:]...)\n\t\t},\n\t}\n\n\to.AddFlags(keysCmd.Flags())\n\treturn keysCmd\n}\n\nfunc keysFunc(cmd *cobra.Command, cfg keysOptions, dbPath string, buckets ...string) error {\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\t// Open database.\n\tdb, err := bolt.Open(dbPath, 0600, &bolt.Options{\n\t\tReadOnly: true,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\t// Print keys.\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\t// Find bucket.\n\t\tlastBucket, err := findLastBucket(tx, buckets)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Iterate over each key.\n\t\treturn lastBucket.ForEach(func(key, _ []byte) error {\n\t\t\treturn writelnBytes(cmd.OutOrStdout(), key, cfg.format)\n\t\t})\n\t})\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_keys_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// Ensure the \"keys\" command can print a list of keys for a bucket.\nfunc TestKeysCommand_Run(t *testing.T) {\n\ttestCases := []struct {\n\t\tname       string\n\t\tprintable  bool\n\t\ttestBucket string\n\t\texpected   string\n\t}{\n\t\t{\n\t\t\tname:       \"printable keys\",\n\t\t\tprintable:  true,\n\t\t\ttestBucket: \"foo\",\n\t\t\texpected:   \"foo-0\\nfoo-1\\nfoo-2\\n\",\n\t\t},\n\t\t{\n\t\t\tname:       \"non printable keys\",\n\t\t\tprintable:  false,\n\t\t\ttestBucket: \"bar\",\n\t\t\texpected:   convertInt64KeysIntoHexString(100001, 100002, 100003),\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tt.Logf(\"Creating test database for subtest '%s'\", tc.name)\n\t\t\tdb := btesting.MustCreateDB(t)\n\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tt.Logf(\"creating test bucket %s\", tc.testBucket)\n\t\t\t\tb, bErr := tx.CreateBucketIfNotExists([]byte(tc.testBucket))\n\t\t\t\tif bErr != nil {\n\t\t\t\t\treturn fmt.Errorf(\"error creating test bucket %q: %v\", tc.testBucket, bErr)\n\t\t\t\t}\n\n\t\t\t\tt.Logf(\"inserting test data into test bucket %s\", tc.testBucket)\n\t\t\t\tif tc.printable {\n\t\t\t\t\tfor i := 0; i < 3; i++ {\n\t\t\t\t\t\tkey := fmt.Sprintf(\"%s-%d\", tc.testBucket, i)\n\t\t\t\t\t\tif pErr := b.Put([]byte(key), []byte{0}); pErr != nil {\n\t\t\t\t\t\t\treturn pErr\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tfor i := 100001; i < 100004; i++ {\n\t\t\t\t\t\tk := convertInt64IntoBytes(int64(i))\n\t\t\t\t\t\tif pErr := b.Put(k, []byte{0}); pErr != nil {\n\t\t\t\t\t\t\treturn pErr\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\t\t\tdb.Close()\n\t\t\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t\t\tt.Log(\"Running Keys cmd\")\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\toutputBuf := bytes.NewBufferString(\"\")\n\t\t\trootCmd.SetOut(outputBuf)\n\t\t\trootCmd.SetArgs([]string{\"keys\", db.Path(), tc.testBucket})\n\t\t\terr = rootCmd.Execute()\n\t\t\trequire.NoError(t, err)\n\n\t\t\tt.Log(\"Checking output\")\n\t\t\toutput, err := io.ReadAll(outputBuf)\n\t\t\trequire.NoError(t, err)\n\t\t\trequire.Equalf(t, tc.expected, string(output), \"unexpected stdout:\\n\\n%s\", string(output))\n\t\t})\n\t}\n}\n\nfunc TestKeyCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"requires at least 2 arg(s), only received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"keys\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_page.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\ntype getPageOptions struct {\n\tall    bool\n\tformat string\n}\n\nfunc newPageCommand() *cobra.Command {\n\tvar opt getPageOptions\n\tpageCmd := &cobra.Command{\n\t\tUse:   \"page <bbolt-file> [pageid...]\",\n\t\tShort: \"page prints one or more pages in human readable format.\",\n\t\tArgs:  cobra.MinimumNArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tdbPath := args[0]\n\t\t\tpageIDs, err := stringToPages(args[1:])\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif len(pageIDs) == 0 && !opt.all {\n\t\t\t\treturn ErrPageIDRequired\n\t\t\t}\n\t\t\treturn pageFunc(cmd, opt, dbPath, pageIDs)\n\t\t},\n\t}\n\topt.AddFlags(pageCmd.Flags())\n\n\treturn pageCmd\n}\n\nfunc (o *getPageOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.BoolVar(&o.all, \"all\", false, \"List all pages.\")\n\tfs.StringVar(&o.format, \"format-value\", \"auto\", \"Output format one of: \"+FORMAT_MODES+\". Applies to values on the leaf page.\")\n}\n\nfunc pageFunc(cmd *cobra.Command, cfg getPageOptions, dbPath string, pageIDs []uint64) (err error) {\n\tif cfg.all && len(pageIDs) != 0 {\n\t\treturn ErrInvalidPageArgs\n\t}\n\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\tif cfg.all {\n\t\tprintAllPages(cmd, dbPath, cfg.format)\n\t} else {\n\t\tprintPages(cmd, pageIDs, dbPath, cfg.format)\n\t}\n\n\treturn\n}\n\nfunc printPages(cmd *cobra.Command, pageIDs []uint64, path string, formatValue string) {\n\t// print each page listed.\n\tfor i, pageID := range pageIDs {\n\t\t// print a separator.\n\t\tif i > 0 {\n\t\t\tfmt.Fprintln(cmd.OutOrStdout(), \"===============================================\")\n\t\t}\n\t\t_, pErr := printPage(cmd, path, pageID, formatValue)\n\t\tif pErr != nil {\n\t\t\tfmt.Fprintf(cmd.OutOrStdout(), \"Prining page %d failed: %s. Continuing...\\n\", pageID, pErr)\n\t\t}\n\t}\n}\n\n// printPage prints given page to cmd.Stdout and returns error or number of interpreted pages.\nfunc printPage(cmd *cobra.Command, path string, pageID uint64, formatValue string) (numPages uint32, reterr error) {\n\tdefer func() {\n\t\tif err := recover(); err != nil {\n\t\t\treterr = fmt.Errorf(\"%s\", err)\n\t\t}\n\t}()\n\n\t// retrieve page info and page size.\n\tp, buf, err := guts_cli.ReadPage(path, pageID)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\n\t// print basic page info.\n\tstdout := cmd.OutOrStdout()\n\tfmt.Fprintf(stdout, \"Page ID:    %d\\n\", p.Id())\n\tfmt.Fprintf(stdout, \"Page Type:  %s\\n\", p.Typ())\n\tfmt.Fprintf(stdout, \"Total Size: %d bytes\\n\", len(buf))\n\tfmt.Fprintf(stdout, \"Overflow pages: %d\\n\", p.Overflow())\n\n\t// print type-specific data.\n\tswitch p.Typ() {\n\tcase \"meta\":\n\t\terr = pagePrintMeta(stdout, buf)\n\tcase \"leaf\":\n\t\terr = pagePrintLeaf(stdout, buf, formatValue)\n\tcase \"branch\":\n\t\terr = pagePrintBranch(stdout, buf)\n\tcase \"freelist\":\n\t\terr = pagePrintFreelist(stdout, buf)\n\t}\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\treturn p.Overflow(), nil\n}\n\nfunc printAllPages(cmd *cobra.Command, path string, formatValue string) {\n\t_, hwm, err := guts_cli.ReadPageAndHWMSize(path)\n\tif err != nil {\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"cannot read number of pages: %v\", err)\n\t}\n\n\t// print each page listed.\n\tfor pageID := uint64(0); pageID < uint64(hwm); {\n\t\t// print a separator.\n\t\tif pageID > 0 {\n\t\t\tfmt.Fprintln(cmd.OutOrStdout(), \"===============================================\")\n\t\t}\n\t\toverflow, pErr := printPage(cmd, path, pageID, formatValue)\n\t\tif pErr != nil {\n\t\t\tfmt.Fprintf(cmd.OutOrStdout(), \"Prining page %d failed: %s. Continuing...\\n\", pageID, pErr)\n\t\t\tpageID++\n\t\t} else {\n\t\t\tpageID += uint64(overflow) + 1\n\t\t}\n\t}\n}\n\n// pagePrintMeta prints the data from the meta page.\nfunc pagePrintMeta(w io.Writer, buf []byte) error {\n\tm := common.LoadPageMeta(buf)\n\tm.Print(w)\n\treturn nil\n}\n\n// pagePrintLeaf prints the data for a leaf page.\nfunc pagePrintLeaf(w io.Writer, buf []byte, formatValue string) error {\n\tp := common.LoadPage(buf)\n\n\t// print number of items.\n\tfmt.Fprintf(w, \"Item Count: %d\\n\", p.Count())\n\tfmt.Fprintf(w, \"\\n\")\n\n\t// print each key/value.\n\tfor i := uint16(0); i < p.Count(); i++ {\n\t\te := p.LeafPageElement(i)\n\n\t\t// format key as string.\n\t\tvar k string\n\t\tif isPrintable(string(e.Key())) {\n\t\t\tk = fmt.Sprintf(\"%q\", string(e.Key()))\n\t\t} else {\n\t\t\tk = fmt.Sprintf(\"%x\", string(e.Key()))\n\t\t}\n\n\t\t// format value as string.\n\t\tvar v string\n\t\tvar err error\n\t\tif e.IsBucketEntry() {\n\t\t\tb := e.Bucket()\n\t\t\tv = b.String()\n\t\t} else {\n\t\t\tv, err = formatBytes(e.Value(), formatValue)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\n\t\tfmt.Fprintf(w, \"%s: %s\\n\", k, v)\n\t}\n\tfmt.Fprintf(w, \"\\n\")\n\treturn nil\n}\n\n// pagePrintBranch prints the data for a leaf page.\nfunc pagePrintBranch(w io.Writer, buf []byte) error {\n\tp := common.LoadPage(buf)\n\n\t// print number of items.\n\tfmt.Fprintf(w, \"Item Count: %d\\n\", p.Count())\n\tfmt.Fprintf(w, \"\\n\")\n\n\t// print each key/value.\n\tfor i := uint16(0); i < p.Count(); i++ {\n\t\te := p.BranchPageElement(i)\n\n\t\t// format key as string.\n\t\tvar k string\n\t\tif isPrintable(string(e.Key())) {\n\t\t\tk = fmt.Sprintf(\"%q\", string(e.Key()))\n\t\t} else {\n\t\t\tk = fmt.Sprintf(\"%x\", string(e.Key()))\n\t\t}\n\n\t\tfmt.Fprintf(w, \"%s: <pgid=%d>\\n\", k, e.Pgid())\n\t}\n\tfmt.Fprintf(w, \"\\n\")\n\treturn nil\n}\n\n// pagePrintFreelist prints the data for a freelist page.\nfunc pagePrintFreelist(w io.Writer, buf []byte) error {\n\tp := common.LoadPage(buf)\n\n\t// print number of items.\n\t_, cnt := p.FreelistPageCount()\n\tfmt.Fprintf(w, \"Item Count: %d\\n\", cnt)\n\tfmt.Fprintf(w, \"Overflow: %d\\n\", p.Overflow())\n\n\tfmt.Fprintf(w, \"\\n\")\n\n\t// print each page in the freelist.\n\tids := p.FreelistPageIds()\n\tfor _, ids := range ids {\n\t\tfmt.Fprintf(w, \"%d\\n\", ids)\n\t}\n\tfmt.Fprintf(w, \"\\n\")\n\treturn nil\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_page_item.go",
    "content": "package command\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"strconv\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\ntype pageItemOptions struct {\n\tkeyOnly   bool\n\tvalueOnly bool\n\tformat    string\n}\n\nfunc newPageItemCommand() *cobra.Command {\n\tvar opt pageItemOptions\n\tpageItemCmd := &cobra.Command{\n\t\tUse:   \"page-item [options] <bbolt-file> pageid itemid\",\n\t\tShort: \"print a page item key and value in a bbolt database\",\n\t\tArgs:  cobra.ExactArgs(3),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tdbPath := args[0]\n\t\t\tpageID, err := strconv.ParseUint(args[1], 10, 64)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\titemID, err := strconv.ParseUint(args[2], 10, 64)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn pageItemFunc(cmd, opt, dbPath, pageID, itemID)\n\t\t},\n\t}\n\topt.AddFlags(pageItemCmd.Flags())\n\n\treturn pageItemCmd\n}\n\nfunc (o *pageItemOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.BoolVar(&o.keyOnly, \"key-only\", false, \"Print only the key\")\n\tfs.BoolVar(&o.valueOnly, \"value-only\", false, \"Print only the value\")\n\tfs.StringVar(&o.format, \"format\", \"auto\", \"Output format one of: \"+FORMAT_MODES)\n}\n\nfunc pageItemFunc(cmd *cobra.Command, cfg pageItemOptions, dbPath string, pageID, itemID uint64) (err error) {\n\tif cfg.keyOnly && cfg.valueOnly {\n\t\treturn errors.New(\"the --key-only or --value-only flag may be set, but not both\")\n\t}\n\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\t// retrieve page info and page size.\n\t_, buf, err := guts_cli.ReadPage(dbPath, pageID)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif !cfg.valueOnly {\n\t\terr := pageItemPrintLeafItemKey(cmd.OutOrStdout(), buf, uint16(itemID), cfg.format)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tif !cfg.keyOnly {\n\t\terr := pageItemPrintLeafItemValue(cmd.OutOrStdout(), buf, uint16(itemID), cfg.format)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn\n}\n\nfunc pageItemPrintLeafItemKey(w io.Writer, pageBytes []byte, index uint16, format string) error {\n\tk, _, err := pageItemLeafPageElement(pageBytes, index)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn writelnBytes(w, k, format)\n}\n\nfunc pageItemPrintLeafItemValue(w io.Writer, pageBytes []byte, index uint16, format string) error {\n\t_, v, err := pageItemLeafPageElement(pageBytes, index)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn writelnBytes(w, v, format)\n}\n\nfunc pageItemLeafPageElement(pageBytes []byte, index uint16) ([]byte, []byte, error) {\n\tp := common.LoadPage(pageBytes)\n\tif index >= p.Count() {\n\t\treturn nil, nil, fmt.Errorf(\"leafPageElement: expected item index less than %d, but got %d\", p.Count(), index)\n\t}\n\tif p.Typ() != \"leaf\" {\n\t\treturn nil, nil, fmt.Errorf(\"leafPageElement: expected page type of 'leaf', but got '%s'\", p.Typ())\n\t}\n\n\te := p.LeafPageElement(index)\n\treturn e.Key(), e.Value(), nil\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_page_item_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/hex\"\n\t\"errors\"\n\t\"fmt\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\nfunc TestPageItemCommand_Run(t *testing.T) {\n\ttestCases := []struct {\n\t\tname          string\n\t\tprintable     bool\n\t\titemId        string\n\t\texpectedKey   string\n\t\texpectedValue string\n\t}{\n\t\t{\n\t\t\tname:          \"printable items\",\n\t\t\tprintable:     true,\n\t\t\titemId:        \"0\",\n\t\t\texpectedKey:   \"key_0\",\n\t\t\texpectedValue: \"value_0\",\n\t\t},\n\t\t{\n\t\t\tname:          \"non printable items\",\n\t\t\tprintable:     false,\n\t\t\titemId:        \"0\",\n\t\t\texpectedKey:   hex.EncodeToString(convertInt64IntoBytes(0 + 1)),\n\t\t\texpectedValue: hex.EncodeToString(convertInt64IntoBytes(0 + 2)),\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})\n\t\t\tsrcPath := db.Path()\n\n\t\t\tt.Log(\"Inserting some sample data\")\n\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tb, bErr := tx.CreateBucketIfNotExists([]byte(\"data\"))\n\t\t\t\tif bErr != nil {\n\t\t\t\t\treturn bErr\n\t\t\t\t}\n\n\t\t\t\tfor i := 0; i < 100; i++ {\n\t\t\t\t\tif tc.printable {\n\t\t\t\t\t\tif bErr = b.Put([]byte(fmt.Sprintf(\"key_%d\", i)), []byte(fmt.Sprintf(\"value_%d\", i))); bErr != nil {\n\t\t\t\t\t\t\treturn bErr\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tk, v := convertInt64IntoBytes(int64(i+1)), convertInt64IntoBytes(int64(i+2))\n\t\t\t\t\t\tif bErr = b.Put(k, v); bErr != nil {\n\t\t\t\t\t\t\treturn bErr\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\t\t\trequire.NoError(t, db.Close())\n\t\t\tdefer requireDBNoChange(t, dbData(t, srcPath), srcPath)\n\n\t\t\tmeta := readMetaPage(t, srcPath)\n\t\t\tleafPageId := 0\n\t\t\tfor i := 2; i < int(meta.Pgid()); i++ {\n\t\t\t\tp, _, err := guts_cli.ReadPage(srcPath, uint64(i))\n\t\t\t\trequire.NoError(t, err)\n\t\t\t\tif p.IsLeafPage() && p.Count() > 1 {\n\t\t\t\t\tleafPageId = int(p.Id())\n\t\t\t\t}\n\t\t\t}\n\t\t\trequire.NotEqual(t, 0, leafPageId)\n\n\t\t\tt.Log(\"Running page-item command\")\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\toutBuf := &bytes.Buffer{}\n\t\t\trootCmd.SetOut(outBuf)\n\t\t\trootCmd.SetArgs([]string{\"page-item\", db.Path(), fmt.Sprintf(\"%d\", leafPageId), tc.itemId})\n\t\t\terr = rootCmd.Execute()\n\t\t\trequire.NoError(t, err)\n\n\t\t\tt.Log(\"Checking output\")\n\t\t\toutput := outBuf.String()\n\t\t\trequire.True(t, strings.Contains(output, tc.expectedKey), \"unexpected output:\", output)\n\t\t\trequire.True(t, strings.Contains(output, tc.expectedValue), \"unexpected output:\", output)\n\t\t})\n\t}\n}\n\nfunc TestPageItemCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"accepts 3 arg(s), received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"page-item\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_page_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\nfunc TestPageCommand_Run(t *testing.T) {\n\tt.Log(\"Creating a new database\")\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})\n\tdb.Close()\n\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\texp := \"Page ID:    0\\n\" +\n\t\t\"Page Type:  meta\\n\" +\n\t\t\"Total Size: 4096 bytes\\n\" +\n\t\t\"Overflow pages: 0\\n\" +\n\t\t\"Version:    2\\n\" +\n\t\t\"Page Size:  4096 bytes\\n\" +\n\t\t\"Flags:      00000000\\n\" +\n\t\t\"Root:       <pgid=3>\\n\" +\n\t\t\"Freelist:   <pgid=2>\\n\" +\n\t\t\"HWM:        <pgid=4>\\n\" +\n\t\t\"Txn ID:     0\\n\" +\n\t\t\"Checksum:   07516e114689fdee\\n\\n\"\n\n\tt.Log(\"Running page command\")\n\trootCmd := command.NewRootCommand()\n\toutBuf := &bytes.Buffer{}\n\trootCmd.SetOut(outBuf)\n\trootCmd.SetArgs([]string{\"page\", db.Path(), \"0\"})\n\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n\trequire.Equal(t, exp, outBuf.String(), \"unexpected stdout\")\n}\n\nfunc TestPageCommand_ExclusiveArgs(t *testing.T) {\n\ttestCases := []struct {\n\t\tname    string\n\t\tpageIds string\n\t\tallFlag string\n\t\texpErr  error\n\t}{\n\t\t{\n\t\t\tname:    \"flag only\",\n\t\t\tpageIds: \"\",\n\t\t\tallFlag: \"--all\",\n\t\t\texpErr:  nil,\n\t\t},\n\t\t{\n\t\t\tname:    \"pageIds only\",\n\t\t\tpageIds: \"0\",\n\t\t\tallFlag: \"\",\n\t\t\texpErr:  nil,\n\t\t},\n\t\t{\n\t\t\tname:    \"pageIds and flag\",\n\t\t\tpageIds: \"0\",\n\t\t\tallFlag: \"--all\",\n\t\t\texpErr:  command.ErrInvalidPageArgs,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tt.Log(\"Creating a new database\")\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})\n\t\t\tdb.Close()\n\n\t\t\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t\t\tt.Log(\"Running page command\")\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\toutBuf := &bytes.Buffer{}\n\t\t\trootCmd.SetOut(outBuf)\n\t\t\trootCmd.SetArgs([]string{\"page\", db.Path(), tc.pageIds, tc.allFlag})\n\n\t\t\terr := rootCmd.Execute()\n\t\t\trequire.Equal(t, tc.expErr, err)\n\t\t})\n\t}\n}\n\nfunc TestPageCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"requires at least 1 arg(s), only received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"page\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_pages.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"github.com/spf13/cobra\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\ntype PageError struct {\n\tID  int\n\tErr error\n}\n\nfunc (e *PageError) Error() string {\n\treturn fmt.Sprintf(\"page error: id=%d, err=%s\", e.ID, e.Err)\n}\n\nfunc newPagesCommand() *cobra.Command {\n\tpagesCmd := &cobra.Command{\n\t\tUse:   \"pages <bbolt-file>\",\n\t\tShort: \"print a list of pages in bbolt database\",\n\t\tLong: strings.TrimLeft(`\nPages prints a table of pages with their type (meta, leaf, branch, freelist).\nLeaf and branch pages will show a key count in the \"items\" column while the\nfreelist will show the number of free pages in the \"items\" column.\n\nThe \"overflow\" column shows the number of blocks that the page spills over\ninto. Normally there is no overflow but large keys and values can cause\na single page to take up multiple blocks.\n`, \"\\n\"),\n\t\tArgs: cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn pagesFunc(cmd, args[0])\n\t\t},\n\t}\n\n\treturn pagesCmd\n}\n\nfunc pagesFunc(cmd *cobra.Command, dbPath string) error {\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\t// Open database.\n\tdb, err := bolt.Open(dbPath, 0600, &bolt.Options{\n\t\tReadOnly:        true,\n\t\tPreLoadFreelist: true,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\t// Write header.\n\tfmt.Fprintln(cmd.OutOrStdout(), \"ID       TYPE       ITEMS  OVRFLW\")\n\tfmt.Fprintln(cmd.OutOrStdout(), \"======== ========== ====== ======\")\n\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tvar id int\n\t\tfor {\n\t\t\tp, err := tx.Page(id)\n\t\t\tif err != nil {\n\t\t\t\treturn &PageError{ID: id, Err: err}\n\t\t\t} else if p == nil {\n\t\t\t\tbreak\n\t\t\t}\n\n\t\t\t// Only display count and overflow if this is a non-free page.\n\t\t\tvar count, overflow string\n\t\t\tif p.Type != \"free\" {\n\t\t\t\tcount = strconv.Itoa(p.Count)\n\t\t\t\tif p.OverflowCount > 0 {\n\t\t\t\t\toverflow = strconv.Itoa(p.OverflowCount)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Print table row.\n\t\t\tfmt.Fprintf(cmd.OutOrStdout(), \"%-8d %-10s %-6s %-6s\\n\", p.ID, p.Type, count, overflow)\n\n\t\t\t// Move to the next non-overflow page.\n\t\t\tid += 1\n\t\t\tif p.Type != \"free\" {\n\t\t\t\tid += p.OverflowCount\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_pages_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// Ensure the \"pages\" command neither panic, nor change the db file.\nfunc TestPagesCommand_Run(t *testing.T) {\n\tt.Log(\"Creating sample DB\")\n\tdb := btesting.MustCreateDB(t)\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tfor _, name := range []string{\"foo\", \"bar\"} {\n\t\t\tb, err := tx.CreateBucket([]byte(name))\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tfor i := 0; i < 3; i++ {\n\t\t\t\tkey := fmt.Sprintf(\"%s-%d\", name, i)\n\t\t\t\tval := fmt.Sprintf(\"val-%s-%d\", name, i)\n\t\t\t\tif err := b.Put([]byte(key), []byte(val)); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\tdb.Close()\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\tt.Log(\"Running pages cmd\")\n\trootCmd := command.NewRootCommand()\n\toutputBuf := bytes.NewBufferString(\"\")\n\trootCmd.SetOut(outputBuf)\n\n\trootCmd.SetArgs([]string{\"pages\", db.Path()})\n\terr = rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tt.Log(\"Checking output\")\n\t_, err = io.ReadAll(outputBuf)\n\trequire.NoError(t, err)\n}\n\nfunc TestPagesCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"accepts 1 arg(s), received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"pages\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_root.go",
    "content": "package command\n\nimport (\n\t\"github.com/spf13/cobra\"\n)\n\nconst (\n\tcliName        = \"bbolt\"\n\tcliDescription = \"A simple command line tool for inspecting bbolt databases\"\n)\n\nfunc NewRootCommand() *cobra.Command {\n\trootCmd := &cobra.Command{\n\t\tUse:     cliName,\n\t\tShort:   cliDescription,\n\t\tVersion: \"dev\",\n\t}\n\n\trootCmd.AddCommand(\n\t\tnewVersionCommand(),\n\t\tnewSurgeryCommand(),\n\t\tnewInspectCommand(),\n\t\tnewCheckCommand(),\n\t\tnewBucketsCommand(),\n\t\tnewInfoCommand(),\n\t\tnewCompactCommand(),\n\t\tnewStatsCommand(),\n\t\tnewPagesCommand(),\n\t\tnewKeysCommand(),\n\t\tnewDumpCommand(),\n\t\tnewPageItemCommand(),\n\t\tnewPageCommand(),\n\t\tnewBenchCommand(),\n\t\tnewGetCommand(),\n\t)\n\n\treturn rootCmd\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_stats.go",
    "content": "package command\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/spf13/cobra\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nfunc newStatsCommand() *cobra.Command {\n\tstatsCmd := &cobra.Command{\n\t\tUse:   \"stats <bbolt-file>\",\n\t\tShort: \"print stats of bbolt database\",\n\t\tLong: strings.TrimLeft(`\nusage: bolt stats PATH\n\nStats performs an extensive search of the database to track every page\nreference. It starts at the current meta page and recursively iterates\nthrough every accessible bucket.\n\nThe following errors can be reported:\n\n    already freed\n        The page is referenced more than once in the freelist.\n\n    unreachable unfreed\n        The page is not referenced by a bucket or in the freelist.\n\n    reachable freed\n        The page is referenced by a bucket but is also in the freelist.\n\n    out of bounds\n        A page is referenced that is above the high water mark.\n\n    multiple references\n        A page is referenced by more than one other page.\n\n    invalid type\n        The page type is not \"meta\", \"leaf\", \"branch\", or \"freelist\".\n\nNo errors should occur in your database. However, if for some reason you\nexperience corruption, please submit a ticket to the etcd-io/bbolt project page:\n\n  https://github.com/etcd-io/bbolt/issues\n`, \"\\n\"),\n\t\tArgs: cobra.RangeArgs(1, 2),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tprefix := \"\"\n\t\t\tif len(args) > 1 {\n\t\t\t\tprefix = args[1]\n\t\t\t}\n\n\t\t\treturn statsFunc(cmd, args[0], prefix)\n\t\t},\n\t}\n\n\treturn statsCmd\n}\n\nfunc statsFunc(cmd *cobra.Command, dbPath string, prefix string) error {\n\tif _, err := checkSourceDBPath(dbPath); err != nil {\n\t\treturn err\n\t}\n\n\t// open database.\n\tdb, err := bolt.Open(dbPath, 0600, &bolt.Options{\n\t\tReadOnly:        true,\n\t\tPreLoadFreelist: true,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tvar s bolt.BucketStats\n\t\tvar count int\n\t\tif err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\tif bytes.HasPrefix(name, []byte(prefix)) {\n\t\t\t\ts.Add(b.Stats())\n\t\t\t\tcount += 1\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"Aggregate statistics for %d buckets\\n\\n\", count)\n\n\t\tfmt.Fprintln(cmd.OutOrStdout(), \"Page count statistics\")\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tNumber of logical branch pages: %d\\n\", s.BranchPageN)\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tNumber of physical branch overflow pages: %d\\n\", s.BranchOverflowN)\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tNumber of logical leaf pages: %d\\n\", s.LeafPageN)\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tNumber of physical leaf overflow pages: %d\\n\", s.LeafOverflowN)\n\n\t\tfmt.Fprintln(cmd.OutOrStdout(), \"Tree statistics\")\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tNumber of keys/value pairs: %d\\n\", s.KeyN)\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tNumber of levels in B+tree: %d\\n\", s.Depth)\n\n\t\tfmt.Fprintln(cmd.OutOrStdout(), \"Page size utilization\")\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tBytes allocated for physical branch pages: %d\\n\", s.BranchAlloc)\n\t\tvar percentage int\n\t\tif s.BranchAlloc != 0 {\n\t\t\tpercentage = int(float32(s.BranchInuse) * 100.0 / float32(s.BranchAlloc))\n\t\t}\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tBytes actually used for branch data: %d (%d%%)\\n\", s.BranchInuse, percentage)\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tBytes allocated for physical leaf pages: %d\\n\", s.LeafAlloc)\n\t\tpercentage = 0\n\t\tif s.LeafAlloc != 0 {\n\t\t\tpercentage = int(float32(s.LeafInuse) * 100.0 / float32(s.LeafAlloc))\n\t\t}\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tBytes actually used for leaf data: %d (%d%%)\\n\", s.LeafInuse, percentage)\n\n\t\tfmt.Fprintln(cmd.OutOrStdout(), \"Bucket statistics\")\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tTotal number of buckets: %d\\n\", s.BucketN)\n\t\tpercentage = 0\n\t\tif s.BucketN != 0 {\n\t\t\tpercentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN))\n\t\t}\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tTotal number on inlined buckets: %d (%d%%)\\n\", s.InlineBucketN, percentage)\n\t\tpercentage = 0\n\t\tif s.LeafInuse != 0 {\n\t\t\tpercentage = int(float32(s.InlineBucketInuse) * 100.0 / float32(s.LeafInuse))\n\t\t}\n\t\tfmt.Fprintf(cmd.OutOrStdout(), \"\\tBytes used for inlined buckets: %d (%d%%)\\n\", s.InlineBucketInuse, percentage)\n\n\t\treturn nil\n\t})\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_stats_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"io\"\n\t\"os\"\n\t\"strconv\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// Ensure the \"stats\" command executes correctly with an empty database.\nfunc TestStatsCommand_Run_EmptyDatabase(t *testing.T) {\n\t// ignore\n\tif os.Getpagesize() != 4096 {\n\t\tt.Skip(\"system does not use 4KB page size\")\n\t}\n\n\tt.Log(\"Creating sample DB\")\n\tdb := btesting.MustCreateDB(t)\n\tdb.Close()\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t// generate expected result.\n\texp := \"Aggregate statistics for 0 buckets\\n\\n\" +\n\t\t\"Page count statistics\\n\" +\n\t\t\"\\tNumber of logical branch pages: 0\\n\" +\n\t\t\"\\tNumber of physical branch overflow pages: 0\\n\" +\n\t\t\"\\tNumber of logical leaf pages: 0\\n\" +\n\t\t\"\\tNumber of physical leaf overflow pages: 0\\n\" +\n\t\t\"Tree statistics\\n\" +\n\t\t\"\\tNumber of keys/value pairs: 0\\n\" +\n\t\t\"\\tNumber of levels in B+tree: 0\\n\" +\n\t\t\"Page size utilization\\n\" +\n\t\t\"\\tBytes allocated for physical branch pages: 0\\n\" +\n\t\t\"\\tBytes actually used for branch data: 0 (0%)\\n\" +\n\t\t\"\\tBytes allocated for physical leaf pages: 0\\n\" +\n\t\t\"\\tBytes actually used for leaf data: 0 (0%)\\n\" +\n\t\t\"Bucket statistics\\n\" +\n\t\t\"\\tTotal number of buckets: 0\\n\" +\n\t\t\"\\tTotal number on inlined buckets: 0 (0%)\\n\" +\n\t\t\"\\tBytes used for inlined buckets: 0 (0%)\\n\"\n\n\tt.Log(\"Running stats cmd\")\n\trootCmd := command.NewRootCommand()\n\toutputBuf := bytes.NewBufferString(\"\")\n\trootCmd.SetOut(outputBuf)\n\n\trootCmd.SetArgs([]string{\"stats\", db.Path()})\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tt.Log(\"Checking output\")\n\toutput, err := io.ReadAll(outputBuf)\n\trequire.NoError(t, err)\n\trequire.Exactlyf(t, exp, string(output), \"unexpected stdout:\\n\\n%s\", string(output))\n}\n\n// Ensure the \"stats\" command can execute correctly.\nfunc TestStatsCommand_Run(t *testing.T) {\n\t// ignore\n\tif os.Getpagesize() != 4096 {\n\t\tt.Skip(\"system does not use 4KB page size\")\n\t}\n\n\tt.Log(\"Creating sample DB\")\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// create \"foo\" bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\n\t\t// create \"bar\" bucket.\n\t\tb, err = tx.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\n\t\t// create \"baz\" bucket.\n\t\tb, err = tx.CreateBucket([]byte(\"baz\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"key\"), []byte(\"value\")); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.Close()\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t// generate expected result.\n\texp := \"Aggregate statistics for 3 buckets\\n\\n\" +\n\t\t\"Page count statistics\\n\" +\n\t\t\"\\tNumber of logical branch pages: 0\\n\" +\n\t\t\"\\tNumber of physical branch overflow pages: 0\\n\" +\n\t\t\"\\tNumber of logical leaf pages: 1\\n\" +\n\t\t\"\\tNumber of physical leaf overflow pages: 0\\n\" +\n\t\t\"Tree statistics\\n\" +\n\t\t\"\\tNumber of keys/value pairs: 111\\n\" +\n\t\t\"\\tNumber of levels in B+tree: 1\\n\" +\n\t\t\"Page size utilization\\n\" +\n\t\t\"\\tBytes allocated for physical branch pages: 0\\n\" +\n\t\t\"\\tBytes actually used for branch data: 0 (0%)\\n\" +\n\t\t\"\\tBytes allocated for physical leaf pages: 4096\\n\" +\n\t\t\"\\tBytes actually used for leaf data: 1996 (48%)\\n\" +\n\t\t\"Bucket statistics\\n\" +\n\t\t\"\\tTotal number of buckets: 3\\n\" +\n\t\t\"\\tTotal number on inlined buckets: 2 (66%)\\n\" +\n\t\t\"\\tBytes used for inlined buckets: 236 (11%)\\n\"\n\n\tt.Log(\"Running stats cmd\")\n\trootCmd := command.NewRootCommand()\n\toutputBuf := bytes.NewBufferString(\"\")\n\trootCmd.SetOut(outputBuf)\n\n\trootCmd.SetArgs([]string{\"stats\", db.Path()})\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tt.Log(\"Checking output\")\n\toutput, err := io.ReadAll(outputBuf)\n\trequire.NoError(t, err)\n\trequire.Exactlyf(t, exp, string(output), \"unexpected stdout:\\n\\n%s\", string(output))\n}\n\nfunc TestStatsCommand_NoArgs(t *testing.T) {\n\texpErr := errors.New(\"accepts between 1 and 2 arg(s), received 0\")\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\"stats\"})\n\terr := rootCmd.Execute()\n\trequire.ErrorContains(t, err, expErr.Error())\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_surgery.go",
    "content": "package command\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n\t\"go.etcd.io/bbolt/internal/surgeon\"\n)\n\nfunc newSurgeryCommand() *cobra.Command {\n\tsurgeryCmd := &cobra.Command{\n\t\tUse:   \"surgery <subcommand>\",\n\t\tShort: \"surgery related commands\",\n\t}\n\n\tsurgeryCmd.AddCommand(newSurgeryRevertMetaPageCommand())\n\tsurgeryCmd.AddCommand(newSurgeryCopyPageCommand())\n\tsurgeryCmd.AddCommand(newSurgeryClearPageCommand())\n\tsurgeryCmd.AddCommand(newSurgeryClearPageElementsCommand())\n\tsurgeryCmd.AddCommand(newSurgeryFreelistCommand())\n\tsurgeryCmd.AddCommand(newSurgeryMetaCommand())\n\n\treturn surgeryCmd\n}\n\ntype surgeryBaseOptions struct {\n\toutputDBFilePath string\n}\n\nfunc (o *surgeryBaseOptions) AddFlags(fs *pflag.FlagSet) {\n\tfs.StringVar(&o.outputDBFilePath, \"output\", o.outputDBFilePath, \"path to the filePath db file\")\n\t_ = cobra.MarkFlagRequired(fs, \"output\")\n}\n\nfunc (o *surgeryBaseOptions) Validate() error {\n\tif o.outputDBFilePath == \"\" {\n\t\treturn errors.New(\"output database path wasn't given, specify output database file path with --output option\")\n\t}\n\treturn nil\n}\n\nfunc newSurgeryRevertMetaPageCommand() *cobra.Command {\n\tvar o surgeryBaseOptions\n\trevertMetaPageCmd := &cobra.Command{\n\t\tUse:   \"revert-meta-page <bbolt-file>\",\n\t\tShort: \"Revert the meta page to revert the changes performed by the latest transaction\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn surgeryRevertMetaPageFunc(args[0], o)\n\t\t},\n\t}\n\to.AddFlags(revertMetaPageCmd.Flags())\n\treturn revertMetaPageCmd\n}\n\nfunc surgeryRevertMetaPageFunc(srcDBPath string, cfg surgeryBaseOptions) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tif err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"[revert-meta-page] copy file failed: %w\", err)\n\t}\n\n\tif err := surgeon.RevertMetaPage(cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"revert-meta-page command failed: %w\", err)\n\t}\n\n\tfmt.Fprintln(os.Stdout, \"The meta page is reverted.\")\n\n\treturn nil\n}\n\ntype surgeryCopyPageOptions struct {\n\tsurgeryBaseOptions\n\tsourcePageId      uint64\n\tdestinationPageId uint64\n}\n\nfunc (o *surgeryCopyPageOptions) AddFlags(fs *pflag.FlagSet) {\n\to.surgeryBaseOptions.AddFlags(fs)\n\tfs.Uint64VarP(&o.sourcePageId, \"from-page\", \"\", o.sourcePageId, \"source page Id\")\n\tfs.Uint64VarP(&o.destinationPageId, \"to-page\", \"\", o.destinationPageId, \"destination page Id\")\n\t_ = cobra.MarkFlagRequired(fs, \"from-page\")\n\t_ = cobra.MarkFlagRequired(fs, \"to-page\")\n}\n\nfunc (o *surgeryCopyPageOptions) Validate() error {\n\tif err := o.surgeryBaseOptions.Validate(); err != nil {\n\t\treturn err\n\t}\n\tif o.sourcePageId == o.destinationPageId {\n\t\treturn fmt.Errorf(\"'--from-page' and '--to-page' have the same value: %d\", o.sourcePageId)\n\t}\n\treturn nil\n}\n\nfunc newSurgeryCopyPageCommand() *cobra.Command {\n\tvar o surgeryCopyPageOptions\n\tcopyPageCmd := &cobra.Command{\n\t\tUse:   \"copy-page <bbolt-file>\",\n\t\tShort: \"Copy page from the source page Id to the destination page Id\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn surgeryCopyPageFunc(args[0], o)\n\t\t},\n\t}\n\to.AddFlags(copyPageCmd.Flags())\n\treturn copyPageCmd\n}\n\nfunc surgeryCopyPageFunc(srcDBPath string, cfg surgeryCopyPageOptions) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tif err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"[copy-page] copy file failed: %w\", err)\n\t}\n\n\tif err := surgeon.CopyPage(cfg.outputDBFilePath, common.Pgid(cfg.sourcePageId), common.Pgid(cfg.destinationPageId)); err != nil {\n\t\treturn fmt.Errorf(\"copy-page command failed: %w\", err)\n\t}\n\n\tmeta, err := readMetaPage(srcDBPath)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif meta.IsFreelistPersisted() {\n\t\tfmt.Fprintf(os.Stdout, \"WARNING: the free list might have changed.\\n\")\n\t\tfmt.Fprintf(os.Stdout, \"Please consider executing `./bbolt surgery freelist abandon ...`\\n\")\n\t}\n\n\tfmt.Fprintf(os.Stdout, \"The page %d was successfully copied to page %d\\n\", cfg.sourcePageId, cfg.destinationPageId)\n\treturn nil\n}\n\ntype surgeryClearPageOptions struct {\n\tsurgeryBaseOptions\n\tpageId uint64\n}\n\nfunc (o *surgeryClearPageOptions) AddFlags(fs *pflag.FlagSet) {\n\to.surgeryBaseOptions.AddFlags(fs)\n\tfs.Uint64VarP(&o.pageId, \"pageId\", \"\", o.pageId, \"page Id\")\n\t_ = cobra.MarkFlagRequired(fs, \"pageId\")\n}\n\nfunc (o *surgeryClearPageOptions) Validate() error {\n\tif err := o.surgeryBaseOptions.Validate(); err != nil {\n\t\treturn err\n\t}\n\tif o.pageId < 2 {\n\t\treturn fmt.Errorf(\"the pageId must be at least 2, but got %d\", o.pageId)\n\t}\n\treturn nil\n}\n\nfunc newSurgeryClearPageCommand() *cobra.Command {\n\tvar o surgeryClearPageOptions\n\tclearPageCmd := &cobra.Command{\n\t\tUse:   \"clear-page <bbolt-file>\",\n\t\tShort: \"Clears all elements from the given page, which can be a branch or leaf page\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn surgeryClearPageFunc(args[0], o)\n\t\t},\n\t}\n\to.AddFlags(clearPageCmd.Flags())\n\treturn clearPageCmd\n}\n\nfunc surgeryClearPageFunc(srcDBPath string, cfg surgeryClearPageOptions) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tif err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"[clear-page] copy file failed: %w\", err)\n\t}\n\n\tneedAbandonFreelist, err := surgeon.ClearPage(cfg.outputDBFilePath, common.Pgid(cfg.pageId))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"clear-page command failed: %w\", err)\n\t}\n\n\tif needAbandonFreelist {\n\t\tfmt.Fprintf(os.Stdout, \"WARNING: The clearing has abandoned some pages that are not yet referenced from free list.\\n\")\n\t\tfmt.Fprintf(os.Stdout, \"Please consider executing `./bbolt surgery freelist abandon ...`\\n\")\n\t}\n\n\tfmt.Fprintf(os.Stdout, \"The page (%d) was cleared\\n\", cfg.pageId)\n\treturn nil\n}\n\ntype surgeryClearPageElementsOptions struct {\n\tsurgeryBaseOptions\n\tpageId          uint64\n\tstartElementIdx int\n\tendElementIdx   int\n}\n\nfunc (o *surgeryClearPageElementsOptions) AddFlags(fs *pflag.FlagSet) {\n\to.surgeryBaseOptions.AddFlags(fs)\n\tfs.Uint64VarP(&o.pageId, \"pageId\", \"\", o.pageId, \"page id\")\n\tfs.IntVarP(&o.startElementIdx, \"from-index\", \"\", o.startElementIdx, \"start element index (included) to clear, starting from 0\")\n\tfs.IntVarP(&o.endElementIdx, \"to-index\", \"\", o.endElementIdx, \"end element index (excluded) to clear, starting from 0, -1 means to the end of page\")\n\t_ = cobra.MarkFlagRequired(fs, \"pageId\")\n\t_ = cobra.MarkFlagRequired(fs, \"from-index\")\n\t_ = cobra.MarkFlagRequired(fs, \"to-index\")\n}\n\nfunc (o *surgeryClearPageElementsOptions) Validate() error {\n\tif err := o.surgeryBaseOptions.Validate(); err != nil {\n\t\treturn err\n\t}\n\tif o.pageId < 2 {\n\t\treturn fmt.Errorf(\"the pageId must be at least 2, but got %d\", o.pageId)\n\t}\n\treturn nil\n}\n\nfunc newSurgeryClearPageElementsCommand() *cobra.Command {\n\tvar o surgeryClearPageElementsOptions\n\tclearElementCmd := &cobra.Command{\n\t\tUse:   \"clear-page-elements <bbolt-file>\",\n\t\tShort: \"Clears elements from the given page, which can be a branch or leaf page\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn surgeryClearPageElementFunc(args[0], o)\n\t\t},\n\t}\n\to.AddFlags(clearElementCmd.Flags())\n\treturn clearElementCmd\n}\n\nfunc surgeryClearPageElementFunc(srcDBPath string, cfg surgeryClearPageElementsOptions) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tif err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"[clear-page-element] copy file failed: %w\", err)\n\t}\n\n\tneedAbandonFreelist, err := surgeon.ClearPageElements(cfg.outputDBFilePath, common.Pgid(cfg.pageId), cfg.startElementIdx, cfg.endElementIdx, false)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"clear-page-element command failed: %w\", err)\n\t}\n\n\tif needAbandonFreelist {\n\t\tfmt.Fprintf(os.Stdout, \"WARNING: The clearing has abandoned some pages that are not yet referenced from free list.\\n\")\n\t\tfmt.Fprintf(os.Stdout, \"Please consider executing `./bbolt surgery freelist abandon ...`\\n\")\n\t}\n\n\tfmt.Fprintf(os.Stdout, \"All elements in [%d, %d) in page %d were cleared\\n\", cfg.startElementIdx, cfg.endElementIdx, cfg.pageId)\n\treturn nil\n}\n\nfunc readMetaPage(path string) (*common.Meta, error) {\n\tpageSize, _, err := guts_cli.ReadPageAndHWMSize(path)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read Page size failed: %w\", err)\n\t}\n\n\tm := make([]*common.Meta, 2)\n\tfor i := 0; i < 2; i++ {\n\t\tm[i], _, err = ReadMetaPageAt(path, uint32(i), uint32(pageSize))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"read meta page %d failed: %w\", i, err)\n\t\t}\n\t}\n\n\tif m[0].Txid() > m[1].Txid() {\n\t\treturn m[0], nil\n\t}\n\treturn m[1], nil\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_surgery_freelist.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/spf13/cobra\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/surgeon\"\n)\n\nfunc newSurgeryFreelistCommand() *cobra.Command {\n\tcmd := &cobra.Command{\n\t\tUse:   \"freelist <subcommand>\",\n\t\tShort: \"freelist related surgery commands\",\n\t}\n\n\tcmd.AddCommand(newSurgeryFreelistAbandonCommand())\n\tcmd.AddCommand(newSurgeryFreelistRebuildCommand())\n\n\treturn cmd\n}\n\nfunc newSurgeryFreelistAbandonCommand() *cobra.Command {\n\tvar o surgeryBaseOptions\n\tabandonFreelistCmd := &cobra.Command{\n\t\tUse:   \"abandon <bbolt-file>\",\n\t\tShort: \"Abandon the freelist from both meta pages\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn surgeryFreelistAbandonFunc(args[0], o)\n\t\t},\n\t}\n\to.AddFlags(abandonFreelistCmd.Flags())\n\n\treturn abandonFreelistCmd\n}\n\nfunc surgeryFreelistAbandonFunc(srcDBPath string, cfg surgeryBaseOptions) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tif err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"[freelist abandon] copy file failed: %w\", err)\n\t}\n\n\tif err := surgeon.ClearFreelist(cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"abandom-freelist command failed: %w\", err)\n\t}\n\n\tfmt.Fprintf(os.Stdout, \"The freelist was abandoned in both meta pages.\\nIt may cause some delay on next startup because bbolt needs to scan the whole db to reconstruct the free list.\\n\")\n\treturn nil\n}\n\nfunc newSurgeryFreelistRebuildCommand() *cobra.Command {\n\tvar o surgeryBaseOptions\n\trebuildFreelistCmd := &cobra.Command{\n\t\tUse:   \"rebuild <bbolt-file>\",\n\t\tShort: \"Rebuild the freelist\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn surgeryFreelistRebuildFunc(args[0], o)\n\t\t},\n\t}\n\to.AddFlags(rebuildFreelistCmd.Flags())\n\n\treturn rebuildFreelistCmd\n}\n\nfunc surgeryFreelistRebuildFunc(srcDBPath string, cfg surgeryBaseOptions) error {\n\t// Ensure source file exists.\n\tfi, err := checkSourceDBPath(srcDBPath)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// make sure the freelist isn't present in the file.\n\tmeta, err := readMetaPage(srcDBPath)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif meta.IsFreelistPersisted() {\n\t\treturn ErrSurgeryFreelistAlreadyExist\n\t}\n\n\tif err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"[freelist rebuild] copy file failed: %w\", err)\n\t}\n\n\t// bboltDB automatically reconstruct & sync freelist in write mode.\n\tdb, err := bolt.Open(cfg.outputDBFilePath, fi.Mode(), &bolt.Options{NoFreelistSync: false})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"[freelist rebuild] open db file failed: %w\", err)\n\t}\n\terr = db.Close()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"[freelist rebuild] close db file failed: %w\", err)\n\t}\n\n\tfmt.Fprintf(os.Stdout, \"The freelist was successfully rebuilt.\\n\")\n\treturn nil\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_surgery_freelist_test.go",
    "content": "package command_test\n\nimport (\n\t\"path/filepath\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\nfunc TestSurgery_Freelist_Abandon(t *testing.T) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\n\tdefer requireDBNoChange(t, dbData(t, srcPath), srcPath)\n\n\trootCmd := command.NewRootCommand()\n\toutput := filepath.Join(t.TempDir(), \"db\")\n\trootCmd.SetArgs([]string{\n\t\t\"surgery\", \"freelist\", \"abandon\", srcPath,\n\t\t\"--output\", output,\n\t})\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tmeta0 := loadMetaPage(t, output, 0)\n\tassert.Equal(t, common.PgidNoFreelist, meta0.Freelist())\n\tmeta1 := loadMetaPage(t, output, 1)\n\tassert.Equal(t, common.PgidNoFreelist, meta1.Freelist())\n}\n\nfunc TestSurgery_Freelist_Rebuild(t *testing.T) {\n\ttestCases := []struct {\n\t\tname          string\n\t\thasFreelist   bool\n\t\texpectedError error\n\t}{\n\t\t{\n\t\t\tname:          \"normal operation\",\n\t\t\thasFreelist:   false,\n\t\t\texpectedError: nil,\n\t\t},\n\t\t{\n\t\t\tname:          \"already has freelist\",\n\t\t\thasFreelist:   true,\n\t\t\texpectedError: command.ErrSurgeryFreelistAlreadyExist,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\ttc := tc\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tpageSize := 4096\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{\n\t\t\t\tPageSize:       pageSize,\n\t\t\t\tNoFreelistSync: !tc.hasFreelist,\n\t\t\t})\n\t\t\tsrcPath := db.Path()\n\n\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\t// do nothing\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\n\t\t\tdefer requireDBNoChange(t, dbData(t, srcPath), srcPath)\n\n\t\t\t// Verify the freelist isn't synced in the beginning\n\t\t\tmeta := readMetaPage(t, srcPath)\n\t\t\tif tc.hasFreelist {\n\t\t\t\tif meta.Freelist() <= 1 || meta.Freelist() >= meta.Pgid() {\n\t\t\t\t\tt.Fatalf(\"freelist (%d) isn't in the valid range (1, %d)\", meta.Freelist(), meta.Pgid())\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\trequire.Equal(t, common.PgidNoFreelist, meta.Freelist())\n\t\t\t}\n\n\t\t\t// Execute `surgery freelist rebuild` command\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\toutput := filepath.Join(t.TempDir(), \"db\")\n\t\t\trootCmd.SetArgs([]string{\n\t\t\t\t\"surgery\", \"freelist\", \"rebuild\", srcPath,\n\t\t\t\t\"--output\", output,\n\t\t\t})\n\t\t\terr = rootCmd.Execute()\n\t\t\trequire.Equal(t, tc.expectedError, err)\n\n\t\t\tif tc.expectedError == nil {\n\t\t\t\t// Verify the freelist has already been rebuilt.\n\t\t\t\tmeta = readMetaPage(t, output)\n\t\t\t\tif meta.Freelist() <= 1 || meta.Freelist() >= meta.Pgid() {\n\t\t\t\t\tt.Fatalf(\"freelist (%d) isn't in the valid range (1, %d)\", meta.Freelist(), meta.Pgid())\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_surgery_meta.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"github.com/spf13/cobra\"\n\t\"github.com/spf13/pflag\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\nconst (\n\tmetaFieldPageSize = \"pageSize\"\n\tmetaFieldRoot     = \"root\"\n\tmetaFieldFreelist = \"freelist\"\n\tmetaFieldPgid     = \"pgid\"\n)\n\nfunc newSurgeryMetaCommand() *cobra.Command {\n\tcmd := &cobra.Command{\n\t\tUse:   \"meta <subcommand>\",\n\t\tShort: \"meta page related surgery commands\",\n\t}\n\n\tcmd.AddCommand(newSurgeryMetaValidateCommand())\n\tcmd.AddCommand(newSurgeryMetaUpdateCommand())\n\n\treturn cmd\n}\n\nfunc newSurgeryMetaValidateCommand() *cobra.Command {\n\tmetaValidateCmd := &cobra.Command{\n\t\tUse:   \"validate <bbolt-file>\",\n\t\tShort: \"Validate both meta pages\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\treturn surgeryMetaValidateFunc(args[0])\n\t\t},\n\t}\n\treturn metaValidateCmd\n}\n\nfunc surgeryMetaValidateFunc(srcDBPath string) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tvar pageSize uint32\n\n\tfor i := 0; i <= 1; i++ {\n\t\tm, _, err := ReadMetaPageAt(srcDBPath, uint32(i), pageSize)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"read meta page %d failed: %w\", i, err)\n\t\t}\n\t\tif mValidateErr := m.Validate(); mValidateErr != nil {\n\t\t\tfmt.Fprintf(os.Stdout, \"WARNING: The meta page %d isn't valid: %v!\\n\", i, mValidateErr)\n\t\t} else {\n\t\t\tfmt.Fprintf(os.Stdout, \"The meta page %d is valid!\\n\", i)\n\t\t}\n\n\t\tpageSize = m.PageSize()\n\t}\n\n\treturn nil\n}\n\ntype surgeryMetaUpdateOptions struct {\n\tsurgeryBaseOptions\n\tfields     []string\n\tmetaPageId uint32\n}\n\nvar allowedMetaUpdateFields = map[string]struct{}{\n\tmetaFieldPageSize: {},\n\tmetaFieldRoot:     {},\n\tmetaFieldFreelist: {},\n\tmetaFieldPgid:     {},\n}\n\n// AddFlags sets the flags for `meta update` command.\n// Example: --fields root:16,freelist:8 --fields pgid:128\n// Result: []string{\"root:16\", \"freelist:8\", \"pgid:128\"}\nfunc (o *surgeryMetaUpdateOptions) AddFlags(fs *pflag.FlagSet) {\n\to.surgeryBaseOptions.AddFlags(fs)\n\tfs.StringSliceVarP(&o.fields, \"fields\", \"\", o.fields, \"comma separated list of fields (supported fields: pageSize, root, freelist and pgid) to be updated, and each item is a colon-separated key-value pair\")\n\tfs.Uint32VarP(&o.metaPageId, \"meta-page\", \"\", o.metaPageId, \"the meta page ID to operate on, valid values are 0 and 1\")\n}\n\nfunc (o *surgeryMetaUpdateOptions) Validate() error {\n\tif err := o.surgeryBaseOptions.Validate(); err != nil {\n\t\treturn err\n\t}\n\n\tif o.metaPageId > 1 {\n\t\treturn fmt.Errorf(\"invalid meta page id: %d\", o.metaPageId)\n\t}\n\n\tfor _, field := range o.fields {\n\t\tkv := strings.Split(field, \":\")\n\t\tif len(kv) != 2 {\n\t\t\treturn fmt.Errorf(\"invalid key-value pair: %s\", field)\n\t\t}\n\n\t\tif _, ok := allowedMetaUpdateFields[kv[0]]; !ok {\n\t\t\treturn fmt.Errorf(\"field %q isn't allowed to be updated\", kv[0])\n\t\t}\n\n\t\tif _, err := strconv.ParseUint(kv[1], 10, 64); err != nil {\n\t\t\treturn fmt.Errorf(\"invalid value %q for field %q\", kv[1], kv[0])\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc newSurgeryMetaUpdateCommand() *cobra.Command {\n\tvar o surgeryMetaUpdateOptions\n\tmetaUpdateCmd := &cobra.Command{\n\t\tUse:   \"update <bbolt-file>\",\n\t\tShort: \"Update fields in meta pages\",\n\t\tArgs:  cobra.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif err := o.Validate(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn surgeryMetaUpdateFunc(args[0], o)\n\t\t},\n\t}\n\to.AddFlags(metaUpdateCmd.Flags())\n\treturn metaUpdateCmd\n}\n\nfunc surgeryMetaUpdateFunc(srcDBPath string, cfg surgeryMetaUpdateOptions) error {\n\tif _, err := checkSourceDBPath(srcDBPath); err != nil {\n\t\treturn err\n\t}\n\n\tif err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil {\n\t\treturn fmt.Errorf(\"[meta update] copy file failed: %w\", err)\n\t}\n\n\t// read the page size from the first meta page if we want to edit the second meta page.\n\tvar pageSize uint32\n\tif cfg.metaPageId == 1 {\n\t\tm0, _, err := ReadMetaPageAt(cfg.outputDBFilePath, 0, pageSize)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"read the first meta page failed: %w\", err)\n\t\t}\n\t\tpageSize = m0.PageSize()\n\t}\n\n\t// update the specified meta page\n\tm, buf, err := ReadMetaPageAt(cfg.outputDBFilePath, cfg.metaPageId, pageSize)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"read meta page %d failed: %w\", cfg.metaPageId, err)\n\t}\n\tmChanged := updateMetaField(m, parseFields(cfg.fields))\n\tif mChanged {\n\t\tif err := writeMetaPageAt(cfg.outputDBFilePath, buf, cfg.metaPageId, pageSize); err != nil {\n\t\t\treturn fmt.Errorf(\"[meta update] write meta page %d failed: %w\", cfg.metaPageId, err)\n\t\t}\n\t}\n\n\tif cfg.metaPageId == 1 && pageSize != m.PageSize() {\n\t\tfmt.Fprintf(os.Stdout, \"WARNING: The page size (%d) in the first meta page doesn't match the second meta page (%d)\\n\", pageSize, m.PageSize())\n\t}\n\n\t// Display results\n\tif !mChanged {\n\t\tfmt.Fprintln(os.Stdout, \"Nothing changed!\")\n\t}\n\n\tif mChanged {\n\t\tfmt.Fprintf(os.Stdout, \"The meta page %d has been updated!\\n\", cfg.metaPageId)\n\t}\n\n\treturn nil\n}\n\nfunc parseFields(fields []string) map[string]uint64 {\n\tfieldsMap := make(map[string]uint64)\n\tfor _, field := range fields {\n\t\tkv := strings.SplitN(field, \":\", 2)\n\t\tval, _ := strconv.ParseUint(kv[1], 10, 64)\n\t\tfieldsMap[kv[0]] = val\n\t}\n\treturn fieldsMap\n}\n\nfunc updateMetaField(m *common.Meta, fields map[string]uint64) bool {\n\tchanged := false\n\tfor key, val := range fields {\n\t\tswitch key {\n\t\tcase metaFieldPageSize:\n\t\t\tm.SetPageSize(uint32(val))\n\t\tcase metaFieldRoot:\n\t\t\tm.SetRootBucket(common.NewInBucket(common.Pgid(val), 0))\n\t\tcase metaFieldFreelist:\n\t\t\tm.SetFreelist(common.Pgid(val))\n\t\tcase metaFieldPgid:\n\t\t\tm.SetPgid(common.Pgid(val))\n\t\t}\n\n\t\tchanged = true\n\t}\n\n\tif m.Magic() != common.Magic {\n\t\tm.SetMagic(common.Magic)\n\t\tchanged = true\n\t}\n\tif m.Version() != common.Version {\n\t\tm.SetVersion(common.Version)\n\t\tchanged = true\n\t}\n\tif m.Flags() != common.MetaPageFlag {\n\t\tm.SetFlags(common.MetaPageFlag)\n\t\tchanged = true\n\t}\n\n\tnewChecksum := m.Sum64()\n\tif m.Checksum() != newChecksum {\n\t\tm.SetChecksum(newChecksum)\n\t\tchanged = true\n\t}\n\n\treturn changed\n}\n\nfunc ReadMetaPageAt(dbPath string, metaPageId uint32, pageSize uint32) (*common.Meta, []byte, error) {\n\tif metaPageId > 1 {\n\t\treturn nil, nil, fmt.Errorf(\"invalid metaPageId: %d\", metaPageId)\n\t}\n\n\tf, err := os.OpenFile(dbPath, os.O_RDONLY, 0444)\n\tif err != nil {\n\t\treturn nil, nil, err\n\t}\n\tdefer f.Close()\n\n\t// The meta page is just 64 bytes, and definitely less than 1024 bytes,\n\t// so it's fine to only read 1024 bytes. Note we don't care about the\n\t// pageSize when reading the first meta page, because we always read the\n\t// file starting from offset 0. Actually the passed pageSize is 0 when\n\t// reading the first meta page in the `surgery meta update` command.\n\tbuf := make([]byte, 1024)\n\tn, err := f.ReadAt(buf, int64(metaPageId*pageSize))\n\tif n == len(buf) && (err == nil || err == io.EOF) {\n\t\treturn common.LoadPageMeta(buf), buf, nil\n\t}\n\n\treturn nil, nil, err\n}\n\nfunc writeMetaPageAt(dbPath string, buf []byte, metaPageId uint32, pageSize uint32) error {\n\tif metaPageId > 1 {\n\t\treturn fmt.Errorf(\"invalid metaPageId: %d\", metaPageId)\n\t}\n\n\tf, err := os.OpenFile(dbPath, os.O_RDWR, 0666)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer f.Close()\n\n\tn, err := f.WriteAt(buf, int64(metaPageId*pageSize))\n\tif n == len(buf) && (err == nil || err == io.EOF) {\n\t\treturn nil\n\t}\n\n\treturn err\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_surgery_meta_test.go",
    "content": "package command_test\n\nimport (\n\t\"fmt\"\n\t\"path/filepath\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\nfunc TestSurgery_Meta_Validate(t *testing.T) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t// validate the meta pages\n\trootCmd := command.NewRootCommand()\n\trootCmd.SetArgs([]string{\n\t\t\"surgery\", \"meta\", \"validate\", srcPath,\n\t})\n\terr := rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\t// TODD: add one more case that the validation may fail. We need to\n\t// make the command output configurable, so that test cases can set\n\t// a customized io.Writer.\n}\n\nfunc TestSurgery_Meta_Update(t *testing.T) {\n\ttestCases := []struct {\n\t\tname     string\n\t\troot     common.Pgid\n\t\tfreelist common.Pgid\n\t\tpgid     common.Pgid\n\t}{\n\t\t{\n\t\t\tname: \"root changed\",\n\t\t\troot: 50,\n\t\t},\n\t\t{\n\t\t\tname:     \"freelist changed\",\n\t\t\tfreelist: 40,\n\t\t},\n\t\t{\n\t\t\tname: \"pgid changed\",\n\t\t\tpgid: 600,\n\t\t},\n\t\t{\n\t\t\tname:     \"both root and freelist changed\",\n\t\t\troot:     45,\n\t\t\tfreelist: 46,\n\t\t},\n\t\t{\n\t\t\tname:     \"both pgid and freelist changed\",\n\t\t\tpgid:     256,\n\t\t\tfreelist: 47,\n\t\t},\n\t\t{\n\t\t\tname:     \"all fields changed\",\n\t\t\troot:     43,\n\t\t\tfreelist: 62,\n\t\t\tpgid:     256,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tfor i := 0; i <= 1; i++ {\n\t\t\ttc := tc\n\t\t\tmetaPageId := uint32(i)\n\n\t\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\t\tpageSize := 4096\n\t\t\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\t\t\t\tsrcPath := db.Path()\n\n\t\t\t\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\t\t\t\tvar fields []string\n\t\t\t\tif tc.root != 0 {\n\t\t\t\t\tfields = append(fields, fmt.Sprintf(\"root:%d\", tc.root))\n\t\t\t\t}\n\t\t\t\tif tc.freelist != 0 {\n\t\t\t\t\tfields = append(fields, fmt.Sprintf(\"freelist:%d\", tc.freelist))\n\t\t\t\t}\n\t\t\t\tif tc.pgid != 0 {\n\t\t\t\t\tfields = append(fields, fmt.Sprintf(\"pgid:%d\", tc.pgid))\n\t\t\t\t}\n\n\t\t\t\trootCmd := command.NewRootCommand()\n\t\t\t\toutput := filepath.Join(t.TempDir(), \"db\")\n\t\t\t\trootCmd.SetArgs([]string{\n\t\t\t\t\t\"surgery\", \"meta\", \"update\", srcPath,\n\t\t\t\t\t\"--output\", output,\n\t\t\t\t\t\"--meta-page\", fmt.Sprintf(\"%d\", metaPageId),\n\t\t\t\t\t\"--fields\", strings.Join(fields, \",\"),\n\t\t\t\t})\n\t\t\t\terr := rootCmd.Execute()\n\t\t\t\trequire.NoError(t, err)\n\n\t\t\t\tm, _, err := command.ReadMetaPageAt(output, metaPageId, 4096)\n\t\t\t\trequire.NoError(t, err)\n\n\t\t\t\trequire.Equal(t, common.Magic, m.Magic())\n\t\t\t\trequire.Equal(t, common.Version, m.Version())\n\n\t\t\t\tif tc.root != 0 {\n\t\t\t\t\trequire.Equal(t, tc.root, m.RootBucket().RootPage())\n\t\t\t\t}\n\t\t\t\tif tc.freelist != 0 {\n\t\t\t\t\trequire.Equal(t, tc.freelist, m.Freelist())\n\t\t\t\t}\n\t\t\t\tif tc.pgid != 0 {\n\t\t\t\t\trequire.Equal(t, tc.pgid, m.Pgid())\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_surgery_test.go",
    "content": "package command_test\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\nfunc TestSurgery_RevertMetaPage(t *testing.T) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\n\tdefer requireDBNoChange(t, dbData(t, db.Path()), db.Path())\n\n\tsrcFile, err := os.Open(srcPath)\n\trequire.NoError(t, err)\n\tdefer srcFile.Close()\n\n\t// Read both meta0 and meta1 from srcFile\n\tsrcBuf0 := readPage(t, srcPath, 0, pageSize)\n\tsrcBuf1 := readPage(t, srcPath, 1, pageSize)\n\tmeta0Page := common.LoadPageMeta(srcBuf0)\n\tmeta1Page := common.LoadPageMeta(srcBuf1)\n\n\t// Get the non-active meta page\n\tnonActiveSrcBuf := srcBuf0\n\tnonActiveMetaPageId := 0\n\tif meta0Page.Txid() > meta1Page.Txid() {\n\t\tnonActiveSrcBuf = srcBuf1\n\t\tnonActiveMetaPageId = 1\n\t}\n\tt.Logf(\"non active meta page id: %d\", nonActiveMetaPageId)\n\n\t// revert the meta page\n\trootCmd := command.NewRootCommand()\n\toutput := filepath.Join(t.TempDir(), \"db\")\n\trootCmd.SetArgs([]string{\n\t\t\"surgery\", \"revert-meta-page\", srcPath,\n\t\t\"--output\", output,\n\t})\n\terr = rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\t// read both meta0 and meta1 from dst file\n\tdstBuf0 := readPage(t, output, 0, pageSize)\n\tdstBuf1 := readPage(t, output, 1, pageSize)\n\n\t// check result. Note we should skip the page ID\n\tassert.Equal(t, pageDataWithoutPageId(nonActiveSrcBuf), pageDataWithoutPageId(dstBuf0))\n\tassert.Equal(t, pageDataWithoutPageId(nonActiveSrcBuf), pageDataWithoutPageId(dstBuf1))\n}\n\nfunc TestSurgery_CopyPage(t *testing.T) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\n\t// Insert some sample data\n\tt.Log(\"Insert some sample data\")\n\terr := db.Fill([]byte(\"data\"), 1, 20,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 10) },\n\t)\n\trequire.NoError(t, err)\n\n\tdefer requireDBNoChange(t, dbData(t, srcPath), srcPath)\n\n\t// copy page 3 to page 2\n\tt.Log(\"copy page 3 to page 2\")\n\trootCmd := command.NewRootCommand()\n\toutput := filepath.Join(t.TempDir(), \"dstdb\")\n\trootCmd.SetArgs([]string{\n\t\t\"surgery\", \"copy-page\", srcPath,\n\t\t\"--output\", output,\n\t\t\"--from-page\", \"3\",\n\t\t\"--to-page\", \"2\",\n\t})\n\terr = rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\t// The page 2 should have exactly the same data as page 3.\n\tt.Log(\"Verify result\")\n\tsrcPageId3Data := readPage(t, srcPath, 3, pageSize)\n\tdstPageId3Data := readPage(t, output, 3, pageSize)\n\tdstPageId2Data := readPage(t, output, 2, pageSize)\n\n\tassert.Equal(t, srcPageId3Data, dstPageId3Data)\n\tassert.Equal(t, pageDataWithoutPageId(srcPageId3Data), pageDataWithoutPageId(dstPageId2Data))\n}\n\n// TODO(ahrtr): add test case below for `surgery clear-page` command:\n//  1. The page is a branch page. All its children should become free pages.\nfunc TestSurgery_ClearPage(t *testing.T) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\n\t// Insert some sample data\n\tt.Log(\"Insert some sample data\")\n\terr := db.Fill([]byte(\"data\"), 1, 20,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 10) },\n\t)\n\trequire.NoError(t, err)\n\n\tdefer requireDBNoChange(t, dbData(t, srcPath), srcPath)\n\n\t// clear page 3\n\tt.Log(\"clear page 3\")\n\trootCmd := command.NewRootCommand()\n\toutput := filepath.Join(t.TempDir(), \"dstdb\")\n\trootCmd.SetArgs([]string{\n\t\t\"surgery\", \"clear-page\", srcPath,\n\t\t\"--output\", output,\n\t\t\"--pageId\", \"3\",\n\t})\n\terr = rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\tt.Log(\"Verify result\")\n\tdstPageId3Data := readPage(t, output, 3, pageSize)\n\n\tp := common.LoadPage(dstPageId3Data)\n\tassert.Equal(t, uint16(0), p.Count())\n\tassert.Equal(t, uint32(0), p.Overflow())\n}\n\nfunc TestSurgery_ClearPageElements_Without_Overflow(t *testing.T) {\n\ttestCases := []struct {\n\t\tname                 string\n\t\tfrom                 int\n\t\tto                   int\n\t\tisBranchPage         bool\n\t\tsetEndIdxAsCount     bool\n\t\tremoveOnlyOneElement bool // only valid when setEndIdxAsCount == true, and startIdx = endIdx -1 in this case.\n\t\texpectError          bool\n\t}{\n\t\t// normal range in leaf page\n\t\t{\n\t\t\tname: \"normal range in leaf page: [4, 8)\",\n\t\t\tfrom: 4,\n\t\t\tto:   8,\n\t\t},\n\t\t{\n\t\t\tname: \"normal range in leaf page: [5, -1)\",\n\t\t\tfrom: 4,\n\t\t\tto:   -1,\n\t\t},\n\t\t{\n\t\t\tname: \"normal range in leaf page: all\",\n\t\t\tfrom: 0,\n\t\t\tto:   -1,\n\t\t},\n\t\t{\n\t\t\tname: \"normal range in leaf page: [0, 7)\",\n\t\t\tfrom: 0,\n\t\t\tto:   7,\n\t\t},\n\t\t{\n\t\t\tname:             \"normal range in leaf page: [3, count)\",\n\t\t\tfrom:             4,\n\t\t\tsetEndIdxAsCount: true,\n\t\t},\n\t\t// normal range in branch page\n\t\t{\n\t\t\tname:         \"normal range in branch page: [4, 8)\",\n\t\t\tfrom:         4,\n\t\t\tto:           8,\n\t\t\tisBranchPage: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"normal range in branch page: [5, -1)\",\n\t\t\tfrom:         4,\n\t\t\tto:           -1,\n\t\t\tisBranchPage: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"normal range in branch page: all\",\n\t\t\tfrom:         0,\n\t\t\tto:           -1,\n\t\t\tisBranchPage: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"normal range in branch page: [0, 7)\",\n\t\t\tfrom:         0,\n\t\t\tto:           7,\n\t\t\tisBranchPage: true,\n\t\t},\n\t\t{\n\t\t\tname:             \"normal range in branch page: [3, count)\",\n\t\t\tfrom:             4,\n\t\t\tisBranchPage:     true,\n\t\t\tsetEndIdxAsCount: true,\n\t\t},\n\t\t// remove only one element\n\t\t{\n\t\t\tname: \"one element: the first one\",\n\t\t\tfrom: 0,\n\t\t\tto:   1,\n\t\t},\n\t\t{\n\t\t\tname: \"one element: [6, 7)\",\n\t\t\tfrom: 6,\n\t\t\tto:   7,\n\t\t},\n\t\t{\n\t\t\tname:                 \"one element: the last one\",\n\t\t\tsetEndIdxAsCount:     true,\n\t\t\tremoveOnlyOneElement: true,\n\t\t},\n\t\t// abnormal range\n\t\t{\n\t\t\tname:        \"abnormal range: [-1, 4)\",\n\t\t\tfrom:        -1,\n\t\t\tto:          4,\n\t\t\texpectError: true,\n\t\t},\n\t\t{\n\t\t\tname:        \"abnormal range: [-2, 5)\",\n\t\t\tfrom:        -1,\n\t\t\tto:          5,\n\t\t\texpectError: true,\n\t\t},\n\t\t{\n\t\t\tname:        \"abnormal range: [3, 3)\",\n\t\t\tfrom:        3,\n\t\t\tto:          3,\n\t\t\texpectError: true,\n\t\t},\n\t\t{\n\t\t\tname:        \"abnormal range: [5, 3)\",\n\t\t\tfrom:        5,\n\t\t\tto:          3,\n\t\t\texpectError: true,\n\t\t},\n\t\t{\n\t\t\tname:        \"abnormal range: [3, -2)\",\n\t\t\tfrom:        3,\n\t\t\tto:          -2,\n\t\t\texpectError: true,\n\t\t},\n\t\t{\n\t\t\tname:        \"abnormal range: [3, 1000000)\",\n\t\t\tfrom:        -1,\n\t\t\tto:          4,\n\t\t\texpectError: true,\n\t\t},\n\t}\n\tfor _, tc := range testCases {\n\t\ttc := tc\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\ttestSurgeryClearPageElementsWithoutOverflow(t, tc.from, tc.to, tc.isBranchPage, tc.setEndIdxAsCount, tc.removeOnlyOneElement, tc.expectError)\n\t\t})\n\t}\n}\n\nfunc testSurgeryClearPageElementsWithoutOverflow(t *testing.T, startIdx, endIdx int, isBranchPage, setEndIdxAsCount, removeOnlyOne, expectError bool) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\n\t// Generate sample db\n\tt.Log(\"Generate some sample data\")\n\terr := db.Fill([]byte(\"data\"), 10, 200,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", tx*10000+k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 10) },\n\t)\n\trequire.NoError(t, err)\n\n\tdefer requireDBNoChange(t, dbData(t, srcPath), srcPath)\n\n\t// find a page with at least 10 elements\n\tvar (\n\t\tpageId       uint64 = 2\n\t\telementCount uint16 = 0\n\t)\n\tfor {\n\t\tp, _, err := guts_cli.ReadPage(srcPath, pageId)\n\t\trequire.NoError(t, err)\n\n\t\tif isBranchPage {\n\t\t\tif p.IsBranchPage() && p.Count() > 10 {\n\t\t\t\telementCount = p.Count()\n\t\t\t\tbreak\n\t\t\t}\n\t\t} else {\n\t\t\tif p.IsLeafPage() && p.Count() > 10 {\n\t\t\t\telementCount = p.Count()\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tpageId++\n\t}\n\tt.Logf(\"The original element count: %d\", elementCount)\n\n\tif setEndIdxAsCount {\n\t\tt.Logf(\"Set the endIdx as the element count: %d\", elementCount)\n\t\tendIdx = int(elementCount)\n\t\tif removeOnlyOne {\n\t\t\tstartIdx = endIdx - 1\n\t\t\tt.Logf(\"Set the startIdx as the endIdx-1: %d\", startIdx)\n\t\t}\n\t}\n\n\t// clear elements [startIdx, endIdx) in the page\n\trootCmd := command.NewRootCommand()\n\toutput := filepath.Join(t.TempDir(), \"db\")\n\trootCmd.SetArgs([]string{\n\t\t\"surgery\", \"clear-page-elements\", srcPath,\n\t\t\"--output\", output,\n\t\t\"--pageId\", fmt.Sprintf(\"%d\", pageId),\n\t\t\"--from-index\", fmt.Sprintf(\"%d\", startIdx),\n\t\t\"--to-index\", fmt.Sprintf(\"%d\", endIdx),\n\t})\n\terr = rootCmd.Execute()\n\tif expectError {\n\t\trequire.Error(t, err)\n\t\treturn\n\t}\n\n\trequire.NoError(t, err)\n\n\t// check the element count again\n\texpectedCnt := 0\n\tif endIdx == -1 {\n\t\texpectedCnt = startIdx\n\t} else {\n\t\texpectedCnt = int(elementCount) - (endIdx - startIdx)\n\t}\n\tp, _, err := guts_cli.ReadPage(output, pageId)\n\trequire.NoError(t, err)\n\tassert.Equal(t, expectedCnt, int(p.Count()))\n\n\tcompareDataAfterClearingElement(t, srcPath, output, pageId, isBranchPage, startIdx, endIdx)\n}\n\nfunc compareDataAfterClearingElement(t *testing.T, srcPath, dstPath string, pageId uint64, isBranchPage bool, startIdx, endIdx int) {\n\tsrcPage, _, err := guts_cli.ReadPage(srcPath, pageId)\n\trequire.NoError(t, err)\n\n\tdstPage, _, err := guts_cli.ReadPage(dstPath, pageId)\n\trequire.NoError(t, err)\n\n\tvar dstIdx uint16\n\tfor i := uint16(0); i < srcPage.Count(); i++ {\n\t\t// skip the cleared elements\n\t\tif dstIdx >= uint16(startIdx) && (dstIdx < uint16(endIdx) || endIdx == -1) {\n\t\t\tcontinue\n\t\t}\n\n\t\tif isBranchPage {\n\t\t\tsrcElement := srcPage.BranchPageElement(i)\n\t\t\tdstElement := dstPage.BranchPageElement(dstIdx)\n\n\t\t\trequire.Equal(t, srcElement.Key(), dstElement.Key())\n\t\t\trequire.Equal(t, srcElement.Pgid(), dstElement.Pgid())\n\t\t} else {\n\t\t\tsrcElement := srcPage.LeafPageElement(i)\n\t\t\tdstElement := dstPage.LeafPageElement(dstIdx)\n\n\t\t\trequire.Equal(t, srcElement.Flags(), dstElement.Flags())\n\t\t\trequire.Equal(t, srcElement.Key(), dstElement.Key())\n\t\t\trequire.Equal(t, srcElement.Value(), dstElement.Value())\n\t\t}\n\n\t\tdstIdx++\n\t}\n}\n\nfunc TestSurgery_ClearPageElements_With_Overflow(t *testing.T) {\n\ttestCases := []struct {\n\t\tname             string\n\t\tfrom             int\n\t\tto               int\n\t\tvalueSizes       []int\n\t\texpectedOverflow int\n\t}{\n\t\t// big element\n\t\t{\n\t\t\tname:             \"remove a big element at the end\",\n\t\t\tvalueSizes:       []int{500, 500, 500, 2600},\n\t\t\tfrom:             3,\n\t\t\tto:               4,\n\t\t\texpectedOverflow: 0,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a big element at the begin\",\n\t\t\tvalueSizes:       []int{2600, 500, 500, 500},\n\t\t\tfrom:             0,\n\t\t\tto:               1,\n\t\t\texpectedOverflow: 0,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a big element in the middle\",\n\t\t\tvalueSizes:       []int{500, 2600, 500, 500},\n\t\t\tfrom:             1,\n\t\t\tto:               2,\n\t\t\texpectedOverflow: 0,\n\t\t},\n\t\t// small element\n\t\t{\n\t\t\tname:             \"remove a small element at the end\",\n\t\t\tvalueSizes:       []int{500, 500, 3100, 100},\n\t\t\tfrom:             3,\n\t\t\tto:               4,\n\t\t\texpectedOverflow: 1,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a small element at the begin\",\n\t\t\tvalueSizes:       []int{100, 500, 3100, 500},\n\t\t\tfrom:             0,\n\t\t\tto:               1,\n\t\t\texpectedOverflow: 1,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a small element in the middle\",\n\t\t\tvalueSizes:       []int{500, 100, 3100, 500},\n\t\t\tfrom:             1,\n\t\t\tto:               2,\n\t\t\texpectedOverflow: 1,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a small element at the end of page with big overflow\",\n\t\t\tvalueSizes:       []int{500, 500, 4096 * 5, 100},\n\t\t\tfrom:             3,\n\t\t\tto:               4,\n\t\t\texpectedOverflow: 5,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a small element at the begin of page with big overflow\",\n\t\t\tvalueSizes:       []int{100, 500, 4096 * 6, 500},\n\t\t\tfrom:             0,\n\t\t\tto:               1,\n\t\t\texpectedOverflow: 6,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a small element in the middle of page with big overflow\",\n\t\t\tvalueSizes:       []int{500, 100, 4096 * 4, 500},\n\t\t\tfrom:             1,\n\t\t\tto:               2,\n\t\t\texpectedOverflow: 4,\n\t\t},\n\t\t// huge element\n\t\t{\n\t\t\tname:             \"remove a huge element at the end\",\n\t\t\tvalueSizes:       []int{500, 500, 500, 4096 * 5},\n\t\t\tfrom:             3,\n\t\t\tto:               4,\n\t\t\texpectedOverflow: 0,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a huge element at the begin\",\n\t\t\tvalueSizes:       []int{4096 * 5, 500, 500, 500},\n\t\t\tfrom:             0,\n\t\t\tto:               1,\n\t\t\texpectedOverflow: 0,\n\t\t},\n\t\t{\n\t\t\tname:             \"remove a huge element in the middle\",\n\t\t\tvalueSizes:       []int{500, 4096 * 5, 500, 500},\n\t\t\tfrom:             1,\n\t\t\tto:               2,\n\t\t\texpectedOverflow: 0,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\ttc := tc\n\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\ttestSurgeryClearPageElementsWithOverflow(t, tc.from, tc.to, tc.valueSizes, tc.expectedOverflow)\n\t\t})\n\t}\n}\n\nfunc testSurgeryClearPageElementsWithOverflow(t *testing.T, startIdx, endIdx int, valueSizes []int, expectedOverflow int) {\n\tpageSize := 4096\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})\n\tsrcPath := db.Path()\n\n\t// Generate sample db\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tb, _ := tx.CreateBucketIfNotExists([]byte(\"data\"))\n\t\tfor i, valueSize := range valueSizes {\n\t\t\tkey := []byte(fmt.Sprintf(\"%04d\", i))\n\t\t\tval := make([]byte, valueSize)\n\t\t\tif putErr := b.Put(key, val); putErr != nil {\n\t\t\t\treturn putErr\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\n\tdefer requireDBNoChange(t, dbData(t, srcPath), srcPath)\n\n\t// find a page with overflow pages\n\tvar (\n\t\tpageId       uint64 = 2\n\t\telementCount uint16 = 0\n\t)\n\tfor {\n\t\tp, _, err := guts_cli.ReadPage(srcPath, pageId)\n\t\trequire.NoError(t, err)\n\n\t\tif p.Overflow() > 0 {\n\t\t\telementCount = p.Count()\n\t\t\tbreak\n\t\t}\n\t\tpageId++\n\t}\n\tt.Logf(\"The original element count: %d\", elementCount)\n\n\t// clear elements [startIdx, endIdx) in the page\n\trootCmd := command.NewRootCommand()\n\toutput := filepath.Join(t.TempDir(), \"db\")\n\trootCmd.SetArgs([]string{\n\t\t\"surgery\", \"clear-page-elements\", srcPath,\n\t\t\"--output\", output,\n\t\t\"--pageId\", fmt.Sprintf(\"%d\", pageId),\n\t\t\"--from-index\", fmt.Sprintf(\"%d\", startIdx),\n\t\t\"--to-index\", fmt.Sprintf(\"%d\", endIdx),\n\t})\n\terr = rootCmd.Execute()\n\trequire.NoError(t, err)\n\n\t// check the element count again\n\texpectedCnt := 0\n\tif endIdx == -1 {\n\t\texpectedCnt = startIdx\n\t} else {\n\t\texpectedCnt = int(elementCount) - (endIdx - startIdx)\n\t}\n\tp, _, err := guts_cli.ReadPage(output, pageId)\n\trequire.NoError(t, err)\n\tassert.Equal(t, expectedCnt, int(p.Count()))\n\n\tassert.Equal(t, expectedOverflow, int(p.Overflow()))\n\n\tcompareDataAfterClearingElement(t, srcPath, output, pageId, false, startIdx, endIdx)\n}\n\nfunc TestSurgeryRequiredFlags(t *testing.T) {\n\terrMsgFmt := `required flag(s) \"%s\" not set`\n\ttestCases := []struct {\n\t\tname           string\n\t\targs           []string\n\t\texpectedErrMsg string\n\t}{\n\t\t// --output is required for all surgery commands\n\t\t{\n\t\t\tname:           \"no output flag for revert-meta-page\",\n\t\t\targs:           []string{\"surgery\", \"revert-meta-page\", \"db\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"output\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no output flag for copy-page\",\n\t\t\targs:           []string{\"surgery\", \"copy-page\", \"db\", \"--from-page\", \"3\", \"--to-page\", \"2\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"output\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no output flag for clear-page\",\n\t\t\targs:           []string{\"surgery\", \"clear-page\", \"db\", \"--pageId\", \"3\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"output\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no output flag for clear-page-element\",\n\t\t\targs:           []string{\"surgery\", \"clear-page-elements\", \"db\", \"--pageId\", \"4\", \"--from-index\", \"3\", \"--to-index\", \"5\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"output\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no output flag for freelist abandon\",\n\t\t\targs:           []string{\"surgery\", \"freelist\", \"abandon\", \"db\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"output\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no output flag for freelist rebuild\",\n\t\t\targs:           []string{\"surgery\", \"freelist\", \"rebuild\", \"db\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"output\"),\n\t\t},\n\t\t// --from-page and --to-page are required for 'surgery copy-page' command\n\t\t{\n\t\t\tname:           \"no from-page flag for copy-page\",\n\t\t\targs:           []string{\"surgery\", \"copy-page\", \"db\", \"--output\", \"db\", \"--to-page\", \"2\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"from-page\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no to-page flag for copy-page\",\n\t\t\targs:           []string{\"surgery\", \"copy-page\", \"db\", \"--output\", \"db\", \"--from-page\", \"2\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"to-page\"),\n\t\t},\n\t\t// --pageId is required for 'surgery clear-page' command\n\t\t{\n\t\t\tname:           \"no pageId flag for clear-page\",\n\t\t\targs:           []string{\"surgery\", \"clear-page\", \"db\", \"--output\", \"db\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"pageId\"),\n\t\t},\n\t\t// --pageId, --from-index and --to-index are required for 'surgery clear-page-element' command\n\t\t{\n\t\t\tname:           \"no pageId flag for clear-page-element\",\n\t\t\targs:           []string{\"surgery\", \"clear-page-elements\", \"db\", \"--output\", \"newdb\", \"--from-index\", \"3\", \"--to-index\", \"5\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"pageId\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no from-index flag for clear-page-element\",\n\t\t\targs:           []string{\"surgery\", \"clear-page-elements\", \"db\", \"--output\", \"newdb\", \"--pageId\", \"2\", \"--to-index\", \"5\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"from-index\"),\n\t\t},\n\t\t{\n\t\t\tname:           \"no to-index flag for clear-page-element\",\n\t\t\targs:           []string{\"surgery\", \"clear-page-elements\", \"db\", \"--output\", \"newdb\", \"--pageId\", \"2\", \"--from-index\", \"3\"},\n\t\t\texpectedErrMsg: fmt.Sprintf(errMsgFmt, \"to-index\"),\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\ttc := tc\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\trootCmd := command.NewRootCommand()\n\t\t\trootCmd.SetArgs(tc.args)\n\t\t\terr := rootCmd.Execute()\n\t\t\trequire.ErrorContains(t, err, tc.expectedErrMsg)\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "cmd/bbolt/command/command_version.go",
    "content": "package command\n\nimport (\n\t\"fmt\"\n\t\"runtime\"\n\n\t\"github.com/spf13/cobra\"\n\n\t\"go.etcd.io/bbolt/version\"\n)\n\nfunc newVersionCommand() *cobra.Command {\n\tversionCmd := &cobra.Command{\n\t\tUse:   \"version\",\n\t\tShort: \"print the current version of bbolt\",\n\t\tLong:  \"print the current version of bbolt\",\n\t\tRun: func(cmd *cobra.Command, args []string) {\n\t\t\tfmt.Printf(\"bbolt Version: %s\\n\", version.Version)\n\t\t\tfmt.Printf(\"Go Version: %s\\n\", runtime.Version())\n\t\t\tfmt.Printf(\"Go OS/Arch: %s/%s\\n\", runtime.GOOS, runtime.GOARCH)\n\t\t},\n\t}\n\n\treturn versionCmd\n}\n"
  },
  {
    "path": "cmd/bbolt/command/errors.go",
    "content": "package command\n\nimport \"errors\"\n\nvar (\n\t// ErrBatchInvalidWriteMode is returned when the write mode is other than seq, rnd, seq-nest, or rnd-nest.\n\tErrBatchInvalidWriteMode = errors.New(\"the write mode should be one of seq, rnd, seq-nest, or rnd-nest\")\n\n\t// ErrBatchNonDivisibleBatchSize is returned when the batch size can't be evenly\n\t// divided by the iteration count.\n\tErrBatchNonDivisibleBatchSize = errors.New(\"the number of iterations must be divisible by the batch size\")\n\n\t// ErrBucketRequired is returned when a bucket is not specified.\n\tErrBucketRequired = errors.New(\"bucket required\")\n\n\t// ErrInvalidPageArgs is returned when Page cmd receives pageIds and all option is true.\n\tErrInvalidPageArgs = errors.New(\"invalid args: either use '--all' or 'pageid...'\")\n\n\t// ErrInvalidValue is returned when a benchmark reads an unexpected value.\n\tErrInvalidValue = errors.New(\"invalid value\")\n\n\t// ErrKeyNotFound is returned when a key is not found.\n\tErrKeyNotFound = errors.New(\"key not found\")\n\n\t// ErrPageIDRequired is returned when a required page id is not specified.\n\tErrPageIDRequired = errors.New(\"page id required\")\n\n\t// ErrPathRequired is returned when the path to a bbolt database is not specified.\n\tErrPathRequired = errors.New(\"path required\")\n\n\t// ErrSurgeryFreelistAlreadyExist is returned when a bbolt database file already has a freelist.\n\tErrSurgeryFreelistAlreadyExist = errors.New(\"the file already has freelist, please consider to abandon the freelist to forcibly rebuild it\")\n)\n"
  },
  {
    "path": "cmd/bbolt/command/utils.go",
    "content": "package command\n\nimport (\n\t\"crypto/sha256\"\n\t\"encoding/hex\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"strconv\"\n\t\"unicode\"\n\t\"unicode/utf8\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\tberrors \"go.etcd.io/bbolt/errors\"\n)\n\nfunc checkSourceDBPath(srcPath string) (os.FileInfo, error) {\n\tfi, err := os.Stat(srcPath)\n\tif os.IsNotExist(err) {\n\t\treturn nil, fmt.Errorf(\"source database file %q doesn't exist\", srcPath)\n\t} else if err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to open source database file %q: %v\", srcPath, err)\n\t}\n\treturn fi, nil\n}\n\nconst FORMAT_MODES = \"auto|ascii-encoded|hex|bytes|redacted\"\n\n// formatBytes converts bytes into string according to format.\n// Supported formats: ascii-encoded, hex, bytes.\nfunc formatBytes(b []byte, format string) (string, error) {\n\tswitch format {\n\tcase \"ascii-encoded\":\n\t\treturn fmt.Sprintf(\"%q\", b), nil\n\tcase \"hex\":\n\t\treturn fmt.Sprintf(\"%x\", b), nil\n\tcase \"bytes\":\n\t\treturn string(b), nil\n\tcase \"auto\":\n\t\treturn bytesToAsciiOrHex(b), nil\n\tcase \"redacted\":\n\t\thash := sha256.New()\n\t\thash.Write(b)\n\t\treturn fmt.Sprintf(\"<redacted len:%d sha256:%x>\", len(b), hash.Sum(nil)), nil\n\tdefault:\n\t\treturn \"\", fmt.Errorf(\"formatBytes: unsupported format: %s\", format)\n\t}\n}\n\nfunc parseBytes(str string, format string) ([]byte, error) {\n\tswitch format {\n\tcase \"ascii-encoded\":\n\t\treturn []byte(str), nil\n\tcase \"hex\":\n\t\treturn hex.DecodeString(str)\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"parseBytes: unsupported format: %s\", format)\n\t}\n}\n\n// writelnBytes writes the byte to the writer. Supported formats: ascii-encoded, hex, bytes, auto, redacted.\n// Terminates the write with a new line symbol;\nfunc writelnBytes(w io.Writer, b []byte, format string) error {\n\tstr, err := formatBytes(b, format)\n\tif err != nil {\n\t\treturn err\n\t}\n\t_, err = fmt.Fprintln(w, str)\n\treturn err\n}\n\n// isPrintable returns true if the string is valid unicode and contains only printable runes.\nfunc isPrintable(s string) bool {\n\tif !utf8.ValidString(s) {\n\t\treturn false\n\t}\n\tfor _, ch := range s {\n\t\tif !unicode.IsPrint(ch) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\nfunc bytesToAsciiOrHex(b []byte) string {\n\tsb := string(b)\n\tif isPrintable(sb) {\n\t\treturn sb\n\t} else {\n\t\treturn hex.EncodeToString(b)\n\t}\n}\n\nfunc stringToPage(str string) (uint64, error) {\n\treturn strconv.ParseUint(str, 10, 64)\n}\n\n// stringToPages parses a slice of strings into page ids.\nfunc stringToPages(strs []string) ([]uint64, error) {\n\tvar a []uint64\n\tfor _, str := range strs {\n\t\tif len(str) == 0 {\n\t\t\tcontinue\n\t\t}\n\t\ti, err := stringToPage(str)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\ta = append(a, i)\n\t}\n\treturn a, nil\n}\n\ntype cmdKvStringer struct{}\n\nfunc (cmdKvStringer) KeyToString(key []byte) string {\n\treturn bytesToAsciiOrHex(key)\n}\n\nfunc (cmdKvStringer) ValueToString(value []byte) string {\n\treturn bytesToAsciiOrHex(value)\n}\n\nfunc CmdKvStringer() bolt.KVStringer {\n\treturn cmdKvStringer{}\n}\n\nfunc findLastBucket(tx *bolt.Tx, bucketNames []string) (*bolt.Bucket, error) {\n\tlastbucket := tx.Bucket([]byte(bucketNames[0]))\n\tif lastbucket == nil {\n\t\treturn nil, berrors.ErrBucketNotFound\n\t}\n\tfor _, bucket := range bucketNames[1:] {\n\t\tlastbucket = lastbucket.Bucket([]byte(bucket))\n\t\tif lastbucket == nil {\n\t\t\treturn nil, berrors.ErrBucketNotFound\n\t\t}\n\t}\n\treturn lastbucket, nil\n}\n"
  },
  {
    "path": "cmd/bbolt/command/utils_test.go",
    "content": "package command_test\n\nimport (\n\t\"bytes\"\n\tcrypto \"crypto/rand\"\n\t\"encoding/binary\"\n\t\"encoding/hex\"\n\t\"fmt\"\n\t\"io\"\n\t\"math/rand\"\n\t\"os\"\n\t\"strings\"\n\t\"sync\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\nfunc loadMetaPage(t *testing.T, dbPath string, pageID uint64) *common.Meta {\n\t_, buf, err := guts_cli.ReadPage(dbPath, pageID)\n\trequire.NoError(t, err)\n\treturn common.LoadPageMeta(buf)\n}\n\nfunc readMetaPage(t *testing.T, path string) *common.Meta {\n\t_, activeMetaPageId, err := guts_cli.GetRootPage(path)\n\trequire.NoError(t, err)\n\t_, buf, err := guts_cli.ReadPage(path, uint64(activeMetaPageId))\n\trequire.NoError(t, err)\n\treturn common.LoadPageMeta(buf)\n}\n\nfunc readPage(t *testing.T, path string, pageId int, pageSize int) []byte {\n\tdbFile, err := os.Open(path)\n\trequire.NoError(t, err)\n\tdefer dbFile.Close()\n\n\tfi, err := dbFile.Stat()\n\trequire.NoError(t, err)\n\trequire.GreaterOrEqual(t, fi.Size(), int64((pageId+1)*pageSize))\n\n\tbuf := make([]byte, pageSize)\n\tbyteRead, err := dbFile.ReadAt(buf, int64(pageId*pageSize))\n\trequire.NoError(t, err)\n\trequire.Equal(t, pageSize, byteRead)\n\n\treturn buf\n}\n\nfunc pageDataWithoutPageId(buf []byte) []byte {\n\treturn buf[8:]\n}\n\ntype ConcurrentBuffer struct {\n\tm   sync.Mutex\n\tbuf bytes.Buffer\n}\n\nfunc (b *ConcurrentBuffer) Read(p []byte) (n int, err error) {\n\tb.m.Lock()\n\tdefer b.m.Unlock()\n\n\treturn b.buf.Read(p)\n}\n\nfunc (b *ConcurrentBuffer) Write(p []byte) (n int, err error) {\n\tb.m.Lock()\n\tdefer b.m.Unlock()\n\n\treturn b.buf.Write(p)\n}\n\nfunc (b *ConcurrentBuffer) String() string {\n\tb.m.Lock()\n\tdefer b.m.Unlock()\n\n\treturn b.buf.String()\n}\n\nfunc fillBucket(b *bolt.Bucket, prefix []byte) error {\n\tn := 10 + rand.Intn(50)\n\tfor i := 0; i < n; i++ {\n\t\tv := make([]byte, 10*(1+rand.Intn(4)))\n\t\t_, err := crypto.Read(v)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tk := append(prefix, []byte(fmt.Sprintf(\"k%d\", i))...)\n\t\tif err := b.Put(k, v); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\t// limit depth of subbuckets\n\ts := 2 + rand.Intn(4)\n\tif len(prefix) > (2*s + 1) {\n\t\treturn nil\n\t}\n\tn = 1 + rand.Intn(3)\n\tfor i := 0; i < n; i++ {\n\t\tk := append(prefix, []byte(fmt.Sprintf(\"b%d\", i))...)\n\t\tsb, err := b.CreateBucket(k)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := fillBucket(sb, append(k, '.')); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc chkdb(path string) ([]byte, error) {\n\tdb, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer db.Close()\n\tvar buf bytes.Buffer\n\terr = db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\treturn walkBucket(b, name, nil, &buf)\n\t\t})\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn buf.Bytes(), nil\n}\n\nfunc walkBucket(parent *bolt.Bucket, k []byte, v []byte, w io.Writer) error {\n\tif _, err := fmt.Fprintf(w, \"%d:%x=%x\\n\", parent.Sequence(), k, v); err != nil {\n\t\treturn err\n\t}\n\n\t// not a bucket, exit.\n\tif v != nil {\n\t\treturn nil\n\t}\n\treturn parent.ForEach(func(k, v []byte) error {\n\t\tif v == nil {\n\t\t\treturn walkBucket(parent.Bucket(k), k, nil, w)\n\t\t}\n\t\treturn walkBucket(parent, k, v, w)\n\t})\n}\n\nfunc dbData(t *testing.T, filePath string) []byte {\n\tdata, err := os.ReadFile(filePath)\n\trequire.NoError(t, err)\n\treturn data\n}\n\nfunc requireDBNoChange(t *testing.T, oldData []byte, filePath string) {\n\tnewData, err := os.ReadFile(filePath)\n\trequire.NoError(t, err)\n\n\tnoChange := bytes.Equal(oldData, newData)\n\trequire.True(t, noChange)\n}\n\nfunc convertInt64IntoBytes(num int64) []byte {\n\tbuf := make([]byte, binary.MaxVarintLen64)\n\tn := binary.PutVarint(buf, num)\n\treturn buf[:n]\n}\n\nfunc convertInt64KeysIntoHexString(nums ...int64) string {\n\tvar res []string\n\tfor _, num := range nums {\n\t\tres = append(res, hex.EncodeToString(convertInt64IntoBytes(num)))\n\t}\n\treturn strings.Join(res, \"\\n\") + \"\\n\" // last newline char\n}\n"
  },
  {
    "path": "cmd/bbolt/main.go",
    "content": "package main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\n\t\"go.etcd.io/bbolt/cmd/bbolt/command\"\n)\n\nfunc main() {\n\trootCmd := command.NewRootCommand()\n\tif err := rootCmd.Execute(); err != nil {\n\t\tif rootCmd.SilenceErrors {\n\t\t\tfmt.Fprintln(os.Stderr, \"Error:\", err)\n\t\t}\n\t\tos.Exit(1)\n\t}\n}\n"
  },
  {
    "path": "code-of-conduct.md",
    "content": "# etcd Community Code of Conduct\n\nPlease refer to [etcd Community Code of Conduct](https://github.com/etcd-io/etcd/blob/main/code-of-conduct.md).\n"
  },
  {
    "path": "compact.go",
    "content": "package bbolt\n\n// Compact will create a copy of the source DB and in the destination DB. This may\n// reclaim space that the source database no longer has use for. txMaxSize can be\n// used to limit the transactions size of this process and may trigger intermittent\n// commits. A value of zero will ignore transaction sizes.\n// TODO: merge with: https://github.com/etcd-io/etcd/blob/b7f0f52a16dbf83f18ca1d803f7892d750366a94/mvcc/backend/backend.go#L349\nfunc Compact(dst, src *DB, txMaxSize int64) error {\n\t// commit regularly, or we'll run out of memory for large datasets if using one transaction.\n\tvar size int64\n\ttx, err := dst.Begin(true)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() {\n\t\tif tempErr := tx.Rollback(); tempErr != nil {\n\t\t\terr = tempErr\n\t\t}\n\t}()\n\n\tif err := walk(src, func(keys [][]byte, k, v []byte, seq uint64) error {\n\t\t// On each key/value, check if we have exceeded tx size.\n\t\tsz := int64(len(k) + len(v))\n\t\tif size+sz > txMaxSize && txMaxSize != 0 {\n\t\t\t// Commit previous transaction.\n\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// Start new transaction.\n\t\t\ttx, err = dst.Begin(true)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tsize = 0\n\t\t}\n\t\tsize += sz\n\n\t\t// Create bucket on the root transaction if this is the first level.\n\t\tnk := len(keys)\n\t\tif nk == 0 {\n\t\t\tbkt, err := tx.CreateBucket(k)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := bkt.SetSequence(seq); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\n\t\t// Create buckets on subsequent levels, if necessary.\n\t\tb := tx.Bucket(keys[0])\n\t\tif nk > 1 {\n\t\t\tfor _, k := range keys[1:] {\n\t\t\t\tb = b.Bucket(k)\n\t\t\t}\n\t\t}\n\n\t\t// Fill the entire page for best compaction.\n\t\tb.FillPercent = 1.0\n\n\t\t// If there is no value then this is a bucket call.\n\t\tif v == nil {\n\t\t\tbkt, err := b.CreateBucket(k)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := bkt.SetSequence(seq); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\n\t\t// Otherwise treat it as a key/value pair.\n\t\treturn b.Put(k, v)\n\t}); err != nil {\n\t\treturn err\n\t}\n\terr = tx.Commit()\n\n\treturn err\n}\n\n// walkFunc is the type of the function called for keys (buckets and \"normal\"\n// values) discovered by Walk. keys is the list of keys to descend to the bucket\n// owning the discovered key/value pair k/v.\ntype walkFunc func(keys [][]byte, k, v []byte, seq uint64) error\n\n// walk walks recursively the bolt database db, calling walkFn for each key it finds.\nfunc walk(db *DB, walkFn walkFunc) error {\n\treturn db.View(func(tx *Tx) error {\n\t\treturn tx.ForEach(func(name []byte, b *Bucket) error {\n\t\t\treturn walkBucket(b, nil, name, nil, b.Sequence(), walkFn)\n\t\t})\n\t})\n}\n\nfunc walkBucket(b *Bucket, keypath [][]byte, k, v []byte, seq uint64, fn walkFunc) error {\n\t// Execute callback.\n\tif err := fn(keypath, k, v, seq); err != nil {\n\t\treturn err\n\t}\n\n\t// If this is not a bucket then stop.\n\tif v != nil {\n\t\treturn nil\n\t}\n\n\t// Iterate over each child key/value.\n\tkeypath = append(keypath, k)\n\treturn b.ForEach(func(k, v []byte) error {\n\t\tif v == nil {\n\t\t\tbkt := b.Bucket(k)\n\t\t\treturn walkBucket(bkt, keypath, k, nil, bkt.Sequence(), fn)\n\t\t}\n\t\treturn walkBucket(b, keypath, k, v, b.Sequence(), fn)\n\t})\n}\n"
  },
  {
    "path": "concurrent_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"bytes\"\n\tcrand \"crypto/rand\"\n\t\"encoding/hex\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\tmrand \"math/rand\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"sort\"\n\t\"strings\"\n\t\"sync\"\n\t\"testing\"\n\t\"time\"\n\t\"unicode/utf8\"\n\n\t\"github.com/stretchr/testify/require\"\n\t\"golang.org/x/sync/errgroup\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nconst (\n\tbucketPrefix = \"bucket\"\n\tkeyPrefix    = \"key\"\n\tnoopTxKey    = \"%magic-no-op-key%\"\n\n\t// TestConcurrentCaseDuration is used as a env variable to specify the\n\t// concurrent test duration.\n\ttestConcurrentCaseDuration    = \"TEST_CONCURRENT_CASE_DURATION\"\n\tdefaultConcurrentTestDuration = 30 * time.Second\n)\n\ntype duration struct {\n\tmin time.Duration\n\tmax time.Duration\n}\n\ntype bytesRange struct {\n\tmin int\n\tmax int\n}\n\ntype operationChance struct {\n\toperation OperationType\n\tchance    int\n}\n\ntype concurrentConfig struct {\n\tbucketCount    int\n\tkeyCount       int\n\tworkInterval   duration\n\toperationRatio []operationChance\n\treadInterval   duration   // only used by readOperation\n\tnoopWriteRatio int        // only used by writeOperation\n\twriteBytes     bytesRange // only used by writeOperation\n}\n\n/*\nTestConcurrentGenericReadAndWrite verifies:\n 1. Repeatable read: a read transaction should always see the same data\n    view during its lifecycle.\n 2. Any data written by a writing transaction should be visible to any\n    following reading transactions (with txid >= previous writing txid).\n 3. The txid should never decrease.\n*/\nfunc TestConcurrentGenericReadAndWrite(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\ttestDuration := concurrentTestDuration(t)\n\tconf := concurrentConfig{\n\t\tbucketCount:  5,\n\t\tkeyCount:     10000,\n\t\tworkInterval: duration{},\n\t\toperationRatio: []operationChance{\n\t\t\t{operation: Read, chance: 60},\n\t\t\t{operation: Write, chance: 20},\n\t\t\t{operation: Delete, chance: 20},\n\t\t},\n\t\treadInterval: duration{\n\t\t\tmin: 50 * time.Millisecond,\n\t\t\tmax: 100 * time.Millisecond,\n\t\t},\n\t\tnoopWriteRatio: 20,\n\t\twriteBytes: bytesRange{\n\t\t\tmin: 200,\n\t\t\tmax: 16000,\n\t\t},\n\t}\n\n\ttestCases := []struct {\n\t\tname         string\n\t\tworkerCount  int\n\t\tconf         concurrentConfig\n\t\ttestDuration time.Duration\n\t}{\n\t\t{\n\t\t\tname:         \"1 worker\",\n\t\t\tworkerCount:  1,\n\t\t\tconf:         conf,\n\t\t\ttestDuration: testDuration,\n\t\t},\n\t\t{\n\t\t\tname:         \"10 workers\",\n\t\t\tworkerCount:  10,\n\t\t\tconf:         conf,\n\t\t\ttestDuration: testDuration,\n\t\t},\n\t\t{\n\t\t\tname:         \"50 workers\",\n\t\t\tworkerCount:  50,\n\t\t\tconf:         conf,\n\t\t\ttestDuration: testDuration,\n\t\t},\n\t\t{\n\t\t\tname:         \"100 workers\",\n\t\t\tworkerCount:  100,\n\t\t\tconf:         conf,\n\t\t\ttestDuration: testDuration,\n\t\t},\n\t\t{\n\t\t\tname:         \"200 workers\",\n\t\t\tworkerCount:  200,\n\t\t\tconf:         conf,\n\t\t\ttestDuration: testDuration,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\ttc := tc\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tconcurrentReadAndWrite(t,\n\t\t\t\ttc.workerCount,\n\t\t\t\ttc.conf,\n\t\t\t\ttc.testDuration)\n\t\t})\n\t}\n}\n\nfunc concurrentTestDuration(t *testing.T) time.Duration {\n\tdurationInEnv := strings.ToLower(os.Getenv(testConcurrentCaseDuration))\n\tif durationInEnv == \"\" {\n\t\tt.Logf(\"%q not set, defaults to %s\", testConcurrentCaseDuration, defaultConcurrentTestDuration)\n\t\treturn defaultConcurrentTestDuration\n\t}\n\n\td, err := time.ParseDuration(durationInEnv)\n\tif err != nil {\n\t\tt.Logf(\"Failed to parse %s=%s, error: %v, defaults to %s\", testConcurrentCaseDuration, durationInEnv, err, defaultConcurrentTestDuration)\n\t\treturn defaultConcurrentTestDuration\n\t}\n\n\tt.Logf(\"Concurrent test duration set by %s=%s\", testConcurrentCaseDuration, d)\n\treturn d\n}\n\nfunc concurrentReadAndWrite(t *testing.T,\n\tworkerCount int,\n\tconf concurrentConfig,\n\ttestDuration time.Duration) {\n\n\tt.Log(\"Preparing db.\")\n\tdb := mustCreateDB(t, &bolt.Options{\n\t\tPageSize: 4096,\n\t})\n\tdefer db.Close()\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tfor i := 0; i < conf.bucketCount; i++ {\n\t\t\tif _, err := tx.CreateBucketIfNotExists(bucketName(i)); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\n\tvar records historyRecords\n\t// t.Failed() returns false during panicking. We need to forcibly\n\t// save data on panicking.\n\t// Refer to: https://github.com/golang/go/issues/49929\n\tpanicked := true\n\tdefer func() {\n\t\tt.Log(\"Save data if failed.\")\n\t\tsaveDataIfFailed(t, db, records, panicked)\n\t}()\n\n\tt.Log(\"Starting workers.\")\n\trecords = runWorkers(t,\n\t\tdb,\n\t\tworkerCount,\n\t\tconf,\n\t\ttestDuration)\n\n\tt.Log(\"Analyzing the history records.\")\n\tif err := validateSequential(records); err != nil {\n\t\tt.Errorf(\"The history records are not sequential:\\n %v\", err)\n\t}\n\n\tt.Log(\"Checking database consistency.\")\n\tif err := checkConsistency(t, db); err != nil {\n\t\tt.Errorf(\"The data isn't consistency: %v\", err)\n\t}\n\n\tpanicked = false\n\t// TODO (ahrtr):\n\t//   1. intentionally inject a random failpoint.\n}\n\n// mustCreateDB is created in place of `btesting.MustCreateDB`, and it's\n// only supposed to be used by the concurrent test case. The purpose is\n// to ensure the test case can be executed on old branches or versions,\n// e.g. `release-1.3` or `1.3.[5-7]`.\nfunc mustCreateDB(t *testing.T, o *bolt.Options) *bolt.DB {\n\tf := filepath.Join(t.TempDir(), \"db\")\n\n\treturn mustOpenDB(t, f, o)\n}\n\nfunc mustReOpenDB(t *testing.T, db *bolt.DB, o *bolt.Options) *bolt.DB {\n\tf := db.Path()\n\n\tt.Logf(\"Closing bbolt DB at: %s\", f)\n\terr := db.Close()\n\trequire.NoError(t, err)\n\n\treturn mustOpenDB(t, f, o)\n}\n\nfunc mustOpenDB(t *testing.T, dbPath string, o *bolt.Options) *bolt.DB {\n\tt.Logf(\"Opening bbolt DB at: %s\", dbPath)\n\tif o == nil {\n\t\to = bolt.DefaultOptions\n\t}\n\n\tfreelistType := bolt.FreelistArrayType\n\tif env := os.Getenv(\"TEST_FREELIST_TYPE\"); env == string(bolt.FreelistMapType) {\n\t\tfreelistType = bolt.FreelistMapType\n\t}\n\n\to.FreelistType = freelistType\n\n\tdb, err := bolt.Open(dbPath, 0600, o)\n\trequire.NoError(t, err)\n\n\treturn db\n}\n\nfunc checkConsistency(t *testing.T, db *bolt.DB) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tcnt := 0\n\t\tfor err := range tx.Check() {\n\t\t\tt.Errorf(\"Consistency error: %v\", err)\n\t\t\tcnt++\n\t\t}\n\t\tif cnt > 0 {\n\t\t\treturn fmt.Errorf(\"%d consistency errors found\", cnt)\n\t\t}\n\t\treturn nil\n\t})\n}\n\n/*\n*********************************************************\nData structures and functions/methods for running concurrent\nworkers, which execute different operations, including `Read`,\n`Write` and `Delete`.\n*********************************************************\n*/\nfunc runWorkers(t *testing.T,\n\tdb *bolt.DB,\n\tworkerCount int,\n\tconf concurrentConfig,\n\ttestDuration time.Duration) historyRecords {\n\tstopCh := make(chan struct{}, 1)\n\terrCh := make(chan error, workerCount)\n\n\tvar mu sync.Mutex\n\tvar rs historyRecords\n\n\tg := new(errgroup.Group)\n\tfor i := 0; i < workerCount; i++ {\n\t\tw := &worker{\n\t\t\tid: i,\n\t\t\tdb: db,\n\n\t\t\tconf: conf,\n\n\t\t\terrCh:  errCh,\n\t\t\tstopCh: stopCh,\n\t\t\tt:      t,\n\t\t}\n\t\tg.Go(func() error {\n\t\t\twrs, err := runWorker(t, w, errCh)\n\t\t\tmu.Lock()\n\t\t\trs = append(rs, wrs...)\n\t\t\tmu.Unlock()\n\t\t\treturn err\n\t\t})\n\t}\n\n\tt.Logf(\"Keep all workers running for about %s.\", testDuration)\n\tselect {\n\tcase <-time.After(testDuration):\n\tcase <-errCh:\n\t}\n\n\tclose(stopCh)\n\tt.Log(\"Waiting for all workers to finish.\")\n\tif err := g.Wait(); err != nil {\n\t\tt.Errorf(\"Received error: %v\", err)\n\t}\n\n\treturn rs\n}\n\nfunc runWorker(t *testing.T, w *worker, errCh chan error) (historyRecords, error) {\n\trs, err := w.run()\n\tif len(rs) > 0 && err == nil {\n\t\tif terr := validateIncrementalTxid(rs); terr != nil {\n\t\t\ttxidErr := fmt.Errorf(\"[%s]: %w\", w.name(), terr)\n\t\t\tt.Error(txidErr)\n\t\t\terrCh <- txidErr\n\t\t\treturn rs, txidErr\n\t\t}\n\t}\n\treturn rs, err\n}\n\ntype worker struct {\n\tid int\n\tdb *bolt.DB\n\n\tconf concurrentConfig\n\n\terrCh  chan error\n\tstopCh chan struct{}\n\n\tt *testing.T\n}\n\nfunc (w *worker) name() string {\n\treturn fmt.Sprintf(\"worker-%d\", w.id)\n}\n\nfunc (w *worker) run() (historyRecords, error) {\n\tvar rs historyRecords\n\n\tticker := time.NewTicker(1 * time.Second)\n\tdefer ticker.Stop()\n\n\tfor {\n\t\tselect {\n\t\tcase <-w.stopCh:\n\t\t\treturn rs, nil\n\t\tdefault:\n\t\t}\n\n\t\terr := w.db.Update(func(tx *bolt.Tx) error {\n\t\t\tfor {\n\t\t\t\top := w.pickOperation()\n\t\t\t\tbucket, key := w.pickBucket(), w.pickKey()\n\t\t\t\trec, eerr := executeOperation(op, tx, bucket, key, w.conf)\n\t\t\t\tif eerr != nil {\n\t\t\t\t\topErr := fmt.Errorf(\"[%s: %s]: %w\", w.name(), op, eerr)\n\t\t\t\t\tw.t.Error(opErr)\n\t\t\t\t\tw.errCh <- opErr\n\t\t\t\t\treturn opErr\n\t\t\t\t}\n\n\t\t\t\trs = append(rs, rec)\n\t\t\t\tif w.conf.workInterval != (duration{}) {\n\t\t\t\t\ttime.Sleep(randomDurationInRange(w.conf.workInterval.min, w.conf.workInterval.max))\n\t\t\t\t}\n\n\t\t\t\tselect {\n\t\t\t\tcase <-ticker.C:\n\t\t\t\t\treturn nil\n\t\t\t\tcase <-w.stopCh:\n\t\t\t\t\treturn nil\n\t\t\t\tdefault:\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t\tif err != nil {\n\t\t\treturn rs, err\n\t\t}\n\t}\n}\n\nfunc (w *worker) pickBucket() []byte {\n\treturn bucketName(mrand.Intn(w.conf.bucketCount))\n}\n\nfunc bucketName(index int) []byte {\n\tbucket := fmt.Sprintf(\"%s_%d\", bucketPrefix, index)\n\treturn []byte(bucket)\n}\n\nfunc (w *worker) pickKey() []byte {\n\tkey := fmt.Sprintf(\"%s_%d\", keyPrefix, mrand.Intn(w.conf.keyCount))\n\treturn []byte(key)\n}\n\nfunc (w *worker) pickOperation() OperationType {\n\tsum := 0\n\tfor _, op := range w.conf.operationRatio {\n\t\tsum += op.chance\n\t}\n\troll := mrand.Int() % sum\n\tfor _, op := range w.conf.operationRatio {\n\t\tif roll < op.chance {\n\t\t\treturn op.operation\n\t\t}\n\t\troll -= op.chance\n\t}\n\tpanic(\"unexpected\")\n}\n\nfunc executeOperation(op OperationType, tx *bolt.Tx, bucket []byte, key []byte, conf concurrentConfig) (historyRecord, error) {\n\tswitch op {\n\tcase Read:\n\t\treturn executeRead(tx, bucket, key, conf.readInterval)\n\tcase Write:\n\t\treturn executeWrite(tx, bucket, key, conf.writeBytes, conf.noopWriteRatio)\n\tcase Delete:\n\t\treturn executeDelete(tx, bucket, key)\n\tdefault:\n\t\tpanic(fmt.Sprintf(\"unexpected operation type: %s\", op))\n\t}\n}\n\nfunc executeRead(tx *bolt.Tx, bucket []byte, key []byte, readInterval duration) (historyRecord, error) {\n\tvar rec historyRecord\n\n\tb := tx.Bucket(bucket)\n\n\tinitialVal := b.Get(key)\n\ttime.Sleep(randomDurationInRange(readInterval.min, readInterval.max))\n\tval := b.Get(key)\n\n\tif !bytes.Equal(initialVal, val) {\n\t\treturn rec, fmt.Errorf(\"read different values for the same key (%q), value1: %q, value2: %q\",\n\t\t\tstring(key), formatBytes(initialVal), formatBytes(val))\n\t}\n\n\tclonedVal := make([]byte, len(val))\n\tcopy(clonedVal, val)\n\n\trec = historyRecord{\n\t\tOperationType: Read,\n\t\tBucket:        string(bucket),\n\t\tKey:           string(key),\n\t\tValue:         clonedVal,\n\t\tTxid:          tx.ID(),\n\t}\n\n\treturn rec, nil\n}\n\nfunc executeWrite(tx *bolt.Tx, bucket []byte, key []byte, writeBytes bytesRange, noopWriteRatio int) (historyRecord, error) {\n\tvar rec historyRecord\n\n\tif mrand.Intn(100) < noopWriteRatio {\n\t\t// A no-op write transaction has two consequences:\n\t\t//    1. The txid increases by 1;\n\t\t//    2. Two meta pages point to the same root page.\n\t\trec = historyRecord{\n\t\t\tOperationType: Write,\n\t\t\tBucket:        string(bucket),\n\t\t\tKey:           noopTxKey,\n\t\t\tValue:         nil,\n\t\t\tTxid:          tx.ID(),\n\t\t}\n\t\treturn rec, nil\n\t}\n\n\tb := tx.Bucket(bucket)\n\n\tvalueBytes := randomIntInRange(writeBytes.min, writeBytes.max)\n\tv := make([]byte, valueBytes)\n\tif _, cErr := crand.Read(v); cErr != nil {\n\t\treturn rec, cErr\n\t}\n\n\tputErr := b.Put(key, v)\n\tif putErr == nil {\n\t\trec = historyRecord{\n\t\t\tOperationType: Write,\n\t\t\tBucket:        string(bucket),\n\t\t\tKey:           string(key),\n\t\t\tValue:         v,\n\t\t\tTxid:          tx.ID(),\n\t\t}\n\t}\n\n\treturn rec, putErr\n}\n\nfunc executeDelete(tx *bolt.Tx, bucket []byte, key []byte) (historyRecord, error) {\n\tvar rec historyRecord\n\n\tb := tx.Bucket(bucket)\n\n\terr := b.Delete(key)\n\tif err == nil {\n\t\trec = historyRecord{\n\t\t\tOperationType: Delete,\n\t\t\tBucket:        string(bucket),\n\t\t\tKey:           string(key),\n\t\t\tTxid:          tx.ID(),\n\t\t}\n\t}\n\n\treturn rec, err\n}\n\nfunc randomDurationInRange(min, max time.Duration) time.Duration {\n\td := int64(max) - int64(min)\n\td = int64(mrand.Intn(int(d))) + int64(min)\n\treturn time.Duration(d)\n}\n\nfunc randomIntInRange(min, max int) int {\n\treturn mrand.Intn(max-min) + min\n}\n\nfunc formatBytes(val []byte) string {\n\tif utf8.ValidString(string(val)) {\n\t\treturn string(val)\n\t}\n\n\treturn hex.EncodeToString(val)\n}\n\n/*\n*********************************************************\nFunctions for persisting test data, including db file\nand operation history\n*********************************************************\n*/\nfunc saveDataIfFailed(t *testing.T, db *bolt.DB, rs historyRecords, force bool) {\n\tif t.Failed() || force {\n\t\tt.Log(\"Saving data...\")\n\t\tdbPath := db.Path()\n\t\tif err := db.Close(); err != nil {\n\t\t\tt.Errorf(\"Failed to close db: %v\", err)\n\t\t}\n\t\tbackupPath := testResultsDirectory(t)\n\t\tbackupDB(t, dbPath, backupPath)\n\t\tpersistHistoryRecords(t, rs, backupPath)\n\t}\n}\n\nfunc backupDB(t *testing.T, srcPath string, dstPath string) {\n\ttargetFile := filepath.Join(dstPath, \"db.bak\")\n\tt.Logf(\"Saving the DB file to %s\", targetFile)\n\terr := copyFile(srcPath, targetFile)\n\trequire.NoError(t, err)\n\tt.Logf(\"DB file saved to %s\", targetFile)\n}\n\nfunc copyFile(srcPath, dstPath string) error {\n\t// Ensure source file exists.\n\t_, err := os.Stat(srcPath)\n\tif os.IsNotExist(err) {\n\t\treturn fmt.Errorf(\"source file %q not found\", srcPath)\n\t} else if err != nil {\n\t\treturn err\n\t}\n\n\t// Ensure output file not exist.\n\t_, err = os.Stat(dstPath)\n\tif err == nil {\n\t\treturn fmt.Errorf(\"output file %q already exists\", dstPath)\n\t} else if !os.IsNotExist(err) {\n\t\treturn err\n\t}\n\n\tsrcDB, err := os.Open(srcPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to open source file %q: %w\", srcPath, err)\n\t}\n\tdefer srcDB.Close()\n\tdstDB, err := os.Create(dstPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create output file %q: %w\", dstPath, err)\n\t}\n\tdefer dstDB.Close()\n\twritten, err := io.Copy(dstDB, srcDB)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to copy database file from %q to %q: %w\", srcPath, dstPath, err)\n\t}\n\n\tsrcFi, err := srcDB.Stat()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get source file info %q: %w\", srcPath, err)\n\t}\n\tinitialSize := srcFi.Size()\n\tif initialSize != written {\n\t\treturn fmt.Errorf(\"the byte copied (%q: %d) isn't equal to the initial db size (%q: %d)\", dstPath, written, srcPath, initialSize)\n\t}\n\n\treturn nil\n}\n\nfunc persistHistoryRecords(t *testing.T, rs historyRecords, path string) {\n\trecordFilePath := filepath.Join(path, \"history_records.json\")\n\tt.Logf(\"Saving history records to %s\", recordFilePath)\n\trecordFile, err := os.OpenFile(recordFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)\n\trequire.NoError(t, err)\n\tdefer recordFile.Close()\n\tencoder := json.NewEncoder(recordFile)\n\tfor _, rec := range rs {\n\t\terr := encoder.Encode(rec)\n\t\trequire.NoError(t, err)\n\t}\n}\n\nfunc testResultsDirectory(t *testing.T) string {\n\tresultsDirectory, ok := os.LookupEnv(\"RESULTS_DIR\")\n\tvar err error\n\tif !ok {\n\t\tresultsDirectory, err = os.MkdirTemp(\"\", \"*.db\")\n\t\trequire.NoError(t, err)\n\t}\n\tresultsDirectory, err = filepath.Abs(resultsDirectory)\n\trequire.NoError(t, err)\n\n\tpath, err := filepath.Abs(filepath.Join(resultsDirectory, strings.ReplaceAll(t.Name(), \"/\", \"_\")))\n\trequire.NoError(t, err)\n\n\terr = os.RemoveAll(path)\n\trequire.NoError(t, err)\n\n\terr = os.MkdirAll(path, 0700)\n\trequire.NoError(t, err)\n\n\treturn path\n}\n\n/*\n*********************************************************\nData structures and functions for analyzing history records\n*********************************************************\n*/\ntype OperationType string\n\nconst (\n\tRead   OperationType = \"read\"\n\tWrite  OperationType = \"write\"\n\tDelete OperationType = \"delete\"\n)\n\ntype historyRecord struct {\n\tOperationType OperationType `json:\"operationType,omitempty\"`\n\tTxid          int           `json:\"txid,omitempty\"`\n\tBucket        string        `json:\"bucket,omitempty\"`\n\tKey           string        `json:\"key,omitempty\"`\n\tValue         []byte        `json:\"value,omitempty\"`\n}\n\ntype historyRecords []historyRecord\n\nfunc (rs historyRecords) Len() int {\n\treturn len(rs)\n}\n\nfunc (rs historyRecords) Less(i, j int) bool {\n\t// Sorted by (bucket, key) firstly: all records in the same\n\t// (bucket, key) are grouped together.\n\tbucketCmp := strings.Compare(rs[i].Bucket, rs[j].Bucket)\n\tif bucketCmp != 0 {\n\t\treturn bucketCmp < 0\n\t}\n\tkeyCmp := strings.Compare(rs[i].Key, rs[j].Key)\n\tif keyCmp != 0 {\n\t\treturn keyCmp < 0\n\t}\n\n\t// Sorted by txid\n\treturn rs[i].Txid < rs[j].Txid\n}\n\nfunc (rs historyRecords) Swap(i, j int) {\n\trs[i], rs[j] = rs[j], rs[i]\n}\n\nfunc validateIncrementalTxid(rs historyRecords) error {\n\tlastTxid := rs[0].Txid\n\n\tfor i := 1; i < len(rs); i++ {\n\t\tif rs[i].Txid < lastTxid {\n\t\t\treturn fmt.Errorf(\"detected non-incremental txid(%d, %d) in %s mode\", lastTxid, rs[i].Txid, rs[i].OperationType)\n\t\t}\n\t\tlastTxid = rs[i].Txid\n\t}\n\n\treturn nil\n}\n\nfunc validateSequential(rs historyRecords) error {\n\tsort.Stable(rs)\n\n\ttype bucketAndKey struct {\n\t\tbucket string\n\t\tkey    string\n\t}\n\tlastWriteKeyValueMap := make(map[bucketAndKey]*historyRecord)\n\n\tfor _, rec := range rs {\n\t\tbk := bucketAndKey{\n\t\t\tbucket: rec.Bucket,\n\t\t\tkey:    rec.Key,\n\t\t}\n\t\tif v, ok := lastWriteKeyValueMap[bk]; ok {\n\t\t\tif rec.OperationType == Write {\n\t\t\t\tv.Txid = rec.Txid\n\t\t\t\tif rec.Key != noopTxKey {\n\t\t\t\t\tv.Value = rec.Value\n\t\t\t\t}\n\t\t\t} else if rec.OperationType == Delete {\n\t\t\t\tdelete(lastWriteKeyValueMap, bk)\n\t\t\t} else {\n\t\t\t\tif !bytes.Equal(v.Value, rec.Value) {\n\t\t\t\t\treturn fmt.Errorf(\"readOperation[txid: %d, bucket: %s, key: %s] read %x, \\nbut writer[txid: %d] wrote %x\",\n\t\t\t\t\t\trec.Txid, rec.Bucket, rec.Key, rec.Value, v.Txid, v.Value)\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif rec.OperationType == Write && rec.Key != noopTxKey {\n\t\t\t\tlastWriteKeyValueMap[bk] = &historyRecord{\n\t\t\t\t\tOperationType: Write,\n\t\t\t\t\tBucket:        rec.Bucket,\n\t\t\t\t\tKey:           rec.Key,\n\t\t\t\t\tValue:         rec.Value,\n\t\t\t\t\tTxid:          rec.Txid,\n\t\t\t\t}\n\t\t\t} else if rec.OperationType == Read {\n\t\t\t\tif len(rec.Value) != 0 {\n\t\t\t\t\treturn fmt.Errorf(\"expected the first readOperation[txid: %d, bucket: %s, key: %s] read nil, \\nbut got %x\",\n\t\t\t\t\t\trec.Txid, rec.Bucket, rec.Key, rec.Value)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\n/*\nTestConcurrentRepeatableRead verifies repeatable read. The case\nintentionally creates a scenario that read and write transactions\nare interleaved. It performs several writing operations after starting\neach long-running read transaction to ensure it has a larger txid\nthan previous read transaction. It verifies that bbolt correctly\nreleases free pages, and will not pollute (e.g. prematurely release)\nany pages which are still being used by any read transaction.\n*/\nfunc TestConcurrentRepeatableRead(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\ttestCases := []struct {\n\t\tname           string\n\t\tnoFreelistSync bool\n\t\tfreelistType   bolt.FreelistType\n\t}{\n\t\t// [array] freelist\n\t\t{\n\t\t\tname:           \"sync array freelist\",\n\t\t\tnoFreelistSync: false,\n\t\t\tfreelistType:   bolt.FreelistArrayType,\n\t\t},\n\t\t{\n\t\t\tname:           \"not sync array freelist\",\n\t\t\tnoFreelistSync: true,\n\t\t\tfreelistType:   bolt.FreelistArrayType,\n\t\t},\n\t\t// [map] freelist\n\t\t{\n\t\t\tname:           \"sync map freelist\",\n\t\t\tnoFreelistSync: false,\n\t\t\tfreelistType:   bolt.FreelistMapType,\n\t\t},\n\t\t{\n\t\t\tname:           \"not sync map freelist\",\n\t\t\tnoFreelistSync: true,\n\t\t\tfreelistType:   bolt.FreelistMapType,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\ttc := tc\n\t\tt.Run(tc.name, func(t *testing.T) {\n\n\t\t\tt.Log(\"Preparing db.\")\n\t\t\tvar (\n\t\t\t\tbucket = []byte(\"data\")\n\t\t\t\tkey    = []byte(\"mykey\")\n\n\t\t\t\toption = &bolt.Options{\n\t\t\t\t\tPageSize:       4096,\n\t\t\t\t\tNoFreelistSync: tc.noFreelistSync,\n\t\t\t\t\tFreelistType:   tc.freelistType,\n\t\t\t\t}\n\t\t\t)\n\n\t\t\tdb := mustCreateDB(t, option)\n\t\t\tdefer func() {\n\t\t\t\t// The db will be reopened later, so put `db.Close()` in a function\n\t\t\t\t// to avoid premature evaluation of `db`. Note that the execution\n\t\t\t\t// of a deferred function is deferred to the moment the surrounding\n\t\t\t\t// function returns, but the function value and parameters to the\n\t\t\t\t// call are evaluated as usual and saved anew.\n\t\t\t\tdb.Close()\n\t\t\t}()\n\n\t\t\t// Create lots of K/V to allocate some pages\n\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tb, err := tx.CreateBucketIfNotExists(bucket)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tfor i := 0; i < 1000; i++ {\n\t\t\t\t\tk := fmt.Sprintf(\"key_%d\", i)\n\t\t\t\t\tif err := b.Put([]byte(k), make([]byte, 1024)); err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\n\t\t\t// Remove all K/V to create some free pages\n\t\t\terr = db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tb := tx.Bucket(bucket)\n\t\t\t\tfor i := 0; i < 1000; i++ {\n\t\t\t\t\tk := fmt.Sprintf(\"key_%d\", i)\n\t\t\t\t\tif err := b.Delete([]byte(k)); err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn b.Put(key, []byte(\"randomValue\"))\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\n\t\t\t// bbolt will not release free pages directly after committing\n\t\t\t// a writing transaction; instead all pages freed are putting\n\t\t\t// into a pending list. Accordingly, the free pages might not\n\t\t\t// be able to be reused by following writing transactions. So\n\t\t\t// we reopen the db to completely release all free pages.\n\t\t\tdb = mustReOpenDB(t, db, option)\n\n\t\t\tvar (\n\t\t\t\twg                     sync.WaitGroup\n\t\t\t\tlongRunningReaderCount = 10\n\t\t\t\tstopCh                 = make(chan struct{})\n\t\t\t\terrCh                  = make(chan error, longRunningReaderCount)\n\t\t\t\treadInterval           = duration{5 * time.Millisecond, 10 * time.Millisecond}\n\n\t\t\t\twriteOperationCountInBetween = 5\n\t\t\t\twriteBytes                   = bytesRange{10, 20}\n\n\t\t\t\ttestDuration = 10 * time.Second\n\t\t\t)\n\n\t\t\tfor i := 0; i < longRunningReaderCount; i++ {\n\t\t\t\treadWorkerName := fmt.Sprintf(\"reader_%d\", i)\n\t\t\t\tt.Logf(\"Starting long running read operation: %s\", readWorkerName)\n\t\t\t\twg.Add(1)\n\t\t\t\tgo func() {\n\t\t\t\t\tdefer wg.Done()\n\t\t\t\t\trErr := executeLongRunningRead(t, readWorkerName, db, bucket, key, readInterval, stopCh)\n\t\t\t\t\tif rErr != nil {\n\t\t\t\t\t\terrCh <- rErr\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t\ttime.Sleep(500 * time.Millisecond)\n\n\t\t\t\tt.Logf(\"Perform %d write operations after starting a long running read operation\", writeOperationCountInBetween)\n\t\t\t\tfor j := 0; j < writeOperationCountInBetween; j++ {\n\t\t\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\t\t\t_, eerr := executeWrite(tx, bucket, key, writeBytes, 0)\n\t\t\t\t\t\treturn eerr\n\t\t\t\t\t})\n\n\t\t\t\t\trequire.NoError(t, err)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tt.Log(\"Perform lots of write operations to check whether the long running read operations will read dirty data\")\n\t\t\twg.Add(1)\n\t\t\tgo func() {\n\t\t\t\tdefer wg.Done()\n\t\t\t\tcnt := longRunningReaderCount * writeOperationCountInBetween\n\t\t\t\tfor i := 0; i < cnt; i++ {\n\t\t\t\t\tselect {\n\t\t\t\t\tcase <-stopCh:\n\t\t\t\t\t\treturn\n\t\t\t\t\tdefault:\n\t\t\t\t\t}\n\t\t\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\t\t\t_, eerr := executeWrite(tx, bucket, key, writeBytes, 0)\n\t\t\t\t\t\treturn eerr\n\t\t\t\t\t})\n\t\t\t\t\trequire.NoError(t, err)\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tt.Log(\"Waiting for result\")\n\t\t\tselect {\n\t\t\tcase err := <-errCh:\n\t\t\t\tclose(stopCh)\n\t\t\t\tt.Errorf(\"Detected dirty read: %v\", err)\n\t\t\tcase <-time.After(testDuration):\n\t\t\t\tclose(stopCh)\n\t\t\t}\n\n\t\t\twg.Wait()\n\t\t})\n\t}\n}\n\nfunc executeLongRunningRead(t *testing.T, name string, db *bolt.DB, bucket []byte, key []byte, readInterval duration, stopCh chan struct{}) error {\n\terr := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket(bucket)\n\n\t\tinitialVal := b.Get(key)\n\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase <-stopCh:\n\t\t\t\tt.Logf(\"%q finished.\", name)\n\t\t\t\treturn nil\n\t\t\tdefault:\n\t\t\t}\n\n\t\t\ttime.Sleep(randomDurationInRange(readInterval.min, readInterval.max))\n\t\t\tval := b.Get(key)\n\n\t\t\tif !bytes.Equal(initialVal, val) {\n\t\t\t\tdirtyReadErr := fmt.Errorf(\"read different values for the same key (%q), value1: %q, value2: %q\",\n\t\t\t\t\tstring(key), formatBytes(initialVal), formatBytes(val))\n\t\t\t\treturn dirtyReadErr\n\t\t\t}\n\t\t}\n\t})\n\n\treturn err\n}\n"
  },
  {
    "path": "cursor.go",
    "content": "package bbolt\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"sort\"\n\n\t\"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// Cursor represents an iterator that can traverse over all key/value pairs in a bucket\n// in lexicographical order.\n// Cursors see nested buckets with value == nil.\n// Cursors can be obtained from a transaction and are valid as long as the transaction is open.\n//\n// Keys and values returned from the cursor are only valid for the life of the transaction.\n//\n// Changing data while traversing with a cursor may cause it to be invalidated\n// and return unexpected keys and/or values. You must reposition your cursor\n// after mutating data.\ntype Cursor struct {\n\tbucket *Bucket\n\tstack  []elemRef\n}\n\n// Bucket returns the bucket that this cursor was created from.\nfunc (c *Cursor) Bucket() *Bucket {\n\treturn c.bucket\n}\n\n// First moves the cursor to the first item in the bucket and returns its key and value.\n// If the bucket is empty then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) First() (key []byte, value []byte) {\n\tcommon.Assert(c.bucket.tx.db != nil, \"tx closed\")\n\tk, v, flags := c.first()\n\tif (flags & uint32(common.BucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\nfunc (c *Cursor) first() (key []byte, value []byte, flags uint32) {\n\tc.stack = c.stack[:0]\n\tp, n := c.bucket.pageNode(c.bucket.RootPage())\n\tc.stack = append(c.stack, elemRef{page: p, node: n, index: 0})\n\tc.goToFirstElementOnTheStack()\n\n\t// If we land on an empty page then move to the next value.\n\t// https://github.com/boltdb/bolt/issues/450\n\tif c.stack[len(c.stack)-1].count() == 0 {\n\t\tc.next()\n\t}\n\n\tk, v, flags := c.keyValue()\n\tif (flags & uint32(common.BucketLeafFlag)) != 0 {\n\t\treturn k, nil, flags\n\t}\n\treturn k, v, flags\n}\n\n// Last moves the cursor to the last item in the bucket and returns its key and value.\n// If the bucket is empty then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Last() (key []byte, value []byte) {\n\tcommon.Assert(c.bucket.tx.db != nil, \"tx closed\")\n\tc.stack = c.stack[:0]\n\tp, n := c.bucket.pageNode(c.bucket.RootPage())\n\tref := elemRef{page: p, node: n}\n\tref.index = ref.count() - 1\n\tc.stack = append(c.stack, ref)\n\tc.last()\n\n\t// If this is an empty page (calling Delete may result in empty pages)\n\t// we call prev to find the last page that is not empty\n\tfor len(c.stack) > 1 && c.stack[len(c.stack)-1].count() == 0 {\n\t\tc.prev()\n\t}\n\n\tif len(c.stack) == 0 {\n\t\treturn nil, nil\n\t}\n\n\tk, v, flags := c.keyValue()\n\tif (flags & uint32(common.BucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Next moves the cursor to the next item in the bucket and returns its key and value.\n// If the cursor is at the end of the bucket then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Next() (key []byte, value []byte) {\n\tcommon.Assert(c.bucket.tx.db != nil, \"tx closed\")\n\tk, v, flags := c.next()\n\tif (flags & uint32(common.BucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Prev moves the cursor to the previous item in the bucket and returns its key and value.\n// If the cursor is at the beginning of the bucket then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Prev() (key []byte, value []byte) {\n\tcommon.Assert(c.bucket.tx.db != nil, \"tx closed\")\n\tk, v, flags := c.prev()\n\tif (flags & uint32(common.BucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Seek moves the cursor to a given key using a b-tree search and returns it.\n// If the key does not exist then the next key is used. If no keys\n// follow, a nil key is returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Seek(seek []byte) (key []byte, value []byte) {\n\tcommon.Assert(c.bucket.tx.db != nil, \"tx closed\")\n\n\tk, v, flags := c.seek(seek)\n\n\t// If we ended up after the last element of a page then move to the next one.\n\tif ref := &c.stack[len(c.stack)-1]; ref.index >= ref.count() {\n\t\tk, v, flags = c.next()\n\t}\n\n\tif k == nil {\n\t\treturn nil, nil\n\t} else if (flags & uint32(common.BucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Delete removes the current key/value under the cursor from the bucket.\n// Delete fails if current key/value is a bucket or if the transaction is not writable.\nfunc (c *Cursor) Delete() error {\n\tif c.bucket.tx.db == nil {\n\t\treturn errors.ErrTxClosed\n\t} else if !c.bucket.Writable() {\n\t\treturn errors.ErrTxNotWritable\n\t}\n\n\tkey, _, flags := c.keyValue()\n\t// Return an error if current value is a bucket.\n\tif (flags & common.BucketLeafFlag) != 0 {\n\t\treturn errors.ErrIncompatibleValue\n\t}\n\tc.node().del(key)\n\n\treturn nil\n}\n\n// seek moves the cursor to a given key and returns it.\n// If the key does not exist then the next key is used.\nfunc (c *Cursor) seek(seek []byte) (key []byte, value []byte, flags uint32) {\n\t// Start from root page/node and traverse to correct page.\n\tc.stack = c.stack[:0]\n\tc.search(seek, c.bucket.RootPage())\n\n\t// If this is a bucket then return a nil value.\n\treturn c.keyValue()\n}\n\n// first moves the cursor to the first leaf element under the last page in the stack.\nfunc (c *Cursor) goToFirstElementOnTheStack() {\n\tfor {\n\t\t// Exit when we hit a leaf page.\n\t\tvar ref = &c.stack[len(c.stack)-1]\n\t\tif ref.isLeaf() {\n\t\t\tbreak\n\t\t}\n\n\t\t// Keep adding pages pointing to the first element to the stack.\n\t\tvar pgId common.Pgid\n\t\tif ref.node != nil {\n\t\t\tpgId = ref.node.inodes[ref.index].Pgid()\n\t\t} else {\n\t\t\tpgId = ref.page.BranchPageElement(uint16(ref.index)).Pgid()\n\t\t}\n\t\tp, n := c.bucket.pageNode(pgId)\n\t\tc.stack = append(c.stack, elemRef{page: p, node: n, index: 0})\n\t}\n}\n\n// last moves the cursor to the last leaf element under the last page in the stack.\nfunc (c *Cursor) last() {\n\tfor {\n\t\t// Exit when we hit a leaf page.\n\t\tref := &c.stack[len(c.stack)-1]\n\t\tif ref.isLeaf() {\n\t\t\tbreak\n\t\t}\n\n\t\t// Keep adding pages pointing to the last element in the stack.\n\t\tvar pgId common.Pgid\n\t\tif ref.node != nil {\n\t\t\tpgId = ref.node.inodes[ref.index].Pgid()\n\t\t} else {\n\t\t\tpgId = ref.page.BranchPageElement(uint16(ref.index)).Pgid()\n\t\t}\n\t\tp, n := c.bucket.pageNode(pgId)\n\n\t\tvar nextRef = elemRef{page: p, node: n}\n\t\tnextRef.index = nextRef.count() - 1\n\t\tc.stack = append(c.stack, nextRef)\n\t}\n}\n\n// next moves to the next leaf element and returns the key and value.\n// If the cursor is at the last leaf element then it stays there and returns nil.\nfunc (c *Cursor) next() (key []byte, value []byte, flags uint32) {\n\tfor {\n\t\t// Attempt to move over one element until we're successful.\n\t\t// Move up the stack as we hit the end of each page in our stack.\n\t\tvar i int\n\t\tfor i = len(c.stack) - 1; i >= 0; i-- {\n\t\t\telem := &c.stack[i]\n\t\t\tif elem.index < elem.count()-1 {\n\t\t\t\telem.index++\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\t// If we've hit the root page then stop and return. This will leave the\n\t\t// cursor on the last element of the last page.\n\t\tif i == -1 {\n\t\t\treturn nil, nil, 0\n\t\t}\n\n\t\t// Otherwise start from where we left off in the stack and find the\n\t\t// first element of the first leaf page.\n\t\tc.stack = c.stack[:i+1]\n\t\tc.goToFirstElementOnTheStack()\n\n\t\t// If this is an empty page then restart and move back up the stack.\n\t\t// https://github.com/boltdb/bolt/issues/450\n\t\tif c.stack[len(c.stack)-1].count() == 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\treturn c.keyValue()\n\t}\n}\n\n// prev moves the cursor to the previous item in the bucket and returns its key and value.\n// If the cursor is at the beginning of the bucket then a nil key and value are returned.\nfunc (c *Cursor) prev() (key []byte, value []byte, flags uint32) {\n\t// Attempt to move back one element until we're successful.\n\t// Move up the stack as we hit the beginning of each page in our stack.\n\tfor i := len(c.stack) - 1; i >= 0; i-- {\n\t\telem := &c.stack[i]\n\t\tif elem.index > 0 {\n\t\t\telem.index--\n\t\t\tbreak\n\t\t}\n\t\t// If we've hit the beginning, we should stop moving the cursor,\n\t\t// and stay at the first element, so that users can continue to\n\t\t// iterate over the elements in reverse direction by calling `Next`.\n\t\t// We should return nil in such case.\n\t\t// Refer to https://github.com/etcd-io/bbolt/issues/733\n\t\tif len(c.stack) == 1 {\n\t\t\tc.first()\n\t\t\treturn nil, nil, 0\n\t\t}\n\t\tc.stack = c.stack[:i]\n\t}\n\n\t// If we've hit the end then return nil.\n\tif len(c.stack) == 0 {\n\t\treturn nil, nil, 0\n\t}\n\n\t// Move down the stack to find the last element of the last leaf under this branch.\n\tc.last()\n\treturn c.keyValue()\n}\n\n// search recursively performs a binary search against a given page/node until it finds a given key.\nfunc (c *Cursor) search(key []byte, pgId common.Pgid) {\n\tp, n := c.bucket.pageNode(pgId)\n\tif p != nil && !p.IsBranchPage() && !p.IsLeafPage() {\n\t\tpanic(fmt.Sprintf(\"invalid page type: %d: %x\", p.Id(), p.Flags()))\n\t}\n\te := elemRef{page: p, node: n}\n\tc.stack = append(c.stack, e)\n\n\t// If we're on a leaf page/node then find the specific node.\n\tif e.isLeaf() {\n\t\tc.nsearch(key)\n\t\treturn\n\t}\n\n\tif n != nil {\n\t\tc.searchNode(key, n)\n\t\treturn\n\t}\n\tc.searchPage(key, p)\n}\n\nfunc (c *Cursor) searchNode(key []byte, n *node) {\n\tvar exact bool\n\tindex := sort.Search(len(n.inodes), func(i int) bool {\n\t\t// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.\n\t\t// sort.Search() finds the lowest index where f() != -1 but we need the highest index.\n\t\tret := bytes.Compare(n.inodes[i].Key(), key)\n\t\tif ret == 0 {\n\t\t\texact = true\n\t\t}\n\t\treturn ret != -1\n\t})\n\tif !exact && index > 0 {\n\t\tindex--\n\t}\n\tc.stack[len(c.stack)-1].index = index\n\n\t// Recursively search to the next page.\n\tc.search(key, n.inodes[index].Pgid())\n}\n\nfunc (c *Cursor) searchPage(key []byte, p *common.Page) {\n\t// Binary search for the correct range.\n\tinodes := p.BranchPageElements()\n\n\tvar exact bool\n\tindex := sort.Search(int(p.Count()), func(i int) bool {\n\t\t// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.\n\t\t// sort.Search() finds the lowest index where f() != -1 but we need the highest index.\n\t\tret := bytes.Compare(inodes[i].Key(), key)\n\t\tif ret == 0 {\n\t\t\texact = true\n\t\t}\n\t\treturn ret != -1\n\t})\n\tif !exact && index > 0 {\n\t\tindex--\n\t}\n\tc.stack[len(c.stack)-1].index = index\n\n\t// Recursively search to the next page.\n\tc.search(key, inodes[index].Pgid())\n}\n\n// nsearch searches the leaf node on the top of the stack for a key.\nfunc (c *Cursor) nsearch(key []byte) {\n\te := &c.stack[len(c.stack)-1]\n\tp, n := e.page, e.node\n\n\t// If we have a node then search its inodes.\n\tif n != nil {\n\t\tindex := sort.Search(len(n.inodes), func(i int) bool {\n\t\t\treturn bytes.Compare(n.inodes[i].Key(), key) != -1\n\t\t})\n\t\te.index = index\n\t\treturn\n\t}\n\n\t// If we have a page then search its leaf elements.\n\tinodes := p.LeafPageElements()\n\tindex := sort.Search(int(p.Count()), func(i int) bool {\n\t\treturn bytes.Compare(inodes[i].Key(), key) != -1\n\t})\n\te.index = index\n}\n\n// keyValue returns the key and value of the current leaf element.\nfunc (c *Cursor) keyValue() ([]byte, []byte, uint32) {\n\tref := &c.stack[len(c.stack)-1]\n\n\t// If the cursor is pointing to the end of page/node then return nil.\n\tif ref.count() == 0 || ref.index >= ref.count() {\n\t\treturn nil, nil, 0\n\t}\n\n\t// Retrieve value from node.\n\tif ref.node != nil {\n\t\tinode := &ref.node.inodes[ref.index]\n\t\treturn inode.Key(), inode.Value(), inode.Flags()\n\t}\n\n\t// Or retrieve value from page.\n\telem := ref.page.LeafPageElement(uint16(ref.index))\n\treturn elem.Key(), elem.Value(), elem.Flags()\n}\n\n// node returns the node that the cursor is currently positioned on.\nfunc (c *Cursor) node() *node {\n\tcommon.Assert(len(c.stack) > 0, \"accessing a node with a zero-length cursor stack\")\n\n\t// If the top of the stack is a leaf node then just return it.\n\tif ref := &c.stack[len(c.stack)-1]; ref.node != nil && ref.isLeaf() {\n\t\treturn ref.node\n\t}\n\n\t// Start from root and traverse down the hierarchy.\n\tvar n = c.stack[0].node\n\tif n == nil {\n\t\tn = c.bucket.node(c.stack[0].page.Id(), nil)\n\t}\n\tfor _, ref := range c.stack[:len(c.stack)-1] {\n\t\tcommon.Assert(!n.isLeaf, \"expected branch node\")\n\t\tn = n.childAt(ref.index)\n\t}\n\tcommon.Assert(n.isLeaf, \"expected leaf node\")\n\treturn n\n}\n\n// elemRef represents a reference to an element on a given page/node.\ntype elemRef struct {\n\tpage  *common.Page\n\tnode  *node\n\tindex int\n}\n\n// isLeaf returns whether the ref is pointing at a leaf page/node.\nfunc (r *elemRef) isLeaf() bool {\n\tif r.node != nil {\n\t\treturn r.node.isLeaf\n\t}\n\treturn r.page.IsLeafPage()\n}\n\n// count returns the number of inodes or page elements.\nfunc (r *elemRef) count() int {\n\tif r.node != nil {\n\t\treturn len(r.node.inodes)\n\t}\n\treturn int(r.page.Count())\n}\n"
  },
  {
    "path": "cursor_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/binary\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\t\"reflect\"\n\t\"sort\"\n\t\"testing\"\n\t\"testing/quick\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// TestCursor_RepeatOperations verifies that a cursor can continue to\n// iterate over all elements in reverse direction when it has already\n// reached to the end or beginning.\n// Refer to https://github.com/etcd-io/bbolt/issues/733\nfunc TestCursor_RepeatOperations(t *testing.T) {\n\ttestCases := []struct {\n\t\tname     string\n\t\ttestFunc func(t2 *testing.T, bucket *bolt.Bucket)\n\t}{\n\t\t{\n\t\t\tname:     \"Repeat NextPrevNext\",\n\t\t\ttestFunc: testRepeatCursorOperations_NextPrevNext,\n\t\t},\n\t\t{\n\t\t\tname:     \"Repeat PrevNextPrev\",\n\t\t\ttestFunc: testRepeatCursorOperations_PrevNextPrev,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})\n\n\t\t\tbucketName := []byte(\"data\")\n\n\t\t\t_ = db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tb, _ := tx.CreateBucketIfNotExists(bucketName)\n\t\t\t\ttestCursorRepeatOperations_PrepareData(t, b)\n\t\t\t\treturn nil\n\t\t\t})\n\n\t\t\t_ = db.View(func(tx *bolt.Tx) error {\n\t\t\t\tb := tx.Bucket(bucketName)\n\t\t\t\ttc.testFunc(t, b)\n\t\t\t\treturn nil\n\t\t\t})\n\t\t})\n\t}\n}\n\nfunc testCursorRepeatOperations_PrepareData(t *testing.T, b *bolt.Bucket) {\n\t// ensure we have at least one branch page.\n\tfor i := 0; i < 1000; i++ {\n\t\tk := []byte(fmt.Sprintf(\"%05d\", i))\n\t\terr := b.Put(k, k)\n\t\trequire.NoError(t, err)\n\t}\n}\n\nfunc testRepeatCursorOperations_NextPrevNext(t *testing.T, b *bolt.Bucket) {\n\tc := b.Cursor()\n\tc.First()\n\tstartKey := []byte(fmt.Sprintf(\"%05d\", 2))\n\treturnedKey, _ := c.Seek(startKey)\n\trequire.Equal(t, startKey, returnedKey)\n\n\t// Step 1: verify next\n\tfor i := 3; i < 1000; i++ {\n\t\texpectedKey := []byte(fmt.Sprintf(\"%05d\", i))\n\t\tactualKey, _ := c.Next()\n\t\trequire.Equal(t, expectedKey, actualKey)\n\t}\n\n\t// Once we've reached the end, it should always return nil no matter how many times we call `Next`.\n\tfor i := 0; i < 10; i++ {\n\t\tk, _ := c.Next()\n\t\trequire.Equal(t, []byte(nil), k)\n\t}\n\n\t// Step 2: verify prev\n\tfor i := 998; i >= 0; i-- {\n\t\texpectedKey := []byte(fmt.Sprintf(\"%05d\", i))\n\t\tactualKey, _ := c.Prev()\n\t\trequire.Equal(t, expectedKey, actualKey)\n\t}\n\n\t// Once we've reached the beginning, it should always return nil no matter how many times we call `Prev`.\n\tfor i := 0; i < 10; i++ {\n\t\tk, _ := c.Prev()\n\t\trequire.Equal(t, []byte(nil), k)\n\t}\n\n\t// Step 3: verify next again\n\tfor i := 1; i < 1000; i++ {\n\t\texpectedKey := []byte(fmt.Sprintf(\"%05d\", i))\n\t\tactualKey, _ := c.Next()\n\t\trequire.Equal(t, expectedKey, actualKey)\n\t}\n}\n\nfunc testRepeatCursorOperations_PrevNextPrev(t *testing.T, b *bolt.Bucket) {\n\tc := b.Cursor()\n\n\tstartKey := []byte(fmt.Sprintf(\"%05d\", 998))\n\treturnedKey, _ := c.Seek(startKey)\n\trequire.Equal(t, startKey, returnedKey)\n\n\t// Step 1: verify prev\n\tfor i := 997; i >= 0; i-- {\n\t\texpectedKey := []byte(fmt.Sprintf(\"%05d\", i))\n\t\tactualKey, _ := c.Prev()\n\t\trequire.Equal(t, expectedKey, actualKey)\n\t}\n\n\t// Once we've reached the beginning, it should always return nil no matter how many times we call `Prev`.\n\tfor i := 0; i < 10; i++ {\n\t\tk, _ := c.Prev()\n\t\trequire.Equal(t, []byte(nil), k)\n\t}\n\n\t// Step 2: verify next\n\tfor i := 1; i < 1000; i++ {\n\t\texpectedKey := []byte(fmt.Sprintf(\"%05d\", i))\n\t\tactualKey, _ := c.Next()\n\t\trequire.Equal(t, expectedKey, actualKey)\n\t}\n\n\t// Once we've reached the end, it should always return nil no matter how many times we call `Next`.\n\tfor i := 0; i < 10; i++ {\n\t\tk, _ := c.Next()\n\t\trequire.Equal(t, []byte(nil), k)\n\t}\n\n\t// Step 3: verify prev again\n\tfor i := 998; i >= 0; i-- {\n\t\texpectedKey := []byte(fmt.Sprintf(\"%05d\", i))\n\t\tactualKey, _ := c.Prev()\n\t\trequire.Equal(t, expectedKey, actualKey)\n\t}\n}\n\n// Ensure that a cursor can return a reference to the bucket that created it.\nfunc TestCursor_Bucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif cb := b.Cursor().Bucket(); !reflect.DeepEqual(cb, b) {\n\t\t\tt.Fatal(\"cursor bucket mismatch\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can seek to the appropriate keys.\nfunc TestCursor_Seek(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"0001\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"0002\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"0003\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := b.CreateBucket([]byte(\"bkt\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\t\t// Exact match should go to the key.\n\t\tif k, v := c.Seek([]byte(\"bar\")); !bytes.Equal(k, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if !bytes.Equal(v, []byte(\"0002\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\t// Inexact match should go to the next key.\n\t\tif k, v := c.Seek([]byte(\"bas\")); !bytes.Equal(k, []byte(\"baz\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if !bytes.Equal(v, []byte(\"0003\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\t// Low key should go to the first key.\n\t\tif k, v := c.Seek([]byte(\"\")); !bytes.Equal(k, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if !bytes.Equal(v, []byte(\"0002\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\t// High key should return no key.\n\t\tif k, v := c.Seek([]byte(\"zzz\")); k != nil {\n\t\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t\t}\n\n\t\t// Buckets should return their key but no value.\n\t\tif k, v := c.Seek([]byte(\"bkt\")); !bytes.Equal(k, []byte(\"bkt\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestCursor_Delete(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tconst count = 1000\n\n\t// Insert every other key between 0 and $count.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < count; i += 1 {\n\t\t\tk := make([]byte, 8)\n\t\t\tbinary.BigEndian.PutUint64(k, uint64(i))\n\t\t\tif err := b.Put(k, make([]byte, 100)); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"sub\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tbound := make([]byte, 8)\n\t\tbinary.BigEndian.PutUint64(bound, uint64(count/2))\n\t\tfor key, _ := c.First(); bytes.Compare(key, bound) < 0; key, _ = c.Next() {\n\t\t\tif err := c.Delete(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tc.Seek([]byte(\"sub\"))\n\t\tif err := c.Delete(); err != errors.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"widgets\")).Stats()\n\t\tif stats.KeyN != count/2+1 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can seek to the appropriate keys when there are a\n// large number of keys. This test also checks that seek will always move\n// forward to the next key.\n//\n// Related: https://github.com/boltdb/bolt/pull/187\nfunc TestCursor_Seek_Large(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar count = 10000\n\n\t// Insert every other key between 0 and $count.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < count; i += 100 {\n\t\t\tfor j := i; j < i+100; j += 2 {\n\t\t\t\tk := make([]byte, 8)\n\t\t\t\tbinary.BigEndian.PutUint64(k, uint64(j))\n\t\t\t\tif err := b.Put(k, make([]byte, 100)); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor i := 0; i < count; i++ {\n\t\t\tseek := make([]byte, 8)\n\t\t\tbinary.BigEndian.PutUint64(seek, uint64(i))\n\n\t\t\tk, _ := c.Seek(seek)\n\n\t\t\t// The last seek is beyond the end of the range so\n\t\t\t// it should return nil.\n\t\t\tif i == count-1 {\n\t\t\t\tif k != nil {\n\t\t\t\t\tt.Fatal(\"expected nil key\")\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t// Otherwise we should seek to the exact key or the next key.\n\t\t\tnum := binary.BigEndian.Uint64(k)\n\t\t\tif i%2 == 0 {\n\t\t\t\tif num != uint64(i) {\n\t\t\t\t\tt.Fatalf(\"unexpected num: %d\", num)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif num != uint64(i+1) {\n\t\t\t\t\tt.Fatalf(\"unexpected num: %d\", num)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a cursor can iterate over an empty bucket without error.\nfunc TestCursor_EmptyBucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tk, v := c.First()\n\t\tif k != nil {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can reverse iterate over an empty bucket without error.\nfunc TestCursor_EmptyBucketReverse(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tk, v := c.Last()\n\t\tif k != nil {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can iterate over a single root with a couple elements.\nfunc TestCursor_Iterate_Leaf(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte{0}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte{1}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdefer func() { _ = tx.Rollback() }()\n\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\tk, v := c.First()\n\tif !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{1}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif !bytes.Equal(k, []byte(\"baz\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{0}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can iterate in reverse over a single root with a couple elements.\nfunc TestCursor_LeafRootReverse(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte{0}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte{1}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\tif k, v := c.Last(); !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{0}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); !bytes.Equal(k, []byte(\"baz\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{1}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can restart from the beginning.\nfunc TestCursor_Restart(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\tif k, _ := c.First(); !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\tif k, _ := c.Next(); !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\n\tif k, _ := c.First(); !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\tif k, _ := c.Next(); !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a cursor can skip over empty pages that have been deleted.\nfunc TestCursor_First_EmptyPages(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Create 1000 keys in the \"widgets\" bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < 1000; i++ {\n\t\t\tif err := b.Put(u64tob(uint64(i)), []byte{}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Delete half the keys and then try to iterate.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 600; i++ {\n\t\t\tif err := b.Delete(u64tob(uint64(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tc := b.Cursor()\n\t\tvar n int\n\t\tfor k, _ := c.First(); k != nil; k, _ = c.Next() {\n\t\t\tn++\n\t\t}\n\t\tif n != 400 {\n\t\t\tt.Fatalf(\"unexpected key count: %d\", n)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a cursor can skip over empty pages that have been deleted.\nfunc TestCursor_Last_EmptyPages(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Create 1000 keys in the \"widgets\" bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < 1000; i++ {\n\t\t\tif err := b.Put(u64tob(uint64(i)), []byte{}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Delete last 800 elements to ensure last page is empty\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 200; i < 1000; i++ {\n\t\t\tif err := b.Delete(u64tob(uint64(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tc := b.Cursor()\n\t\tvar n int\n\t\tfor k, _ := c.Last(); k != nil; k, _ = c.Prev() {\n\t\t\tn++\n\t\t}\n\t\tif n != 200 {\n\t\t\tt.Fatalf(\"unexpected key count: %d\", n)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx can iterate over all elements in a bucket.\nfunc TestCursor_QuickCheck(t *testing.T) {\n\tf := func(items testdata) bool {\n\t\tdb := btesting.MustCreateDB(t)\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\ttx, err := db.Begin(true)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor _, item := range items {\n\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\tif err := tx.Commit(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Sort test data.\n\t\tsort.Sort(items)\n\n\t\t// Iterate over all items and check consistency.\n\t\tvar index = 0\n\t\ttx, err = db.Begin(false)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.First(); k != nil && index < len(items); k, v = c.Next() {\n\t\t\tif !bytes.Equal(k, items[index].Key) {\n\t\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t\t} else if !bytes.Equal(v, items[index].Value) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t\tindex++\n\t\t}\n\t\tif len(items) != index {\n\t\t\tt.Fatalf(\"unexpected item count: %v, expected %v\", len(items), index)\n\t\t}\n\n\t\tif err := tx.Rollback(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}\n\tif err := quick.Check(f, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a transaction can iterate over all elements in a bucket in reverse.\nfunc TestCursor_QuickCheck_Reverse(t *testing.T) {\n\tf := func(items testdata) bool {\n\t\tdb := btesting.MustCreateDB(t)\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\ttx, err := db.Begin(true)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor _, item := range items {\n\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\tif err := tx.Commit(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Sort test data.\n\t\tsort.Sort(revtestdata(items))\n\n\t\t// Iterate over all items and check consistency.\n\t\tvar index = 0\n\t\ttx, err = db.Begin(false)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.Last(); k != nil && index < len(items); k, v = c.Prev() {\n\t\t\tif !bytes.Equal(k, items[index].Key) {\n\t\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t\t} else if !bytes.Equal(v, items[index].Value) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t\tindex++\n\t\t}\n\t\tif len(items) != index {\n\t\t\tt.Fatalf(\"unexpected item count: %v, expected %v\", len(items), index)\n\t\t}\n\n\t\tif err := tx.Rollback(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}\n\tif err := quick.Check(f, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a Tx cursor can iterate over subbuckets.\nfunc TestCursor_QuickCheck_BucketsOnly(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"baz\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvar names []string\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\tnames = append(names, string(k))\n\t\t\tif v != nil {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t}\n\t\tif !reflect.DeepEqual(names, []string{\"bar\", \"baz\", \"foo\"}) {\n\t\t\tt.Fatalf(\"unexpected names: %+v\", names)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can reverse iterate over subbuckets.\nfunc TestCursor_QuickCheck_BucketsOnly_Reverse(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"baz\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvar names []string\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.Last(); k != nil; k, v = c.Prev() {\n\t\t\tnames = append(names, string(k))\n\t\t\tif v != nil {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t}\n\t\tif !reflect.DeepEqual(names, []string{\"foo\", \"baz\", \"bar\"}) {\n\t\t\tt.Fatalf(\"unexpected names: %+v\", names)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc ExampleCursor() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a read-write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a new bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"animals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Insert data into a bucket.\n\t\tif err := b.Put([]byte(\"dog\"), []byte(\"fun\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"cat\"), []byte(\"lame\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"liger\"), []byte(\"awesome\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\n\t\t// Create a cursor for iteration.\n\t\tc := b.Cursor()\n\n\t\t// Iterate over items in sorted key order. This starts from the\n\t\t// first key/value pair and updates the k/v variables to the\n\t\t// next key/value on each iteration.\n\t\t//\n\t\t// The loop finishes at the end of the cursor when a nil key is returned.\n\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\tfmt.Printf(\"A %s is %s.\\n\", k, v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// A cat is lame.\n\t// A dog is fun.\n\t// A liger is awesome.\n}\n\nfunc ExampleCursor_reverse() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a read-write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a new bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"animals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Insert data into a bucket.\n\t\tif err := b.Put([]byte(\"dog\"), []byte(\"fun\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"cat\"), []byte(\"lame\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"liger\"), []byte(\"awesome\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\n\t\t// Create a cursor for iteration.\n\t\tc := b.Cursor()\n\n\t\t// Iterate over items in reverse sorted key order. This starts\n\t\t// from the last key/value pair and updates the k/v variables to\n\t\t// the previous key/value on each iteration.\n\t\t//\n\t\t// The loop finishes at the beginning of the cursor when a nil key\n\t\t// is returned.\n\t\tfor k, v := c.Last(); k != nil; k, v = c.Prev() {\n\t\t\tfmt.Printf(\"A %s is %s.\\n\", k, v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close the database to release the file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// A liger is awesome.\n\t// A dog is fun.\n\t// A cat is lame.\n}\n"
  },
  {
    "path": "db.go",
    "content": "package bbolt\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"runtime\"\n\t\"sync\"\n\t\"time\"\n\t\"unsafe\"\n\n\tberrors \"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/common\"\n\tfl \"go.etcd.io/bbolt/internal/freelist\"\n)\n\n// The time elapsed between consecutive file locking attempts.\nconst flockRetryTimeout = 50 * time.Millisecond\n\n// FreelistType is the type of the freelist backend\ntype FreelistType string\n\n// TODO(ahrtr): eventually we should (step by step)\n//  1. default to `FreelistMapType`;\n//  2. remove the `FreelistArrayType`, do not export `FreelistMapType`\n//     and remove field `FreelistType' from both `DB` and `Options`;\nconst (\n\t// FreelistArrayType indicates backend freelist type is array\n\tFreelistArrayType = FreelistType(\"array\")\n\t// FreelistMapType indicates backend freelist type is hashmap\n\tFreelistMapType = FreelistType(\"hashmap\")\n)\n\n// DB represents a collection of buckets persisted to a file on disk.\n// All data access is performed through transactions which can be obtained through the DB.\n// All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.\ntype DB struct {\n\t// When enabled, the database will perform a Check() after every commit.\n\t// A panic is issued if the database is in an inconsistent state. This\n\t// flag has a large performance impact so it should only be used for\n\t// debugging purposes.\n\tStrictMode bool\n\n\t// Setting the NoSync flag will cause the database to skip fsync()\n\t// calls after each commit. This can be useful when bulk loading data\n\t// into a database and you can restart the bulk load in the event of\n\t// a system failure or database corruption. Do not set this flag for\n\t// normal use.\n\t//\n\t// If the package global IgnoreNoSync constant is true, this value is\n\t// ignored.  See the comment on that constant for more details.\n\t//\n\t// THIS IS UNSAFE. PLEASE USE WITH CAUTION.\n\tNoSync bool\n\n\t// When true, skips syncing freelist to disk. This improves the database\n\t// write performance under normal operation, but requires a full database\n\t// re-sync during recovery.\n\tNoFreelistSync bool\n\n\t// FreelistType sets the backend freelist type. There are two options. Array which is simple but endures\n\t// dramatic performance degradation if database is large and fragmentation in freelist is common.\n\t// The alternative one is using hashmap, it is faster in almost all circumstances\n\t// but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe.\n\t// The default type is array\n\tFreelistType FreelistType\n\n\t// When true, skips the truncate call when growing the database.\n\t// Setting this to true is only safe on non-ext3/ext4 systems.\n\t// Skipping truncation avoids preallocation of hard drive space and\n\t// bypasses a truncate() and fsync() syscall on remapping.\n\t//\n\t// https://github.com/boltdb/bolt/issues/284\n\tNoGrowSync bool\n\n\t// When `true`, bbolt will always load the free pages when opening the DB.\n\t// When opening db in write mode, this flag will always automatically\n\t// set to `true`.\n\tPreLoadFreelist bool\n\n\t// If you want to read the entire database fast, you can set MmapFlag to\n\t// syscall.MAP_POPULATE on Linux 2.6.23+ for sequential read-ahead.\n\tMmapFlags int\n\n\t// MaxBatchSize is the maximum size of a batch. Default value is\n\t// copied from DefaultMaxBatchSize in Open.\n\t//\n\t// If <=0, disables batching.\n\t//\n\t// Do not change concurrently with calls to Batch.\n\tMaxBatchSize int\n\n\t// MaxBatchDelay is the maximum delay before a batch starts.\n\t// Default value is copied from DefaultMaxBatchDelay in Open.\n\t//\n\t// If <=0, effectively disables batching.\n\t//\n\t// Do not change concurrently with calls to Batch.\n\tMaxBatchDelay time.Duration\n\n\t// AllocSize is the amount of space allocated when the database\n\t// needs to create new pages. This is done to amortize the cost\n\t// of truncate() and fsync() when growing the data file.\n\tAllocSize int\n\n\t// MaxSize is the maximum size (in bytes) allowed for the data file.\n\t// If a caller's attempt to add data results in the need to grow\n\t// the data file, an error will be returned and the data file will not grow.\n\t// <=0 means no limit.\n\tMaxSize int\n\n\t// Mlock locks database file in memory when set to true.\n\t// It prevents major page faults, however used memory can't be reclaimed.\n\t//\n\t// Supported only on Unix via mlock/munlock syscalls.\n\tMlock bool\n\n\tlogger Logger\n\n\tpath     string\n\topenFile func(string, int, os.FileMode) (*os.File, error)\n\tfile     *os.File\n\t// `dataref` isn't used at all on Windows, and the golangci-lint\n\t// always fails on Windows platform.\n\t//nolint\n\tdataref  []byte // mmap'ed readonly, write throws SEGV\n\tdata     *[common.MaxMapSize]byte\n\tdatasz   int\n\tmeta0    *common.Meta\n\tmeta1    *common.Meta\n\tpageSize int\n\topened   bool\n\trwtx     *Tx\n\tstats    *Stats\n\n\tfreelist     fl.Interface\n\tfreelistLoad sync.Once\n\n\tpagePool sync.Pool\n\n\tbatchMu sync.Mutex\n\tbatch   *batch\n\n\trwlock   sync.Mutex   // Allows only one writer at a time.\n\tmetalock sync.Mutex   // Protects meta page access.\n\tmmaplock sync.RWMutex // Protects mmap access during remapping.\n\tstatlock sync.RWMutex // Protects stats access.\n\n\tops struct {\n\t\twriteAt func(b []byte, off int64) (n int, err error)\n\t}\n\n\t// Read only mode.\n\t// When true, Update() and Begin(true) return ErrDatabaseReadOnly immediately.\n\treadOnly bool\n}\n\n// Path returns the path to currently open database file.\nfunc (db *DB) Path() string {\n\treturn db.path\n}\n\n// GoString returns the Go string representation of the database.\nfunc (db *DB) GoString() string {\n\treturn fmt.Sprintf(\"bolt.DB{path:%q}\", db.path)\n}\n\n// String returns the string representation of the database.\nfunc (db *DB) String() string {\n\treturn fmt.Sprintf(\"DB<%q>\", db.path)\n}\n\n// Open creates and opens a database at the given path with a given file mode.\n// If the file does not exist then it will be created automatically with a given file mode.\n// Passing in nil options will cause Bolt to open the database with the default options.\n// Note: For read/write transactions, ensure the owner has write permission on the created/opened database file, e.g. 0600\nfunc Open(path string, mode os.FileMode, options *Options) (db *DB, err error) {\n\tdb = &DB{\n\t\topened: true,\n\t}\n\n\t// Set default options if no options are provided.\n\tif options == nil {\n\t\toptions = DefaultOptions\n\t}\n\tdb.NoSync = options.NoSync\n\tdb.NoGrowSync = options.NoGrowSync\n\tdb.MmapFlags = options.MmapFlags\n\tdb.NoFreelistSync = options.NoFreelistSync\n\tdb.PreLoadFreelist = options.PreLoadFreelist\n\tdb.FreelistType = options.FreelistType\n\tdb.Mlock = options.Mlock\n\tdb.MaxSize = options.MaxSize\n\n\t// Set default values for later DB operations.\n\tdb.MaxBatchSize = common.DefaultMaxBatchSize\n\tdb.MaxBatchDelay = common.DefaultMaxBatchDelay\n\tdb.AllocSize = common.DefaultAllocSize\n\n\tif !options.NoStatistics {\n\t\tdb.stats = new(Stats)\n\t}\n\n\tif options.Logger == nil {\n\t\tdb.logger = getDiscardLogger()\n\t} else {\n\t\tdb.logger = options.Logger\n\t}\n\n\tlg := db.Logger()\n\tif lg != discardLogger {\n\t\tlg.Infof(\"Opening db file (%s) with mode %s and with options: %s\", path, mode, options)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Opening bbolt db (%s) failed: %v\", path, err)\n\t\t\t} else {\n\t\t\t\tlg.Infof(\"Opening bbolt db (%s) successfully\", path)\n\t\t\t}\n\t\t}()\n\t}\n\n\tflag := os.O_RDWR\n\tif options.ReadOnly {\n\t\tflag = os.O_RDONLY\n\t\tdb.readOnly = true\n\t} else {\n\t\t// always load free pages in write mode\n\t\tdb.PreLoadFreelist = true\n\t\tflag |= os.O_CREATE\n\t}\n\n\tdb.openFile = options.OpenFile\n\tif db.openFile == nil {\n\t\tdb.openFile = os.OpenFile\n\t}\n\n\t// Open data file and separate sync handler for metadata writes.\n\tif db.file, err = db.openFile(path, flag, mode); err != nil {\n\t\t_ = db.close()\n\t\tlg.Errorf(\"failed to open db file (%s): %v\", path, err)\n\t\treturn nil, err\n\t}\n\tdb.path = db.file.Name()\n\n\t// Lock file so that other processes using Bolt in read-write mode cannot\n\t// use the database  at the same time. This would cause corruption since\n\t// the two processes would write meta pages and free pages separately.\n\t// The database file is locked exclusively (only one process can grab the lock)\n\t// if !options.ReadOnly.\n\t// The database file is locked using the shared lock (more than one process may\n\t// hold a lock at the same time) otherwise (options.ReadOnly is set).\n\tif err = flock(db, !db.readOnly, options.Timeout); err != nil {\n\t\t_ = db.close()\n\t\tlg.Errorf(\"failed to lock db file (%s), readonly: %t, error: %v\", path, db.readOnly, err)\n\t\treturn nil, err\n\t}\n\n\t// Default values for test hooks\n\tdb.ops.writeAt = db.file.WriteAt\n\n\tif db.pageSize = options.PageSize; db.pageSize == 0 {\n\t\t// Set the default page size to the OS page size.\n\t\tdb.pageSize = common.DefaultPageSize\n\t}\n\n\t// Initialize the database if it doesn't exist.\n\tif info, statErr := db.file.Stat(); statErr != nil {\n\t\t_ = db.close()\n\t\tlg.Errorf(\"failed to get db file's stats (%s): %v\", path, err)\n\t\treturn nil, statErr\n\t} else if info.Size() == 0 {\n\t\t// Initialize new files with meta pages.\n\t\tif err = db.init(); err != nil {\n\t\t\t// clean up file descriptor on initialization fail\n\t\t\t_ = db.close()\n\t\t\tlg.Errorf(\"failed to initialize db file (%s): %v\", path, err)\n\t\t\treturn nil, err\n\t\t}\n\t} else {\n\t\t// try to get the page size from the metadata pages\n\t\tif db.pageSize, err = db.getPageSize(); err != nil {\n\t\t\t_ = db.close()\n\t\t\tlg.Errorf(\"failed to get page size from db file (%s): %v\", path, err)\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\t// Initialize page pool.\n\tdb.pagePool = sync.Pool{\n\t\tNew: func() interface{} {\n\t\t\treturn make([]byte, db.pageSize)\n\t\t},\n\t}\n\n\t// Memory map the data file.\n\tif err = db.mmap(options.InitialMmapSize); err != nil {\n\t\t_ = db.close()\n\t\tlg.Errorf(\"failed to map db file (%s): %v\", path, err)\n\t\treturn nil, err\n\t}\n\n\tif db.PreLoadFreelist {\n\t\tdb.loadFreelist()\n\t}\n\n\tif db.readOnly {\n\t\treturn db, nil\n\t}\n\n\t// Flush freelist when transitioning from no sync to sync so\n\t// NoFreelistSync unaware boltdb can open the db later.\n\tif !db.NoFreelistSync && !db.hasSyncedFreelist() {\n\t\ttx, txErr := db.Begin(true)\n\t\tif tx != nil {\n\t\t\ttxErr = tx.Commit()\n\t\t}\n\t\tif txErr != nil {\n\t\t\tlg.Errorf(\"starting readwrite transaction failed: %v\", txErr)\n\t\t\t_ = db.close()\n\t\t\treturn nil, txErr\n\t\t}\n\t}\n\n\t// Mark the database as opened and return.\n\treturn db, nil\n}\n\n// getPageSize reads the pageSize from the meta pages. It tries\n// to read the first meta page firstly. If the first page is invalid,\n// then it tries to read the second page using the default page size.\nfunc (db *DB) getPageSize() (int, error) {\n\tvar (\n\t\tmeta0CanRead, meta1CanRead bool\n\t)\n\n\t// Read the first meta page to determine the page size.\n\tif pgSize, canRead, err := db.getPageSizeFromFirstMeta(); err != nil {\n\t\t// We cannot read the page size from page 0, but can read page 0.\n\t\tmeta0CanRead = canRead\n\t} else {\n\t\treturn pgSize, nil\n\t}\n\n\t// Read the second meta page to determine the page size.\n\tif pgSize, canRead, err := db.getPageSizeFromSecondMeta(); err != nil {\n\t\t// We cannot read the page size from page 1, but can read page 1.\n\t\tmeta1CanRead = canRead\n\t} else {\n\t\treturn pgSize, nil\n\t}\n\n\t// If we can't read the page size from both pages, but can read\n\t// either page, then we assume it's the same as the OS or the one\n\t// given, since that's how the page size was chosen in the first place.\n\t//\n\t// If both pages are invalid, and (this OS uses a different page size\n\t// from what the database was created with or the given page size is\n\t// different from what the database was created with), then we are out\n\t// of luck and cannot access the database.\n\tif meta0CanRead || meta1CanRead {\n\t\treturn db.pageSize, nil\n\t}\n\n\treturn 0, berrors.ErrInvalid\n}\n\n// getPageSizeFromFirstMeta reads the pageSize from the first meta page\nfunc (db *DB) getPageSizeFromFirstMeta() (int, bool, error) {\n\tvar buf [0x1000]byte\n\tvar metaCanRead bool\n\tif bw, err := db.file.ReadAt(buf[:], 0); err == nil && bw == len(buf) {\n\t\tmetaCanRead = true\n\t\tif m := db.pageInBuffer(buf[:], 0).Meta(); m.Validate() == nil {\n\t\t\treturn int(m.PageSize()), metaCanRead, nil\n\t\t}\n\t}\n\treturn 0, metaCanRead, berrors.ErrInvalid\n}\n\n// getPageSizeFromSecondMeta reads the pageSize from the second meta page\nfunc (db *DB) getPageSizeFromSecondMeta() (int, bool, error) {\n\tvar (\n\t\tfileSize    int64\n\t\tmetaCanRead bool\n\t)\n\n\t// get the db file size\n\tif info, err := db.file.Stat(); err != nil {\n\t\treturn 0, metaCanRead, err\n\t} else {\n\t\tfileSize = info.Size()\n\t}\n\n\t// We need to read the second meta page, so we should skip the first page;\n\t// but we don't know the exact page size yet, it's chicken & egg problem.\n\t// The solution is to try all the possible page sizes, which starts from 1KB\n\t// and until 16MB (1024<<14) or the end of the db file\n\t//\n\t// TODO: should we support larger page size?\n\tfor i := 0; i <= 14; i++ {\n\t\tvar buf [0x1000]byte\n\t\tvar pos int64 = 1024 << uint(i)\n\t\tif pos >= fileSize-1024 {\n\t\t\tbreak\n\t\t}\n\t\tbw, err := db.file.ReadAt(buf[:], pos)\n\t\tif (err == nil && bw == len(buf)) || (err == io.EOF && int64(bw) == (fileSize-pos)) {\n\t\t\tmetaCanRead = true\n\t\t\tif m := db.pageInBuffer(buf[:], 0).Meta(); m.Validate() == nil {\n\t\t\t\treturn int(m.PageSize()), metaCanRead, nil\n\t\t\t}\n\t\t}\n\t}\n\n\treturn 0, metaCanRead, berrors.ErrInvalid\n}\n\n// loadFreelist reads the freelist if it is synced, or reconstructs it\n// by scanning the DB if it is not synced. It assumes there are no\n// concurrent accesses being made to the freelist.\nfunc (db *DB) loadFreelist() {\n\tdb.freelistLoad.Do(func() {\n\t\tdb.freelist = newFreelist(db.FreelistType)\n\t\tif !db.hasSyncedFreelist() {\n\t\t\t// Reconstruct free list by scanning the DB.\n\t\t\tdb.freelist.Init(db.freepages())\n\t\t} else {\n\t\t\t// Read free list from freelist page.\n\t\t\tdb.freelist.Read(db.page(db.meta().Freelist()))\n\t\t}\n\t\tif db.stats != nil {\n\t\t\tdb.stats.FreePageN = db.freelist.FreeCount()\n\t\t}\n\t})\n}\n\nfunc (db *DB) hasSyncedFreelist() bool {\n\treturn db.meta().Freelist() != common.PgidNoFreelist\n}\n\nfunc (db *DB) fileSize() (int, error) {\n\tinfo, err := db.file.Stat()\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"file stat error: %w\", err)\n\t}\n\tsz := int(info.Size())\n\tif sz < db.pageSize*2 {\n\t\treturn 0, fmt.Errorf(\"file size too small %d\", sz)\n\t}\n\treturn sz, nil\n}\n\n// mmap opens the underlying memory-mapped file and initializes the meta references.\n// minsz is the minimum size that the new mmap can be.\nfunc (db *DB) mmap(minsz int) (err error) {\n\tdb.mmaplock.Lock()\n\tdefer db.mmaplock.Unlock()\n\n\tlg := db.Logger()\n\n\t// Ensure the size is at least the minimum size.\n\tvar fileSize int\n\tfileSize, err = db.fileSize()\n\tif err != nil {\n\t\tlg.Errorf(\"getting file size failed: %w\", err)\n\t\treturn err\n\t}\n\tvar size = fileSize\n\tif size < minsz {\n\t\tsize = minsz\n\t}\n\tsize, err = db.mmapSize(size)\n\tif err != nil {\n\t\tlg.Errorf(\"getting map size failed: %w\", err)\n\t\treturn err\n\t}\n\n\tif db.Mlock {\n\t\t// Unlock db memory\n\t\tif err := db.munlock(fileSize); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Dereference all mmap references before unmapping.\n\tif db.rwtx != nil {\n\t\tdb.rwtx.root.dereference()\n\t}\n\n\t// Unmap existing data before continuing.\n\tif err = db.munmap(); err != nil {\n\t\treturn err\n\t}\n\n\t// Memory-map the data file as a byte slice.\n\t// gofail: var mapError string\n\t// return errors.New(mapError)\n\tif err = mmap(db, size); err != nil {\n\t\tlg.Errorf(\"[GOOS: %s, GOARCH: %s] mmap failed, size: %d, error: %v\", runtime.GOOS, runtime.GOARCH, size, err)\n\t\treturn err\n\t}\n\n\t// Perform unmmap on any error to reset all data fields:\n\t// dataref, data, datasz, meta0 and meta1.\n\tdefer func() {\n\t\tif err != nil {\n\t\t\tif unmapErr := db.munmap(); unmapErr != nil {\n\t\t\t\terr = fmt.Errorf(\"%w; rollback unmap also failed: %v\", err, unmapErr)\n\t\t\t}\n\t\t}\n\t}()\n\n\tif db.Mlock {\n\t\t// Don't allow swapping of data file\n\t\tif err := db.mlock(fileSize); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Save references to the meta pages.\n\tdb.meta0 = db.page(0).Meta()\n\tdb.meta1 = db.page(1).Meta()\n\n\t// Validate the meta pages. We only return an error if both meta pages fail\n\t// validation, since meta0 failing validation means that it wasn't saved\n\t// properly -- but we can recover using meta1. And vice-versa.\n\terr0 := db.meta0.Validate()\n\terr1 := db.meta1.Validate()\n\tif err0 != nil && err1 != nil {\n\t\tlg.Errorf(\"both meta pages are invalid, meta0: %v, meta1: %v\", err0, err1)\n\t\treturn err0\n\t}\n\n\treturn nil\n}\n\nfunc (db *DB) invalidate() {\n\tdb.dataref = nil\n\tdb.data = nil\n\tdb.datasz = 0\n\n\tdb.meta0 = nil\n\tdb.meta1 = nil\n}\n\n// munmap unmaps the data file from memory.\nfunc (db *DB) munmap() error {\n\tdefer db.invalidate()\n\n\t// gofail: var unmapError string\n\t// return errors.New(unmapError)\n\tif err := munmap(db); err != nil {\n\t\tdb.Logger().Errorf(\"[GOOS: %s, GOARCH: %s] munmap failed, db.datasz: %d, error: %v\", runtime.GOOS, runtime.GOARCH, db.datasz, err)\n\t\treturn fmt.Errorf(\"unmap error: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// mmapSize determines the appropriate size for the mmap given the current size\n// of the database. The minimum size is 32KB and doubles until it reaches 1GB.\n// Returns an error if the new mmap size is greater than the max allowed.\nfunc (db *DB) mmapSize(size int) (int, error) {\n\t// Double the size from 32KB until 1GB.\n\tfor i := uint(15); i <= 30; i++ {\n\t\tif size <= 1<<i {\n\t\t\treturn 1 << i, nil\n\t\t}\n\t}\n\n\t// Verify the requested size is not above the maximum allowed.\n\tif size > common.MaxMapSize {\n\t\treturn 0, errors.New(\"mmap too large\")\n\t}\n\n\t// If larger than 1GB then grow by 1GB at a time.\n\tsz := int64(size)\n\tif remainder := sz % int64(common.MaxMmapStep); remainder > 0 {\n\t\tsz += int64(common.MaxMmapStep) - remainder\n\t}\n\n\t// Ensure that the mmap size is a multiple of the page size.\n\t// This should always be true since we're incrementing in MBs.\n\tpageSize := int64(db.pageSize)\n\tif (sz % pageSize) != 0 {\n\t\tsz = ((sz / pageSize) + 1) * pageSize\n\t}\n\n\t// If we've exceeded the max size then only grow up to the max size.\n\tif sz > common.MaxMapSize {\n\t\tsz = common.MaxMapSize\n\t}\n\n\treturn int(sz), nil\n}\n\nfunc (db *DB) munlock(fileSize int) error {\n\t// gofail: var munlockError string\n\t// return errors.New(munlockError)\n\tif err := munlock(db, fileSize); err != nil {\n\t\tdb.Logger().Errorf(\"[GOOS: %s, GOARCH: %s] munlock failed, fileSize: %d, db.datasz: %d, error: %v\", runtime.GOOS, runtime.GOARCH, fileSize, db.datasz, err)\n\t\treturn fmt.Errorf(\"munlock error: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (db *DB) mlock(fileSize int) error {\n\t// gofail: var mlockError string\n\t// return errors.New(mlockError)\n\tif err := mlock(db, fileSize); err != nil {\n\t\tdb.Logger().Errorf(\"[GOOS: %s, GOARCH: %s] mlock failed, fileSize: %d, db.datasz: %d, error: %v\", runtime.GOOS, runtime.GOARCH, fileSize, db.datasz, err)\n\t\treturn fmt.Errorf(\"mlock error: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (db *DB) mrelock(fileSizeFrom, fileSizeTo int) error {\n\tif err := db.munlock(fileSizeFrom); err != nil {\n\t\treturn err\n\t}\n\tif err := db.mlock(fileSizeTo); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n// init creates a new database file and initializes its meta pages.\nfunc (db *DB) init() error {\n\t// Create two meta pages on a buffer.\n\tbuf := make([]byte, db.pageSize*4)\n\tfor i := 0; i < 2; i++ {\n\t\tp := db.pageInBuffer(buf, common.Pgid(i))\n\t\tp.SetId(common.Pgid(i))\n\t\tp.SetFlags(common.MetaPageFlag)\n\n\t\t// Initialize the meta page.\n\t\tm := p.Meta()\n\t\tm.SetMagic(common.Magic)\n\t\tm.SetVersion(common.Version)\n\t\tm.SetPageSize(uint32(db.pageSize))\n\t\tm.SetFreelist(2)\n\t\tm.SetRootBucket(common.NewInBucket(3, 0))\n\t\tm.SetPgid(4)\n\t\tm.SetTxid(common.Txid(i))\n\t\tm.SetChecksum(m.Sum64())\n\t}\n\n\t// Write an empty freelist at page 3.\n\tp := db.pageInBuffer(buf, common.Pgid(2))\n\tp.SetId(2)\n\tp.SetFlags(common.FreelistPageFlag)\n\tp.SetCount(0)\n\n\t// Write an empty leaf page at page 4.\n\tp = db.pageInBuffer(buf, common.Pgid(3))\n\tp.SetId(3)\n\tp.SetFlags(common.LeafPageFlag)\n\tp.SetCount(0)\n\n\t// Write the buffer to our data file.\n\tif _, err := db.ops.writeAt(buf, 0); err != nil {\n\t\tdb.Logger().Errorf(\"writeAt failed: %w\", err)\n\t\treturn err\n\t}\n\tif err := fdatasync(db); err != nil {\n\t\tdb.Logger().Errorf(\"[GOOS: %s, GOARCH: %s] fdatasync failed: %w\", runtime.GOOS, runtime.GOARCH, err)\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// Close releases all database resources.\n// It will block waiting for any open transactions to finish\n// before closing the database and returning.\nfunc (db *DB) Close() error {\n\tdb.rwlock.Lock()\n\tdefer db.rwlock.Unlock()\n\n\tdb.metalock.Lock()\n\tdefer db.metalock.Unlock()\n\n\tdb.mmaplock.Lock()\n\tdefer db.mmaplock.Unlock()\n\n\treturn db.close()\n}\n\nfunc (db *DB) close() error {\n\tif !db.opened {\n\t\treturn nil\n\t}\n\n\tdb.opened = false\n\n\tdb.freelist = nil\n\n\t// Clear ops.\n\tdb.ops.writeAt = nil\n\n\tvar errs []error\n\t// Close the mmap.\n\tif err := db.munmap(); err != nil {\n\t\terrs = append(errs, err)\n\t}\n\n\t// Close file handles.\n\tif db.file != nil {\n\t\t// No need to unlock read-only file.\n\t\tif !db.readOnly {\n\t\t\t// Unlock the file.\n\t\t\tif err := funlock(db); err != nil {\n\t\t\t\terrs = append(errs, fmt.Errorf(\"bolt.Close(): funlock error: %w\", err))\n\t\t\t}\n\t\t}\n\n\t\t// Close the file descriptor.\n\t\tif err := db.file.Close(); err != nil {\n\t\t\terrs = append(errs, fmt.Errorf(\"db file close: %w\", err))\n\t\t}\n\t\tdb.file = nil\n\t}\n\n\tdb.path = \"\"\n\n\tif len(errs) > 0 {\n\t\treturn errs[0]\n\t}\n\treturn nil\n}\n\n// Begin starts a new transaction.\n// Multiple read-only transactions can be used concurrently but only one\n// write transaction can be used at a time. Starting multiple write transactions\n// will cause the calls to block and be serialized until the current write\n// transaction finishes.\n//\n// Transactions should not be dependent on one another. Opening a read\n// transaction and a write transaction in the same goroutine can cause the\n// writer to deadlock because the database periodically needs to re-mmap itself\n// as it grows and it cannot do that while a read transaction is open.\n//\n// If a long running read transaction (for example, a snapshot transaction) is\n// needed, you might want to set DB.InitialMmapSize to a large enough value\n// to avoid potential blocking of write transaction.\n//\n// IMPORTANT: You must close read-only transactions after you are finished or\n// else the database will not reclaim old pages.\nfunc (db *DB) Begin(writable bool) (t *Tx, err error) {\n\tif lg := db.Logger(); lg != discardLogger {\n\t\tlg.Debugf(\"Starting a new transaction [writable: %t]\", writable)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Starting a new transaction [writable: %t] failed: %v\", writable, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Starting a new transaction [writable: %t] successfully\", writable)\n\t\t\t}\n\t\t}()\n\t}\n\n\tif writable {\n\t\treturn db.beginRWTx()\n\t}\n\treturn db.beginTx()\n}\n\nfunc (db *DB) Logger() Logger {\n\tif db == nil || db.logger == nil {\n\t\treturn getDiscardLogger()\n\t}\n\treturn db.logger\n}\n\nfunc (db *DB) beginTx() (*Tx, error) {\n\t// Lock the meta pages while we initialize the transaction. We obtain\n\t// the meta lock before the mmap lock because that's the order that the\n\t// write transaction will obtain them.\n\tdb.metalock.Lock()\n\n\t// Obtain a read-only lock on the mmap. When the mmap is remapped it will\n\t// obtain a write lock so all transactions must finish before it can be\n\t// remapped.\n\tdb.mmaplock.RLock()\n\n\t// Exit if the database is not open yet.\n\tif !db.opened {\n\t\tdb.mmaplock.RUnlock()\n\t\tdb.metalock.Unlock()\n\t\treturn nil, berrors.ErrDatabaseNotOpen\n\t}\n\n\t// Exit if the database is not correctly mapped.\n\tif db.data == nil {\n\t\tdb.mmaplock.RUnlock()\n\t\tdb.metalock.Unlock()\n\t\treturn nil, berrors.ErrInvalidMapping\n\t}\n\n\t// Create a transaction associated with the database.\n\tt := &Tx{}\n\tt.init(db)\n\n\tif db.freelist != nil {\n\t\tdb.freelist.AddReadonlyTXID(t.meta.Txid())\n\t}\n\n\t// Unlock the meta pages.\n\tdb.metalock.Unlock()\n\n\t// Update the transaction stats.\n\tif db.stats != nil {\n\t\tdb.statlock.Lock()\n\t\tdb.stats.TxN++\n\t\tdb.stats.OpenTxN++\n\t\tdb.statlock.Unlock()\n\t}\n\n\treturn t, nil\n}\n\nfunc (db *DB) beginRWTx() (*Tx, error) {\n\t// If the database was opened with Options.ReadOnly, return an error.\n\tif db.readOnly {\n\t\treturn nil, berrors.ErrDatabaseReadOnly\n\t}\n\n\t// Obtain writer lock. This is released by the transaction when it closes.\n\t// This enforces only one writer transaction at a time.\n\tdb.rwlock.Lock()\n\n\t// Once we have the writer lock then we can lock the meta pages so that\n\t// we can set up the transaction.\n\tdb.metalock.Lock()\n\tdefer db.metalock.Unlock()\n\n\t// Exit if the database is not open yet.\n\tif !db.opened {\n\t\tdb.rwlock.Unlock()\n\t\treturn nil, berrors.ErrDatabaseNotOpen\n\t}\n\n\t// Exit if the database is not correctly mapped.\n\tif db.data == nil {\n\t\tdb.rwlock.Unlock()\n\t\treturn nil, berrors.ErrInvalidMapping\n\t}\n\n\t// Create a transaction associated with the database.\n\tt := &Tx{writable: true}\n\tt.init(db)\n\tdb.rwtx = t\n\tdb.freelist.ReleasePendingPages()\n\treturn t, nil\n}\n\n// removeTx removes a transaction from the database.\nfunc (db *DB) removeTx(tx *Tx) {\n\t// Release the read lock on the mmap.\n\tdb.mmaplock.RUnlock()\n\n\t// Use the meta lock to restrict access to the DB object.\n\tdb.metalock.Lock()\n\n\tif db.freelist != nil {\n\t\tdb.freelist.RemoveReadonlyTXID(tx.meta.Txid())\n\t}\n\n\t// Unlock the meta pages.\n\tdb.metalock.Unlock()\n\n\t// Merge statistics.\n\tif db.stats != nil {\n\t\tdb.statlock.Lock()\n\t\tdb.stats.OpenTxN--\n\t\tdb.stats.TxStats.add(&tx.stats)\n\t\tdb.statlock.Unlock()\n\t}\n}\n\n// Update executes a function within the context of a read-write managed transaction.\n// If no error is returned from the function then the transaction is committed.\n// If an error is returned then the entire transaction is rolled back.\n// Any error that is returned from the function or returned from the commit is\n// returned from the Update() method.\n//\n// Attempting to manually commit or rollback within the function will cause a panic.\nfunc (db *DB) Update(fn func(*Tx) error) error {\n\tt, err := db.Begin(true)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Make sure the transaction rolls back in the event of a panic.\n\tdefer func() {\n\t\tif t.db != nil {\n\t\t\tt.rollback()\n\t\t}\n\t}()\n\n\t// Mark as a managed tx so that the inner function cannot manually commit.\n\tt.managed = true\n\n\t// If an error is returned from the function then rollback and return error.\n\terr = fn(t)\n\tt.managed = false\n\tif err != nil {\n\t\t_ = t.Rollback()\n\t\treturn err\n\t}\n\n\treturn t.Commit()\n}\n\n// View executes a function within the context of a managed read-only transaction.\n// Any error that is returned from the function is returned from the View() method.\n//\n// Attempting to manually rollback within the function will cause a panic.\nfunc (db *DB) View(fn func(*Tx) error) error {\n\tt, err := db.Begin(false)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Make sure the transaction rolls back in the event of a panic.\n\tdefer func() {\n\t\tif t.db != nil {\n\t\t\tt.rollback()\n\t\t}\n\t}()\n\n\t// Mark as a managed tx so that the inner function cannot manually rollback.\n\tt.managed = true\n\n\t// If an error is returned from the function then pass it through.\n\terr = fn(t)\n\tt.managed = false\n\tif err != nil {\n\t\t_ = t.Rollback()\n\t\treturn err\n\t}\n\n\treturn t.Rollback()\n}\n\n// Batch calls fn as part of a batch. It behaves similar to Update,\n// except:\n//\n// 1. concurrent Batch calls can be combined into a single Bolt\n// transaction.\n//\n// 2. the function passed to Batch may be called multiple times,\n// regardless of whether it returns error or not.\n//\n// This means that Batch function side effects must be idempotent and\n// take permanent effect only after a successful return is seen in\n// caller.\n//\n// The maximum batch size and delay can be adjusted with DB.MaxBatchSize\n// and DB.MaxBatchDelay, respectively.\n//\n// Batch is only useful when there are multiple goroutines calling it.\nfunc (db *DB) Batch(fn func(*Tx) error) error {\n\terrCh := make(chan error, 1)\n\n\tdb.batchMu.Lock()\n\tif (db.batch == nil) || (db.batch != nil && len(db.batch.calls) >= db.MaxBatchSize) {\n\t\t// There is no existing batch, or the existing batch is full; start a new one.\n\t\tdb.batch = &batch{\n\t\t\tdb: db,\n\t\t}\n\t\tdb.batch.timer = time.AfterFunc(db.MaxBatchDelay, db.batch.trigger)\n\t}\n\tdb.batch.calls = append(db.batch.calls, call{fn: fn, err: errCh})\n\tif len(db.batch.calls) >= db.MaxBatchSize {\n\t\t// wake up batch, it's ready to run\n\t\tgo db.batch.trigger()\n\t}\n\tdb.batchMu.Unlock()\n\n\terr := <-errCh\n\tif err == trySolo {\n\t\terr = db.Update(fn)\n\t}\n\treturn err\n}\n\ntype call struct {\n\tfn  func(*Tx) error\n\terr chan<- error\n}\n\ntype batch struct {\n\tdb    *DB\n\ttimer *time.Timer\n\tstart sync.Once\n\tcalls []call\n}\n\n// trigger runs the batch if it hasn't already been run.\nfunc (b *batch) trigger() {\n\tb.start.Do(b.run)\n}\n\n// run performs the transactions in the batch and communicates results\n// back to DB.Batch.\nfunc (b *batch) run() {\n\tb.db.batchMu.Lock()\n\tb.timer.Stop()\n\t// Make sure no new work is added to this batch, but don't break\n\t// other batches.\n\tif b.db.batch == b {\n\t\tb.db.batch = nil\n\t}\n\tb.db.batchMu.Unlock()\n\nretry:\n\tfor len(b.calls) > 0 {\n\t\tvar failIdx = -1\n\t\terr := b.db.Update(func(tx *Tx) error {\n\t\t\tfor i, c := range b.calls {\n\t\t\t\tif err := safelyCall(c.fn, tx); err != nil {\n\t\t\t\t\tfailIdx = i\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t})\n\n\t\tif failIdx >= 0 {\n\t\t\t// take the failing transaction out of the batch. it's\n\t\t\t// safe to shorten b.calls here because db.batch no longer\n\t\t\t// points to us, and we hold the mutex anyway.\n\t\t\tc := b.calls[failIdx]\n\t\t\tb.calls[failIdx], b.calls = b.calls[len(b.calls)-1], b.calls[:len(b.calls)-1]\n\t\t\t// tell the submitter re-run it solo, continue with the rest of the batch\n\t\t\tc.err <- trySolo\n\t\t\tcontinue retry\n\t\t}\n\n\t\t// pass success, or bolt internal errors, to all callers\n\t\tfor _, c := range b.calls {\n\t\t\tc.err <- err\n\t\t}\n\t\tbreak retry\n\t}\n}\n\n// trySolo is a special sentinel error value used for signaling that a\n// transaction function should be re-run. It should never be seen by\n// callers.\nvar trySolo = errors.New(\"batch function returned an error and should be re-run solo\")\n\ntype panicked struct {\n\treason interface{}\n}\n\nfunc (p panicked) Error() string {\n\tif err, ok := p.reason.(error); ok {\n\t\treturn err.Error()\n\t}\n\treturn fmt.Sprintf(\"panic: %v\", p.reason)\n}\n\nfunc safelyCall(fn func(*Tx) error, tx *Tx) (err error) {\n\tdefer func() {\n\t\tif p := recover(); p != nil {\n\t\t\terr = panicked{p}\n\t\t}\n\t}()\n\treturn fn(tx)\n}\n\n// Sync executes fdatasync() against the database file handle.\n//\n// This is not necessary under normal operation, however, if you use NoSync\n// then it allows you to force the database file to sync against the disk.\nfunc (db *DB) Sync() (err error) {\n\tif lg := db.Logger(); lg != discardLogger {\n\t\tlg.Debugf(\"Syncing bbolt db (%s)\", db.path)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"[GOOS: %s, GOARCH: %s] syncing bbolt db (%s) failed: %v\", runtime.GOOS, runtime.GOARCH, db.path, err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Syncing bbolt db (%s) successfully\", db.path)\n\t\t\t}\n\t\t}()\n\t}\n\n\treturn fdatasync(db)\n}\n\n// Stats retrieves ongoing performance stats for the database.\n// This is only updated when a transaction closes.\nfunc (db *DB) Stats() Stats {\n\tvar s Stats\n\tif db.stats != nil {\n\t\tdb.statlock.RLock()\n\t\ts = *db.stats\n\t\tdb.statlock.RUnlock()\n\t}\n\treturn s\n}\n\n// This is for internal access to the raw data bytes from the C cursor, use\n// carefully, or not at all.\nfunc (db *DB) Info() *Info {\n\tcommon.Assert(db.data != nil, \"database file isn't correctly mapped\")\n\treturn &Info{uintptr(unsafe.Pointer(&db.data[0])), db.pageSize}\n}\n\n// page retrieves a page reference from the mmap based on the current page size.\nfunc (db *DB) page(id common.Pgid) *common.Page {\n\tpos := id * common.Pgid(db.pageSize)\n\treturn (*common.Page)(unsafe.Pointer(&db.data[pos]))\n}\n\n// pageInBuffer retrieves a page reference from a given byte array based on the current page size.\nfunc (db *DB) pageInBuffer(b []byte, id common.Pgid) *common.Page {\n\treturn (*common.Page)(unsafe.Pointer(&b[id*common.Pgid(db.pageSize)]))\n}\n\n// meta retrieves the current meta page reference.\nfunc (db *DB) meta() *common.Meta {\n\t// We have to return the meta with the highest txid which doesn't fail\n\t// validation. Otherwise, we can cause errors when in fact the database is\n\t// in a consistent state. metaA is the one with the higher txid.\n\tmetaA := db.meta0\n\tmetaB := db.meta1\n\tif db.meta1.Txid() > db.meta0.Txid() {\n\t\tmetaA = db.meta1\n\t\tmetaB = db.meta0\n\t}\n\n\t// Use higher meta page if valid. Otherwise, fallback to previous, if valid.\n\tif err := metaA.Validate(); err == nil {\n\t\treturn metaA\n\t} else if err := metaB.Validate(); err == nil {\n\t\treturn metaB\n\t}\n\n\t// This should never be reached, because both meta1 and meta0 were validated\n\t// on mmap() and we do fsync() on every write.\n\tpanic(\"bolt.DB.meta(): invalid meta pages\")\n}\n\n// allocate returns a contiguous block of memory starting at a given page.\nfunc (db *DB) allocate(txid common.Txid, count int) (*common.Page, error) {\n\t// Allocate a temporary buffer for the page.\n\tvar buf []byte\n\tif count == 1 {\n\t\tbuf = db.pagePool.Get().([]byte)\n\t} else {\n\t\tbuf = make([]byte, count*db.pageSize)\n\t}\n\tp := (*common.Page)(unsafe.Pointer(&buf[0]))\n\tp.SetOverflow(uint32(count - 1))\n\n\t// Use pages from the freelist if they are available.\n\tp.SetId(db.freelist.Allocate(txid, count))\n\tif p.Id() != 0 {\n\t\treturn p, nil\n\t}\n\n\t// Resize mmap() if we're at the end.\n\tp.SetId(db.rwtx.meta.Pgid())\n\tvar minsz = int((p.Id()+common.Pgid(count))+1) * db.pageSize\n\tif minsz >= db.datasz {\n\t\tif err := db.mmap(minsz); err != nil {\n\t\t\tif err == berrors.ErrMaxSizeReached {\n\t\t\t\treturn nil, err\n\t\t\t} else {\n\t\t\t\treturn nil, fmt.Errorf(\"mmap allocate error: %s\", err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Move the page id high water mark.\n\tcurPgid := db.rwtx.meta.Pgid()\n\tdb.rwtx.meta.SetPgid(curPgid + common.Pgid(count))\n\n\treturn p, nil\n}\n\n// grow grows the size of the database to the given sz.\nfunc (db *DB) grow(sz int) error {\n\t// Ignore if the new size is less than available file size.\n\tlg := db.Logger()\n\tfileSize, err := db.fileSize()\n\tif err != nil {\n\t\tlg.Errorf(\"getting file size failed: %w\", err)\n\t\treturn err\n\t}\n\tif sz <= fileSize {\n\t\treturn nil\n\t}\n\n\t// If the data is smaller than the alloc size then only allocate what's needed.\n\t// Once it goes over the allocation size then allocate in chunks.\n\tif db.datasz <= db.AllocSize {\n\t\tsz = db.datasz\n\t} else {\n\t\tsz += db.AllocSize\n\t}\n\n\tif !db.readOnly && db.MaxSize > 0 && sz > db.MaxSize {\n\t\tlg.Errorf(\"[GOOS: %s, GOARCH: %s] maximum db size reached, size: %d, db.MaxSize: %d\", runtime.GOOS, runtime.GOARCH, sz, db.MaxSize)\n\t\treturn berrors.ErrMaxSizeReached\n\t}\n\n\t// Truncate and fsync to ensure file size metadata is flushed.\n\t// https://github.com/boltdb/bolt/issues/284\n\tif !db.NoGrowSync && !db.readOnly {\n\t\tif runtime.GOOS != \"windows\" {\n\t\t\t// gofail: var resizeFileError string\n\t\t\t// return errors.New(resizeFileError)\n\t\t\tif err := db.file.Truncate(int64(sz)); err != nil {\n\t\t\t\tlg.Errorf(\"[GOOS: %s, GOARCH: %s] truncating file failed, size: %d, db.datasz: %d, error: %v\", runtime.GOOS, runtime.GOARCH, sz, db.datasz, err)\n\t\t\t\treturn fmt.Errorf(\"file resize error: %s\", err)\n\t\t\t}\n\t\t}\n\t\tif err := db.file.Sync(); err != nil {\n\t\t\tlg.Errorf(\"[GOOS: %s, GOARCH: %s] syncing file failed, db.datasz: %d, error: %v\", runtime.GOOS, runtime.GOARCH, db.datasz, err)\n\t\t\treturn fmt.Errorf(\"file sync error: %s\", err)\n\t\t}\n\t\tif db.Mlock {\n\t\t\t// unlock old file and lock new one\n\t\t\tif err := db.mrelock(fileSize, sz); err != nil {\n\t\t\t\treturn fmt.Errorf(\"mlock/munlock error: %s\", err)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (db *DB) IsReadOnly() bool {\n\treturn db.readOnly\n}\n\nfunc (db *DB) freepages() []common.Pgid {\n\ttx, err := db.beginTx()\n\tdefer func() {\n\t\terr = tx.Rollback()\n\t\tif err != nil {\n\t\t\tpanic(\"freepages: failed to rollback tx\")\n\t\t}\n\t}()\n\tif err != nil {\n\t\tpanic(\"freepages: failed to open read only tx\")\n\t}\n\n\treachable := make(map[common.Pgid]*common.Page)\n\tnofreed := make(map[common.Pgid]bool)\n\tech := make(chan error)\n\n\tgo func() {\n\t\tdefer close(ech)\n\t\ttx.recursivelyCheckBucket(&tx.root, reachable, nofreed, HexKVStringer(), ech)\n\t}()\n\t// following for loop will exit once channel is closed in the above goroutine.\n\t// we don't need to wait explictly with a waitgroup\n\tfor e := range ech {\n\t\tpanic(fmt.Sprintf(\"freepages: failed to get all reachable pages (%v)\", e))\n\t}\n\n\t// TODO: If check bucket reported any corruptions (ech) we shouldn't proceed to freeing the pages.\n\n\tvar fids []common.Pgid\n\tfor i := common.Pgid(2); i < db.meta().Pgid(); i++ {\n\t\tif _, ok := reachable[i]; !ok {\n\t\t\tfids = append(fids, i)\n\t\t}\n\t}\n\treturn fids\n}\n\nfunc newFreelist(freelistType FreelistType) fl.Interface {\n\tif freelistType == FreelistMapType {\n\t\treturn fl.NewHashMapFreelist()\n\t}\n\treturn fl.NewArrayFreelist()\n}\n\n// Options represents the options that can be set when opening a database.\ntype Options struct {\n\t// Timeout is the amount of time to wait to obtain a file lock.\n\t// When set to zero it will wait indefinitely.\n\tTimeout time.Duration\n\n\t// Sets the DB.NoGrowSync flag before memory mapping the file.\n\tNoGrowSync bool\n\n\t// Do not sync freelist to disk. This improves the database write performance\n\t// under normal operation, but requires a full database re-sync during recovery.\n\tNoFreelistSync bool\n\n\t// PreLoadFreelist sets whether to load the free pages when opening\n\t// the db file. Note when opening db in write mode, bbolt will always\n\t// load the free pages.\n\tPreLoadFreelist bool\n\n\t// FreelistType sets the backend freelist type. There are two options. Array which is simple but endures\n\t// dramatic performance degradation if database is large and fragmentation in freelist is common.\n\t// The alternative one is using hashmap, it is faster in almost all circumstances\n\t// but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe.\n\t// The default type is array\n\tFreelistType FreelistType\n\n\t// Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to\n\t// grab a shared lock (UNIX).\n\tReadOnly bool\n\n\t// Sets the DB.MmapFlags flag before memory mapping the file.\n\tMmapFlags int\n\n\t// InitialMmapSize is the initial mmap size of the database\n\t// in bytes. Read transactions won't block write transaction\n\t// if the InitialMmapSize is large enough to hold database mmap\n\t// size. (See DB.Begin for more information)\n\t//\n\t// If <=0, the initial map size is 0.\n\t// If initialMmapSize is smaller than the previous database size,\n\t// it takes no effect.\n\t//\n\t// Note: On Windows, due to platform limitations, the database file size\n\t// will be immediately resized to match `InitialMmapSize` (aligned to page size)\n\t// when the DB is opened. On non-Windows platforms, the file size will grow\n\t// dynamically based on the actual amount of written data, regardless of `InitialMmapSize`.\n\t// Refer to https://github.com/etcd-io/bbolt/issues/378#issuecomment-1378121966.\n\tInitialMmapSize int\n\n\t// PageSize overrides the default OS page size.\n\tPageSize int\n\n\t// MaxSize sets the maximum size of the data file. <=0 means no maximum.\n\tMaxSize int\n\n\t// NoSync sets the initial value of DB.NoSync. Normally this can just be\n\t// set directly on the DB itself when returned from Open(), but this option\n\t// is useful in APIs which expose Options but not the underlying DB.\n\tNoSync bool\n\n\t// OpenFile is used to open files. It defaults to os.OpenFile. This option\n\t// is useful for writing hermetic tests.\n\tOpenFile func(string, int, os.FileMode) (*os.File, error)\n\n\t// Mlock locks database file in memory when set to true.\n\t// It prevents potential page faults, however\n\t// used memory can't be reclaimed. (UNIX only)\n\tMlock bool\n\n\t// Logger is the logger used for bbolt.\n\tLogger Logger\n\n\t// NoStatistics turns off statistics collection, Stats method will\n\t// return empty structure in this case. This can be beneficial for\n\t// performance under high-concurrency read-only transactions.\n\tNoStatistics bool\n}\n\nfunc (o *Options) String() string {\n\tif o == nil {\n\t\treturn \"{}\"\n\t}\n\n\treturn fmt.Sprintf(\"{Timeout: %s, NoGrowSync: %t, NoFreelistSync: %t, PreLoadFreelist: %t, FreelistType: %s, ReadOnly: %t, MmapFlags: %x, InitialMmapSize: %d, PageSize: %d, MaxSize: %d, NoSync: %t, OpenFile: %p, Mlock: %t, Logger: %p, NoStatistics: %t}\",\n\t\to.Timeout, o.NoGrowSync, o.NoFreelistSync, o.PreLoadFreelist, o.FreelistType, o.ReadOnly, o.MmapFlags, o.InitialMmapSize, o.PageSize, o.MaxSize, o.NoSync, o.OpenFile, o.Mlock, o.Logger, o.NoStatistics)\n\n}\n\n// DefaultOptions represent the options used if nil options are passed into Open().\n// No timeout is used which will cause Bolt to wait indefinitely for a lock.\nvar DefaultOptions = &Options{\n\tTimeout:      0,\n\tNoGrowSync:   false,\n\tFreelistType: FreelistArrayType,\n}\n\n// Stats represents statistics about the database.\ntype Stats struct {\n\t// Put `TxStats` at the first field to ensure it's 64-bit aligned. Note\n\t// that the first word in an allocated struct can be relied upon to be\n\t// 64-bit aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG.\n\t// Also refer to discussion in https://github.com/etcd-io/bbolt/issues/577.\n\tTxStats TxStats // global, ongoing stats.\n\n\t// Freelist stats\n\tFreePageN     int // total number of free pages on the freelist\n\tPendingPageN  int // total number of pending pages on the freelist\n\tFreeAlloc     int // total bytes allocated in free pages\n\tFreelistInuse int // total bytes used by the freelist\n\n\t// Transaction stats\n\tTxN     int // total number of started read transactions\n\tOpenTxN int // number of currently open read transactions\n}\n\n// Sub calculates and returns the difference between two sets of database stats.\n// This is useful when obtaining stats at two different points and time and\n// you need the performance counters that occurred within that time span.\nfunc (s *Stats) Sub(other *Stats) Stats {\n\tif other == nil {\n\t\treturn *s\n\t}\n\tvar diff Stats\n\tdiff.FreePageN = s.FreePageN\n\tdiff.PendingPageN = s.PendingPageN\n\tdiff.FreeAlloc = s.FreeAlloc\n\tdiff.FreelistInuse = s.FreelistInuse\n\tdiff.TxN = s.TxN - other.TxN\n\tdiff.TxStats = s.TxStats.Sub(&other.TxStats)\n\treturn diff\n}\n\ntype Info struct {\n\tData     uintptr\n\tPageSize int\n}\n"
  },
  {
    "path": "db_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"fmt\"\n\t\"hash/fnv\"\n\t\"log\"\n\t\"math/rand\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"reflect\"\n\t\"runtime\"\n\t\"strings\"\n\t\"sync\"\n\t\"testing\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\tberrors \"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// pageSize is the size of one page in the data file.\nconst pageSize = 4096\n\n// pageHeaderSize is the size of a page header.\nconst pageHeaderSize = 16\n\n// meta represents a simplified version of a database meta page for testing.\ntype meta struct {\n\t_       uint32\n\tversion uint32\n\t_       uint32\n\t_       uint32\n\t_       [16]byte\n\t_       uint64\n\tpgid    uint64\n\t_       uint64\n\t_       uint64\n}\n\n// Ensure that a database can be opened without error.\nfunc TestOpen(t *testing.T) {\n\tpath := tempfile()\n\tdefer os.RemoveAll(path)\n\n\tdb, err := bolt.Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t} else if db == nil {\n\t\tt.Fatal(\"expected db\")\n\t}\n\n\tif s := db.Path(); s != path {\n\t\tt.Fatalf(\"unexpected path: %s\", s)\n\t}\n\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Regression validation for https://github.com/etcd-io/bbolt/pull/122.\n// Tests multiple goroutines simultaneously opening a database.\nfunc TestOpen_MultipleGoroutines(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode\")\n\t}\n\n\tconst (\n\t\tinstances  = 30\n\t\titerations = 30\n\t)\n\tpath := tempfile()\n\tdefer os.RemoveAll(path)\n\tvar wg sync.WaitGroup\n\terrCh := make(chan error, iterations*instances)\n\tfor iteration := 0; iteration < iterations; iteration++ {\n\t\tfor instance := 0; instance < instances; instance++ {\n\t\t\twg.Add(1)\n\t\t\tgo func() {\n\t\t\t\tdefer wg.Done()\n\t\t\t\tdb, err := bolt.Open(path, 0600, nil)\n\t\t\t\tif err != nil {\n\t\t\t\t\terrCh <- err\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tif err := db.Close(); err != nil {\n\t\t\t\t\terrCh <- err\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}()\n\t\t}\n\t\twg.Wait()\n\t}\n\tclose(errCh)\n\tfor err := range errCh {\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"error from inside goroutine: %v\", err)\n\t\t}\n\t}\n}\n\n// Ensure that opening a database with a blank path returns an error.\nfunc TestOpen_ErrPathRequired(t *testing.T) {\n\t_, err := bolt.Open(\"\", 0600, nil)\n\tif err == nil {\n\t\tt.Fatalf(\"expected error\")\n\t}\n}\n\n// Ensure that opening a database with a bad path returns an error.\nfunc TestOpen_ErrNotExists(t *testing.T) {\n\t_, err := bolt.Open(filepath.Join(tempfile(), \"bad-path\"), 0600, nil)\n\tif err == nil {\n\t\tt.Fatal(\"expected error\")\n\t}\n}\n\n// Ensure that opening a file that is not a Bolt database returns ErrInvalid.\nfunc TestOpen_ErrInvalid(t *testing.T) {\n\tpath := tempfile()\n\tdefer os.RemoveAll(path)\n\n\tf, err := os.Create(path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif _, err := fmt.Fprintln(f, \"this is not a bolt database\"); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := f.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif _, err := bolt.Open(path, 0600, nil); err != berrors.ErrInvalid {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that opening a file with two invalid versions returns ErrVersionMismatch.\nfunc TestOpen_ErrVersionMismatch(t *testing.T) {\n\tif pageSize != os.Getpagesize() {\n\t\tt.Skip(\"page size mismatch\")\n\t}\n\n\t// Create empty database.\n\tdb := btesting.MustCreateDB(t)\n\tpath := db.Path()\n\n\t// Close database.\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read data file.\n\tbuf, err := os.ReadFile(path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Rewrite meta pages.\n\tmeta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize]))\n\tmeta0.version++\n\tmeta1 := (*meta)(unsafe.Pointer(&buf[pageSize+pageHeaderSize]))\n\tmeta1.version++\n\tif err := os.WriteFile(path, buf, 0666); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Reopen data file.\n\tif _, err := bolt.Open(path, 0600, nil); err != berrors.ErrVersionMismatch {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that opening a file with two invalid checksums returns ErrChecksum.\nfunc TestOpen_ErrChecksum(t *testing.T) {\n\tif pageSize != os.Getpagesize() {\n\t\tt.Skip(\"page size mismatch\")\n\t}\n\n\t// Create empty database.\n\tdb := btesting.MustCreateDB(t)\n\tpath := db.Path()\n\n\t// Close database.\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read data file.\n\tbuf, err := os.ReadFile(path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Rewrite meta pages.\n\tmeta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize]))\n\tmeta0.pgid++\n\tmeta1 := (*meta)(unsafe.Pointer(&buf[pageSize+pageHeaderSize]))\n\tmeta1.pgid++\n\tif err := os.WriteFile(path, buf, 0666); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Reopen data file.\n\tif _, err := bolt.Open(path, 0600, nil); err != berrors.ErrChecksum {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that it can read the page size from the second meta page if the first one is invalid.\n// The page size is expected to be the OS's page size in this case.\nfunc TestOpen_ReadPageSize_FromMeta1_OS(t *testing.T) {\n\t// Create empty database.\n\tdb := btesting.MustCreateDB(t)\n\tpath := db.Path()\n\t// Close the database\n\tdb.MustClose()\n\n\t// Read data file.\n\tbuf, err := os.ReadFile(path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Rewrite first meta page.\n\tmeta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize]))\n\tmeta0.pgid++\n\tif err := os.WriteFile(path, buf, 0666); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Reopen data file.\n\tdb = btesting.MustOpenDBWithOption(t, path, nil)\n\trequire.Equalf(t, os.Getpagesize(), db.Info().PageSize, \"check page size failed\")\n}\n\n// Ensure that it can read the page size from the second meta page if the first one is invalid.\n// The page size is expected to be the given page size in this case.\nfunc TestOpen_ReadPageSize_FromMeta1_Given(t *testing.T) {\n\t// test page size from 1KB (1024<<0) to 16MB(1024<<14)\n\tfor i := 0; i <= 14; i++ {\n\t\tgivenPageSize := 1024 << uint(i)\n\t\tt.Logf(\"Testing page size %d\", givenPageSize)\n\t\t// Create empty database.\n\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: givenPageSize})\n\t\tpath := db.Path()\n\t\t// Close the database\n\t\tdb.MustClose()\n\n\t\t// Read data file.\n\t\tbuf, err := os.ReadFile(path)\n\t\trequire.NoError(t, err)\n\n\t\t// Rewrite meta pages.\n\t\tif i%3 == 0 {\n\t\t\tt.Logf(\"#%d: Intentionally corrupt the first meta page for pageSize %d\", i, givenPageSize)\n\t\t\tmeta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize]))\n\t\t\tmeta0.pgid++\n\t\t\terr = os.WriteFile(path, buf, 0666)\n\t\t\trequire.NoError(t, err)\n\t\t}\n\n\t\t// Reopen data file.\n\t\tdb = btesting.MustOpenDBWithOption(t, path, nil)\n\t\trequire.Equalf(t, givenPageSize, db.Info().PageSize, \"check page size failed\")\n\t\tdb.MustClose()\n\t}\n}\n\n// Ensure that opening a database does not increase its size.\n// https://github.com/boltdb/bolt/issues/291\nfunc TestOpen_Size(t *testing.T) {\n\t// Open a data file.\n\tdb := btesting.MustCreateDB(t)\n\n\tpagesize := db.Info().PageSize\n\n\t// Insert until we get above the minimum 4MB size.\n\terr := db.Fill([]byte(\"data\"), 1, 10000,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 1000) },\n\t)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tpath := db.Path()\n\tdb.MustClose()\n\n\tsz := fileSize(path)\n\tif sz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", sz)\n\t}\n\n\tdb.MustReopen()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.Bucket([]byte(\"data\")).Put([]byte{0}, []byte{0}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tnewSz := fileSize(path)\n\tif newSz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", newSz)\n\t}\n\n\t// Compare the original size with the new size.\n\t// db size might increase by a few page sizes due to the new small update.\n\tif sz < newSz-5*int64(pagesize) {\n\t\tt.Fatalf(\"unexpected file growth: %d => %d\", sz, newSz)\n\t}\n}\n\n// Ensure that opening a database beyond the max step size does not increase its size.\n// https://github.com/boltdb/bolt/issues/303\nfunc TestOpen_Size_Large(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"short mode\")\n\t}\n\n\t// Open a data file.\n\tdb := btesting.MustCreateDB(t)\n\tpath := db.Path()\n\n\tpagesize := db.Info().PageSize\n\n\t// Insert until we get above the minimum 4MB size.\n\tvar index uint64\n\tfor i := 0; i < 10000; i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, _ := tx.CreateBucketIfNotExists([]byte(\"data\"))\n\t\t\tfor j := 0; j < 1000; j++ {\n\t\t\t\tif err := b.Put(u64tob(index), make([]byte, 50)); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t\tindex++\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Close database and grab the size.\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tsz := fileSize(path)\n\tif sz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", sz)\n\t} else if sz < (1 << 30) {\n\t\tt.Fatalf(\"expected larger initial size: %d\", sz)\n\t}\n\n\t// Reopen database, update, and check size again.\n\tdb0, err := bolt.Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db0.Update(func(tx *bolt.Tx) error {\n\t\treturn tx.Bucket([]byte(\"data\")).Put([]byte{0}, []byte{0})\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db0.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tnewSz := fileSize(path)\n\tif newSz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", newSz)\n\t}\n\n\t// Compare the original size with the new size.\n\t// db size might increase by a few page sizes due to the new small update.\n\tif sz < newSz-5*int64(pagesize) {\n\t\tt.Fatalf(\"unexpected file growth: %d => %d\", sz, newSz)\n\t}\n}\n\n// Ensure that a re-opened database is consistent.\nfunc TestOpen_Check(t *testing.T) {\n\tpath := tempfile()\n\tdefer os.RemoveAll(path)\n\n\tdb, err := bolt.Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err = db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err = db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb, err = bolt.Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that write errors to the meta file handler during initialization are returned.\nfunc TestOpen_MetaInitWriteError(t *testing.T) {\n\tt.Skip(\"pending\")\n}\n\n// Ensure that a database that is too small returns an error.\nfunc TestOpen_FileTooSmall(t *testing.T) {\n\tpath := tempfile()\n\tdefer os.RemoveAll(path)\n\n\tdb, err := bolt.Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tpageSize := int64(db.Info().PageSize)\n\tif err = db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// corrupt the database\n\tif err = os.Truncate(path, pageSize); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t_, err = bolt.Open(path, 0600, nil)\n\tif err == nil || !strings.Contains(err.Error(), \"file size too small\") {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// TestDB_Open_InitialMmapSize tests if having InitialMmapSize large enough\n// to hold data from concurrent write transaction resolves the issue that\n// read transaction blocks the write transaction and causes deadlock.\n// This is a very hacky test since the mmap size is not exposed.\nfunc TestDB_Open_InitialMmapSize(t *testing.T) {\n\ttests := []struct {\n\t\tname       string\n\t\twithReadTx bool\n\t}{\n\t\t{\n\t\t\tname:       \"with concurrent read transaction\",\n\t\t\twithReadTx: true,\n\t\t},\n\t\t{\n\t\t\tname:       \"without concurrent read transaction\",\n\t\t\twithReadTx: false,\n\t\t},\n\t}\n\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tpath := tempfile()\n\t\t\tdefer os.Remove(path)\n\n\t\t\tinitMmapSize := 1 << 30  // 1GB\n\t\t\ttestWriteSize := 1 << 27 // 134MB\n\n\t\t\tdb, err := bolt.Open(path, 0600, &bolt.Options{InitialMmapSize: initMmapSize})\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tdefer db.Close()\n\n\t\t\tvar rtx *bolt.Tx\n\t\t\tif tt.withReadTx {\n\t\t\t\t// create a long-running read transaction\n\t\t\t\t// that never gets closed while writing\n\t\t\t\trtx, err = db.Begin(false)\n\t\t\t\tif err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// create a write transaction\n\t\t\twtx, err := db.Begin(true)\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\tb, err := wtx.CreateBucket([]byte(\"test\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\t// commit a large write\n\t\t\terr = b.Put([]byte(\"foo\"), make([]byte, testWriteSize))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\tdone := make(chan error, 1)\n\t\t\tstart := time.Now()\n\n\t\t\tgo func() {\n\t\t\t\terr := wtx.Commit()\n\t\t\t\tdone <- err\n\t\t\t}()\n\n\t\t\tselect {\n\t\t\tcase <-time.After(5 * time.Second):\n\t\t\t\tt.Error(\"unexpected that the writer is blocked for a long time\")\n\t\t\tcase err := <-done:\n\t\t\t\telapsed := time.Since(start)\n\t\t\t\tif err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t\tt.Logf(\"Write commit completed in %v\", elapsed)\n\t\t\t}\n\n\t\t\tif rtx != nil {\n\t\t\t\tif err := rtx.Rollback(); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\t}\n}\n\n// TestDB_Open_ReadOnly checks a database in read only mode can read but not write.\nfunc TestDB_Open_ReadOnly(t *testing.T) {\n\t// Create a writable db, write k-v and close it.\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tf := db.Path()\n\to := &bolt.Options{ReadOnly: true}\n\treadOnlyDB, err := bolt.Open(f, 0600, o)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tif !readOnlyDB.IsReadOnly() {\n\t\tt.Fatal(\"expect db in read only mode\")\n\t}\n\n\t// Read from a read-only transaction.\n\tif err := readOnlyDB.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tif !bytes.Equal(value, []byte(\"bar\")) {\n\t\t\tt.Fatal(\"expect value 'bar', got\", value)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Can't launch read-write transaction.\n\tif _, err := readOnlyDB.Begin(true); err != berrors.ErrDatabaseReadOnly {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n\n\tif err := readOnlyDB.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestDB_Open_ReadOnly_NoCreate(t *testing.T) {\n\tf := filepath.Join(t.TempDir(), \"db\")\n\t_, err := bolt.Open(f, 0600, &bolt.Options{ReadOnly: true})\n\trequire.ErrorIs(t, err, os.ErrNotExist)\n}\n\n// TestOpen_BigPage checks the database uses bigger pages when\n// changing PageSize.\nfunc TestOpen_BigPage(t *testing.T) {\n\tpageSize := os.Getpagesize()\n\n\tdb1 := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize * 2})\n\n\tdb2 := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize * 4})\n\n\tif db1sz, db2sz := fileSize(db1.Path()), fileSize(db2.Path()); db1sz >= db2sz {\n\t\tt.Errorf(\"expected %d < %d\", db1sz, db2sz)\n\t}\n}\n\n// TestOpen_RecoverFreeList tests opening the DB with free-list\n// write-out after no free list sync will recover the free list\n// and write it out.\nfunc TestOpen_RecoverFreeList(t *testing.T) {\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{NoFreelistSync: true})\n\n\t// Write some pages.\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\twbuf := make([]byte, 8192)\n\tfor i := 0; i < 100; i++ {\n\t\ts := fmt.Sprintf(\"%d\", i)\n\t\tb, err := tx.CreateBucket([]byte(s))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err = b.Put([]byte(s), wbuf); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\tif err = tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Generate free pages.\n\tif tx, err = db.Begin(true); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tfor i := 0; i < 50; i++ {\n\t\ts := fmt.Sprintf(\"%d\", i)\n\t\tb := tx.Bucket([]byte(s))\n\t\tif b == nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Delete([]byte(s)); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustClose()\n\n\t// Record freelist count from opening with NoFreelistSync.\n\tdb.MustReopen()\n\tfreepages := db.Stats().FreePageN\n\tif freepages == 0 {\n\t\tt.Fatalf(\"no free pages on NoFreelistSync reopen\")\n\t}\n\tdb.MustClose()\n\n\t// Check free page count is reconstructed when opened with freelist sync.\n\tdb.SetOptions(&bolt.Options{})\n\tdb.MustReopen()\n\t// One less free page for syncing the free list on open.\n\tfreepages--\n\tif fp := db.Stats().FreePageN; fp < freepages {\n\t\tt.Fatalf(\"closed with %d free pages, opened with %d\", freepages, fp)\n\t}\n}\n\n// Ensure that a database cannot open a transaction when it's not open.\nfunc TestDB_Begin_ErrDatabaseNotOpen(t *testing.T) {\n\tvar db bolt.DB\n\tif _, err := db.Begin(false); err != berrors.ErrDatabaseNotOpen {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that a read-write transaction can be retrieved.\nfunc TestDB_BeginRW(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\ttx, err := db.Begin(true)\n\trequire.NoError(t, err)\n\trequire.NotNil(t, tx, \"expected tx\")\n\tdefer func() { require.NoError(t, tx.Commit()) }()\n\n\trequire.True(t, tx.Writable(), \"expected writable tx\")\n\trequire.Same(t, db.DB, tx.DB())\n}\n\n// TestDB_Concurrent_WriteTo checks that issuing WriteTo operations concurrently\n// with commits does not produce corrupted db files. It also verifies that all\n// readonly transactions, which are created based on the same data view, should\n// always read the same data.\nfunc TestDB_Concurrent_WriteTo_and_ConsistentRead(t *testing.T) {\n\to := &bolt.Options{\n\t\tNoFreelistSync: false,\n\t\tPageSize:       4096,\n\t}\n\tdb := btesting.MustCreateDBWithOption(t, o)\n\n\twtxs, rtxs := 50, 5\n\tbucketName := []byte(\"data\")\n\n\tvar dataLock sync.Mutex\n\tdataCache := make(map[int][]map[string]string)\n\n\tvar wg sync.WaitGroup\n\twg.Add(wtxs * rtxs)\n\tf := func(round int, tx *bolt.Tx) {\n\t\tdefer wg.Done()\n\t\ttime.Sleep(time.Duration(rand.Intn(200)+10) * time.Millisecond)\n\t\tf := filepath.Join(t.TempDir(), fmt.Sprintf(\"%d-bolt-\", round))\n\t\terr := tx.CopyFile(f, 0600)\n\t\trequire.NoError(t, err)\n\n\t\t// read all the data\n\t\tb := tx.Bucket(bucketName)\n\t\tdata := make(map[string]string)\n\t\terr = b.ForEach(func(k, v []byte) error {\n\t\t\tdata[string(k)] = string(v)\n\t\t\treturn nil\n\t\t})\n\t\trequire.NoError(t, err)\n\n\t\t// cache the data\n\t\tdataLock.Lock()\n\t\tdataSlice := dataCache[round]\n\t\tdataSlice = append(dataSlice, data)\n\t\tdataCache[round] = dataSlice\n\t\tdataLock.Unlock()\n\n\t\terr = tx.Rollback()\n\t\trequire.NoError(t, err)\n\n\t\tcopyOpt := *o\n\t\tsnap := btesting.MustOpenDBWithOption(t, f, &copyOpt)\n\t\tdefer snap.MustClose()\n\t\tsnap.MustCheck()\n\t}\n\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket(bucketName)\n\t\treturn err\n\t})\n\trequire.NoError(t, err)\n\n\tfor i := 0; i < wtxs; i++ {\n\t\ttx, err := db.Begin(true)\n\t\trequire.NoError(t, err)\n\n\t\tb := tx.Bucket(bucketName)\n\n\t\tfor j := 0; j < rtxs; j++ {\n\t\t\trtx, rerr := db.Begin(false)\n\t\t\trequire.NoError(t, rerr)\n\t\t\tgo f(i, rtx)\n\n\t\t\tfor k := 0; k < 10; k++ {\n\t\t\t\tkey, value := fmt.Sprintf(\"key_%d\", rand.Intn(10)), fmt.Sprintf(\"value_%d\", rand.Intn(100))\n\t\t\t\tperr := b.Put([]byte(key), []byte(value))\n\t\t\t\trequire.NoError(t, perr)\n\t\t\t}\n\t\t}\n\t\terr = tx.Commit()\n\t\trequire.NoError(t, err)\n\t}\n\twg.Wait()\n\n\t// compare the data. The data generated in the same round\n\t// should be exactly the same.\n\tfor round, dataSlice := range dataCache {\n\t\tdata0 := dataSlice[0]\n\n\t\tfor i := 1; i < len(dataSlice); i++ {\n\t\t\tdatai := dataSlice[i]\n\t\t\tsame := reflect.DeepEqual(data0, datai)\n\t\t\trequire.True(t, same, fmt.Sprintf(\"found inconsistent data in round %d, data[0]: %v, data[%d] : %v\", round, data0, i, datai))\n\t\t}\n\t}\n}\n\n// TestDB_WriteTo_and_Overwrite verifies that `(tx *Tx) WriteTo` can still\n// work even the underlying file is overwritten between the time a read-only\n// transaction is created and the time the file is actually opened\nfunc TestDB_WriteTo_and_Overwrite(t *testing.T) {\n\ttestCases := []struct {\n\t\tname      string\n\t\twriteFlag int\n\t}{\n\t\t{\n\t\t\tname:      \"writeFlag not set\",\n\t\t\twriteFlag: 0,\n\t\t},\n\t\t/* syscall.O_DIRECT not supported on some platforms, i.e. Windows and MacOS\n\t\t{\n\t\t\tname:      \"writeFlag set\",\n\t\t\twriteFlag: syscall.O_DIRECT,\n\t\t},*/\n\t}\n\n\tfRead := func(db *bolt.DB, bucketName []byte) map[string]string {\n\t\tdata := make(map[string]string)\n\t\t_ = db.View(func(tx *bolt.Tx) error {\n\t\t\tb := tx.Bucket(bucketName)\n\t\t\tberr := b.ForEach(func(k, v []byte) error {\n\t\t\t\tdata[string(k)] = string(v)\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, berr)\n\t\t\treturn nil\n\t\t})\n\t\treturn data\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{\n\t\t\t\tPageSize: 4096,\n\t\t\t})\n\t\t\tfilePathOfDb := db.Path()\n\n\t\t\tvar (\n\t\t\t\tbucketName   = []byte(\"data\")\n\t\t\t\tdataExpected map[string]string\n\t\t\t\tdataActual   map[string]string\n\t\t\t)\n\n\t\t\tt.Log(\"Populate some data\")\n\t\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tb, berr := tx.CreateBucket(bucketName)\n\t\t\t\tif berr != nil {\n\t\t\t\t\treturn berr\n\t\t\t\t}\n\t\t\t\tfor k := 0; k < 10; k++ {\n\t\t\t\t\tkey, value := fmt.Sprintf(\"key_%d\", rand.Intn(10)), fmt.Sprintf(\"value_%d\", rand.Intn(100))\n\t\t\t\t\tif perr := b.Put([]byte(key), []byte(value)); perr != nil {\n\t\t\t\t\t\treturn perr\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\n\t\t\tt.Log(\"Read all the data before calling WriteTo\")\n\t\t\tdataExpected = fRead(db.DB, bucketName)\n\n\t\t\tt.Log(\"Create a readonly transaction for WriteTo\")\n\t\t\trtx, rerr := db.Begin(false)\n\t\t\trequire.NoError(t, rerr)\n\n\t\t\t// Some platforms (i.e. Windows) don't support renaming a file\n\t\t\t// when the target file already exist and is opened.\n\t\t\tif runtime.GOOS == \"linux\" {\n\t\t\t\tt.Log(\"Create another empty db file\")\n\t\t\t\tdb2 := btesting.MustCreateDBWithOption(t, &bolt.Options{\n\t\t\t\t\tPageSize: 4096,\n\t\t\t\t})\n\t\t\t\tdb2.MustClose()\n\t\t\t\tfilePathOfDb2 := db2.Path()\n\n\t\t\t\tt.Logf(\"Renaming the new empty db file (%s) to the original db path (%s)\", filePathOfDb2, filePathOfDb)\n\t\t\t\terr = os.Rename(filePathOfDb2, filePathOfDb)\n\t\t\t\trequire.NoError(t, err)\n\t\t\t} else {\n\t\t\t\tt.Log(\"Ignore renaming step on non-Linux platform\")\n\t\t\t}\n\n\t\t\tt.Logf(\"Call WriteTo to copy the data of the original db file\")\n\t\t\tf := filepath.Join(t.TempDir(), \"-backup-db\")\n\t\t\terr = rtx.CopyFile(f, 0600)\n\t\t\trequire.NoError(t, err)\n\t\t\trequire.NoError(t, rtx.Rollback())\n\n\t\t\tt.Logf(\"Read all the data from the backup db after calling WriteTo\")\n\t\t\tnewDB, err := bolt.Open(f, 0600, &bolt.Options{\n\t\t\t\tReadOnly: true,\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\t\t\tdataActual = fRead(newDB, bucketName)\n\t\t\terr = newDB.Close()\n\t\t\trequire.NoError(t, err)\n\n\t\t\tt.Log(\"Compare the dataExpected and dataActual\")\n\t\t\tsame := reflect.DeepEqual(dataExpected, dataActual)\n\t\t\trequire.True(t, same, fmt.Sprintf(\"found inconsistent data, dataExpected: %v, ddataActual : %v\", dataExpected, dataActual))\n\t\t})\n\t}\n}\n\n// Ensure that opening a transaction while the DB is closed returns an error.\nfunc TestDB_BeginRW_Closed(t *testing.T) {\n\tvar db bolt.DB\n\tif _, err := db.Begin(true); err != berrors.ErrDatabaseNotOpen {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\nfunc TestDB_Close_PendingTx_RW(t *testing.T) { testDB_Close_PendingTx(t, true) }\nfunc TestDB_Close_PendingTx_RO(t *testing.T) { testDB_Close_PendingTx(t, false) }\n\n// Ensure that a database cannot close while transactions are open.\nfunc testDB_Close_PendingTx(t *testing.T, writable bool) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Start transaction.\n\ttx, err := db.Begin(writable)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Open update in separate goroutine.\n\tstartCh := make(chan struct{}, 1)\n\tdone := make(chan error, 1)\n\tgo func() {\n\t\tstartCh <- struct{}{}\n\t\terr := db.Close()\n\t\tdone <- err\n\t}()\n\t// wait for the above goroutine to get scheduled.\n\t<-startCh\n\n\t// Ensure database hasn't closed.\n\ttime.Sleep(100 * time.Millisecond)\n\tselect {\n\tcase err := <-done:\n\t\tif err != nil {\n\t\t\tt.Errorf(\"error from inside goroutine: %v\", err)\n\t\t}\n\t\tt.Fatal(\"database closed too early\")\n\tdefault:\n\t}\n\n\t// Commit/close transaction.\n\tif writable {\n\t\terr = tx.Commit()\n\t} else {\n\t\terr = tx.Rollback()\n\t}\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Ensure database closed now.\n\tselect {\n\tcase err := <-done:\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"error from inside goroutine: %v\", err)\n\t\t}\n\tcase <-time.After(5 * time.Second):\n\t\tt.Fatalf(\"database did not close\")\n\t}\n}\n\n// Ensure a database can provide a transactional block.\nfunc TestDB_Update(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Delete([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatalf(\"expected nil value, got: %v\", v)\n\t\t}\n\t\tif v := b.Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"bat\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a closed database returns an error while running a transaction block\nfunc TestDB_Update_Closed(t *testing.T) {\n\tvar db bolt.DB\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != berrors.ErrDatabaseNotOpen {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure a panic occurs while trying to commit a managed transaction.\nfunc TestDB_Update_ManualCommit(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar panicked bool\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a panic occurs while trying to rollback a managed transaction.\nfunc TestDB_Update_ManualRollback(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar panicked bool\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Rollback(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a panic occurs while trying to commit a managed transaction.\nfunc TestDB_View_ManualCommit(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar panicked bool\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a panic occurs while trying to rollback a managed transaction.\nfunc TestDB_View_ManualRollback(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar panicked bool\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Rollback(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a write transaction that panics does not hold open locks.\nfunc TestDB_Update_Panic(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Panic during update but recover.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tt.Log(\"recover: update\", r)\n\t\t\t}\n\t\t}()\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tpanic(\"omg\")\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}()\n\n\t// Verify we can update again.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Verify that our change persisted.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a database can return an error through a read-only transactional block.\nfunc TestDB_View_Error(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn errors.New(\"xxx\")\n\t}); err == nil || err.Error() != \"xxx\" {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure a read transaction that panics does not hold open locks.\nfunc TestDB_View_Panic(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Panic during view transaction but recover.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tt.Log(\"recover: view\", r)\n\t\t\t}\n\t\t}()\n\n\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\t\tt.Fatal(\"expected bucket\")\n\t\t\t}\n\t\t\tpanic(\"omg\")\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}()\n\n\t// Verify that we can still use read transactions.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that DB stats can be returned.\nfunc TestDB_Stats(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tstats := db.Stats()\n\tif stats.TxStats.GetPageCount() != 2 {\n\t\tt.Fatalf(\"unexpected TxStats.PageCount: %d\", stats.TxStats.GetPageCount())\n\t} else if stats.FreePageN != 0 {\n\t\tt.Fatalf(\"unexpected FreePageN != 0: %d\", stats.FreePageN)\n\t} else if stats.PendingPageN != 2 {\n\t\tt.Fatalf(\"unexpected PendingPageN != 2: %d\", stats.PendingPageN)\n\t}\n}\n\n// Ensure that database pages are in expected order and type.\nfunc TestDB_Consistency(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tfor i := 0; i < 10; i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif err := tx.Bucket([]byte(\"widgets\")).Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif p, _ := tx.Page(0); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"meta\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(1); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"meta\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(2); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"free\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(3); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"free\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(4); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"leaf\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(5); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"freelist\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(6); p != nil {\n\t\t\tt.Fatal(\"unexpected page\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that DB stats can be subtracted from one another.\nfunc TestDBStats_Sub(t *testing.T) {\n\tvar a, b bolt.Stats\n\ta.TxStats.PageCount = 3\n\ta.FreePageN = 4\n\tb.TxStats.PageCount = 10\n\tb.FreePageN = 14\n\tdiff := b.Sub(&a)\n\tif diff.TxStats.GetPageCount() != 7 {\n\t\tt.Fatalf(\"unexpected TxStats.PageCount: %d\", diff.TxStats.GetPageCount())\n\t}\n\n\t// free page stats are copied from the receiver and not subtracted\n\tif diff.FreePageN != 14 {\n\t\tt.Fatalf(\"unexpected FreePageN: %d\", diff.FreePageN)\n\t}\n}\n\n// Ensure two functions can perform updates in a single batch.\nfunc TestDB_Batch(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Iterate over multiple updates in separate goroutines.\n\tn := 2\n\tch := make(chan error, n)\n\tfor i := 0; i < n; i++ {\n\t\tgo func(i int) {\n\t\t\tch <- db.Batch(func(tx *bolt.Tx) error {\n\t\t\t\treturn tx.Bucket([]byte(\"widgets\")).Put(u64tob(uint64(i)), []byte{})\n\t\t\t})\n\t\t}(i)\n\t}\n\n\t// Check all responses to make sure there's no error.\n\tfor i := 0; i < n; i++ {\n\t\tif err := <-ch; err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Ensure data is correct.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < n; i++ {\n\t\t\tif v := b.Get(u64tob(uint64(i))); v == nil {\n\t\t\t\tt.Errorf(\"key not found: %d\", i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestDB_Batch_Panic(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar sentinel int\n\tvar bork = &sentinel\n\tvar problem interface{}\n\tvar err error\n\n\t// Execute a function inside a batch that panics.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tif p := recover(); p != nil {\n\t\t\t\tproblem = p\n\t\t\t}\n\t\t}()\n\t\terr = db.Batch(func(tx *bolt.Tx) error {\n\t\t\tpanic(bork)\n\t\t})\n\t}()\n\n\t// Verify there is no error.\n\tif g, e := err, error(nil); g != e {\n\t\tt.Fatalf(\"wrong error: %v != %v\", g, e)\n\t}\n\t// Verify the panic was captured.\n\tif g, e := problem, bork; g != e {\n\t\tt.Fatalf(\"wrong error: %v != %v\", g, e)\n\t}\n}\n\nfunc TestDB_BatchFull(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tconst size = 3\n\t// buffered so we never leak goroutines\n\tch := make(chan error, size)\n\tput := func(i int) {\n\t\tch <- db.Batch(func(tx *bolt.Tx) error {\n\t\t\treturn tx.Bucket([]byte(\"widgets\")).Put(u64tob(uint64(i)), []byte{})\n\t\t})\n\t}\n\n\tdb.MaxBatchSize = size\n\t// high enough to never trigger here\n\tdb.MaxBatchDelay = 1 * time.Hour\n\n\tgo put(1)\n\tgo put(2)\n\n\t// Give the batch a chance to exhibit bugs.\n\ttime.Sleep(10 * time.Millisecond)\n\n\t// not triggered yet\n\tselect {\n\tcase <-ch:\n\t\tt.Fatalf(\"batch triggered too early\")\n\tdefault:\n\t}\n\n\tgo put(3)\n\n\t// Check all responses to make sure there's no error.\n\tfor i := 0; i < size; i++ {\n\t\tif err := <-ch; err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Ensure data is correct.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 1; i <= size; i++ {\n\t\t\tif v := b.Get(u64tob(uint64(i))); v == nil {\n\t\t\t\tt.Errorf(\"key not found: %d\", i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestDB_BatchTime(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tconst size = 1\n\t// buffered so we never leak goroutines\n\tch := make(chan error, size)\n\tput := func(i int) {\n\t\tch <- db.Batch(func(tx *bolt.Tx) error {\n\t\t\treturn tx.Bucket([]byte(\"widgets\")).Put(u64tob(uint64(i)), []byte{})\n\t\t})\n\t}\n\n\tdb.MaxBatchSize = 1000\n\tdb.MaxBatchDelay = 0\n\n\tgo put(1)\n\n\t// Batch must trigger by time alone.\n\n\t// Check all responses to make sure there's no error.\n\tfor i := 0; i < size; i++ {\n\t\tif err := <-ch; err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Ensure data is correct.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 1; i <= size; i++ {\n\t\t\tif v := b.Get(u64tob(uint64(i))); v == nil {\n\t\t\t\tt.Errorf(\"key not found: %d\", i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// TestDBUnmap verifes that `dataref`, `data` and `datasz` must be reset\n// to zero values respectively after unmapping the db.\nfunc TestDBUnmap(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\trequire.NoError(t, db.DB.Close())\n\n\t// Ignore the following error:\n\t// Error: copylocks: call of reflect.ValueOf copies lock value: go.etcd.io/bbolt.DB contains sync.Once contains sync.Mutex (govet)\n\t//nolint:govet\n\tv := reflect.ValueOf(*db.DB)\n\tdataref := v.FieldByName(\"dataref\")\n\tdata := v.FieldByName(\"data\")\n\tdatasz := v.FieldByName(\"datasz\")\n\tassert.True(t, dataref.IsNil())\n\tassert.True(t, data.IsNil())\n\tassert.True(t, datasz.IsZero())\n\n\t// Set db.DB to nil to prevent MustCheck from panicking.\n\tdb.DB = nil\n}\n\n// Convenience function for inserting a bunch of keys with 1000 byte values\nfunc fillDBWithKeys(db *btesting.DB, numKeys int) error {\n\treturn db.Fill([]byte(\"data\"), 1, numKeys,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 1000) },\n\t)\n}\n\n// Creates a new database size, forces a specific allocation size jump, and fills it with the number of keys specified\nfunc createFilledDB(t testing.TB, o *bolt.Options, allocSize int, numKeys int) *btesting.DB {\n\t// Open a data file.\n\tdb := btesting.MustCreateDBWithOption(t, o)\n\tdb.AllocSize = allocSize\n\n\t// Insert a reasonable amount of data below the max size.\n\terr := db.Fill([]byte(\"data\"), 1, numKeys,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 1000) },\n\t)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\treturn db\n}\n\n// Ensure that a database cannot exceed its maximum size\n// https://github.com/etcd-io/bbolt/issues/928\nfunc TestDB_MaxSizeNotExceeded(t *testing.T) {\n\ttestCases := []struct {\n\t\tname    string\n\t\toptions bolt.Options\n\t}{\n\t\t{\n\t\t\tname: \"Standard case\",\n\t\t\toptions: bolt.Options{\n\t\t\t\tMaxSize:  5 * 1024 * 1024, // 5 MiB\n\t\t\t\tPageSize: 4096,\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tname: \"NoGrowSync\",\n\t\t\toptions: bolt.Options{\n\t\t\t\tMaxSize:    5 * 1024 * 1024, // 5 MiB\n\t\t\t\tPageSize:   4096,\n\t\t\t\tNoGrowSync: true,\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, testCase := range testCases {\n\t\tt.Run(testCase.name, func(t *testing.T) {\n\t\t\tdb := createFilledDB(t,\n\t\t\t\t&testCase.options,\n\t\t\t\t4*1024*1024, // adjust allocation jumps to 4 MiB\n\t\t\t\t2000,\n\t\t\t)\n\n\t\t\tpath := db.Path()\n\n\t\t\t// The data file should be 4 MiB now (expanded once from zero).\n\t\t\t// It should have space for roughly 16 more entries before trying to grow\n\t\t\t// Keep inserting until grow is required\n\t\t\terr := fillDBWithKeys(db, 100)\n\t\t\tassert.ErrorIs(t, err, berrors.ErrMaxSizeReached)\n\n\t\t\tnewSz := fileSize(path)\n\t\t\trequire.Greater(t, newSz, int64(0), \"unexpected new file size: %d\", newSz)\n\t\t\tassert.LessOrEqual(t, newSz, int64(db.MaxSize), \"The size of the data file should not exceed db.MaxSize\")\n\n\t\t\terr = db.Close()\n\t\t\trequire.NoError(t, err, \"Closing the re-opened database should succeed\")\n\t\t})\n\t}\n}\n\n// Ensure that opening a database that is beyond the maximum size succeeds\n// The maximum size should only apply to growing the data file\n// https://github.com/etcd-io/bbolt/issues/928\nfunc TestDB_MaxSizeExceededCanOpen(t *testing.T) {\n\t// Open a data file.\n\tdb := createFilledDB(t, nil, 4*1024*1024, 2000) // adjust allocation jumps to 4 MiB, fill with 2000, 1KB keys\n\tpath := db.Path()\n\n\t// Insert a reasonable amount of data below the max size.\n\terr := fillDBWithKeys(db, 2000)\n\trequire.NoError(t, err, \"fillDbWithKeys should succeed\")\n\n\terr = db.Close()\n\trequire.NoError(t, err, \"Close should succeed\")\n\n\t// The data file should be 4 MiB now (expanded once from zero).\n\tminimumSizeForTest := int64(1024 * 1024)\n\tnewSz := fileSize(path)\n\trequire.GreaterOrEqual(t, newSz, minimumSizeForTest, \"unexpected new file size: %d. Expected at least %d\", newSz, minimumSizeForTest)\n\n\t// Now try to re-open the database with an extremely small max size\n\tt.Logf(\"Reopening bbolt DB at: %s\", path)\n\tdb, err = btesting.OpenDBWithOption(t, path, &bolt.Options{\n\t\tMaxSize: 1,\n\t})\n\tassert.NoError(t, err, \"Should be able to open database bigger than MaxSize\")\n\n\terr = db.Close()\n\trequire.NoError(t, err, \"Closing the re-opened database should succeed\")\n}\n\n// Ensure that opening a database that is beyond the maximum size succeeds,\n// even when InitialMmapSize is above the limit (mmaps should not affect file size)\n// This test exists for platforms where Truncate should not be called during mmap\n// https://github.com/etcd-io/bbolt/issues/928\nfunc TestDB_MaxSizeExceededCanOpenWithHighMmap(t *testing.T) {\n\tif runtime.GOOS == \"windows\" {\n\t\t// In Windows, the file must be expanded to the mmap initial size,\n\t\t// so this test doesn't run in Windows.\n\t\tt.SkipNow()\n\t}\n\n\t// Open a data file.\n\tdb := createFilledDB(t, nil, 4*1024*1024, 2000) // adjust allocation jumps to 4 MiB, fill with 2000 1KB entries\n\tpath := db.Path()\n\n\terr := db.Close()\n\trequire.NoError(t, err, \"Close should succeed\")\n\n\t// The data file should be 4 MiB now (expanded once from zero).\n\tminimumSizeForTest := int64(1024 * 1024)\n\tnewSz := fileSize(path)\n\trequire.GreaterOrEqual(t, newSz, minimumSizeForTest, \"unexpected new file size: %d. Expected at least %d\", newSz, minimumSizeForTest)\n\n\t// Now try to re-open the database with an extremely small max size\n\tt.Logf(\"Reopening bbolt DB at: %s\", path)\n\tdb, err = btesting.OpenDBWithOption(t, path, &bolt.Options{\n\t\tMaxSize:         1,\n\t\tInitialMmapSize: int(minimumSizeForTest) * 2,\n\t})\n\tassert.NoError(t, err, \"Should be able to open database bigger than MaxSize when InitialMmapSize set high\")\n\n\terr = db.Close()\n\trequire.NoError(t, err, \"Closing the re-opened database should succeed\")\n}\n\n// Ensure that when InitialMmapSize is above the limit, opening a database\n// that is beyond the maximum size fails in Windows.\n// In Windows, the file must be expanded to the mmap initial size.\n// https://github.com/etcd-io/bbolt/issues/928\nfunc TestDB_MaxSizeExceededDoesNotGrow(t *testing.T) {\n\tif runtime.GOOS != \"windows\" {\n\t\t// This test is only relevant on Windows\n\t\tt.SkipNow()\n\t}\n\n\t// Open a data file.\n\tdb := createFilledDB(t, nil, 4*1024*1024, 2000) // adjust allocation jumps to 4 MiB, fill with 2000 1KB entries\n\tpath := db.Path()\n\n\terr := db.Close()\n\trequire.NoError(t, err, \"Close should succeed\")\n\n\t// The data file should be 4 MiB now (expanded once from zero).\n\tminimumSizeForTest := int64(1024 * 1024)\n\tnewSz := fileSize(path)\n\tassert.GreaterOrEqual(t, newSz, minimumSizeForTest, \"unexpected new file size: %d. Expected at least %d\", newSz, minimumSizeForTest)\n\n\t// Now try to re-open the database with an extremely small max size and\n\t// an initial mmap size to be greater than the actual file size, forcing an illegal grow on open\n\tt.Logf(\"Reopening bbolt DB at: %s\", path)\n\t_, err = btesting.OpenDBWithOption(t, path, &bolt.Options{\n\t\tMaxSize:         1,\n\t\tInitialMmapSize: int(newSz) * 2,\n\t})\n\tassert.Error(t, err, \"Opening the DB with InitialMmapSize > MaxSize should cause an error on Windows\")\n}\n\nfunc TestDB_HugeValue(t *testing.T) {\n\tdbPath := filepath.Join(t.TempDir(), \"db\")\n\tdb, err := bolt.Open(dbPath, 0600, nil)\n\trequire.NoError(t, err)\n\tdefer func() {\n\t\trequire.NoError(t, db.Close())\n\t}()\n\n\tmaxSize := 0xFFFFFFF + 1\n\t// On 32 bit systems, the MaxAllocSize is 0xFFFFFFF (268435455,\n\t// roughly 256MB), and the test will fail for sure, so we reduce\n\t// the maxSize by half in such case.\n\tif maxSize > common.MaxAllocSize {\n\t\tmaxSize = maxSize / 2\n\t}\n\tdata := make([]byte, maxSize)\n\n\t_ = db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"data\"))\n\t\trequire.NoError(t, err)\n\n\t\terr = b.Put([]byte(\"key\"), data)\n\t\trequire.NoError(t, err)\n\n\t\treturn nil\n\t})\n}\n\nfunc ExampleDB_Update() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Execute several commands within a read-write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Read the value back from a separate read-only transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value of 'foo' is: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release the file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value of 'foo' is: bar\n}\n\nfunc ExampleDB_View() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Insert data into a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"people\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"john\"), []byte(\"doe\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"susy\"), []byte(\"que\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Access data from within a read-only transactional block.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tv := tx.Bucket([]byte(\"people\")).Get([]byte(\"john\"))\n\t\tfmt.Printf(\"John's last name is %s.\\n\", v)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release the file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// John's last name is doe.\n}\n\nfunc ExampleDB_Begin() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Create a bucket using a read-write transaction.\n\tif err = db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Create several keys in a transaction.\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tb := tx.Bucket([]byte(\"widgets\"))\n\tif err = b.Put([]byte(\"john\"), []byte(\"blue\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err = b.Put([]byte(\"abby\"), []byte(\"red\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err = b.Put([]byte(\"zephyr\"), []byte(\"purple\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err = tx.Commit(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Iterate over the values in sorted key order.\n\ttx, err = db.Begin(false)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\tfmt.Printf(\"%s likes %s\\n\", k, v)\n\t}\n\n\tif err = tx.Rollback(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif err = db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// abby likes red\n\t// john likes blue\n\t// zephyr likes purple\n}\n\nfunc BenchmarkDBBatchAutomatic(b *testing.B) {\n\tdb := btesting.MustCreateDB(b)\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"bench\"))\n\t\treturn err\n\t}); err != nil {\n\t\tb.Fatal(err)\n\t}\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tstart := make(chan struct{})\n\t\tvar wg sync.WaitGroup\n\n\t\tfor round := 0; round < 1000; round++ {\n\t\t\twg.Add(1)\n\n\t\t\tgo func(id uint32) {\n\t\t\t\tdefer wg.Done()\n\t\t\t\t<-start\n\n\t\t\t\th := fnv.New32a()\n\t\t\t\tbuf := make([]byte, 4)\n\t\t\t\tbinary.LittleEndian.PutUint32(buf, id)\n\t\t\t\t_, _ = h.Write(buf[:])\n\t\t\t\tk := h.Sum(nil)\n\t\t\t\tinsert := func(tx *bolt.Tx) error {\n\t\t\t\t\tb := tx.Bucket([]byte(\"bench\"))\n\t\t\t\t\treturn b.Put(k, []byte(\"filler\"))\n\t\t\t\t}\n\t\t\t\tif err := db.Batch(insert); err != nil {\n\t\t\t\t\tb.Error(err)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}(uint32(round))\n\t\t}\n\t\tclose(start)\n\t\twg.Wait()\n\t}\n\n\tb.StopTimer()\n\tvalidateBatchBench(b, db)\n}\n\nfunc BenchmarkDBBatchSingle(b *testing.B) {\n\tdb := btesting.MustCreateDB(b)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"bench\"))\n\t\treturn err\n\t}); err != nil {\n\t\tb.Fatal(err)\n\t}\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tstart := make(chan struct{})\n\t\tvar wg sync.WaitGroup\n\n\t\tfor round := 0; round < 1000; round++ {\n\t\t\twg.Add(1)\n\t\t\tgo func(id uint32) {\n\t\t\t\tdefer wg.Done()\n\t\t\t\t<-start\n\n\t\t\t\th := fnv.New32a()\n\t\t\t\tbuf := make([]byte, 4)\n\t\t\t\tbinary.LittleEndian.PutUint32(buf, id)\n\t\t\t\t_, _ = h.Write(buf[:])\n\t\t\t\tk := h.Sum(nil)\n\t\t\t\tinsert := func(tx *bolt.Tx) error {\n\t\t\t\t\tb := tx.Bucket([]byte(\"bench\"))\n\t\t\t\t\treturn b.Put(k, []byte(\"filler\"))\n\t\t\t\t}\n\t\t\t\tif err := db.Update(insert); err != nil {\n\t\t\t\t\tb.Error(err)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}(uint32(round))\n\t\t}\n\t\tclose(start)\n\t\twg.Wait()\n\t}\n\n\tb.StopTimer()\n\tvalidateBatchBench(b, db)\n}\n\nfunc BenchmarkDBBatchManual10x100(b *testing.B) {\n\tdb := btesting.MustCreateDB(b)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"bench\"))\n\t\treturn err\n\t}); err != nil {\n\t\tb.Fatal(err)\n\t}\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tstart := make(chan struct{})\n\t\tvar wg sync.WaitGroup\n\t\terrCh := make(chan error, 10)\n\n\t\tfor major := 0; major < 10; major++ {\n\t\t\twg.Add(1)\n\t\t\tgo func(id uint32) {\n\t\t\t\tdefer wg.Done()\n\t\t\t\t<-start\n\n\t\t\t\tinsert100 := func(tx *bolt.Tx) error {\n\t\t\t\t\th := fnv.New32a()\n\t\t\t\t\tbuf := make([]byte, 4)\n\t\t\t\t\tfor minor := uint32(0); minor < 100; minor++ {\n\t\t\t\t\t\tbinary.LittleEndian.PutUint32(buf, uint32(id*100+minor))\n\t\t\t\t\t\th.Reset()\n\t\t\t\t\t\t_, _ = h.Write(buf[:])\n\t\t\t\t\t\tk := h.Sum(nil)\n\t\t\t\t\t\tb := tx.Bucket([]byte(\"bench\"))\n\t\t\t\t\t\tif err := b.Put(k, []byte(\"filler\")); err != nil {\n\t\t\t\t\t\t\treturn err\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\terr := db.Update(insert100)\n\t\t\t\terrCh <- err\n\t\t\t}(uint32(major))\n\t\t}\n\t\tclose(start)\n\t\twg.Wait()\n\t\tclose(errCh)\n\t\tfor err := range errCh {\n\t\t\tif err != nil {\n\t\t\t\tb.Fatal(err)\n\t\t\t}\n\t\t}\n\t}\n\n\tb.StopTimer()\n\tvalidateBatchBench(b, db)\n}\n\nfunc validateBatchBench(b *testing.B, db *btesting.DB) {\n\tvar rollback = errors.New(\"sentinel error to cause rollback\")\n\tvalidate := func(tx *bolt.Tx) error {\n\t\tbucket := tx.Bucket([]byte(\"bench\"))\n\t\th := fnv.New32a()\n\t\tbuf := make([]byte, 4)\n\t\tfor id := uint32(0); id < 1000; id++ {\n\t\t\tbinary.LittleEndian.PutUint32(buf, id)\n\t\t\th.Reset()\n\t\t\t_, _ = h.Write(buf[:])\n\t\t\tk := h.Sum(nil)\n\t\t\tv := bucket.Get(k)\n\t\t\tif v == nil {\n\t\t\t\tb.Errorf(\"not found id=%d key=%x\", id, k)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif g, e := v, []byte(\"filler\"); !bytes.Equal(g, e) {\n\t\t\t\tb.Errorf(\"bad value for id=%d key=%x: %s != %q\", id, k, g, e)\n\t\t\t}\n\t\t\tif err := bucket.Delete(k); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\t// should be empty now\n\t\tc := bucket.Cursor()\n\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\tb.Errorf(\"unexpected key: %x = %q\", k, v)\n\t\t}\n\t\treturn rollback\n\t}\n\tif err := db.Update(validate); err != nil && err != rollback {\n\t\tb.Error(err)\n\t}\n}\n\n// tempfile returns a temporary file path.\nfunc tempfile() string {\n\tf, err := os.CreateTemp(\"\", \"bolt-\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif err := f.Close(); err != nil {\n\t\tpanic(err)\n\t}\n\tif err := os.Remove(f.Name()); err != nil {\n\t\tpanic(err)\n\t}\n\treturn f.Name()\n}\n\nfunc trunc(b []byte, length int) []byte {\n\tif length < len(b) {\n\t\treturn b[:length]\n\t}\n\treturn b\n}\n\nfunc fileSize(path string) int64 {\n\tfi, err := os.Stat(path)\n\tif err != nil {\n\t\treturn 0\n\t}\n\treturn fi.Size()\n}\n\n// u64tob converts a uint64 into an 8-byte slice.\nfunc u64tob(v uint64) []byte {\n\tb := make([]byte, 8)\n\tbinary.BigEndian.PutUint64(b, v)\n\treturn b\n}\n"
  },
  {
    "path": "db_whitebox_test.go",
    "content": "package bbolt\n\nimport (\n\t\"path/filepath\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt/errors\"\n)\n\nfunc TestOpenWithPreLoadFreelist(t *testing.T) {\n\ttestCases := []struct {\n\t\tname                    string\n\t\treadonly                bool\n\t\tpreLoadFreePage         bool\n\t\texpectedFreePagesLoaded bool\n\t}{\n\t\t{\n\t\t\tname:                    \"write mode always load free pages\",\n\t\t\treadonly:                false,\n\t\t\tpreLoadFreePage:         false,\n\t\t\texpectedFreePagesLoaded: true,\n\t\t},\n\t\t{\n\t\t\tname:                    \"readonly mode load free pages when flag set\",\n\t\t\treadonly:                true,\n\t\t\tpreLoadFreePage:         true,\n\t\t\texpectedFreePagesLoaded: true,\n\t\t},\n\t\t{\n\t\t\tname:                    \"readonly mode doesn't load free pages when flag not set\",\n\t\t\treadonly:                true,\n\t\t\tpreLoadFreePage:         false,\n\t\t\texpectedFreePagesLoaded: false,\n\t\t},\n\t}\n\n\tfileName, err := prepareData(t)\n\trequire.NoError(t, err)\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdb, err := Open(fileName, 0666, &Options{\n\t\t\t\tReadOnly:        tc.readonly,\n\t\t\t\tPreLoadFreelist: tc.preLoadFreePage,\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\n\t\t\tassert.Equal(t, tc.expectedFreePagesLoaded, db.freelist != nil)\n\n\t\t\tassert.NoError(t, db.Close())\n\t\t})\n\t}\n}\n\nfunc TestMethodPage(t *testing.T) {\n\ttestCases := []struct {\n\t\tname            string\n\t\treadonly        bool\n\t\tpreLoadFreePage bool\n\t\texpectedError   error\n\t}{\n\t\t{\n\t\t\tname:            \"write mode\",\n\t\t\treadonly:        false,\n\t\t\tpreLoadFreePage: false,\n\t\t\texpectedError:   nil,\n\t\t},\n\t\t{\n\t\t\tname:            \"readonly mode with preloading free pages\",\n\t\t\treadonly:        true,\n\t\t\tpreLoadFreePage: true,\n\t\t\texpectedError:   nil,\n\t\t},\n\t\t{\n\t\t\tname:            \"readonly mode without preloading free pages\",\n\t\t\treadonly:        true,\n\t\t\tpreLoadFreePage: false,\n\t\t\texpectedError:   errors.ErrFreePagesNotLoaded,\n\t\t},\n\t}\n\n\tfileName, err := prepareData(t)\n\trequire.NoError(t, err)\n\n\tfor _, tc := range testCases {\n\t\ttc := tc\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdb, err := Open(fileName, 0666, &Options{\n\t\t\t\tReadOnly:        tc.readonly,\n\t\t\t\tPreLoadFreelist: tc.preLoadFreePage,\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\t\t\tdefer db.Close()\n\n\t\t\ttx, err := db.Begin(!tc.readonly)\n\t\t\trequire.NoError(t, err)\n\n\t\t\t_, err = tx.Page(0)\n\t\t\trequire.Equal(t, tc.expectedError, err)\n\n\t\t\tif tc.readonly {\n\t\t\t\trequire.NoError(t, tx.Rollback())\n\t\t\t} else {\n\t\t\t\trequire.NoError(t, tx.Commit())\n\t\t\t}\n\n\t\t\trequire.NoError(t, db.Close())\n\t\t})\n\t}\n}\n\nfunc prepareData(t *testing.T) (string, error) {\n\tfileName := filepath.Join(t.TempDir(), \"db\")\n\tdb, err := Open(fileName, 0666, nil)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif err := db.Close(); err != nil {\n\t\treturn \"\", err\n\t}\n\n\treturn fileName, nil\n}\n"
  },
  {
    "path": "doc.go",
    "content": "/*\npackage bbolt implements a low-level key/value store in pure Go. It supports\nfully serializable transactions, ACID semantics, and lock-free MVCC with\nmultiple readers and a single writer. Bolt can be used for projects that\nwant a simple data store without the need to add large dependencies such as\nPostgres or MySQL.\n\nBolt is a single-level, zero-copy, B+tree data store. This means that Bolt is\noptimized for fast read access and does not require recovery in the event of a\nsystem crash. Transactions which have not finished committing will simply be\nrolled back in the event of a crash.\n\nThe design of Bolt is based on Howard Chu's LMDB database project.\n\nBolt currently works on Windows, Mac OS X, and Linux.\n\n# Basics\n\nThere are only a few types in Bolt: DB, Bucket, Tx, and Cursor. The DB is\na collection of buckets and is represented by a single file on disk. A bucket is\na collection of unique keys that are associated with values.\n\nTransactions provide either read-only or read-write access to the database.\nRead-only transactions can retrieve key/value pairs and can use Cursors to\niterate over the dataset sequentially. Read-write transactions can create and\ndelete buckets and can insert and remove keys. Only one read-write transaction\nis allowed at a time.\n\n# Caveats\n\nThe database uses a read-only, memory-mapped data file to ensure that\napplications cannot corrupt the database, however, this means that keys and\nvalues returned from Bolt cannot be changed. Writing to a read-only byte slice\nwill cause Go to panic.\n\nKeys and values retrieved from the database are only valid for the life of\nthe transaction. When used outside the transaction, these byte slices can\npoint to different data or can point to invalid memory which will cause a panic.\n*/\npackage bbolt\n"
  },
  {
    "path": "errors/errors.go",
    "content": "// Package errors defines the error variables that may be returned\n// during bbolt operations.\npackage errors\n\nimport \"errors\"\n\n// These errors can be returned when opening or calling methods on a DB.\nvar (\n\t// ErrDatabaseNotOpen is returned when a DB instance is accessed before it\n\t// is opened or after it is closed.\n\tErrDatabaseNotOpen = errors.New(\"database not open\")\n\n\t// ErrInvalid is returned when both meta pages on a database are invalid.\n\t// This typically occurs when a file is not a bolt database.\n\tErrInvalid = errors.New(\"invalid database\")\n\n\t// ErrInvalidMapping is returned when the database file fails to get mapped.\n\tErrInvalidMapping = errors.New(\"database isn't correctly mapped\")\n\n\t// ErrVersionMismatch is returned when the data file was created with a\n\t// different version of Bolt.\n\tErrVersionMismatch = errors.New(\"version mismatch\")\n\n\t// ErrChecksum is returned when a checksum mismatch occurs on either of the two meta pages.\n\tErrChecksum = errors.New(\"checksum error\")\n\n\t// ErrTimeout is returned when a database cannot obtain an exclusive lock\n\t// on the data file after the timeout passed to Open().\n\tErrTimeout = errors.New(\"timeout\")\n)\n\n// These errors can occur when beginning or committing a Tx.\nvar (\n\t// ErrTxNotWritable is returned when performing a write operation on a\n\t// read-only transaction.\n\tErrTxNotWritable = errors.New(\"tx not writable\")\n\n\t// ErrTxClosed is returned when committing or rolling back a transaction\n\t// that has already been committed or rolled back.\n\tErrTxClosed = errors.New(\"tx closed\")\n\n\t// ErrDatabaseReadOnly is returned when a mutating transaction is started on a\n\t// read-only database.\n\tErrDatabaseReadOnly = errors.New(\"database is in read-only mode\")\n\n\t// ErrFreePagesNotLoaded is returned when a readonly transaction without\n\t// preloading the free pages is trying to access the free pages.\n\tErrFreePagesNotLoaded = errors.New(\"free pages are not pre-loaded\")\n)\n\n// These errors can occur when putting or deleting a value or a bucket.\nvar (\n\t// ErrBucketNotFound is returned when trying to access a bucket that has\n\t// not been created yet.\n\tErrBucketNotFound = errors.New(\"bucket not found\")\n\n\t// ErrBucketExists is returned when creating a bucket that already exists.\n\tErrBucketExists = errors.New(\"bucket already exists\")\n\n\t// ErrBucketNameRequired is returned when creating a bucket with a blank name.\n\tErrBucketNameRequired = errors.New(\"bucket name required\")\n\n\t// ErrKeyRequired is returned when inserting a zero-length key.\n\tErrKeyRequired = errors.New(\"key required\")\n\n\t// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.\n\tErrKeyTooLarge = errors.New(\"key too large\")\n\n\t// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.\n\tErrValueTooLarge = errors.New(\"value too large\")\n\n\t// ErrMaxSizeReached is returned when the configured maximum size of the data file is reached.\n\tErrMaxSizeReached = errors.New(\"database reached maximum size\")\n\n\t// ErrIncompatibleValue is returned when trying to create or delete a bucket\n\t// on an existing non-bucket key or when trying to create or delete a\n\t// non-bucket key on an existing bucket key.\n\tErrIncompatibleValue = errors.New(\"incompatible value\")\n\n\t// ErrSameBuckets is returned when trying to move a sub-bucket between\n\t// source and target buckets, while source and target buckets are the same.\n\tErrSameBuckets = errors.New(\"the source and target are the same bucket\")\n\n\t// ErrDifferentDB is returned when trying to move a sub-bucket between\n\t// source and target buckets, while source and target buckets are in different database files.\n\tErrDifferentDB = errors.New(\"the source and target buckets are in different database files\")\n)\n"
  },
  {
    "path": "errors.go",
    "content": "package bbolt\n\nimport \"go.etcd.io/bbolt/errors\"\n\n// These errors can be returned when opening or calling methods on a DB.\nvar (\n\t// ErrDatabaseNotOpen is returned when a DB instance is accessed before it\n\t// is opened or after it is closed.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrDatabaseNotOpen = errors.ErrDatabaseNotOpen\n\n\t// ErrInvalid is returned when both meta pages on a database are invalid.\n\t// This typically occurs when a file is not a bolt database.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrInvalid = errors.ErrInvalid\n\n\t// ErrInvalidMapping is returned when the database file fails to get mapped.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrInvalidMapping = errors.ErrInvalidMapping\n\n\t// ErrVersionMismatch is returned when the data file was created with a\n\t// different version of Bolt.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrVersionMismatch = errors.ErrVersionMismatch\n\n\t// ErrChecksum is returned when a checksum mismatch occurs on either of the two meta pages.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrChecksum = errors.ErrChecksum\n\n\t// ErrTimeout is returned when a database cannot obtain an exclusive lock\n\t// on the data file after the timeout passed to Open().\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrTimeout = errors.ErrTimeout\n)\n\n// These errors can occur when beginning or committing a Tx.\nvar (\n\t// ErrTxNotWritable is returned when performing a write operation on a\n\t// read-only transaction.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrTxNotWritable = errors.ErrTxNotWritable\n\n\t// ErrTxClosed is returned when committing or rolling back a transaction\n\t// that has already been committed or rolled back.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrTxClosed = errors.ErrTxClosed\n\n\t// ErrDatabaseReadOnly is returned when a mutating transaction is started on a\n\t// read-only database.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrDatabaseReadOnly = errors.ErrDatabaseReadOnly\n\n\t// ErrFreePagesNotLoaded is returned when a readonly transaction without\n\t// preloading the free pages is trying to access the free pages.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrFreePagesNotLoaded = errors.ErrFreePagesNotLoaded\n)\n\n// These errors can occur when putting or deleting a value or a bucket.\nvar (\n\t// ErrBucketNotFound is returned when trying to access a bucket that has\n\t// not been created yet.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrBucketNotFound = errors.ErrBucketNotFound\n\n\t// ErrBucketExists is returned when creating a bucket that already exists.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrBucketExists = errors.ErrBucketExists\n\n\t// ErrBucketNameRequired is returned when creating a bucket with a blank name.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrBucketNameRequired = errors.ErrBucketNameRequired\n\n\t// ErrKeyRequired is returned when inserting a zero-length key.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrKeyRequired = errors.ErrKeyRequired\n\n\t// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrKeyTooLarge = errors.ErrKeyTooLarge\n\n\t// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrValueTooLarge = errors.ErrValueTooLarge\n\n\t// ErrIncompatibleValue is returned when trying create or delete a bucket\n\t// on an existing non-bucket key or when trying to create or delete a\n\t// non-bucket key on an existing bucket key.\n\t//\n\t// Deprecated: Use the error variables defined in the bbolt/errors package.\n\tErrIncompatibleValue = errors.ErrIncompatibleValue\n)\n"
  },
  {
    "path": "go.mod",
    "content": "module go.etcd.io/bbolt\n\ngo 1.24.0\n\ntoolchain go1.24.13\n\nrequire (\n\tgithub.com/spf13/cobra v1.10.2\n\tgithub.com/spf13/pflag v1.0.10\n\tgithub.com/stretchr/testify v1.11.1\n\tgo.etcd.io/gofail v0.2.0\n\tgolang.org/x/sync v0.19.0\n\tgolang.org/x/sys v0.41.0\n)\n\nrequire (\n\tgithub.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 // indirect\n\tgithub.com/davecgh/go-spew v1.1.1 // indirect\n\tgithub.com/inconshreveable/mousetrap v1.1.0 // indirect\n\tgithub.com/pmezard/go-difflib v1.0.0 // indirect\n\tgolang.org/x/mod v0.27.0 // indirect\n\tgolang.org/x/perf v0.0.0-20250813145418-2f7363a06fe1 // indirect\n\tgolang.org/x/tools v0.36.0 // indirect\n\tgopkg.in/yaml.v3 v3.0.1 // indirect\n)\n\ntool (\n\tgo.etcd.io/gofail\n\tgolang.org/x/perf/cmd/benchstat\n\tgolang.org/x/tools/cmd/goimports\n)\n"
  },
  {
    "path": "go.sum",
    "content": "github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 h1:xlwdaKcTNVW4PtpQb8aKA4Pjy0CdJHEqvFbAnvR5m2g=\ngithub.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794/go.mod h1:7e+I0LQFUI9AXWxOfsQROs9xPhoJtbsyWcjJqDd4KPY=\ngithub.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=\ngithub.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=\ngithub.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=\ngithub.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=\ngithub.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=\ngithub.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=\ngithub.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=\ngithub.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=\ngithub.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=\ngithub.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=\ngithub.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=\ngithub.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=\ngithub.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=\ngithub.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=\ngithub.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=\ngithub.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=\ngithub.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=\ngo.etcd.io/gofail v0.2.0 h1:p19drv16FKK345a09a1iubchlw/vmRuksmRzgBIGjcA=\ngo.etcd.io/gofail v0.2.0/go.mod h1:nL3ILMGfkXTekKI3clMBNazKnjUZjYLKmBHzsVAnC1o=\ngo.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=\ngolang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=\ngolang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=\ngolang.org/x/perf v0.0.0-20250813145418-2f7363a06fe1 h1:stGRioFgvBd3x8HoGVg9bb41lLTWLjBMFT/dMB7f4mQ=\ngolang.org/x/perf v0.0.0-20250813145418-2f7363a06fe1/go.mod h1:rjfRjhHXb3XNVh/9i5Jr2tXoTd0vOlZN5rzsM8cQE6k=\ngolang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=\ngolang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=\ngolang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=\ngolang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=\ngolang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=\ngolang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=\ngopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=\ngopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=\ngopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=\ngopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=\n"
  },
  {
    "path": "internal/btesting/btesting.go",
    "content": "package btesting\n\nimport (\n\t\"flag\"\n\t\"fmt\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"regexp\"\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nvar statsFlag = flag.Bool(\"stats\", false, \"show performance stats\")\n\nconst (\n\t// TestFreelistType is used as an env variable for test to indicate the backend type.\n\tTestFreelistType = \"TEST_FREELIST_TYPE\"\n\t// TestEnableStrictMode is used to enable strict check by default after opening each DB.\n\tTestEnableStrictMode = \"TEST_ENABLE_STRICT_MODE\"\n)\n\n// DB is a test wrapper for bolt.DB.\ntype DB struct {\n\t*bolt.DB\n\tf string\n\to *bolt.Options\n\tt testing.TB\n}\n\n// MustCreateDB returns a new, open DB at a temporary location.\nfunc MustCreateDB(t testing.TB) *DB {\n\treturn MustCreateDBWithOption(t, nil)\n}\n\n// MustCreateDBWithOption returns a new, open DB at a temporary location with given options.\nfunc MustCreateDBWithOption(t testing.TB, o *bolt.Options) *DB {\n\tf := filepath.Join(t.TempDir(), \"db\")\n\treturn MustOpenDBWithOption(t, f, o)\n}\n\nfunc MustOpenDBWithOption(t testing.TB, f string, o *bolt.Options) *DB {\n\tdb, err := OpenDBWithOption(t, f, o)\n\trequire.NoError(t, err)\n\trequire.NotNil(t, db)\n\treturn db\n}\n\nfunc OpenDBWithOption(t testing.TB, f string, o *bolt.Options) (*DB, error) {\n\tt.Logf(\"Opening bbolt DB at: %s\", f)\n\tif o == nil {\n\t\to = bolt.DefaultOptions\n\t}\n\n\tfreelistType := bolt.FreelistArrayType\n\tif env := os.Getenv(TestFreelistType); env == string(bolt.FreelistMapType) {\n\t\tfreelistType = bolt.FreelistMapType\n\t}\n\n\to.FreelistType = freelistType\n\n\tdb, err := bolt.Open(f, 0600, o)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tresDB := &DB{\n\t\tDB: db,\n\t\tf:  f,\n\t\to:  o,\n\t\tt:  t,\n\t}\n\tresDB.strictModeEnabledDefault()\n\tt.Cleanup(resDB.PostTestCleanup)\n\treturn resDB, nil\n}\n\nfunc (db *DB) PostTestCleanup() {\n\t// Check database consistency after every test.\n\tif db.DB != nil {\n\t\tdb.MustCheck()\n\t\tdb.MustClose()\n\t}\n}\n\n// Close closes the database but does NOT delete the underlying file.\nfunc (db *DB) Close() error {\n\tif db.DB != nil {\n\t\t// Log statistics.\n\t\tif *statsFlag {\n\t\t\tdb.PrintStats()\n\t\t}\n\t\tdb.t.Logf(\"Closing bbolt DB at: %s\", db.f)\n\t\terr := db.DB.Close()\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdb.DB = nil\n\t}\n\treturn nil\n}\n\n// MustClose closes the database but does NOT delete the underlying file.\nfunc (db *DB) MustClose() {\n\terr := db.Close()\n\trequire.NoError(db.t, err)\n}\n\nfunc (db *DB) MustDeleteFile() {\n\terr := os.Remove(db.Path())\n\trequire.NoError(db.t, err)\n}\n\nfunc (db *DB) SetOptions(o *bolt.Options) {\n\tdb.o = o\n}\n\n// MustReopen reopen the database. Panic on error.\nfunc (db *DB) MustReopen() {\n\tif db.DB != nil {\n\t\tpanic(\"Please call Close() before MustReopen()\")\n\t}\n\tdb.t.Logf(\"Reopening bbolt DB at: %s\", db.f)\n\tindb, err := bolt.Open(db.Path(), 0600, db.o)\n\trequire.NoError(db.t, err)\n\tdb.DB = indb\n\tdb.strictModeEnabledDefault()\n}\n\n// MustCheck runs a consistency check on the database and panics if any errors are found.\nfunc (db *DB) MustCheck() {\n\terr := db.View(func(tx *bolt.Tx) error {\n\t\t// Collect all the errors.\n\t\tvar errors []error\n\t\tfor err := range tx.Check() {\n\t\t\terrors = append(errors, err)\n\t\t\tif len(errors) > 10 {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\t// If errors occurred, copy the DB and print the errors.\n\t\tif len(errors) > 0 {\n\t\t\tvar path = filepath.Join(db.t.TempDir(), \"db.backup\")\n\t\t\terr := tx.CopyFile(path, 0600)\n\t\t\trequire.NoError(db.t, err)\n\n\t\t\t// Print errors.\n\t\t\tfmt.Print(\"\\n\\n\")\n\t\t\tfmt.Printf(\"consistency check failed (%d errors)\\n\", len(errors))\n\t\t\tfor _, err := range errors {\n\t\t\t\tfmt.Println(err)\n\t\t\t}\n\t\t\tfmt.Println(\"\")\n\t\t\tfmt.Println(\"db saved to:\")\n\t\t\tfmt.Println(path)\n\t\t\tfmt.Print(\"\\n\\n\")\n\t\t\tos.Exit(-1)\n\t\t}\n\n\t\treturn nil\n\t})\n\trequire.NoError(db.t, err)\n}\n\n// Fill - fills the DB using numTx transactions and numKeysPerTx.\nfunc (db *DB) Fill(bucket []byte, numTx int, numKeysPerTx int,\n\tkeyGen func(tx int, key int) []byte,\n\tvalueGen func(tx int, key int) []byte) error {\n\tfor tr := 0; tr < numTx; tr++ {\n\t\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, _ := tx.CreateBucketIfNotExists(bucket)\n\t\t\tfor i := 0; i < numKeysPerTx; i++ {\n\t\t\t\tif err := b.Put(keyGen(tr, i), valueGen(tr, i)); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (db *DB) Path() string {\n\treturn db.f\n}\n\n// CopyTempFile copies a database to a temporary file.\nfunc (db *DB) CopyTempFile() {\n\tpath := filepath.Join(db.t.TempDir(), \"db.copy\")\n\terr := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.CopyFile(path, 0600)\n\t})\n\trequire.NoError(db.t, err)\n\tfmt.Println(\"db copied to: \", path)\n}\n\n// PrintStats prints the database stats\nfunc (db *DB) PrintStats() {\n\tvar stats = db.Stats()\n\tfmt.Printf(\"[db] %-20s %-20s %-20s\\n\",\n\t\tfmt.Sprintf(\"pg(%d/%d)\", stats.TxStats.GetPageCount(), stats.TxStats.GetPageAlloc()),\n\t\tfmt.Sprintf(\"cur(%d)\", stats.TxStats.GetCursorCount()),\n\t\tfmt.Sprintf(\"node(%d/%d)\", stats.TxStats.GetNodeCount(), stats.TxStats.GetNodeDeref()),\n\t)\n\tfmt.Printf(\"     %-20s %-20s %-20s\\n\",\n\t\tfmt.Sprintf(\"rebal(%d/%v)\", stats.TxStats.GetRebalance(), truncDuration(stats.TxStats.GetRebalanceTime())),\n\t\tfmt.Sprintf(\"spill(%d/%v)\", stats.TxStats.GetSpill(), truncDuration(stats.TxStats.GetSpillTime())),\n\t\tfmt.Sprintf(\"w(%d/%v)\", stats.TxStats.GetWrite(), truncDuration(stats.TxStats.GetWriteTime())),\n\t)\n}\n\nfunc truncDuration(d time.Duration) string {\n\treturn regexp.MustCompile(`^(\\d+)(\\.\\d+)`).ReplaceAllString(d.String(), \"$1\")\n}\n\nfunc (db *DB) strictModeEnabledDefault() {\n\tstrictModeEnabled := strings.ToLower(os.Getenv(TestEnableStrictMode))\n\tdb.StrictMode = strictModeEnabled == \"true\"\n}\n\nfunc (db *DB) ForceDisableStrictMode() {\n\tdb.StrictMode = false\n}\n"
  },
  {
    "path": "internal/common/bolt_386.go",
    "content": "package common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0x7FFFFFFF // 2GB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0xFFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_amd64.go",
    "content": "package common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_arm.go",
    "content": "package common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0x7FFFFFFF // 2GB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0xFFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_arm64.go",
    "content": "//go:build arm64\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_loong64.go",
    "content": "//go:build loong64\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_mips64x.go",
    "content": "//go:build mips64 || mips64le\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0x8000000000 // 512GB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_mipsx.go",
    "content": "//go:build mips || mipsle\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0x40000000 // 1GB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0xFFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_ppc.go",
    "content": "//go:build ppc\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0x7FFFFFFF // 2GB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0xFFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_ppc64.go",
    "content": "//go:build ppc64\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_ppc64le.go",
    "content": "//go:build ppc64le\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_riscv64.go",
    "content": "//go:build riscv64\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bolt_s390x.go",
    "content": "//go:build s390x\n\npackage common\n\n// MaxMapSize represents the largest mmap size supported by Bolt.\nconst MaxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// MaxAllocSize is the size used when creating array pointers.\nconst MaxAllocSize = 0x7FFFFFFF\n"
  },
  {
    "path": "internal/common/bucket.go",
    "content": "package common\n\nimport (\n\t\"fmt\"\n\t\"unsafe\"\n)\n\nconst BucketHeaderSize = int(unsafe.Sizeof(InBucket{}))\n\n// InBucket represents the on-file representation of a bucket.\n// This is stored as the \"value\" of a bucket key. If the bucket is small enough,\n// then its root page can be stored inline in the \"value\", after the bucket\n// header. In the case of inline buckets, the \"root\" will be 0.\ntype InBucket struct {\n\troot     Pgid   // page id of the bucket's root-level page\n\tsequence uint64 // monotonically incrementing, used by NextSequence()\n}\n\nfunc NewInBucket(root Pgid, seq uint64) InBucket {\n\treturn InBucket{\n\t\troot:     root,\n\t\tsequence: seq,\n\t}\n}\n\nfunc (b *InBucket) RootPage() Pgid {\n\treturn b.root\n}\n\nfunc (b *InBucket) SetRootPage(id Pgid) {\n\tb.root = id\n}\n\n// InSequence returns the sequence. The reason why not naming it `Sequence`\n// is to avoid duplicated name as `(*Bucket) Sequence()`\nfunc (b *InBucket) InSequence() uint64 {\n\treturn b.sequence\n}\n\nfunc (b *InBucket) SetInSequence(v uint64) {\n\tb.sequence = v\n}\n\nfunc (b *InBucket) IncSequence() {\n\tb.sequence++\n}\n\nfunc (b *InBucket) InlinePage(v []byte) *Page {\n\treturn (*Page)(unsafe.Pointer(&v[BucketHeaderSize]))\n}\n\nfunc (b *InBucket) String() string {\n\treturn fmt.Sprintf(\"<pgid=%d,seq=%d>\", b.root, b.sequence)\n}\n"
  },
  {
    "path": "internal/common/inode.go",
    "content": "package common\n\nimport \"unsafe\"\n\n// Inode represents an internal node inside of a node.\n// It can be used to point to elements in a page or point\n// to an element which hasn't been added to a page yet.\ntype Inode struct {\n\tflags uint32\n\tpgid  Pgid\n\tkey   []byte\n\tvalue []byte\n}\n\ntype Inodes []Inode\n\nfunc (in *Inode) Flags() uint32 {\n\treturn in.flags\n}\n\nfunc (in *Inode) SetFlags(flags uint32) {\n\tin.flags = flags\n}\n\nfunc (in *Inode) Pgid() Pgid {\n\treturn in.pgid\n}\n\nfunc (in *Inode) SetPgid(id Pgid) {\n\tin.pgid = id\n}\n\nfunc (in *Inode) Key() []byte {\n\treturn in.key\n}\n\nfunc (in *Inode) SetKey(key []byte) {\n\tin.key = key\n}\n\nfunc (in *Inode) Value() []byte {\n\treturn in.value\n}\n\nfunc (in *Inode) SetValue(value []byte) {\n\tin.value = value\n}\n\nfunc ReadInodeFromPage(p *Page) Inodes {\n\tinodes := make(Inodes, int(p.Count()))\n\tisLeaf := p.IsLeafPage()\n\tfor i := 0; i < int(p.Count()); i++ {\n\t\tinode := &inodes[i]\n\t\tif isLeaf {\n\t\t\telem := p.LeafPageElement(uint16(i))\n\t\t\tinode.SetFlags(elem.Flags())\n\t\t\tinode.SetKey(elem.Key())\n\t\t\tinode.SetValue(elem.Value())\n\t\t} else {\n\t\t\telem := p.BranchPageElement(uint16(i))\n\t\t\tinode.SetPgid(elem.Pgid())\n\t\t\tinode.SetKey(elem.Key())\n\t\t}\n\t\tAssert(len(inode.Key()) > 0, \"read: zero-length inode key\")\n\t}\n\n\treturn inodes\n}\n\nfunc WriteInodeToPage(inodes Inodes, p *Page) uint32 {\n\t// Loop over each item and write it to the page.\n\t// off tracks the offset into p of the start of the next data.\n\toff := unsafe.Sizeof(*p) + p.PageElementSize()*uintptr(len(inodes))\n\tisLeaf := p.IsLeafPage()\n\tfor i, item := range inodes {\n\t\tAssert(len(item.Key()) > 0, \"write: zero-length inode key\")\n\n\t\t// Create a slice to write into of needed size and advance\n\t\t// byte pointer for next iteration.\n\t\tsz := len(item.Key()) + len(item.Value())\n\t\tb := UnsafeByteSlice(unsafe.Pointer(p), off, 0, sz)\n\t\toff += uintptr(sz)\n\n\t\t// Write the page element.\n\t\tif isLeaf {\n\t\t\telem := p.LeafPageElement(uint16(i))\n\t\t\telem.SetPos(uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem))))\n\t\t\telem.SetFlags(item.Flags())\n\t\t\telem.SetKsize(uint32(len(item.Key())))\n\t\t\telem.SetVsize(uint32(len(item.Value())))\n\t\t} else {\n\t\t\telem := p.BranchPageElement(uint16(i))\n\t\t\telem.SetPos(uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem))))\n\t\t\telem.SetKsize(uint32(len(item.Key())))\n\t\t\telem.SetPgid(item.Pgid())\n\t\t\tAssert(elem.Pgid() != p.Id(), \"write: circular dependency occurred\")\n\t\t}\n\n\t\t// Write data for the element to the end of the page.\n\t\tl := copy(b, item.Key())\n\t\tcopy(b[l:], item.Value())\n\t}\n\n\treturn uint32(off)\n}\n\nfunc UsedSpaceInPage(inodes Inodes, p *Page) uint32 {\n\toff := unsafe.Sizeof(*p) + p.PageElementSize()*uintptr(len(inodes))\n\tfor _, item := range inodes {\n\t\tsz := len(item.Key()) + len(item.Value())\n\t\toff += uintptr(sz)\n\t}\n\n\treturn uint32(off)\n}\n"
  },
  {
    "path": "internal/common/meta.go",
    "content": "package common\n\nimport (\n\t\"fmt\"\n\t\"hash/fnv\"\n\t\"io\"\n\t\"unsafe\"\n\n\t\"go.etcd.io/bbolt/errors\"\n)\n\ntype Meta struct {\n\tmagic    uint32\n\tversion  uint32\n\tpageSize uint32\n\tflags    uint32\n\troot     InBucket\n\tfreelist Pgid\n\tpgid     Pgid\n\ttxid     Txid\n\tchecksum uint64\n}\n\n// Validate checks the marker bytes and version of the meta page to ensure it matches this binary.\nfunc (m *Meta) Validate() error {\n\tif m.magic != Magic {\n\t\treturn errors.ErrInvalid\n\t} else if m.version != Version {\n\t\treturn errors.ErrVersionMismatch\n\t} else if m.checksum != m.Sum64() {\n\t\treturn errors.ErrChecksum\n\t}\n\treturn nil\n}\n\n// Copy copies one meta object to another.\nfunc (m *Meta) Copy(dest *Meta) {\n\t*dest = *m\n}\n\n// Write writes the meta onto a page.\nfunc (m *Meta) Write(p *Page) {\n\tif m.root.root >= m.pgid {\n\t\tpanic(fmt.Sprintf(\"root bucket pgid (%d) above high water mark (%d)\", m.root.root, m.pgid))\n\t} else if m.freelist >= m.pgid && m.freelist != PgidNoFreelist {\n\t\t// TODO: reject pgidNoFreeList if !NoFreelistSync\n\t\tpanic(fmt.Sprintf(\"freelist pgid (%d) above high water mark (%d)\", m.freelist, m.pgid))\n\t}\n\n\t// Page id is either going to be 0 or 1 which we can determine by the transaction ID.\n\tp.id = Pgid(m.txid % 2)\n\tp.SetFlags(MetaPageFlag)\n\n\t// Calculate the checksum.\n\tm.checksum = m.Sum64()\n\n\tm.Copy(p.Meta())\n}\n\n// Sum64 generates the checksum for the meta.\nfunc (m *Meta) Sum64() uint64 {\n\tvar h = fnv.New64a()\n\t_, _ = h.Write((*[unsafe.Offsetof(Meta{}.checksum)]byte)(unsafe.Pointer(m))[:])\n\treturn h.Sum64()\n}\n\nfunc (m *Meta) Magic() uint32 {\n\treturn m.magic\n}\n\nfunc (m *Meta) SetMagic(v uint32) {\n\tm.magic = v\n}\n\nfunc (m *Meta) Version() uint32 {\n\treturn m.version\n}\n\nfunc (m *Meta) SetVersion(v uint32) {\n\tm.version = v\n}\n\nfunc (m *Meta) PageSize() uint32 {\n\treturn m.pageSize\n}\n\nfunc (m *Meta) SetPageSize(v uint32) {\n\tm.pageSize = v\n}\n\nfunc (m *Meta) Flags() uint32 {\n\treturn m.flags\n}\n\nfunc (m *Meta) SetFlags(v uint32) {\n\tm.flags = v\n}\n\nfunc (m *Meta) SetRootBucket(b InBucket) {\n\tm.root = b\n}\n\nfunc (m *Meta) RootBucket() *InBucket {\n\treturn &m.root\n}\n\nfunc (m *Meta) Freelist() Pgid {\n\treturn m.freelist\n}\n\nfunc (m *Meta) SetFreelist(v Pgid) {\n\tm.freelist = v\n}\n\nfunc (m *Meta) IsFreelistPersisted() bool {\n\treturn m.freelist != PgidNoFreelist\n}\n\nfunc (m *Meta) Pgid() Pgid {\n\treturn m.pgid\n}\n\nfunc (m *Meta) SetPgid(id Pgid) {\n\tm.pgid = id\n}\n\nfunc (m *Meta) Txid() Txid {\n\treturn m.txid\n}\n\nfunc (m *Meta) SetTxid(id Txid) {\n\tm.txid = id\n}\n\nfunc (m *Meta) IncTxid() {\n\tm.txid += 1\n}\n\nfunc (m *Meta) DecTxid() {\n\tm.txid -= 1\n}\n\nfunc (m *Meta) Checksum() uint64 {\n\treturn m.checksum\n}\n\nfunc (m *Meta) SetChecksum(v uint64) {\n\tm.checksum = v\n}\n\nfunc (m *Meta) Print(w io.Writer) {\n\tfmt.Fprintf(w, \"Version:    %d\\n\", m.version)\n\tfmt.Fprintf(w, \"Page Size:  %d bytes\\n\", m.pageSize)\n\tfmt.Fprintf(w, \"Flags:      %08x\\n\", m.flags)\n\tfmt.Fprintf(w, \"Root:       <pgid=%d>\\n\", m.root.root)\n\tfmt.Fprintf(w, \"Freelist:   <pgid=%d>\\n\", m.freelist)\n\tfmt.Fprintf(w, \"HWM:        <pgid=%d>\\n\", m.pgid)\n\tfmt.Fprintf(w, \"Txn ID:     %d\\n\", m.txid)\n\tfmt.Fprintf(w, \"Checksum:   %016x\\n\", m.checksum)\n\tfmt.Fprintf(w, \"\\n\")\n}\n"
  },
  {
    "path": "internal/common/page.go",
    "content": "package common\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"sort\"\n\t\"unsafe\"\n)\n\nconst PageHeaderSize = unsafe.Sizeof(Page{})\n\nconst MinKeysPerPage = 2\n\nconst BranchPageElementSize = unsafe.Sizeof(branchPageElement{})\nconst LeafPageElementSize = unsafe.Sizeof(leafPageElement{})\nconst pgidSize = unsafe.Sizeof(Pgid(0))\n\nconst (\n\tBranchPageFlag   = 0x01\n\tLeafPageFlag     = 0x02\n\tMetaPageFlag     = 0x04\n\tFreelistPageFlag = 0x10\n)\n\nconst (\n\tBucketLeafFlag = 0x01\n)\n\ntype Pgid uint64\n\ntype Page struct {\n\tid       Pgid\n\tflags    uint16\n\tcount    uint16\n\toverflow uint32\n}\n\nfunc NewPage(id Pgid, flags, count uint16, overflow uint32) *Page {\n\treturn &Page{\n\t\tid:       id,\n\t\tflags:    flags,\n\t\tcount:    count,\n\t\toverflow: overflow,\n\t}\n}\n\n// Typ returns a human-readable page type string used for debugging.\nfunc (p *Page) Typ() string {\n\tif p.IsBranchPage() {\n\t\treturn \"branch\"\n\t} else if p.IsLeafPage() {\n\t\treturn \"leaf\"\n\t} else if p.IsMetaPage() {\n\t\treturn \"meta\"\n\t} else if p.IsFreelistPage() {\n\t\treturn \"freelist\"\n\t}\n\treturn fmt.Sprintf(\"unknown<%02x>\", p.flags)\n}\n\nfunc (p *Page) IsBranchPage() bool {\n\treturn p.flags == BranchPageFlag\n}\n\nfunc (p *Page) IsLeafPage() bool {\n\treturn p.flags == LeafPageFlag\n}\n\nfunc (p *Page) IsMetaPage() bool {\n\treturn p.flags == MetaPageFlag\n}\n\nfunc (p *Page) IsFreelistPage() bool {\n\treturn p.flags == FreelistPageFlag\n}\n\n// Meta returns a pointer to the metadata section of the page.\nfunc (p *Page) Meta() *Meta {\n\treturn (*Meta)(UnsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p)))\n}\n\nfunc (p *Page) FastCheck(id Pgid) {\n\tAssert(p.id == id, \"Page expected to be: %v, but self identifies as %v\", id, p.id)\n\t// Only one flag of page-type can be set.\n\tAssert(p.IsBranchPage() ||\n\t\tp.IsLeafPage() ||\n\t\tp.IsMetaPage() ||\n\t\tp.IsFreelistPage(),\n\t\t\"page %v: has unexpected type/flags: %x\", p.id, p.flags)\n}\n\n// LeafPageElement retrieves the leaf node by index\nfunc (p *Page) LeafPageElement(index uint16) *leafPageElement {\n\treturn (*leafPageElement)(UnsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p),\n\t\tLeafPageElementSize, int(index)))\n}\n\n// LeafPageElements retrieves a list of leaf nodes.\nfunc (p *Page) LeafPageElements() []leafPageElement {\n\tif p.count == 0 {\n\t\treturn nil\n\t}\n\tdata := UnsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))\n\telems := unsafe.Slice((*leafPageElement)(data), int(p.count))\n\treturn elems\n}\n\n// BranchPageElement retrieves the branch node by index\nfunc (p *Page) BranchPageElement(index uint16) *branchPageElement {\n\treturn (*branchPageElement)(UnsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p),\n\t\tunsafe.Sizeof(branchPageElement{}), int(index)))\n}\n\n// BranchPageElements retrieves a list of branch nodes.\nfunc (p *Page) BranchPageElements() []branchPageElement {\n\tif p.count == 0 {\n\t\treturn nil\n\t}\n\tdata := UnsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))\n\telems := unsafe.Slice((*branchPageElement)(data), int(p.count))\n\treturn elems\n}\n\nfunc (p *Page) FreelistPageCount() (int, int) {\n\tAssert(p.IsFreelistPage(), fmt.Sprintf(\"can't get freelist page count from a non-freelist page: %2x\", p.flags))\n\n\t// If the page.count is at the max uint16 value (64k) then it's considered\n\t// an overflow and the size of the freelist is stored as the first element.\n\tvar idx, count = 0, int(p.count)\n\tif count == 0xFFFF {\n\t\tidx = 1\n\t\tc := *(*Pgid)(UnsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p)))\n\t\tcount = int(c)\n\t\tif count < 0 {\n\t\t\tpanic(fmt.Sprintf(\"leading element count %d overflows int\", c))\n\t\t}\n\t}\n\n\treturn idx, count\n}\n\nfunc (p *Page) FreelistPageIds() []Pgid {\n\tAssert(p.IsFreelistPage(), fmt.Sprintf(\"can't get freelist page IDs from a non-freelist page: %2x\", p.flags))\n\n\tidx, count := p.FreelistPageCount()\n\n\tif count == 0 {\n\t\treturn nil\n\t}\n\n\tdata := UnsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p), pgidSize, idx)\n\tids := unsafe.Slice((*Pgid)(data), count)\n\n\treturn ids\n}\n\n// dump writes n bytes of the page to STDERR as hex output.\nfunc (p *Page) hexdump(n int) {\n\tbuf := UnsafeByteSlice(unsafe.Pointer(p), 0, 0, n)\n\tfmt.Fprintf(os.Stderr, \"%x\\n\", buf)\n}\n\nfunc (p *Page) PageElementSize() uintptr {\n\tif p.IsLeafPage() {\n\t\treturn LeafPageElementSize\n\t}\n\treturn BranchPageElementSize\n}\n\nfunc (p *Page) Id() Pgid {\n\treturn p.id\n}\n\nfunc (p *Page) SetId(target Pgid) {\n\tp.id = target\n}\n\nfunc (p *Page) Flags() uint16 {\n\treturn p.flags\n}\n\nfunc (p *Page) SetFlags(v uint16) {\n\tp.flags = v\n}\n\nfunc (p *Page) Count() uint16 {\n\treturn p.count\n}\n\nfunc (p *Page) SetCount(target uint16) {\n\tp.count = target\n}\n\nfunc (p *Page) Overflow() uint32 {\n\treturn p.overflow\n}\n\nfunc (p *Page) SetOverflow(target uint32) {\n\tp.overflow = target\n}\n\nfunc (p *Page) String() string {\n\treturn fmt.Sprintf(\"ID: %d, Type: %s, count: %d, overflow: %d\", p.id, p.Typ(), p.count, p.overflow)\n}\n\ntype Pages []*Page\n\nfunc (s Pages) Len() int           { return len(s) }\nfunc (s Pages) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }\nfunc (s Pages) Less(i, j int) bool { return s[i].id < s[j].id }\n\n// branchPageElement represents a node on a branch page.\ntype branchPageElement struct {\n\tpos   uint32\n\tksize uint32\n\tpgid  Pgid\n}\n\nfunc (n *branchPageElement) Pos() uint32 {\n\treturn n.pos\n}\n\nfunc (n *branchPageElement) SetPos(v uint32) {\n\tn.pos = v\n}\n\nfunc (n *branchPageElement) Ksize() uint32 {\n\treturn n.ksize\n}\n\nfunc (n *branchPageElement) SetKsize(v uint32) {\n\tn.ksize = v\n}\n\nfunc (n *branchPageElement) Pgid() Pgid {\n\treturn n.pgid\n}\n\nfunc (n *branchPageElement) SetPgid(v Pgid) {\n\tn.pgid = v\n}\n\n// Key returns a byte slice of the node key.\nfunc (n *branchPageElement) Key() []byte {\n\treturn UnsafeByteSlice(unsafe.Pointer(n), 0, int(n.pos), int(n.pos)+int(n.ksize))\n}\n\n// leafPageElement represents a node on a leaf page.\ntype leafPageElement struct {\n\tflags uint32\n\tpos   uint32\n\tksize uint32\n\tvsize uint32\n}\n\nfunc NewLeafPageElement(flags, pos, ksize, vsize uint32) *leafPageElement {\n\treturn &leafPageElement{\n\t\tflags: flags,\n\t\tpos:   pos,\n\t\tksize: ksize,\n\t\tvsize: vsize,\n\t}\n}\n\nfunc (n *leafPageElement) Flags() uint32 {\n\treturn n.flags\n}\n\nfunc (n *leafPageElement) SetFlags(v uint32) {\n\tn.flags = v\n}\n\nfunc (n *leafPageElement) Pos() uint32 {\n\treturn n.pos\n}\n\nfunc (n *leafPageElement) SetPos(v uint32) {\n\tn.pos = v\n}\n\nfunc (n *leafPageElement) Ksize() uint32 {\n\treturn n.ksize\n}\n\nfunc (n *leafPageElement) SetKsize(v uint32) {\n\tn.ksize = v\n}\n\nfunc (n *leafPageElement) Vsize() uint32 {\n\treturn n.vsize\n}\n\nfunc (n *leafPageElement) SetVsize(v uint32) {\n\tn.vsize = v\n}\n\n// Key returns a byte slice of the node key.\nfunc (n *leafPageElement) Key() []byte {\n\ti := int(n.pos)\n\tj := i + int(n.ksize)\n\treturn UnsafeByteSlice(unsafe.Pointer(n), 0, i, j)\n}\n\n// Value returns a byte slice of the node value.\nfunc (n *leafPageElement) Value() []byte {\n\ti := int(n.pos) + int(n.ksize)\n\tj := i + int(n.vsize)\n\treturn UnsafeByteSlice(unsafe.Pointer(n), 0, i, j)\n}\n\nfunc (n *leafPageElement) IsBucketEntry() bool {\n\treturn n.flags&uint32(BucketLeafFlag) != 0\n}\n\nfunc (n *leafPageElement) Bucket() *InBucket {\n\tif n.IsBucketEntry() {\n\t\treturn LoadBucket(n.Value())\n\t} else {\n\t\treturn nil\n\t}\n}\n\n// PageInfo represents human readable information about a page.\ntype PageInfo struct {\n\tID            int\n\tType          string\n\tCount         int\n\tOverflowCount int\n}\n\ntype Pgids []Pgid\n\nfunc (s Pgids) Len() int           { return len(s) }\nfunc (s Pgids) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }\nfunc (s Pgids) Less(i, j int) bool { return s[i] < s[j] }\n\n// Merge returns the sorted union of a and b.\nfunc (s Pgids) Merge(b Pgids) Pgids {\n\t// Return the opposite slice if one is nil.\n\tif len(s) == 0 {\n\t\treturn b\n\t}\n\tif len(b) == 0 {\n\t\treturn s\n\t}\n\tmerged := make(Pgids, len(s)+len(b))\n\tMergepgids(merged, s, b)\n\treturn merged\n}\n\n// Mergepgids copies the sorted union of a and b into dst.\n// If dst is too small, it panics.\nfunc Mergepgids(dst, a, b Pgids) {\n\tif len(dst) < len(a)+len(b) {\n\t\tpanic(fmt.Errorf(\"mergepgids bad len %d < %d + %d\", len(dst), len(a), len(b)))\n\t}\n\t// Copy in the opposite slice if one is nil.\n\tif len(a) == 0 {\n\t\tcopy(dst, b)\n\t\treturn\n\t}\n\tif len(b) == 0 {\n\t\tcopy(dst, a)\n\t\treturn\n\t}\n\n\t// Merged will hold all elements from both lists.\n\tmerged := dst[:0]\n\n\t// Assign lead to the slice with a lower starting value, follow to the higher value.\n\tlead, follow := a, b\n\tif b[0] < a[0] {\n\t\tlead, follow = b, a\n\t}\n\n\t// Continue while there are elements in the lead.\n\tfor len(lead) > 0 {\n\t\t// Merge largest prefix of lead that is ahead of follow[0].\n\t\tn := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] })\n\t\tmerged = append(merged, lead[:n]...)\n\t\tif n >= len(lead) {\n\t\t\tbreak\n\t\t}\n\n\t\t// Swap lead and follow.\n\t\tlead, follow = follow, lead[n:]\n\t}\n\n\t// Append what's left in follow.\n\t_ = append(merged, follow...)\n}\n"
  },
  {
    "path": "internal/common/page_test.go",
    "content": "package common\n\nimport (\n\t\"reflect\"\n\t\"sort\"\n\t\"testing\"\n\t\"testing/quick\"\n)\n\n// Ensure that the page type can be returned in human readable format.\nfunc TestPage_typ(t *testing.T) {\n\tif typ := (&Page{flags: BranchPageFlag}).Typ(); typ != \"branch\" {\n\t\tt.Fatalf(\"exp=branch; got=%v\", typ)\n\t}\n\tif typ := (&Page{flags: LeafPageFlag}).Typ(); typ != \"leaf\" {\n\t\tt.Fatalf(\"exp=leaf; got=%v\", typ)\n\t}\n\tif typ := (&Page{flags: MetaPageFlag}).Typ(); typ != \"meta\" {\n\t\tt.Fatalf(\"exp=meta; got=%v\", typ)\n\t}\n\tif typ := (&Page{flags: FreelistPageFlag}).Typ(); typ != \"freelist\" {\n\t\tt.Fatalf(\"exp=freelist; got=%v\", typ)\n\t}\n\tif typ := (&Page{flags: 20000}).Typ(); typ != \"unknown<4e20>\" {\n\t\tt.Fatalf(\"exp=unknown<4e20>; got=%v\", typ)\n\t}\n}\n\n// Ensure that the hexdump debugging function doesn't blow up.\nfunc TestPage_dump(t *testing.T) {\n\t(&Page{id: 256}).hexdump(16)\n}\n\nfunc TestPgids_merge(t *testing.T) {\n\ta := Pgids{4, 5, 6, 10, 11, 12, 13, 27}\n\tb := Pgids{1, 3, 8, 9, 25, 30}\n\tc := a.Merge(b)\n\tif !reflect.DeepEqual(c, Pgids{1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30}) {\n\t\tt.Errorf(\"mismatch: %v\", c)\n\t}\n\n\ta = Pgids{4, 5, 6, 10, 11, 12, 13, 27, 35, 36}\n\tb = Pgids{8, 9, 25, 30}\n\tc = a.Merge(b)\n\tif !reflect.DeepEqual(c, Pgids{4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30, 35, 36}) {\n\t\tt.Errorf(\"mismatch: %v\", c)\n\t}\n}\n\nfunc TestPgids_merge_quick(t *testing.T) {\n\tif err := quick.Check(func(a, b Pgids) bool {\n\t\t// Sort incoming lists.\n\t\tsort.Sort(a)\n\t\tsort.Sort(b)\n\n\t\t// Merge the two lists together.\n\t\tgot := a.Merge(b)\n\n\t\t// The expected value should be the two lists combined and sorted.\n\t\texp := append(a, b...)\n\t\tsort.Sort(exp)\n\n\t\tif !reflect.DeepEqual(exp, got) {\n\t\t\tt.Errorf(\"\\nexp=%+v\\ngot=%+v\\n\", exp, got)\n\t\t\treturn false\n\t\t}\n\n\t\treturn true\n\t}, nil); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n"
  },
  {
    "path": "internal/common/types.go",
    "content": "package common\n\nimport (\n\t\"os\"\n\t\"runtime\"\n\t\"time\"\n)\n\n// MaxMmapStep is the largest step that can be taken when remapping the mmap.\nconst MaxMmapStep = 1 << 30 // 1GB\n\n// Version represents the data file format version.\nconst Version uint32 = 2\n\n// Magic represents a marker value to indicate that a file is a Bolt DB.\nconst Magic uint32 = 0xED0CDAED\n\nconst PgidNoFreelist Pgid = 0xffffffffffffffff\n\n// IgnoreNoSync specifies whether the NoSync field of a DB is ignored when\n// syncing changes to a file.  This is required as some operating systems,\n// such as OpenBSD, do not have a unified buffer cache (UBC) and writes\n// must be synchronized using the msync(2) syscall.\nconst IgnoreNoSync = runtime.GOOS == \"openbsd\"\n\n// Default values if not set in a DB instance.\nconst (\n\tDefaultMaxBatchSize  int = 1000\n\tDefaultMaxBatchDelay     = 10 * time.Millisecond\n\tDefaultAllocSize         = 16 * 1024 * 1024\n)\n\n// DefaultPageSize is the default page size for db which is set to the OS page size.\nvar DefaultPageSize = os.Getpagesize()\n\n// Txid represents the internal transaction identifier.\ntype Txid uint64\n"
  },
  {
    "path": "internal/common/unsafe.go",
    "content": "package common\n\nimport (\n\t\"unsafe\"\n)\n\nfunc UnsafeAdd(base unsafe.Pointer, offset uintptr) unsafe.Pointer {\n\treturn unsafe.Pointer(uintptr(base) + offset)\n}\n\nfunc UnsafeIndex(base unsafe.Pointer, offset uintptr, elemsz uintptr, n int) unsafe.Pointer {\n\treturn unsafe.Pointer(uintptr(base) + offset + uintptr(n)*elemsz)\n}\n\nfunc UnsafeByteSlice(base unsafe.Pointer, offset uintptr, i, j int) []byte {\n\t// See: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices\n\t//\n\t// This memory is not allocated from C, but it is unmanaged by Go's\n\t// garbage collector and should behave similarly, and the compiler\n\t// should produce similar code.  Note that this conversion allows a\n\t// subslice to begin after the base address, with an optional offset,\n\t// while the URL above does not cover this case and only slices from\n\t// index 0.  However, the wiki never says that the address must be to\n\t// the beginning of a C allocation (or even that malloc was used at\n\t// all), so this is believed to be correct.\n\treturn (*[MaxAllocSize]byte)(UnsafeAdd(base, offset))[i:j:j]\n}\n"
  },
  {
    "path": "internal/common/utils.go",
    "content": "package common\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"unsafe\"\n)\n\nfunc LoadBucket(buf []byte) *InBucket {\n\treturn (*InBucket)(unsafe.Pointer(&buf[0]))\n}\n\nfunc LoadPage(buf []byte) *Page {\n\treturn (*Page)(unsafe.Pointer(&buf[0]))\n}\n\nfunc LoadPageMeta(buf []byte) *Meta {\n\treturn (*Meta)(unsafe.Pointer(&buf[PageHeaderSize]))\n}\n\nfunc CopyFile(srcPath, dstPath string) error {\n\t// Ensure source file exists.\n\t_, err := os.Stat(srcPath)\n\tif os.IsNotExist(err) {\n\t\treturn fmt.Errorf(\"source file %q not found\", srcPath)\n\t} else if err != nil {\n\t\treturn err\n\t}\n\n\t// Ensure output file not exist.\n\t_, err = os.Stat(dstPath)\n\tif err == nil {\n\t\treturn fmt.Errorf(\"output file %q already exists\", dstPath)\n\t} else if !os.IsNotExist(err) {\n\t\treturn err\n\t}\n\n\tsrcDB, err := os.Open(srcPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to open source file %q: %w\", srcPath, err)\n\t}\n\tdefer srcDB.Close()\n\tdstDB, err := os.Create(dstPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create output file %q: %w\", dstPath, err)\n\t}\n\tdefer dstDB.Close()\n\twritten, err := io.Copy(dstDB, srcDB)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to copy database file from %q to %q: %w\", srcPath, dstPath, err)\n\t}\n\n\tsrcFi, err := srcDB.Stat()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get source file info %q: %w\", srcPath, err)\n\t}\n\tinitialSize := srcFi.Size()\n\tif initialSize != written {\n\t\treturn fmt.Errorf(\"the byte copied (%q: %d) isn't equal to the initial db size (%q: %d)\", dstPath, written, srcPath, initialSize)\n\t}\n\n\treturn nil\n}\n"
  },
  {
    "path": "internal/common/verify.go",
    "content": "// Copied from https://github.com/etcd-io/etcd/blob/main/client/pkg/verify/verify.go\npackage common\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n)\n\nconst ENV_VERIFY = \"BBOLT_VERIFY\"\n\ntype VerificationType string\n\nconst (\n\tENV_VERIFY_VALUE_ALL    VerificationType = \"all\"\n\tENV_VERIFY_VALUE_ASSERT VerificationType = \"assert\"\n)\n\nfunc getEnvVerify() string {\n\treturn strings.ToLower(os.Getenv(ENV_VERIFY))\n}\n\nfunc IsVerificationEnabled(verification VerificationType) bool {\n\tenv := getEnvVerify()\n\treturn env == string(ENV_VERIFY_VALUE_ALL) || env == strings.ToLower(string(verification))\n}\n\n// EnableVerifications sets `ENV_VERIFY` and returns a function that\n// can be used to bring the original settings.\nfunc EnableVerifications(verification VerificationType) func() {\n\tpreviousEnv := getEnvVerify()\n\tos.Setenv(ENV_VERIFY, string(verification))\n\treturn func() {\n\t\tos.Setenv(ENV_VERIFY, previousEnv)\n\t}\n}\n\n// EnableAllVerifications enables verification and returns a function\n// that can be used to bring the original settings.\nfunc EnableAllVerifications() func() {\n\treturn EnableVerifications(ENV_VERIFY_VALUE_ALL)\n}\n\n// DisableVerifications unsets `ENV_VERIFY` and returns a function that\n// can be used to bring the original settings.\nfunc DisableVerifications() func() {\n\tpreviousEnv := getEnvVerify()\n\tos.Unsetenv(ENV_VERIFY)\n\treturn func() {\n\t\tos.Setenv(ENV_VERIFY, previousEnv)\n\t}\n}\n\n// Verify performs verification if the assertions are enabled.\n// In the default setup running in tests and skipped in the production code.\nfunc Verify(f func()) {\n\tif IsVerificationEnabled(ENV_VERIFY_VALUE_ASSERT) {\n\t\tf()\n\t}\n}\n\n// Assert will panic with a given formatted message if the given condition is false.\nfunc Assert(condition bool, msg string, v ...any) {\n\tif !condition {\n\t\tpanic(fmt.Sprintf(\"assertion failed: \"+msg, v...))\n\t}\n}\n"
  },
  {
    "path": "internal/freelist/array.go",
    "content": "package freelist\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\ntype array struct {\n\t*shared\n\n\tids []common.Pgid // all free and available free page ids.\n}\n\nfunc (f *array) Init(ids common.Pgids) {\n\tf.ids = ids\n\tf.reindex()\n}\n\nfunc (f *array) Allocate(txid common.Txid, n int) common.Pgid {\n\tif len(f.ids) == 0 {\n\t\treturn 0\n\t}\n\n\tvar initial, previd common.Pgid\n\tfor i, id := range f.ids {\n\t\tif id <= 1 {\n\t\t\tpanic(fmt.Sprintf(\"invalid page allocation: %d\", id))\n\t\t}\n\n\t\t// Reset initial page if this is not contiguous.\n\t\tif previd == 0 || id-previd != 1 {\n\t\t\tinitial = id\n\t\t}\n\n\t\t// If we found a contiguous block then remove it and return it.\n\t\tif (id-initial)+1 == common.Pgid(n) {\n\t\t\t// If we're allocating off the beginning then take the fast path\n\t\t\t// and just adjust the existing slice. This will use extra memory\n\t\t\t// temporarily but the append() in free() will realloc the slice\n\t\t\t// as is necessary.\n\t\t\tif (i + 1) == n {\n\t\t\t\tf.ids = f.ids[i+1:]\n\t\t\t} else {\n\t\t\t\tcopy(f.ids[i-n+1:], f.ids[i+1:])\n\t\t\t\tf.ids = f.ids[:len(f.ids)-n]\n\t\t\t}\n\n\t\t\t// Remove from the free cache.\n\t\t\tfor i := common.Pgid(0); i < common.Pgid(n); i++ {\n\t\t\t\tdelete(f.cache, initial+i)\n\t\t\t}\n\t\t\tf.allocs[initial] = txid\n\t\t\treturn initial\n\t\t}\n\n\t\tprevid = id\n\t}\n\treturn 0\n}\n\nfunc (f *array) FreeCount() int {\n\treturn len(f.ids)\n}\n\nfunc (f *array) freePageIds() common.Pgids {\n\treturn f.ids\n}\n\nfunc (f *array) mergeSpans(ids common.Pgids) {\n\tsort.Sort(ids)\n\tcommon.Verify(func() {\n\t\tidsIdx := make(map[common.Pgid]struct{})\n\t\tfor _, id := range f.ids {\n\t\t\t// The existing f.ids shouldn't have duplicated free ID.\n\t\t\tif _, ok := idsIdx[id]; ok {\n\t\t\t\tpanic(fmt.Sprintf(\"detected duplicated free page ID: %d in existing f.ids: %v\", id, f.ids))\n\t\t\t}\n\t\t\tidsIdx[id] = struct{}{}\n\t\t}\n\n\t\tprev := common.Pgid(0)\n\t\tfor _, id := range ids {\n\t\t\t// The ids shouldn't have duplicated free ID. Note page 0 and 1\n\t\t\t// are reserved for meta pages, so they can never be free page IDs.\n\t\t\tif prev == id {\n\t\t\t\tpanic(fmt.Sprintf(\"detected duplicated free ID: %d in ids: %v\", id, ids))\n\t\t\t}\n\t\t\tprev = id\n\n\t\t\t// The ids shouldn't have any overlap with the existing f.ids.\n\t\t\tif _, ok := idsIdx[id]; ok {\n\t\t\t\tpanic(fmt.Sprintf(\"detected overlapped free page ID: %d between ids: %v and existing f.ids: %v\", id, ids, f.ids))\n\t\t\t}\n\t\t}\n\t})\n\tf.ids = common.Pgids(f.ids).Merge(ids)\n}\n\nfunc NewArrayFreelist() Interface {\n\ta := &array{\n\t\tshared: newShared(),\n\t\tids:    []common.Pgid{},\n\t}\n\ta.Interface = a\n\treturn a\n}\n"
  },
  {
    "path": "internal/freelist/array_test.go",
    "content": "package freelist\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// Ensure that a freelist can find contiguous blocks of pages.\nfunc TestFreelistArray_allocate(t *testing.T) {\n\tf := NewArrayFreelist()\n\tids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}\n\tf.Init(ids)\n\tif id := int(f.Allocate(1, 3)); id != 3 {\n\t\tt.Fatalf(\"exp=3; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 1)); id != 6 {\n\t\tt.Fatalf(\"exp=6; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 3)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 2)); id != 12 {\n\t\tt.Fatalf(\"exp=12; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 1)); id != 7 {\n\t\tt.Fatalf(\"exp=7; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 0)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 0)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif exp := common.Pgids([]common.Pgid{9, 18}); !reflect.DeepEqual(exp, f.freePageIds()) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.freePageIds())\n\t}\n\n\tif id := int(f.Allocate(1, 1)); id != 9 {\n\t\tt.Fatalf(\"exp=9; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 1)); id != 18 {\n\t\tt.Fatalf(\"exp=18; got=%v\", id)\n\t}\n\tif id := int(f.Allocate(1, 1)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif exp := common.Pgids([]common.Pgid{}); !reflect.DeepEqual(exp, f.freePageIds()) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.freePageIds())\n\t}\n}\n\nfunc TestInvalidArrayAllocation(t *testing.T) {\n\tf := NewArrayFreelist()\n\t// page 0 and 1 are reserved for meta pages, so they should never be free pages.\n\tids := []common.Pgid{1}\n\tf.Init(ids)\n\trequire.Panics(t, func() {\n\t\tf.Allocate(common.Txid(1), 1)\n\t})\n}\n\nfunc Test_Freelist_Array_Rollback(t *testing.T) {\n\tf := newTestArrayFreelist()\n\n\tf.Init([]common.Pgid{3, 5, 6, 7, 12, 13})\n\n\tf.Free(100, common.NewPage(20, 0, 0, 1))\n\tf.Allocate(100, 3)\n\tf.Free(100, common.NewPage(25, 0, 0, 0))\n\tf.Allocate(100, 2)\n\n\trequire.Equal(t, map[common.Pgid]common.Txid{5: 100, 12: 100}, f.allocs)\n\trequire.Equal(t, map[common.Txid]*txPending{100: {\n\t\tids:     []common.Pgid{20, 21, 25},\n\t\talloctx: []common.Txid{0, 0, 0},\n\t}}, f.pending)\n\n\tf.Rollback(100)\n\n\trequire.Equal(t, map[common.Pgid]common.Txid{}, f.allocs)\n\trequire.Equal(t, map[common.Txid]*txPending{}, f.pending)\n}\n\nfunc newTestArrayFreelist() *array {\n\tf := NewArrayFreelist()\n\treturn f.(*array)\n}\n"
  },
  {
    "path": "internal/freelist/freelist.go",
    "content": "package freelist\n\nimport (\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\ntype ReadWriter interface {\n\t// Read calls Init with the page ids stored in the given page.\n\tRead(page *common.Page)\n\n\t// Write writes the freelist into the given page.\n\tWrite(page *common.Page)\n\n\t// EstimatedWritePageSize returns the size in bytes of the freelist after serialization in Write.\n\t// This should never underestimate the size.\n\tEstimatedWritePageSize() int\n}\n\ntype Interface interface {\n\tReadWriter\n\n\t// Init initializes this freelist with the given list of pages.\n\tInit(ids common.Pgids)\n\n\t// Allocate tries to allocate the given number of contiguous pages\n\t// from the free list pages. It returns the starting page ID if\n\t// available; otherwise, it returns 0.\n\tAllocate(txid common.Txid, numPages int) common.Pgid\n\n\t// Count returns the number of free and pending pages.\n\tCount() int\n\n\t// FreeCount returns the number of free pages.\n\tFreeCount() int\n\n\t// PendingCount returns the number of pending pages.\n\tPendingCount() int\n\n\t// AddReadonlyTXID adds a given read-only transaction id for pending page tracking.\n\tAddReadonlyTXID(txid common.Txid)\n\n\t// RemoveReadonlyTXID removes a given read-only transaction id for pending page tracking.\n\tRemoveReadonlyTXID(txid common.Txid)\n\n\t// ReleasePendingPages releases any pages associated with closed read-only transactions.\n\tReleasePendingPages()\n\n\t// Free releases a page and its overflow for a given transaction id.\n\t// If the page is already free or is one of the meta pages, then a panic will occur.\n\tFree(txId common.Txid, p *common.Page)\n\n\t// Freed returns whether a given page is in the free list.\n\tFreed(pgId common.Pgid) bool\n\n\t// Rollback removes the pages from a given pending tx.\n\tRollback(txId common.Txid)\n\n\t// Copyall copies a list of all free ids and all pending ids in one sorted list.\n\t// f.count returns the minimum length required for dst.\n\tCopyall(dst []common.Pgid)\n\n\t// Reload reads the freelist from a page and filters out pending items.\n\tReload(p *common.Page)\n\n\t// NoSyncReload reads the freelist from Pgids and filters out pending items.\n\tNoSyncReload(pgIds common.Pgids)\n\n\t// freePageIds returns the IDs of all free pages. Returns an empty slice if no free pages are available.\n\tfreePageIds() common.Pgids\n\n\t// pendingPageIds returns all pending pages by transaction id.\n\tpendingPageIds() map[common.Txid]*txPending\n\n\t// release moves all page ids for a transaction id (or older) to the freelist.\n\trelease(txId common.Txid)\n\n\t// releaseRange moves pending pages allocated within an extent [begin,end] to the free list.\n\treleaseRange(begin, end common.Txid)\n\n\t// mergeSpans is merging the given pages into the freelist\n\tmergeSpans(ids common.Pgids)\n}\n"
  },
  {
    "path": "internal/freelist/freelist_test.go",
    "content": "package freelist\n\nimport (\n\t\"fmt\"\n\t\"math\"\n\t\"math/rand\"\n\t\"os\"\n\t\"reflect\"\n\t\"slices\"\n\t\"sort\"\n\t\"testing\"\n\t\"testing/quick\"\n\t\"unsafe\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// TestFreelistType is used as a env variable for test to indicate the backend type\nconst TestFreelistType = \"TEST_FREELIST_TYPE\"\n\n// Ensure that a page is added to a transaction's freelist.\nfunc TestFreelist_free(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Free(100, common.NewPage(12, 0, 0, 0))\n\tif !reflect.DeepEqual([]common.Pgid{12}, f.pendingPageIds()[100].ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", []common.Pgid{12}, f.pendingPageIds()[100].ids)\n\t}\n}\n\n// Ensure that a page and its overflow is added to a transaction's freelist.\nfunc TestFreelist_free_overflow(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Free(100, common.NewPage(12, 0, 0, 3))\n\tif exp := []common.Pgid{12, 13, 14, 15}; !reflect.DeepEqual(exp, f.pendingPageIds()[100].ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.pendingPageIds()[100].ids)\n\t}\n}\n\n// Ensure that double freeing a page is causing a panic\nfunc TestFreelist_free_double_free_panics(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Free(100, common.NewPage(12, 0, 0, 3))\n\trequire.Panics(t, func() {\n\t\tf.Free(100, common.NewPage(12, 0, 0, 3))\n\t})\n}\n\n// Ensure that attempting to free the meta page panics\nfunc TestFreelist_free_meta_panics(t *testing.T) {\n\tf := newTestFreelist()\n\trequire.Panics(t, func() {\n\t\tf.Free(100, common.NewPage(0, 0, 0, 0))\n\t})\n\trequire.Panics(t, func() {\n\t\tf.Free(100, common.NewPage(1, 0, 0, 0))\n\t})\n}\n\nfunc TestFreelist_free_freelist(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Free(100, common.NewPage(12, common.FreelistPageFlag, 0, 0))\n\tpp := f.pendingPageIds()[100]\n\trequire.Equal(t, []common.Pgid{12}, pp.ids)\n\trequire.Equal(t, []common.Txid{0}, pp.alloctx)\n}\n\nfunc TestFreelist_free_freelist_alloctx(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Free(100, common.NewPage(12, common.FreelistPageFlag, 0, 0))\n\tf.Rollback(100)\n\trequire.Empty(t, f.freePageIds())\n\trequire.Empty(t, f.pendingPageIds())\n\trequire.False(t, f.Freed(12))\n\n\tf.Free(101, common.NewPage(12, common.FreelistPageFlag, 0, 0))\n\trequire.True(t, f.Freed(12))\n\tif exp := []common.Pgid{12}; !reflect.DeepEqual(exp, f.pendingPageIds()[101].ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.pendingPageIds()[101].ids)\n\t}\n\tf.ReleasePendingPages()\n\trequire.True(t, f.Freed(12))\n\trequire.Empty(t, f.pendingPageIds())\n\tif exp := common.Pgids([]common.Pgid{12}); !reflect.DeepEqual(exp, f.freePageIds()) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.freePageIds())\n\t}\n}\n\n// Ensure that a transaction's free pages can be released.\nfunc TestFreelist_release(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Free(100, common.NewPage(12, 0, 0, 1))\n\tf.Free(100, common.NewPage(9, 0, 0, 0))\n\tf.Free(102, common.NewPage(39, 0, 0, 0))\n\tf.release(100)\n\tf.release(101)\n\tif exp := common.Pgids([]common.Pgid{9, 12, 13}); !reflect.DeepEqual(exp, f.freePageIds()) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.freePageIds())\n\t}\n\n\tf.release(102)\n\tif exp := common.Pgids([]common.Pgid{9, 12, 13, 39}); !reflect.DeepEqual(exp, f.freePageIds()) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.freePageIds())\n\t}\n}\n\n// Ensure that releaseRange handles boundary conditions correctly\nfunc TestFreelist_releaseRange(t *testing.T) {\n\ttype testRange struct {\n\t\tbegin, end common.Txid\n\t}\n\n\ttype testPage struct {\n\t\tid       common.Pgid\n\t\tn        int\n\t\tallocTxn common.Txid\n\t\tfreeTxn  common.Txid\n\t}\n\n\tvar releaseRangeTests = []struct {\n\t\ttitle         string\n\t\tpagesIn       []testPage\n\t\treleaseRanges []testRange\n\t\twantFree      []common.Pgid\n\t}{\n\t\t{\n\t\t\ttitle:         \"Single pending in range\",\n\t\t\tpagesIn:       []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}},\n\t\t\treleaseRanges: []testRange{{1, 300}},\n\t\t\twantFree:      []common.Pgid{3},\n\t\t},\n\t\t{\n\t\t\ttitle:         \"Single pending with minimum end range\",\n\t\t\tpagesIn:       []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}},\n\t\t\treleaseRanges: []testRange{{1, 200}},\n\t\t\twantFree:      []common.Pgid{3},\n\t\t},\n\t\t{\n\t\t\ttitle:         \"Single pending outsize minimum end range\",\n\t\t\tpagesIn:       []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}},\n\t\t\treleaseRanges: []testRange{{1, 199}},\n\t\t\twantFree:      []common.Pgid{},\n\t\t},\n\t\t{\n\t\t\ttitle:         \"Single pending with minimum begin range\",\n\t\t\tpagesIn:       []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}},\n\t\t\treleaseRanges: []testRange{{100, 300}},\n\t\t\twantFree:      []common.Pgid{3},\n\t\t},\n\t\t{\n\t\t\ttitle:         \"Single pending outside minimum begin range\",\n\t\t\tpagesIn:       []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}},\n\t\t\treleaseRanges: []testRange{{101, 300}},\n\t\t\twantFree:      []common.Pgid{},\n\t\t},\n\t\t{\n\t\t\ttitle:         \"Single pending in minimum range\",\n\t\t\tpagesIn:       []testPage{{id: 3, n: 1, allocTxn: 199, freeTxn: 200}},\n\t\t\treleaseRanges: []testRange{{199, 200}},\n\t\t\twantFree:      []common.Pgid{3},\n\t\t},\n\t\t{\n\t\t\ttitle:         \"Single pending and read transaction at 199\",\n\t\t\tpagesIn:       []testPage{{id: 3, n: 1, allocTxn: 199, freeTxn: 200}},\n\t\t\treleaseRanges: []testRange{{100, 198}, {200, 300}},\n\t\t\twantFree:      []common.Pgid{},\n\t\t},\n\t\t{\n\t\t\ttitle: \"Adjacent pending and read transactions at 199, 200\",\n\t\t\tpagesIn: []testPage{\n\t\t\t\t{id: 3, n: 1, allocTxn: 199, freeTxn: 200},\n\t\t\t\t{id: 4, n: 1, allocTxn: 200, freeTxn: 201},\n\t\t\t},\n\t\t\treleaseRanges: []testRange{\n\t\t\t\t{100, 198},\n\t\t\t\t{200, 199}, // Simulate the ranges db.freePages might produce.\n\t\t\t\t{201, 300},\n\t\t\t},\n\t\t\twantFree: []common.Pgid{},\n\t\t},\n\t\t{\n\t\t\ttitle: \"Out of order ranges\",\n\t\t\tpagesIn: []testPage{\n\t\t\t\t{id: 3, n: 1, allocTxn: 199, freeTxn: 200},\n\t\t\t\t{id: 4, n: 1, allocTxn: 200, freeTxn: 201},\n\t\t\t},\n\t\t\treleaseRanges: []testRange{\n\t\t\t\t{201, 199},\n\t\t\t\t{201, 200},\n\t\t\t\t{200, 200},\n\t\t\t},\n\t\t\twantFree: []common.Pgid{},\n\t\t},\n\t\t{\n\t\t\ttitle: \"Multiple pending, read transaction at 150\",\n\t\t\tpagesIn: []testPage{\n\t\t\t\t{id: 3, n: 1, allocTxn: 100, freeTxn: 200},\n\t\t\t\t{id: 4, n: 1, allocTxn: 100, freeTxn: 125},\n\t\t\t\t{id: 5, n: 1, allocTxn: 125, freeTxn: 150},\n\t\t\t\t{id: 6, n: 1, allocTxn: 125, freeTxn: 175},\n\t\t\t\t{id: 7, n: 2, allocTxn: 150, freeTxn: 175},\n\t\t\t\t{id: 9, n: 2, allocTxn: 175, freeTxn: 200},\n\t\t\t},\n\t\t\treleaseRanges: []testRange{{50, 149}, {151, 300}},\n\t\t\twantFree:      []common.Pgid{4, 9, 10},\n\t\t},\n\t}\n\n\tfor _, c := range releaseRangeTests {\n\t\tt.Run(c.title, func(t *testing.T) {\n\t\t\tf := newTestFreelist()\n\t\t\tvar ids []common.Pgid\n\t\t\tfor _, p := range c.pagesIn {\n\t\t\t\tfor i := uint64(0); i < uint64(p.n); i++ {\n\t\t\t\t\tids = append(ids, common.Pgid(uint64(p.id)+i))\n\t\t\t\t}\n\t\t\t}\n\t\t\tf.Init(ids)\n\t\t\tfor _, p := range c.pagesIn {\n\t\t\t\tf.Allocate(p.allocTxn, p.n)\n\t\t\t}\n\n\t\t\tfor _, p := range c.pagesIn {\n\t\t\t\tf.Free(p.freeTxn, common.NewPage(p.id, 0, 0, uint32(p.n-1)))\n\t\t\t}\n\n\t\t\tfor _, r := range c.releaseRanges {\n\t\t\t\tf.releaseRange(r.begin, r.end)\n\t\t\t}\n\n\t\t\trequire.Equal(t, common.Pgids(c.wantFree), f.freePageIds())\n\t\t})\n\t}\n}\n\nfunc TestFreeList_init(t *testing.T) {\n\tbuf := make([]byte, 4096)\n\tf := newTestFreelist()\n\tf.Init(common.Pgids{5, 6, 8})\n\n\tp := common.LoadPage(buf)\n\tf.Write(p)\n\n\tf2 := newTestFreelist()\n\tf2.Read(p)\n\trequire.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())\n\n\t// When initializing the freelist with an empty list of page ID,\n\t// it should reset the freelist page IDs.\n\tf2.Init([]common.Pgid{})\n\trequire.Equal(t, common.Pgids{}, f2.freePageIds())\n}\n\nfunc TestFreeList_reload(t *testing.T) {\n\tbuf := make([]byte, 4096)\n\tf := newTestFreelist()\n\tf.Init(common.Pgids{5, 6, 8})\n\n\tp := common.LoadPage(buf)\n\tf.Write(p)\n\n\tf2 := newTestFreelist()\n\tf2.Read(p)\n\trequire.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())\n\n\tf2.Free(common.Txid(5), common.NewPage(10, common.LeafPageFlag, 0, 2))\n\n\t// reload shouldn't affect the pending list\n\tf2.Reload(p)\n\n\trequire.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())\n\trequire.Equal(t, []common.Pgid{10, 11, 12}, f2.pendingPageIds()[5].ids)\n}\n\n// Ensure that the txIDx swap, less and len are properly implemented\nfunc TestTxidSorting(t *testing.T) {\n\trequire.NoError(t, quick.Check(func(a []uint64) bool {\n\t\tvar txids []common.Txid\n\t\tfor _, txid := range a {\n\t\t\ttxids = append(txids, common.Txid(txid))\n\t\t}\n\n\t\tsort.Sort(txIDx(txids))\n\n\t\tvar r []uint64\n\t\tfor _, txid := range txids {\n\t\t\tr = append(r, uint64(txid))\n\t\t}\n\n\t\tif !slices.IsSorted(r) {\n\t\t\tt.Errorf(\"txids were not sorted correctly=%v\", txids)\n\t\t\treturn false\n\t\t}\n\n\t\treturn true\n\t}, nil))\n}\n\n// Ensure that a freelist can deserialize from a freelist page.\nfunc TestFreelist_read(t *testing.T) {\n\t// Create a page.\n\tvar buf [4096]byte\n\tpage := (*common.Page)(unsafe.Pointer(&buf[0]))\n\tpage.SetFlags(common.FreelistPageFlag)\n\tpage.SetCount(2)\n\n\t// Insert 2 page ids.\n\tids := (*[3]common.Pgid)(unsafe.Pointer(uintptr(unsafe.Pointer(page)) + unsafe.Sizeof(*page)))\n\tids[0] = 23\n\tids[1] = 50\n\n\t// Deserialize page into a freelist.\n\tf := newTestFreelist()\n\tf.Read(page)\n\n\t// Ensure that there are two page ids in the freelist.\n\tif exp := common.Pgids([]common.Pgid{23, 50}); !reflect.DeepEqual(exp, f.freePageIds()) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.freePageIds())\n\t}\n}\n\n// Ensure that we never read a non-freelist page\nfunc TestFreelist_read_panics(t *testing.T) {\n\tbuf := make([]byte, 4096)\n\tpage := common.LoadPage(buf)\n\tpage.SetFlags(common.BranchPageFlag)\n\tpage.SetCount(2)\n\tf := newTestFreelist()\n\trequire.Panics(t, func() {\n\t\tf.Read(page)\n\t})\n}\n\n// Ensure that a freelist can serialize into a freelist page.\nfunc TestFreelist_write(t *testing.T) {\n\t// Create a freelist and write it to a page.\n\tvar buf [4096]byte\n\tf := newTestFreelist()\n\n\tf.Init([]common.Pgid{12, 39})\n\tf.pendingPageIds()[100] = &txPending{ids: []common.Pgid{28, 11}}\n\tf.pendingPageIds()[101] = &txPending{ids: []common.Pgid{3}}\n\tp := (*common.Page)(unsafe.Pointer(&buf[0]))\n\tf.Write(p)\n\n\t// Read the page back out.\n\tf2 := newTestFreelist()\n\tf2.Read(p)\n\n\t// Ensure that the freelist is correct.\n\t// All pages should be present and in reverse order.\n\tif exp := common.Pgids([]common.Pgid{3, 11, 12, 28, 39}); !reflect.DeepEqual(exp, f2.freePageIds()) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f2.freePageIds())\n\t}\n}\n\nfunc TestFreelist_E2E_HappyPath(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Init([]common.Pgid{})\n\trequirePages(t, f, common.Pgids{}, common.Pgids{})\n\n\tallocated := f.Allocate(common.Txid(1), 5)\n\trequire.Equal(t, common.Pgid(0), allocated)\n\t// tx.go may now allocate more space, and eventually we need to delete a page again\n\tf.Free(common.Txid(2), common.NewPage(5, common.LeafPageFlag, 0, 0))\n\tf.Free(common.Txid(2), common.NewPage(3, common.LeafPageFlag, 0, 0))\n\tf.Free(common.Txid(2), common.NewPage(8, common.LeafPageFlag, 0, 0))\n\t// the above will only mark the pages as pending, so free pages should not return anything\n\trequirePages(t, f, common.Pgids{}, common.Pgids{3, 5, 8})\n\n\t// someone wants to do a read on top of the next tx id\n\tf.AddReadonlyTXID(common.Txid(3))\n\t// this should free the above pages for tx 2 entirely\n\tf.ReleasePendingPages()\n\trequirePages(t, f, common.Pgids{3, 5, 8}, common.Pgids{})\n\n\t// no span of two pages available should yield a zero-page result\n\trequire.Equal(t, common.Pgid(0), f.Allocate(common.Txid(4), 2))\n\t// we should be able to allocate those pages independently however,\n\t// map and array differ in the order they return the pages\n\texpectedPgids := map[common.Pgid]struct{}{3: {}, 5: {}, 8: {}}\n\tfor i := 0; i < 3; i++ {\n\t\tallocated = f.Allocate(common.Txid(4), 1)\n\t\trequire.Contains(t, expectedPgids, allocated, \"expected to find pgid %d\", allocated)\n\t\trequire.False(t, f.Freed(allocated))\n\t\tdelete(expectedPgids, allocated)\n\t}\n\trequire.Emptyf(t, expectedPgids, \"unexpectedly more than one page was still found\")\n\t// no more free pages to allocate\n\trequire.Equal(t, common.Pgid(0), f.Allocate(common.Txid(4), 1))\n}\n\nfunc TestFreelist_E2E_MultiSpanOverflows(t *testing.T) {\n\tf := newTestFreelist()\n\tf.Init([]common.Pgid{})\n\tf.Free(common.Txid(10), common.NewPage(20, common.LeafPageFlag, 0, 1))\n\tf.Free(common.Txid(10), common.NewPage(25, common.LeafPageFlag, 0, 2))\n\tf.Free(common.Txid(10), common.NewPage(35, common.LeafPageFlag, 0, 3))\n\tf.Free(common.Txid(10), common.NewPage(39, common.LeafPageFlag, 0, 2))\n\tf.Free(common.Txid(10), common.NewPage(45, common.LeafPageFlag, 0, 4))\n\trequirePages(t, f, common.Pgids{}, common.Pgids{20, 21, 25, 26, 27, 35, 36, 37, 38, 39, 40, 41, 45, 46, 47, 48, 49})\n\tf.ReleasePendingPages()\n\trequirePages(t, f, common.Pgids{20, 21, 25, 26, 27, 35, 36, 37, 38, 39, 40, 41, 45, 46, 47, 48, 49}, common.Pgids{})\n\n\t// that sequence, regardless of implementation, should always yield the same blocks of pages\n\tallocSequence := []int{7, 5, 3, 2}\n\texpectedSpanStarts := []common.Pgid{35, 45, 25, 20}\n\tfor i, pageNums := range allocSequence {\n\t\tallocated := f.Allocate(common.Txid(11), pageNums)\n\t\trequire.Equal(t, expectedSpanStarts[i], allocated)\n\t\t// ensure all pages in that span are not considered free anymore\n\t\tfor i := 0; i < pageNums; i++ {\n\t\t\trequire.False(t, f.Freed(allocated+common.Pgid(i)))\n\t\t}\n\t}\n}\n\nfunc TestFreelist_E2E_Rollbacks(t *testing.T) {\n\tfreelist := newTestFreelist()\n\tfreelist.Init([]common.Pgid{})\n\tfreelist.Free(common.Txid(2), common.NewPage(5, common.LeafPageFlag, 0, 1))\n\tfreelist.Free(common.Txid(2), common.NewPage(8, common.LeafPageFlag, 0, 0))\n\trequirePages(t, freelist, common.Pgids{}, common.Pgids{5, 6, 8})\n\tfreelist.Rollback(common.Txid(2))\n\trequirePages(t, freelist, common.Pgids{}, common.Pgids{})\n\n\t// unknown transaction should not trigger anything\n\tfreelist.Free(common.Txid(4), common.NewPage(13, common.LeafPageFlag, 0, 3))\n\trequirePages(t, freelist, common.Pgids{}, common.Pgids{13, 14, 15, 16})\n\tfreelist.ReleasePendingPages()\n\trequirePages(t, freelist, common.Pgids{13, 14, 15, 16}, common.Pgids{})\n\tfreelist.Rollback(common.Txid(1337))\n\trequirePages(t, freelist, common.Pgids{13, 14, 15, 16}, common.Pgids{})\n}\n\nfunc TestFreelist_E2E_RollbackPanics(t *testing.T) {\n\tfreelist := newTestFreelist()\n\tfreelist.Init([]common.Pgid{5})\n\trequirePages(t, freelist, common.Pgids{5}, common.Pgids{})\n\n\t_ = freelist.Allocate(common.Txid(5), 1)\n\trequire.Panics(t, func() {\n\t\t// depending on the verification level, either should panic\n\t\tfreelist.Free(common.Txid(5), common.NewPage(5, common.LeafPageFlag, 0, 0))\n\t\tfreelist.Rollback(5)\n\t})\n}\n\n// tests the reloading from another physical page\nfunc TestFreelist_E2E_Reload(t *testing.T) {\n\tfreelist := newTestFreelist()\n\tfreelist.Init([]common.Pgid{})\n\tfreelist.Free(common.Txid(2), common.NewPage(5, common.LeafPageFlag, 0, 1))\n\tfreelist.Free(common.Txid(2), common.NewPage(8, common.LeafPageFlag, 0, 0))\n\tfreelist.ReleasePendingPages()\n\trequirePages(t, freelist, common.Pgids{5, 6, 8}, common.Pgids{})\n\tbuf := make([]byte, 4096)\n\tp := common.LoadPage(buf)\n\tfreelist.Write(p)\n\n\tfreelist.Free(common.Txid(3), common.NewPage(3, common.LeafPageFlag, 0, 1))\n\tfreelist.Free(common.Txid(3), common.NewPage(10, common.LeafPageFlag, 0, 2))\n\trequirePages(t, freelist, common.Pgids{5, 6, 8}, common.Pgids{3, 4, 10, 11, 12})\n\n\totherBuf := make([]byte, 4096)\n\tpx := common.LoadPage(otherBuf)\n\tfreelist.Write(px)\n\n\tloadFreeList := newTestFreelist()\n\tloadFreeList.Init([]common.Pgid{})\n\tloadFreeList.Read(px)\n\trequirePages(t, loadFreeList, common.Pgids{3, 4, 5, 6, 8, 10, 11, 12}, common.Pgids{})\n\t// restore the original freelist again\n\tloadFreeList.Reload(p)\n\trequirePages(t, loadFreeList, common.Pgids{5, 6, 8}, common.Pgids{})\n\n\t// reload another page with different free pages to test we are deduplicating the free pages with the pending ones correctly\n\tfreelist = newTestFreelist()\n\tfreelist.Init([]common.Pgid{})\n\tfreelist.Free(common.Txid(5), common.NewPage(5, common.LeafPageFlag, 0, 4))\n\tfreelist.Reload(p)\n\trequirePages(t, freelist, common.Pgids{}, common.Pgids{5, 6, 7, 8, 9})\n}\n\n// tests the loading and reloading from physical pages\nfunc TestFreelist_E2E_SerDe_HappyPath(t *testing.T) {\n\tfreelist := newTestFreelist()\n\tfreelist.Init([]common.Pgid{})\n\tfreelist.Free(common.Txid(2), common.NewPage(5, common.LeafPageFlag, 0, 1))\n\tfreelist.Free(common.Txid(2), common.NewPage(8, common.LeafPageFlag, 0, 0))\n\tfreelist.ReleasePendingPages()\n\trequirePages(t, freelist, common.Pgids{5, 6, 8}, common.Pgids{})\n\n\tfreelist.Free(common.Txid(3), common.NewPage(3, common.LeafPageFlag, 0, 1))\n\tfreelist.Free(common.Txid(3), common.NewPage(10, common.LeafPageFlag, 0, 2))\n\trequirePages(t, freelist, common.Pgids{5, 6, 8}, common.Pgids{3, 4, 10, 11, 12})\n\n\tbuf := make([]byte, 4096)\n\tp := common.LoadPage(buf)\n\trequire.Equal(t, 80, freelist.EstimatedWritePageSize())\n\tfreelist.Write(p)\n\n\tloadFreeList := newTestFreelist()\n\tloadFreeList.Init([]common.Pgid{})\n\tloadFreeList.Read(p)\n\trequirePages(t, loadFreeList, common.Pgids{3, 4, 5, 6, 8, 10, 11, 12}, common.Pgids{})\n}\n\n// tests the loading of a freelist against other implementations with various sizes\nfunc TestFreelist_E2E_SerDe_AcrossImplementations(t *testing.T) {\n\ttestSizes := []int{0, 1, 10, 100, 1000, math.MaxUint16, math.MaxUint16 + 1, math.MaxUint16 * 2}\n\tfor _, size := range testSizes {\n\t\tt.Run(fmt.Sprintf(\"n=%d\", size), func(t *testing.T) {\n\t\t\tfreelist := newTestFreelist()\n\t\t\texpectedFreePgids := common.Pgids{}\n\t\t\tfor i := 0; i < size; i++ {\n\t\t\t\tpgid := common.Pgid(i + 2)\n\t\t\t\tfreelist.Free(common.Txid(1), common.NewPage(pgid, common.LeafPageFlag, 0, 0))\n\t\t\t\texpectedFreePgids = append(expectedFreePgids, pgid)\n\t\t\t}\n\t\t\tfreelist.ReleasePendingPages()\n\t\t\trequirePages(t, freelist, expectedFreePgids, common.Pgids{})\n\t\t\tbuf := make([]byte, freelist.EstimatedWritePageSize())\n\t\t\tp := common.LoadPage(buf)\n\t\t\tfreelist.Write(p)\n\n\t\t\tfor n, loadFreeList := range map[string]Interface{\n\t\t\t\t\"hashmap\": NewHashMapFreelist(),\n\t\t\t\t\"array\":   NewArrayFreelist(),\n\t\t\t} {\n\t\t\t\tt.Run(n, func(t *testing.T) {\n\t\t\t\t\tloadFreeList.Read(p)\n\t\t\t\t\trequirePages(t, loadFreeList, expectedFreePgids, common.Pgids{})\n\t\t\t\t})\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc requirePages(t *testing.T, f Interface, freePageIds common.Pgids, pendingPageIds common.Pgids) {\n\trequire.Equal(t, f.FreeCount()+f.PendingCount(), f.Count())\n\trequire.Equalf(t, freePageIds, f.freePageIds(), \"unexpected free pages\")\n\trequire.Equal(t, len(freePageIds), f.FreeCount())\n\n\tpp := allPendingPages(f.pendingPageIds())\n\trequire.Equalf(t, pendingPageIds, pp, \"unexpected pending pages\")\n\trequire.Equal(t, len(pp), f.PendingCount())\n\n\tfor _, pgid := range f.freePageIds() {\n\t\trequire.Truef(t, f.Freed(pgid), \"expected free page to return true on Freed\")\n\t}\n\n\tfor _, pgid := range pp {\n\t\trequire.Truef(t, f.Freed(pgid), \"expected pending page to return true on Freed\")\n\t}\n}\n\nfunc allPendingPages(p map[common.Txid]*txPending) common.Pgids {\n\tpgids := common.Pgids{}\n\tfor _, pending := range p {\n\t\tpgids = append(pgids, pending.ids...)\n\t}\n\tsort.Sort(pgids)\n\treturn pgids\n}\n\nfunc Benchmark_FreelistRelease10K(b *testing.B)    { benchmark_FreelistRelease(b, 10000) }\nfunc Benchmark_FreelistRelease100K(b *testing.B)   { benchmark_FreelistRelease(b, 100000) }\nfunc Benchmark_FreelistRelease1000K(b *testing.B)  { benchmark_FreelistRelease(b, 1000000) }\nfunc Benchmark_FreelistRelease10000K(b *testing.B) { benchmark_FreelistRelease(b, 10000000) }\n\nfunc benchmark_FreelistRelease(b *testing.B, size int) {\n\tids := randomPgids(size)\n\tpending := randomPgids(len(ids) / 400)\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\ttxp := &txPending{ids: pending}\n\t\tf := newTestFreelist()\n\t\tf.pendingPageIds()[1] = txp\n\t\tf.Init(ids)\n\t\tf.release(1)\n\t}\n}\n\nfunc randomPgids(n int) []common.Pgid {\n\tpgids := make(common.Pgids, n)\n\tfor i := range pgids {\n\t\tpgids[i] = common.Pgid(rand.Int63())\n\t}\n\tsort.Sort(pgids)\n\treturn pgids\n}\n\nfunc Test_freelist_ReadIDs_and_getFreePageIDs(t *testing.T) {\n\tf := newTestFreelist()\n\texp := common.Pgids([]common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18})\n\n\tf.Init(exp)\n\n\tif got := f.freePageIds(); !reflect.DeepEqual(exp, got) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, got)\n\t}\n\n\tf2 := newTestFreelist()\n\texp2 := []common.Pgid{}\n\tf2.Init(exp2)\n\n\tif got2 := f2.freePageIds(); !reflect.DeepEqual(got2, common.Pgids(exp2)) {\n\t\tt.Fatalf(\"exp2=%#v; got2=%#v\", exp2, got2)\n\t}\n\n}\n\n// newTestFreelist get the freelist type from env and initial the freelist\nfunc newTestFreelist() Interface {\n\tif env := os.Getenv(TestFreelistType); env == \"hashmap\" {\n\t\treturn NewHashMapFreelist()\n\t}\n\n\treturn NewArrayFreelist()\n}\n"
  },
  {
    "path": "internal/freelist/hashmap.go",
    "content": "package freelist\n\nimport (\n\t\"fmt\"\n\t\"reflect\"\n\t\"sort\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// pidSet holds the set of starting pgids which have the same span size\ntype pidSet map[common.Pgid]struct{}\n\ntype hashMap struct {\n\t*shared\n\n\tfreePagesCount uint64                 // count of free pages(hashmap version)\n\tfreemaps       map[uint64]pidSet      // key is the size of continuous pages(span), value is a set which contains the starting pgids of same size\n\tforwardMap     map[common.Pgid]uint64 // key is start pgid, value is its span size\n\tbackwardMap    map[common.Pgid]uint64 // key is end pgid, value is its span size\n}\n\nfunc (f *hashMap) Init(pgids common.Pgids) {\n\t// reset the counter when freelist init\n\tf.freePagesCount = 0\n\tf.freemaps = make(map[uint64]pidSet)\n\tf.forwardMap = make(map[common.Pgid]uint64)\n\tf.backwardMap = make(map[common.Pgid]uint64)\n\n\tif len(pgids) == 0 {\n\t\treturn\n\t}\n\n\tif !sort.SliceIsSorted([]common.Pgid(pgids), func(i, j int) bool { return pgids[i] < pgids[j] }) {\n\t\tpanic(\"pgids not sorted\")\n\t}\n\n\tsize := uint64(1)\n\tstart := pgids[0]\n\n\tfor i := 1; i < len(pgids); i++ {\n\t\t// continuous page\n\t\tif pgids[i] == pgids[i-1]+1 {\n\t\t\tsize++\n\t\t} else {\n\t\t\tf.addSpan(start, size)\n\n\t\t\tsize = 1\n\t\t\tstart = pgids[i]\n\t\t}\n\t}\n\n\t// init the tail\n\tif size != 0 && start != 0 {\n\t\tf.addSpan(start, size)\n\t}\n\n\tf.reindex()\n}\n\nfunc (f *hashMap) Allocate(txid common.Txid, n int) common.Pgid {\n\tif n == 0 {\n\t\treturn 0\n\t}\n\n\t// if we have a exact size match just return short path\n\tif bm, ok := f.freemaps[uint64(n)]; ok {\n\t\tfor pid := range bm {\n\t\t\t// remove the span\n\t\t\tf.delSpan(pid, uint64(n))\n\n\t\t\tf.allocs[pid] = txid\n\n\t\t\tfor i := common.Pgid(0); i < common.Pgid(n); i++ {\n\t\t\t\tdelete(f.cache, pid+i)\n\t\t\t}\n\t\t\treturn pid\n\t\t}\n\t}\n\n\t// lookup the map to find larger span\n\tfor size, bm := range f.freemaps {\n\t\tif size < uint64(n) {\n\t\t\tcontinue\n\t\t}\n\n\t\tfor pid := range bm {\n\t\t\t// remove the initial\n\t\t\tf.delSpan(pid, size)\n\n\t\t\tf.allocs[pid] = txid\n\n\t\t\tremain := size - uint64(n)\n\n\t\t\t// add remain span\n\t\t\tf.addSpan(pid+common.Pgid(n), remain)\n\n\t\t\tfor i := common.Pgid(0); i < common.Pgid(n); i++ {\n\t\t\t\tdelete(f.cache, pid+i)\n\t\t\t}\n\t\t\treturn pid\n\t\t}\n\t}\n\n\treturn 0\n}\n\nfunc (f *hashMap) FreeCount() int {\n\tcommon.Verify(func() {\n\t\texpectedFreePageCount := f.hashmapFreeCountSlow()\n\t\tcommon.Assert(int(f.freePagesCount) == expectedFreePageCount,\n\t\t\t\"freePagesCount (%d) is out of sync with free pages map (%d)\", f.freePagesCount, expectedFreePageCount)\n\t})\n\treturn int(f.freePagesCount)\n}\n\nfunc (f *hashMap) freePageIds() common.Pgids {\n\tcount := f.FreeCount()\n\tif count == 0 {\n\t\treturn common.Pgids{}\n\t}\n\n\tm := make([]common.Pgid, 0, count)\n\n\tstartPageIds := make([]common.Pgid, 0, len(f.forwardMap))\n\tfor k := range f.forwardMap {\n\t\tstartPageIds = append(startPageIds, k)\n\t}\n\tsort.Sort(common.Pgids(startPageIds))\n\n\tfor _, start := range startPageIds {\n\t\tif size, ok := f.forwardMap[start]; ok {\n\t\t\tfor i := 0; i < int(size); i++ {\n\t\t\t\tm = append(m, start+common.Pgid(i))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn m\n}\n\nfunc (f *hashMap) hashmapFreeCountSlow() int {\n\tcount := 0\n\tfor _, size := range f.forwardMap {\n\t\tcount += int(size)\n\t}\n\treturn count\n}\n\nfunc (f *hashMap) addSpan(start common.Pgid, size uint64) {\n\tf.backwardMap[start-1+common.Pgid(size)] = size\n\tf.forwardMap[start] = size\n\tif _, ok := f.freemaps[size]; !ok {\n\t\tf.freemaps[size] = make(map[common.Pgid]struct{})\n\t}\n\n\tf.freemaps[size][start] = struct{}{}\n\tf.freePagesCount += size\n}\n\nfunc (f *hashMap) delSpan(start common.Pgid, size uint64) {\n\tdelete(f.forwardMap, start)\n\tdelete(f.backwardMap, start+common.Pgid(size-1))\n\tdelete(f.freemaps[size], start)\n\tif len(f.freemaps[size]) == 0 {\n\t\tdelete(f.freemaps, size)\n\t}\n\tf.freePagesCount -= size\n}\n\nfunc (f *hashMap) mergeSpans(ids common.Pgids) {\n\tcommon.Verify(func() {\n\t\tids1Freemap := f.idsFromFreemaps()\n\t\tids2Forward := f.idsFromForwardMap()\n\t\tids3Backward := f.idsFromBackwardMap()\n\n\t\tif !reflect.DeepEqual(ids1Freemap, ids2Forward) {\n\t\t\tpanic(fmt.Sprintf(\"Detected mismatch, f.freemaps: %v, f.forwardMap: %v\", f.freemaps, f.forwardMap))\n\t\t}\n\t\tif !reflect.DeepEqual(ids1Freemap, ids3Backward) {\n\t\t\tpanic(fmt.Sprintf(\"Detected mismatch, f.freemaps: %v, f.backwardMap: %v\", f.freemaps, f.backwardMap))\n\t\t}\n\n\t\tsort.Sort(ids)\n\t\tprev := common.Pgid(0)\n\t\tfor _, id := range ids {\n\t\t\t// The ids shouldn't have duplicated free ID.\n\t\t\tif prev == id {\n\t\t\t\tpanic(fmt.Sprintf(\"detected duplicated free ID: %d in ids: %v\", id, ids))\n\t\t\t}\n\t\t\tprev = id\n\n\t\t\t// The ids shouldn't have any overlap with the existing f.freemaps.\n\t\t\tif _, ok := ids1Freemap[id]; ok {\n\t\t\t\tpanic(fmt.Sprintf(\"detected overlapped free page ID: %d between ids: %v and existing f.freemaps: %v\", id, ids, f.freemaps))\n\t\t\t}\n\t\t}\n\t})\n\tfor _, id := range ids {\n\t\t// try to see if we can merge and update\n\t\tf.mergeWithExistingSpan(id)\n\t}\n}\n\n// mergeWithExistingSpan merges pid to the existing free spans, try to merge it backward and forward\nfunc (f *hashMap) mergeWithExistingSpan(pid common.Pgid) {\n\tprev := pid - 1\n\tnext := pid + 1\n\n\tpreSize, mergeWithPrev := f.backwardMap[prev]\n\tnextSize, mergeWithNext := f.forwardMap[next]\n\tnewStart := pid\n\tnewSize := uint64(1)\n\n\tif mergeWithPrev {\n\t\t//merge with previous span\n\t\tstart := prev + 1 - common.Pgid(preSize)\n\t\tf.delSpan(start, preSize)\n\n\t\tnewStart -= common.Pgid(preSize)\n\t\tnewSize += preSize\n\t}\n\n\tif mergeWithNext {\n\t\t// merge with next span\n\t\tf.delSpan(next, nextSize)\n\t\tnewSize += nextSize\n\t}\n\n\tf.addSpan(newStart, newSize)\n}\n\n// idsFromFreemaps get all free page IDs from f.freemaps.\n// used by test only.\nfunc (f *hashMap) idsFromFreemaps() map[common.Pgid]struct{} {\n\tids := make(map[common.Pgid]struct{})\n\tfor size, idSet := range f.freemaps {\n\t\tfor start := range idSet {\n\t\t\tfor i := 0; i < int(size); i++ {\n\t\t\t\tid := start + common.Pgid(i)\n\t\t\t\tif _, ok := ids[id]; ok {\n\t\t\t\t\tpanic(fmt.Sprintf(\"detected duplicated free page ID: %d in f.freemaps: %v\", id, f.freemaps))\n\t\t\t\t}\n\t\t\t\tids[id] = struct{}{}\n\t\t\t}\n\t\t}\n\t}\n\treturn ids\n}\n\n// idsFromForwardMap get all free page IDs from f.forwardMap.\n// used by test only.\nfunc (f *hashMap) idsFromForwardMap() map[common.Pgid]struct{} {\n\tids := make(map[common.Pgid]struct{})\n\tfor start, size := range f.forwardMap {\n\t\tfor i := 0; i < int(size); i++ {\n\t\t\tid := start + common.Pgid(i)\n\t\t\tif _, ok := ids[id]; ok {\n\t\t\t\tpanic(fmt.Sprintf(\"detected duplicated free page ID: %d in f.forwardMap: %v\", id, f.forwardMap))\n\t\t\t}\n\t\t\tids[id] = struct{}{}\n\t\t}\n\t}\n\treturn ids\n}\n\n// idsFromBackwardMap get all free page IDs from f.backwardMap.\n// used by test only.\nfunc (f *hashMap) idsFromBackwardMap() map[common.Pgid]struct{} {\n\tids := make(map[common.Pgid]struct{})\n\tfor end, size := range f.backwardMap {\n\t\tfor i := 0; i < int(size); i++ {\n\t\t\tid := end - common.Pgid(i)\n\t\t\tif _, ok := ids[id]; ok {\n\t\t\t\tpanic(fmt.Sprintf(\"detected duplicated free page ID: %d in f.backwardMap: %v\", id, f.backwardMap))\n\t\t\t}\n\t\t\tids[id] = struct{}{}\n\t\t}\n\t}\n\treturn ids\n}\n\nfunc NewHashMapFreelist() Interface {\n\thm := &hashMap{\n\t\tshared:      newShared(),\n\t\tfreemaps:    make(map[uint64]pidSet),\n\t\tforwardMap:  make(map[common.Pgid]uint64),\n\t\tbackwardMap: make(map[common.Pgid]uint64),\n\t}\n\thm.Interface = hm\n\treturn hm\n}\n"
  },
  {
    "path": "internal/freelist/hashmap_test.go",
    "content": "package freelist\n\nimport (\n\t\"math/rand\"\n\t\"reflect\"\n\t\"sort\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\nfunc TestFreelistHashmap_init_panics(t *testing.T) {\n\tf := NewHashMapFreelist()\n\trequire.Panics(t, func() {\n\t\t// init expects sorted input\n\t\tf.Init([]common.Pgid{25, 5})\n\t})\n}\n\nfunc TestFreelistHashmap_allocate(t *testing.T) {\n\tf := NewHashMapFreelist()\n\n\tids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}\n\tf.Init(ids)\n\n\tf.Allocate(1, 3)\n\tif x := f.FreeCount(); x != 6 {\n\t\tt.Fatalf(\"exp=6; got=%v\", x)\n\t}\n\n\tf.Allocate(1, 2)\n\tif x := f.FreeCount(); x != 4 {\n\t\tt.Fatalf(\"exp=4; got=%v\", x)\n\t}\n\tf.Allocate(1, 1)\n\tif x := f.FreeCount(); x != 3 {\n\t\tt.Fatalf(\"exp=3; got=%v\", x)\n\t}\n\n\tf.Allocate(1, 0)\n\tif x := f.FreeCount(); x != 3 {\n\t\tt.Fatalf(\"exp=3; got=%v\", x)\n\t}\n}\n\nfunc TestFreelistHashmap_mergeWithExist(t *testing.T) {\n\tbm1 := pidSet{1: struct{}{}}\n\n\tbm2 := pidSet{5: struct{}{}}\n\ttests := []struct {\n\t\tname            string\n\t\tids             common.Pgids\n\t\tpgid            common.Pgid\n\t\twant            common.Pgids\n\t\twantForwardmap  map[common.Pgid]uint64\n\t\twantBackwardmap map[common.Pgid]uint64\n\t\twantfreemap     map[uint64]pidSet\n\t}{\n\t\t{\n\t\t\tname:            \"test1\",\n\t\t\tids:             []common.Pgid{1, 2, 4, 5, 6},\n\t\t\tpgid:            3,\n\t\t\twant:            []common.Pgid{1, 2, 3, 4, 5, 6},\n\t\t\twantForwardmap:  map[common.Pgid]uint64{1: 6},\n\t\t\twantBackwardmap: map[common.Pgid]uint64{6: 6},\n\t\t\twantfreemap:     map[uint64]pidSet{6: bm1},\n\t\t},\n\t\t{\n\t\t\tname:            \"test2\",\n\t\t\tids:             []common.Pgid{1, 2, 5, 6},\n\t\t\tpgid:            3,\n\t\t\twant:            []common.Pgid{1, 2, 3, 5, 6},\n\t\t\twantForwardmap:  map[common.Pgid]uint64{1: 3, 5: 2},\n\t\t\twantBackwardmap: map[common.Pgid]uint64{6: 2, 3: 3},\n\t\t\twantfreemap:     map[uint64]pidSet{3: bm1, 2: bm2},\n\t\t},\n\t\t{\n\t\t\tname:            \"test3\",\n\t\t\tids:             []common.Pgid{1, 2},\n\t\t\tpgid:            3,\n\t\t\twant:            []common.Pgid{1, 2, 3},\n\t\t\twantForwardmap:  map[common.Pgid]uint64{1: 3},\n\t\t\twantBackwardmap: map[common.Pgid]uint64{3: 3},\n\t\t\twantfreemap:     map[uint64]pidSet{3: bm1},\n\t\t},\n\t\t{\n\t\t\tname:            \"test4\",\n\t\t\tids:             []common.Pgid{2, 3},\n\t\t\tpgid:            1,\n\t\t\twant:            []common.Pgid{1, 2, 3},\n\t\t\twantForwardmap:  map[common.Pgid]uint64{1: 3},\n\t\t\twantBackwardmap: map[common.Pgid]uint64{3: 3},\n\t\t\twantfreemap:     map[uint64]pidSet{3: bm1},\n\t\t},\n\t}\n\tfor _, tt := range tests {\n\t\tf := newTestHashMapFreelist()\n\t\tf.Init(tt.ids)\n\n\t\tf.mergeWithExistingSpan(tt.pgid)\n\n\t\tif got := f.freePageIds(); !reflect.DeepEqual(tt.want, got) {\n\t\t\tt.Fatalf(\"name %s; exp=%v; got=%v\", tt.name, tt.want, got)\n\t\t}\n\t\tif got := f.forwardMap; !reflect.DeepEqual(tt.wantForwardmap, got) {\n\t\t\tt.Fatalf(\"name %s; exp=%v; got=%v\", tt.name, tt.wantForwardmap, got)\n\t\t}\n\t\tif got := f.backwardMap; !reflect.DeepEqual(tt.wantBackwardmap, got) {\n\t\t\tt.Fatalf(\"name %s; exp=%v; got=%v\", tt.name, tt.wantBackwardmap, got)\n\t\t}\n\t\tif got := f.freemaps; !reflect.DeepEqual(tt.wantfreemap, got) {\n\t\t\tt.Fatalf(\"name %s; exp=%v; got=%v\", tt.name, tt.wantfreemap, got)\n\t\t}\n\t}\n}\n\nfunc TestFreelistHashmap_GetFreePageIDs(t *testing.T) {\n\tf := newTestHashMapFreelist()\n\n\tN := int32(100000)\n\tfm := make(map[common.Pgid]uint64)\n\ti := int32(0)\n\tval := int32(0)\n\tfor i = 0; i < N; {\n\t\tval = rand.Int31n(1000)\n\t\tfm[common.Pgid(i)] = uint64(val)\n\t\ti += val\n\t\tf.freePagesCount += uint64(val)\n\t}\n\n\tf.forwardMap = fm\n\tres := f.freePageIds()\n\n\tif !sort.SliceIsSorted(res, func(i, j int) bool { return res[i] < res[j] }) {\n\t\tt.Fatalf(\"pgids not sorted\")\n\t}\n}\n\nfunc Test_Freelist_Hashmap_Rollback(t *testing.T) {\n\tf := newTestHashMapFreelist()\n\n\tf.Init([]common.Pgid{3, 5, 6, 7, 12, 13})\n\n\tf.Free(100, common.NewPage(20, 0, 0, 1))\n\tf.Allocate(100, 3)\n\tf.Free(100, common.NewPage(25, 0, 0, 0))\n\tf.Allocate(100, 2)\n\n\trequire.Equal(t, map[common.Pgid]common.Txid{5: 100, 12: 100}, f.allocs)\n\trequire.Equal(t, map[common.Txid]*txPending{100: {\n\t\tids:     []common.Pgid{20, 21, 25},\n\t\talloctx: []common.Txid{0, 0, 0},\n\t}}, f.pending)\n\n\tf.Rollback(100)\n\n\trequire.Equal(t, map[common.Pgid]common.Txid{}, f.allocs)\n\trequire.Equal(t, map[common.Txid]*txPending{}, f.pending)\n}\n\nfunc Benchmark_freelist_hashmapGetFreePageIDs(b *testing.B) {\n\tf := newTestHashMapFreelist()\n\tN := int32(100000)\n\tfm := make(map[common.Pgid]uint64)\n\ti := int32(0)\n\tval := int32(0)\n\tfor i = 0; i < N; {\n\t\tval = rand.Int31n(1000)\n\t\tfm[common.Pgid(i)] = uint64(val)\n\t\ti += val\n\t}\n\n\tf.forwardMap = fm\n\n\tb.ReportAllocs()\n\tb.ResetTimer()\n\tfor n := 0; n < b.N; n++ {\n\t\tf.freePageIds()\n\t}\n}\n\nfunc newTestHashMapFreelist() *hashMap {\n\tf := NewHashMapFreelist()\n\treturn f.(*hashMap)\n}\n"
  },
  {
    "path": "internal/freelist/shared.go",
    "content": "package freelist\n\nimport (\n\t\"fmt\"\n\t\"math\"\n\t\"sort\"\n\t\"unsafe\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\ntype txPending struct {\n\tids              []common.Pgid\n\talloctx          []common.Txid // txids allocating the ids\n\tlastReleaseBegin common.Txid   // beginning txid of last matching releaseRange\n}\n\ntype shared struct {\n\tInterface\n\n\treadonlyTXIDs []common.Txid               // all readonly transaction IDs.\n\tallocs        map[common.Pgid]common.Txid // mapping of Txid that allocated a pgid.\n\tcache         map[common.Pgid]struct{}    // fast lookup of all free and pending page ids.\n\tpending       map[common.Txid]*txPending  // mapping of soon-to-be free page ids by tx.\n}\n\nfunc newShared() *shared {\n\treturn &shared{\n\t\tpending: make(map[common.Txid]*txPending),\n\t\tallocs:  make(map[common.Pgid]common.Txid),\n\t\tcache:   make(map[common.Pgid]struct{}),\n\t}\n}\n\nfunc (t *shared) pendingPageIds() map[common.Txid]*txPending {\n\treturn t.pending\n}\n\nfunc (t *shared) PendingCount() int {\n\tvar count int\n\tfor _, txp := range t.pending {\n\t\tcount += len(txp.ids)\n\t}\n\treturn count\n}\n\nfunc (t *shared) Count() int {\n\treturn t.FreeCount() + t.PendingCount()\n}\n\nfunc (t *shared) Freed(pgId common.Pgid) bool {\n\t_, ok := t.cache[pgId]\n\treturn ok\n}\n\nfunc (t *shared) Free(txid common.Txid, p *common.Page) {\n\tif p.Id() <= 1 {\n\t\tpanic(fmt.Sprintf(\"cannot free page 0 or 1: %d\", p.Id()))\n\t}\n\n\t// Free page and all its overflow pages.\n\ttxp := t.pending[txid]\n\tif txp == nil {\n\t\ttxp = &txPending{}\n\t\tt.pending[txid] = txp\n\t}\n\tallocTxid, ok := t.allocs[p.Id()]\n\tcommon.Verify(func() {\n\t\tif allocTxid == txid {\n\t\t\tpanic(fmt.Sprintf(\"free: freed page (%d) was allocated by the same transaction (%d)\", p.Id(), txid))\n\t\t}\n\t})\n\tif ok {\n\t\tdelete(t.allocs, p.Id())\n\t}\n\n\tfor id := p.Id(); id <= p.Id()+common.Pgid(p.Overflow()); id++ {\n\t\t// Verify that page is not already free.\n\t\tif _, ok := t.cache[id]; ok {\n\t\t\tpanic(fmt.Sprintf(\"page %d already freed\", id))\n\t\t}\n\t\t// Add to the freelist and cache.\n\t\ttxp.ids = append(txp.ids, id)\n\t\ttxp.alloctx = append(txp.alloctx, allocTxid)\n\t\tt.cache[id] = struct{}{}\n\t}\n}\n\nfunc (t *shared) Rollback(txid common.Txid) {\n\t// Remove page ids from cache.\n\ttxp := t.pending[txid]\n\tif txp == nil {\n\t\treturn\n\t}\n\tfor i, pgid := range txp.ids {\n\t\tdelete(t.cache, pgid)\n\t\ttx := txp.alloctx[i]\n\t\tif tx == 0 {\n\t\t\tcontinue\n\t\t}\n\t\tif tx != txid {\n\t\t\t// Pending free aborted; restore page back to alloc list.\n\t\t\tt.allocs[pgid] = tx\n\t\t} else {\n\t\t\t// A writing TXN should never free a page which was allocated by itself.\n\t\t\tpanic(fmt.Sprintf(\"rollback: freed page (%d) was allocated by the same transaction (%d)\", pgid, txid))\n\t\t}\n\t}\n\t// Remove pages from pending list and mark as free if allocated by txid.\n\tdelete(t.pending, txid)\n\n\t// Remove pgids which are allocated by this txid\n\tfor pgid, tid := range t.allocs {\n\t\tif tid == txid {\n\t\t\tdelete(t.allocs, pgid)\n\t\t}\n\t}\n}\n\nfunc (t *shared) AddReadonlyTXID(tid common.Txid) {\n\tt.readonlyTXIDs = append(t.readonlyTXIDs, tid)\n}\n\nfunc (t *shared) RemoveReadonlyTXID(tid common.Txid) {\n\tfor i := range t.readonlyTXIDs {\n\t\tif t.readonlyTXIDs[i] == tid {\n\t\t\tlast := len(t.readonlyTXIDs) - 1\n\t\t\tt.readonlyTXIDs[i] = t.readonlyTXIDs[last]\n\t\t\tt.readonlyTXIDs = t.readonlyTXIDs[:last]\n\t\t\tbreak\n\t\t}\n\t}\n}\n\ntype txIDx []common.Txid\n\nfunc (t txIDx) Len() int           { return len(t) }\nfunc (t txIDx) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }\nfunc (t txIDx) Less(i, j int) bool { return t[i] < t[j] }\n\nfunc (t *shared) ReleasePendingPages() {\n\t// Free all pending pages prior to the earliest open transaction.\n\tsort.Sort(txIDx(t.readonlyTXIDs))\n\tminid := common.Txid(math.MaxUint64)\n\tif len(t.readonlyTXIDs) > 0 {\n\t\tminid = t.readonlyTXIDs[0]\n\t}\n\tif minid > 0 {\n\t\tt.release(minid - 1)\n\t}\n\t// Release unused txid extents.\n\tfor _, tid := range t.readonlyTXIDs {\n\t\tt.releaseRange(minid, tid-1)\n\t\tminid = tid + 1\n\t}\n\tt.releaseRange(minid, common.Txid(math.MaxUint64))\n\t// Any page both allocated and freed in an extent is safe to release.\n}\n\nfunc (t *shared) release(txid common.Txid) {\n\tm := make(common.Pgids, 0)\n\tfor tid, txp := range t.pending {\n\t\tif tid <= txid {\n\t\t\t// Move transaction's pending pages to the available freelist.\n\t\t\t// Don't remove from the cache since the page is still free.\n\t\t\tm = append(m, txp.ids...)\n\t\t\tdelete(t.pending, tid)\n\t\t}\n\t}\n\tt.mergeSpans(m)\n}\n\nfunc (t *shared) releaseRange(begin, end common.Txid) {\n\tif begin > end {\n\t\treturn\n\t}\n\tm := common.Pgids{}\n\tfor tid, txp := range t.pending {\n\t\tif tid < begin || tid > end {\n\t\t\tcontinue\n\t\t}\n\t\t// Don't recompute freed pages if ranges haven't updated.\n\t\tif txp.lastReleaseBegin == begin {\n\t\t\tcontinue\n\t\t}\n\t\tfor i := 0; i < len(txp.ids); i++ {\n\t\t\tif atx := txp.alloctx[i]; atx < begin || atx > end {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tm = append(m, txp.ids[i])\n\t\t\ttxp.ids[i] = txp.ids[len(txp.ids)-1]\n\t\t\ttxp.ids = txp.ids[:len(txp.ids)-1]\n\t\t\ttxp.alloctx[i] = txp.alloctx[len(txp.alloctx)-1]\n\t\t\ttxp.alloctx = txp.alloctx[:len(txp.alloctx)-1]\n\t\t\ti--\n\t\t}\n\t\ttxp.lastReleaseBegin = begin\n\t\tif len(txp.ids) == 0 {\n\t\t\tdelete(t.pending, tid)\n\t\t}\n\t}\n\tt.mergeSpans(m)\n}\n\n// Copyall copies a list of all free ids and all pending ids in one sorted list.\n// f.count returns the minimum length required for dst.\nfunc (t *shared) Copyall(dst []common.Pgid) {\n\tm := make(common.Pgids, 0, t.PendingCount())\n\tfor _, txp := range t.pendingPageIds() {\n\t\tm = append(m, txp.ids...)\n\t}\n\tsort.Sort(m)\n\tcommon.Mergepgids(dst, t.freePageIds(), m)\n}\n\nfunc (t *shared) Reload(p *common.Page) {\n\tt.Read(p)\n\tt.NoSyncReload(t.freePageIds())\n}\n\nfunc (t *shared) NoSyncReload(pgIds common.Pgids) {\n\t// Build a cache of only pending pages.\n\tpcache := make(map[common.Pgid]struct{})\n\tfor _, txp := range t.pending {\n\t\tfor _, pendingID := range txp.ids {\n\t\t\tpcache[pendingID] = struct{}{}\n\t\t}\n\t}\n\n\t// Check each page in the freelist and build a new available freelist\n\t// with any pages not in the pending lists.\n\ta := []common.Pgid{}\n\tfor _, id := range pgIds {\n\t\tif _, ok := pcache[id]; !ok {\n\t\t\ta = append(a, id)\n\t\t}\n\t}\n\n\tt.Init(a)\n}\n\n// reindex rebuilds the free cache based on available and pending free lists.\nfunc (t *shared) reindex() {\n\tfree := t.freePageIds()\n\tpending := t.pendingPageIds()\n\tt.cache = make(map[common.Pgid]struct{}, len(free))\n\tfor _, id := range free {\n\t\tt.cache[id] = struct{}{}\n\t}\n\tfor _, txp := range pending {\n\t\tfor _, pendingID := range txp.ids {\n\t\t\tt.cache[pendingID] = struct{}{}\n\t\t}\n\t}\n}\n\nfunc (t *shared) Read(p *common.Page) {\n\tif !p.IsFreelistPage() {\n\t\tpanic(fmt.Sprintf(\"invalid freelist page: %d, page type is %s\", p.Id(), p.Typ()))\n\t}\n\n\tids := p.FreelistPageIds()\n\n\t// Copy the list of page ids from the freelist.\n\tif len(ids) == 0 {\n\t\tt.Init([]common.Pgid{})\n\t} else {\n\t\t// copy the ids, so we don't modify on the freelist page directly\n\t\tidsCopy := make([]common.Pgid, len(ids))\n\t\tcopy(idsCopy, ids)\n\t\t// Make sure they're sorted.\n\t\tsort.Sort(common.Pgids(idsCopy))\n\n\t\tt.Init(idsCopy)\n\t}\n}\n\nfunc (t *shared) EstimatedWritePageSize() int {\n\tn := t.Count()\n\tif n >= 0xFFFF {\n\t\t// The first element will be used to store the count. See freelist.write.\n\t\tn++\n\t}\n\treturn int(common.PageHeaderSize) + (int(unsafe.Sizeof(common.Pgid(0))) * n)\n}\n\nfunc (t *shared) Write(p *common.Page) {\n\t// Combine the old free pgids and pgids waiting on an open transaction.\n\n\t// Update the header flag.\n\tp.SetFlags(common.FreelistPageFlag)\n\n\t// The page.count can only hold up to 64k elements so if we overflow that\n\t// number then we handle it by putting the size in the first element.\n\tl := t.Count()\n\tif l == 0 {\n\t\tp.SetCount(uint16(l))\n\t} else if l < 0xFFFF {\n\t\tp.SetCount(uint16(l))\n\t\tdata := common.UnsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))\n\t\tids := unsafe.Slice((*common.Pgid)(data), l)\n\t\tt.Copyall(ids)\n\t} else {\n\t\tp.SetCount(0xFFFF)\n\t\tdata := common.UnsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))\n\t\tids := unsafe.Slice((*common.Pgid)(data), l+1)\n\t\tids[0] = common.Pgid(l)\n\t\tt.Copyall(ids[1:])\n\t}\n}\n"
  },
  {
    "path": "internal/guts_cli/guts_cli.go",
    "content": "package guts_cli\n\n// Low level access to pages / data-structures of the bbolt file.\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\nvar (\n\t// ErrCorrupt is returned when a checking a data file finds errors.\n\tErrCorrupt = errors.New(\"invalid value\")\n)\n\n// ReadPage reads Page info & full Page data from a path.\n// This is not transactionally safe.\nfunc ReadPage(path string, pageID uint64) (*common.Page, []byte, error) {\n\t// Find Page size.\n\tpageSize, hwm, err := ReadPageAndHWMSize(path)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"read Page size: %s\", err)\n\t}\n\n\t// Open database file.\n\tf, err := os.Open(path)\n\tif err != nil {\n\t\treturn nil, nil, err\n\t}\n\tdefer f.Close()\n\n\t// Read one block into buffer.\n\tbuf := make([]byte, pageSize)\n\tif n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil {\n\t\treturn nil, nil, err\n\t} else if n != len(buf) {\n\t\treturn nil, nil, io.ErrUnexpectedEOF\n\t}\n\n\t// Determine total number of blocks.\n\tp := common.LoadPage(buf)\n\tif p.Id() != common.Pgid(pageID) {\n\t\treturn nil, nil, fmt.Errorf(\"error: %w due to unexpected Page id: %d != %d\", ErrCorrupt, p.Id(), pageID)\n\t}\n\toverflowN := p.Overflow()\n\tif overflowN >= uint32(hwm)-3 { // we exclude 2 Meta pages and the current Page.\n\t\treturn nil, nil, fmt.Errorf(\"error: %w, Page claims to have %d overflow pages (>=hwm=%d). Interrupting to avoid risky OOM\", ErrCorrupt, overflowN, hwm)\n\t}\n\n\tif overflowN == 0 {\n\t\treturn p, buf, nil\n\t}\n\n\t// Re-read entire Page (with overflow) into buffer.\n\tbuf = make([]byte, (uint64(overflowN)+1)*pageSize)\n\tif n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil {\n\t\treturn nil, nil, err\n\t} else if n != len(buf) {\n\t\treturn nil, nil, io.ErrUnexpectedEOF\n\t}\n\tp = common.LoadPage(buf)\n\tif p.Id() != common.Pgid(pageID) {\n\t\treturn nil, nil, fmt.Errorf(\"error: %w due to unexpected Page id: %d != %d\", ErrCorrupt, p.Id(), pageID)\n\t}\n\n\treturn p, buf, nil\n}\n\nfunc WritePage(path string, pageBuf []byte) error {\n\tpage := common.LoadPage(pageBuf)\n\tpageSize, _, err := ReadPageAndHWMSize(path)\n\tif err != nil {\n\t\treturn err\n\t}\n\texpectedLen := pageSize * (uint64(page.Overflow()) + 1)\n\tif expectedLen != uint64(len(pageBuf)) {\n\t\treturn fmt.Errorf(\"WritePage: len(buf):%d != pageSize*(overflow+1):%d\", len(pageBuf), expectedLen)\n\t}\n\tf, err := os.OpenFile(path, os.O_WRONLY, 0)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer f.Close()\n\t_, err = f.WriteAt(pageBuf, int64(page.Id())*int64(pageSize))\n\treturn err\n}\n\n// ReadPageAndHWMSize reads Page size and HWM (id of the last+1 Page).\n// This is not transactionally safe.\nfunc ReadPageAndHWMSize(path string) (uint64, common.Pgid, error) {\n\t// Open database file.\n\tf, err := os.Open(path)\n\tif err != nil {\n\t\treturn 0, 0, err\n\t}\n\tdefer f.Close()\n\n\t// Read 4KB chunk.\n\tbuf := make([]byte, 4096)\n\tif _, err := io.ReadFull(f, buf); err != nil {\n\t\treturn 0, 0, err\n\t}\n\n\t// Read Page size from metadata.\n\tm := common.LoadPageMeta(buf)\n\tif m.Magic() != common.Magic {\n\t\treturn 0, 0, fmt.Errorf(\"the Meta Page has wrong (unexpected) magic\")\n\t}\n\treturn uint64(m.PageSize()), common.Pgid(m.Pgid()), nil\n}\n\n// GetRootPage returns the root-page (according to the most recent transaction).\nfunc GetRootPage(path string) (root common.Pgid, activeMeta common.Pgid, err error) {\n\tm, id, err := GetActiveMetaPage(path)\n\tif err != nil {\n\t\treturn 0, id, err\n\t}\n\treturn m.RootBucket().RootPage(), id, nil\n}\n\n// GetActiveMetaPage returns the active meta page and its page ID (0 or 1).\nfunc GetActiveMetaPage(path string) (*common.Meta, common.Pgid, error) {\n\t_, buf0, err0 := ReadPage(path, 0)\n\tif err0 != nil {\n\t\treturn nil, 0, err0\n\t}\n\tm0 := common.LoadPageMeta(buf0)\n\t_, buf1, err1 := ReadPage(path, 1)\n\tif err1 != nil {\n\t\treturn nil, 1, err1\n\t}\n\tm1 := common.LoadPageMeta(buf1)\n\tif m0.Txid() < m1.Txid() {\n\t\treturn m1, 1, nil\n\t} else {\n\t\treturn m0, 0, nil\n\t}\n}\n"
  },
  {
    "path": "internal/surgeon/surgeon.go",
    "content": "package surgeon\n\nimport (\n\t\"fmt\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\nfunc CopyPage(path string, srcPage common.Pgid, target common.Pgid) error {\n\tp1, d1, err1 := guts_cli.ReadPage(path, uint64(srcPage))\n\tif err1 != nil {\n\t\treturn err1\n\t}\n\tp1.SetId(target)\n\treturn guts_cli.WritePage(path, d1)\n}\n\nfunc ClearPage(path string, pgId common.Pgid) (bool, error) {\n\treturn ClearPageElements(path, pgId, 0, -1, false)\n}\n\n// ClearPageElements supports clearing elements in both branch and leaf\n// pages. Note if the ${abandonFreelist} is true, the freelist may be cleaned\n// in the meta pages in the following two cases, and bbolt needs to scan the\n// db to reconstruct free list. It may cause some delay on next startup,\n// depending on the db size.\n//  1. Any branch elements are cleared;\n//  2. An object saved in overflow pages is cleared;\n//\n// Usually ${abandonFreelist} defaults to false, it means it will not clear the\n// freelist in meta pages automatically. Users will receive a warning message\n// to remind them to explicitly execute `bbolt surgery abandom-freelist`\n// afterwards; the first return parameter will be true in such case. But if\n// the freelist isn't synced at all, no warning message will be displayed.\nfunc ClearPageElements(path string, pgId common.Pgid, start, end int, abandonFreelist bool) (bool, error) {\n\t// Read the page\n\tp, buf, err := guts_cli.ReadPage(path, uint64(pgId))\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"ReadPage failed: %w\", err)\n\t}\n\n\tif !p.IsLeafPage() && !p.IsBranchPage() {\n\t\treturn false, fmt.Errorf(\"can't clear elements in %q page\", p.Typ())\n\t}\n\n\telementCnt := int(p.Count())\n\n\tif elementCnt == 0 {\n\t\treturn false, nil\n\t}\n\n\tif start < 0 || start >= elementCnt {\n\t\treturn false, fmt.Errorf(\"the start index (%d) is out of range [0, %d)\", start, elementCnt)\n\t}\n\n\tif (end < 0 || end > elementCnt) && end != -1 {\n\t\treturn false, fmt.Errorf(\"the end index (%d) is out of range [0, %d]\", end, elementCnt)\n\t}\n\n\tif start > end && end != -1 {\n\t\treturn false, fmt.Errorf(\"the start index (%d) is bigger than the end index (%d)\", start, end)\n\t}\n\n\tif start == end {\n\t\treturn false, fmt.Errorf(\"invalid: the start index (%d) is equal to the end index (%d)\", start, end)\n\t}\n\n\tpreOverflow := p.Overflow()\n\n\tvar (\n\t\tdataWritten uint32\n\t)\n\tif end == int(p.Count()) || end == -1 {\n\t\tinodes := common.ReadInodeFromPage(p)\n\t\tinodes = inodes[:start]\n\n\t\tp.SetCount(uint16(start))\n\t\t// no need to write inode & data again, we just need to get\n\t\t// the data size which will be kept.\n\t\tdataWritten = common.UsedSpaceInPage(inodes, p)\n\t} else {\n\t\tinodes := common.ReadInodeFromPage(p)\n\t\tinodes = append(inodes[:start], inodes[end:]...)\n\n\t\tp.SetCount(uint16(len(inodes)))\n\t\tdataWritten = common.WriteInodeToPage(inodes, p)\n\t}\n\n\tpageSize, _, err := guts_cli.ReadPageAndHWMSize(path)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"ReadPageAndHWMSize failed: %w\", err)\n\t}\n\tif dataWritten%uint32(pageSize) == 0 {\n\t\tp.SetOverflow(dataWritten/uint32(pageSize) - 1)\n\t} else {\n\t\tp.SetOverflow(dataWritten / uint32(pageSize))\n\t}\n\n\tdatasz := pageSize * (uint64(p.Overflow()) + 1)\n\tif err := guts_cli.WritePage(path, buf[0:datasz]); err != nil {\n\t\treturn false, fmt.Errorf(\"WritePage failed: %w\", err)\n\t}\n\n\tif preOverflow != p.Overflow() || p.IsBranchPage() {\n\t\tif abandonFreelist {\n\t\t\treturn false, ClearFreelist(path)\n\t\t}\n\t\treturn true, nil\n\t}\n\n\treturn false, nil\n}\n\nfunc ClearFreelist(path string) error {\n\tif err := clearFreelistInMetaPage(path, 0); err != nil {\n\t\treturn fmt.Errorf(\"clearFreelist on meta page 0 failed: %w\", err)\n\t}\n\tif err := clearFreelistInMetaPage(path, 1); err != nil {\n\t\treturn fmt.Errorf(\"clearFreelist on meta page 1 failed: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc clearFreelistInMetaPage(path string, pageId uint64) error {\n\t_, buf, err := guts_cli.ReadPage(path, pageId)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"ReadPage %d failed: %w\", pageId, err)\n\t}\n\n\tmeta := common.LoadPageMeta(buf)\n\tmeta.SetFreelist(common.PgidNoFreelist)\n\tmeta.SetChecksum(meta.Sum64())\n\n\tif err := guts_cli.WritePage(path, buf); err != nil {\n\t\treturn fmt.Errorf(\"WritePage %d failed: %w\", pageId, err)\n\t}\n\n\treturn nil\n}\n\n// RevertMetaPage replaces the newer metadata page with the older.\n// It usually means that one transaction is being lost. But frequently\n// data corruption happens on the last transaction pages and the\n// previous state is consistent.\nfunc RevertMetaPage(path string) error {\n\t_, activeMetaPage, err := guts_cli.GetRootPage(path)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif activeMetaPage == 0 {\n\t\treturn CopyPage(path, 1, 0)\n\t} else {\n\t\treturn CopyPage(path, 0, 1)\n\t}\n}\n"
  },
  {
    "path": "internal/surgeon/surgeon_test.go",
    "content": "package surgeon_test\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/surgeon\"\n)\n\nfunc TestRevertMetaPage(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tassert.NoError(t,\n\t\tdb.Fill([]byte(\"data\"), 1, 500,\n\t\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t\t))\n\tassert.NoError(t,\n\t\tdb.Update(\n\t\t\tfunc(tx *bolt.Tx) error {\n\t\t\t\tb := tx.Bucket([]byte(\"data\"))\n\t\t\t\tassert.NoError(t, b.Put([]byte(\"0123\"), []byte(\"new Value for 123\")))\n\t\t\t\tassert.NoError(t, b.Put([]byte(\"1234b\"), []byte(\"additional object\")))\n\t\t\t\tassert.NoError(t, b.Delete([]byte(\"0246\")))\n\t\t\t\treturn nil\n\t\t\t}))\n\n\tassert.NoError(t,\n\t\tdb.View(\n\t\t\tfunc(tx *bolt.Tx) error {\n\t\t\t\tb := tx.Bucket([]byte(\"data\"))\n\t\t\t\tassert.Equal(t, []byte(\"new Value for 123\"), b.Get([]byte(\"0123\")))\n\t\t\t\tassert.Equal(t, []byte(\"additional object\"), b.Get([]byte(\"1234b\")))\n\t\t\t\tassert.Nil(t, b.Get([]byte(\"0246\")))\n\t\t\t\treturn nil\n\t\t\t}))\n\n\tdb.Close()\n\n\t// This causes the whole tree to be linked to the previous state\n\tassert.NoError(t, surgeon.RevertMetaPage(db.Path()))\n\n\tdb.MustReopen()\n\tdb.MustCheck()\n\tassert.NoError(t,\n\t\tdb.View(\n\t\t\tfunc(tx *bolt.Tx) error {\n\t\t\t\tb := tx.Bucket([]byte(\"data\"))\n\t\t\t\tassert.Equal(t, make([]byte, 100), b.Get([]byte(\"0123\")))\n\t\t\t\tassert.Nil(t, b.Get([]byte(\"1234b\")))\n\t\t\t\tassert.Equal(t, make([]byte, 100), b.Get([]byte(\"0246\")))\n\t\t\t\treturn nil\n\t\t\t}))\n}\n"
  },
  {
    "path": "internal/surgeon/xray.go",
    "content": "package surgeon\n\n// Library contains raw access to bbolt files for sake of testing or fixing of corrupted files.\n//\n// The library must not be used bbolt btree - just by CLI or tests.\n// It's not optimized for performance.\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\ntype XRay struct {\n\tpath string\n}\n\nfunc NewXRay(path string) XRay {\n\treturn XRay{path}\n}\n\nfunc (n XRay) traverse(stack []common.Pgid, callback func(page *common.Page, stack []common.Pgid) error) error {\n\tp, data, err := guts_cli.ReadPage(n.path, uint64(stack[len(stack)-1]))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed reading page (stack %v): %w\", stack, err)\n\t}\n\terr = callback(p, stack)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed callback for page (stack %v): %w\", stack, err)\n\t}\n\tswitch p.Typ() {\n\tcase \"meta\":\n\t\t{\n\t\t\tm := common.LoadPageMeta(data)\n\t\t\tr := m.RootBucket().RootPage()\n\t\t\treturn n.traverse(append(stack, r), callback)\n\t\t}\n\tcase \"branch\":\n\t\t{\n\t\t\tfor i := uint16(0); i < p.Count(); i++ {\n\t\t\t\tbpe := p.BranchPageElement(i)\n\t\t\t\tif err := n.traverse(append(stack, bpe.Pgid()), callback); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tcase \"leaf\":\n\t\tfor i := uint16(0); i < p.Count(); i++ {\n\t\t\tlpe := p.LeafPageElement(i)\n\t\t\tif lpe.IsBucketEntry() {\n\t\t\t\tpgid := lpe.Bucket().RootPage()\n\t\t\t\tif pgid > 0 {\n\t\t\t\t\tif err := n.traverse(append(stack, pgid), callback); err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tinlinePage := lpe.Bucket().InlinePage(lpe.Value())\n\t\t\t\t\tif err := callback(inlinePage, stack); err != nil {\n\t\t\t\t\t\treturn fmt.Errorf(\"failed callback for inline page  (stack %v): %w\", stack, err)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tcase \"freelist\":\n\t\treturn nil\n\t\t// Free does not have children.\n\t}\n\treturn nil\n}\n\n// FindPathsToKey finds all paths from root to the page that contains the given key.\n// As it traverses multiple buckets, so in theory there might be multiple keys with the given name.\n// Note: For simplicity it's currently implemented as traversing of the whole reachable tree.\n// If key is a bucket name, a page-path referencing the key will be returned as well.\nfunc (n XRay) FindPathsToKey(key []byte) ([][]common.Pgid, error) {\n\tvar found [][]common.Pgid\n\n\trootPage, _, err := guts_cli.GetRootPage(n.path)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\terr = n.traverse([]common.Pgid{rootPage},\n\t\tfunc(page *common.Page, stack []common.Pgid) error {\n\t\t\tif page.Typ() == \"leaf\" {\n\t\t\t\tfor i := uint16(0); i < page.Count(); i++ {\n\t\t\t\t\tif bytes.Equal(page.LeafPageElement(i).Key(), key) {\n\t\t\t\t\t\tvar copyPath []common.Pgid\n\t\t\t\t\t\tcopyPath = append(copyPath, stack...)\n\t\t\t\t\t\tfound = append(found, copyPath)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t})\n\tif err != nil {\n\t\treturn nil, err\n\t} else {\n\t\treturn found, nil\n\t}\n}\n"
  },
  {
    "path": "internal/surgeon/xray_test.go",
    "content": "package surgeon_test\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n\t\"go.etcd.io/bbolt/internal/surgeon\"\n)\n\nfunc TestFindPathsToKey(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tassert.NoError(t,\n\t\tdb.Fill([]byte(\"data\"), 1, 500,\n\t\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t\t))\n\tassert.NoError(t, db.Close())\n\n\tnavigator := surgeon.NewXRay(db.Path())\n\tpath1, err := navigator.FindPathsToKey([]byte(\"0451\"))\n\tassert.NoError(t, err)\n\tassert.NotEmpty(t, path1)\n\n\tpage := path1[0][len(path1[0])-1]\n\tp, _, err := guts_cli.ReadPage(db.Path(), uint64(page))\n\tassert.NoError(t, err)\n\tassert.GreaterOrEqual(t, []byte(\"0451\"), p.LeafPageElement(0).Key())\n\tassert.LessOrEqual(t, []byte(\"0451\"), p.LeafPageElement(p.Count()-1).Key())\n}\n\nfunc TestFindPathsToKey_Bucket(t *testing.T) {\n\trootBucket := []byte(\"data\")\n\tsubBucket := []byte(\"0451A\")\n\n\tdb := btesting.MustCreateDB(t)\n\tassert.NoError(t,\n\t\tdb.Fill(rootBucket, 1, 500,\n\t\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t\t))\n\trequire.NoError(t, db.Update(func(tx *bbolt.Tx) error {\n\t\tsb, err := tx.Bucket(rootBucket).CreateBucket(subBucket)\n\t\trequire.NoError(t, err)\n\t\trequire.NoError(t, sb.Put([]byte(\"foo\"), []byte(\"bar\")))\n\t\treturn nil\n\t}))\n\n\tassert.NoError(t, db.Close())\n\n\tnavigator := surgeon.NewXRay(db.Path())\n\tpath1, err := navigator.FindPathsToKey(subBucket)\n\tassert.NoError(t, err)\n\tassert.NotEmpty(t, path1)\n\n\tpage := path1[0][len(path1[0])-1]\n\tp, _, err := guts_cli.ReadPage(db.Path(), uint64(page))\n\tassert.NoError(t, err)\n\tassert.GreaterOrEqual(t, subBucket, p.LeafPageElement(0).Key())\n\tassert.LessOrEqual(t, subBucket, p.LeafPageElement(p.Count()-1).Key())\n}\n"
  },
  {
    "path": "internal/tests/tx_check_test.go",
    "content": "package tests_test\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n\t\"go.etcd.io/bbolt/internal/surgeon\"\n)\n\nfunc TestTx_RecursivelyCheckPages_MisplacedPage(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tdb.ForceDisableStrictMode()\n\trequire.NoError(t,\n\t\tdb.Fill([]byte(\"data\"), 1, 10000,\n\t\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t\t))\n\trequire.NoError(t, db.Close())\n\n\txRay := surgeon.NewXRay(db.Path())\n\n\tpath1, err := xRay.FindPathsToKey([]byte(\"0451\"))\n\trequire.NoError(t, err, \"cannot find page that contains key:'0451'\")\n\trequire.Len(t, path1, 1, \"Expected only one page that contains key:'0451'\")\n\n\tpath2, err := xRay.FindPathsToKey([]byte(\"7563\"))\n\trequire.NoError(t, err, \"cannot find page that contains key:'7563'\")\n\trequire.Len(t, path2, 1, \"Expected only one page that contains key:'7563'\")\n\n\tsrcPage := path1[0][len(path1[0])-1]\n\ttargetPage := path2[0][len(path2[0])-1]\n\trequire.NoError(t, surgeon.CopyPage(db.Path(), srcPage, targetPage))\n\n\tdb.MustReopen()\n\tdb.ForceDisableStrictMode()\n\trequire.NoError(t, db.Update(func(tx *bolt.Tx) error {\n\t\t// Collect all the errors.\n\t\tvar errors []error\n\t\tfor err := range tx.Check() {\n\t\t\terrors = append(errors, err)\n\t\t}\n\t\trequire.Len(t, errors, 1)\n\t\trequire.ErrorContains(t, errors[0], fmt.Sprintf(\"leaf page(%v) needs to be >= the key in the ancestor\", targetPage))\n\t\treturn nil\n\t}))\n\trequire.NoError(t, db.Close())\n}\n\nfunc TestTx_RecursivelyCheckPages_CorruptedLeaf(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tdb.ForceDisableStrictMode()\n\trequire.NoError(t,\n\t\tdb.Fill([]byte(\"data\"), 1, 10000,\n\t\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t\t))\n\trequire.NoError(t, db.Close())\n\n\txray := surgeon.NewXRay(db.Path())\n\n\tpath1, err := xray.FindPathsToKey([]byte(\"0451\"))\n\trequire.NoError(t, err, \"cannot find page that contains key:'0451'\")\n\trequire.Len(t, path1, 1, \"Expected only one page that contains key:'0451'\")\n\n\tsrcPage := path1[0][len(path1[0])-1]\n\tp, pbuf, err := guts_cli.ReadPage(db.Path(), uint64(srcPage))\n\trequire.NoError(t, err)\n\trequire.Positive(t, p.Count(), \"page must be not empty\")\n\tp.LeafPageElement(p.Count() / 2).Key()[0] = 'z'\n\trequire.NoError(t, guts_cli.WritePage(db.Path(), pbuf))\n\n\tdb.MustReopen()\n\tdb.ForceDisableStrictMode()\n\trequire.NoError(t, db.Update(func(tx *bolt.Tx) error {\n\t\t// Collect all the errors.\n\t\tvar errors []error\n\t\tfor err := range tx.Check() {\n\t\t\terrors = append(errors, err)\n\t\t}\n\t\trequire.Len(t, errors, 2)\n\t\trequire.ErrorContains(t, errors[0], fmt.Sprintf(\"leaf page(%v) needs to be < than key of the next element in ancestor\", srcPage))\n\t\trequire.ErrorContains(t, errors[1], fmt.Sprintf(\"leaf page(%v) needs to be > (found <) than previous element\", srcPage))\n\t\treturn nil\n\t}))\n\trequire.NoError(t, db.Close())\n}\n"
  },
  {
    "path": "logger.go",
    "content": "package bbolt\n\n// See https://github.com/etcd-io/raft/blob/main/logger.go\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"log\"\n\t\"os\"\n)\n\ntype Logger interface {\n\tDebug(v ...interface{})\n\tDebugf(format string, v ...interface{})\n\n\tError(v ...interface{})\n\tErrorf(format string, v ...interface{})\n\n\tInfo(v ...interface{})\n\tInfof(format string, v ...interface{})\n\n\tWarning(v ...interface{})\n\tWarningf(format string, v ...interface{})\n\n\tFatal(v ...interface{})\n\tFatalf(format string, v ...interface{})\n\n\tPanic(v ...interface{})\n\tPanicf(format string, v ...interface{})\n}\n\nfunc getDiscardLogger() Logger {\n\treturn discardLogger\n}\n\nvar (\n\tdiscardLogger = &DefaultLogger{Logger: log.New(io.Discard, \"\", 0)}\n)\n\nconst (\n\tcalldepth = 2\n)\n\n// DefaultLogger is a default implementation of the Logger interface.\ntype DefaultLogger struct {\n\t*log.Logger\n\tdebug bool\n}\n\nfunc (l *DefaultLogger) EnableTimestamps() {\n\tl.SetFlags(l.Flags() | log.Ldate | log.Ltime)\n}\n\nfunc (l *DefaultLogger) EnableDebug() {\n\tl.debug = true\n}\n\nfunc (l *DefaultLogger) Debug(v ...interface{}) {\n\tif l.debug {\n\t\t_ = l.Output(calldepth, header(\"DEBUG\", fmt.Sprint(v...)))\n\t}\n}\n\nfunc (l *DefaultLogger) Debugf(format string, v ...interface{}) {\n\tif l.debug {\n\t\t_ = l.Output(calldepth, header(\"DEBUG\", fmt.Sprintf(format, v...)))\n\t}\n}\n\nfunc (l *DefaultLogger) Info(v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"INFO\", fmt.Sprint(v...)))\n}\n\nfunc (l *DefaultLogger) Infof(format string, v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"INFO\", fmt.Sprintf(format, v...)))\n}\n\nfunc (l *DefaultLogger) Error(v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"ERROR\", fmt.Sprint(v...)))\n}\n\nfunc (l *DefaultLogger) Errorf(format string, v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"ERROR\", fmt.Sprintf(format, v...)))\n}\n\nfunc (l *DefaultLogger) Warning(v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"WARN\", fmt.Sprint(v...)))\n}\n\nfunc (l *DefaultLogger) Warningf(format string, v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"WARN\", fmt.Sprintf(format, v...)))\n}\n\nfunc (l *DefaultLogger) Fatal(v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"FATAL\", fmt.Sprint(v...)))\n\tos.Exit(1)\n}\n\nfunc (l *DefaultLogger) Fatalf(format string, v ...interface{}) {\n\t_ = l.Output(calldepth, header(\"FATAL\", fmt.Sprintf(format, v...)))\n\tos.Exit(1)\n}\n\nfunc (l *DefaultLogger) Panic(v ...interface{}) {\n\tl.Logger.Panic(v...)\n}\n\nfunc (l *DefaultLogger) Panicf(format string, v ...interface{}) {\n\tl.Logger.Panicf(format, v...)\n}\n\nfunc header(lvl, msg string) string {\n\treturn fmt.Sprintf(\"%s: %s\", lvl, msg)\n}\n"
  },
  {
    "path": "manydbs_test.go",
    "content": "package bbolt\n\nimport (\n\t\"crypto/rand\"\n\t\"fmt\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"testing\"\n)\n\nfunc createDb(t *testing.T) (*DB, func()) {\n\t// First, create a temporary directory to be used for the duration of\n\t// this test.\n\ttempDirName, err := os.MkdirTemp(\"\", \"bboltmemtest\")\n\tif err != nil {\n\t\tt.Fatalf(\"error creating temp dir: %v\", err)\n\t}\n\tpath := filepath.Join(tempDirName, \"testdb.db\")\n\n\tbdb, err := Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatalf(\"error creating bbolt db: %v\", err)\n\t}\n\n\tcleanup := func() {\n\t\tbdb.Close()\n\t\tos.RemoveAll(tempDirName)\n\t}\n\n\treturn bdb, cleanup\n}\n\nfunc createAndPutKeys(t *testing.T) {\n\tt.Parallel()\n\n\tdb, cleanup := createDb(t)\n\tdefer cleanup()\n\n\tbucketName := []byte(\"bucket\")\n\n\tfor i := 0; i < 100; i++ {\n\t\terr := db.Update(func(tx *Tx) error {\n\t\t\tnodes, err := tx.CreateBucketIfNotExists(bucketName)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tvar key [16]byte\n\t\t\t_, rerr := rand.Read(key[:])\n\t\t\tif rerr != nil {\n\t\t\t\treturn rerr\n\t\t\t}\n\t\t\tif err := nodes.Put(key[:], nil); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\treturn nil\n\t\t})\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n}\n\nfunc TestManyDBs(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode\")\n\t}\n\n\tfor i := 0; i < 100; i++ {\n\t\tt.Run(fmt.Sprintf(\"%d\", i), createAndPutKeys)\n\t}\n}\n"
  },
  {
    "path": "mlock_unix.go",
    "content": "//go:build !windows\n\npackage bbolt\n\nimport \"golang.org/x/sys/unix\"\n\n// mlock locks memory of db file\nfunc mlock(db *DB, fileSize int) error {\n\tsizeToLock := fileSize\n\tif sizeToLock > db.datasz {\n\t\t// Can't lock more than mmaped slice\n\t\tsizeToLock = db.datasz\n\t}\n\tif err := unix.Mlock(db.dataref[:sizeToLock]); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n// munlock unlocks memory of db file\nfunc munlock(db *DB, fileSize int) error {\n\tif db.dataref == nil {\n\t\treturn nil\n\t}\n\n\tsizeToUnlock := fileSize\n\tif sizeToUnlock > db.datasz {\n\t\t// Can't unlock more than mmaped slice\n\t\tsizeToUnlock = db.datasz\n\t}\n\n\tif err := unix.Munlock(db.dataref[:sizeToUnlock]); err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n"
  },
  {
    "path": "mlock_windows.go",
    "content": "package bbolt\n\n// mlock locks memory of db file\nfunc mlock(_ *DB, _ int) error {\n\tpanic(\"mlock is supported only on UNIX systems\")\n}\n\n// munlock unlocks memory of db file\nfunc munlock(_ *DB, _ int) error {\n\tpanic(\"munlock is supported only on UNIX systems\")\n}\n"
  },
  {
    "path": "movebucket_test.go",
    "content": "package bbolt_test\n\nimport (\n\tcrand \"crypto/rand\"\n\t\"math/rand\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\nfunc TestTx_MoveBucket(t *testing.T) {\n\ttestCases := []struct {\n\t\tname                    string\n\t\tsrcBucketPath           []string\n\t\tdstBucketPath           []string\n\t\tbucketToMove            string\n\t\tbucketExistInSrc        bool\n\t\tbucketExistInDst        bool\n\t\thasIncompatibleKeyInSrc bool\n\t\thasIncompatibleKeyInDst bool\n\t\texpectedErr             error\n\t}{\n\t\t// normal cases\n\t\t{\n\t\t\tname:                    \"normal case\",\n\t\t\tsrcBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{\"db1\", \"db2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             nil,\n\t\t},\n\t\t{\n\t\t\tname:                    \"the source and target bucket share the same grandparent\",\n\t\t\tsrcBucketPath:           []string{\"grandparent\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{\"grandparent\", \"db2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             nil,\n\t\t},\n\t\t{\n\t\t\tname:                    \"bucketToMove is a top level bucket\",\n\t\t\tsrcBucketPath:           []string{},\n\t\t\tdstBucketPath:           []string{\"db1\", \"db2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             nil,\n\t\t},\n\t\t{\n\t\t\tname:                    \"convert bucketToMove to a top level bucket\",\n\t\t\tsrcBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             nil,\n\t\t},\n\t\t// negative cases\n\t\t{\n\t\t\tname:                    \"bucketToMove not exist in source bucket\",\n\t\t\tsrcBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{\"db1\", \"db2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        false,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             errors.ErrBucketNotFound,\n\t\t},\n\t\t{\n\t\t\tname:                    \"bucketToMove exist in target bucket\",\n\t\t\tsrcBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{\"db1\", \"db2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        true,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             errors.ErrBucketExists,\n\t\t},\n\t\t{\n\t\t\tname:                    \"incompatible key exist in source bucket\",\n\t\t\tsrcBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{\"db1\", \"db2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        false,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: true,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             errors.ErrIncompatibleValue,\n\t\t},\n\t\t{\n\t\t\tname:                    \"incompatible key exist in target bucket\",\n\t\t\tsrcBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{\"db1\", \"db2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: true,\n\t\t\texpectedErr:             errors.ErrIncompatibleValue,\n\t\t},\n\t\t{\n\t\t\tname:                    \"the source and target are the same bucket\",\n\t\t\tsrcBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:           []string{\"sb1\", \"sb2\"},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             errors.ErrSameBuckets,\n\t\t},\n\t\t{\n\t\t\tname:                    \"both the source and target are the root bucket\",\n\t\t\tsrcBucketPath:           []string{},\n\t\t\tdstBucketPath:           []string{},\n\t\t\tbucketToMove:            \"bucketToMove\",\n\t\t\tbucketExistInSrc:        true,\n\t\t\tbucketExistInDst:        false,\n\t\t\thasIncompatibleKeyInSrc: false,\n\t\t\thasIncompatibleKeyInDst: false,\n\t\t\texpectedErr:             errors.ErrSameBuckets,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\n\t\tt.Run(tc.name, func(*testing.T) {\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})\n\n\t\t\tdumpBucketBeforeMoving := filepath.Join(t.TempDir(), \"dbBeforeMove\")\n\t\t\tdumpBucketAfterMoving := filepath.Join(t.TempDir(), \"dbAfterMove\")\n\n\t\t\tt.Log(\"Creating sample db and populate some data\")\n\t\t\terr := db.Update(func(tx *bbolt.Tx) error {\n\t\t\t\tsrcBucket := prepareBuckets(t, tx, tc.srcBucketPath...)\n\t\t\t\tdstBucket := prepareBuckets(t, tx, tc.dstBucketPath...)\n\n\t\t\t\tif tc.bucketExistInSrc {\n\t\t\t\t\t_ = createBucketAndPopulateData(t, tx, srcBucket, tc.bucketToMove)\n\t\t\t\t}\n\n\t\t\t\tif tc.bucketExistInDst {\n\t\t\t\t\t_ = createBucketAndPopulateData(t, tx, dstBucket, tc.bucketToMove)\n\t\t\t\t}\n\n\t\t\t\tif tc.hasIncompatibleKeyInSrc {\n\t\t\t\t\tputErr := srcBucket.Put([]byte(tc.bucketToMove), []byte(\"bar\"))\n\t\t\t\t\trequire.NoError(t, putErr)\n\t\t\t\t}\n\n\t\t\t\tif tc.hasIncompatibleKeyInDst {\n\t\t\t\t\tputErr := dstBucket.Put([]byte(tc.bucketToMove), []byte(\"bar\"))\n\t\t\t\t\trequire.NoError(t, putErr)\n\t\t\t\t}\n\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\n\t\t\tt.Log(\"Moving bucket\")\n\t\t\terr = db.Update(func(tx *bbolt.Tx) error {\n\t\t\t\tsrcBucket := prepareBuckets(t, tx, tc.srcBucketPath...)\n\t\t\t\tdstBucket := prepareBuckets(t, tx, tc.dstBucketPath...)\n\n\t\t\t\tif tc.expectedErr == nil {\n\t\t\t\t\tt.Logf(\"Dump the bucket to %s before moving it\", dumpBucketBeforeMoving)\n\t\t\t\t\tbk := openBucket(tx, srcBucket, tc.bucketToMove)\n\t\t\t\t\tdumpErr := dumpBucket([]byte(tc.bucketToMove), bk, dumpBucketBeforeMoving)\n\t\t\t\t\trequire.NoError(t, dumpErr)\n\t\t\t\t}\n\n\t\t\t\tmErr := tx.MoveBucket([]byte(tc.bucketToMove), srcBucket, dstBucket)\n\t\t\t\trequire.Equal(t, tc.expectedErr, mErr)\n\n\t\t\t\tif tc.expectedErr == nil {\n\t\t\t\t\tt.Logf(\"Dump the bucket to %s after moving it\", dumpBucketAfterMoving)\n\t\t\t\t\tbk := openBucket(tx, dstBucket, tc.bucketToMove)\n\t\t\t\t\tdumpErr := dumpBucket([]byte(tc.bucketToMove), bk, dumpBucketAfterMoving)\n\t\t\t\t\trequire.NoError(t, dumpErr)\n\t\t\t\t}\n\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\n\t\t\t// skip assertion if failure expected\n\t\t\tif tc.expectedErr != nil {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tt.Log(\"Verifying the bucket should be identical before and after being moved\")\n\t\t\tdataBeforeMove, err := os.ReadFile(dumpBucketBeforeMoving)\n\t\t\trequire.NoError(t, err)\n\t\t\tdataAfterMove, err := os.ReadFile(dumpBucketAfterMoving)\n\t\t\trequire.NoError(t, err)\n\t\t\trequire.Equal(t, dataBeforeMove, dataAfterMove)\n\t\t})\n\t}\n}\n\nfunc TestBucket_MoveBucket_DiffDB(t *testing.T) {\n\tsrcBucketPath := []string{\"sb1\", \"sb2\"}\n\tdstBucketPath := []string{\"db1\", \"db2\"}\n\tbucketToMove := \"bucketToMove\"\n\n\tvar srcBucket *bbolt.Bucket\n\n\tt.Log(\"Creating source bucket and populate some data\")\n\tsrcDB := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})\n\terr := srcDB.Update(func(tx *bbolt.Tx) error {\n\t\tsrcBucket = prepareBuckets(t, tx, srcBucketPath...)\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\tdefer func() {\n\t\trequire.NoError(t, srcDB.Close())\n\t}()\n\n\tt.Log(\"Creating target bucket and populate some data\")\n\tdstDB := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})\n\terr = dstDB.Update(func(tx *bbolt.Tx) error {\n\t\tprepareBuckets(t, tx, dstBucketPath...)\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\tdefer func() {\n\t\trequire.NoError(t, dstDB.Close())\n\t}()\n\n\tt.Log(\"Reading source bucket in a separate RWTx\")\n\tsTx, sErr := srcDB.Begin(true)\n\trequire.NoError(t, sErr)\n\tdefer func() {\n\t\trequire.NoError(t, sTx.Rollback())\n\t}()\n\tsrcBucket = prepareBuckets(t, sTx, srcBucketPath...)\n\n\tt.Log(\"Moving the sub-bucket in a separate RWTx\")\n\terr = dstDB.Update(func(tx *bbolt.Tx) error {\n\t\tdstBucket := prepareBuckets(t, tx, dstBucketPath...)\n\t\tmErr := srcBucket.MoveBucket([]byte(bucketToMove), dstBucket)\n\t\trequire.Equal(t, errors.ErrDifferentDB, mErr)\n\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n}\n\nfunc TestBucket_MoveBucket_DiffTx(t *testing.T) {\n\ttestCases := []struct {\n\t\tname            string\n\t\tsrcBucketPath   []string\n\t\tdstBucketPath   []string\n\t\tisSrcReadonlyTx bool\n\t\tisDstReadonlyTx bool\n\t\tbucketToMove    string\n\t\texpectedErr     error\n\t}{\n\t\t{\n\t\t\tname:            \"src is RWTx and target is RTx\",\n\t\t\tsrcBucketPath:   []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:   []string{\"db1\", \"db2\"},\n\t\t\tisSrcReadonlyTx: true,\n\t\t\tisDstReadonlyTx: false,\n\t\t\tbucketToMove:    \"bucketToMove\",\n\t\t\texpectedErr:     errors.ErrTxNotWritable,\n\t\t},\n\t\t{\n\t\t\tname:            \"src is RTx and target is RWTx\",\n\t\t\tsrcBucketPath:   []string{\"sb1\", \"sb2\"},\n\t\t\tdstBucketPath:   []string{\"db1\", \"db2\"},\n\t\t\tisSrcReadonlyTx: false,\n\t\t\tisDstReadonlyTx: true,\n\t\t\tbucketToMove:    \"bucketToMove\",\n\t\t\texpectedErr:     errors.ErrTxNotWritable,\n\t\t},\n\t}\n\n\tfor _, tc := range testCases {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tvar srcBucket *bbolt.Bucket\n\t\t\tvar dstBucket *bbolt.Bucket\n\n\t\t\tt.Log(\"Creating source and target buckets and populate some data\")\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})\n\t\t\terr := db.Update(func(tx *bbolt.Tx) error {\n\t\t\t\tsrcBucket = prepareBuckets(t, tx, tc.srcBucketPath...)\n\t\t\t\tdstBucket = prepareBuckets(t, tx, tc.dstBucketPath...)\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\t\t\tdefer func() {\n\t\t\t\trequire.NoError(t, db.Close())\n\t\t\t}()\n\n\t\t\tt.Log(\"Opening source bucket in a separate Tx\")\n\t\t\tsTx, sErr := db.Begin(tc.isSrcReadonlyTx)\n\t\t\trequire.NoError(t, sErr)\n\t\t\tdefer func() {\n\t\t\t\trequire.NoError(t, sTx.Rollback())\n\t\t\t}()\n\t\t\tsrcBucket = prepareBuckets(t, sTx, tc.srcBucketPath...)\n\n\t\t\tt.Log(\"Opening target bucket in a separate Tx\")\n\t\t\tdTx, dErr := db.Begin(tc.isDstReadonlyTx)\n\t\t\trequire.NoError(t, dErr)\n\t\t\tdefer func() {\n\t\t\t\trequire.NoError(t, dTx.Rollback())\n\t\t\t}()\n\t\t\tdstBucket = prepareBuckets(t, dTx, tc.dstBucketPath...)\n\n\t\t\tt.Log(\"Moving the sub-bucket\")\n\t\t\terr = db.View(func(tx *bbolt.Tx) error {\n\t\t\t\tmErr := srcBucket.MoveBucket([]byte(tc.bucketToMove), dstBucket)\n\t\t\t\trequire.Equal(t, tc.expectedErr, mErr)\n\n\t\t\t\treturn nil\n\t\t\t})\n\t\t\trequire.NoError(t, err)\n\t\t})\n\t}\n}\n\n// prepareBuckets opens the bucket chain. For each bucket in the chain,\n// open it if existed, otherwise create it and populate sample data.\nfunc prepareBuckets(t testing.TB, tx *bbolt.Tx, buckets ...string) *bbolt.Bucket {\n\tvar bk *bbolt.Bucket\n\n\tfor _, key := range buckets {\n\t\tif childBucket := openBucket(tx, bk, key); childBucket == nil {\n\t\t\tbk = createBucketAndPopulateData(t, tx, bk, key)\n\t\t} else {\n\t\t\tbk = childBucket\n\t\t}\n\t}\n\treturn bk\n}\n\nfunc openBucket(tx *bbolt.Tx, bk *bbolt.Bucket, bucketToOpen string) *bbolt.Bucket {\n\tif bk == nil {\n\t\treturn tx.Bucket([]byte(bucketToOpen))\n\t}\n\treturn bk.Bucket([]byte(bucketToOpen))\n}\n\nfunc createBucketAndPopulateData(t testing.TB, tx *bbolt.Tx, bk *bbolt.Bucket, bucketName string) *bbolt.Bucket {\n\tif bk == nil {\n\t\tnewBucket, err := tx.CreateBucket([]byte(bucketName))\n\t\trequire.NoError(t, err, \"failed to create bucket %s\", bucketName)\n\t\tpopulateSampleDataInBucket(t, newBucket, rand.Intn(4096))\n\t\treturn newBucket\n\t}\n\n\tnewBucket, err := bk.CreateBucket([]byte(bucketName))\n\trequire.NoError(t, err, \"failed to create bucket %s\", bucketName)\n\tpopulateSampleDataInBucket(t, newBucket, rand.Intn(4096))\n\treturn newBucket\n}\n\nfunc populateSampleDataInBucket(t testing.TB, bk *bbolt.Bucket, n int) {\n\tvar min, max = 1, 1024\n\n\tfor i := 0; i < n; i++ {\n\t\t// generate rand key/value length\n\t\tkeyLength := rand.Intn(max-min) + min\n\t\tvalLength := rand.Intn(max-min) + min\n\n\t\tkeyData := make([]byte, keyLength)\n\t\tvalData := make([]byte, valLength)\n\n\t\t_, err := crand.Read(keyData)\n\t\trequire.NoError(t, err)\n\n\t\t_, err = crand.Read(valData)\n\t\trequire.NoError(t, err)\n\n\t\terr = bk.Put(keyData, valData)\n\t\trequire.NoError(t, err)\n\t}\n}\n"
  },
  {
    "path": "node.go",
    "content": "package bbolt\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"sort\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// node represents an in-memory, deserialized page.\ntype node struct {\n\tbucket     *Bucket\n\tisLeaf     bool\n\tunbalanced bool\n\tspilled    bool\n\tkey        []byte\n\tpgid       common.Pgid\n\tparent     *node\n\tchildren   nodes\n\tinodes     common.Inodes\n}\n\n// root returns the top-level node this node is attached to.\nfunc (n *node) root() *node {\n\tif n.parent == nil {\n\t\treturn n\n\t}\n\treturn n.parent.root()\n}\n\n// minKeys returns the minimum number of inodes this node should have.\nfunc (n *node) minKeys() int {\n\tif n.isLeaf {\n\t\treturn 1\n\t}\n\treturn 2\n}\n\n// size returns the size of the node after serialization.\nfunc (n *node) size() int {\n\tsz, elsz := common.PageHeaderSize, n.pageElementSize()\n\tfor i := 0; i < len(n.inodes); i++ {\n\t\titem := &n.inodes[i]\n\t\tsz += elsz + uintptr(len(item.Key())) + uintptr(len(item.Value()))\n\t}\n\treturn int(sz)\n}\n\n// sizeLessThan returns true if the node is less than a given size.\n// This is an optimization to avoid calculating a large node when we only need\n// to know if it fits inside a certain page size.\nfunc (n *node) sizeLessThan(v uintptr) bool {\n\tsz, elsz := common.PageHeaderSize, n.pageElementSize()\n\tfor i := 0; i < len(n.inodes); i++ {\n\t\titem := &n.inodes[i]\n\t\tsz += elsz + uintptr(len(item.Key())) + uintptr(len(item.Value()))\n\t\tif sz >= v {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// pageElementSize returns the size of each page element based on the type of node.\nfunc (n *node) pageElementSize() uintptr {\n\tif n.isLeaf {\n\t\treturn common.LeafPageElementSize\n\t}\n\treturn common.BranchPageElementSize\n}\n\n// childAt returns the child node at a given index.\nfunc (n *node) childAt(index int) *node {\n\tif n.isLeaf {\n\t\tpanic(fmt.Sprintf(\"invalid childAt(%d) on a leaf node\", index))\n\t}\n\treturn n.bucket.node(n.inodes[index].Pgid(), n)\n}\n\n// childIndex returns the index of a given child node.\nfunc (n *node) childIndex(child *node) int {\n\tindex := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].Key(), child.key) != -1 })\n\treturn index\n}\n\n// numChildren returns the number of children.\nfunc (n *node) numChildren() int {\n\treturn len(n.inodes)\n}\n\n// nextSibling returns the next node with the same parent.\nfunc (n *node) nextSibling() *node {\n\tif n.parent == nil {\n\t\treturn nil\n\t}\n\tindex := n.parent.childIndex(n)\n\tif index >= n.parent.numChildren()-1 {\n\t\treturn nil\n\t}\n\treturn n.parent.childAt(index + 1)\n}\n\n// prevSibling returns the previous node with the same parent.\nfunc (n *node) prevSibling() *node {\n\tif n.parent == nil {\n\t\treturn nil\n\t}\n\tindex := n.parent.childIndex(n)\n\tif index == 0 {\n\t\treturn nil\n\t}\n\treturn n.parent.childAt(index - 1)\n}\n\n// put inserts a key/value.\nfunc (n *node) put(oldKey, newKey, value []byte, pgId common.Pgid, flags uint32) {\n\tif pgId >= n.bucket.tx.meta.Pgid() {\n\t\tpanic(fmt.Sprintf(\"pgId (%d) above high water mark (%d)\", pgId, n.bucket.tx.meta.Pgid()))\n\t} else if len(oldKey) <= 0 {\n\t\tpanic(\"put: zero-length old key\")\n\t} else if len(newKey) <= 0 {\n\t\tpanic(\"put: zero-length new key\")\n\t}\n\n\t// Find insertion index.\n\tindex := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].Key(), oldKey) != -1 })\n\n\t// Add capacity and shift nodes if we don't have an exact match and need to insert.\n\texact := len(n.inodes) > 0 && index < len(n.inodes) && bytes.Equal(n.inodes[index].Key(), oldKey)\n\tif !exact {\n\t\tn.inodes = append(n.inodes, common.Inode{})\n\t\tcopy(n.inodes[index+1:], n.inodes[index:])\n\t}\n\n\tinode := &n.inodes[index]\n\tinode.SetFlags(flags)\n\tinode.SetKey(newKey)\n\tinode.SetValue(value)\n\tinode.SetPgid(pgId)\n\tcommon.Assert(len(inode.Key()) > 0, \"put: zero-length inode key\")\n}\n\n// del removes a key from the node.\nfunc (n *node) del(key []byte) {\n\t// Find index of key.\n\tindex := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].Key(), key) != -1 })\n\n\t// Exit if the key isn't found.\n\tif index >= len(n.inodes) || !bytes.Equal(n.inodes[index].Key(), key) {\n\t\treturn\n\t}\n\n\t// Delete inode from the node.\n\tn.inodes = append(n.inodes[:index], n.inodes[index+1:]...)\n\n\t// Mark the node as needing rebalancing.\n\tn.unbalanced = true\n}\n\n// read initializes the node from a page.\nfunc (n *node) read(p *common.Page) {\n\tn.pgid = p.Id()\n\tn.isLeaf = p.IsLeafPage()\n\tn.inodes = common.ReadInodeFromPage(p)\n\n\t// Save first key, so we can find the node in the parent when we spill.\n\tif len(n.inodes) > 0 {\n\t\tn.key = n.inodes[0].Key()\n\t\tcommon.Assert(len(n.key) > 0, \"read: zero-length node key\")\n\t} else {\n\t\tn.key = nil\n\t}\n}\n\n// write writes the items onto one or more pages.\n// The page should have p.id (might be 0 for meta or bucket-inline page) and p.overflow set\n// and the rest should be zeroed.\nfunc (n *node) write(p *common.Page) {\n\tcommon.Assert(p.Count() == 0 && p.Flags() == 0, \"node cannot be written into a not empty page\")\n\n\t// Initialize page.\n\tif n.isLeaf {\n\t\tp.SetFlags(common.LeafPageFlag)\n\t} else {\n\t\tp.SetFlags(common.BranchPageFlag)\n\t}\n\n\tif len(n.inodes) >= 0xFFFF {\n\t\tpanic(fmt.Sprintf(\"inode overflow: %d (pgid=%d)\", len(n.inodes), p.Id()))\n\t}\n\tp.SetCount(uint16(len(n.inodes)))\n\n\t// Stop here if there are no items to write.\n\tif p.Count() == 0 {\n\t\treturn\n\t}\n\n\tcommon.WriteInodeToPage(n.inodes, p)\n\n\t// DEBUG ONLY: n.dump()\n}\n\n// split breaks up a node into multiple smaller nodes, if appropriate.\n// This should only be called from the spill() function.\nfunc (n *node) split(pageSize uintptr) []*node {\n\tvar nodes []*node\n\n\tnode := n\n\tfor {\n\t\t// Split node into two.\n\t\ta, b := node.splitTwo(pageSize)\n\t\tnodes = append(nodes, a)\n\n\t\t// If we can't split then exit the loop.\n\t\tif b == nil {\n\t\t\tbreak\n\t\t}\n\n\t\t// Set node to b so it gets split on the next iteration.\n\t\tnode = b\n\t}\n\n\treturn nodes\n}\n\n// splitTwo breaks up a node into two smaller nodes, if appropriate.\n// This should only be called from the split() function.\nfunc (n *node) splitTwo(pageSize uintptr) (*node, *node) {\n\t// Ignore the split if the page doesn't have at least enough nodes for\n\t// two pages or if the nodes can fit in a single page.\n\tif len(n.inodes) <= (common.MinKeysPerPage*2) || n.sizeLessThan(pageSize) {\n\t\treturn n, nil\n\t}\n\n\t// Determine the threshold before starting a new node.\n\tvar fillPercent = n.bucket.FillPercent\n\tif fillPercent < minFillPercent {\n\t\tfillPercent = minFillPercent\n\t} else if fillPercent > maxFillPercent {\n\t\tfillPercent = maxFillPercent\n\t}\n\tthreshold := int(float64(pageSize) * fillPercent)\n\n\t// Determine split position and sizes of the two pages.\n\tsplitIndex, _ := n.splitIndex(threshold)\n\n\t// Split node into two separate nodes.\n\t// If there's no parent then we'll need to create one.\n\tif n.parent == nil {\n\t\tn.parent = &node{bucket: n.bucket, children: []*node{n}}\n\t}\n\n\t// Create a new node and add it to the parent.\n\tnext := &node{bucket: n.bucket, isLeaf: n.isLeaf, parent: n.parent}\n\tn.parent.children = append(n.parent.children, next)\n\n\t// Split inodes across two nodes.\n\tnext.inodes = n.inodes[splitIndex:]\n\tn.inodes = n.inodes[:splitIndex]\n\n\t// Update the statistics.\n\tn.bucket.tx.stats.IncSplit(1)\n\n\treturn n, next\n}\n\n// splitIndex finds the position where a page will fill a given threshold.\n// It returns the index as well as the size of the first page.\n// This is only be called from split().\nfunc (n *node) splitIndex(threshold int) (index, sz uintptr) {\n\tsz = common.PageHeaderSize\n\n\t// Loop until we only have the minimum number of keys required for the second page.\n\tfor i := 0; i < len(n.inodes)-common.MinKeysPerPage; i++ {\n\t\tindex = uintptr(i)\n\t\tinode := n.inodes[i]\n\t\telsize := n.pageElementSize() + uintptr(len(inode.Key())) + uintptr(len(inode.Value()))\n\n\t\t// If we have at least the minimum number of keys and adding another\n\t\t// node would put us over the threshold then exit and return.\n\t\tif index >= common.MinKeysPerPage && sz+elsize > uintptr(threshold) {\n\t\t\tbreak\n\t\t}\n\n\t\t// Add the element size to the total size.\n\t\tsz += elsize\n\t}\n\n\treturn\n}\n\n// spill writes the nodes to dirty pages and splits nodes as it goes.\n// Returns an error if dirty pages cannot be allocated.\nfunc (n *node) spill() error {\n\tvar tx = n.bucket.tx\n\tif n.spilled {\n\t\treturn nil\n\t}\n\n\t// Spill child nodes first. Child nodes can materialize sibling nodes in\n\t// the case of split-merge so we cannot use a range loop. We have to check\n\t// the children size on every loop iteration.\n\tsort.Sort(n.children)\n\tfor i := 0; i < len(n.children); i++ {\n\t\tif err := n.children[i].spill(); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// We no longer need the child list because it's only used for spill tracking.\n\tn.children = nil\n\n\t// Split nodes into appropriate sizes. The first node will always be n.\n\tvar nodes = n.split(uintptr(tx.db.pageSize))\n\tfor _, node := range nodes {\n\t\t// Add node's page to the freelist if it's not new.\n\t\tif node.pgid > 0 {\n\t\t\ttx.db.freelist.Free(tx.meta.Txid(), tx.page(node.pgid))\n\t\t\tnode.pgid = 0\n\t\t}\n\n\t\t// Allocate contiguous space for the node.\n\t\tp, err := tx.allocate((node.size() + tx.db.pageSize - 1) / tx.db.pageSize)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Write the node.\n\t\tif p.Id() >= tx.meta.Pgid() {\n\t\t\tpanic(fmt.Sprintf(\"pgid (%d) above high water mark (%d)\", p.Id(), tx.meta.Pgid()))\n\t\t}\n\t\tnode.pgid = p.Id()\n\t\tnode.write(p)\n\t\tnode.spilled = true\n\n\t\t// Insert into parent inodes.\n\t\tif node.parent != nil {\n\t\t\tvar key = node.key\n\t\t\tif key == nil {\n\t\t\t\tkey = node.inodes[0].Key()\n\t\t\t}\n\n\t\t\tnode.parent.put(key, node.inodes[0].Key(), nil, node.pgid, 0)\n\t\t\tnode.key = node.inodes[0].Key()\n\t\t\tcommon.Assert(len(node.key) > 0, \"spill: zero-length node key\")\n\t\t}\n\n\t\t// Update the statistics.\n\t\ttx.stats.IncSpill(1)\n\t}\n\n\t// If the root node split and created a new root then we need to spill that\n\t// as well. We'll clear out the children to make sure it doesn't try to respill.\n\tif n.parent != nil && n.parent.pgid == 0 {\n\t\tn.children = nil\n\t\treturn n.parent.spill()\n\t}\n\n\treturn nil\n}\n\n// rebalance attempts to combine the node with sibling nodes if the node fill\n// size is below a threshold or if there are not enough keys.\nfunc (n *node) rebalance() {\n\tif !n.unbalanced {\n\t\treturn\n\t}\n\tn.unbalanced = false\n\n\t// Update statistics.\n\tn.bucket.tx.stats.IncRebalance(1)\n\n\t// Ignore if node is above threshold (25% when FillPercent is set to DefaultFillPercent) and has enough keys.\n\tvar threshold = int(float64(n.bucket.tx.db.pageSize)*n.bucket.FillPercent) / 2\n\tif n.size() > threshold && len(n.inodes) > n.minKeys() {\n\t\treturn\n\t}\n\n\t// Root node has special handling.\n\tif n.parent == nil {\n\t\t// If root node is a branch and only has one node then collapse it.\n\t\tif !n.isLeaf && len(n.inodes) == 1 {\n\t\t\t// Move root's child up.\n\t\t\tchild := n.bucket.node(n.inodes[0].Pgid(), n)\n\t\t\tn.isLeaf = child.isLeaf\n\t\t\tn.inodes = child.inodes[:]\n\t\t\tn.children = child.children\n\n\t\t\t// Reparent all child nodes being moved.\n\t\t\tfor _, inode := range n.inodes {\n\t\t\t\tif child, ok := n.bucket.nodes[inode.Pgid()]; ok {\n\t\t\t\t\tchild.parent = n\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Remove old child.\n\t\t\tchild.parent = nil\n\t\t\tdelete(n.bucket.nodes, child.pgid)\n\t\t\tchild.free()\n\t\t}\n\n\t\treturn\n\t}\n\n\t// If node has no keys then just remove it.\n\tif n.numChildren() == 0 {\n\t\tn.parent.del(n.key)\n\t\tn.parent.removeChild(n)\n\t\tdelete(n.bucket.nodes, n.pgid)\n\t\tn.free()\n\t\tn.parent.rebalance()\n\t\treturn\n\t}\n\n\tcommon.Assert(n.parent.numChildren() > 1, \"parent must have at least 2 children\")\n\n\t// Merge with right sibling if idx == 0, otherwise left sibling.\n\tvar leftNode, rightNode *node\n\tvar useNextSibling = n.parent.childIndex(n) == 0\n\tif useNextSibling {\n\t\tleftNode = n\n\t\trightNode = n.nextSibling()\n\t} else {\n\t\tleftNode = n.prevSibling()\n\t\trightNode = n\n\t}\n\n\t// If both nodes are too small then merge them.\n\t// Reparent all child nodes being moved.\n\tfor _, inode := range rightNode.inodes {\n\t\tif child, ok := n.bucket.nodes[inode.Pgid()]; ok {\n\t\t\tchild.parent.removeChild(child)\n\t\t\tchild.parent = leftNode\n\t\t\tchild.parent.children = append(child.parent.children, child)\n\t\t}\n\t}\n\n\t// Copy over inodes from right node to left node and remove right node.\n\tleftNode.inodes = append(leftNode.inodes, rightNode.inodes...)\n\tn.parent.del(rightNode.key)\n\tn.parent.removeChild(rightNode)\n\tdelete(n.bucket.nodes, rightNode.pgid)\n\trightNode.free()\n\n\t// Either this node or the sibling node was deleted from the parent so rebalance it.\n\tn.parent.rebalance()\n}\n\n// removes a node from the list of in-memory children.\n// This does not affect the inodes.\nfunc (n *node) removeChild(target *node) {\n\tfor i, child := range n.children {\n\t\tif child == target {\n\t\t\tn.children = append(n.children[:i], n.children[i+1:]...)\n\t\t\treturn\n\t\t}\n\t}\n}\n\n// dereference causes the node to copy all its inode key/value references to heap memory.\n// This is required when the mmap is reallocated so inodes are not pointing to stale data.\nfunc (n *node) dereference() {\n\tif n.key != nil {\n\t\tkey := make([]byte, len(n.key))\n\t\tcopy(key, n.key)\n\t\tn.key = key\n\t\tcommon.Assert(n.pgid == 0 || len(n.key) > 0, \"dereference: zero-length node key on existing node\")\n\t}\n\n\tfor i := range n.inodes {\n\t\tinode := &n.inodes[i]\n\n\t\tkey := make([]byte, len(inode.Key()))\n\t\tcopy(key, inode.Key())\n\t\tinode.SetKey(key)\n\t\tcommon.Assert(len(inode.Key()) > 0, \"dereference: zero-length inode key\")\n\n\t\tvalue := make([]byte, len(inode.Value()))\n\t\tcopy(value, inode.Value())\n\t\tinode.SetValue(value)\n\t}\n\n\t// Recursively dereference children.\n\tfor _, child := range n.children {\n\t\tchild.dereference()\n\t}\n\n\t// Update statistics.\n\tn.bucket.tx.stats.IncNodeDeref(1)\n}\n\n// free adds the node's underlying page to the freelist.\nfunc (n *node) free() {\n\tif n.pgid != 0 {\n\t\tn.bucket.tx.db.freelist.Free(n.bucket.tx.meta.Txid(), n.bucket.tx.page(n.pgid))\n\t\tn.pgid = 0\n\t}\n}\n\n// dump writes the contents of the node to STDERR for debugging purposes.\n/*\nfunc (n *node) dump() {\n\t// Write node header.\n\tvar typ = \"branch\"\n\tif n.isLeaf {\n\t\ttyp = \"leaf\"\n\t}\n\twarnf(\"[NODE %d {type=%s count=%d}]\", n.pgid, typ, len(n.inodes))\n\n\t// Write out abbreviated version of each item.\n\tfor _, item := range n.inodes {\n\t\tif n.isLeaf {\n\t\t\tif item.flags&bucketLeafFlag != 0 {\n\t\t\t\tbucket := (*bucket)(unsafe.Pointer(&item.value[0]))\n\t\t\t\twarnf(\"+L %08x -> (bucket root=%d)\", trunc(item.key, 4), bucket.root)\n\t\t\t} else {\n\t\t\t\twarnf(\"+L %08x -> %08x\", trunc(item.key, 4), trunc(item.value, 4))\n\t\t\t}\n\t\t} else {\n\t\t\twarnf(\"+B %08x -> pgid=%d\", trunc(item.key, 4), item.pgid)\n\t\t}\n\t}\n\twarn(\"\")\n}\n*/\n\nfunc compareKeys(left, right []byte) int {\n\treturn bytes.Compare(left, right)\n}\n\ntype nodes []*node\n\nfunc (s nodes) Len() int      { return len(s) }\nfunc (s nodes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }\nfunc (s nodes) Less(i, j int) bool {\n\treturn bytes.Compare(s[i].inodes[0].Key(), s[j].inodes[0].Key()) == -1\n}\n"
  },
  {
    "path": "node_test.go",
    "content": "package bbolt\n\nimport (\n\t\"testing\"\n\t\"unsafe\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// Ensure that a node can insert a key/value.\nfunc TestNode_put(t *testing.T) {\n\tm := &common.Meta{}\n\tm.SetPgid(1)\n\tn := &node{inodes: make(common.Inodes, 0), bucket: &Bucket{tx: &Tx{meta: m}}}\n\tn.put([]byte(\"baz\"), []byte(\"baz\"), []byte(\"2\"), 0, 0)\n\tn.put([]byte(\"foo\"), []byte(\"foo\"), []byte(\"0\"), 0, 0)\n\tn.put([]byte(\"bar\"), []byte(\"bar\"), []byte(\"1\"), 0, 0)\n\tn.put([]byte(\"foo\"), []byte(\"foo\"), []byte(\"3\"), 0, common.LeafPageFlag)\n\n\tif len(n.inodes) != 3 {\n\t\tt.Fatalf(\"exp=3; got=%d\", len(n.inodes))\n\t}\n\tif k, v := n.inodes[0].Key(), n.inodes[0].Value(); string(k) != \"bar\" || string(v) != \"1\" {\n\t\tt.Fatalf(\"exp=<bar,1>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n.inodes[1].Key(), n.inodes[1].Value(); string(k) != \"baz\" || string(v) != \"2\" {\n\t\tt.Fatalf(\"exp=<baz,2>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n.inodes[2].Key(), n.inodes[2].Value(); string(k) != \"foo\" || string(v) != \"3\" {\n\t\tt.Fatalf(\"exp=<foo,3>; got=<%s,%s>\", k, v)\n\t}\n\tif n.inodes[2].Flags() != uint32(common.LeafPageFlag) {\n\t\tt.Fatalf(\"not a leaf: %d\", n.inodes[2].Flags())\n\t}\n}\n\n// Ensure that a node can deserialize from a leaf page.\nfunc TestNode_read_LeafPage(t *testing.T) {\n\t// Create a page.\n\tvar buf [4096]byte\n\tpage := (*common.Page)(unsafe.Pointer(&buf[0]))\n\tpage.SetFlags(common.LeafPageFlag)\n\tpage.SetCount(2)\n\n\t// Insert 2 elements at the beginning. sizeof(leafPageElement) == 16\n\tnodes := page.LeafPageElements()\n\t//nodes := (*[3]leafPageElement)(unsafe.Pointer(uintptr(unsafe.Pointer(page)) + unsafe.Sizeof(*page)))\n\tnodes[0] = *common.NewLeafPageElement(0, 32, 3, 4)  // pos = sizeof(leafPageElement) * 2\n\tnodes[1] = *common.NewLeafPageElement(0, 23, 10, 3) // pos = sizeof(leafPageElement) + 3 + 4\n\n\t// Write data for the nodes at the end.\n\tconst s = \"barfoozhelloworldbye\"\n\tdata := common.UnsafeByteSlice(unsafe.Pointer(uintptr(unsafe.Pointer(page))+unsafe.Sizeof(*page)+common.LeafPageElementSize*2), 0, 0, len(s))\n\tcopy(data, s)\n\n\t// Deserialize page into a leaf.\n\tn := &node{}\n\tn.read(page)\n\n\t// Check that there are two inodes with correct data.\n\tif !n.isLeaf {\n\t\tt.Fatal(\"expected leaf\")\n\t}\n\tif len(n.inodes) != 2 {\n\t\tt.Fatalf(\"exp=2; got=%d\", len(n.inodes))\n\t}\n\tif k, v := n.inodes[0].Key(), n.inodes[0].Value(); string(k) != \"bar\" || string(v) != \"fooz\" {\n\t\tt.Fatalf(\"exp=<bar,fooz>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n.inodes[1].Key(), n.inodes[1].Value(); string(k) != \"helloworld\" || string(v) != \"bye\" {\n\t\tt.Fatalf(\"exp=<helloworld,bye>; got=<%s,%s>\", k, v)\n\t}\n}\n\n// Ensure that a node can serialize into a leaf page.\nfunc TestNode_write_LeafPage(t *testing.T) {\n\t// Create a node.\n\tm := &common.Meta{}\n\tm.SetPgid(1)\n\tn := &node{isLeaf: true, inodes: make(common.Inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: m}}}\n\tn.put([]byte(\"susy\"), []byte(\"susy\"), []byte(\"que\"), 0, 0)\n\tn.put([]byte(\"ricki\"), []byte(\"ricki\"), []byte(\"lake\"), 0, 0)\n\tn.put([]byte(\"john\"), []byte(\"john\"), []byte(\"johnson\"), 0, 0)\n\n\t// Write it to a page.\n\tvar buf [4096]byte\n\tp := (*common.Page)(unsafe.Pointer(&buf[0]))\n\tn.write(p)\n\n\t// Read the page back in.\n\tn2 := &node{}\n\tn2.read(p)\n\n\t// Check that the two pages are the same.\n\tif len(n2.inodes) != 3 {\n\t\tt.Fatalf(\"exp=3; got=%d\", len(n2.inodes))\n\t}\n\tif k, v := n2.inodes[0].Key(), n2.inodes[0].Value(); string(k) != \"john\" || string(v) != \"johnson\" {\n\t\tt.Fatalf(\"exp=<john,johnson>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n2.inodes[1].Key(), n2.inodes[1].Value(); string(k) != \"ricki\" || string(v) != \"lake\" {\n\t\tt.Fatalf(\"exp=<ricki,lake>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n2.inodes[2].Key(), n2.inodes[2].Value(); string(k) != \"susy\" || string(v) != \"que\" {\n\t\tt.Fatalf(\"exp=<susy,que>; got=<%s,%s>\", k, v)\n\t}\n}\n\n// Ensure that a node can split into appropriate subgroups.\nfunc TestNode_split(t *testing.T) {\n\t// Create a node.\n\tm := &common.Meta{}\n\tm.SetPgid(1)\n\tn := &node{inodes: make(common.Inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: m}}}\n\tn.put([]byte(\"00000001\"), []byte(\"00000001\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000002\"), []byte(\"00000002\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000003\"), []byte(\"00000003\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000004\"), []byte(\"00000004\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000005\"), []byte(\"00000005\"), []byte(\"0123456701234567\"), 0, 0)\n\n\t// Split between 2 & 3.\n\tn.split(100)\n\n\tvar parent = n.parent\n\tif len(parent.children) != 2 {\n\t\tt.Fatalf(\"exp=2; got=%d\", len(parent.children))\n\t}\n\tif len(parent.children[0].inodes) != 2 {\n\t\tt.Fatalf(\"exp=2; got=%d\", len(parent.children[0].inodes))\n\t}\n\tif len(parent.children[1].inodes) != 3 {\n\t\tt.Fatalf(\"exp=3; got=%d\", len(parent.children[1].inodes))\n\t}\n}\n\n// Ensure that a page with the minimum number of inodes just returns a single node.\nfunc TestNode_split_MinKeys(t *testing.T) {\n\t// Create a node.\n\tm := &common.Meta{}\n\tm.SetPgid(1)\n\tn := &node{inodes: make(common.Inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: m}}}\n\tn.put([]byte(\"00000001\"), []byte(\"00000001\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000002\"), []byte(\"00000002\"), []byte(\"0123456701234567\"), 0, 0)\n\n\t// Split.\n\tn.split(20)\n\tif n.parent != nil {\n\t\tt.Fatalf(\"expected nil parent\")\n\t}\n}\n\n// Ensure that a node that has keys that all fit on a page just returns one leaf.\nfunc TestNode_split_SinglePage(t *testing.T) {\n\t// Create a node.\n\tm := &common.Meta{}\n\tm.SetPgid(1)\n\tn := &node{inodes: make(common.Inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: m}}}\n\tn.put([]byte(\"00000001\"), []byte(\"00000001\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000002\"), []byte(\"00000002\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000003\"), []byte(\"00000003\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000004\"), []byte(\"00000004\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000005\"), []byte(\"00000005\"), []byte(\"0123456701234567\"), 0, 0)\n\n\t// Split.\n\tn.split(4096)\n\tif n.parent != nil {\n\t\tt.Fatalf(\"expected nil parent\")\n\t}\n}\n"
  },
  {
    "path": "quick_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"bytes\"\n\t\"flag\"\n\t\"fmt\"\n\t\"math/rand\"\n\t\"os\"\n\t\"reflect\"\n\t\"testing\"\n\t\"testing/quick\"\n\t\"time\"\n)\n\n// testing/quick defaults to 5 iterations and a random seed.\n// You can override these settings from the command line:\n//\n//   -quick.count     The number of iterations to perform.\n//   -quick.seed      The seed to use for randomizing.\n//   -quick.maxitems  The maximum number of items to insert into a DB.\n//   -quick.maxksize  The maximum size of a key.\n//   -quick.maxvsize  The maximum size of a value.\n//\n\nvar qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int\n\nfunc TestMain(m *testing.M) {\n\tflag.IntVar(&qcount, \"quick.count\", 5, \"\")\n\tflag.IntVar(&qseed, \"quick.seed\", int(time.Now().UnixNano())%100000, \"\")\n\tflag.IntVar(&qmaxitems, \"quick.maxitems\", 1000, \"\")\n\tflag.IntVar(&qmaxksize, \"quick.maxksize\", 1024, \"\")\n\tflag.IntVar(&qmaxvsize, \"quick.maxvsize\", 1024, \"\")\n\tflag.Parse()\n\tfmt.Fprintln(os.Stderr, \"seed:\", qseed)\n\tfmt.Fprintf(os.Stderr, \"quick settings: count=%v, items=%v, ksize=%v, vsize=%v\\n\", qcount, qmaxitems, qmaxksize, qmaxvsize)\n\n\tos.Exit(m.Run())\n}\n\nfunc qconfig() *quick.Config {\n\treturn &quick.Config{\n\t\tMaxCount: qcount,\n\t\tRand:     rand.New(rand.NewSource(int64(qseed))),\n\t}\n}\n\ntype testdata []testdataitem\n\nfunc (t testdata) Len() int           { return len(t) }\nfunc (t testdata) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }\nfunc (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 }\n\nfunc (t testdata) Generate(rand *rand.Rand, size int) reflect.Value {\n\tn := rand.Intn(qmaxitems-1) + 1\n\titems := make(testdata, n)\n\tused := make(map[string]bool)\n\tfor i := 0; i < n; i++ {\n\t\titem := &items[i]\n\t\t// Ensure that keys are unique by looping until we find one that we have not already used.\n\t\tfor {\n\t\t\titem.Key = randByteSlice(rand, 1, qmaxksize)\n\t\t\tif !used[string(item.Key)] {\n\t\t\t\tused[string(item.Key)] = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\titem.Value = randByteSlice(rand, 0, qmaxvsize)\n\t}\n\treturn reflect.ValueOf(items)\n}\n\ntype revtestdata []testdataitem\n\nfunc (t revtestdata) Len() int           { return len(t) }\nfunc (t revtestdata) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }\nfunc (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 }\n\ntype testdataitem struct {\n\tKey   []byte\n\tValue []byte\n}\n\nfunc randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte {\n\tn := rand.Intn(maxSize-minSize) + minSize\n\tb := make([]byte, n)\n\tfor i := 0; i < n; i++ {\n\t\tb[i] = byte(rand.Intn(255))\n\t}\n\treturn b\n}\n"
  },
  {
    "path": "scripts/compare_benchmarks.sh",
    "content": "#!/usr/bin/env bash\n# https://github.com/kubernetes/kube-state-metrics/blob/main/tests/compare_benchmarks.sh (originally written by mxinden)\n\n# exit immediately when a command fails\nset -e\n# only exit with zero if all commands of the pipeline exit successfully\nset -o pipefail\n# error on unset variables\nset -u\n\n[[ \"$#\" -eq 1 ]] || echo \"One argument required, $# provided.\"\n\nREF_CURRENT=\"$(git rev-parse --abbrev-ref HEAD)\"\nBASE_TO_COMPARE=$1\n\nRESULT_CURRENT=\"$(mktemp)-${REF_CURRENT}\"\nRESULT_TO_COMPARE=\"$(mktemp)-${BASE_TO_COMPARE}\"\n\nBENCH_COUNT=${BENCH_COUNT:-10}\nBENCHSTAT_CONFIDENCE_LEVEL=${BENCHSTAT_CONFIDENCE_LEVEL:-0.9}\nBENCHSTAT_FORMAT=${BENCHSTAT_FORMAT:-\"text\"}\nBENCH_PARAMETERS=${BENCH_PARAMETERS:-\"--count 2000000 --batch-size 10000\"}\n\nif [[ \"${BENCHSTAT_FORMAT}\" == \"csv\" ]] && [[ -z \"${BENCHSTAT_OUTPUT_FILE}\" ]]; then\n  echo \"BENCHSTAT_FORMAT is set to csv, but BENCHSTAT_OUTPUT_FILE is not set.\"\n  exit 1\nfi\n\nfunction bench() {\n  local output_file\n  output_file=\"$1\"\n  make build\n\n  for _ in $(seq \"$BENCH_COUNT\"); do\n    echo ./bin/bbolt bench --gobench-output --profile-mode n ${BENCH_PARAMETERS}\n    # shellcheck disable=SC2086\n    ./bin/bbolt bench --gobench-output --profile-mode n ${BENCH_PARAMETERS} >> \"${output_file}\"\n  done\n}\n\nfunction main() {\n  echo \"### Benchmarking PR ${REF_CURRENT}\"\n  bench \"${RESULT_CURRENT}\"\n  echo \"\"\n  echo \"### Done benchmarking ${REF_CURRENT}\"\n\n  echo \"### Benchmarking base ${BASE_TO_COMPARE}\"\n  git checkout \"${BASE_TO_COMPARE}\"\n  bench \"${RESULT_TO_COMPARE}\"\n  echo \"\"\n  echo \"### Done benchmarking ${BASE_TO_COMPARE}\"\n\n  git checkout -\n\n  echo \"\"\n  echo \"### Result\"\n  echo \"BASE=${BASE_TO_COMPARE} HEAD=${REF_CURRENT}\"\n\n  if [[ \"${BENCHSTAT_FORMAT}\" == \"csv\" ]]; then\n    go tool golang.org/x/perf/cmd/benchstat -format=csv -confidence=\"${BENCHSTAT_CONFIDENCE_LEVEL}\" BASE=\"${RESULT_TO_COMPARE}\" HEAD=\"${RESULT_CURRENT}\" 2>/dev/null 1>\"${BENCHSTAT_OUTPUT_FILE}\"\n  else\n    if [[ -z \"${BENCHSTAT_OUTPUT_FILE}\" ]]; then\n      go tool golang.org/x/perf/cmd/benchstat -confidence=\"${BENCHSTAT_CONFIDENCE_LEVEL}\" BASE=\"${RESULT_TO_COMPARE}\" HEAD=\"${RESULT_CURRENT}\"\n    else\n      go tool golang.org/x/perf/cmd/benchstat -confidence=\"${BENCHSTAT_CONFIDENCE_LEVEL}\" BASE=\"${RESULT_TO_COMPARE}\" HEAD=\"${RESULT_CURRENT}\" 1>\"${BENCHSTAT_OUTPUT_FILE}\"\n    fi\n  fi\n}\n\nmain\n"
  },
  {
    "path": "scripts/fix.sh",
    "content": "GO_CMD=\"go\"\n\n# TODO(ptabor): Expand to cover different architectures (GOOS GOARCH), or just list go files.\n\nGOFILES=$(${GO_CMD} list  --f \"{{with \\$d:=.}}{{range .GoFiles}}{{\\$d.Dir}}/{{.}}{{\\\"\\n\\\"}}{{end}}{{end}}\" ./...)\nTESTGOFILES=$(${GO_CMD} list  --f \"{{with \\$d:=.}}{{range .TestGoFiles}}{{\\$d.Dir}}/{{.}}{{\\\"\\n\\\"}}{{end}}{{end}}\" ./...)\nXTESTGOFILES=$(${GO_CMD} list  --f \"{{with \\$d:=.}}{{range .XTestGoFiles}}{{\\$d.Dir}}/{{.}}{{\\\"\\n\\\"}}{{end}}{{end}}\" ./...)\n\n\necho \"${GOFILES}\" \"${TESTGOFILES}\" \"${XTESTGOFILES}\"| xargs -n 100 go tool golang.org/x/tools/cmd/goimports -w -local go.etcd.io\n\ngo fmt ./...\ngo mod tidy\n"
  },
  {
    "path": "scripts/release.sh",
    "content": "#!/usr/bin/env bash\n\nset -o errexit\nset -o nounset\nset -o pipefail\n\n# === Function Definitions ===\nfunction get_gpg_key {\n  local git_email\n  local key_id\n\n  git_email=$(git config --get user.email)\n  key_id=$(gpg --list-keys --with-colons \"${git_email}\" | awk -F: '/^pub:/ { print $5 }')\n  if [[ -z \"${key_id}\" ]]; then\n    echo \"Failed to load gpg key. Is gpg set up correctly for etcd releases?\"\n    return 2\n  fi\n  echo \"${key_id}\"\n}\n\n# === Main Script Logic ===\nfunction main {\n  VERSION=\"$1\"\n\n  if [ -z \"${VERSION}\" ]; then\n    read -p \"Release version (e.g., v1.2.3) \" -r VERSION\n    if [[ ! \"${VERSION}\" =~ ^v[0-9]+.[0-9]+.[0-9]+ ]]; then\n      echo \"Expected 'version' param of the form 'v<major-version>.<minor-version>.<patch-version>' but got '${VERSION}'\"\n      exit 1\n    fi\n  fi\n\n  VERSION=v${VERSION#v}\n  RELEASE_VERSION=\"${VERSION#v}\"\n  MINOR_VERSION=$(echo \"${RELEASE_VERSION}\" | cut -d. -f 1-2)\n  RELEASE_BRANCH=\"release-${MINOR_VERSION}\"\n\n  REPOSITORY=${REPOSITORY:-\"git@github.com:etcd-io/bbolt.git\"}\n\n  local remote_tag_exists\n  remote_tag_exists=$(git ls-remote \"${REPOSITORY}\" \"refs/tags/${VERSION}\" | grep -c \"${VERSION}\" || true)\n  if [ \"${remote_tag_exists}\" -gt 0 ]; then\n    echo \"Release version tag exists on remote.\"\n    exit 1\n  fi\n\n  # Set up release directory.\n  local reldir=\"/tmp/bbolt-release-${VERSION}\"\n  echo \"Preparing temporary directory: ${reldir}\"\n  if [ ! -d \"${reldir}/bbolt\" ]; then\n    mkdir -p \"${reldir}\"\n    cd \"${reldir}\"\n    git clone \"${REPOSITORY}\" --branch \"${RELEASE_BRANCH}\" --depth 1\n  fi\n  cd \"${reldir}/bbolt\" || exit 2\n  git checkout \"${RELEASE_BRANCH}\" || exit 2\n  git fetch origin\n  git reset --hard \"origin/${RELEASE_BRANCH}\"\n\n  # ensuring the minor-version is identical.\n  source_version=$(grep -E \"\\s+Version\\s*=\" ./version/version.go | sed -e \"s/.*\\\"\\(.*\\)\\\".*/\\1/g\")\n  if [[ \"${source_version}\" != \"${RELEASE_VERSION}\" ]]; then\n     source_minor_version=$(echo \"${source_version}\" | cut -d. -f 1-2)\n     if [[ \"${source_minor_version}\" != \"${MINOR_VERSION}\" ]]; then\n       echo \"Wrong bbolt minor version in version.go. Expected ${MINOR_VERSION} but got ${source_minor_version}. Aborting.\"\n       exit 1\n     fi\n  fi\n\n  # bump 'version.go'.\n  echo \"Updating version from '${source_version}' to '${RELEASE_VERSION}' in 'version.go'\"\n  if [[ \"$OSTYPE\" == \"darwin\"* ]]; then\n    sed -i '' \"s/${source_version}/${RELEASE_VERSION}/g\" ./version/version.go\n  else\n    sed -i \"s/${source_version}/${RELEASE_VERSION}/g\" ./version/version.go\n  fi\n\n  # push 'version.go' to remote.\n  echo \"committing 'version.go'\"\n  git add ./version/version.go\n  git commit -s -m \"Update version to ${VERSION}\"\n  git push origin \"${RELEASE_BRANCH}\"\n  echo \"'version.go' has been committed to remote repo.\"\n\n  # create tag and push to remote.\n  echo \"Creating new tag for '${VERSION}'\"\n  key_id=$(get_gpg_key) || return 2\n  git tag --local-user \"${key_id}\" --sign \"${VERSION}\" --message \"${VERSION}\"\n  git push origin \"${VERSION}\"\n  echo \"Tag '${VERSION}' has been created and pushed to remote repo.\"\n  echo \"SUCCESS\"\n}\n\nmain \"$1\"\n"
  },
  {
    "path": "simulation_no_freelist_sync_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"testing\"\n\n\tbolt \"go.etcd.io/bbolt\"\n)\n\nfunc TestSimulateNoFreeListSync_1op_1p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1, 1)\n}\nfunc TestSimulateNoFreeListSync_10op_1p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10, 1)\n}\nfunc TestSimulateNoFreeListSync_100op_1p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 100, 1)\n}\nfunc TestSimulateNoFreeListSync_1000op_1p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1000, 1)\n}\nfunc TestSimulateNoFreeListSync_10000op_1p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 1)\n}\nfunc TestSimulateNoFreeListSync_10op_10p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10, 10)\n}\nfunc TestSimulateNoFreeListSync_100op_10p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 100, 10)\n}\nfunc TestSimulateNoFreeListSync_1000op_10p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1000, 10)\n}\nfunc TestSimulateNoFreeListSync_10000op_10p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 10)\n}\nfunc TestSimulateNoFreeListSync_100op_100p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 100, 100)\n}\nfunc TestSimulateNoFreeListSync_1000op_100p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1000, 100)\n}\nfunc TestSimulateNoFreeListSync_10000op_100p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 100)\n}\nfunc TestSimulateNoFreeListSync_10000op_1000p(t *testing.T) {\n\ttestSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 1000)\n}\n"
  },
  {
    "path": "simulation_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"math/rand\"\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"testing\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\nfunc TestSimulate_1op_1p(t *testing.T)     { testSimulate(t, nil, 1, 1, 1) }\nfunc TestSimulate_10op_1p(t *testing.T)    { testSimulate(t, nil, 1, 10, 1) }\nfunc TestSimulate_100op_1p(t *testing.T)   { testSimulate(t, nil, 1, 100, 1) }\nfunc TestSimulate_1000op_1p(t *testing.T)  { testSimulate(t, nil, 1, 1000, 1) }\nfunc TestSimulate_10000op_1p(t *testing.T) { testSimulate(t, nil, 1, 10000, 1) }\n\nfunc TestSimulate_10op_10p(t *testing.T)    { testSimulate(t, nil, 1, 10, 10) }\nfunc TestSimulate_100op_10p(t *testing.T)   { testSimulate(t, nil, 1, 100, 10) }\nfunc TestSimulate_1000op_10p(t *testing.T)  { testSimulate(t, nil, 1, 1000, 10) }\nfunc TestSimulate_10000op_10p(t *testing.T) { testSimulate(t, nil, 1, 10000, 10) }\n\nfunc TestSimulate_100op_100p(t *testing.T)   { testSimulate(t, nil, 1, 100, 100) }\nfunc TestSimulate_1000op_100p(t *testing.T)  { testSimulate(t, nil, 1, 1000, 100) }\nfunc TestSimulate_10000op_100p(t *testing.T) { testSimulate(t, nil, 1, 10000, 100) }\n\nfunc TestSimulate_10000op_1000p(t *testing.T) { testSimulate(t, nil, 1, 10000, 1000) }\n\n// Randomly generate operations on a given database with multiple clients to ensure consistency and thread safety.\nfunc testSimulate(t *testing.T, openOption *bolt.Options, round, threadCount, parallelism int) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\t// A list of operations that readers and writers can perform.\n\tvar readerHandlers = []simulateHandler{simulateGetHandler}\n\tvar writerHandlers = []simulateHandler{simulateGetHandler, simulatePutHandler}\n\n\tvar versions = make(map[int]*QuickDB)\n\tversions[1] = NewQuickDB()\n\n\tdb := btesting.MustCreateDBWithOption(t, openOption)\n\n\tvar mutex sync.Mutex\n\n\tfor n := 0; n < round; n++ {\n\t\t// Run n threads in parallel, each with their own operation.\n\t\tvar threads = make(chan bool, parallelism)\n\t\tvar wg sync.WaitGroup\n\n\t\t// counter for how many goroutines were fired\n\t\tvar opCount int64\n\n\t\t// counter for ignored operations\n\t\tvar igCount int64\n\n\t\tvar errCh = make(chan error, threadCount)\n\n\t\tvar i int\n\t\tfor {\n\t\t\t// this buffered channel will keep accepting booleans\n\t\t\t// until it hits the limit defined by the parallelism\n\t\t\t// argument to testSimulate()\n\t\t\tthreads <- true\n\n\t\t\t// this wait group can only be marked \"done\" from inside\n\t\t\t// the subsequent goroutine\n\t\t\twg.Add(1)\n\t\t\twritable := ((rand.Int() % 100) < 20) // 20% writers\n\n\t\t\t// Choose an operation to execute.\n\t\t\tvar handler simulateHandler\n\t\t\tif writable {\n\t\t\t\thandler = writerHandlers[rand.Intn(len(writerHandlers))]\n\t\t\t} else {\n\t\t\t\thandler = readerHandlers[rand.Intn(len(readerHandlers))]\n\t\t\t}\n\n\t\t\t// Execute a thread for the given operation.\n\t\t\tgo func(writable bool, handler simulateHandler) {\n\t\t\t\tdefer wg.Done()\n\t\t\t\tatomic.AddInt64(&opCount, 1)\n\t\t\t\t// Start transaction.\n\t\t\t\ttx, err := db.Begin(writable)\n\t\t\t\tif err != nil {\n\t\t\t\t\terrCh <- fmt.Errorf(\"error tx begin: %v\", err)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\t// Obtain current state of the dataset.\n\t\t\t\tmutex.Lock()\n\t\t\t\tvar qdb = versions[tx.ID()]\n\t\t\t\tif writable {\n\t\t\t\t\tqdb = versions[tx.ID()-1].Copy()\n\t\t\t\t}\n\t\t\t\tmutex.Unlock()\n\n\t\t\t\t// Make sure we commit/rollback the tx at the end and update the state.\n\t\t\t\tif writable {\n\t\t\t\t\tdefer func() {\n\t\t\t\t\t\tmutex.Lock()\n\t\t\t\t\t\tversions[tx.ID()] = qdb\n\t\t\t\t\t\tmutex.Unlock()\n\n\t\t\t\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\t\t\t\terrCh <- err\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t\t}\n\t\t\t\t\t}()\n\t\t\t\t} else {\n\t\t\t\t\tdefer func() { _ = tx.Rollback() }()\n\t\t\t\t}\n\n\t\t\t\t// Ignore operation if we don't have data yet.\n\t\t\t\tif qdb == nil {\n\t\t\t\t\tatomic.AddInt64(&igCount, 1)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\t// Execute handler.\n\t\t\t\thandler(tx, qdb)\n\n\t\t\t\t// Release a thread back to the scheduling loop.\n\t\t\t\t<-threads\n\t\t\t}(writable, handler)\n\n\t\t\ti++\n\t\t\tif i >= threadCount {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\t// Wait until all threads are done.\n\t\twg.Wait()\n\t\tt.Logf(\"transactions:%d ignored:%d\", opCount, igCount)\n\t\tclose(errCh)\n\t\tfor err := range errCh {\n\t\t\tif err != nil {\n\t\t\t\tt.Fatalf(\"error from inside goroutine: %v\", err)\n\t\t\t}\n\t\t}\n\n\t\tdb.MustClose()\n\t\t// I have doubts the DB drop is indented here (as 'versions' is not being reset).\n\t\t// But I'm preserving for now the original behavior.\n\t\tdb.MustDeleteFile()\n\t\tdb.MustReopen()\n\t}\n\n}\n\ntype simulateHandler func(tx *bolt.Tx, qdb *QuickDB)\n\n// Retrieves a key from the database and verifies that it is what is expected.\nfunc simulateGetHandler(tx *bolt.Tx, qdb *QuickDB) {\n\t// Randomly retrieve an existing exist.\n\tkeys := qdb.Rand()\n\tif len(keys) == 0 {\n\t\treturn\n\t}\n\n\t// Retrieve root bucket.\n\tb := tx.Bucket(keys[0])\n\tif b == nil {\n\t\tpanic(fmt.Sprintf(\"bucket[0] expected: %08x\\n\", trunc(keys[0], 4)))\n\t}\n\n\t// Drill into nested buckets.\n\tfor _, key := range keys[1 : len(keys)-1] {\n\t\tb = b.Bucket(key)\n\t\tif b == nil {\n\t\t\tpanic(fmt.Sprintf(\"bucket[n] expected: %v -> %v\\n\", keys, key))\n\t\t}\n\t}\n\n\t// Verify key/value on the final bucket.\n\texpected := qdb.Get(keys)\n\tactual := b.Get(keys[len(keys)-1])\n\tif !bytes.Equal(actual, expected) {\n\t\tfmt.Println(\"=== EXPECTED ===\")\n\t\tfmt.Println(expected)\n\t\tfmt.Println(\"=== ACTUAL ===\")\n\t\tfmt.Println(actual)\n\t\tfmt.Println(\"=== END ===\")\n\t\tpanic(\"value mismatch\")\n\t}\n}\n\n// Inserts a key into the database.\nfunc simulatePutHandler(tx *bolt.Tx, qdb *QuickDB) {\n\tvar err error\n\tkeys, value := randKeys(), randValue()\n\n\t// Retrieve root bucket.\n\tb := tx.Bucket(keys[0])\n\tif b == nil {\n\t\tb, err = tx.CreateBucket(keys[0])\n\t\tif err != nil {\n\t\t\tpanic(\"create bucket: \" + err.Error())\n\t\t}\n\t}\n\n\t// Create nested buckets, if necessary.\n\tfor _, key := range keys[1 : len(keys)-1] {\n\t\tchild := b.Bucket(key)\n\t\tif child != nil {\n\t\t\tb = child\n\t\t} else {\n\t\t\tb, err = b.CreateBucket(key)\n\t\t\tif err != nil {\n\t\t\t\tpanic(\"create bucket: \" + err.Error())\n\t\t\t}\n\t\t}\n\t}\n\n\t// Insert into database.\n\tif err := b.Put(keys[len(keys)-1], value); err != nil {\n\t\tpanic(\"put: \" + err.Error())\n\t}\n\n\t// Insert into in-memory database.\n\tqdb.Put(keys, value)\n}\n\n// QuickDB is an in-memory database that replicates the functionality of the\n// Bolt DB type except that it is entirely in-memory. It is meant for testing\n// that the Bolt database is consistent.\ntype QuickDB struct {\n\tsync.RWMutex\n\tm map[string]interface{}\n}\n\n// NewQuickDB returns an instance of QuickDB.\nfunc NewQuickDB() *QuickDB {\n\treturn &QuickDB{m: make(map[string]interface{})}\n}\n\n// Get retrieves the value at a key path.\nfunc (db *QuickDB) Get(keys [][]byte) []byte {\n\tdb.RLock()\n\tdefer db.RUnlock()\n\n\tm := db.m\n\tfor _, key := range keys[:len(keys)-1] {\n\t\tvalue := m[string(key)]\n\t\tif value == nil {\n\t\t\treturn nil\n\t\t}\n\t\tswitch value := value.(type) {\n\t\tcase map[string]interface{}:\n\t\t\tm = value\n\t\tcase []byte:\n\t\t\treturn nil\n\t\t}\n\t}\n\n\t// Only return if it's a simple value.\n\tif value, ok := m[string(keys[len(keys)-1])].([]byte); ok {\n\t\treturn value\n\t}\n\treturn nil\n}\n\n// Put inserts a value into a key path.\nfunc (db *QuickDB) Put(keys [][]byte, value []byte) {\n\tdb.Lock()\n\tdefer db.Unlock()\n\n\t// Build buckets all the way down the key path.\n\tm := db.m\n\tfor _, key := range keys[:len(keys)-1] {\n\t\tif _, ok := m[string(key)].([]byte); ok {\n\t\t\treturn // Keypath intersects with a simple value. Do nothing.\n\t\t}\n\n\t\tif m[string(key)] == nil {\n\t\t\tm[string(key)] = make(map[string]interface{})\n\t\t}\n\t\tm = m[string(key)].(map[string]interface{})\n\t}\n\n\t// Insert value into the last key.\n\tm[string(keys[len(keys)-1])] = value\n}\n\n// Rand returns a random key path that points to a simple value.\nfunc (db *QuickDB) Rand() [][]byte {\n\tdb.RLock()\n\tdefer db.RUnlock()\n\tif len(db.m) == 0 {\n\t\treturn nil\n\t}\n\tvar keys [][]byte\n\tdb.rand(db.m, &keys)\n\treturn keys\n}\n\nfunc (db *QuickDB) rand(m map[string]interface{}, keys *[][]byte) {\n\ti, index := 0, rand.Intn(len(m))\n\tfor k, v := range m {\n\t\tif i == index {\n\t\t\t*keys = append(*keys, []byte(k))\n\t\t\tif v, ok := v.(map[string]interface{}); ok {\n\t\t\t\tdb.rand(v, keys)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\ti++\n\t}\n\tpanic(\"quickdb rand: out-of-range\")\n}\n\n// Copy copies the entire database.\nfunc (db *QuickDB) Copy() *QuickDB {\n\tdb.RLock()\n\tdefer db.RUnlock()\n\treturn &QuickDB{m: db.copy(db.m)}\n}\n\nfunc (db *QuickDB) copy(m map[string]interface{}) map[string]interface{} {\n\tclone := make(map[string]interface{}, len(m))\n\tfor k, v := range m {\n\t\tswitch v := v.(type) {\n\t\tcase map[string]interface{}:\n\t\t\tclone[k] = db.copy(v)\n\t\tdefault:\n\t\t\tclone[k] = v\n\t\t}\n\t}\n\treturn clone\n}\n\nfunc randKey() []byte {\n\tvar min, max = 1, 1024\n\tn := rand.Intn(max-min) + min\n\tb := make([]byte, n)\n\tfor i := 0; i < n; i++ {\n\t\tb[i] = byte(rand.Intn(255))\n\t}\n\treturn b\n}\n\nfunc randKeys() [][]byte {\n\tvar keys [][]byte\n\tvar count = rand.Intn(2) + 2\n\tfor i := 0; i < count; i++ {\n\t\tkeys = append(keys, randKey())\n\t}\n\treturn keys\n}\n\nfunc randValue() []byte {\n\tn := rand.Intn(8192)\n\tb := make([]byte, n)\n\tfor i := 0; i < n; i++ {\n\t\tb[i] = byte(rand.Intn(255))\n\t}\n\treturn b\n}\n"
  },
  {
    "path": "tests/dmflakey/dmflakey.go",
    "content": "//go:build linux\n\npackage dmflakey\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n\t\"os/exec\"\n\t\"path\"\n\t\"path/filepath\"\n\t\"strings\"\n\t\"time\"\n\n\t\"golang.org/x/sys/unix\"\n)\n\ntype featCfg struct {\n\t// SyncFS attempts to synchronize filesystem before inject failure.\n\tsyncFS bool\n\t// interval is used to determine the up time for feature.\n\t//\n\t// For AllowWrites, it means that the device is available for `interval` seconds.\n\t// For Other features, the device exhibits unreliable behaviour for\n\t// `interval` seconds.\n\tinterval time.Duration\n}\n\n// Default values.\nconst (\n\t// defaultImgSize is the default size for filesystem image.\n\tdefaultImgSize int64 = 1024 * 1024 * 1024 * 10 // 10 GiB\n\t// defaultInterval is the default interval for the up time of feature.\n\tdefaultInterval = 2 * time.Minute\n)\n\n// defaultFeatCfg is the default setting for flakey feature.\nvar defaultFeatCfg = featCfg{interval: defaultInterval}\n\n// FeatOpt is used to configure failure feature.\ntype FeatOpt func(*featCfg)\n\n// WithIntervalFeatOpt updates the up time for the feature.\nfunc WithIntervalFeatOpt(interval time.Duration) FeatOpt {\n\treturn func(cfg *featCfg) {\n\t\tcfg.interval = interval\n\t}\n}\n\n// WithSyncFSFeatOpt is to determine if the caller wants to synchronize\n// filesystem before inject failure.\nfunc WithSyncFSFeatOpt(syncFS bool) FeatOpt {\n\treturn func(cfg *featCfg) {\n\t\tcfg.syncFS = syncFS\n\t}\n}\n\n// Flakey is to inject failure into device.\ntype Flakey interface {\n\t// DevicePath returns the flakey device path.\n\tDevicePath() string\n\n\t// Filesystem returns filesystem's type.\n\tFilesystem() FSType\n\n\t// AllowWrites allows write I/O.\n\tAllowWrites(opts ...FeatOpt) error\n\n\t// DropWrites drops all write I/O silently.\n\tDropWrites(opts ...FeatOpt) error\n\n\t// ErrorWrites drops all write I/O and returns error.\n\tErrorWrites(opts ...FeatOpt) error\n\n\t// Teardown releases the flakey device.\n\tTeardown() error\n}\n\n// FSType represents the filesystem name.\ntype FSType string\n\n// Supported filesystems.\nconst (\n\tFSTypeEXT4 FSType = \"ext4\"\n\tFSTypeXFS  FSType = \"xfs\"\n)\n\n// InitFlakey creates an filesystem on a loopback device and returns Flakey on it.\n//\n// The device-mapper device will be /dev/mapper/$flakeyDevice. And the filesystem\n// image will be created at $dataStorePath/$flakeyDevice.img. By default, the\n// device is available for 2 minutes and size is 10 GiB.\nfunc InitFlakey(flakeyDevice, dataStorePath string, fsType FSType, mkfsOpt string) (_ Flakey, retErr error) {\n\timgPath := filepath.Join(dataStorePath, fmt.Sprintf(\"%s.img\", flakeyDevice))\n\tif err := createEmptyFSImage(imgPath, fsType, mkfsOpt); err != nil {\n\t\treturn nil, err\n\t}\n\tdefer func() {\n\t\tif retErr != nil {\n\t\t\tos.RemoveAll(imgPath)\n\t\t}\n\t}()\n\n\tloopDevice, err := attachToLoopDevice(imgPath)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer func() {\n\t\tif retErr != nil {\n\t\t\t_ = detachLoopDevice(loopDevice)\n\t\t}\n\t}()\n\n\timgSize, err := getBlkSize(loopDevice)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif err := newFlakeyDevice(flakeyDevice, loopDevice, defaultInterval); err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn &flakey{\n\t\tfsType:  fsType,\n\t\timgPath: imgPath,\n\t\timgSize: imgSize,\n\n\t\tloopDevice:   loopDevice,\n\t\tflakeyDevice: flakeyDevice,\n\t}, nil\n}\n\ntype flakey struct {\n\tfsType  FSType\n\timgPath string\n\timgSize int64\n\n\tloopDevice   string\n\tflakeyDevice string\n}\n\n// DevicePath returns the flakey device path.\nfunc (f *flakey) DevicePath() string {\n\treturn fmt.Sprintf(\"/dev/mapper/%s\", f.flakeyDevice)\n}\n\n// Filesystem returns filesystem's type.\nfunc (f *flakey) Filesystem() FSType {\n\treturn f.fsType\n}\n\n// AllowWrites allows write I/O.\nfunc (f *flakey) AllowWrites(opts ...FeatOpt) error {\n\tvar o = defaultFeatCfg\n\tfor _, opt := range opts {\n\t\topt(&o)\n\t}\n\n\t// NOTE: Table parameters\n\t//\n\t// 0 imgSize flakey <dev path> <offset> <up interval> <down interval> [<num_features> [<feature arguments>]]\n\t//\n\t// Mandatory parameters:\n\t//\n\t//  <dev path>: Full pathname to the underlying block-device, or a \"major:minor\" device-number.\n\t//  <offset>: Starting sector within the device.\n\t//  <up interval>: Number of seconds device is available.\n\t//  <down interval>: Number of seconds device returns errors.\n\t//\n\t// Optional:\n\t//\n\t// If no feature parameters are present, during the periods of unreliability, all I/O returns errors.\n\t//\n\t// For AllowWrites, the device will handle data correctly in `interval` seconds.\n\t//\n\t// REF: https://docs.kernel.org/admin-guide/device-mapper/dm-flakey.html.\n\ttable := fmt.Sprintf(\"0 %d flakey %s 0 %d 0\",\n\t\tf.imgSize, f.loopDevice, int(o.interval.Seconds()))\n\n\treturn reloadFlakeyDevice(f.flakeyDevice, o.syncFS, table)\n}\n\n// DropWrites drops all write I/O silently.\nfunc (f *flakey) DropWrites(opts ...FeatOpt) error {\n\tvar o = defaultFeatCfg\n\tfor _, opt := range opts {\n\t\topt(&o)\n\t}\n\n\t// NOTE: Table parameters\n\t//\n\t// 0 imgSize flakey <dev path> <offset> <up interval> <down interval> [<num_features> [<feature arguments>]]\n\t//\n\t// Mandatory parameters:\n\t//\n\t//  <dev path>: Full pathname to the underlying block-device, or a \"major:minor\" device-number.\n\t//  <offset>: Starting sector within the device.\n\t//  <up interval>: Number of seconds device is available.\n\t//  <down interval>: Number of seconds device returns errors.\n\t//\n\t// Optional:\n\t//\n\t// <num_features>: How many arguments (length of <feature_arguments>)\n\t//\n\t// For DropWrites,\n\t//\n\t// num_features: 1 (there is only one argument)\n\t// feature_arguments: drop_writes\n\t//\n\t// The Device will drop all the writes into disk in `interval` seconds.\n\t// Read I/O is handled correctly.\n\t//\n\t// For example, the application calls fsync, all the dirty pages will\n\t// be flushed into disk ideally. But during DropWrites, device will\n\t// ignore all the data and return successfully. It can be used to\n\t// simulate data-loss after power failure.\n\t//\n\t// REF: https://docs.kernel.org/admin-guide/device-mapper/dm-flakey.html.\n\ttable := fmt.Sprintf(\"0 %d flakey %s 0 0 %d 1 drop_writes\",\n\t\tf.imgSize, f.loopDevice, int(o.interval.Seconds()))\n\n\treturn reloadFlakeyDevice(f.flakeyDevice, o.syncFS, table)\n}\n\n// ErrorWrites drops all write I/O and returns error.\nfunc (f *flakey) ErrorWrites(opts ...FeatOpt) error {\n\tvar o = defaultFeatCfg\n\tfor _, opt := range opts {\n\t\topt(&o)\n\t}\n\n\t// NOTE: Table parameters\n\t//\n\t// 0 imgSize flakey <dev path> <offset> <up interval> <down interval> [<num_features> [<feature arguments>]]\n\t//\n\t// Mandatory parameters:\n\t//\n\t//  <dev path>: Full pathname to the underlying block-device, or a \"major:minor\" device-number.\n\t//  <offset>: Starting sector within the device.\n\t//  <up interval>: Number of seconds device is available.\n\t//  <down interval>: Number of seconds device returns errors.\n\t//\n\t// Optional:\n\t//\n\t// <num_features>: How many arguments (length of <feature_arguments>)\n\t//\n\t// For ErrorWrites,\n\t//\n\t// num_features: 1 (there is only one argument)\n\t// feature_arguments: error_writes\n\t//\n\t// The Device will drop all the writes into disk in `interval` seconds\n\t// and return failure to caller. Read I/O is handled correctly.\n\t//\n\t// REF: https://docs.kernel.org/admin-guide/device-mapper/dm-flakey.html.\n\ttable := fmt.Sprintf(\"0 %d flakey %s 0 0 %d 1 error_writes\",\n\t\tf.imgSize, f.loopDevice, int(o.interval.Seconds()))\n\n\treturn reloadFlakeyDevice(f.flakeyDevice, o.syncFS, table)\n}\n\n// Teardown releases the flakey device.\nfunc (f *flakey) Teardown() error {\n\t// FIXME(XXX): Even though we umount device successfully, it's still\n\t// possible to run into `Device or resource busy` issue. It's easy to\n\t// reproduce it in slow storage or 2-4 cores ARM64 host with xfs. We\n\t// should retry it to fix transisent issue.\n\tvar derr error\n\tfor i := 0; i < 10; i++ {\n\t\tderr = deleteFlakeyDevice(f.flakeyDevice)\n\t\tif derr != nil {\n\t\t\tif strings.Contains(derr.Error(), \"Device or resource busy\") {\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif strings.Contains(derr.Error(), \"No such device or address\") {\n\t\t\t\tderr = nil\n\t\t\t}\n\t\t}\n\t\tbreak\n\t}\n\tif derr != nil {\n\t\treturn derr\n\t}\n\n\tif err := detachLoopDevice(f.loopDevice); err != nil {\n\t\tif !errors.Is(err, unix.ENXIO) {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn os.RemoveAll(f.imgPath)\n}\n\n// createEmptyFSImage creates empty filesystem on dataStorePath folder with\n// default size - 10 GiB.\nfunc createEmptyFSImage(imgPath string, fsType FSType, mkfsOpt string) error {\n\tif err := validateFSType(fsType); err != nil {\n\t\treturn err\n\t}\n\n\tmkfs, err := exec.LookPath(fmt.Sprintf(\"mkfs.%s\", fsType))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to ensure mkfs.%s: %w\", fsType, err)\n\t}\n\n\tif _, err := os.Stat(imgPath); err == nil {\n\t\treturn fmt.Errorf(\"failed to create image because %s already exists\", imgPath)\n\t}\n\n\tif err := os.MkdirAll(path.Dir(imgPath), 0600); err != nil {\n\t\treturn fmt.Errorf(\"failed to ensure parent directory %s: %w\", path.Dir(imgPath), err)\n\t}\n\n\tf, err := os.Create(imgPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create image %s: %w\", imgPath, err)\n\t}\n\n\tif err = func() error {\n\t\tdefer f.Close()\n\n\t\treturn f.Truncate(defaultImgSize)\n\t}(); err != nil {\n\t\treturn fmt.Errorf(\"failed to truncate image %s with %v bytes: %w\",\n\t\t\timgPath, defaultImgSize, err)\n\t}\n\n\targs := []string{imgPath}\n\tif mkfsOpt != \"\" {\n\t\tsplitArgs := strings.Split(mkfsOpt, \" \")\n\t\targs = append(splitArgs, imgPath)\n\t}\n\n\toutput, err := exec.Command(mkfs, args...).CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to mkfs on %s (%s %v) (out: %s): %w\",\n\t\t\timgPath, mkfs, args, string(output), err)\n\t}\n\treturn nil\n}\n\n// validateFSType validates the fs type input.\nfunc validateFSType(fsType FSType) error {\n\tswitch fsType {\n\tcase FSTypeEXT4, FSTypeXFS:\n\t\treturn nil\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported filesystem %s\", fsType)\n\t}\n}\n"
  },
  {
    "path": "tests/dmflakey/dmflakey_test.go",
    "content": "//go:build linux\n\npackage dmflakey\n\nimport (\n\t\"errors\"\n\t\"flag\"\n\t\"fmt\"\n\t\"os\"\n\t\"os/exec\"\n\t\"path/filepath\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\t\"golang.org/x/sys/unix\"\n\n\ttestutils \"go.etcd.io/bbolt/tests/utils\"\n)\n\nfunc TestMain(m *testing.M) {\n\tflag.Parse()\n\ttestutils.RequiresRoot()\n\tos.Exit(m.Run())\n}\n\nfunc TestBasic(t *testing.T) {\n\tfor _, fsType := range []FSType{FSTypeEXT4, FSTypeXFS} {\n\t\tt.Run(string(fsType), func(t *testing.T) {\n\t\t\ttmpDir := t.TempDir()\n\n\t\t\tflakey, err := InitFlakey(\"go-dmflakey\", tmpDir, fsType, \"\")\n\t\t\trequire.NoError(t, err, \"init flakey\")\n\t\t\tdefer func() {\n\t\t\t\tassert.NoError(t, flakey.Teardown())\n\t\t\t}()\n\n\t\t\ttarget := filepath.Join(tmpDir, \"root\")\n\t\t\trequire.NoError(t, os.MkdirAll(target, 0600))\n\n\t\t\trequire.NoError(t, mount(target, flakey.DevicePath(), \"\"))\n\t\t\tdefer func() {\n\t\t\t\tassert.NoError(t, unmount(target))\n\t\t\t}()\n\n\t\t\tfile := filepath.Join(target, \"test\")\n\t\t\tassert.NoError(t, writeFile(file, []byte(\"hello, world\"), 0600, true))\n\n\t\t\tassert.NoError(t, unmount(target))\n\n\t\t\tassert.NoError(t, flakey.Teardown())\n\t\t})\n\t}\n}\n\nfunc TestDropWritesExt4(t *testing.T) {\n\tflakey, root := initFlakey(t, FSTypeEXT4)\n\n\t// commit=1000 is to delay commit triggered by writeback thread\n\trequire.NoError(t, mount(root, flakey.DevicePath(), \"commit=1000\"))\n\n\t// ensure testdir/f1 is synced.\n\ttarget := filepath.Join(root, \"testdir\")\n\trequire.NoError(t, os.MkdirAll(target, 0600))\n\n\tf1 := filepath.Join(target, \"f1\")\n\tassert.NoError(t, writeFile(f1, []byte(\"hello, world from f1\"), 0600, false))\n\trequire.NoError(t, syncfs(f1))\n\n\t// testdir/f2 is created but without fsync\n\tf2 := filepath.Join(target, \"f2\")\n\tassert.NoError(t, writeFile(f2, []byte(\"hello, world from f2\"), 0600, false))\n\n\t// simulate power failure\n\tassert.NoError(t, flakey.DropWrites())\n\tassert.NoError(t, unmount(root))\n\tassert.NoError(t, flakey.AllowWrites())\n\trequire.NoError(t, mount(root, flakey.DevicePath(), \"\"))\n\n\tdata, err := os.ReadFile(f1)\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"hello, world from f1\", string(data))\n\n\t_, err = os.ReadFile(f2)\n\tassert.True(t, errors.Is(err, os.ErrNotExist))\n}\n\nfunc TestErrorWritesExt4(t *testing.T) {\n\tflakey, root := initFlakey(t, FSTypeEXT4)\n\n\t// commit=1000 is to delay commit triggered by writeback thread\n\trequire.NoError(t, mount(root, flakey.DevicePath(), \"commit=1000\"))\n\n\t// inject IO failure on write\n\tassert.NoError(t, flakey.ErrorWrites())\n\n\tf1 := filepath.Join(root, \"f1\")\n\terr := writeFile(f1, []byte(\"hello, world during failpoint\"), 0600, true)\n\tassert.ErrorContains(t, err, \"input/output error\")\n\n\t// resume\n\tassert.NoError(t, flakey.AllowWrites())\n\terr = writeFile(f1, []byte(\"hello, world\"), 0600, true)\n\tassert.NoError(t, err)\n\n\tassert.NoError(t, unmount(root))\n\trequire.NoError(t, mount(root, flakey.DevicePath(), \"\"))\n\n\tdata, err := os.ReadFile(f1)\n\tassert.NoError(t, err)\n\tassert.Equal(t, \"hello, world\", string(data))\n}\n\nfunc initFlakey(t *testing.T, fsType FSType) (_ Flakey, root string) {\n\ttmpDir := t.TempDir()\n\n\ttarget := filepath.Join(tmpDir, \"root\")\n\trequire.NoError(t, os.MkdirAll(target, 0600))\n\n\tflakey, err := InitFlakey(\"go-dmflakey\", tmpDir, fsType, \"\")\n\trequire.NoError(t, err, \"init flakey\")\n\n\tt.Cleanup(func() {\n\t\tassert.NoError(t, unmount(target))\n\t\tassert.NoError(t, flakey.Teardown())\n\t})\n\treturn flakey, target\n}\n\nfunc writeFile(name string, data []byte, perm os.FileMode, sync bool) error {\n\tf, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer f.Close()\n\n\tif _, err = f.Write(data); err != nil {\n\t\treturn err\n\t}\n\n\tif sync {\n\t\treturn f.Sync()\n\t}\n\treturn nil\n}\n\nfunc syncfs(file string) error {\n\tf, err := os.Open(file)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to open %s: %w\", file, err)\n\t}\n\tdefer f.Close()\n\n\t_, _, errno := unix.Syscall(unix.SYS_SYNCFS, uintptr(f.Fd()), 0, 0)\n\tif errno != 0 {\n\t\treturn errno\n\t}\n\treturn nil\n}\n\nfunc mount(target string, devPath string, opt string) error {\n\targs := []string{\"-o\", opt, devPath, target}\n\n\toutput, err := exec.Command(\"mount\", args...).CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to mount (args: %v) (out: %s): %w\",\n\t\t\targs, string(output), err)\n\t}\n\treturn nil\n}\n\nfunc unmount(target string) error {\n\tfor i := 0; i < 50; i++ {\n\t\tif err := unix.Unmount(target, 0); err != nil {\n\t\t\tswitch err {\n\t\t\tcase unix.EBUSY:\n\t\t\t\ttime.Sleep(500 * time.Millisecond)\n\t\t\t\tcontinue\n\t\t\tcase unix.EINVAL:\n\t\t\tdefault:\n\t\t\t\treturn fmt.Errorf(\"failed to umount %s: %w\", target, err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}\n\treturn unix.EBUSY\n}\n"
  },
  {
    "path": "tests/dmflakey/dmsetup.go",
    "content": "//go:build linux\n\npackage dmflakey\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"os/exec\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"golang.org/x/sys/unix\"\n)\n\n// newFlakeyDevice creates flakey device.\n//\n// REF: https://docs.kernel.org/admin-guide/device-mapper/dm-flakey.html\nfunc newFlakeyDevice(flakeyDevice, loopDevice string, interval time.Duration) error {\n\tloopSize, err := getBlkSize(loopDevice)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get the size of the loop device %s: %w\", loopDevice, err)\n\t}\n\n\t// The flakey device will be available in interval.Seconds().\n\ttable := fmt.Sprintf(\"0 %d flakey %s 0 %d 0\",\n\t\tloopSize, loopDevice, int(interval.Seconds()))\n\n\targs := []string{\"create\", flakeyDevice, \"--table\", table}\n\n\toutput, err := exec.Command(\"dmsetup\", args...).CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create flakey device %s with table %s (out: %s): %w\",\n\t\t\tflakeyDevice, table, string(output), err)\n\t}\n\treturn nil\n}\n\n// reloadFlakeyDevice reloads the flakey device with feature table.\nfunc reloadFlakeyDevice(flakeyDevice string, syncFS bool, table string) (retErr error) {\n\targs := []string{\"suspend\", \"--nolockfs\", flakeyDevice}\n\tif syncFS {\n\t\targs[1] = flakeyDevice\n\t\targs = args[:len(args)-1]\n\t}\n\n\toutput, err := exec.Command(\"dmsetup\", args...).CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to suspend flakey device %s (out: %s): %w\",\n\t\t\tflakeyDevice, string(output), err)\n\t}\n\n\tdefer func() {\n\t\toutput, derr := exec.Command(\"dmsetup\", \"resume\", flakeyDevice).CombinedOutput()\n\t\tif derr != nil {\n\t\t\tderr = fmt.Errorf(\"failed to resume flakey device %s (out: %s): %w\",\n\t\t\t\tflakeyDevice, string(output), derr)\n\t\t}\n\n\t\tif retErr == nil {\n\t\t\tretErr = derr\n\t\t}\n\t}()\n\n\toutput, err = exec.Command(\"dmsetup\", \"load\", flakeyDevice, \"--table\", table).CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to reload flakey device %s with table (%s) (out: %s): %w\",\n\t\t\tflakeyDevice, table, string(output), err)\n\t}\n\treturn nil\n}\n\n// removeFlakeyDevice removes flakey device.\nfunc deleteFlakeyDevice(flakeyDevice string) error {\n\toutput, err := exec.Command(\"dmsetup\", \"remove\", flakeyDevice).CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to remove flakey device %s (out: %s): %w\",\n\t\t\tflakeyDevice, string(output), err)\n\t}\n\treturn nil\n}\n\n// getBlkSize64 gets device size in bytes (BLKGETSIZE64).\n//\n// REF: https://man7.org/linux/man-pages/man8/blockdev.8.html\nfunc getBlkSize64(device string) (int64, error) {\n\tdeviceFd, err := os.Open(device)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to open device %s: %w\", device, err)\n\t}\n\tdefer deviceFd.Close()\n\n\tvar size int64\n\tif _, _, err := unix.Syscall(unix.SYS_IOCTL, deviceFd.Fd(), unix.BLKGETSIZE64, uintptr(unsafe.Pointer(&size))); err != 0 {\n\t\treturn 0, fmt.Errorf(\"failed to get block size: %w\", err)\n\t}\n\treturn size, nil\n}\n\n// getBlkSize gets size in 512-byte sectors (BLKGETSIZE64 / 512).\n//\n// REF: https://man7.org/linux/man-pages/man8/blockdev.8.html\nfunc getBlkSize(device string) (int64, error) {\n\tsize, err := getBlkSize64(device)\n\treturn size / 512, err\n}\n"
  },
  {
    "path": "tests/dmflakey/loopback.go",
    "content": "//go:build linux\n\npackage dmflakey\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n\t\"time\"\n\n\t\"golang.org/x/sys/unix\"\n)\n\nconst (\n\tloopControlDevice = \"/dev/loop-control\"\n\tloopDevicePattern = \"/dev/loop%d\"\n\n\tmaxRetryToAttach = 50\n)\n\n// attachToLoopDevice associates free loop device with backing file.\n//\n// There might have race condition. It needs to retry when it runs into EBUSY.\n//\n// REF: https://man7.org/linux/man-pages/man4/loop.4.html\nfunc attachToLoopDevice(backingFile string) (string, error) {\n\tbackingFd, err := os.OpenFile(backingFile, os.O_RDWR, 0)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to open loop device's backing file %s: %w\",\n\t\t\tbackingFile, err)\n\t}\n\tdefer backingFd.Close()\n\n\tfor i := 0; i < maxRetryToAttach; i++ {\n\t\tloop, err := getFreeLoopDevice()\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to get free loop device: %w\", err)\n\t\t}\n\n\t\terr = func() error {\n\t\t\tloopFd, err := os.OpenFile(loop, os.O_RDWR, 0)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdefer loopFd.Close()\n\n\t\t\treturn unix.IoctlSetInt(int(loopFd.Fd()),\n\t\t\t\tunix.LOOP_SET_FD, int(backingFd.Fd()))\n\t\t}()\n\t\tif err != nil {\n\t\t\tif errors.Is(err, unix.EBUSY) {\n\t\t\t\ttime.Sleep(500 * time.Millisecond)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\treturn \"\", err\n\t\t}\n\t\treturn loop, nil\n\t}\n\treturn \"\", fmt.Errorf(\"failed to associate free loop device with backing file %s after retry %v\",\n\t\tbackingFile, maxRetryToAttach)\n}\n\n// detachLoopDevice disassociates the loop device from any backing file.\n//\n// REF: https://man7.org/linux/man-pages/man4/loop.4.html\nfunc detachLoopDevice(loopDevice string) error {\n\tloopFd, err := os.Open(loopDevice)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to open loop %s: %w\", loopDevice, err)\n\t}\n\tdefer loopFd.Close()\n\n\treturn unix.IoctlSetInt(int(loopFd.Fd()), unix.LOOP_CLR_FD, 0)\n}\n\n// getFreeLoopDevice allocates or finds a free loop device for use.\n//\n// REF: https://man7.org/linux/man-pages/man4/loop.4.html\nfunc getFreeLoopDevice() (string, error) {\n\tcontrol, err := os.OpenFile(loopControlDevice, os.O_RDWR, 0)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to open %s: %w\", loopControlDevice, err)\n\t}\n\n\tidx, err := unix.IoctlRetInt(int(control.Fd()), unix.LOOP_CTL_GET_FREE)\n\tcontrol.Close()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get free loop device number: %w\", err)\n\t}\n\treturn fmt.Sprintf(loopDevicePattern, idx), nil\n}\n"
  },
  {
    "path": "tests/failpoint/db_failpoint_test.go",
    "content": "package failpoint\n\nimport (\n\tcrand \"crypto/rand\"\n\t\"fmt\"\n\t\"path/filepath\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n\tgofail \"go.etcd.io/gofail/runtime\"\n)\n\nfunc TestFailpoint_MapFail(t *testing.T) {\n\terr := gofail.Enable(\"mapError\", `return(\"map somehow failed\")`)\n\trequire.NoError(t, err)\n\tdefer func() {\n\t\terr = gofail.Disable(\"mapError\")\n\t\trequire.NoError(t, err)\n\t}()\n\n\tf := filepath.Join(t.TempDir(), \"db\")\n\t_, err = bolt.Open(f, 0600, nil)\n\trequire.Error(t, err)\n\trequire.ErrorContains(t, err, \"map somehow failed\")\n}\n\n// ensures when munmap fails, the flock is unlocked\nfunc TestFailpoint_UnmapFail_DbClose(t *testing.T) {\n\t//unmap error on db close\n\t//we need to open the db first, and then enable the error.\n\t//otherwise the db cannot be opened.\n\tf := filepath.Join(t.TempDir(), \"db\")\n\n\terr := gofail.Enable(\"unmapError\", `return(\"unmap somehow failed\")`)\n\trequire.NoError(t, err)\n\t_, err = bolt.Open(f, 0600, nil)\n\trequire.Error(t, err)\n\trequire.ErrorContains(t, err, \"unmap somehow failed\")\n\t//disable the error, and try to reopen the db\n\terr = gofail.Disable(\"unmapError\")\n\trequire.NoError(t, err)\n\n\tdb, err := bolt.Open(f, 0600, &bolt.Options{Timeout: 30 * time.Second})\n\trequire.NoError(t, err)\n\terr = db.Close()\n\trequire.NoError(t, err)\n}\n\nfunc TestFailpoint_mLockFail(t *testing.T) {\n\terr := gofail.Enable(\"mlockError\", `return(\"mlock somehow failed\")`)\n\trequire.NoError(t, err)\n\n\tf := filepath.Join(t.TempDir(), \"db\")\n\t_, err = bolt.Open(f, 0600, &bolt.Options{Mlock: true})\n\trequire.Error(t, err)\n\trequire.ErrorContains(t, err, \"mlock somehow failed\")\n\n\t// It should work after disabling the failpoint.\n\terr = gofail.Disable(\"mlockError\")\n\trequire.NoError(t, err)\n\n\t_, err = bolt.Open(f, 0600, &bolt.Options{Mlock: true})\n\trequire.NoError(t, err)\n}\n\nfunc TestFailpoint_mLockFail_When_remap(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tdb.Mlock = true\n\n\terr := gofail.Enable(\"mlockError\", `return(\"mlock somehow failed in allocate\")`)\n\trequire.NoError(t, err)\n\n\terr = db.Fill([]byte(\"data\"), 1, 10000,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t)\n\n\trequire.Error(t, err)\n\trequire.ErrorContains(t, err, \"mlock somehow failed in allocate\")\n\n\t// It should work after disabling the failpoint.\n\terr = gofail.Disable(\"mlockError\")\n\trequire.NoError(t, err)\n\tdb.MustClose()\n\tdb.MustReopen()\n\n\terr = db.Fill([]byte(\"data\"), 1, 10000,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t)\n\n\trequire.NoError(t, err)\n}\n\nfunc TestFailpoint_ResizeFileFail(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\terr := gofail.Enable(\"resizeFileError\", `return(\"resizeFile somehow failed\")`)\n\trequire.NoError(t, err)\n\n\terr = db.Fill([]byte(\"data\"), 1, 10000,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t)\n\n\trequire.Error(t, err)\n\trequire.ErrorContains(t, err, \"resizeFile somehow failed\")\n\n\t// It should work after disabling the failpoint.\n\terr = gofail.Disable(\"resizeFileError\")\n\trequire.NoError(t, err)\n\tdb.MustClose()\n\tdb.MustReopen()\n\n\terr = db.Fill([]byte(\"data\"), 1, 10000,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t)\n\n\trequire.NoError(t, err)\n}\n\nfunc TestFailpoint_LackOfDiskSpace(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\terr := gofail.Enable(\"lackOfDiskSpace\", `return(\"grow somehow failed\")`)\n\trequire.NoError(t, err)\n\n\ttx, err := db.Begin(true)\n\trequire.NoError(t, err)\n\n\terr = tx.Commit()\n\trequire.Error(t, err)\n\trequire.ErrorContains(t, err, \"grow somehow failed\")\n\n\terr = tx.Rollback()\n\trequire.Error(t, err)\n\trequire.ErrorIs(t, err, errors.ErrTxClosed)\n\n\t// It should work after disabling the failpoint.\n\terr = gofail.Disable(\"lackOfDiskSpace\")\n\trequire.NoError(t, err)\n\n\ttx, err = db.Begin(true)\n\trequire.NoError(t, err)\n\n\terr = tx.Commit()\n\trequire.NoError(t, err)\n\n\terr = tx.Rollback()\n\trequire.Error(t, err)\n\trequire.ErrorIs(t, err, errors.ErrTxClosed)\n}\n\n// TestIssue72 reproduces issue 72.\n//\n// When bbolt is processing a `Put` invocation, the key might be concurrently\n// updated by the application which calls the `Put` API (although it shouldn't).\n// It might lead to a situation that bbolt use an old key to find a proper\n// position to insert the key/value pair, but actually inserts a new key.\n// Eventually it might break the rule that all keys should be sorted. In a\n// worse case, it might cause page elements to point to already freed pages.\n//\n// REF: https://github.com/etcd-io/bbolt/issues/72\nfunc TestIssue72(t *testing.T) {\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})\n\n\tbucketName := []byte(t.Name())\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\t_, txerr := tx.CreateBucket(bucketName)\n\t\treturn txerr\n\t})\n\trequire.NoError(t, err)\n\n\t// The layout is like:\n\t//\n\t//         +--+--+--+\n\t//  +------+1 |3 |10+---+\n\t//  |      +-++--+--+   |\n\t//  |         |         |\n\t//  |         |         |\n\t// +v-+--+   +v-+--+  +-v+--+--+\n\t// |1 |2 |   |3 |4 |  |10|11|12|\n\t// +--+--+   +--+--+  +--+--+--+\n\t//\n\terr = db.Update(func(tx *bolt.Tx) error {\n\t\tbk := tx.Bucket(bucketName)\n\n\t\tfor _, id := range []int{1, 2, 3, 4, 10, 11, 12} {\n\t\t\tif txerr := bk.Put(idToBytes(id), make([]byte, 1000)); txerr != nil {\n\t\t\t\treturn txerr\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\n\trequire.NoError(t, gofail.Enable(\"beforeBucketPut\", `sleep(5000)`))\n\n\t//         +--+--+--+\n\t//  +------+1 |3 |1 +---+\n\t//  |      +-++--+--+   |\n\t//  |         |         |\n\t//  |         |         |\n\t// +v-+--+   +v-+--+  +-v+--+--+--+\n\t// |1 |2 |   |3 |4 |  |1 |10|11|12|\n\t// +--+--+   +--+--+  +--+--+--+--+\n\t//\n\tkey := idToBytes(13)\n\tupdatedKey := idToBytes(1)\n\terr = db.Update(func(tx *bolt.Tx) error {\n\t\tbk := tx.Bucket(bucketName)\n\n\t\tgo func() {\n\t\t\ttime.Sleep(3 * time.Second)\n\t\t\tcopy(key, updatedKey)\n\t\t}()\n\t\treturn bk.Put(key, make([]byte, 100))\n\t})\n\trequire.NoError(t, err)\n\n\trequire.NoError(t, gofail.Disable(\"beforeBucketPut\"))\n\n\t// bbolt inserts 100 into last branch page. Since there are two `1`\n\t// keys in branch, spill operation will update first `1` pointer and\n\t// then last one won't be updated and continues to point to freed page.\n\t//\n\t//\n\t//                  +--+--+--+\n\t//  +---------------+1 |3 |1 +---------+\n\t//  |               +--++-+--+         |\n\t//  |                   |              |\n\t//  |                   |              |\n\t//  |        +--+--+   +v-+--+   +-----v-----+\n\t//  |        |1 |2 |   |3 |4 |   |freed page |\n\t//  |        +--+--+   +--+--+   +-----------+\n\t//  |\n\t// +v-+--+--+--+---+\n\t// |1 |10|11|12|100|\n\t// +--+--+--+--+---+\n\terr = db.Update(func(tx *bolt.Tx) error {\n\t\treturn tx.Bucket(bucketName).Put(idToBytes(100), make([]byte, 100))\n\t})\n\trequire.NoError(t, err)\n\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tt.Logf(\"panic info:\\n %v\", r)\n\t\t}\n\t}()\n\n\t// Add more keys to ensure branch node to spill.\n\terr = db.Update(func(tx *bolt.Tx) error {\n\t\tbk := tx.Bucket(bucketName)\n\n\t\tfor _, id := range []int{101, 102, 103, 104, 105} {\n\t\t\tif txerr := bk.Put(idToBytes(id), make([]byte, 1000)); txerr != nil {\n\t\t\t\treturn txerr\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n}\n\nfunc TestTx_Rollback_Freelist(t *testing.T) {\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})\n\n\tbucketName := []byte(\"data\")\n\n\tt.Log(\"Populate some data to have at least 5 leaf pages.\")\n\tvar keys []string\n\terr := db.Update(func(tx *bolt.Tx) error {\n\t\tb, terr := tx.CreateBucket(bucketName)\n\t\tif terr != nil {\n\t\t\treturn terr\n\t\t}\n\t\tfor i := 0; i <= 10; i++ {\n\t\t\tk := fmt.Sprintf(\"t1_k%02d\", i)\n\t\t\tkeys = append(keys, k)\n\n\t\t\tv := make([]byte, 1500)\n\t\t\tif _, terr := crand.Read(v); terr != nil {\n\t\t\t\treturn terr\n\t\t\t}\n\n\t\t\tif terr := b.Put([]byte(k), v); terr != nil {\n\t\t\t\treturn terr\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\n\tt.Log(\"Remove some keys to have at least 3 more free pages.\")\n\terr = db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket(bucketName)\n\t\tfor i := 0; i < 6; i++ {\n\t\t\tif terr := b.Delete([]byte(keys[i])); terr != nil {\n\t\t\t\treturn terr\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\n\tt.Log(\"Close and then reopen the db to release all pending free pages.\")\n\tdb.MustClose()\n\tdb.MustReopen()\n\n\tt.Log(\"Enable the `beforeWriteMetaError` failpoint.\")\n\trequire.NoError(t, gofail.Enable(\"beforeWriteMetaError\", `return(\"writeMeta somehow failed\")`))\n\tdefer func() {\n\t\tt.Log(\"Disable the `beforeWriteMetaError` failpoint.\")\n\t\trequire.NoError(t, gofail.Disable(\"beforeWriteMetaError\"))\n\t}()\n\n\tbeforeFreelistPgids, err := readFreelistPageIds(db.Path())\n\trequire.NoError(t, err)\n\trequire.Greater(t, len(beforeFreelistPgids), 0)\n\n\tt.Log(\"Simulate TXN rollback\")\n\terr = db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket(bucketName)\n\t\tfor i := 6; i < len(keys); i++ {\n\t\t\tv := make([]byte, 1500)\n\t\t\tif _, terr := crand.Read(v); terr != nil {\n\t\t\t\treturn terr\n\t\t\t}\n\t\t\t// update the keys\n\t\t\tif terr := b.Put([]byte(keys[i]), v); terr != nil {\n\t\t\t\treturn terr\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\trequire.Error(t, err)\n\n\tafterFreelistPgids, err := readFreelistPageIds(db.Path())\n\trequire.NoError(t, err)\n\n\trequire.Equal(t, beforeFreelistPgids, afterFreelistPgids)\n}\n\nfunc idToBytes(id int) []byte {\n\treturn []byte(fmt.Sprintf(\"%010d\", id))\n}\n\nfunc readFreelistPageIds(path string) ([]common.Pgid, error) {\n\tm, _, err := guts_cli.GetActiveMetaPage(path)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tp, _, err := guts_cli.ReadPage(path, uint64(m.Freelist()))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn p.FreelistPageIds(), nil\n}\n"
  },
  {
    "path": "tests/robustness/main_test.go",
    "content": "//go:build linux\n\npackage robustness\n\nimport (\n\t\"flag\"\n\t\"os\"\n\t\"testing\"\n\n\ttestutils \"go.etcd.io/bbolt/tests/utils\"\n)\n\nfunc TestMain(m *testing.M) {\n\tflag.Parse()\n\ttestutils.RequiresRoot()\n\tos.Exit(m.Run())\n}\n"
  },
  {
    "path": "tests/robustness/powerfailure_test.go",
    "content": "//go:build linux\n\npackage robustness\n\nimport (\n\t\"bytes\"\n\t\"crypto/rand\"\n\t\"fmt\"\n\t\"io\"\n\t\"math\"\n\t\"math/big\"\n\t\"net/http\"\n\t\"net/url\"\n\t\"os\"\n\t\"os/exec\"\n\t\"path\"\n\t\"path/filepath\"\n\t\"strings\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\t\"golang.org/x/sys/unix\"\n\n\t\"go.etcd.io/bbolt/tests/dmflakey\"\n)\n\nvar panicFailpoints = []string{\n\t\"beforeSyncDataPages\",\n\t\"beforeSyncMetaPage\",\n\t\"lackOfDiskSpace\",\n\t\"mapError\",\n\t\"resizeFileError\",\n\t\"unmapError\",\n}\n\n// TestRestartFromPowerFailureExt4 is to test data after unexpected power failure on ext4.\nfunc TestRestartFromPowerFailureExt4(t *testing.T) {\n\tfor _, tc := range []struct {\n\t\tname         string\n\t\tdu           time.Duration\n\t\tfsMountOpt   string\n\t\tuseFailpoint bool\n\t}{\n\t\t{\n\t\t\tname:         \"fp_ext4_commit5s\",\n\t\t\tdu:           5 * time.Second,\n\t\t\tfsMountOpt:   \"commit=5\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"fp_ext4_commit1s\",\n\t\t\tdu:           10 * time.Second,\n\t\t\tfsMountOpt:   \"commit=1\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"fp_ext4_commit1000s\",\n\t\t\tdu:           10 * time.Second,\n\t\t\tfsMountOpt:   \"commit=1000\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:       \"kill_ext4_commit5s\",\n\t\t\tdu:         5 * time.Second,\n\t\t\tfsMountOpt: \"commit=5\",\n\t\t},\n\t\t{\n\t\t\tname:       \"kill_ext4_commit1s\",\n\t\t\tdu:         10 * time.Second,\n\t\t\tfsMountOpt: \"commit=1\",\n\t\t},\n\t\t{\n\t\t\tname:       \"kill_ext4_commit1000s\",\n\t\t\tdu:         10 * time.Second,\n\t\t\tfsMountOpt: \"commit=1000\",\n\t\t},\n\t} {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tdoPowerFailure(t, tc.du, dmflakey.FSTypeEXT4, \"\", tc.fsMountOpt, tc.useFailpoint)\n\t\t})\n\t}\n}\n\nfunc TestRestartFromPowerFailureXFS(t *testing.T) {\n\tfor _, tc := range []struct {\n\t\tname         string\n\t\tmkfsOpt      string\n\t\tfsMountOpt   string\n\t\tuseFailpoint bool\n\t}{\n\t\t{\n\t\t\tname:         \"xfs_no_opts\",\n\t\t\tmkfsOpt:      \"\",\n\t\t\tfsMountOpt:   \"\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"lazy-log\",\n\t\t\tmkfsOpt:      \"-l lazy-count=1\",\n\t\t\tfsMountOpt:   \"\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"odd-allocsize\",\n\t\t\tmkfsOpt:      \"\",\n\t\t\tfsMountOpt:   \"allocsize=\" + fmt.Sprintf(\"%d\", 4096*5),\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"nolargeio\",\n\t\t\tmkfsOpt:      \"\",\n\t\t\tfsMountOpt:   \"nolargeio\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:         \"odd-alignment\",\n\t\t\tmkfsOpt:      \"-d sunit=1024,swidth=1024\",\n\t\t\tfsMountOpt:   \"noalign\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t\t{\n\t\t\tname:    \"openshift-sno-options\",\n\t\t\tmkfsOpt: \"-m bigtime=1,finobt=1,rmapbt=0,reflink=1 -i sparse=1 -l lazy-count=1\",\n\t\t\t// openshift also supplies seclabel,relatime,prjquota on RHEL, but that's not supported on our CI\n\t\t\t// prjquota is only unsupported on our ARM runners.\n\t\t\t// You can find more information in either the man page with `man xfs` or `man mkfs.xfs`.\n\t\t\t// Also refer to https://man7.org/linux/man-pages/man8/mkfs.xfs.8.html.\n\t\t\tfsMountOpt:   \"rw,attr2,inode64,logbufs=8,logbsize=32k\",\n\t\t\tuseFailpoint: true,\n\t\t},\n\t} {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tt.Logf(\"mkfs opts: %s\", tc.mkfsOpt)\n\t\t\tt.Logf(\"mount opts: %s\", tc.fsMountOpt)\n\t\t\tdoPowerFailure(t, 5*time.Second, dmflakey.FSTypeXFS, tc.mkfsOpt, tc.fsMountOpt, tc.useFailpoint)\n\t\t})\n\t}\n}\n\nfunc doPowerFailure(t *testing.T, du time.Duration, fsType dmflakey.FSType, mkfsOpt string, fsMountOpt string, useFailpoint bool) {\n\tflakey := initFlakeyDevice(t, strings.ReplaceAll(t.Name(), \"/\", \"_\"), fsType, mkfsOpt, fsMountOpt)\n\troot := flakey.RootFS()\n\n\tdbPath := filepath.Join(root, \"boltdb\")\n\n\targs := []string{\"bbolt\", \"bench\",\n\t\t\"--work\", // keep the database\n\t\t\"--path\", dbPath,\n\t\t\"--count=1000000000\",\n\t\t\"--batch-size=5\", // separate total count into multiple truncation\n\t\t\"--value-size=512\",\n\t}\n\n\tlogPath := filepath.Join(t.TempDir(), fmt.Sprintf(\"%s.log\", t.Name()))\n\trequire.NoError(t, os.MkdirAll(path.Dir(logPath), 0600))\n\n\tlogFd, err := os.Create(logPath)\n\trequire.NoError(t, err)\n\tdefer logFd.Close()\n\n\tfpURL := \"127.0.0.1:12345\"\n\n\tcmd := exec.Command(args[0], args[1:]...)\n\tcmd.Stdout = logFd\n\tcmd.Stderr = logFd\n\tcmd.Env = append(cmd.Env, \"GOFAIL_HTTP=\"+fpURL)\n\tt.Logf(\"start %s\", strings.Join(args, \" \"))\n\trequire.NoError(t, cmd.Start(), \"args: %v\", args)\n\n\terrCh := make(chan error, 1)\n\tgo func() {\n\t\terrCh <- cmd.Wait()\n\t}()\n\n\tdefer func() {\n\t\tif t.Failed() {\n\t\t\tlogData, err := os.ReadFile(logPath)\n\t\t\tassert.NoError(t, err)\n\t\t\tt.Logf(\"dump log:\\n: %s\", string(logData))\n\t\t}\n\t}()\n\n\ttime.Sleep(du)\n\tt.Logf(\"simulate power failure\")\n\n\tif useFailpoint {\n\t\tfpURL = \"http://\" + fpURL\n\t\ttargetFp := panicFailpoints[randomInt(t, math.MaxInt32)%len(panicFailpoints)]\n\t\tt.Logf(\"random pick failpoint: %s\", targetFp)\n\t\tactiveFailpoint(t, fpURL, targetFp, \"panic\")\n\t} else {\n\t\tt.Log(\"kill bbolt\")\n\t\tassert.NoError(t, cmd.Process.Kill())\n\t}\n\n\tselect {\n\tcase <-time.After(10 * time.Second):\n\t\tt.Log(\"bbolt is supposed to be already stopped, but actually not yet; forcibly kill it\")\n\t\tassert.NoError(t, cmd.Process.Kill())\n\tcase err := <-errCh:\n\t\trequire.Error(t, err)\n\t}\n\trequire.NoError(t, flakey.PowerFailure(\"\"))\n\n\tst, err := os.Stat(dbPath)\n\trequire.NoError(t, err)\n\tt.Logf(\"db size: %d\", st.Size())\n\n\tt.Logf(\"verify data\")\n\toutput, err := exec.Command(\"bbolt\", \"check\", dbPath).CombinedOutput()\n\trequire.NoError(t, err, \"bbolt check output: %s\", string(output))\n}\n\n// activeFailpoint actives the failpoint by http.\nfunc activeFailpoint(t *testing.T, targetUrl string, fpName, fpVal string) {\n\tu, err := url.JoinPath(targetUrl, fpName)\n\trequire.NoError(t, err, \"parse url %s\", targetUrl)\n\n\treq, err := http.NewRequest(\"PUT\", u, bytes.NewBuffer([]byte(fpVal)))\n\trequire.NoError(t, err)\n\n\tresp, err := http.DefaultClient.Do(req)\n\trequire.NoError(t, err)\n\tdefer resp.Body.Close()\n\n\tdata, err := io.ReadAll(resp.Body)\n\trequire.NoError(t, err)\n\trequire.Equal(t, 204, resp.StatusCode, \"response body: %s\", string(data))\n}\n\n// FlakeyDevice extends dmflakey.Flakey interface.\ntype FlakeyDevice interface {\n\t// RootFS returns root filesystem.\n\tRootFS() string\n\n\t// PowerFailure simulates power failure with drop all the writes.\n\tPowerFailure(mntOpt string) error\n\n\tdmflakey.Flakey\n}\n\n// initFlakeyDevice returns FlakeyDevice instance with a given filesystem.\nfunc initFlakeyDevice(t *testing.T, name string, fsType dmflakey.FSType, mkfsOpt string, mntOpt string) FlakeyDevice {\n\timgDir := t.TempDir()\n\n\tflakey, err := dmflakey.InitFlakey(name, imgDir, fsType, mkfsOpt)\n\trequire.NoError(t, err, \"init flakey %s\", name)\n\tt.Cleanup(func() {\n\t\tassert.NoError(t, flakey.Teardown())\n\t})\n\n\trootDir := t.TempDir()\n\terr = unix.Mount(flakey.DevicePath(), rootDir, string(fsType), 0, mntOpt)\n\trequire.NoError(t, err, \"init rootfs on %s\", rootDir)\n\n\tt.Cleanup(func() { assert.NoError(t, unmountAll(rootDir)) })\n\n\treturn &flakeyT{\n\t\tFlakey: flakey,\n\n\t\trootDir: rootDir,\n\t\tmntOpt:  mntOpt,\n\t}\n}\n\ntype flakeyT struct {\n\tdmflakey.Flakey\n\n\trootDir string\n\tmntOpt  string\n}\n\n// RootFS returns root filesystem.\nfunc (f *flakeyT) RootFS() string {\n\treturn f.rootDir\n}\n\n// PowerFailure simulates power failure with drop all the writes.\nfunc (f *flakeyT) PowerFailure(mntOpt string) error {\n\tif err := f.DropWrites(); err != nil {\n\t\treturn fmt.Errorf(\"failed to drop_writes: %w\", err)\n\t}\n\n\tif err := unmountAll(f.rootDir); err != nil {\n\t\treturn fmt.Errorf(\"failed to unmount rootfs %s: %w\", f.rootDir, err)\n\t}\n\n\tif mntOpt == \"\" {\n\t\tmntOpt = f.mntOpt\n\t}\n\n\tif err := f.AllowWrites(); err != nil {\n\t\treturn fmt.Errorf(\"failed to allow_writes: %w\", err)\n\t}\n\n\tif err := unix.Mount(f.DevicePath(), f.rootDir, string(f.Filesystem()), 0, mntOpt); err != nil {\n\t\treturn fmt.Errorf(\"failed to mount rootfs %s (%s): %w\", f.rootDir, mntOpt, err)\n\t}\n\treturn nil\n}\n\nfunc unmountAll(target string) error {\n\tfor i := 0; i < 50; i++ {\n\t\tif err := unix.Unmount(target, 0); err != nil {\n\t\t\tswitch err {\n\t\t\tcase unix.EBUSY:\n\t\t\t\ttime.Sleep(500 * time.Millisecond)\n\t\t\t\tcontinue\n\t\t\tcase unix.EINVAL:\n\t\t\t\treturn nil\n\t\t\tdefault:\n\t\t\t\treturn fmt.Errorf(\"failed to umount %s: %w\", target, err)\n\t\t\t}\n\t\t}\n\t\tcontinue\n\t}\n\treturn fmt.Errorf(\"failed to umount %s: %w\", target, unix.EBUSY)\n}\n\nfunc randomInt(t *testing.T, max int) int {\n\tn, err := rand.Int(rand.Reader, big.NewInt(int64(max)))\n\tassert.NoError(t, err)\n\treturn int(n.Int64())\n}\n"
  },
  {
    "path": "tests/utils/helpers.go",
    "content": "package utils\n\nimport (\n\t\"flag\"\n\t\"fmt\"\n\t\"os\"\n)\n\nvar enableRoot bool\n\nfunc init() {\n\tflag.BoolVar(&enableRoot, \"test.root\", false, \"enable tests that require root\")\n}\n\n// RequiresRoot requires root and the test.root flag has been set.\nfunc RequiresRoot() {\n\tif !enableRoot {\n\t\tfmt.Fprintln(os.Stderr, \"Skip tests that require root\")\n\t\tos.Exit(0)\n\t}\n\n\tif os.Getuid() != 0 {\n\t\tfmt.Fprintln(os.Stderr, \"This test must be run as root.\")\n\t\tos.Exit(1)\n\t}\n}\n"
  },
  {
    "path": "tx.go",
    "content": "package bbolt\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"runtime\"\n\t\"sort\"\n\t\"strings\"\n\t\"sync/atomic\"\n\t\"time\"\n\t\"unsafe\"\n\n\tberrors \"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// Tx represents a read-only or read/write transaction on the database.\n// Read-only transactions can be used for retrieving values for keys and creating cursors.\n// Read/write transactions can create and remove buckets and create and remove keys.\n//\n// IMPORTANT: You must commit or rollback transactions when you are done with\n// them. Pages can not be reclaimed by the writer until no more transactions\n// are using them. A long running read transaction can cause the database to\n// quickly grow.\ntype Tx struct {\n\twritable       bool\n\tmanaged        bool\n\tdb             *DB\n\tmeta           *common.Meta\n\troot           Bucket\n\tpages          map[common.Pgid]*common.Page\n\tstats          TxStats\n\tcommitHandlers []func()\n\n\t// WriteFlag specifies the flag for write-related methods like WriteTo().\n\t// Tx opens the database file with the specified flag to copy the data.\n\t//\n\t// By default, the flag is unset, which works well for mostly in-memory\n\t// workloads. For databases that are much larger than available RAM,\n\t// set the flag to syscall.O_DIRECT to avoid trashing the page cache.\n\tWriteFlag int\n}\n\n// init initializes the transaction.\nfunc (tx *Tx) init(db *DB) {\n\ttx.db = db\n\ttx.pages = nil\n\n\t// Copy the meta page since it can be changed by the writer.\n\ttx.meta = &common.Meta{}\n\tdb.meta().Copy(tx.meta)\n\n\t// Copy over the root bucket.\n\ttx.root = newBucket(tx)\n\ttx.root.InBucket = &common.InBucket{}\n\t*tx.root.InBucket = *(tx.meta.RootBucket())\n\n\t// Increment the transaction id and add a page cache for writable transactions.\n\tif tx.writable {\n\t\ttx.pages = make(map[common.Pgid]*common.Page)\n\t\ttx.meta.IncTxid()\n\t}\n}\n\n// ID returns the transaction id.\nfunc (tx *Tx) ID() int {\n\tif tx == nil || tx.meta == nil {\n\t\treturn -1\n\t}\n\treturn int(tx.meta.Txid())\n}\n\n// DB returns a reference to the database that created the transaction.\nfunc (tx *Tx) DB() *DB {\n\treturn tx.db\n}\n\n// Size returns current database size in bytes as seen by this transaction.\nfunc (tx *Tx) Size() int64 {\n\treturn int64(tx.meta.Pgid()) * int64(tx.db.pageSize)\n}\n\n// Writable returns whether the transaction can perform write operations.\nfunc (tx *Tx) Writable() bool {\n\treturn tx.writable\n}\n\n// Cursor creates a cursor associated with the root bucket.\n// All items in the cursor will return a nil value because all root bucket keys point to buckets.\n// The cursor is only valid as long as the transaction is open.\n// Do not use a cursor after the transaction is closed.\nfunc (tx *Tx) Cursor() *Cursor {\n\treturn tx.root.Cursor()\n}\n\n// Stats retrieves a copy of the current transaction statistics.\nfunc (tx *Tx) Stats() TxStats {\n\treturn tx.stats\n}\n\n// Inspect returns the structure of the database.\nfunc (tx *Tx) Inspect() BucketStructure {\n\treturn tx.root.Inspect()\n}\n\n// Bucket retrieves a bucket by name.\n// Returns nil if the bucket does not exist.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (tx *Tx) Bucket(name []byte) *Bucket {\n\treturn tx.root.Bucket(name)\n}\n\n// CreateBucket creates a new bucket.\n// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (tx *Tx) CreateBucket(name []byte) (*Bucket, error) {\n\treturn tx.root.CreateBucket(name)\n}\n\n// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.\n// Returns an error if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {\n\treturn tx.root.CreateBucketIfNotExists(name)\n}\n\n// DeleteBucket deletes a bucket.\n// Returns an error if the bucket cannot be found or if the key represents a non-bucket value.\nfunc (tx *Tx) DeleteBucket(name []byte) error {\n\treturn tx.root.DeleteBucket(name)\n}\n\n// MoveBucket moves a sub-bucket from the source bucket to the destination bucket.\n// Returns an error if\n//  1. the sub-bucket cannot be found in the source bucket;\n//  2. or the key already exists in the destination bucket;\n//  3. the key represents a non-bucket value.\n//\n// If src is nil, it means moving a top level bucket into the target bucket.\n// If dst is nil, it means converting the child bucket into a top level bucket.\nfunc (tx *Tx) MoveBucket(child []byte, src *Bucket, dst *Bucket) error {\n\tif src == nil {\n\t\tsrc = &tx.root\n\t}\n\tif dst == nil {\n\t\tdst = &tx.root\n\t}\n\treturn src.MoveBucket(child, dst)\n}\n\n// ForEach executes a function for each bucket in the root.\n// If the provided function returns an error then the iteration is stopped and\n// the error is returned to the caller.\nfunc (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error {\n\treturn tx.root.ForEach(func(k, v []byte) error {\n\t\treturn fn(k, tx.root.Bucket(k))\n\t})\n}\n\n// OnCommit adds a handler function to be executed after the transaction successfully commits.\nfunc (tx *Tx) OnCommit(fn func()) {\n\ttx.commitHandlers = append(tx.commitHandlers, fn)\n}\n\n// Commit writes all changes to disk, updates the meta page and closes the transaction.\n// Returns an error if a disk write error occurs, or if Commit is\n// called on a read-only transaction.\nfunc (tx *Tx) Commit() (err error) {\n\ttxId := tx.ID()\n\tlg := tx.db.Logger()\n\tif lg != discardLogger {\n\t\tlg.Debugf(\"Committing transaction %d\", txId)\n\t\tdefer func() {\n\t\t\tif err != nil {\n\t\t\t\tlg.Errorf(\"Committing transaction failed: %v\", err)\n\t\t\t} else {\n\t\t\t\tlg.Debugf(\"Committing transaction %d successfully\", txId)\n\t\t\t}\n\t\t}()\n\t}\n\n\tcommon.Assert(!tx.managed, \"managed tx commit not allowed\")\n\tif tx.db == nil {\n\t\treturn berrors.ErrTxClosed\n\t} else if !tx.writable {\n\t\treturn berrors.ErrTxNotWritable\n\t}\n\n\t// TODO(benbjohnson): Use vectorized I/O to write out dirty pages.\n\n\t// Rebalance nodes which have had deletions.\n\tvar startTime = time.Now()\n\ttx.root.rebalance()\n\tif tx.stats.GetRebalance() > 0 {\n\t\ttx.stats.IncRebalanceTime(time.Since(startTime))\n\t}\n\n\topgid := tx.meta.Pgid()\n\n\t// spill data onto dirty pages.\n\tstartTime = time.Now()\n\tif err = tx.root.spill(); err != nil {\n\t\tlg.Errorf(\"spilling data onto dirty pages failed: %v\", err)\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\ttx.stats.IncSpillTime(time.Since(startTime))\n\n\t// Free the old root bucket.\n\ttx.meta.RootBucket().SetRootPage(tx.root.RootPage())\n\n\t// Free the old freelist because commit writes out a fresh freelist.\n\tif tx.meta.Freelist() != common.PgidNoFreelist {\n\t\ttx.db.freelist.Free(tx.meta.Txid(), tx.db.page(tx.meta.Freelist()))\n\t}\n\n\tif !tx.db.NoFreelistSync {\n\t\terr = tx.commitFreelist()\n\t\tif err != nil {\n\t\t\tlg.Errorf(\"committing freelist failed: %v\", err)\n\t\t\treturn err\n\t\t}\n\t} else {\n\t\ttx.meta.SetFreelist(common.PgidNoFreelist)\n\t}\n\n\t// If the high water mark has moved up then attempt to grow the database.\n\tif tx.meta.Pgid() > opgid {\n\t\t_ = errors.New(\"\")\n\t\t// gofail: var lackOfDiskSpace string\n\t\t// tx.rollback()\n\t\t// return errors.New(lackOfDiskSpace)\n\t\tif err = tx.db.grow(int(tx.meta.Pgid()+1) * tx.db.pageSize); err != nil {\n\t\t\tlg.Errorf(\"growing db size failed, pgid: %d, pagesize: %d, error: %v\", tx.meta.Pgid(), tx.db.pageSize, err)\n\t\t\ttx.rollback()\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Write dirty pages to disk.\n\tstartTime = time.Now()\n\tif err = tx.write(); err != nil {\n\t\tlg.Errorf(\"writing data failed: %v\", err)\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\n\t// If strict mode is enabled then perform a consistency check.\n\tif tx.db.StrictMode {\n\t\tch := tx.Check()\n\t\tvar errs []string\n\t\tfor {\n\t\t\tchkErr, ok := <-ch\n\t\t\tif !ok {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\terrs = append(errs, chkErr.Error())\n\t\t}\n\t\tif len(errs) > 0 {\n\t\t\tpanic(\"check fail: \" + strings.Join(errs, \"\\n\"))\n\t\t}\n\t}\n\n\t// Write meta to disk.\n\tif err = tx.writeMeta(); err != nil {\n\t\tlg.Errorf(\"writeMeta failed: %v\", err)\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\ttx.stats.IncWriteTime(time.Since(startTime))\n\n\t// Finalize the transaction.\n\ttx.close()\n\n\t// Execute commit handlers now that the locks have been removed.\n\tfor _, fn := range tx.commitHandlers {\n\t\tfn()\n\t}\n\n\treturn nil\n}\n\nfunc (tx *Tx) commitFreelist() error {\n\t// Allocate new pages for the new free list. This will overestimate\n\t// the size of the freelist but not underestimate the size (which would be bad).\n\tp, err := tx.allocate((tx.db.freelist.EstimatedWritePageSize() / tx.db.pageSize) + 1)\n\tif err != nil {\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\n\ttx.db.freelist.Write(p)\n\ttx.meta.SetFreelist(p.Id())\n\n\treturn nil\n}\n\n// Rollback closes the transaction and ignores all previous updates. Read-only\n// transactions must be rolled back and not committed.\nfunc (tx *Tx) Rollback() error {\n\tcommon.Assert(!tx.managed, \"managed tx rollback not allowed\")\n\tif tx.db == nil {\n\t\treturn berrors.ErrTxClosed\n\t}\n\ttx.nonPhysicalRollback()\n\treturn nil\n}\n\n// nonPhysicalRollback is called when user calls Rollback directly, in this case we do not need to reload the free pages from disk.\nfunc (tx *Tx) nonPhysicalRollback() {\n\tif tx.db == nil {\n\t\treturn\n\t}\n\tif tx.writable {\n\t\ttx.db.freelist.Rollback(tx.meta.Txid())\n\t}\n\ttx.close()\n}\n\n// rollback needs to reload the free pages from disk in case some system error happens like fsync error.\nfunc (tx *Tx) rollback() {\n\tif tx.db == nil {\n\t\treturn\n\t}\n\tif tx.writable {\n\t\ttx.db.freelist.Rollback(tx.meta.Txid())\n\t\t// When mmap fails, the `data`, `dataref` and `datasz` may be reset to\n\t\t// zero values, and there is no way to reload free page IDs in this case.\n\t\tif tx.db.data != nil {\n\t\t\tif !tx.db.hasSyncedFreelist() {\n\t\t\t\t// Reconstruct free page list by scanning the DB to get the whole free page list.\n\t\t\t\t// Note: scanning the whole db is heavy if your db size is large in NoSyncFreeList mode.\n\t\t\t\ttx.db.freelist.NoSyncReload(tx.db.freepages())\n\t\t\t} else {\n\t\t\t\t// Read free page list from freelist page.\n\t\t\t\ttx.db.freelist.Reload(tx.db.page(tx.db.meta().Freelist()))\n\t\t\t}\n\t\t}\n\t}\n\ttx.close()\n}\n\nfunc (tx *Tx) close() {\n\tif tx.db == nil {\n\t\treturn\n\t}\n\tif tx.writable {\n\t\t// Grab freelist stats.\n\t\tvar freelistFreeN = tx.db.freelist.FreeCount()\n\t\tvar freelistPendingN = tx.db.freelist.PendingCount()\n\t\tvar freelistAlloc = tx.db.freelist.EstimatedWritePageSize()\n\n\t\t// Remove transaction ref & writer lock.\n\t\ttx.db.rwtx = nil\n\t\ttx.db.rwlock.Unlock()\n\n\t\t// Merge statistics.\n\t\tif tx.db.stats != nil {\n\t\t\ttx.db.statlock.Lock()\n\t\t\ttx.db.stats.FreePageN = freelistFreeN\n\t\t\ttx.db.stats.PendingPageN = freelistPendingN\n\t\t\ttx.db.stats.FreeAlloc = (freelistFreeN + freelistPendingN) * tx.db.pageSize\n\t\t\ttx.db.stats.FreelistInuse = freelistAlloc\n\t\t\ttx.db.stats.TxStats.add(&tx.stats)\n\t\t\ttx.db.statlock.Unlock()\n\t\t}\n\t} else {\n\t\ttx.db.removeTx(tx)\n\t}\n\n\t// Clear all references.\n\ttx.db = nil\n\ttx.meta = nil\n\ttx.root = Bucket{tx: tx}\n\ttx.pages = nil\n}\n\n// Copy writes the entire database to a writer.\n// This function exists for backwards compatibility.\n//\n// Deprecated: Use WriteTo() instead.\nfunc (tx *Tx) Copy(w io.Writer) error {\n\t_, err := tx.WriteTo(w)\n\treturn err\n}\n\n// WriteTo writes the entire database to a writer.\n// If err == nil then exactly tx.Size() bytes will be written into the writer.\nfunc (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {\n\tvar f *os.File\n\t// There is a risk that between the time a read-only transaction\n\t// is created and the time the file is actually opened, the\n\t// underlying db file at tx.db.path may have been replaced\n\t// (e.g. via rename). In that case, opening the file again would\n\t// unexpectedly point to a different file, rather than the one\n\t// the transaction was based on.\n\t//\n\t// To overcome this, we reuse the already opened file handle when\n\t// WriteFlag not set. When the WriteFlag is set, we reopen the file\n\t// but verify that it still refers to the same underlying file\n\t// (by device and inode). If it does not, we fall back to\n\t// reusing the existing already opened file handle.\n\tif tx.WriteFlag != 0 {\n\t\t// Attempt to open reader with WriteFlag\n\t\tf, err = tx.db.openFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0)\n\t\tif err != nil {\n\t\t\treturn 0, err\n\t\t}\n\n\t\tif ok, err := sameFile(tx.db.file, f); !ok {\n\t\t\tlg := tx.db.Logger()\n\t\t\tif cerr := f.Close(); cerr != nil {\n\t\t\t\tlg.Errorf(\"failed to close the file (%s): %v\", tx.db.path, cerr)\n\t\t\t}\n\t\t\tlg.Warningf(\"The underlying file has changed, so reuse the already opened file (%s): %v\", tx.db.path, err)\n\t\t\tf = tx.db.file\n\t\t} else {\n\t\t\tdefer func() {\n\t\t\t\tif cerr := f.Close(); err == nil {\n\t\t\t\t\terr = cerr\n\t\t\t\t}\n\t\t\t}()\n\t\t}\n\t} else {\n\t\tf = tx.db.file\n\t}\n\n\t// Generate a meta page. We use the same page data for both meta pages.\n\tbuf := make([]byte, tx.db.pageSize)\n\tpage := (*common.Page)(unsafe.Pointer(&buf[0]))\n\tpage.SetFlags(common.MetaPageFlag)\n\t*page.Meta() = *tx.meta\n\n\t// Write meta 0.\n\tpage.SetId(0)\n\tpage.Meta().SetChecksum(page.Meta().Sum64())\n\tnn, err := w.Write(buf)\n\tn += int64(nn)\n\tif err != nil {\n\t\treturn n, fmt.Errorf(\"meta 0 copy: %s\", err)\n\t}\n\n\t// Write meta 1 with a lower transaction id.\n\tpage.SetId(1)\n\tpage.Meta().DecTxid()\n\tpage.Meta().SetChecksum(page.Meta().Sum64())\n\tnn, err = w.Write(buf)\n\tn += int64(nn)\n\tif err != nil {\n\t\treturn n, fmt.Errorf(\"meta 1 copy: %s\", err)\n\t}\n\n\t// Copy data pages using a SectionReader to avoid affecting f's offset.\n\tdataOffset := int64(tx.db.pageSize * 2)\n\tdataSize := tx.Size() - dataOffset\n\tsr := io.NewSectionReader(f, dataOffset, dataSize)\n\n\t// Copy data pages.\n\twn, err := io.CopyN(w, sr, dataSize)\n\tn += wn\n\tif err != nil {\n\t\treturn n, err\n\t}\n\n\treturn n, nil\n}\n\nfunc sameFile(f1, f2 *os.File) (bool, error) {\n\tfi1, err := f1.Stat()\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"failed to get fileInfo of the first file (%s): %w\", f1.Name(), err)\n\t}\n\tfi2, err := f2.Stat()\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"failed to get fileInfo of the second file (%s): %w\", f2.Name(), err)\n\t}\n\n\treturn os.SameFile(fi1, fi2), nil\n}\n\n// CopyFile copies the entire database to file at the given path.\n// A reader transaction is maintained during the copy so it is safe to continue\n// using the database while a copy is in progress.\nfunc (tx *Tx) CopyFile(path string, mode os.FileMode) error {\n\tf, err := tx.db.openFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t_, err = tx.WriteTo(f)\n\tif err != nil {\n\t\t_ = f.Close()\n\t\treturn err\n\t}\n\treturn f.Close()\n}\n\n// allocate returns a contiguous block of memory starting at a given page.\nfunc (tx *Tx) allocate(count int) (*common.Page, error) {\n\tlg := tx.db.Logger()\n\tp, err := tx.db.allocate(tx.meta.Txid(), count)\n\tif err != nil {\n\t\tlg.Errorf(\"allocating failed, txid: %d, count: %d, error: %v\", tx.meta.Txid(), count, err)\n\t\treturn nil, err\n\t}\n\n\t// Save to our page cache.\n\ttx.pages[p.Id()] = p\n\n\t// Update statistics.\n\ttx.stats.IncPageCount(int64(count))\n\ttx.stats.IncPageAlloc(int64(count * tx.db.pageSize))\n\n\treturn p, nil\n}\n\n// write writes any dirty pages to disk.\nfunc (tx *Tx) write() error {\n\t// Sort pages by id.\n\tlg := tx.db.Logger()\n\tpages := make(common.Pages, 0, len(tx.pages))\n\tfor _, p := range tx.pages {\n\t\tpages = append(pages, p)\n\t}\n\t// Clear out page cache early.\n\ttx.pages = make(map[common.Pgid]*common.Page)\n\tsort.Sort(pages)\n\n\t// Write pages to disk in order.\n\tfor _, p := range pages {\n\t\trem := (uint64(p.Overflow()) + 1) * uint64(tx.db.pageSize)\n\t\toffset := int64(p.Id()) * int64(tx.db.pageSize)\n\t\tvar written uintptr\n\n\t\t// Write out page in \"max allocation\" sized chunks.\n\t\tfor {\n\t\t\tsz := rem\n\t\t\tif sz > common.MaxAllocSize-1 {\n\t\t\t\tsz = common.MaxAllocSize - 1\n\t\t\t}\n\t\t\tbuf := common.UnsafeByteSlice(unsafe.Pointer(p), written, 0, int(sz))\n\n\t\t\tif _, err := tx.db.ops.writeAt(buf, offset); err != nil {\n\t\t\t\tlg.Errorf(\"writeAt failed, offset: %d: %w\", offset, err)\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// Update statistics.\n\t\t\ttx.stats.IncWrite(1)\n\n\t\t\t// Exit inner for loop if we've written all the chunks.\n\t\t\trem -= sz\n\t\t\tif rem == 0 {\n\t\t\t\tbreak\n\t\t\t}\n\n\t\t\t// Otherwise move offset forward and move pointer to next chunk.\n\t\t\toffset += int64(sz)\n\t\t\twritten += uintptr(sz)\n\t\t}\n\t}\n\n\t// Ignore file sync if flag is set on DB.\n\tif !tx.db.NoSync || common.IgnoreNoSync {\n\t\t// gofail: var beforeSyncDataPages struct{}\n\t\tif err := fdatasync(tx.db); err != nil {\n\t\t\tlg.Errorf(\"[GOOS: %s, GOARCH: %s] fdatasync failed: %w\", runtime.GOOS, runtime.GOARCH, err)\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Put small pages back to page pool.\n\tfor _, p := range pages {\n\t\t// Ignore page sizes over 1 page.\n\t\t// These are allocated using make() instead of the page pool.\n\t\tif int(p.Overflow()) != 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\tbuf := common.UnsafeByteSlice(unsafe.Pointer(p), 0, 0, tx.db.pageSize)\n\n\t\t// See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1\n\t\tfor i := range buf {\n\t\t\tbuf[i] = 0\n\t\t}\n\t\ttx.db.pagePool.Put(buf) //nolint:staticcheck\n\t}\n\n\treturn nil\n}\n\n// writeMeta writes the meta to the disk.\nfunc (tx *Tx) writeMeta() error {\n\t// gofail: var beforeWriteMetaError string\n\t// return errors.New(beforeWriteMetaError)\n\n\t// Create a temporary buffer for the meta page.\n\tlg := tx.db.Logger()\n\tbuf := make([]byte, tx.db.pageSize)\n\tp := tx.db.pageInBuffer(buf, 0)\n\ttx.meta.Write(p)\n\n\t// Write the meta page to file.\n\ttx.db.metalock.Lock()\n\tif _, err := tx.db.ops.writeAt(buf, int64(p.Id())*int64(tx.db.pageSize)); err != nil {\n\t\ttx.db.metalock.Unlock()\n\t\tlg.Errorf(\"writeAt failed, pgid: %d, pageSize: %d, error: %v\", p.Id(), tx.db.pageSize, err)\n\t\treturn err\n\t}\n\ttx.db.metalock.Unlock()\n\tif !tx.db.NoSync || common.IgnoreNoSync {\n\t\t// gofail: var beforeSyncMetaPage struct{}\n\t\tif err := fdatasync(tx.db); err != nil {\n\t\t\tlg.Errorf(\"[GOOS: %s, GOARCH: %s] fdatasync failed: %w\", runtime.GOOS, runtime.GOARCH, err)\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Update statistics.\n\ttx.stats.IncWrite(1)\n\n\treturn nil\n}\n\n// page returns a reference to the page with a given id.\n// If page has been written to then a temporary buffered page is returned.\nfunc (tx *Tx) page(id common.Pgid) *common.Page {\n\t// Check the dirty pages first.\n\tif tx.pages != nil {\n\t\tif p, ok := tx.pages[id]; ok {\n\t\t\tp.FastCheck(id)\n\t\t\treturn p\n\t\t}\n\t}\n\n\t// Otherwise return directly from the mmap.\n\tp := tx.db.page(id)\n\tp.FastCheck(id)\n\treturn p\n}\n\n// forEachPage iterates over every page within a given page and executes a function.\nfunc (tx *Tx) forEachPage(pgidnum common.Pgid, fn func(*common.Page, int, []common.Pgid)) {\n\tstack := make([]common.Pgid, 10)\n\tstack[0] = pgidnum\n\ttx.forEachPageInternal(stack[:1], fn)\n}\n\nfunc (tx *Tx) forEachPageInternal(pgidstack []common.Pgid, fn func(*common.Page, int, []common.Pgid)) {\n\tp := tx.page(pgidstack[len(pgidstack)-1])\n\n\t// Execute function.\n\tfn(p, len(pgidstack)-1, pgidstack)\n\n\t// Recursively loop over children.\n\tif p.IsBranchPage() {\n\t\tfor i := 0; i < int(p.Count()); i++ {\n\t\t\telem := p.BranchPageElement(uint16(i))\n\t\t\ttx.forEachPageInternal(append(pgidstack, elem.Pgid()), fn)\n\t\t}\n\t}\n}\n\n// Page returns page information for a given page number.\n// This is only safe for concurrent use when used by a writable transaction.\nfunc (tx *Tx) Page(id int) (*common.PageInfo, error) {\n\tif tx.db == nil {\n\t\treturn nil, berrors.ErrTxClosed\n\t} else if common.Pgid(id) >= tx.meta.Pgid() {\n\t\treturn nil, nil\n\t}\n\n\tif tx.db.freelist == nil {\n\t\treturn nil, berrors.ErrFreePagesNotLoaded\n\t}\n\n\t// Build the page info.\n\tp := tx.db.page(common.Pgid(id))\n\tinfo := &common.PageInfo{\n\t\tID:            id,\n\t\tCount:         int(p.Count()),\n\t\tOverflowCount: int(p.Overflow()),\n\t}\n\n\t// Determine the type (or if it's free).\n\tif tx.db.freelist.Freed(common.Pgid(id)) {\n\t\tinfo.Type = \"free\"\n\t} else {\n\t\tinfo.Type = p.Typ()\n\t}\n\n\treturn info, nil\n}\n\n// TxStats represents statistics about the actions performed by the transaction.\ntype TxStats struct {\n\t// Page statistics.\n\t//\n\t// DEPRECATED: Use GetPageCount() or IncPageCount()\n\tPageCount int64 // number of page allocations\n\t// DEPRECATED: Use GetPageAlloc() or IncPageAlloc()\n\tPageAlloc int64 // total bytes allocated\n\n\t// Cursor statistics.\n\t//\n\t// DEPRECATED: Use GetCursorCount() or IncCursorCount()\n\tCursorCount int64 // number of cursors created\n\n\t// Node statistics\n\t//\n\t// DEPRECATED: Use GetNodeCount() or IncNodeCount()\n\tNodeCount int64 // number of node allocations\n\t// DEPRECATED: Use GetNodeDeref() or IncNodeDeref()\n\tNodeDeref int64 // number of node dereferences\n\n\t// Rebalance statistics.\n\t//\n\t// DEPRECATED: Use GetRebalance() or IncRebalance()\n\tRebalance int64 // number of node rebalances\n\t// DEPRECATED: Use GetRebalanceTime() or IncRebalanceTime()\n\tRebalanceTime time.Duration // total time spent rebalancing\n\n\t// Split/Spill statistics.\n\t//\n\t// DEPRECATED: Use GetSplit() or IncSplit()\n\tSplit int64 // number of nodes split\n\t// DEPRECATED: Use GetSpill() or IncSpill()\n\tSpill int64 // number of nodes spilled\n\t// DEPRECATED: Use GetSpillTime() or IncSpillTime()\n\tSpillTime time.Duration // total time spent spilling\n\n\t// Write statistics.\n\t//\n\t// DEPRECATED: Use GetWrite() or IncWrite()\n\tWrite int64 // number of writes performed\n\t// DEPRECATED: Use GetWriteTime() or IncWriteTime()\n\tWriteTime time.Duration // total time spent writing to disk\n}\n\nfunc (s *TxStats) add(other *TxStats) {\n\ts.IncPageCount(other.GetPageCount())\n\ts.IncPageAlloc(other.GetPageAlloc())\n\ts.IncCursorCount(other.GetCursorCount())\n\ts.IncNodeCount(other.GetNodeCount())\n\ts.IncNodeDeref(other.GetNodeDeref())\n\ts.IncRebalance(other.GetRebalance())\n\ts.IncRebalanceTime(other.GetRebalanceTime())\n\ts.IncSplit(other.GetSplit())\n\ts.IncSpill(other.GetSpill())\n\ts.IncSpillTime(other.GetSpillTime())\n\ts.IncWrite(other.GetWrite())\n\ts.IncWriteTime(other.GetWriteTime())\n}\n\n// Sub calculates and returns the difference between two sets of transaction stats.\n// This is useful when obtaining stats at two different points and time and\n// you need the performance counters that occurred within that time span.\nfunc (s *TxStats) Sub(other *TxStats) TxStats {\n\tvar diff TxStats\n\tdiff.PageCount = s.GetPageCount() - other.GetPageCount()\n\tdiff.PageAlloc = s.GetPageAlloc() - other.GetPageAlloc()\n\tdiff.CursorCount = s.GetCursorCount() - other.GetCursorCount()\n\tdiff.NodeCount = s.GetNodeCount() - other.GetNodeCount()\n\tdiff.NodeDeref = s.GetNodeDeref() - other.GetNodeDeref()\n\tdiff.Rebalance = s.GetRebalance() - other.GetRebalance()\n\tdiff.RebalanceTime = s.GetRebalanceTime() - other.GetRebalanceTime()\n\tdiff.Split = s.GetSplit() - other.GetSplit()\n\tdiff.Spill = s.GetSpill() - other.GetSpill()\n\tdiff.SpillTime = s.GetSpillTime() - other.GetSpillTime()\n\tdiff.Write = s.GetWrite() - other.GetWrite()\n\tdiff.WriteTime = s.GetWriteTime() - other.GetWriteTime()\n\treturn diff\n}\n\n// GetPageCount returns PageCount atomically.\nfunc (s *TxStats) GetPageCount() int64 {\n\treturn atomic.LoadInt64(&s.PageCount)\n}\n\n// IncPageCount increases PageCount atomically and returns the new value.\nfunc (s *TxStats) IncPageCount(delta int64) int64 {\n\treturn atomic.AddInt64(&s.PageCount, delta)\n}\n\n// GetPageAlloc returns PageAlloc atomically.\nfunc (s *TxStats) GetPageAlloc() int64 {\n\treturn atomic.LoadInt64(&s.PageAlloc)\n}\n\n// IncPageAlloc increases PageAlloc atomically and returns the new value.\nfunc (s *TxStats) IncPageAlloc(delta int64) int64 {\n\treturn atomic.AddInt64(&s.PageAlloc, delta)\n}\n\n// GetCursorCount returns CursorCount atomically.\nfunc (s *TxStats) GetCursorCount() int64 {\n\treturn atomic.LoadInt64(&s.CursorCount)\n}\n\n// IncCursorCount increases CursorCount atomically and return the new value.\nfunc (s *TxStats) IncCursorCount(delta int64) int64 {\n\treturn atomic.AddInt64(&s.CursorCount, delta)\n}\n\n// GetNodeCount returns NodeCount atomically.\nfunc (s *TxStats) GetNodeCount() int64 {\n\treturn atomic.LoadInt64(&s.NodeCount)\n}\n\n// IncNodeCount increases NodeCount atomically and returns the new value.\nfunc (s *TxStats) IncNodeCount(delta int64) int64 {\n\treturn atomic.AddInt64(&s.NodeCount, delta)\n}\n\n// GetNodeDeref returns NodeDeref atomically.\nfunc (s *TxStats) GetNodeDeref() int64 {\n\treturn atomic.LoadInt64(&s.NodeDeref)\n}\n\n// IncNodeDeref increases NodeDeref atomically and returns the new value.\nfunc (s *TxStats) IncNodeDeref(delta int64) int64 {\n\treturn atomic.AddInt64(&s.NodeDeref, delta)\n}\n\n// GetRebalance returns Rebalance atomically.\nfunc (s *TxStats) GetRebalance() int64 {\n\treturn atomic.LoadInt64(&s.Rebalance)\n}\n\n// IncRebalance increases Rebalance atomically and returns the new value.\nfunc (s *TxStats) IncRebalance(delta int64) int64 {\n\treturn atomic.AddInt64(&s.Rebalance, delta)\n}\n\n// GetRebalanceTime returns RebalanceTime atomically.\nfunc (s *TxStats) GetRebalanceTime() time.Duration {\n\treturn atomicLoadDuration(&s.RebalanceTime)\n}\n\n// IncRebalanceTime increases RebalanceTime atomically and returns the new value.\nfunc (s *TxStats) IncRebalanceTime(delta time.Duration) time.Duration {\n\treturn atomicAddDuration(&s.RebalanceTime, delta)\n}\n\n// GetSplit returns Split atomically.\nfunc (s *TxStats) GetSplit() int64 {\n\treturn atomic.LoadInt64(&s.Split)\n}\n\n// IncSplit increases Split atomically and returns the new value.\nfunc (s *TxStats) IncSplit(delta int64) int64 {\n\treturn atomic.AddInt64(&s.Split, delta)\n}\n\n// GetSpill returns Spill atomically.\nfunc (s *TxStats) GetSpill() int64 {\n\treturn atomic.LoadInt64(&s.Spill)\n}\n\n// IncSpill increases Spill atomically and returns the new value.\nfunc (s *TxStats) IncSpill(delta int64) int64 {\n\treturn atomic.AddInt64(&s.Spill, delta)\n}\n\n// GetSpillTime returns SpillTime atomically.\nfunc (s *TxStats) GetSpillTime() time.Duration {\n\treturn atomicLoadDuration(&s.SpillTime)\n}\n\n// IncSpillTime increases SpillTime atomically and returns the new value.\nfunc (s *TxStats) IncSpillTime(delta time.Duration) time.Duration {\n\treturn atomicAddDuration(&s.SpillTime, delta)\n}\n\n// GetWrite returns Write atomically.\nfunc (s *TxStats) GetWrite() int64 {\n\treturn atomic.LoadInt64(&s.Write)\n}\n\n// IncWrite increases Write atomically and returns the new value.\nfunc (s *TxStats) IncWrite(delta int64) int64 {\n\treturn atomic.AddInt64(&s.Write, delta)\n}\n\n// GetWriteTime returns WriteTime atomically.\nfunc (s *TxStats) GetWriteTime() time.Duration {\n\treturn atomicLoadDuration(&s.WriteTime)\n}\n\n// IncWriteTime increases WriteTime atomically and returns the new value.\nfunc (s *TxStats) IncWriteTime(delta time.Duration) time.Duration {\n\treturn atomicAddDuration(&s.WriteTime, delta)\n}\n\nfunc atomicAddDuration(ptr *time.Duration, du time.Duration) time.Duration {\n\treturn time.Duration(atomic.AddInt64((*int64)(unsafe.Pointer(ptr)), int64(du)))\n}\n\nfunc atomicLoadDuration(ptr *time.Duration) time.Duration {\n\treturn time.Duration(atomic.LoadInt64((*int64)(unsafe.Pointer(ptr))))\n}\n"
  },
  {
    "path": "tx_check.go",
    "content": "package bbolt\n\nimport (\n\t\"encoding/hex\"\n\t\"fmt\"\n\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// Check performs several consistency checks on the database for this transaction.\n// An error is returned if any inconsistency is found.\n//\n// It can be safely run concurrently on a writable transaction. However, this\n// incurs a high cost for large databases and databases with a lot of subbuckets\n// because of caching. This overhead can be removed if running on a read-only\n// transaction, however, it is not safe to execute other writer transactions at\n// the same time.\n//\n// It also allows users to provide a customized `KVStringer` implementation,\n// so that bolt can generate human-readable diagnostic messages.\nfunc (tx *Tx) Check(options ...CheckOption) <-chan error {\n\tchkConfig := checkConfig{\n\t\tkvStringer: HexKVStringer(),\n\t}\n\tfor _, op := range options {\n\t\top(&chkConfig)\n\t}\n\n\tch := make(chan error)\n\tgo func() {\n\t\t// Close the channel to signal completion.\n\t\tdefer close(ch)\n\t\ttx.check(chkConfig, ch)\n\t}()\n\treturn ch\n}\n\nfunc (tx *Tx) check(cfg checkConfig, ch chan error) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tch <- panicked{r}\n\t\t}\n\t}()\n\t// Force loading free list if opened in ReadOnly mode.\n\ttx.db.loadFreelist()\n\n\t// Check if any pages are double freed.\n\tfreed := make(map[common.Pgid]bool)\n\tall := make([]common.Pgid, tx.db.freelist.Count())\n\ttx.db.freelist.Copyall(all)\n\tfor _, id := range all {\n\t\tif freed[id] {\n\t\t\tch <- fmt.Errorf(\"page %d: already freed\", id)\n\t\t}\n\t\tfreed[id] = true\n\t}\n\n\t// Track every reachable page.\n\treachable := make(map[common.Pgid]*common.Page)\n\treachable[0] = tx.page(0) // meta0\n\treachable[1] = tx.page(1) // meta1\n\tif tx.meta.Freelist() != common.PgidNoFreelist {\n\t\tfor i := uint32(0); i <= tx.page(tx.meta.Freelist()).Overflow(); i++ {\n\t\t\treachable[tx.meta.Freelist()+common.Pgid(i)] = tx.page(tx.meta.Freelist())\n\t\t}\n\t}\n\n\tif cfg.pageId == 0 {\n\t\t// Check the whole db file, starting from the root bucket and\n\t\t// recursively check all child buckets.\n\t\ttx.recursivelyCheckBucket(&tx.root, reachable, freed, cfg.kvStringer, ch)\n\n\t\t// Ensure all pages below high water mark are either reachable or freed.\n\t\tfor i := common.Pgid(0); i < tx.meta.Pgid(); i++ {\n\t\t\t_, isReachable := reachable[i]\n\t\t\tif !isReachable && !freed[i] {\n\t\t\t\tch <- fmt.Errorf(\"page %d: unreachable unfreed\", int(i))\n\t\t\t}\n\t\t}\n\t} else {\n\t\t// Check the db file starting from a specified pageId.\n\t\tif cfg.pageId < 2 || cfg.pageId >= uint64(tx.meta.Pgid()) {\n\t\t\tch <- fmt.Errorf(\"page ID (%d) out of range [%d, %d)\", cfg.pageId, 2, tx.meta.Pgid())\n\t\t\treturn\n\t\t}\n\n\t\ttx.recursivelyCheckPage(common.Pgid(cfg.pageId), reachable, freed, cfg.kvStringer, ch)\n\t}\n}\n\nfunc (tx *Tx) recursivelyCheckPage(pageId common.Pgid, reachable map[common.Pgid]*common.Page, freed map[common.Pgid]bool,\n\tkvStringer KVStringer, ch chan error) {\n\ttx.checkInvariantProperties(pageId, reachable, freed, kvStringer, ch)\n\ttx.recursivelyCheckBucketInPage(pageId, reachable, freed, kvStringer, ch)\n}\n\nfunc (tx *Tx) recursivelyCheckBucketInPage(pageId common.Pgid, reachable map[common.Pgid]*common.Page, freed map[common.Pgid]bool,\n\tkvStringer KVStringer, ch chan error) {\n\tp := tx.page(pageId)\n\n\tswitch {\n\tcase p.IsBranchPage():\n\t\tfor i := range p.BranchPageElements() {\n\t\t\telem := p.BranchPageElement(uint16(i))\n\t\t\ttx.recursivelyCheckBucketInPage(elem.Pgid(), reachable, freed, kvStringer, ch)\n\t\t}\n\tcase p.IsLeafPage():\n\t\tfor i := range p.LeafPageElements() {\n\t\t\telem := p.LeafPageElement(uint16(i))\n\t\t\tif elem.IsBucketEntry() {\n\t\t\t\tinBkt := common.NewInBucket(pageId, 0)\n\t\t\t\ttmpBucket := Bucket{\n\t\t\t\t\tInBucket:    &inBkt,\n\t\t\t\t\trootNode:    &node{isLeaf: p.IsLeafPage()},\n\t\t\t\t\tFillPercent: DefaultFillPercent,\n\t\t\t\t\ttx:          tx,\n\t\t\t\t}\n\t\t\t\tif child := tmpBucket.Bucket(elem.Key()); child != nil {\n\t\t\t\t\ttx.recursivelyCheckBucket(child, reachable, freed, kvStringer, ch)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tdefault:\n\t\tch <- fmt.Errorf(\"unexpected page type (flags: %x) for pgId:%d\", p.Flags(), pageId)\n\t}\n}\n\nfunc (tx *Tx) recursivelyCheckBucket(b *Bucket, reachable map[common.Pgid]*common.Page, freed map[common.Pgid]bool,\n\tkvStringer KVStringer, ch chan error) {\n\t// Ignore inline buckets.\n\tif b.RootPage() == 0 {\n\t\treturn\n\t}\n\n\ttx.checkInvariantProperties(b.RootPage(), reachable, freed, kvStringer, ch)\n\n\t// Check each bucket within this bucket.\n\t_ = b.ForEachBucket(func(k []byte) error {\n\t\tif child := b.Bucket(k); child != nil {\n\t\t\ttx.recursivelyCheckBucket(child, reachable, freed, kvStringer, ch)\n\t\t}\n\t\treturn nil\n\t})\n}\n\nfunc (tx *Tx) checkInvariantProperties(pageId common.Pgid, reachable map[common.Pgid]*common.Page, freed map[common.Pgid]bool,\n\tkvStringer KVStringer, ch chan error) {\n\ttx.forEachPage(pageId, func(p *common.Page, _ int, stack []common.Pgid) {\n\t\tverifyPageReachable(p, tx.meta.Pgid(), stack, reachable, freed, ch)\n\t})\n\n\ttx.recursivelyCheckPageKeyOrder(pageId, kvStringer.KeyToString, ch)\n}\n\nfunc verifyPageReachable(p *common.Page, hwm common.Pgid, stack []common.Pgid, reachable map[common.Pgid]*common.Page, freed map[common.Pgid]bool, ch chan error) {\n\tif p.Id() > hwm {\n\t\tch <- fmt.Errorf(\"page %d: out of bounds: %d (stack: %v)\", int(p.Id()), int(hwm), stack)\n\t}\n\n\t// Ensure each page is only referenced once.\n\tfor i := common.Pgid(0); i <= common.Pgid(p.Overflow()); i++ {\n\t\tvar id = p.Id() + i\n\t\tif _, ok := reachable[id]; ok {\n\t\t\tch <- fmt.Errorf(\"page %d: multiple references (stack: %v)\", int(id), stack)\n\t\t}\n\t\treachable[id] = p\n\t}\n\n\t// We should only encounter un-freed leaf and branch pages.\n\tif freed[p.Id()] {\n\t\tch <- fmt.Errorf(\"page %d: reachable freed\", int(p.Id()))\n\t} else if !p.IsBranchPage() && !p.IsLeafPage() {\n\t\tch <- fmt.Errorf(\"page %d: invalid type: %s (stack: %v)\", int(p.Id()), p.Typ(), stack)\n\t}\n}\n\n// recursivelyCheckPageKeyOrder verifies database consistency with respect to b-tree\n// key order constraints:\n//   - keys on pages must be sorted\n//   - keys on children pages are between 2 consecutive keys on the parent's branch page).\nfunc (tx *Tx) recursivelyCheckPageKeyOrder(pgId common.Pgid, keyToString func([]byte) string, ch chan error) {\n\ttx.recursivelyCheckPageKeyOrderInternal(pgId, nil, nil, nil, keyToString, ch)\n}\n\n// recursivelyCheckPageKeyOrderInternal verifies that all keys in the subtree rooted at `pgid` are:\n//   - >=`minKeyClosed` (can be nil)\n//   - <`maxKeyOpen` (can be nil)\n//   - Are in right ordering relationship to their parents.\n//     `pagesStack` is expected to contain IDs of pages from the tree root to `pgid` for the clean debugging message.\nfunc (tx *Tx) recursivelyCheckPageKeyOrderInternal(\n\tpgId common.Pgid, minKeyClosed, maxKeyOpen []byte, pagesStack []common.Pgid,\n\tkeyToString func([]byte) string, ch chan error) (maxKeyInSubtree []byte) {\n\n\tp := tx.page(pgId)\n\tpagesStack = append(pagesStack, pgId)\n\tswitch {\n\tcase p.IsBranchPage():\n\t\t// For branch page we navigate ranges of all subpages.\n\t\trunningMin := minKeyClosed\n\t\tfor i := range p.BranchPageElements() {\n\t\t\telem := p.BranchPageElement(uint16(i))\n\t\t\tverifyKeyOrder(elem.Pgid(), \"branch\", i, elem.Key(), runningMin, maxKeyOpen, ch, keyToString, pagesStack)\n\n\t\t\tmaxKey := maxKeyOpen\n\t\t\tif i < len(p.BranchPageElements())-1 {\n\t\t\t\tmaxKey = p.BranchPageElement(uint16(i + 1)).Key()\n\t\t\t}\n\t\t\tmaxKeyInSubtree = tx.recursivelyCheckPageKeyOrderInternal(elem.Pgid(), elem.Key(), maxKey, pagesStack, keyToString, ch)\n\t\t\trunningMin = maxKeyInSubtree\n\t\t}\n\t\treturn maxKeyInSubtree\n\tcase p.IsLeafPage():\n\t\trunningMin := minKeyClosed\n\t\tfor i := range p.LeafPageElements() {\n\t\t\telem := p.LeafPageElement(uint16(i))\n\t\t\tverifyKeyOrder(pgId, \"leaf\", i, elem.Key(), runningMin, maxKeyOpen, ch, keyToString, pagesStack)\n\t\t\trunningMin = elem.Key()\n\t\t}\n\t\tif p.Count() > 0 {\n\t\t\treturn p.LeafPageElement(p.Count() - 1).Key()\n\t\t}\n\tdefault:\n\t\tch <- fmt.Errorf(\"unexpected page type (flags: %x) for pgId:%d\", p.Flags(), pgId)\n\t}\n\treturn maxKeyInSubtree\n}\n\n/***\n * verifyKeyOrder checks whether an entry with given #index on pgId (pageType: \"branch|leaf\") that has given \"key\",\n * is within range determined by (previousKey..maxKeyOpen) and reports found violations to the channel (ch).\n */\nfunc verifyKeyOrder(pgId common.Pgid, pageType string, index int, key []byte, previousKey []byte, maxKeyOpen []byte, ch chan error, keyToString func([]byte) string, pagesStack []common.Pgid) {\n\tif index == 0 && previousKey != nil && compareKeys(previousKey, key) > 0 {\n\t\tch <- fmt.Errorf(\"the first key[%d]=(hex)%s on %s page(%d) needs to be >= the key in the ancestor (%s). Stack: %v\",\n\t\t\tindex, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack)\n\t}\n\tif index > 0 {\n\t\tcmpRet := compareKeys(previousKey, key)\n\t\tif cmpRet > 0 {\n\t\t\tch <- fmt.Errorf(\"key[%d]=(hex)%s on %s page(%d) needs to be > (found <) than previous element (hex)%s. Stack: %v\",\n\t\t\t\tindex, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack)\n\t\t}\n\t\tif cmpRet == 0 {\n\t\t\tch <- fmt.Errorf(\"key[%d]=(hex)%s on %s page(%d) needs to be > (found =) than previous element (hex)%s. Stack: %v\",\n\t\t\t\tindex, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack)\n\t\t}\n\t}\n\tif maxKeyOpen != nil && compareKeys(key, maxKeyOpen) >= 0 {\n\t\tch <- fmt.Errorf(\"key[%d]=(hex)%s on %s page(%d) needs to be < than key of the next element in ancestor (hex)%s. Pages stack: %v\",\n\t\t\tindex, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack)\n\t}\n}\n\n// ===========================================================================================\n\ntype checkConfig struct {\n\tkvStringer KVStringer\n\tpageId     uint64\n}\n\ntype CheckOption func(options *checkConfig)\n\nfunc WithKVStringer(kvStringer KVStringer) CheckOption {\n\treturn func(c *checkConfig) {\n\t\tc.kvStringer = kvStringer\n\t}\n}\n\n// WithPageId sets a page ID from which the check command starts to check\nfunc WithPageId(pageId uint64) CheckOption {\n\treturn func(c *checkConfig) {\n\t\tc.pageId = pageId\n\t}\n}\n\n// KVStringer allows to prepare human-readable diagnostic messages.\ntype KVStringer interface {\n\tKeyToString([]byte) string\n\tValueToString([]byte) string\n}\n\n// HexKVStringer serializes both key & value to hex representation.\nfunc HexKVStringer() KVStringer {\n\treturn hexKvStringer{}\n}\n\ntype hexKvStringer struct{}\n\nfunc (hexKvStringer) KeyToString(key []byte) string {\n\treturn hex.EncodeToString(key)\n}\n\nfunc (hexKvStringer) ValueToString(value []byte) string {\n\treturn hex.EncodeToString(value)\n}\n"
  },
  {
    "path": "tx_check_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"fmt\"\n\t\"math/rand\"\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/require\"\n\n\t\"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n\t\"go.etcd.io/bbolt/internal/common\"\n\t\"go.etcd.io/bbolt/internal/guts_cli\"\n)\n\nfunc TestTx_Check_CorruptPage(t *testing.T) {\n\tbucketName := []byte(\"data\")\n\n\tt.Log(\"Creating db file.\")\n\tdb := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})\n\n\t// Each page can hold roughly 20 key/values pair, so 100 such\n\t// key/value pairs will consume about 5 leaf pages.\n\terr := db.Fill(bucketName, 1, 100,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t)\n\trequire.NoError(t, err)\n\n\tt.Log(\"Corrupting a random leaf page.\")\n\tvictimPageId, validPageIds := corruptRandomLeafPageInBucket(t, db.DB, bucketName)\n\n\tt.Log(\"Running consistency check.\")\n\tvErr := db.View(func(tx *bbolt.Tx) error {\n\t\tvar cErrs []error\n\n\t\tt.Log(\"Check corrupted page.\")\n\t\terrChan := tx.Check(bbolt.WithPageId(uint64(victimPageId)))\n\t\tfor cErr := range errChan {\n\t\t\tcErrs = append(cErrs, cErr)\n\t\t}\n\t\trequire.Greater(t, len(cErrs), 0)\n\n\t\tt.Log(\"Check valid pages.\")\n\t\tcErrs = cErrs[:0]\n\t\tfor _, pgId := range validPageIds {\n\t\t\terrChan = tx.Check(bbolt.WithPageId(uint64(pgId)))\n\t\t\tfor cErr := range errChan {\n\t\t\t\tcErrs = append(cErrs, cErr)\n\t\t\t}\n\t\t\trequire.Equal(t, 0, len(cErrs))\n\t\t}\n\t\treturn nil\n\t})\n\trequire.NoError(t, vErr)\n\tt.Log(\"All check passed\")\n\n\t// Manually close the db, otherwise the PostTestCleanup will\n\t// check the db again and accordingly fail the test.\n\tdb.MustClose()\n}\n\nfunc TestTx_Check_WithNestBucket(t *testing.T) {\n\tparentBucketName := []byte(\"parentBucket\")\n\n\tt.Log(\"Creating db file.\")\n\tdb := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})\n\n\terr := db.Update(func(tx *bbolt.Tx) error {\n\t\tpb, bErr := tx.CreateBucket(parentBucketName)\n\t\tif bErr != nil {\n\t\t\treturn bErr\n\t\t}\n\n\t\tt.Log(\"put some key/values under the parent bucket directly\")\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tk, v := fmt.Sprintf(\"%04d\", i), fmt.Sprintf(\"value_%4d\", i)\n\t\t\tif pErr := pb.Put([]byte(k), []byte(v)); pErr != nil {\n\t\t\t\treturn pErr\n\t\t\t}\n\t\t}\n\n\t\tt.Log(\"create a nested bucket and put some key/values under the nested bucket\")\n\t\tcb, bErr := pb.CreateBucket([]byte(\"nestedBucket\"))\n\t\tif bErr != nil {\n\t\t\treturn bErr\n\t\t}\n\n\t\tfor i := 0; i < 2000; i++ {\n\t\t\tk, v := fmt.Sprintf(\"%04d\", i), fmt.Sprintf(\"value_%4d\", i)\n\t\t\tif pErr := cb.Put([]byte(k), []byte(v)); pErr != nil {\n\t\t\t\treturn pErr\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n\trequire.NoError(t, err)\n\n\t// Get the bucket's root page.\n\tbucketRootPageId := mustGetBucketRootPage(t, db.DB, parentBucketName)\n\n\tt.Logf(\"Running consistency check starting from pageId: %d\", bucketRootPageId)\n\tvErr := db.View(func(tx *bbolt.Tx) error {\n\t\tvar cErrs []error\n\n\t\terrChan := tx.Check(bbolt.WithPageId(uint64(bucketRootPageId)))\n\t\tfor cErr := range errChan {\n\t\t\tcErrs = append(cErrs, cErr)\n\t\t}\n\t\trequire.Equal(t, 0, len(cErrs))\n\n\t\treturn nil\n\t})\n\trequire.NoError(t, vErr)\n\tt.Log(\"All check passed\")\n\n\t// Manually close the db, otherwise the PostTestCleanup will\n\t// check the db again and accordingly fail the test.\n\tdb.MustClose()\n}\n\nfunc TestTx_Check_Panic(t *testing.T) {\n\tbucketName := []byte(\"data\")\n\tt.Log(\"Creating db file.\")\n\tdb := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})\n\n\t// Each page can hold roughly 20 key/values pair, so 100 such\n\t// key/value pairs will consume about 5 leaf pages.\n\terr := db.Fill(bucketName, 1, 100,\n\t\tfunc(tx int, k int) []byte { return []byte(fmt.Sprintf(\"%04d\", k)) },\n\t\tfunc(tx int, k int) []byte { return make([]byte, 100) },\n\t)\n\trequire.NoError(t, err)\n\n\tcorruptRootPage(t, db.DB, bucketName)\n\n\tpath := db.Path()\n\n\trequire.NoError(t, db.Close())\n\n\tdb = btesting.MustOpenDBWithOption(t, path, &bbolt.Options{PageSize: 4096})\n\n\tvErr := db.View(func(tx *bbolt.Tx) error {\n\t\terrChan := tx.Check()\n\t\tfor cErr := range errChan {\n\t\t\tfmt.Println(\"cErr\", cErr)\n\t\t\treturn cErr\n\t\t}\n\t\treturn nil\n\t})\n\trequire.Error(t, vErr)\n\trequire.ErrorContains(t, vErr, \"has unexpected type/flags: 0\")\n\n\t// Manually close the db, otherwise the PostTestCleanup will\n\t// check the db again and accordingly fail the test.\n\tdb.MustClose()\n}\n\n// corruptRandomLeafPage corrupts one random leaf page.\nfunc corruptRandomLeafPageInBucket(t testing.TB, db *bbolt.DB, bucketName []byte) (victimPageId common.Pgid, validPageIds []common.Pgid) {\n\tbucketRootPageId := mustGetBucketRootPage(t, db, bucketName)\n\tbucketRootPage, _, err := guts_cli.ReadPage(db.Path(), uint64(bucketRootPageId))\n\trequire.NoError(t, err)\n\trequire.True(t, bucketRootPage.IsBranchPage())\n\n\t// Retrieve all the leaf pages included in the branch page, and pick up random one from them.\n\tvar bucketPageIds []common.Pgid\n\tfor _, bpe := range bucketRootPage.BranchPageElements() {\n\t\tbucketPageIds = append(bucketPageIds, bpe.Pgid())\n\t}\n\trandomIdx := rand.Intn(len(bucketPageIds))\n\tvictimPageId = bucketPageIds[randomIdx]\n\tvalidPageIds = append(bucketPageIds[:randomIdx], bucketPageIds[randomIdx+1:]...)\n\n\tvictimPage, victimBuf, err := guts_cli.ReadPage(db.Path(), uint64(victimPageId))\n\trequire.NoError(t, err)\n\trequire.True(t, victimPage.IsLeafPage())\n\trequire.True(t, victimPage.Count() > 1)\n\n\t// intentionally make the second key < the first key.\n\telement := victimPage.LeafPageElement(1)\n\tkey := element.Key()\n\tkey[0] = 0\n\n\t// Write the corrupt page to db file.\n\terr = guts_cli.WritePage(db.Path(), victimBuf)\n\trequire.NoError(t, err)\n\treturn victimPageId, validPageIds\n}\n\nfunc corruptRootPage(t testing.TB, db *bbolt.DB, bucketName []byte) {\n\tbucketRootPageId := mustGetBucketRootPage(t, db, bucketName)\n\tbucketRootPage, bucketRootPageBuf, err := guts_cli.ReadPage(db.Path(), uint64(bucketRootPageId))\n\trequire.NoError(t, err)\n\trequire.True(t, bucketRootPage.IsBranchPage())\n\n\tbucketRootPage.SetFlags(0)\n\n\terr = guts_cli.WritePage(db.Path(), bucketRootPageBuf)\n\trequire.NoError(t, err)\n}\n\n// mustGetBucketRootPage returns the root page for the provided bucket.\nfunc mustGetBucketRootPage(t testing.TB, db *bbolt.DB, bucketName []byte) common.Pgid {\n\tvar rootPageId common.Pgid\n\t_ = db.View(func(tx *bbolt.Tx) error {\n\t\tb := tx.Bucket(bucketName)\n\t\trequire.NotNil(t, b)\n\t\trootPageId = b.RootPage()\n\t\treturn nil\n\t})\n\n\treturn rootPageId\n}\n"
  },
  {
    "path": "tx_stats_test.go",
    "content": "package bbolt\n\nimport (\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc TestTxStats_add(t *testing.T) {\n\tstatsA := TxStats{\n\t\tPageCount:     1,\n\t\tPageAlloc:     2,\n\t\tCursorCount:   3,\n\t\tNodeCount:     100,\n\t\tNodeDeref:     101,\n\t\tRebalance:     1000,\n\t\tRebalanceTime: 1001 * time.Second,\n\t\tSplit:         10000,\n\t\tSpill:         10001,\n\t\tSpillTime:     10001 * time.Second,\n\t\tWrite:         100000,\n\t\tWriteTime:     100001 * time.Second,\n\t}\n\n\tstatsB := TxStats{\n\t\tPageCount:     2,\n\t\tPageAlloc:     3,\n\t\tCursorCount:   4,\n\t\tNodeCount:     101,\n\t\tNodeDeref:     102,\n\t\tRebalance:     1001,\n\t\tRebalanceTime: 1002 * time.Second,\n\t\tSplit:         11001,\n\t\tSpill:         11002,\n\t\tSpillTime:     11002 * time.Second,\n\t\tWrite:         110001,\n\t\tWriteTime:     110010 * time.Second,\n\t}\n\n\tstatsB.add(&statsA)\n\tassert.Equal(t, int64(3), statsB.GetPageCount())\n\tassert.Equal(t, int64(5), statsB.GetPageAlloc())\n\tassert.Equal(t, int64(7), statsB.GetCursorCount())\n\tassert.Equal(t, int64(201), statsB.GetNodeCount())\n\tassert.Equal(t, int64(203), statsB.GetNodeDeref())\n\tassert.Equal(t, int64(2001), statsB.GetRebalance())\n\tassert.Equal(t, 2003*time.Second, statsB.GetRebalanceTime())\n\tassert.Equal(t, int64(21001), statsB.GetSplit())\n\tassert.Equal(t, int64(21003), statsB.GetSpill())\n\tassert.Equal(t, 21003*time.Second, statsB.GetSpillTime())\n\tassert.Equal(t, int64(210001), statsB.GetWrite())\n\tassert.Equal(t, 210011*time.Second, statsB.GetWriteTime())\n}\n"
  },
  {
    "path": "tx_test.go",
    "content": "package bbolt_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\t\"runtime\"\n\t\"testing\"\n\t\"time\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/require\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\tberrors \"go.etcd.io/bbolt/errors\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\n// TestTx_Check_ReadOnly tests consistency checking on a ReadOnly database.\nfunc TestTx_Check_ReadOnly(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\treadOnlyDB, err := bolt.Open(db.Path(), 0600, &bolt.Options{ReadOnly: true})\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdefer readOnlyDB.Close()\n\n\ttx, err := readOnlyDB.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\t// ReadOnly DB will load freelist on Check call.\n\tnumChecks := 2\n\terrc := make(chan error, numChecks)\n\tcheck := func() {\n\t\terrc <- <-tx.Check()\n\t}\n\t// Ensure the freelist is not reloaded and does not race.\n\tfor i := 0; i < numChecks; i++ {\n\t\tgo check()\n\t}\n\tfor i := 0; i < numChecks; i++ {\n\t\tif err := <-errc; err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\t// Close the view transaction\n\terr = tx.Rollback()\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that committing a closed transaction returns an error.\nfunc TestTx_Commit_ErrTxClosed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif _, err := tx.CreateBucket([]byte(\"foo\")); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Commit(); err != berrors.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that rolling back a closed transaction returns an error.\nfunc TestTx_Rollback_ErrTxClosed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Rollback(); err != berrors.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that committing a read-only transaction returns an error.\nfunc TestTx_Commit_ErrTxNotWritable(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Commit(); err != berrors.ErrTxNotWritable {\n\t\tt.Fatal(err)\n\t}\n\t// Close the view transaction\n\terr = tx.Rollback()\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a transaction can retrieve a cursor on the root bucket.\nfunc TestTx_Cursor(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := tx.CreateBucket([]byte(\"woojits\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tc := tx.Cursor()\n\t\tif k, v := c.First(); !bytes.Equal(k, []byte(\"widgets\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\tif k, v := c.Next(); !bytes.Equal(k, []byte(\"woojits\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\tif k, v := c.Next(); k != nil {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", k)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that creating a bucket with a read-only transaction returns an error.\nfunc TestTx_CreateBucket_ErrTxNotWritable(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"foo\"))\n\t\tif err != berrors.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that creating a bucket on a closed transaction returns an error.\nfunc TestTx_CreateBucket_ErrTxClosed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif _, err := tx.CreateBucket([]byte(\"foo\")); err != berrors.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that a Tx can retrieve a bucket.\nfunc TestTx_Bucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx retrieving a non-existent key returns nil.\nfunc TestTx_Get_NotFound(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif b.Get([]byte(\"no_such_key\")) != nil {\n\t\t\tt.Fatal(\"expected nil value\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can be created and retrieved.\nfunc TestTx_CreateBucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Create a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if b == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read the bucket through a separate transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can be created if it doesn't already exist.\nfunc TestTx_CreateBucketIfNotExists(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create bucket.\n\t\tif b, err := tx.CreateBucketIfNotExists([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if b == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\n\t\t// Create bucket again.\n\t\tif b, err := tx.CreateBucketIfNotExists([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if b == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read the bucket through a separate transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure transaction returns an error if creating an unnamed bucket.\nfunc TestTx_CreateBucketIfNotExists_ErrBucketNameRequired(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucketIfNotExists([]byte{}); err != berrors.ErrBucketNameRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\n\t\tif _, err := tx.CreateBucketIfNotExists(nil); err != berrors.ErrBucketNameRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket cannot be created twice.\nfunc TestTx_CreateBucket_ErrBucketExists(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Create a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Create the same bucket again.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != berrors.ErrBucketExists {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket is created with a non-blank name.\nfunc TestTx_CreateBucket_ErrBucketNameRequired(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket(nil); err != berrors.ErrBucketNameRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can be deleted.\nfunc TestTx_DeleteBucket(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\t// Create a bucket and add a value.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Delete the bucket and make sure we can't get the value.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif tx.Bucket([]byte(\"widgets\")) != nil {\n\t\t\tt.Fatal(\"unexpected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create the bucket again and make sure there's not a phantom value.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatalf(\"unexpected phantom value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket on a closed transaction returns an error.\nfunc TestTx_DeleteBucket_ErrTxClosed(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.DeleteBucket([]byte(\"foo\")); err != berrors.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that deleting a bucket with a read-only transaction returns an error.\nfunc TestTx_DeleteBucket_ReadOnly(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"foo\")); err != berrors.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that nothing happens when deleting a bucket that doesn't exist.\nfunc TestTx_DeleteBucket_NotFound(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != berrors.ErrBucketNotFound {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that no error is returned when a tx.ForEach function does not return\n// an error.\nfunc TestTx_ForEach_NoError(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that an error is returned when a tx.ForEach function returns an error.\nfunc TestTx_ForEach_WithError(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tmarker := errors.New(\"marker\")\n\t\tif err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\treturn marker\n\t\t}); err != marker {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that Tx commit handlers are called after a transaction successfully commits.\nfunc TestTx_OnCommit(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar x int\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\ttx.OnCommit(func() { x += 1 })\n\t\ttx.OnCommit(func() { x += 2 })\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if x != 3 {\n\t\tt.Fatalf(\"unexpected x: %d\", x)\n\t}\n}\n\n// Ensure that Tx commit handlers are NOT called after a transaction rolls back.\nfunc TestTx_OnCommit_Rollback(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tvar x int\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\ttx.OnCommit(func() { x += 1 })\n\t\ttx.OnCommit(func() { x += 2 })\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn errors.New(\"rollback this commit\")\n\t}); err == nil || err.Error() != \"rollback this commit\" {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t} else if x != 0 {\n\t\tt.Fatalf(\"unexpected x: %d\", x)\n\t}\n}\n\n// Ensure that the database can be copied to a file path.\nfunc TestTx_CopyFile(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\n\tpath := tempfile()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.CopyFile(path, 0600)\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb2, err := bolt.Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db2.View(func(tx *bolt.Tx) error {\n\t\tif v := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\")); !bytes.Equal(v, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tif v := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"bat\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db2.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\ntype failWriterError struct{}\n\nfunc (failWriterError) Error() string {\n\treturn \"error injected for tests\"\n}\n\ntype failWriter struct {\n\t// fail after this many bytes\n\tAfter int\n}\n\nfunc (f *failWriter) Write(p []byte) (n int, err error) {\n\tn = len(p)\n\tif n > f.After {\n\t\tn = f.After\n\t\terr = failWriterError{}\n\t}\n\tf.After -= n\n\treturn n, err\n}\n\n// Ensure that Copy handles write errors right.\nfunc TestTx_CopyFile_Error_Meta(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.Copy(&failWriter{})\n\t}); err == nil || err.Error() != \"meta 0 copy: error injected for tests\" {\n\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t}\n}\n\n// Ensure that Copy handles write errors right.\nfunc TestTx_CopyFile_Error_Normal(t *testing.T) {\n\tdb := btesting.MustCreateDB(t)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.Copy(&failWriter{3 * db.Info().PageSize})\n\t}); err == nil || err.Error() != \"error injected for tests\" {\n\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t}\n}\n\n// TestTx_Rollback ensures there is no error when tx rollback whether we sync freelist or not.\nfunc TestTx_Rollback(t *testing.T) {\n\tfor _, isSyncFreelist := range []bool{false, true} {\n\t\t// Open the database.\n\t\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\t\tif err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tdefer os.Remove(db.Path())\n\t\tdb.NoFreelistSync = isSyncFreelist\n\n\t\ttx, err := db.Begin(true)\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"Error starting tx: %v\", err)\n\t\t}\n\t\tbucket := []byte(\"mybucket\")\n\t\tif _, err := tx.CreateBucket(bucket); err != nil {\n\t\t\tt.Fatalf(\"Error creating bucket: %v\", err)\n\t\t}\n\t\tif err := tx.Commit(); err != nil {\n\t\t\tt.Fatalf(\"Error on commit: %v\", err)\n\t\t}\n\n\t\ttx, err = db.Begin(true)\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"Error starting tx: %v\", err)\n\t\t}\n\t\tb := tx.Bucket(bucket)\n\t\tif err := b.Put([]byte(\"k\"), []byte(\"v\")); err != nil {\n\t\t\tt.Fatalf(\"Error on put: %v\", err)\n\t\t}\n\t\t// Imagine there is an error and tx needs to be rolled-back\n\t\tif err := tx.Rollback(); err != nil {\n\t\t\tt.Fatalf(\"Error on rollback: %v\", err)\n\t\t}\n\n\t\ttx, err = db.Begin(false)\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"Error starting tx: %v\", err)\n\t\t}\n\t\tb = tx.Bucket(bucket)\n\t\tif v := b.Get([]byte(\"k\")); v != nil {\n\t\t\tt.Fatalf(\"Value for k should not have been stored\")\n\t\t}\n\t\tif err := tx.Rollback(); err != nil {\n\t\t\tt.Fatalf(\"Error on rollback: %v\", err)\n\t\t}\n\n\t}\n}\n\n// TestTx_releaseRange ensures db.freePages handles page releases\n// correctly when there are transaction that are no longer reachable\n// via any read/write transactions and are \"between\" ongoing read\n// transactions, which requires they must be freed by\n// freelist.releaseRange.\nfunc TestTx_releaseRange(t *testing.T) {\n\t// Set initial mmap size well beyond the limit we will hit in this\n\t// test, since we are testing with long running read transactions\n\t// and will deadlock if db.grow is triggered.\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{InitialMmapSize: os.Getpagesize() * 100})\n\n\tbucket := \"bucket\"\n\n\tput := func(key, value string) {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(bucket))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn b.Put([]byte(key), []byte(value))\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tdel := func(key string) {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(bucket))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn b.Delete([]byte(key))\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tgetWithTxn := func(txn *bolt.Tx, key string) []byte {\n\t\treturn txn.Bucket([]byte(bucket)).Get([]byte(key))\n\t}\n\n\topenReadTxn := func() *bolt.Tx {\n\t\treadTx, err := db.Begin(false)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn readTx\n\t}\n\n\tcheckWithReadTxn := func(txn *bolt.Tx, key string, wantValue []byte) {\n\t\tvalue := getWithTxn(txn, key)\n\t\tif !bytes.Equal(value, wantValue) {\n\t\t\tt.Errorf(\"Wanted value to be %s for key %s, but got %s\", wantValue, key, string(value))\n\t\t}\n\t}\n\n\trollback := func(txn *bolt.Tx) {\n\t\tif err := txn.Rollback(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tput(\"k1\", \"v1\")\n\trtx1 := openReadTxn()\n\tput(\"k2\", \"v2\")\n\thold1 := openReadTxn()\n\tput(\"k3\", \"v3\")\n\thold2 := openReadTxn()\n\tdel(\"k3\")\n\trtx2 := openReadTxn()\n\tdel(\"k1\")\n\thold3 := openReadTxn()\n\tdel(\"k2\")\n\thold4 := openReadTxn()\n\tput(\"k4\", \"v4\")\n\thold5 := openReadTxn()\n\n\t// Close the read transactions we established to hold a portion of the pages in pending state.\n\trollback(hold1)\n\trollback(hold2)\n\trollback(hold3)\n\trollback(hold4)\n\trollback(hold5)\n\n\t// Execute a write transaction to trigger a releaseRange operation in the db\n\t// that will free multiple ranges between the remaining open read transactions, now that the\n\t// holds have been rolled back.\n\tput(\"k4\", \"v4\")\n\n\t// Check that all long running reads still read correct values.\n\tcheckWithReadTxn(rtx1, \"k1\", []byte(\"v1\"))\n\tcheckWithReadTxn(rtx2, \"k2\", []byte(\"v2\"))\n\trollback(rtx1)\n\trollback(rtx2)\n\n\t// Check that the final state is correct.\n\trtx7 := openReadTxn()\n\tcheckWithReadTxn(rtx7, \"k1\", nil)\n\tcheckWithReadTxn(rtx7, \"k2\", nil)\n\tcheckWithReadTxn(rtx7, \"k3\", nil)\n\tcheckWithReadTxn(rtx7, \"k4\", []byte(\"v4\"))\n\trollback(rtx7)\n}\n\nfunc ExampleTx_Rollback() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Create a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Set a value for a key.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\treturn tx.Bucket([]byte(\"widgets\")).Put([]byte(\"foo\"), []byte(\"bar\"))\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Update the key but rollback the transaction so it never saves.\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tb := tx.Bucket([]byte(\"widgets\"))\n\tif err := b.Put([]byte(\"foo\"), []byte(\"baz\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err := tx.Rollback(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Ensure that our original value is still set.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value for 'foo' is still: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value for 'foo' is still: bar\n}\n\nfunc ExampleTx_CopyFile() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Create a bucket and a key.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Copy the database to another file.\n\ttoFile := tempfile()\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.CopyFile(toFile, 0666)\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(toFile)\n\n\t// Open the cloned database.\n\tdb2, err := bolt.Open(toFile, 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Ensure that the key exists in the copy.\n\tif err := db2.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value for 'foo' in the clone is: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif err := db2.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value for 'foo' in the clone is: bar\n}\n\nfunc TestTxStats_GetAndIncAtomically(t *testing.T) {\n\tvar stats bolt.TxStats\n\n\tstats.IncPageCount(1)\n\tassert.Equal(t, int64(1), stats.GetPageCount())\n\n\tstats.IncPageAlloc(2)\n\tassert.Equal(t, int64(2), stats.GetPageAlloc())\n\n\tstats.IncCursorCount(3)\n\tassert.Equal(t, int64(3), stats.GetCursorCount())\n\n\tstats.IncNodeCount(100)\n\tassert.Equal(t, int64(100), stats.GetNodeCount())\n\n\tstats.IncNodeDeref(101)\n\tassert.Equal(t, int64(101), stats.GetNodeDeref())\n\n\tstats.IncRebalance(1000)\n\tassert.Equal(t, int64(1000), stats.GetRebalance())\n\n\tstats.IncRebalanceTime(1001 * time.Second)\n\tassert.Equal(t, 1001*time.Second, stats.GetRebalanceTime())\n\n\tstats.IncSplit(10000)\n\tassert.Equal(t, int64(10000), stats.GetSplit())\n\n\tstats.IncSpill(10001)\n\tassert.Equal(t, int64(10001), stats.GetSpill())\n\n\tstats.IncSpillTime(10001 * time.Second)\n\tassert.Equal(t, 10001*time.Second, stats.GetSpillTime())\n\n\tstats.IncWrite(100000)\n\tassert.Equal(t, int64(100000), stats.GetWrite())\n\n\tstats.IncWriteTime(100001 * time.Second)\n\tassert.Equal(t, 100001*time.Second, stats.GetWriteTime())\n\n\tassert.Equal(t,\n\t\tbolt.TxStats{\n\t\t\tPageCount:     1,\n\t\t\tPageAlloc:     2,\n\t\t\tCursorCount:   3,\n\t\t\tNodeCount:     100,\n\t\t\tNodeDeref:     101,\n\t\t\tRebalance:     1000,\n\t\t\tRebalanceTime: 1001 * time.Second,\n\t\t\tSplit:         10000,\n\t\t\tSpill:         10001,\n\t\t\tSpillTime:     10001 * time.Second,\n\t\t\tWrite:         100000,\n\t\t\tWriteTime:     100001 * time.Second,\n\t\t},\n\t\tstats,\n\t)\n}\n\nfunc TestTxStats_Sub(t *testing.T) {\n\tstatsA := bolt.TxStats{\n\t\tPageCount:     1,\n\t\tPageAlloc:     2,\n\t\tCursorCount:   3,\n\t\tNodeCount:     100,\n\t\tNodeDeref:     101,\n\t\tRebalance:     1000,\n\t\tRebalanceTime: 1001 * time.Second,\n\t\tSplit:         10000,\n\t\tSpill:         10001,\n\t\tSpillTime:     10001 * time.Second,\n\t\tWrite:         100000,\n\t\tWriteTime:     100001 * time.Second,\n\t}\n\n\tstatsB := bolt.TxStats{\n\t\tPageCount:     2,\n\t\tPageAlloc:     3,\n\t\tCursorCount:   4,\n\t\tNodeCount:     101,\n\t\tNodeDeref:     102,\n\t\tRebalance:     1001,\n\t\tRebalanceTime: 1002 * time.Second,\n\t\tSplit:         11001,\n\t\tSpill:         11002,\n\t\tSpillTime:     11002 * time.Second,\n\t\tWrite:         110001,\n\t\tWriteTime:     110010 * time.Second,\n\t}\n\n\tdiff := statsB.Sub(&statsA)\n\tassert.Equal(t, int64(1), diff.GetPageCount())\n\tassert.Equal(t, int64(1), diff.GetPageAlloc())\n\tassert.Equal(t, int64(1), diff.GetCursorCount())\n\tassert.Equal(t, int64(1), diff.GetNodeCount())\n\tassert.Equal(t, int64(1), diff.GetNodeDeref())\n\tassert.Equal(t, int64(1), diff.GetRebalance())\n\tassert.Equal(t, time.Second, diff.GetRebalanceTime())\n\tassert.Equal(t, int64(1001), diff.GetSplit())\n\tassert.Equal(t, int64(1001), diff.GetSpill())\n\tassert.Equal(t, 1001*time.Second, diff.GetSpillTime())\n\tassert.Equal(t, int64(10001), diff.GetWrite())\n\tassert.Equal(t, 10009*time.Second, diff.GetWriteTime())\n}\n\n// TestTx_TruncateBeforeWrite ensures the file is truncated ahead whether we sync freelist or not.\nfunc TestTx_TruncateBeforeWrite(t *testing.T) {\n\tif runtime.GOOS == \"windows\" {\n\t\treturn\n\t}\n\tfor _, isSyncFreelist := range []bool{false, true} {\n\t\tt.Run(fmt.Sprintf(\"isSyncFreelist:%v\", isSyncFreelist), func(t *testing.T) {\n\t\t\t// Open the database.\n\t\t\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{\n\t\t\t\tNoFreelistSync: isSyncFreelist,\n\t\t\t})\n\n\t\t\tbigvalue := make([]byte, db.AllocSize/100)\n\t\t\tcount := 0\n\t\t\tfor {\n\t\t\t\tcount++\n\t\t\t\ttx, err := db.Begin(true)\n\t\t\t\trequire.NoError(t, err)\n\t\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"bucket\"))\n\t\t\t\trequire.NoError(t, err)\n\t\t\t\terr = b.Put([]byte{byte(count)}, bigvalue)\n\t\t\t\trequire.NoError(t, err)\n\t\t\t\terr = tx.Commit()\n\t\t\t\trequire.NoError(t, err)\n\n\t\t\t\tsize := fileSize(db.Path())\n\n\t\t\t\tif size > int64(db.AllocSize) && size < int64(db.AllocSize)*2 {\n\t\t\t\t\t// db.grow expands the file aggresively, that double the size while smaller than db.AllocSize,\n\t\t\t\t\t// or increase with a step of db.AllocSize if larger, by which we can test if db.grow has run.\n\t\t\t\t\tt.Fatalf(\"db.grow doesn't run when file size changes. file size: %d\", size)\n\t\t\t\t}\n\t\t\t\tif size > int64(db.AllocSize) {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t\tdb.MustClose()\n\t\t\tdb.MustDeleteFile()\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "unix_test.go",
    "content": "//go:build !windows\n\npackage bbolt_test\n\nimport (\n\t\"fmt\"\n\t\"testing\"\n\n\t\"golang.org/x/sys/unix\"\n\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/btesting\"\n)\n\nfunc TestMlock_DbOpen(t *testing.T) {\n\t// 32KB\n\tskipOnMemlockLimitBelow(t, 32*1024)\n\n\tbtesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true})\n}\n\n// Test change between \"empty\" (16KB) and \"non-empty\" db\nfunc TestMlock_DbCanGrow_Small(t *testing.T) {\n\t// 32KB\n\tskipOnMemlockLimitBelow(t, 32*1024)\n\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true})\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"bucket\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tkey := []byte(\"key\")\n\t\tvalue := []byte(\"value\")\n\t\tif err := b.Put(key, value); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n}\n\n// Test crossing of 16MB (AllocSize) of db size\nfunc TestMlock_DbCanGrow_Big(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode\")\n\t}\n\n\t// 32MB\n\tskipOnMemlockLimitBelow(t, 32*1024*1024)\n\n\tchunksBefore := 64\n\tchunksAfter := 64\n\n\tdb := btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true})\n\n\tfor chunk := 0; chunk < chunksBefore; chunk++ {\n\t\tinsertChunk(t, db, chunk)\n\t}\n\tdbSize := fileSize(db.Path())\n\n\tfor chunk := 0; chunk < chunksAfter; chunk++ {\n\t\tinsertChunk(t, db, chunksBefore+chunk)\n\t}\n\tnewDbSize := fileSize(db.Path())\n\n\tif newDbSize <= dbSize {\n\t\tt.Errorf(\"db didn't grow: %v <= %v\", newDbSize, dbSize)\n\t}\n}\n\nfunc insertChunk(t *testing.T, db *btesting.DB, chunkId int) {\n\tchunkSize := 1024\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"bucket\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < chunkSize; i++ {\n\t\t\tkey := []byte(fmt.Sprintf(\"key-%d-%d\", chunkId, i))\n\t\t\tvalue := []byte(\"value\")\n\t\t\tif err := b.Put(key, value); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Main reason for this check is travis limiting mlockable memory to 64KB\n// https://github.com/travis-ci/travis-ci/issues/2462\nfunc skipOnMemlockLimitBelow(t *testing.T, memlockLimitRequest uint64) {\n\tvar info unix.Rlimit\n\tif err := unix.Getrlimit(unix.RLIMIT_MEMLOCK, &info); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif info.Cur < memlockLimitRequest {\n\t\tt.Skipf(\n\t\t\t\"skipping as RLIMIT_MEMLOCK is insufficient: %v < %v\",\n\t\t\tinfo.Cur,\n\t\t\tmemlockLimitRequest,\n\t\t)\n\t}\n}\n"
  },
  {
    "path": "utils_test.go",
    "content": "package bbolt_test\n\nimport (\n\tbolt \"go.etcd.io/bbolt\"\n\t\"go.etcd.io/bbolt/internal/common\"\n)\n\n// `dumpBucket` dumps all the data, including both key/value data\n// and child buckets, from the source bucket into the target db file.\nfunc dumpBucket(srcBucketName []byte, srcBucket *bolt.Bucket, dstFilename string) error {\n\tcommon.Assert(len(srcBucketName) != 0, \"source bucket name can't be empty\")\n\tcommon.Assert(srcBucket != nil, \"the source bucket can't be nil\")\n\tcommon.Assert(len(dstFilename) != 0, \"the target file path can't be empty\")\n\n\tdstDB, err := bolt.Open(dstFilename, 0600, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer dstDB.Close()\n\n\treturn dstDB.Update(func(tx *bolt.Tx) error {\n\t\tdstBucket, err := tx.CreateBucket(srcBucketName)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn cloneBucket(srcBucket, dstBucket)\n\t})\n}\n\nfunc cloneBucket(src *bolt.Bucket, dst *bolt.Bucket) error {\n\treturn src.ForEach(func(k, v []byte) error {\n\t\tif v == nil {\n\t\t\tsrcChild := src.Bucket(k)\n\t\t\tdstChild, err := dst.CreateBucket(k)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err = dstChild.SetSequence(srcChild.Sequence()); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\treturn cloneBucket(srcChild, dstChild)\n\t\t}\n\n\t\treturn dst.Put(k, v)\n\t})\n}\n"
  },
  {
    "path": "version/version.go",
    "content": "package version\n\nvar (\n\t// Version shows the last bbolt binary version released.\n\tVersion = \"1.4.0-alpha.0\"\n)\n"
  }
]