| 1 | package edu.ucsb.cs156.courses.services; | |
| 2 | ||
| 3 | import com.mongodb.client.result.UpdateResult; | |
| 4 | import edu.ucsb.cs156.courses.documents.ConvertedSection; | |
| 5 | import org.springframework.dao.DuplicateKeyException; | |
| 6 | import org.springframework.data.domain.Sort.Direction; | |
| 7 | import org.springframework.data.mongodb.core.MongoTemplate; | |
| 8 | import org.springframework.data.mongodb.core.index.Index; | |
| 9 | import org.springframework.data.mongodb.core.query.Criteria; | |
| 10 | import org.springframework.data.mongodb.core.query.Query; | |
| 11 | import org.springframework.data.mongodb.core.query.Update; | |
| 12 | import org.springframework.stereotype.Service; | |
| 13 | ||
| 14 | /** Shared section persistence. Does not record subject completeness or enrollment snapshots. */ | |
| 15 | @Service | |
| 16 | public class CourseCacheService { | |
| 17 | private final MongoTemplate mongoTemplate; | |
| 18 | ||
| 19 | public CourseCacheService(MongoTemplate mongoTemplate) { | |
| 20 | this.mongoTemplate = mongoTemplate; | |
| 21 | } | |
| 22 | ||
| 23 | /** | |
| 24 | * Ensure uniqueness before any writes. Existing duplicates cause index creation to fail; | |
| 25 | * MongoDB's exception reports the conflicting key. Never repair or delete existing data here. | |
| 26 | */ | |
| 27 | public void prepareIndex() { | |
| 28 | mongoTemplate | |
| 29 | .indexOps(ConvertedSection.class) | |
| 30 | .createIndex( | |
| 31 | new Index() | |
| 32 | .on("courseInfo.quarter", Direction.ASC) | |
| 33 | .on("section.enrollCode", Direction.ASC) | |
| 34 | .unique() | |
| 35 | .named("quarter_enrollCode_unique")); | |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * Call prepareIndex() once before writing a batch; abort the batch if preparation fails. | |
| 40 | * | |
| 41 | * @return true for an insert, false for an existing section (even if values are unchanged). | |
| 42 | */ | |
| 43 | public boolean upsertSection(ConvertedSection section) { | |
| 44 | String quarter = section.getCourseInfo().getQuarter(); | |
| 45 | String enrollCode = section.getSection().getEnrollCode(); | |
| 46 |
4
1. upsertSection : negated conditional → KILLED 2. upsertSection : negated conditional → KILLED 3. upsertSection : negated conditional → KILLED 4. upsertSection : negated conditional → KILLED |
if (quarter == null || quarter.isBlank() || enrollCode == null || enrollCode.isBlank()) { |
| 47 | throw new IllegalArgumentException("Quarter and enrollment code are required for caching"); | |
| 48 | } | |
| 49 | Query query = | |
| 50 | Query.query( | |
| 51 | Criteria.where("courseInfo.quarter") | |
| 52 | .is(quarter) | |
| 53 | .and("section.enrollCode") | |
| 54 | .is(enrollCode)); | |
| 55 | Update update = | |
| 56 | new Update() | |
| 57 | .set("courseInfo", section.getCourseInfo()) | |
| 58 | .set("section", section.getSection()); | |
| 59 | try { | |
| 60 | UpdateResult result = mongoTemplate.upsert(query, update, ConvertedSection.class); | |
| 61 |
2
1. upsertSection : replaced boolean return with true for edu/ucsb/cs156/courses/services/CourseCacheService::upsertSection → KILLED 2. upsertSection : negated conditional → KILLED |
return result.getUpsertedId() != null; |
| 62 | } catch (DuplicateKeyException concurrentInsert) { | |
| 63 | // Another writer may insert between matching and inserting. Retry only as an update; | |
| 64 | // preserve its _id and propagate an unexpected duplicate-key failure. | |
| 65 | UpdateResult result = mongoTemplate.updateFirst(query, update, ConvertedSection.class); | |
| 66 |
1
1. upsertSection : negated conditional → KILLED |
if (result.getMatchedCount() == 0) { |
| 67 | throw concurrentInsert; | |
| 68 | } | |
| 69 |
1
1. upsertSection : replaced boolean return with true for edu/ucsb/cs156/courses/services/CourseCacheService::upsertSection → KILLED |
return false; |
| 70 | } | |
| 71 | } | |
| 72 | } | |
Mutations | ||
| 46 |
1.1 2.2 3.3 4.4 |
|
| 61 |
1.1 2.2 |
|
| 66 |
1.1 |
|
| 69 |
1.1 |