lokaldb is a wrapper around bbolt key-value database to manage messaging data in a local database for the Go programming language.
Its primary purpose is to persist messages if sending messages to queues like NATS fails.
This is an initial version.
LokalDB is a wrapper around bbolt key-value database to manage messaging data in a local database.
type LokalDB struct {
ldb *bolt.DB
FileName string
}
ChunkData represents the key-value chunks of data to be used as a result for slices of key-value data.
type ChunkData struct {
Key string
Value []byte
}
Opens a local database file. It creates the file if it does not exist.
// Open a new lokaldb instance
db, err := lokaldb.Open(`test.db`)
if err != nil {
log.Fatalf("%e", err)
}
Inserts data in the local database. It will update records containing the same key with the current value.
// Store a George in the key `name`
err = db.Store(`default`, `name`, []byte(`George`))
if err != nil {
log.Println(err.Error())
}
Inserts data in the local database in one go. It will update records containing the same key with the current value.
kv := []ChunkData {
ChunkData {
Key: `beatle1`,
Value: `John`,
},
ChunkData {
Key: `beatle2`,
Value: `Paul`,
},
ChunkData {
Key: `beatle3`,
Value: `George`,
},
ChunkData {
Key: `beatle4`,
Value: `Ringo`,
},
}
err = db.StoreOnce(`default`, kv)
if err != nil {
log.Fatalf("%e", err)
}
Gets a single record from the local database with the provided key. If the record does not exist, it will return nil.
b, err = db.Fetch(`default`, `beatle1`)
Removes a single record in the database that matches the provided key.
err = db.Delete(`default`, key)
if err != nil {
log.Fatalf("%e", err)
}
Remove records in the local database in one go from a supplied provided key.
ks := []string{
`beatle1`,
`beatle2`,
`beatle3`,
`beatle4`,
}
err = db.DeleteOnce(`default`, ks)
if err != nil {
log.Fatalf("%e", err)
}
Gets a chunk of data starting from the bottom to top limited by max.
// Get 10 records from bottom to top, no offset
kv1, err = db.FetchChunkUp(`default`, 10, 0)
Gets a chunk of data starting from the top to bottom limited by max.
// Get 10 records from top to bottom, no offset
kv1, err = db.FetchChunkDown(`default`, 10, 0)
Gets the record with the provided key and deletes it.
bf, err = db.FetchDelete(`default`, `nuisance`)
if err != nil {
log.Fatalf("%e", err)
}
Fetches and deletes a record from bottom to top.
b, err = db.SliceUp(`default`)
if err != nil {
log.Fatalf("%e", err)
}
Fetches and deletes a record from top to bottom.
b, err = db.SliceDown(`default`)
if err != nil {
log.Fatalf("%e", err)
}
Gets a chunk of data starting from bottom to top in descending order and removes them.
b, err = db.CutChunkUp(`default`, 50)
if err != nil {
log.Fatalf("%e", err)
}
Gets a chunk of data starting from top to bottom in ascending order and removes them.
b, err = db.CutChunkDown(`default`, 50)
if err != nil {
log.Fatalf("%e", err)
}
Count records in the bucket
Close the local database
Please see more examples of using lokaldb in your projects on the lokaldb_test.go
file.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.