diff --git a/storage/graph.go b/storage/graph.go index 0f5fd5b..72c53db 100644 --- a/storage/graph.go +++ b/storage/graph.go @@ -2,6 +2,7 @@ package storage import ( "context" + "fmt" "local/dndex/storage/entity" "local/dndex/storage/operator" @@ -19,12 +20,25 @@ func NewGraph() Graph { } } +func (g Graph) Search(ctx context.Context, namespace string, nameContains string) ([]entity.One, error) { + filter := operator.Regex{Key: entity.Name, Value: fmt.Sprintf(".*%s.*", nameContains)} + return g.find(ctx, namespace, filter) +} + func (g Graph) List(ctx context.Context, namespace string, from ...string) ([]entity.One, error) { - filter := operator.NewFilterIn("_id", from) + filter := operator.NewFilterIn(entity.Name, from) + return g.find(ctx, namespace, filter) +} + +func (g Graph) find(ctx context.Context, namespace string, filter interface{}) ([]entity.One, error) { ch, err := g.mongo.Find(ctx, namespace, filter) if err != nil { return nil, err } + return g.gatherOnes(ctx, ch) +} + +func (g Graph) gatherOnes(ctx context.Context, ch <-chan bson.Raw) ([]entity.One, error) { var ones []entity.One for one := range ch { var o entity.One diff --git a/storage/graph_test.go b/storage/graph_test.go index 107e36d..2cc9d11 100644 --- a/storage/graph_test.go +++ b/storage/graph_test.go @@ -20,7 +20,7 @@ func TestIntegration(t *testing.T) { os.Args = os.Args[:1] graph := NewGraph() - graph.mongo.db = "test-db" + graph.mongo.db = "db" ctx, can := context.WithCancel(context.TODO()) defer can() clean := func() { @@ -67,6 +67,17 @@ func TestIntegration(t *testing.T) { } }) + t.Run("graph.Search(foo => *)", func(t *testing.T) { + some, err := graph.Search(ctx, "col", ones[0].Name[:3]) + if err != nil { + t.Fatal(err) + } + t.Logf("searching for %s (%s)\nsom = %+v", ones[0].Name, ones[0].Name[:3], some) + if len(some) < 1 { + t.Fatalf("%+v: %+v", len(some), some) + } + }) + t.Run("graph.Update(foo, --bar)", func(t *testing.T) { err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}}) if err != nil { diff --git a/storage/operator/filter.go b/storage/operator/filter.go index 507ad8d..0286d3e 100644 --- a/storage/operator/filter.go +++ b/storage/operator/filter.go @@ -6,6 +6,15 @@ import ( "go.mongodb.org/mongo-driver/bson" ) +type Regex struct { + Key string + Value string +} + +func (re Regex) MarshalBSON() ([]byte, error) { + return filterMarshal("$regex", re.Key, re.Value) +} + type FilterIn struct { Key string Values []interface{} @@ -46,12 +55,17 @@ func NewFilterIn(key string, values interface{}) FilterIn { } func (fi FilterIn) MarshalBSON() ([]byte, error) { - if len(fi.Key) == 0 { + return filterMarshal("$in", fi.Key, fi.Values) +} + +func filterMarshal(op, key string, value interface{}) ([]byte, error) { + if len(key) == 0 { return bson.Marshal(map[string]interface{}{}) } - return bson.Marshal(map[string]map[string][]interface{}{ - fi.Key: map[string][]interface{}{ - "$in": fi.Values, + m := map[string]map[string]interface{}{ + key: map[string]interface{}{ + op: value, }, - }) + } + return bson.Marshal(m) }