Skip to content

Commit

Permalink
chore: Refactor storage to simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
diiyw committed Aug 13, 2024
1 parent 7f3631d commit c3ddcc8
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 341 deletions.
8 changes: 8 additions & 0 deletions ds/ds.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package ds

import (
"errors"
)

var (
ErrCorruptedData = errors.New("corrupted data")
)

type Key struct {
Name string
Expiration int64
Expand Down
3 changes: 1 addition & 2 deletions examples/watch/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"fmt"

"github.com/diiyw/nodis"
"github.com/diiyw/nodis/fs"
"github.com/diiyw/nodis/patch"
)

func main() {
var opt = nodis.DefaultOptions
opt.Filesystem = &fs.Memory{}
opt.Synchronizer = nodis.NewWebsocket()
n := nodis.Open(opt)
n.WatchKey([]string{"*"}, func(op patch.Op) {
Expand Down
5 changes: 3 additions & 2 deletions examples/watch/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"time"

"github.com/diiyw/nodis"
"github.com/diiyw/nodis/patch"
"time"
)

func main() {
Expand All @@ -20,7 +21,7 @@ func main() {
n.Set("test", []byte(time.Now().Format("2006-01-02 15:04:05")), false)
}
}()
err := n.Publish("127.0.0.1:6380", []string{"*"})
err := n.Broadcast("127.0.0.1:6380", []string{"*"})
if err != nil {
panic(err)
}
Expand Down
106 changes: 0 additions & 106 deletions fs/disk.go

This file was deleted.

21 changes: 0 additions & 21 deletions fs/fs.go

This file was deleted.

116 changes: 0 additions & 116 deletions fs/memory.go

This file was deleted.

15 changes: 8 additions & 7 deletions internal/notifier/notifier.go → internal/listener/listener.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package notifier
package listener

import (
"github.com/diiyw/nodis/patch"
"path/filepath"

"github.com/diiyw/nodis/patch"
)

type Notifier struct {
type Listener struct {
pattern []string
fn func(op patch.Op)
}

func New(pattern []string, fn func(op patch.Op)) *Notifier {
return &Notifier{
func New(pattern []string, fn func(op patch.Op)) *Listener {
return &Listener{
pattern: pattern,
fn: fn,
}
}

// Matched checks if the key matches the pattern
func (w *Notifier) Matched(key string) bool {
func (w *Listener) Matched(key string) bool {
for _, p := range w.pattern {
matched, err := filepath.Match(p, key)
if err != nil {
Expand All @@ -32,6 +33,6 @@ func (w *Notifier) Matched(key string) bool {
}

// Push sends the operation to the watcher
func (w *Notifier) Push(op patch.Op) {
func (w *Listener) Push(op patch.Op) {
w.fn(op)
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package notifier
package listener

import (
"testing"

"github.com/diiyw/nodis/patch"
)

func TestNotifier_Matched(t *testing.T) {
func TestListener_Matched(t *testing.T) {
pattern := []string{"test"}
w := New(pattern, nil)
if w == nil {
t.Errorf("NewNotifier() = %v, want %v", w, "Notifier{}")
t.Errorf("NewListener() = %v, want %v", w, "Listener{}")
}
if !w.Matched("test") {
t.Errorf("Matched() = %v, want %v", false, true)
}
}

func TestNotifier_Push(t *testing.T) {
func TestListener_Push(t *testing.T) {
pattern := []string{"test"}
w := New(pattern, func(op patch.Op) {
if op.Data.GetKey() != "test" {
t.Errorf("Push() = %v, want %v", op.Data.GetKey(), "test")
}
})
if w == nil {
t.Errorf("NewNotifier() = %v, want %v", w, "Notifier{}")
t.Errorf("NewListener() = %v, want %v", w, "Listener{}")
}
w.Push(patch.Op{patch.OpTypeSet, &patch.OpSet{Key: "test", Value: []byte("test")}})
}
Loading

0 comments on commit c3ddcc8

Please sign in to comment.