Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package util |
| 6 | |
| 7 | import ( |
| 8 | "strings" |
| 9 | |
| 10 | "v.io/syncbase/x/ref/services/syncbase/store" |
| 11 | "v.io/v23/context" |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 12 | "v.io/v23/rpc" |
| 13 | "v.io/v23/verror" |
| 14 | "v.io/x/lib/vlog" |
| 15 | "v.io/x/ref/lib/glob" |
| 16 | ) |
| 17 | |
Nicolas Lacasse | ac4a1f8 | 2015-05-29 10:11:20 -0700 | [diff] [blame] | 18 | // NOTE(nlacasse): Syncbase handles Glob requests by implementing |
| 19 | // GlobChildren__ at each level (service, app, database, table). |
| 20 | // |
| 21 | // Each GlobChildren__ method returns all the children of the name without |
| 22 | // filtering them in any way. Any filtering that needs to happen based on the |
| 23 | // glob pattern happens magically at a higher level. This results in a simple |
| 24 | // GlobChildren__ implementation, but often an inefficient one, since we must |
| 25 | // return every single child name, even though many will be filtered before |
| 26 | // being sent to the client. |
| 27 | // |
| 28 | // We should consider changing GlobChildren__ so that it takes the pattern that |
| 29 | // is being Globbed as an argument. Then, GlobChildren__ could be smarter about |
| 30 | // what it returns, and only return names that already match the pattern. This |
| 31 | // would prevent us from having to read all child names from the database every |
| 32 | // time we Glob. |
| 33 | |
Adam Sadovsky | eabf659 | 2015-05-22 16:23:22 -0700 | [diff] [blame] | 34 | // globPatternToPrefix takes "foo*" and returns "foo". |
| 35 | // It assumes the input pattern is a valid glob pattern, and returns |
| 36 | // verror.ErrBadArg if the input is not a valid prefix. |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 37 | func globPatternToPrefix(pattern string) (string, error) { |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 38 | if pattern == "" { |
| 39 | return "", verror.NewErrBadArg(nil) |
| 40 | } |
| 41 | if pattern[len(pattern)-1] != '*' { |
| 42 | return "", verror.NewErrBadArg(nil) |
| 43 | } |
| 44 | res := pattern[:len(pattern)-1] |
| 45 | // Disallow chars and char sequences that have special meaning in glob, since |
| 46 | // our Glob() does not support these. |
| 47 | if strings.ContainsAny(res, "/*?[") { |
| 48 | return "", verror.NewErrBadArg(nil) |
| 49 | } |
| 50 | if strings.Contains(res, "...") { |
| 51 | return "", verror.NewErrBadArg(nil) |
| 52 | } |
| 53 | return res, nil |
| 54 | } |
| 55 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame^] | 56 | // Glob performs a glob. It calls closeStoreReader to close st. |
Adam Sadovsky | 8db7443 | 2015-05-29 17:37:32 -0700 | [diff] [blame] | 57 | // TODO(sadovsky): Why do we make developers implement Glob differently from |
| 58 | // other streaming RPCs? It's confusing that Glob must return immediately and |
| 59 | // write its results to a channel, while other streaming RPC handlers must block |
| 60 | // and write their results to the output stream. See nlacasse's TODO below, too. |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame^] | 61 | func Glob(ctx *context.T, call rpc.ServerCall, pattern string, st store.StoreReader, closeStoreReader func() error, stKeyPrefix string) (<-chan string, error) { |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 62 | // TODO(sadovsky): Support glob with non-prefix pattern. |
Adam Sadovsky | eabf659 | 2015-05-22 16:23:22 -0700 | [diff] [blame] | 63 | if _, err := glob.Parse(pattern); err != nil { |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame^] | 64 | closeStoreReader() |
Adam Sadovsky | eabf659 | 2015-05-22 16:23:22 -0700 | [diff] [blame] | 65 | return nil, verror.New(verror.ErrBadArg, ctx, err) |
| 66 | } |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 67 | prefix, err := globPatternToPrefix(pattern) |
| 68 | if err != nil { |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame^] | 69 | closeStoreReader() |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 70 | if verror.ErrorID(err) == verror.ErrBadArg.ID { |
| 71 | return nil, verror.NewErrNotImplemented(ctx) |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 72 | } |
Adam Sadovsky | eabf659 | 2015-05-22 16:23:22 -0700 | [diff] [blame] | 73 | return nil, verror.New(verror.ErrInternal, ctx, err) |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 74 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame^] | 75 | it := st.Scan(ScanPrefixArgs(stKeyPrefix, prefix)) |
Nicolas Lacasse | ac4a1f8 | 2015-05-29 10:11:20 -0700 | [diff] [blame] | 76 | ch := make(chan string) |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 77 | go func() { |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame^] | 78 | defer closeStoreReader() |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 79 | defer close(ch) |
| 80 | key := []byte{} |
| 81 | for it.Advance() { |
| 82 | key = it.Key(key) |
| 83 | parts := SplitKeyParts(string(key)) |
Nicolas Lacasse | ac4a1f8 | 2015-05-29 10:11:20 -0700 | [diff] [blame] | 84 | ch <- parts[len(parts)-1] |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 85 | } |
| 86 | if err := it.Err(); err != nil { |
Nicolas Lacasse | ac4a1f8 | 2015-05-29 10:11:20 -0700 | [diff] [blame] | 87 | // TODO(nlacasse): We should do something with the error other than |
| 88 | // just logging. Ideally we could send the error back to the |
| 89 | // client, but the GlobChildren__ API doesn't really allow for that |
| 90 | // since it uses a channel of strings rather than GlobResults. |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 91 | vlog.VI(1).Infof("Glob(%q) failed: %v", pattern, err) |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 92 | } |
| 93 | }() |
| 94 | return ch, nil |
| 95 | } |