Beispiel #1
0
        public string GetDataInJson(StoragePath storagePath)
        {
            string ret = "";

            if (String.IsNullOrEmpty(storagePath.CategoryPath))
            {
                ret += "{";
                ret += String.Format("'path':'{0}',", storagePath.ToString());

                ret += "'result': [";

                foreach (var colname in _db.GetCollectionNames())
                {
                    ret += "{";
                    ret += String.Format("'_name':'{0}',", colname);
                    ret += String.Format("'_type':'{0}'", "collection");
                    ret += "},";
                }
                ret += "],";
                ret += String.Format("'status':'{0}'", "ok");
                ret += "}";
            }
            else if (String.IsNullOrEmpty(storagePath.ObjectId) && String.IsNullOrEmpty(storagePath.QueryString))
            {
                MongoCollection col = _db.GetCollection(storagePath.CategoryPath);

                MongoCursor <BsonDocument> mc = col.FindAllAs <BsonDocument>();

                ret += "{";

                ret += String.Format("'path':'{0}',", storagePath.ToString());

                ret += "'result': [";

                foreach (var d in mc)
                {
                    ret += "{";
                    ret += String.Format("'_name':'{0}',", d.GetValue("_id"));
                    ret += String.Format("'_type':'{0}'", "object");
                    ret += "}";
                }
                ret += "],";
                ret += String.Format("'status':'{0}'", "ok");
                ret += "}";
            }
            else if (String.IsNullOrEmpty(storagePath.ObjectId))
            {
                MongoCollection col = _db.GetCollection(storagePath.CategoryPath);
                throw new NotImplementedException();
            }
            else if (String.IsNullOrEmpty(storagePath.QueryString))
            {
                MongoCollection col = _db.GetCollection(storagePath.CategoryPath);
                throw new NotImplementedException();
            }
            else
            {
                throw new ArgumentException("Storage path can't contain both query string and obid");
            }
            return(ret);
        }
Beispiel #2
0
        public int CleanOldData(StoragePath from, TimeSpan savePeriod)
        {
            int ret = 0;

            if (!SupportHistory)
            {
                throw new NotImplementedException();
            }

            if (from == null || String.IsNullOrEmpty(from.CategoryPath))
            {
                foreach (var colname in _db.GetCollectionNames())
                {
                    MongoCollection col = _db.GetCollection(colname);

                    var q = Query.And(Query.LT("&timings.&actualTill", DateTime.Now - savePeriod));

                    SafeModeResult smr = col.Remove(q, SafeMode.True);

                    if (smr != null)
                    {
                        if (smr.Ok)
                        {
                            ret = (int)smr.DocumentsAffected;
                        }
                        else
                        {
                            throw new Exception(smr.ErrorMessage);
                        }
                    }
                    else
                    {
                        ret = 1;
                    }
                }
            }
            else if (String.IsNullOrEmpty(from.ObjectId) && String.IsNullOrEmpty(from.QueryString))
            {
                MongoCollection col = _db.GetCollection(from.CategoryPath);
                var             q   = Query.And(Query.LT("&timings.&actualTill", DateTime.Now - savePeriod));

                SafeModeResult smr = col.Remove(q, SafeMode.True);
                if (smr != null)
                {
                    if (smr.Ok)
                    {
                        ret = (int )smr.DocumentsAffected;
                    }
                    else
                    {
                        throw new Exception(smr.ErrorMessage);
                    }
                }
                else
                {
                    ret = 1;
                }
            }
            else if (String.IsNullOrEmpty(from.ObjectId))
            {
                MongoCollection col     = _db.GetCollection(from.CategoryPath);
                BsonDocument    bdquery = HelperJsonToBson(from.QueryString);

                var q = Query.And(new QueryComplete(bdquery), Query.LT("&timings.&actualTill", DateTime.Now - savePeriod));

                SafeModeResult smr = col.Remove(q, SafeMode.True);

                if (smr != null)
                {
                    if (smr.Ok)
                    {
                        ret = (int)smr.DocumentsAffected;
                    }
                    else
                    {
                        throw new Exception(smr.ErrorMessage);
                    }
                }
                else
                {
                    ret = 1;
                }
            }
            else if (String.IsNullOrEmpty(from.QueryString))
            {
                throw new InvalidOperationException();
            }
            else
            {
                throw new ArgumentException("Storage path can't contain both query string and obid");
            }
            return(ret);
        }
Beispiel #3
0
 public string GetActualEntityInJson(StoragePath storagePath)
 {
     return(GetActualEntityInBson(storagePath).ToJson());
 }
Beispiel #4
0
        public void SetDataObject <T>(StoragePath storagePath, T obj, TimeSpan actualFor)
        {
            MongoCollection <T> col = _db.GetCollection <T>(storagePath.CategoryPath);

            BsonDocument bd;

            if (obj is BsonDocument)
            {
                bd = obj as BsonDocument;
            }
            else
            {
                bd = obj.ToBsonDocument();
            }

            if (actualFor == TimeSpan.MaxValue)
            {
                bd = (BsonDocument)PackTimestampedDocument(bd, DateTime.Now, DateTime.MaxValue);
            }
            else
            {
                bd = (BsonDocument)PackTimestampedDocument(bd, DateTime.Now, (DateTime.Now + actualFor));
            }


            if (String.IsNullOrEmpty(storagePath.ObjectId) && String.IsNullOrEmpty(storagePath.QueryString))
            {
                col.Insert(obj);
            }
            else if (String.IsNullOrEmpty(storagePath.ObjectId))
            {
                BsonDocument  bdquery = HelperJsonToBson(storagePath.QueryString);
                QueryDocument query   = new QueryDocument(bdquery);

                if (!SupportHistory && col.FindOneAs <BsonDocument>(query) != null)
                {
                    var update = new UpdateDocument
                    {
                        { "$set", bd }
                    };

                    col.Update(query, update);
                }
                else
                {
                    col.Save(bd);
                }
            }
            else if (String.IsNullOrEmpty(storagePath.QueryString))
            {
                Debug.Assert(!SupportHistory);
                BsonValue id    = BsonValue.Create(storagePath.ObjectId);
                var       query = new QueryDocument {
                    { "_id", id }
                };
                var update = new UpdateDocument {
                    { "$set", bd }
                };
                col.Update(query, update);
            }
            else
            {
                throw new ArgumentException("Storage path can't contain both: query string and object id");
            }
        }