StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 6 · LINQ and Collections

Deferred Execution Bug

easy—No data yet

Problem statement

A query is built, the source list is modified afterwards, and the results change every time the query is enumerated: the depot captures a shift handover report from its delivery log while the night crew keeps storing, dropping and re-labeling deliveries, so the report the supervisor signed off is never the one anybody reads back. Complete the three methods of HandoverReportService so the query is materialized at the intended point, making later changes to the log leave a captured report alone, while a fresh run of the same pipeline still reports the log as it stands right now.

Sample Data

The delivery log starts with five deliveries held in memory, carrying the ids 1 to 5. The log does not hold them in id order, so a run of the handover pipeline has to put its own lines in order.

A delivery starts out carrying one of three statuses, pending, shipped or cancelled. Re-labeling stores whatever status it is handed, so a delivery can end up carrying a label outside those three, and only a delivery labeled pending ever reaches a handover line. Here is an example of a Delivery JSON object:

{
  "id": 2,
  "destination": "Harbour Gate",
  "weightKg": 45.00,
  "status": "shipped"
}

A stored delivery receives the next id after the ones already held, and the status pending.

Service

The Handover Pipeline

HandoverReportService is handed an IDeliveryLog and an IReportStore. Reading the log's Rows walks whatever deliveries the log holds at that moment, in the order the log holds them, and adds one to its EnumerationCount, which starts at 0. Only reading Rows adds to that count, so listing the deliveries, storing one, dropping one or re-labeling one never does.

The handover pipeline is one query over the delivery log, and both Capture and RunNow run that same query:

1. Read Rows.

2. Keep only the deliveries whose status is pending.

3. Order what is left by id, ascending.

4. Turn each one into a handover line holding its id, its destination and its weightKg.

The report built from that query carries the lines themselves, lineCount, the number of lines, and totalWeightKg, the sum of their weights, which is 0 when there are no lines.

Run over the five deliveries the log starts with, the pipeline keeps 1, 3 and 5, in that order, for a lineCount of 3 and a totalWeightKg of 258.75.

HandoverReportService

Implement the three methods of HandoverReportService:

  • Capture:
    • Run the handover pipeline and settle its result then and there, so the lines the report carries are the ones the log held at that moment and nothing that happens to the log afterwards can reach them. A report handed the query itself rather than its result is the bug this repository exists to fix: it is read again on every later look, and it answers with whatever the log holds by then.
    • Give the report the id IReportStore.NextId hands out, which answers 1 on its first call and one more than the last on every further call, so the first capture is numbered 1, the second 2, and so on. Take one id per captured report.
    • Keep the report through IReportStore.Save so it can be read back under that id, and hand it back.
    • Read the delivery log while capturing, so EnumerationCount is higher after a capture than it was before it.
  • Find:
    • Hand back the report kept under that id, exactly as it was captured, or nothing when no report carries that id.
    • Read the delivery log no further, so EnumerationCount is the same after the read as it was before it.
  • RunNow:
    • Run the same pipeline over the delivery log as it stands at that moment, so every delivery stored, dropped or re-labeled since the last capture is accounted for.
    • Read the delivery log again, so EnumerationCount is higher after a fresh run than it was before it.
    • Give the result the id 0 and keep nothing: a fresh run is never saved, so it is not readable under 0, the id it carries, and not under any other id either. GET /api/handovers/0 answers 404 just as any id that was never captured does.
    • Take no report id of its own, so never call IReportStore.NextId, and the first report captured after a fresh run is still numbered 1.
Sample Requests & Responses

GET /api/deliveries answers 200 with the five seeded deliveries ordered by id, and reading them never adds to the log's enumeration count: 1 Riverside Depot 120.50 pending, 2 Harbour Gate 45.00 shipped, 3 Northfield Yard 78.25 pending, 4 Old Mill Stop 12.75 cancelled, 5 Lakeside Hub 60.00 pending. GET / answers 200.

POST /api/handovers against the log as it starts

Response code: 201

Location: /api/handovers/1
{
  "id": 1,
  "lines": [
    { "id": 1, "destination": "Riverside Depot", "weightKg": 120.50 },
    { "id": 3, "destination": "Northfield Yard", "weightKg": 78.25 },
    { "id": 5, "destination": "Lakeside Hub", "weightKg": 60.00 }
  ],
  "lineCount": 3,
  "totalWeightKg": 258.75
}

Deliveries 2 and 4 are left out, because only a pending delivery reaches a handover line.

Now change the log and compare the captured report, read back with GET /api/handovers/1, against a fresh run, read with GET /api/handovers/current. The captured one never moves; the fresh one always reflects the log as it stands:

  • Storing a delivery with POST /api/deliveries and a body of { "destination": "Southgate Wharf", "weightKg": 33.00 } answers 201 with a Location of /api/deliveries/6, and it arrives pending. The captured report does not list it, a fresh run does, and a second capture taken now does list it and is kept under 2, so the two captured reports differ.
  • DELETE /api/deliveries/3 answers 204 and a fresh run drops that line, while the report captured before the drop still lists delivery 3.
  • PUT /api/deliveries/5/status re-labeling a pending delivery answers 200, and a fresh run then leaves delivery 5 out for a lineCount of 2 and a totalWeightKg of 198.75, the earlier capture still listing it. A label outside the three starting statuses, such as held, leaves it out the same way.
  • PUT /api/deliveries/2/status re-labeling the shipped delivery as pending answers 200, and a fresh run then lists it too, for a lineCount of 4 and a totalWeightKg of 303.75, while the earlier capture still holds three lines.
  • DELETE /api/deliveries/99 and PUT /api/deliveries/99/status both answer 404 with a message of Delivery 99 was not found., and a fresh run reports what it did before.

Reading reports back:

  • GET /api/handovers/2 when only one report has been captured answers 404 with a message of Handover report 2 was not found.
  • GET /api/handovers/0 after a fresh run answers 404 with a message of Handover report 0 was not found., a fresh run never being saved.
  • POST /api/handovers after a fresh run is still numbered 1, the fresh run having taken no report id.
  • POST /api/handovers when every delivery has been re-labeled away from pending answers 201 with no lines, a lineCount of 0 and a totalWeightKg of 0.

More in LINQ and Collections

See the module
Catalog Filter And ProjectionThis problem covers turning the catalog the warehouse keeps into a listing a customer can see, dropping what is out of stock or outside the asked-for price range, ordering what is left by price, and leaving the internal fields behind.—No data yeteasySales Grouping ReportThis problem covers gathering recorded sales into one line per category, reporting how many each holds, what they add up to and what they average, and listing the richest category first with ties settled by name.—No data yeteasyTop Customers AnalyticsThis problem covers ranking customers by what they have spent, reporting how many orders each placed and what they average, and pinning a ranking so the pinned board keeps saying what it said when it was taken.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.