Ejemplo n.º 1
0
        public override async Task <FileWeightsReply> GetFileWeights(GetFileWeightsRequest request, ServerCallContext context)
        {
            var version = await _cache.GetStringAsync(FileWeightsVersionCacheKey(request.RootName));

            if (string.IsNullOrWhiteSpace(version) || !await _contentService.Existed(version))
            {
                var reply = await GetFileWeightsInternal(request);

                return(reply);
            }
            else
            {
                var reply = new FileWeightsReply {
                    Version = version
                };
                return(reply);
            }
        }
Ejemplo n.º 2
0
        private async Task <FileWeightsReply> GetFileWeightsInternal(GetFileWeightsRequest request)
        {
            var reply = new FileWeightsReply();

            if (string.IsNullOrWhiteSpace(request.RootName))
            {
                reply.Error = Error.NoSuchEntity;
                return(reply);
            }
            var path   = PathSep + request.RootName;
            var folder = await _service.All().Where(k => k.Name == request.RootName && k.Path == path && k.Type == (int)FileType.Folder)
                         .FirstOrDefaultAsync();

            if (folder == null)
            {
                reply.Error = Error.NoSuchEntity;
                return(reply);
            }
            var pathStart = folder.Path + PathSep;
            var weights   = await _service.All().Where(p => p.Type == (int)FileType.Normal && p.Path.StartsWith(pathStart)).Select(f => new { f.Name, f.Weight })
                            .ToArrayAsync();

            foreach (var w in weights)
            {
                reply.Weights.Add(w.Name.ToString(), w.Weight ?? 0);
            }
            var codec = new MapField <string, int> .Codec(FieldCodec.ForString(0), FieldCodec.ForInt32(1), 0);

            var version = await _contentService.Add(_cachesGroup, fs =>
            {
                using (var s = new CodedOutputStream(fs))
                {
                    reply.WriteTo(s);
                }

                return(Task.FromResult(true));
            }, "bin");

            await _cache.SetStringAsync(FileWeightsVersionCacheKey(request.RootName), version);

            return(reply);
        }