Verified Commit 1d533fca authored by AtjonTV's avatar AtjonTV
Browse files

**BREAKING**: All() returns [][]byte, use ToString()

parent 7055e5a2
# Go-Database
This is a unified wrapper interface for GoLevelDB and Bitcask.
# Install
Use `go get`
```
go get gitlab.atvg-studios.com/atvg-studios/go-database
```
Use `go.mod`
```
require gitlab.atvg-studios.com/atvg-studios/go-database v2.0.0
```
# Using
This is the current Interface
```go
package app
import "gitlab.atvg-studios.com/atvg-studios/go-database/database"
func main() {
// Create Bitcask Database
db, err1 := database.New(database.USE_BITCASK, "/tmp/bitcask.db")
// Create LevelDB Database
db, err2 := database.New(database.USE_LEVELDB, "/tmp/leveldb.db")
// Put key-value into database
wasInserted, err3 := db.Put([]byte("Key"), []byte("value"))
// Check for key
hasKey, err4 := db.Has([]byte("Key"))
// Get value
value, err5 := db.Get([]byte("Key"))
// Convert to String
jsonArray, err6 := db.ToString()
// Bitcask Only
err7 := db.Merge()
// Get expected KeyCount (can be wrong after Merge()!!)
count := db.Stats.KeyCount
// Close database
err8 := db.Close()
}
```
# Changes
## [2.0.0] (12th Sep 2019)
### Added
* `Database.ToString` returns data as String (Format: `[<data1>,<data2>]`)
* `DStats` struct to count how many keys are in the database (for GoLevelDB)
* `Locks.BitcaskLock` a Mutex for Bitcask (I know it has its own, but lets be consistent for all backends)
### Changed
* Exported `Locks` to own file
* `Database.All` returns `([][]byte, error)` **!BREAKING!**
## [1.0.0] (11th Sep 2019)
### Added
* `Database` wrapper interface for GoLevelDB and Bitcask
* `Locks` holder struct for Mutexes
\ No newline at end of file
......@@ -5,7 +5,6 @@ import (
"github.com/syndtr/goleveldb/leveldb"
"log"
"strings"
"sync"
)
const (
......@@ -13,14 +12,11 @@ const (
USE_BITCASK = 2
)
type Locks struct {
LeveldbLock sync.Mutex
}
type Database struct {
ActiveDB int
leveldb *leveldb.DB
bitcask *bitcask.Bitcask
Stats DStats
Locks Locks
}
......@@ -45,52 +41,76 @@ func New(db int, path string) (Database, error) {
}
database.ActiveDB = db
database.Stats = DStats{}
return database, err
}
func (db Database) All() (string, error) {
func (db Database) All() ([][]byte, error) {
var err error
var data [][]byte
if db.ActiveDB == USE_BITCASK {
var data strings.Builder
stats, err1 := db.bitcask.Stats()
if err1 != nil {
return "", err1
return data, err1
}
length := stats.Keys
data = make([][]byte,length)
var count int
var blob []byte
for key := range db.bitcask.Keys() {
blob, err = db.Get(key)
if count < length {
data.Write(blob)
} else {
data.Write(blob)
data.WriteString(",")
}
data[count] = blob
count++
}
return data.String(), err
return data, err
}
if db.ActiveDB == USE_LEVELDB {
var data strings.Builder
iter := db.leveldb.NewIterator(nil, nil)
length := db.Stats.KeyCount
data = make([][]byte, length)
var count int
for iter.Next() {
if iter.Last() {
data.Write(iter.Value())
data.WriteString(",")
data[count] = iter.Value()
count++
}
}
return data, err
}
func (db Database) ToString() (string, error) {
var str strings.Builder
var data [][]byte
var err error
data, err = db.All()
str.WriteString("[")
var i int
var blob []byte
for j := 0; j < len(data); j++ {
for i, blob = range data {
if i < len(data[j]) {
str.Write(blob)
str.WriteString(",")
} else {
data.Write(iter.Value())
str.Write(blob)
}
}
}
return "", err
str.WriteString("]")
return str.String(), err
}
func (db Database) Put(key, value []byte) (bool, error) {
var err error
var has bool
......@@ -98,7 +118,12 @@ func (db Database) Put(key, value []byte) (bool, error) {
has, err = db.Has(key)
if db.ActiveDB == USE_BITCASK {
if !has {
db.Locks.BitcaskLock.Lock()
err = db.bitcask.Put(key, value)
db.Stats.incKeyCount(err)
db.Locks.BitcaskLock.Unlock()
has = true
}
}
......@@ -106,7 +131,10 @@ func (db Database) Put(key, value []byte) (bool, error) {
if db.ActiveDB == USE_LEVELDB {
if !has {
db.Locks.LeveldbLock.Lock()
err = db.leveldb.Put(key, value, nil)
db.Stats.incKeyCount(err)
db.Locks.LeveldbLock.Unlock()
has = true
}
......@@ -114,6 +142,7 @@ func (db Database) Put(key, value []byte) (bool, error) {
return has, err
}
func (db Database) Has(key []byte) (bool, error) {
var err error
var ok bool
......@@ -128,6 +157,7 @@ func (db Database) Has(key []byte) (bool, error) {
return ok, err
}
func (db Database) Get(key []byte) ([]byte, error) {
var err error
var ok bool
......@@ -149,6 +179,7 @@ func (db Database) Get(key []byte) ([]byte, error) {
return data, err
}
func (db Database) Merge() error {
if db.ActiveDB == USE_BITCASK {
log.Println("Database::Cleanup::BitCask")
......@@ -156,6 +187,7 @@ func (db Database) Merge() error {
}
return nil
}
func (db Database) Close() error {
if db.ActiveDB == USE_BITCASK {
log.Println("Database::Close::BitCask")
......
package database
type DStats struct {
KeyCount uint64
}
func (ds DStats) incKeyCount(err error) {
if err != nil {
return
}
ds.KeyCount++
}
\ No newline at end of file
package database
import "sync"
type Locks struct {
BitcaskLock sync.Mutex
LeveldbLock sync.Mutex
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment