예제 #1
0
        protected virtual int DoUpdate(Connector.Database db, Doc row, IDataStoreKey key, FieldFilterFunc filter = null)
        {
            var doc = convertDocToBSONDocumentWith_ID(row, "update", filter);
            var _id = doc[Connector.Protocol._ID];

            doc.Delete(Connector.Protocol._ID);
            if (doc.Count == 0)
            {
                return(0);              // nothing to update
            }
            //20160212 spol
            if (filter != null)
            {
                var wrapDoc = new BSONDocument();
                wrapDoc.Set(new BSONDocumentElement(Connector.Protocol.SET, doc));
                doc = wrapDoc;
            }

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(_id);
            var upd = new Connector.UpdateEntry(qry, doc, false, false);

            var result = collection.Update(upd);

            CheckCRUDResult(result, row.Schema.Name, "update");

            return(result.TotalDocumentsAffected);
        }
예제 #2
0
                                 internal bool Delete(string table, byte[] key)
                                 {
                                     //todo: Why do we obtain ref to db every time, need to consider cache for speed
                                     var db = App.GetMongoDatabaseFromConnectString(EffectiveConnectionString);

                                     return(db[table].DeleteOne(MongoQuery.ID_EQ_BYTE_ARRAY(key)).TotalDocumentsAffected > 0);
                                 }
예제 #3
0
                                 internal KdbRecord <byte[]> GetRaw(string table, byte[] key)
                                 {
                                     var db  = App.GetMongoDatabaseFromConnectString(EffectiveConnectionString);
                                     var doc = db[table].FindOne(MongoQuery.ID_EQ_BYTE_ARRAY(key));

                                     if (doc == null)
                                     {
                                         return(KdbRecord <byte[]> .Unassigned);
                                     }
                                     var elmValue = doc[FIELD_VALUE] as BSONBinaryElement;

                                     if (elmValue == null)
                                     {
                                         return(KdbRecord <byte[]> .Unassigned);
                                     }

                                     DateTime lastUseDate;
                                     DateTime?absoluteExpirationDateUTC;
                                     int      slidingExpirationDays;

                                     readAttrs(table, doc, out lastUseDate, out absoluteExpirationDateUTC, out slidingExpirationDays);

                                     var value = elmValue.Value;

                                     return(new KdbRecord <byte[]>(value.Data, slidingExpirationDays, lastUseDate, absoluteExpirationDateUTC));
                                 }
예제 #4
0
                                 internal void Touch(string table, byte[] key)
                                 {
                                     //todo: Why do we obtain ref to db every time, need to consider cache for speed
                                     var db   = App.GetMongoDatabaseFromConnectString(EffectiveConnectionString);
                                     var udoc = new BSONDocument()
                                                .Set(new BSONDateTimeElement(FIELD_LAST_USE_DATE, App.TimeSource.UTCNow));

                                     //Need to update document with $set to prevent document clear
                                     udoc = new BSONDocument().Set(new BSONDocumentElement("$set", udoc));

                                     db[table].Update(new UpdateEntry(MongoQuery.ID_EQ_BYTE_ARRAY(key), udoc, multi: false, upsert: false));
                                 }
예제 #5
0
        protected virtual int DoDelete(Connector.Database db, Doc row, IDataStoreKey key)
        {
            var doc = convertDocToBSONDocumentWith_ID(row, "delete");

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(doc[Connector.Protocol._ID]);

            var result = collection.Delete(new Connector.DeleteEntry(qry, Connector.DeleteLimit.OnlyFirstMatch));

            CheckCRUDResult(result, row.Schema.Name, "delete");

            return(result.TotalDocumentsAffected);
        }
예제 #6
0
                                 /* BSON Document Schema:
                                  * ----------------
                                  *
                                  * {_id: key, v: {}|binary, lastUseDate: dateUTC, absoluteExpirationDateUTC: date|null, slidingExpirationDays: int|null}
                                  * actual field names:
                                  * {_id: key, v: {}|binary, d: dateUTC, a: date|null, s: int|null}
                                  */

                                 internal KdbRecord <TDoc> Get <TDoc>(string table, byte[] key) where TDoc : Doc
                                 {
                                     var db  = App.GetMongoDatabaseFromConnectString(EffectiveConnectionString);
                                     var doc = db[table].FindOne(MongoQuery.ID_EQ_BYTE_ARRAY(key));

                                     if (doc == null)
                                     {
                                         return(KdbRecord <TDoc> .Unassigned);
                                     }
                                     var elmValue = doc[FIELD_VALUE] as BSONDocumentElement;

                                     if (elmValue == null)
                                     {
                                         return(KdbRecord <TDoc> .Unassigned);
                                     }

                                     DateTime lastUseDate;
                                     DateTime?absoluteExpirationDateUTC;
                                     int      slidingExpirationDays;

                                     readAttrs(table, doc, out lastUseDate, out absoluteExpirationDateUTC, out slidingExpirationDays);

                                     var value = elmValue.Value;

                                     TDoc row;

                                     if (value == null)
                                     {
                                         row = Doc.MakeDoc(new Schema(Guid.NewGuid().ToString()), typeof(TDoc)) as TDoc;
                                     }
                                     else
                                     {
                                         var schema = Store.m_Converter.InferSchemaFromBSONDocument(value);
                                         row = Doc.MakeDoc(schema, typeof(TDoc)) as TDoc;
                                         Store.m_Converter.BSONDocumentToDataDoc(value, row, null);
                                     }
                                     return(new KdbRecord <TDoc>(row, slidingExpirationDays, lastUseDate, absoluteExpirationDateUTC));
                                 }