Exemplo n.º 1
0
        // List is used ot list all the keys under a given
        // prefix, up to the next prefix.
        //~ func (i *InmemBackend) List(prefix string) ([]string, error) {
        public async Task <IEnumerable <string> > ListAsync(string prefix)
        {
            using (var defer = new Util.Defer())
            {
                //~ i.permitPool.Acquire()
                //~ defer i.permitPool.Release()
                await _permitPool.Acquire();

                //~ i.l.RLock()
                //~ defer i.l.RUnlock()
                _l.EnterReadLock();
                defer.Add(() => _l.ExitReadLock());

                //~ var out []string
                //~ seen := make(map[string]interface{})
                var @out = new List <string>();
                var seen = new Dictionary <string, bool>();

                //~ walkFn := func(s string, v interface{}) bool {
                //~     trimmed := strings.TrimPrefix(s, prefix)
                //~     sep := strings.Index(trimmed, "/")
                //~     if sep == -1 {
                //~         out = append(out, trimmed)
                //~     } else {
                //~         trimmed = trimmed[:sep+1]
                //~         if _, ok := seen[trimmed]; !ok {
                //~             out = append(out, trimmed)
                //~             seen[trimmed] = struct{}{}
                //~         }
                //~     }
                //~     return false
                //~ }
                Walker <Entry> walkFn = (s, v) =>
                {
                    var trimmed = s.StartsWith(prefix) ? s.Substring(prefix.Length) : s;
                    var sep     = trimmed.IndexOf('/');
                    if (sep == -1)
                    {
                        @out.Add(trimmed);
                    }
                    else
                    {
                        trimmed = trimmed.Substring(0, sep + 1);
                        if (!seen.ContainsKey(trimmed))
                        {
                            @out.Add(trimmed);
                            seen[trimmed] = true;
                        }
                    }
                    return(false);
                };

                //~ i.root.WalkPrefix(prefix, walkFn)
                //~ return out, nil
                _root.WalkPrefix(prefix, walkFn);
                return(@out);
            }
        }