Exemplo n.º 1
0
        public void Unlink( MongoReference reference, int count = 1 )
        {
            if (count == 0)
            {
                return;
            }

            var collection = GetCollection( reference );

            var result = collection.FindAndModify( new FindAndModifyArgs
            {
                Query = Query.And( Query.EQ( IdField, reference.Reference ) ),
                Update = Update.Inc( CountField, -count ),
                Upsert = true
            } );

            if ( !result.Ok )
            {
                //TODO : log last error
                throw new StorageRequestException();
            }

            var x = result.ModifiedDocument;

            if ( x == null || !x.Contains( CountField ) || x[ CountField ] < 0 )
            {
                throw new ReferenceOutOfRangeException();
            }
        }
Exemplo n.º 2
0
 public void Add( MongoReference reference )
 {
     foreach ( var item in References )
     {
         if ( item.MongoReference == reference )
         {
             item.Count++;
             return;
         }
     }
     References.Add(new ReferencePack(reference));
 }
Exemplo n.º 3
0
        public void Link( MongoReference reference, int count = 1 )
        {
            if (count == 0)
            {
                return;
            }

            var collection = GetCollection( reference );

            var result = collection.FindAndModify( new FindAndModifyArgs
            {
                Query = Query.And( Query.EQ( IdField, reference.Reference ) ),
                Update = Update.Inc( CountField, count ),
                Upsert = true
            } );

            if ( !result.Ok )
            {
                throw new StorageRequestException();
            }
        }
Exemplo n.º 4
0
 public ReferencePack(MongoReference mongoReference)
 {
     MongoReference = mongoReference;
     Count = 1;
 }
Exemplo n.º 5
0
        public int Count( MongoReference reference )
        {
            var document = GetCollection( reference ).FindOneByIdAs<BsonDocument>( reference.Reference );

            return document == null ? 0 : Convert.ToInt32( document[ CountField ] );
        }
Exemplo n.º 6
0
 private MongoCollection GetCollection( MongoReference reference )
 {
     return MongoDatabase.GetCollection( GetCollectionName( reference.DatabaseName, reference.CollectionName ) );
 }