Exemplo n.º 1
0
        public static ViewOptions GetOptions(this IPageableModel model)
        {
            var options = new ViewOptions();

            options.Descending = model.Descending;
            options.StartKey.Add(model.StartKey);
            options.Skip          = model.Skip;
            options.Stale         = model.Stale;
            options.StartKeyDocId = model.StartKeyDocId;
            options.EndKeyDocId   = model.EndKeyDocId;
            options.Limit         = model.Limit.HasValue ? model.Limit : 10;
            return(options);
        }
Exemplo n.º 2
0
        private ViewResult <T> ProcessGenericResults <T>(string uri, ViewOptions options)
        {
            CouchRequest  req  = GetRequest(options, uri);
            CouchResponse resp = req.GetCouchResponse();

            bool includeDocs = false;

            if (options != null)
            {
                includeDocs = options.IncludeDocs ?? false;
            }

            return(new ViewResult <T>(resp, req.GetRequest(), ObjectSerializer, includeDocs));
        }
Exemplo n.º 3
0
        public IEnumerable<long> Produce(int freshnessInMinutes, int limit)
        {
            try
            {
                var options = new ViewOptions { Limit = limit };
                options.EndKey.Add(freshnessInMinutes);

                return GetSummonerIds(options);
            }
            catch (Exception ex)
            {
                Log.Error("Error reading from the CouchDB index.", ex);
                return Enumerable.Empty<long>();
            }
        }
Exemplo n.º 4
0
        private CouchRequest GetRequest(ViewOptions options, string uri)
        {
            if (options != null)
            {
                uri += options.ToString();
            }
            CouchRequest request = GetRequest(uri, options == null ? null : options.Etag).Get().Json();

            if (options.isAtKeysSizeLimit)
            {
                // Encode the keys parameter in the request body and turn it into a POST request.
                string keys = "{\"keys\": [" + String.Join(",", options.Keys.Select(k => k.ToRawString()).ToArray()) + "]}";
                request.Post().Data(keys);
            }
            return(request);
        }
Exemplo n.º 5
0
        private void ScavengeByDate(int deleteBatchSize)
        {
            if (ConfigurationManager.AppSettings["MessageExpiryDays"] == null) return;

            var expiryDays = double.Parse(ConfigurationManager.AppSettings["MessageExpiryDays"]);
            var options = new ViewOptions();
            options.StartKey.Add(MessageIdManager.Create(new DateTime(1970, 1, 1)).ToDocId());
            options.EndKey.Add(MessageIdManager.Create(DateTime.UtcNow.AddDays(-expiryDays)).ToDocId());
            options.Limit = deleteBatchSize;
            var db = new CouchClient(ConfigurationManager.AppSettings["HydraServer"], int.Parse(ConfigurationManager.AppSettings["Port"]), null, null, false, AuthenticationType.Basic).
                GetDatabase(ConfigurationManager.AppSettings["Database"]);
            var rows = db.GetAllDocuments(options).Rows;
            while (rows.Any()) {
                DeleteDocs(rows, db);
                rows = db.GetAllDocuments(options).Rows;
            }
        }
Exemplo n.º 6
0
        public Task<IEnumerable<long>> ProduceAsync(int freshnessInMinutes, int limit)
        {
            var source = new TaskCompletionSource<IEnumerable<long>>();

            try
            {
                var options = new ViewOptions { Limit = limit };
                options.EndKey.Add(freshnessInMinutes);

                source.TrySetResult(GetSummonerIds(options));
            }
            catch (Exception ex)
            {
                Log.Error("Error reading from the CouchDB index.", ex);
                source.TrySetException(ex);
            }

            return source.Task;
        }
Exemplo n.º 7
0
        private ViewResult <T> ProcessGenericResults <T>(string uri, ViewOptions options)
        {
            CouchRequest req  = GetRequest(options, uri);
            var          resp = req.GetResponse();

            if (resp.StatusCode == HttpStatusCode.BadRequest)
            {
                throw new CouchException(req.GetRequest(), resp, resp.GetResponseString() + "\n" + req.GetRequest().RequestUri);
            }

            bool includeDocs = false;

            if (options != null)
            {
                includeDocs = options.IncludeDocs ?? false;
            }

            return(new ViewResult <T>(resp, req.GetRequest(), ObjectSerializer, includeDocs));
        }
Exemplo n.º 8
0
        private LoveSeat.ViewOptions ToLoveSeatOptions(CouchViewOptions odmViewOptions)
        {
            var opt = new LoveSeat.ViewOptions
            {
                Limit       = odmViewOptions.Limit,
                IncludeDocs = odmViewOptions.IncludeDocs,
                Descending  = odmViewOptions.Descending,
                Reduce      = odmViewOptions.Reduce,
                Group       = odmViewOptions.Group,
                Keys        = odmViewOptions.Keys.Count == 0 ? null : odmViewOptions.Keys.Select(x => new KeyOptions(x)).ToArray()
            };

            CopyKeys(odmViewOptions.Key, opt.Key);
            CopyKeys(odmViewOptions.StartKey, opt.StartKey);
            CopyKeys(odmViewOptions.EndKey, opt.EndKey);


            return(opt);
        }
Exemplo n.º 9
0
		public static IEnumerable<ImageAnnotation> GetAnnotations(string library)
        {
            List<ImageAnnotation> annotations = new List<ImageAnnotation>();

            if (library != null)
            {
                CouchDatabase db = GetDatabase(library);
                var options = new ViewOptions();
                options.IncludeDocs = true;

                var cannotations = db.View<CouchDbAnnotation>("all", options, "annotations");

                foreach (CouchDbAnnotation ca in cannotations.Items)
                {
                    BoundingBox bb = new BoundingBox(ca.left, ca.top, ca.width, ca.height);
                    ImageAnnotation ia = new ImageAnnotation(bb, ca.data, ca.screenshotId);
                    annotations.Add(ia);
                }
            }
            return annotations;
        }
Exemplo n.º 10
0
 public IListResult List(string listName, string viewName, ViewOptions options)
 {
     ThrowDesignDocException();
     return(List(listName, viewName, options, defaultDesignDoc));
 }
Exemplo n.º 11
0
        public static IEnumerable<string> GetAllImageIds(string library)
        {
            List<string> images = new List<string>();

            if (library != null)
            {
                CouchDatabase db = GetDatabase(library);
                var options = new ViewOptions();
                options.IncludeDocs = true;

                var imgDocs = db.View<JObject>("all", options, "screenshots");

                foreach (JObject doc in imgDocs.Items)
                {
                    images.Add(doc["_id"].Value<string>());
                }
            }
            return images;
        }
Exemplo n.º 12
0
 public static ViewOptions GetOptions(this IPageableModel model)
 {
     var options = new ViewOptions();
     options.Descending = model.Descending;
     options.StartKey.Add(model.StartKey);
     options.Skip = model.Skip;
     options.Stale = model.Stale;
     options.StartKeyDocId = model.StartKeyDocId;
     options.EndKeyDocId = model.EndKeyDocId;
     options.Limit = model.Limit.HasValue ? model.Limit : 10;
     return options;
 }
Exemplo n.º 13
0
        public ViewResult View(string viewName, ViewOptions options, string designDoc)
        {
            var uri = string.Format("{0}/_design/{1}/_view/{2}", databaseBaseUri, designDoc, viewName);

            return(ProcessResults(uri, options));
        }
Exemplo n.º 14
0
 public ViewResult View(string viewName, ViewOptions options)
 {
     ThrowDesignDocException();
     return(View(viewName, options, this.defaultDesignDoc));
 }
Exemplo n.º 15
0
 public void Should_Get_Results_Quickly()
 {
     var db = client.GetDatabase("accounting");
     var startTime = DateTime.Now;
     var options = new ViewOptions {Limit = 20};
     var result= db.View<Company>("companies_by_name", options,"accounting");
     foreach ( var item in result.Items)
     {
         Console.WriteLine(item.Name);
     }
     var endTime = DateTime.Now;
     Assert.Less((endTime - startTime).TotalMilliseconds, 80);
 }
Exemplo n.º 16
0
 private IEnumerable<long> GetSummonerIds(ViewOptions options)
 {
     return _summoners.View<long[]>("awaiting_refresh", options)
                      .Items.Select(el => el[1]);
 }
Exemplo n.º 17
0
        public static List<ImageAnnotation> GetAllAnnotationsForImage(string library, string imageId, IBoundingBox region)
        {
            CouchClient client = new CouchClient();
            
            CouchDatabase db = GetDatabase(library);
            var options = new ViewOptions();
            options.Key.Add(imageId);
            options.IncludeDocs = true;

            var cannotations = db.View<CouchDbAnnotation>("by_screenshot", options, "annotations");
            List<ImageAnnotation> annotations = new List<ImageAnnotation>();
            foreach (CouchDbAnnotation ca in cannotations.Items)
            {
                BoundingBox bb = new BoundingBox(ca.left, ca.top, ca.width, ca.height);
                if (BoundingBox.Equals(region, bb))
                {
                    ImageAnnotation ia = new ImageAnnotation(bb, ca.data, ca.screenshotId);
                    annotations.Add(ia);
                }
            }

            return annotations;
        }
Exemplo n.º 18
0
        public ViewResult GetAllDocuments(ViewOptions options)
        {
            var uri = databaseBaseUri + "/_all_docs";

            return(ProcessResults(uri, options));
        }
Exemplo n.º 19
0
 /// <summary>
 /// Allows you to specify options and uses the defaultDesignDoc Specified.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="viewName"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public ViewResult <T> View <T>(string viewName, ViewOptions options)
 {
     ThrowDesignDocException();
     return(View <T>(viewName, options, defaultDesignDoc));
 }
Exemplo n.º 20
0
        public ViewResult View(string viewName, ViewOptions options, string designDoc)
        {
            var uri = databaseBaseUri + "/_design/" + designDoc + "/_view/" + viewName;

            return(ProcessResults(uri, options));
        }
Exemplo n.º 21
0
        private void ScavengeByNumber(int deleteBatchSize)
        {
            if (ConfigurationManager.AppSettings["MaxDocsInDatabase"] == null) return;

            var maxDocs = long.Parse(ConfigurationManager.AppSettings["MaxDocsInDatabase"]);
            var options = new ViewOptions();
            options.StartKey.Add(MessageIdManager.Create(new DateTime(1970, 1, 1)).ToDocId());
            options.EndKey.Add(MessageIdManager.Create(DateTime.UtcNow).ToDocId());
            var db = new CouchClient(ConfigurationManager.AppSettings["HydraServer"], int.Parse(ConfigurationManager.AppSettings["Port"]), null, null, false, AuthenticationType.Basic).
                GetDatabase(ConfigurationManager.AppSettings["Database"]);
            // Getting the empty document returns general database info
            var deleteCount = db.GetDocument("").Value<long>("doc_count") - maxDocs;
            while (deleteCount > 0) {
                options.Limit = (int) Math.Min(deleteCount, deleteBatchSize);
                var rows = db.GetAllDocuments(options).Rows;
                if (!rows.Any()) break;
                DeleteDocs(rows, db);
                deleteCount -= deleteBatchSize;
            }
        }
Exemplo n.º 22
0
        public void Should_Populate_Items_When_IncludeDocs_Set_In_ViewOptions()
        {
            string designDoc = "test";
            string viewName = "testView";
            var settings = new JsonSerializerSettings();
            var converters = new List<JsonConverter> { new IsoDateTimeConverter() };
            settings.Converters = converters;
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            settings.NullValueHandling = NullValueHandling.Ignore;

            var doc = new
                          {
                              _id = "_design/" + designDoc,
                              Language = "javascript",
                              Views = new
                                {
                                    TestView = new
                                    {
                                        Map = "function(doc) {\n  if(doc.type == 'company') {\n    emit(doc._id, null);\n  }\n}"
                                    }
                                }
                          };

            var db = client.GetDatabase(baseDatabase);
            db.CreateDocument(doc._id, JsonConvert.SerializeObject(doc, Formatting.Indented, settings));

            var company = new Company();
            company.Name = "foo";
            db.CreateDocument(company);

            // Without IncludeDocs
            Assert.IsNull(db.View<Company>(viewName, designDoc).Items.ToList()[0]);

            // With IncludeDocs
            ViewOptions options = new ViewOptions { IncludeDocs = true };
            Assert.AreEqual("foo", db.View<Company>(viewName, options, designDoc).Items.ToList()[0].Name);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Gets the results of the view using any and all parameters
        /// </summary>
        /// <param name="viewName">The name of the view</param>
        /// <param name="options">Options such as startkey etc.</param>
        /// <param name="designDoc">The design doc on which the view resides</param>
        /// <returns></returns>
        public ViewResult <T> View <T>(string viewName, ViewOptions options, string designDoc) where T : class
        {
            var uri = string.Format("{0}/_design/{1}/_view/{2}", databaseBaseUri, designDoc, viewName);

            return(ProcessGenericResults <T>(uri, options));
        }