예제 #1
0
        private void ReadItem()
        {
            ItemPkey ipk = new ItemPkey {
                I_ID = input.OL_I_IDs[i]
            };

            this.AddReq(ipk.ToString());
        }
예제 #2
0
        object ExecuteStoredProcedure(SyncExecution exec, WorkloadParam param)
        {
            NewOrderInParameters input = (NewOrderInParameters)param;

            // Transaction tx = new Transaction(null, vdb);

            // check input
            if (input.OL_I_IDs.Length <= 0 ||
                input.OL_I_IDs.Length != input.OL_QUANTITYs.Length ||
                input.OL_I_IDs.Length != input.OL_SUPPLY_W_IDs.Length)
            {
                throw new Exception("invalid input");
            }

            // read Warehouse,District, Customer
            this.wpk.Set(W_ID: input.W_ID);
            // WarehousePayload wpl = JsonConvert.DeserializeObject<WarehousePayload>((string)tx.Read(Constants.DefaultTbl, wpk.ToString()));
            WarehousePayload wpl;

            if (exec.ReadCopy(this.wpk, out wpl).IsAborted())
            {
                return(null);
            }
            double W_TAX = wpl.W_TAX;

            this.dpk.Set(D_ID: input.D_ID, D_W_ID: input.W_ID);
            // DistrictPayload dpl = JsonConvert.DeserializeObject<DistrictPayload>((string)tx.Read(Constants.DefaultTbl, dpk.ToString()));
            DistrictPayload dpl;

            if (exec.Read(this.dpk, out dpl).IsAborted())
            {
                return(null);
            }
            double D_TAX       = dpl.D_TAX;
            uint   D_NEXT_O_ID = dpl.D_NEXT_O_ID;

            dpl.D_NEXT_O_ID = D_NEXT_O_ID + 1;
            // tx.Update(Constants.DefaultTbl, dpk.ToString(), JsonConvert.SerializeObject(dpl));
            if (exec.Update(dpk, dpl).IsAborted())
            {
                return(null);
            }

            this.cpk.Set(C_ID: input.C_ID, C_D_ID: input.D_ID, C_W_ID: input.W_ID);
            // CustomerPayload cpl = JsonConvert.DeserializeObject<CustomerPayload>((string)tx.Read(Constants.DefaultTbl, cpk.ToString()));
            CustomerPayload cpl;

            if (exec.Read(this.cpk, out cpl).IsAborted())
            {
                return(null);
            }
            double C_DISCOUNT = cpl.C_DISCOUNT;

            // all local or not
            bool allLocal = true;

            for (int i = 0; i < input.OL_I_IDs.Length; i++)
            {
                allLocal = allLocal & input.OL_I_IDs[i] == input.W_ID;
            }

            /*
             * // insert order/new-order, update next-order-id
             * OrderPkey opk = new OrderPkey
             * {
             *  O_ID = D_NEXT_O_ID,
             *  O_D_ID = input.D_ID,
             *  O_W_ID = input.W_ID
             * };
             * OrderPayload opl = new OrderPayload
             * {
             *  O_C_ID = input.C_ID,
             *  O_ENTRY_D = input.O_ENTRY_D,
             *  O_CARRIER_ID = Constants.NullCarrierID,
             *  O_OL_CNT = (uint)input.OL_I_IDs.Length,
             *  O_ALL_LOCAL = Convert.ToUInt32(allLocal)
             * };
             * // tx.ReadAndInitialize(Constants.DefaultTbl, opk.ToString());
             * // tx.Insert(Constants.DefaultTbl, opk.ToString(), JsonConvert.SerializeObject(opl));
             * if (false && exec.Insert(opk, opl).IsAborted())
             * {
             *  return null;
             * }
             */
            /*
             * NewOrderPkey nopk = new NewOrderPkey
             * {
             *  NO_O_ID = D_NEXT_O_ID,
             *  NO_D_ID = input.D_ID,
             *  NO_W_ID = input.W_ID
             * };
             * // tx.ReadAndInitialize(Constants.DefaultTbl, nopk.ToString());
             * // tx.Insert(Constants.DefaultTbl, nopk.ToString(), Constants.PlaceHolder);
             * if (false && exec.Insert(nopk, NewOrderPayload.Placeholder()).IsAborted())
             * {
             *  return null;
             * }
             */

            // whether all items exist without a wrong item number
            ipks.Clear();
            for (int i = 0; i < input.OL_I_IDs.Length; i++)
            {
                ItemPkey ipk = ipks.AllocateNew();
                ipk.Set(I_ID: input.OL_I_IDs[i]);
                // var str = (string)tx.Read(Constants.DefaultTbl, ipk.ToString());
                // items[i] = JsonConvert.DeserializeObject<ItemPayload>(str);
                TpccTablePayload payload;
                if (exec.Read(ipk, out payload).IsAborted())
                {
                    return(null);
                }
                this.items[i] = payload as ItemPayload;
            }

            // insert order lines
            this.spks.Clear();
            this.noOutput.itemOutputs.Clear();
            var total = 0.0;

            for (int i = 0; i < input.OL_I_IDs.Length; i++)
            {
                var OL_NUMBER      = (uint)i + 1;
                var OL_SUPPLY_W_ID = input.OL_SUPPLY_W_IDs[i];
                var OL_I_ID        = input.OL_I_IDs[i];
                var OL_QUANTITY    = input.OL_QUANTITYs[i];

                var I_NAME  = this.items[i].I_NAME;
                var I_DATA  = this.items[i].I_DATA;
                var I_PRICE = this.items[i].I_PRICE;

                // read & update stock info
                StockPkey spk = this.spks.AllocateNew();
                spk.Set(S_I_ID: OL_I_ID, S_W_ID: OL_SUPPLY_W_ID);
                // StockPayload spl = JsonConvert.DeserializeObject<StockPayload>((string)tx.Read(Constants.DefaultTbl, spk.ToString()));
                StockPayload spl;
                if (exec.ReadCopy(spk, out spl).IsAborted())
                {
                    return(null);
                }
                spl.S_YTD += OL_QUANTITY;
                if (spl.S_QUANTITY >= OL_QUANTITY + 10)
                {
                    spl.S_QUANTITY -= (int)OL_QUANTITY;
                }
                else
                {
                    spl.S_QUANTITY += 91 - (int)OL_QUANTITY;
                }
                spl.S_ORDER_CNT += 1;
                if (input.OL_SUPPLY_W_IDs[i] != input.W_ID)
                {
                    spl.S_REMOTE_CNT += 1;
                }
                // tx.Update(Constants.DefaultTbl, spk.ToString(), JsonConvert.SerializeObject(spl));
                if (exec.Update(spk, spl).IsAborted())
                {
                    return(null);
                }

                var OL_AMOUNT = OL_QUANTITY * I_PRICE;
                total += OL_AMOUNT;

                /*
                 * // insert order line
                 * OrderLinePkey olpk = new OrderLinePkey
                 * {
                 *  OL_O_ID = D_NEXT_O_ID,
                 *  OL_D_ID = input.D_ID,
                 *  OL_W_ID = input.W_ID,
                 *  OL_NUMBER = OL_NUMBER
                 * };
                 *
                 * OrderLinePayload olpl = new OrderLinePayload
                 * {
                 *  OL_I_ID = OL_I_ID,
                 *  OL_SUPPLY_W_ID = OL_SUPPLY_W_ID,
                 *  OL_DELIVERY_D = null,
                 *  OL_QUANTITY = OL_QUANTITY,
                 *  OL_AMOUNT = OL_AMOUNT,
                 *  OL_DIST_INFO = spl.S_DIST_01        // TODO, assign to S_DIST_XX, where XX equals to D_ID
                 * };
                 * // tx.ReadAndInitialize(Constants.DefaultTbl, olpk.ToString());
                 * // tx.Insert(Constants.DefaultTbl, olpk.ToString(), JsonConvert.SerializeObject(olpl));
                 * if (false && exec.Insert(olpk, olpl).IsAborted())
                 * {
                 *  return null;
                 * }
                 *
                 * // add to return
                 * var brand = (I_DATA.Contains("ORIGINAL") && spl.S_DATA.Contains("ORIGINAL")) ? 'B' : 'G';
                 * this.noOutput.itemOutputs.AllocateNew().Set(
                 *  this.items[i].I_NAME, brand,
                 *  this.items[i].I_PRICE, olpl.OL_AMOUNT);
                 */
            }

            // tx.Commit();
            if (exec.Commit().IsAborted())
            {
                return(null);
            }

            // to return
            total *= (1 - C_DISCOUNT) * (1 + W_TAX + D_TAX);
            this.noOutput.Set(
                wpk.W_ID, cpl.C_LAST, cpl.C_CREDIT, cpl.C_DISCOUNT,
                wpl.W_TAX, dpl.D_TAX, (uint)input.OL_I_IDs.Length, D_NEXT_O_ID,
                input.O_ENTRY_D, total);

            return(this.noOutput);
        }