コード例 #1
0
        /// <summary>
        /// Reserve a list of resources for a customer
        /// Decrease all reserved resources by 1.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="c"></param>
        /// <param name="i"></param>
        /// <returns>true if reservation is successful</returns>
        public bool Reserve(Transaction context, Customer c, RID i)
        {
            WaitForReady();
            Enlist(context);

            _lockManager.LockForWrite(context, c);
            _lockManager.LockForWrite(context, i);
            Resource resource = _transactionStorage.Read(context, i);

            if (resource == null)
            {
                throw new InvalidOperationException(i + " does not exist!");
            }
            if (resource.getCount() == 0)
            {
                return(false);
            }

            HashSet <RID> r = _transactionStorage.Read(context, c) ?? new HashSet <RID>();

            r.Add(resource.getID());

            Console.WriteLine("Reserving flight: Stops={0}", r.Count);
            _transactionStorage.Write(context, c, r);

            resource.decrCount();
            _transactionStorage.Write(context, i, resource);
            return(true);
        }
コード例 #2
0
ファイル: MyRM.cs プロジェクト: vlung/Citicenter
        public bool Delete(Transaction context, RID rId, int count)
        {
            // enlist with TM
            this.Enlist(context);

            // read in the resource
            Resource data    = null;
            bool     removed = this.dataStore.Read(context, rId, out data);

            if (!removed ||
                null == data)
            {
                // silently discard
                return(false);
            }

            // update the resource
            if (data.getCount() > count)
            {
                data.decrCount(count);
            }
            else
            {
                data.setCount(0);
            }

            // write the resource back
            return(this.dataStore.Write(context, rId, data));
        }
コード例 #3
0
        /// <summary>
        /// Deletes certain amount of resource.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="rid"></param>
        /// <param name="count"></param>
        /// <returns>true the given resources exists. False if not</returns>
        public bool Delete(Transaction context, RID rid, int count)
        {
            WaitForReady();
            Enlist(context);

            _lockManager.LockForWrite(context, rid);
            Resource resource = _transactionStorage.Read(context, rid);

            if (resource == null)
            {
                // silently discard
                return(false);
            }
            if (resource.getCount() > count)
            {
                resource.decrCount(count);
            }
            else
            {
                resource.setCount(0);
            }

            _transactionStorage.Write(context, rid, resource);
            return(true);
        }
コード例 #4
0
ファイル: MyRM.cs プロジェクト: vlung/Citicenter
        public bool Reserve(Transaction context, Customer customer, RID resource)
        {
            // enlist with TM
            this.Enlist(context);

            // get the resource info
            Resource item   = null;
            bool     result = this.dataStore.Read(context, resource, out item);

            if (!result)
            {
                throw new InvalidOperationException(resource + " does not exist!");
            }
            if (item.getCount() <= 0)
            {
                return(false);
            }

            // get the reservation record
            Reservation data = null;

            result = this.dataStore.Read(context, customer, out data);
            if (!result)
            {
                data = new Reservation(customer);
            }

            // only allow one reservation per item
            if (data.Resources.Contains(resource))
            {
                return(false);
            }

            // update the data
            data.Resources.Add(resource);
            item.decrCount();

            // write back the records
            result = this.dataStore.Write(context, customer, data);
            if (!result)
            {
                return(result);
            }

            result = this.dataStore.Write(context, resource, item);
            if (!result)
            {
                return(result);
            }

            return(true);
        }