Skip to content

Commit

Permalink
Compatible with RocksDB v6.16, tecbot#203
Browse files Browse the repository at this point in the history
  • Loading branch information
flier committed Mar 22, 2021
1 parent f0fad39 commit 567cc51
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 128 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Continuous integration

on: [push, pull_request]

defaults:
run:
shell: bash

jobs:
test:
strategy:
matrix:
os: [ubuntu-20.04, ubuntu-18.04, macos-11.0, macos-10.15]
go: [1.16.x, 1.15.x, 1.14.x, 1.13.x]
name: Go ${{ matrix.go }} tests @ ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Install Linux dependencies
if: startsWith(matrix.os, 'ubuntu-')
run: |
sudo apt-get update
sudo apt-get install -yq librocksdb-dev
- name: Install MacOS dependencies
if: startsWith(matrix.os, 'macos-')
run: |
brew install rocksdb
- name: Install Golang ${{ matrix.go }}
id: install-golang
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- run: go version

- name: Checkout code
uses: actions/checkout@v2

- name: Cache Golang modules
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test v5
if: startsWith(matrix.os, 'ubuntu-')
run: go test -v

- name: Test v6.16 or later
if: startsWith(matrix.os, 'macos-')
run: go test -v -tags rocksdb_6_16

golangci:
name: lint
runs-on: ubuntu-20.04
steps:
- name: Install Linux dependencies
run: |
sudo apt-get update
sudo apt-get install -yq librocksdb-dev
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.38
2 changes: 1 addition & 1 deletion checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestCheckpoint(t *testing.T) {
opts := NewDefaultOptions()
opts.SetCreateIfMissing(true)
dbCheck, err = OpenDb(opts, dir)
defer dbCheck.Close()
ensure.Nil(t, err)
defer dbCheck.Close()

// test keys
var value *Slice
Expand Down
83 changes: 0 additions & 83 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,89 +577,6 @@ func (db *DB) DropColumnFamily(c *ColumnFamilyHandle) error {
return nil
}

// GetApproximateSizes returns the approximate number of bytes of file system
// space used by one or more key ranges.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizes(ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
}

cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = (*C.char)(C.CBytes(r.Start))
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = (*C.char)(C.CBytes(r.Limit))
cLimitLens[i] = C.size_t(len(r.Limit))
}

defer func() {
for i := range ranges {
C.free(unsafe.Pointer(cStarts[i]))
C.free(unsafe.Pointer(cLimits[i]))
}
}()

C.rocksdb_approximate_sizes(
db.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))

return sizes
}

// GetApproximateSizesCF returns the approximate number of bytes of file system
// space used by one or more key ranges in the column family.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizesCF(cf *ColumnFamilyHandle, ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
}

cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = (*C.char)(C.CBytes(r.Start))
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = (*C.char)(C.CBytes(r.Limit))
cLimitLens[i] = C.size_t(len(r.Limit))
}

defer func() {
for i := range ranges {
C.free(unsafe.Pointer(cStarts[i]))
C.free(unsafe.Pointer(cLimits[i]))
}
}()

C.rocksdb_approximate_sizes_cf(
db.c,
cf.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))

return sizes
}

// SetOptions dynamically changes options through the SetOptions API.
func (db *DB) SetOptions(keys, values []string) error {
num_keys := len(keys)
Expand Down
91 changes: 91 additions & 0 deletions db_6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// +build !rocksdb_6_16

package gorocksdb

// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import "unsafe"

// GetApproximateSizes returns the approximate number of bytes of file system
// space used by one or more key ranges.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizes(ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
}

cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = (*C.char)(C.CBytes(r.Start))
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = (*C.char)(C.CBytes(r.Limit))
cLimitLens[i] = C.size_t(len(r.Limit))
}

defer func() {
for i := range ranges {
C.free(unsafe.Pointer(cStarts[i]))
C.free(unsafe.Pointer(cLimits[i]))
}
}()

C.rocksdb_approximate_sizes(
db.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))

return sizes
}

// GetApproximateSizesCF returns the approximate number of bytes of file system
// space used by one or more key ranges in the column family.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizesCF(cf *ColumnFamilyHandle, ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
}

cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = (*C.char)(C.CBytes(r.Start))
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = (*C.char)(C.CBytes(r.Limit))
cLimitLens[i] = C.size_t(len(r.Limit))
}

defer func() {
for i := range ranges {
C.free(unsafe.Pointer(cStarts[i]))
C.free(unsafe.Pointer(cLimits[i]))
}
}()

C.rocksdb_approximate_sizes_cf(
db.c,
cf.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))

return sizes
}
108 changes: 108 additions & 0 deletions db_6_16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// +build rocksdb_6_16

package gorocksdb

// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"unsafe"
)

// GetApproximateSizes returns the approximate number of bytes of file system
// space used by one or more key ranges.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizes(ranges []Range) (sizes []uint64, err error) {
var cErr *C.char
sizes = make([]uint64, len(ranges))
if len(ranges) == 0 {
return
}

cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = (*C.char)(C.CBytes(r.Start))
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = (*C.char)(C.CBytes(r.Limit))
cLimitLens[i] = C.size_t(len(r.Limit))
}

defer func() {
for i := range ranges {
C.free(unsafe.Pointer(cStarts[i]))
C.free(unsafe.Pointer(cLimits[i]))
}
}()

C.rocksdb_approximate_sizes(
db.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]),
&cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
err = errors.New(C.GoString(cErr))
}

return
}

// GetApproximateSizesCF returns the approximate number of bytes of file system
// space used by one or more key ranges in the column family.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizesCF(cf *ColumnFamilyHandle, ranges []Range) (sizes []uint64, err error) {
var cErr *C.char
sizes = make([]uint64, len(ranges))
if len(ranges) == 0 {
return
}

cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = (*C.char)(C.CBytes(r.Start))
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = (*C.char)(C.CBytes(r.Limit))
cLimitLens[i] = C.size_t(len(r.Limit))
}

defer func() {
for i := range ranges {
C.free(unsafe.Pointer(cStarts[i]))
C.free(unsafe.Pointer(cLimits[i]))
}
}()

C.rocksdb_approximate_sizes_cf(
db.c,
cf.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]),
&cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
err = errors.New(C.GoString(cErr))
}

return
}
Loading

0 comments on commit 567cc51

Please sign in to comment.