Skip to content

Commit

Permalink
wip: db
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Nov 26, 2021
1 parent 4c926db commit 8a8124c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 20 deletions.
92 changes: 92 additions & 0 deletions vdir/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package vdir

import (
"fmt"
"os"
"path/filepath"

"github.com/boltdb/bolt"
)

var bucketName = "todos"

func dbPath() (dbPath string, err error) {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return "", fmt.Errorf("get cache dir: %w", err)
}
cacheDir := filepath.Join(userCacheDir, "tdx")
if err := os.MkdirAll(cacheDir, 0700); err != nil {
return "", fmt.Errorf("create cache dir: %w", err)
}
dbPath = filepath.Join(cacheDir, "db")
return
}

func updateDB() error {
path, err := dbPath()
if err != nil {
return err
}

db, err := bolt.Open(path, 0644, nil)
if err != nil {
return fmt.Errorf("open db: %w", err)
}
defer db.Close()

if err != nil {
return err
}

key := []byte("hey")
val := []byte("world")

err = db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName))
if err != nil {
return err
}

err = bucket.Put(key, val)
if err != nil {
return err
}
return nil
})

return nil
}

func viewDB() (string, error) {
path, err := dbPath()
if err != nil {
return "", err
}

db, err := bolt.Open(path, 0644, nil)
if err != nil {
return "", fmt.Errorf("open db: %w", err)
}
defer db.Close()

key := []byte("hey")
var val []byte

err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(bucketName))
if bucket == nil {
return fmt.Errorf("Bucket %q not found", bucketName)
}
val = bucket.Get(key)

return nil
})

if err != nil {
return "", err
}
fmt.Println(string(key), string(val))

return string(val), nil
}
2 changes: 1 addition & 1 deletion vdir/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestTags(t *testing.T) {
path string
want []Tag
}{
{vdpath, []Tag{"#Quebec", "#1some", "#tags", "#go", "#sway", "#Later"}},
{vdpath, []Tag{"#quebec", "#1some", "#tags", "#go", "#sway", "#later"}},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
Expand Down
19 changes: 0 additions & 19 deletions vdir/vdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,12 @@ import (
"os"
"path/filepath"

"github.com/boltdb/bolt"
"github.com/emersion/go-ical"
)

// Vdir is a map of all collections and items
type Vdir map[*Collection][]*Item

func initDB(opts *bolt.Options) (*bolt.DB, error) {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return nil, fmt.Errorf("get cache dir: %w", err)
}
cacheDir := filepath.Join(userCacheDir, "tdx")
if err := os.MkdirAll(cacheDir, 0700); err != nil {
return nil, fmt.Errorf("create cache dir: %w", err)
}
dbPath := filepath.Join(cacheDir, "db")
db, err := bolt.Open(dbPath, 0644, nil)
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
}

return db, nil
}

// Init initializes the map with collections and items in path, items have unique IDs
func (v *Vdir) Init(path string) error {
f, err := os.Stat(path)
Expand Down
18 changes: 18 additions & 0 deletions vdir/vdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ func TestInit(t *testing.T) {
})
}
}

func TestUpdateDB(t *testing.T) {
t.Run("", func(t *testing.T) {
err := updateDB()
if err != nil {
t.Fatal(err)
}
})
}

func TestViewDB(t *testing.T) {
t.Run("", func(t *testing.T) {
_, err := viewDB()
if err != nil {
t.Fatal(err)
}
})
}

0 comments on commit 8a8124c

Please sign in to comment.