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)); }
/// <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); }
/// <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); }
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); }
/// <summary> /// Need to add code here /// returns the amount available for the specified item type /// </summary> public int Query(Transaction context, RID rId) { // enlist with TM this.Enlist(context); Resource data = null; bool result = this.dataStore.Read(context, rId, out data); if (!result) { throw new ArgumentException(rId + " does not exist"); } return(data.getCount()); }
/// <summary> /// Queries the # of given resource /// </summary> /// <param name="context"></param> /// <param name="rid"></param> /// <returns>returns the amount available for the specified item type */</returns> public int Query(TP.Transaction context, RID rid) { WaitForReady(); Enlist(context); _lockManager.LockForRead(context, rid); Console.WriteLine("RM: Query"); Resource resource = _transactionStorage.Read(context, rid); if (resource == null) { throw new ArgumentException(rid + " does not exist"); } return(resource.getCount()); }
public static Row ConvertResourceToRow(Resource resource) { var encoder = new UTF8Encoding(); var rowString = resource.getType() + "," + resource.getID().getName() + "," + resource.getCount().ToString(CultureInfo.InvariantCulture) + "," + resource.getPrice(); //TODO: FIX THE SIZE var row = new Row(96) { Data = encoder.GetBytes(rowString) }; return(row); }