Browse Source

Add non-sequence options

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main v0.0.3
jolheiser 1 year ago
parent
commit
54c787c45a
No known key found for this signature in database GPG Key ID: 83E486E71AFEB820
  1. 5
      .gitignore
  2. 7
      Makefile
  3. 9
      bucket.go
  4. 52
      example_key_test.go
  5. 18
      example_seq_test.go
  6. 20
      store.go
  7. 46
      store_test.go

5
.gitignore

@ -1,2 +1,5 @@
# GoLand
.idea/
.idea/
# Code Coverage
coverage.out

7
Makefile

@ -10,4 +10,9 @@ test:
.PHONY: vet
vet:
$(GO) vet ./...
$(GO) vet ./...
.PHONY: coverage
coverage:
$(GO) test --coverprofile=coverage.out
$(GO) tool cover --html=coverage.out

9
bucket.go

@ -11,9 +11,14 @@ func (b *Bucket) Put(val Sequencer) error {
return b.DB.Put(b.Name, val)
}
// PutWithKey adds a new value with the specified key to the bucket
func (b *Bucket) PutWithKey(key, val interface{}) error {
return b.DB.PutWithKey(b.Name, key, val)
}
// Get returns a value from the bucket with the specified sequence ID
func (b *Bucket) Get(seq uint64, val interface{}) error {
return b.DB.Get(b.Name, seq, val)
func (b *Bucket) Get(id, val interface{}) error {
return b.DB.Get(b.Name, id, val)
}
// Count returns the number of objects in the bucket

52
example_key_test.go

@ -0,0 +1,52 @@
package shock
import (
"fmt"
"io/ioutil"
"os"
"path"
)
// TestUserSettings object
type TestUserSettings struct {
UserID uint64 `json:"user_id"`
Theme string `json:"theme"`
}
func ExampleTestUserSettings() {
dir, err := ioutil.TempDir(os.TempDir(), "shock")
if err != nil {
panic(err)
}
dbPath := path.Join(dir, "shock.db")
db, err = Open(dbPath, os.ModePerm, DefaultOptions)
if err != nil {
panic(err)
}
// Get a bucket to work with instead of specifying each time
bucket := db.Bucket("test-user-settings")
// Add a new TestUserSettings
t := &TestUserSettings{
UserID: 1,
Theme: "dark",
}
if err := bucket.PutWithKey(t.UserID, t); err != nil {
panic(err)
}
// Get the TestUser from the DB later (via sequence ID)
var tt TestUserSettings
if err := bucket.Get(t.UserID, &tt); err != nil {
panic(err)
}
if err := db.Bolt.Close(); err != nil {
fmt.Printf("Could not close DB %s: %v\n", dbPath, err)
}
if err := os.RemoveAll(dir); err != nil {
fmt.Printf("Could not delete temp dir %s: %v\n", dir, err)
}
}

18
example_test.go → example_seq_test.go

@ -7,8 +7,8 @@ import (
"path"
)
// Test object, perhaps a User
type Test struct {
// TestUser object
type TestUser struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
@ -16,11 +16,11 @@ type Test struct {
}
// Implement the interface for Sequencer
func (t *Test) AssignSeq(seq uint64) {
func (t *TestUser) AssignSeq(seq uint64) {
t.ID = seq
}
func Example() {
func ExampleTestUser() {
dir, err := ioutil.TempDir(os.TempDir(), "shock")
if err != nil {
panic(err)
@ -33,10 +33,10 @@ func Example() {
}
// Get a bucket to work with instead of specifying each time
bucket := db.Bucket("test")
bucket := db.Bucket("test-user")
// Add a new Test user
t := &Test{
// Add a new TestUser
t := &TestUser{
Name: "Tester123",
Age: 25,
Admin: true,
@ -45,8 +45,8 @@ func Example() {
panic(err)
}
// Get the Test user from the DB later (via sequence ID)
var tt Test
// Get the TestUser from the DB later (via sequence ID)
var tt TestUser
if err := bucket.Get(t.ID, &tt); err != nil {
panic(err)
}

20
store.go

@ -33,13 +33,29 @@ func (d *DB) Put(bucket string, val Sequencer) error {
})
}
// PutWithKey adds a new value to the bucket with a defined key
func (d *DB) PutWithKey(bucket string, key, val interface{}) error {
if err := d.initBucket(bucket); err != nil {
return err
}
return d.Bolt.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucket))
serial, err := d.Serializer.Marshal(val)
if err != nil {
return err
}
return b.Put([]byte(fmt.Sprintf("%v", key)), serial)
})
}
// Get returns a value from a bucket with the specified sequence ID
func (d *DB) Get(bucket string, seq uint64, val interface{}) error {
func (d *DB) Get(bucket string, id, val interface{}) error {
if err := d.initBucket(bucket); err != nil {
return err
}
if err := d.Bolt.View(func(tx *bbolt.Tx) error {
serial := tx.Bucket([]byte(bucket)).Get([]byte(fmt.Sprintf("%d", seq)))
serial := tx.Bucket([]byte(bucket)).Get([]byte(fmt.Sprintf("%v", id)))
return d.Serializer.Unmarshal(serial, val)
}); err != nil {
return err

46
store_test.go

@ -6,7 +6,7 @@ import (
func TestStore(t *testing.T) {
tt := []*Test{
tt := []*TestUser{
{
Name: "user1",
Age: 25,
@ -24,6 +24,17 @@ func TestStore(t *testing.T) {
},
}
ttt := []*TestUserSettings{
{
UserID: 1,
Theme: "light",
},
{
UserID: 2,
Theme: "dark",
},
}
bucket := db.Bucket("test")
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
@ -32,7 +43,7 @@ func TestStore(t *testing.T) {
t.FailNow()
}
var tcc Test
var tcc TestUser
err := bucket.Get(tc.ID, &tcc)
if err != nil {
t.Log(err)
@ -60,9 +71,9 @@ func TestStore(t *testing.T) {
})
t.Run("list", func(t *testing.T) {
list := make([]Test, 0, len(tt))
list := make([]TestUser, 0, len(tt))
if err := bucket.ViewEach(func(_, serial []byte) error {
var t Test
var t TestUser
if err := db.Serializer.Unmarshal(serial, &t); err != nil {
return err
}
@ -80,9 +91,34 @@ func TestStore(t *testing.T) {
}
}
})
for _, tc := range ttt {
t.Run(tc.Theme, func(t *testing.T) {
if err := bucket.PutWithKey(tc.UserID, tc); err != nil {
t.Log(err)
t.FailNow()
}
var tcc TestUserSettings
err := bucket.Get(tc.UserID, &tcc)
if err != nil {
t.Log(err)
t.FailNow()
}
if !tcc.Equal(*tc) {
t.Log("Serialized struct is not the same")
t.FailNow()
}
})
}
}
func (t Test) Equal(tt Test) bool {
func (t TestUser) Equal(tt TestUser) bool {
return t.ID == tt.ID && t.Name == tt.Name &&
t.Age == tt.Age && t.Admin == tt.Admin
}
func (t TestUserSettings) Equal(tt TestUserSettings) bool {
return t.UserID == tt.UserID && t.Theme == tt.Theme
}

Loading…
Cancel
Save