Example #1
0
        public IEnumerable<JToken> GetDocs(string viewName, IViewOptions options)
        {
            // We are only interested in viewNames "broadcastMessages" and "directedMessages"
            // options always has IncludeDocs=true and either:
            //   Keys = list of keys to get, each all [topic, couchId] or all [topic, destination, couchId] respectively. Or
            //   StartKey = [topic, couchId] or [topic, destination, couchId] and EndKey = [topic, maxvalue] or [topic, destination, maxvalue] respectively

            // Extract topic and possibly destination from here
            JArray filterArray;
            string startId = null;
            HashSet<string> keySet = null;
            if (options.StartKey.HasValues) {
                filterArray = JArray.Parse(HttpUtility.UrlDecode(options.StartKey.ToString()));
                startId = (string) filterArray.Last;
            } else {
                var keyArray = options.Keys.Select(key => JArray.Parse(HttpUtility.UrlDecode(key.ToString()))).ToList();
                // Empty array of keys for some reason
                if (!options.Keys.Any()) return Enumerable.Empty<JToken>();
                filterArray = keyArray[0];
                keySet = new HashSet<string>(keyArray.Select(key => (string) key.Last));
            }
            string topic = (string) filterArray[0];

            // Find the docs matching topic and possibly destination
            IEnumerable<DocInfo> filteredDocInfos;
            if (viewName == "broadcastMessages") {
                filteredDocInfos = _docInfos.Where(di => di.Topic == topic && di.Destination == null);
            } else if (viewName == "directedMessages") {
                string destination = (string) filterArray[1];
                filteredDocInfos = _docInfos.Where(di => di.Topic == topic && di.Destination == destination);
            } else {
                throw new Exception(string.Format("MockStore.GetDocs: View name {0} is not implemented.", viewName));
            }

            // Return either those on or after startId, or in keySet
            if (startId != null) {
                filteredDocInfos = filteredDocInfos.Where(di => String.Compare(di.DocId, startId, StringComparison.InvariantCulture) >= 0);
            } else {
                filteredDocInfos = filteredDocInfos.Where(di => keySet.Contains(di.DocId));
            }
            return filteredDocInfos.OrderBy(di => di.DocId).Select(di => _docs[di.DocId]);
        }
Example #2
0
        public static void UpdatePaging(this IPageableModel model, IViewOptions options, IViewResult result)
        {
            int count = result.Rows.Count();
            var limit = options.Limit.HasValue ? options.Limit.Value : 10 ;
            model.Limit = limit;
            int rowsMinusOffset = (result.TotalRows - result.OffSet);
            model.ShowPrev = result.OffSet != 0  && !(model.Descending && (rowsMinusOffset <= count));
            model.ShowNext = (result.TotalRows > options.Limit + result.OffSet) || options.Descending.GetValueOrDefault();
            string skipPrev =  result.OffSet < limit ? "" : "&skip=1";
            string skipNext = rowsMinusOffset < limit ? "" : "&skip=1";
            JToken lastRow;
            JToken firstRow;
            if (options.Descending.HasValue && options.Descending.Value)
            {   //descending
                lastRow = result.Rows.FirstOrDefault();
                firstRow = result.Rows.LastOrDefault();
                model.StartIndex = (rowsMinusOffset - limit) < 1 ? 1 : (rowsMinusOffset - limit+1);
            }else
            {   //ascending
                lastRow = result.Rows.LastOrDefault();
                firstRow = result.Rows.FirstOrDefault();
                model.StartIndex = result.OffSet + 1;
            }

            var startIndexPlusCount = model.StartIndex + count-1;
            model.EndIndex =result.TotalRows == 0 ? 0 : startIndexPlusCount;
            if (count == 0 ) model.EndIndex = model.StartIndex = 0;

            model.TotalRows = result.TotalRows;
            string prevStartKey = firstRow != null ? "&startkey=" + HttpUtility.UrlEncode(firstRow.Value<string>("key")) + "&StartKeyDocId=" + firstRow.Value<string>("id") : "";
            string nextStartKey = lastRow != null ? "&startkey=" + HttpUtility.UrlEncode(lastRow.Value<string>("key") ) + "&StartKeyDocId="+ lastRow.Value<string>("id") : "";
            model.NextUrlParameters = "?limit=" + model.Limit  + nextStartKey  + skipNext  ;
            model.PrevUrlParameters = "?limit=" + model.Limit  + prevStartKey
                 + skipPrev +
                "&descending=true";
        }
Example #3
0
 public IEnumerable<JToken> GetDocs(string viewName, IViewOptions options)
 {
     // TODO: check out whether LoveSeat can be improved to use IViewOptions for the second parameter type
     return _db.View(viewName, (ViewOptions) options, DesignDoc).Rows;
 }