/
txn-delivery.go
84 lines (71 loc) · 1.91 KB
/
txn-delivery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
// "fmt"
"github.com/mit-pdos/vmvcc/vmvcc"
)
type DeliveryNewOrderResult struct {
NO_D_ID uint8
NO_O_ID uint32
}
type DeliveryResult struct {
NO_RES []DeliveryNewOrderResult
}
func delivery(
txn *vmvcc.Txn,
wid uint8, did uint8, carrierid uint8, deliveryd uint32,
res *DeliveryResult,
) bool {
/* Find the oldest in-progress order. */
district, _ := GetDistrict(txn, did, wid)
oid := district.D_OLD_O_ID
district.IncrementOldestOrderId(txn)
_, found := GetNewOrder(txn, oid, did, wid)
if !found {
return true
}
/* Append to result. */
noid := DeliveryNewOrderResult { NO_D_ID : did, NO_O_ID : oid }
res.NO_RES = append(res.NO_RES, noid)
/* Get the customer id of this order. */
order, _ := GetOrder(txn, oid, did, wid)
cid := order.O_C_ID
/* Update the carrier id of this order. */
order.UpdateCarrier(txn, carrierid)
/* Sum the total of this order. */
var total float32 = 0
for olnum := uint8(1); olnum <= 15; olnum++ {
/* Get the order line. */
ol, found := GetOrderLine(txn, oid, did, wid, olnum)
if !found {
break
}
/* Update the delivery date of each order line. */
ol.UpdateDeliveryDate(txn, deliveryd)
total += ol.OL_AMOUNT
}
/* Delete this order from NewOrder. */
DeleteNewOrder(txn, oid, did, wid)
/* Update the customer with */
customer, _ := GetCustomer(txn, cid, did, wid)
customer.IncreaseBalance(txn, total)
return true
}
func TxnDelivery(txno *vmvcc.Txn, p *DeliveryInput) ([]*DeliveryResult, bool) {
/* prepare output */
ress := make([]*DeliveryResult, 10)
/* prepare input */
wid := p.W_ID
ocarrierid := p.O_CARRIER_ID
oldeliveryd := p.OL_DELIVERY_D
for did := uint8(1); did <= 10; did ++ {
res := new(DeliveryResult)
body := func(txni *vmvcc.Txn) bool {
return delivery(txni, wid, did, ocarrierid, oldeliveryd, res)
}
/* Restart this transaction if fails. */
for !txno.Run(body) {
}
ress[did - 1] = res
}
return ress, true
}