Ejemplo n.º 1
0
        public bool Equals(Resource other)
        {
            if (null == other)
            {
                return false;
            }

            return (this.i.Equals(other.i)
                && this.p.Equals(other.p)
                && this.c.Equals(other.c));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a resource to the available ones
        /// </summary>
        /// <param name="context"></param>
        /// <param name="rId"></param>
        /// <param name="count"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        public bool Add(Transaction context, RID rId, int count, int price)
        {
            // enlist with TM
            this.Enlist(context);

            // read the resource
            Resource data = null;
            bool result = this.dataStore.Read(context, rId, out data);
            if (!result)
            {
                data = new Resource(rId);
            }

            // update the item
            data.incrCount(count);
            data.setPrice(price);

            // write the resource
            return this.dataStore.Write(context, rId, data);
        }
 /// <summary>
 /// Write the resource to the shadow
 /// It supports multiple transactions
 /// </summary>
 /// <param name="context">Design for supporting multiple transactions</param>
 /// <param name="key"> </param>
 /// <param name="resource"> </param>
 /// <returns>success</returns>
 public void Write(Transaction context, RID key, Resource resource)
 {
     this.NumberWrites++;
     SelfDestructing();
     var row = SerializationHelper.ConvertResourceToRow(resource);
     _simpleDatabase.UpsertRecord(context, Constants.ResourcesTableName, key.ToString(), row);
 }
Ejemplo n.º 4
0
        private static void WriteResources(Transaction context, StorageManager storage, Resource[] data, bool abort)
        {
            bool createTransaction = (null == context);
            if (createTransaction)
            {
                context = new Transaction();
            }

            // write the data
            foreach (var item in data)
            {
                storage.Write(context, item.Id, item);
            }

            // read the data in the same transaction
            ReadResources(context, storage, data);

            if (createTransaction
                && !abort)
            {
                storage.Prepare(context);
                storage.Commit(context);
            }
            else if (createTransaction
                && abort)
            {
                storage.Abort(context);
            }
        }
Ejemplo n.º 5
0
        private static void ReadResources(Transaction context, StorageManager storage, Resource[] data)
        {
            bool createTransaction = (null == context);
            if (createTransaction)
            {
                context = new Transaction();
            }

            foreach (var item in data)
            {
                Resource output = null;
                if (!storage.Read(context, item.Id, out output))
                {
                    Assert.Fail("{0}: Read of [{1}] was un-successful.", context.Id.ToString(), item.Id.ToString());
                }
                Assert.AreEqual<Resource>(item, output, "{0}: Read was un-successful.", context.Id.ToString());
            }

            if (createTransaction)
            {
                storage.Prepare(context);
                storage.Commit(context);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// implemented using the new TransactionStorage class
        /// This method adds a resource to the available ones
        /// </summary>
        /// <param name="context"></param>
        /// <param name="i"></param>
        /// <param name="count"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        public bool Add(TP.Transaction context, TP.RID i, int count, int price)
        {
            WaitForReady();
            Enlist(context);

            _lockManager.LockForWrite(context, i);
            Resource res = _transactionStorage.Read(context, i);
            if (res == null)
            {
                res = new Resource(i, count, price);
            }
            else
            {
                res.incrCount(count);
                res.setPrice(price);
            }

            _transactionStorage.Write(context, i, res);
            return true;
        }