blob: 13007e490b173b3e02d9b63db128ea1aae93aed5 [file] [log] [blame]
Adam Sadovsky23bc12d2015-09-24 10:10:09 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package util
6
7import (
8 "v.io/v23/context"
9 "v.io/v23/glob"
10 "v.io/v23/naming"
11 "v.io/v23/rpc"
12 pubutil "v.io/v23/syncbase/util"
Adam Sadovsky3e4a4d52015-09-25 16:53:19 -070013 "v.io/v23/verror"
Adam Sadovsky23bc12d2015-09-24 10:10:09 -070014 "v.io/x/ref/services/syncbase/store"
15)
16
17// Note, Syncbase handles Glob requests by implementing GlobChildren__ at each
18// level (service, app, database, table).
19
Adam Sadovsky3e4a4d52015-09-25 16:53:19 -070020// GlobChildren implements glob over the Syncbase namespace.
21func GlobChildren(ctx *context.T, call rpc.GlobChildrenServerCall, matcher *glob.Element, sntx store.SnapshotOrTransaction, stKeyPrefix string) error {
Adam Sadovsky23bc12d2015-09-24 10:10:09 -070022 prefix, _ := matcher.FixedPrefix()
Adam Sadovsky3e4a4d52015-09-25 16:53:19 -070023 unescPrefix, ok := pubutil.Unescape(prefix)
24 if !ok {
25 return verror.New(verror.ErrBadArg, ctx, prefix)
26 }
27 it := sntx.Scan(ScanPrefixArgs(stKeyPrefix, unescPrefix))
Adam Sadovsky23bc12d2015-09-24 10:10:09 -070028 key := []byte{}
29 for it.Advance() {
30 key = it.Key(key)
31 parts := SplitKeyParts(string(key))
Adam Sadovsky3e4a4d52015-09-25 16:53:19 -070032 // For explanation of Escape(), see comment in server/nosql/dispatcher.go.
33 name := pubutil.Escape(parts[len(parts)-1])
Adam Sadovsky23bc12d2015-09-24 10:10:09 -070034 if matcher.Match(name) {
35 // TODO(rogulenko): Check for resolve access. (For table glob, this means
36 // checking prefix perms.)
Adam Sadovsky3e4a4d52015-09-25 16:53:19 -070037 if err := call.SendStream().Send(naming.GlobChildrenReplyName{Value: name}); err != nil {
Adam Sadovsky23bc12d2015-09-24 10:10:09 -070038 return err
39 }
40 }
41 }
42 if err := it.Err(); err != nil {
43 call.SendStream().Send(naming.GlobChildrenReplyError{Value: naming.GlobError{Error: err}})
44 }
45 return nil
46}