Skip to content

Commit

Permalink
editoast(feat): 'CacheOperation' only deals with 'ObjectCache'
Browse files Browse the repository at this point in the history
`CacheOperation` is only applied on `InfraCache`. There is no point in using `RailjsonObject`
at that point. Therefore, `CacheOperation` should only depend on `ObjectCache`.
  • Loading branch information
woshilapin committed Dec 12, 2023
1 parent 458675f commit ccd6085
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 185 deletions.
21 changes: 1 addition & 20 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1103,25 +1103,6 @@ components:
required:
- operation_type
type: object
OperationObject:
properties:
obj_type:
$ref: '#/components/schemas/ObjectType'
operation_type:
enum:
- CREATE
- UPDATE
type: string
railjson:
$ref: '#/components/schemas/Railjson'
required:
- operation_type
- obj_type
- railjson
CacheOperation:
oneOf:
- $ref: '#/components/schemas/DeleteOperation'
- $ref: '#/components/schemas/OperationObject'
Ordering:
enum:
- NameAsc
Expand Down Expand Up @@ -4152,7 +4133,7 @@ paths:
application/json:
schema:
items:
$ref: '#/components/schemas/CacheOperation'
$ref: '#/components/schemas/Railjson'
type: array
description: An array containing infos about the operations processed
summary: Update/Create/Delete an object of the infra
Expand Down
23 changes: 1 addition & 22 deletions editoast/openapi_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ paths:
schema:
type: array
items:
$ref: "#/components/schemas/CacheOperation"
$ref: "#/components/schemas/Railjson"

put:
tags:
Expand Down Expand Up @@ -888,11 +888,6 @@ components:
detectors:
type: array

CacheOperation:
oneOf:
- $ref: "#/components/schemas/DeleteOperation"
- $ref: "#/components/schemas/OperationObject"

RailjsonObject:
required:
- operation_type
Expand Down Expand Up @@ -942,22 +937,6 @@ components:
railjson_patch:
$ref: "#/components/schemas/Patches"

OperationObject:
required:
- operation_type
- obj_type
- railjson
properties:
operation_type:
type: string
enum:
- CREATE
- UPDATE
obj_type:
$ref: "#/components/schemas/ObjectType"
railjson:
$ref: "#/components/schemas/Railjson"

Patch:
description: A JSONPatch document as defined by RFC 6902
required:
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/generated_data/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl GeneratedData for ErrorLayer {

async fn update(
conn: &mut PgConnection,
infra: i64,
infra_id: i64,
_operations: &[crate::schema::operation::CacheOperation],
infra_cache: &InfraCache,
) -> Result<()> {
Expand Down
48 changes: 21 additions & 27 deletions editoast/src/generated_data/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::collections::HashSet;

use crate::{
infra_cache::InfraCache,
schema::{
operation::{CacheOperation, RailjsonObject},
OSRDIdentified, OSRDObject, ObjectType,
},
infra_cache::{InfraCache, ObjectCache},
schema::{operation::CacheOperation, OSRDIdentified, OSRDObject, ObjectType},
};

/// This struct gives a set of objects that needs to be updated or deleted given a list of operations.
Expand All @@ -29,16 +26,16 @@ impl<'a> InvolvedObjects<'a> {
let mut res = Self::default();
for op in operations {
match op {
CacheOperation::Create(railjson) | CacheOperation::Update(railjson)
if railjson.get_type() == obj_type =>
CacheOperation::Create(object_cache) | CacheOperation::Update(object_cache)
if object_cache.get_type() == obj_type =>
{
res.updated.insert(railjson.get_id());
res.updated.insert(object_cache.get_id());
}
CacheOperation::Create(RailjsonObject::TrackSection { railjson })
| CacheOperation::Update(RailjsonObject::TrackSection { railjson }) => {
CacheOperation::Create(ObjectCache::TrackSection(track_section))
| CacheOperation::Update(ObjectCache::TrackSection(track_section)) => {
// Retrieve all the objects that are linked to the track section
infra_cache
.get_track_refs_type(&railjson.id, obj_type)
.get_track_refs_type(track_section.get_id(), obj_type)
.iter()
.for_each(|obj_ref| {
res.updated.insert(&obj_ref.obj_id);
Expand Down Expand Up @@ -66,9 +63,9 @@ mod test {
use super::InvolvedObjects;

use crate::infra_cache::tests::create_small_infra_cache;
use crate::schema::operation::{CacheOperation, RailjsonObject};
use crate::schema::utils::Identifier;
use crate::schema::{Detector, ObjectRef, ObjectType, TrackSection};
use crate::infra_cache::ObjectCache;
use crate::schema::operation::CacheOperation;
use crate::schema::{DetectorCache, ObjectRef, ObjectType, TrackSectionCache};

#[test]
fn track_section_deleted() {
Expand All @@ -89,19 +86,16 @@ mod test {
let infra_cache = create_small_infra_cache();
let track = String::from("B");
let operations = vec![
CacheOperation::Update(RailjsonObject::TrackSection {
railjson: TrackSection {
id: track.into(),
length: 420.,
..Default::default()
},
}),
CacheOperation::Create(RailjsonObject::Detector {
railjson: Detector {
id: Identifier::from("D2"),
..Default::default()
},
}),
CacheOperation::Update(ObjectCache::TrackSection(TrackSectionCache {
obj_id: track,
length: 420.,
..Default::default()
})),
CacheOperation::Create(ObjectCache::Detector(DetectorCache {
obj_id: "D2".to_string(),
track: "TA1".to_string(),
position: 42.0,
})),
];
let involved_objects =
InvolvedObjects::from_operations(&operations, &infra_cache, ObjectType::Detector);
Expand Down
80 changes: 45 additions & 35 deletions editoast/src/infra_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use diesel_async::{AsyncPgConnection as PgConnection, RunQueryDsl};
use editoast_derive::EditoastError;
use enum_map::EnumMap;
use geos::geojson::Geometry;
use itertools::Itertools as _;
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use thiserror::Error;
Expand Down Expand Up @@ -51,6 +52,26 @@ pub enum ObjectCache {
Catenary(Catenary),
}

impl From<RailjsonObject> for ObjectCache {
fn from(railjson: RailjsonObject) -> Self {
match railjson {
RailjsonObject::TrackSection { railjson } => ObjectCache::TrackSection(railjson.into()),
RailjsonObject::Signal { railjson } => ObjectCache::Signal(railjson.into()),
RailjsonObject::NeutralSection { .. } => unimplemented!(),
RailjsonObject::SpeedSection { railjson } => ObjectCache::SpeedSection(railjson),
RailjsonObject::Switch { railjson } => ObjectCache::Switch(railjson.into()),
RailjsonObject::SwitchType { railjson } => ObjectCache::SwitchType(railjson),
RailjsonObject::Detector { railjson } => ObjectCache::Detector(railjson.into()),
RailjsonObject::BufferStop { railjson } => ObjectCache::BufferStop(railjson.into()),
RailjsonObject::Route { railjson } => ObjectCache::Route(railjson),
RailjsonObject::OperationalPoint { railjson } => {
ObjectCache::OperationalPoint(railjson.into())
}
RailjsonObject::Catenary { railjson } => ObjectCache::Catenary(railjson),
}
}
}

impl<T: Cache> From<T> for ObjectCache {
fn from(cache: T) -> Self {
cache.get_object_cache()
Expand Down Expand Up @@ -259,7 +280,7 @@ impl From<OperationalPointQueryable> for OperationalPointCache {
let parts: Vec<OperationalPointPart> = serde_json::from_str(&op.parts).unwrap();
Self {
obj_id: op.obj_id,
parts: parts.into_iter().map(|p| p.into()).collect(),
parts: parts.into_iter().map_into().collect(),
}
}
}
Expand All @@ -279,7 +300,7 @@ impl InfraCache {
match entry {
Entry::Occupied(_) => Err(CacheOperationError::DuplicateIdsProvided {
obj_type: obj.get_type().to_string(),
obj_id: obj.get_id().to_owned(),
obj_id: obj.get_id().clone(),
}
.into()),
Entry::Vacant(v) => {
Expand Down Expand Up @@ -491,56 +512,45 @@ impl InfraCache {
.remove(&object_ref.obj_id)
.ok_or_else(|| CacheOperationError::ObjectNotFound {
obj_type: object_ref.obj_type.to_string(),
obj_id: object_ref.obj_id.to_owned(),
obj_id: object_ref.obj_id.clone(),
})?;

for track_id in obj_cache.get_track_referenced_id() {
self.track_sections_refs
.get_mut(track_id)
.ok_or_else(|| CacheOperationError::ObjectNotFound {
obj_type: object_ref.obj_type.to_string(),
obj_id: object_ref.obj_id.to_owned(),
obj_id: object_ref.obj_id.clone(),
})?
.remove(object_ref);
}
Ok(())
}

/// Apply update operation to the infra cache
fn apply_update(&mut self, railjson_obj: &RailjsonObject) -> Result<()> {
self.apply_delete(&railjson_obj.get_ref())?;
self.apply_create(railjson_obj)?;
fn apply_update(&mut self, object_cache: ObjectCache) -> Result<()> {
self.apply_delete(&object_cache.get_ref())?;
self.apply_create(object_cache)?;
Ok(())
}

/// Apply create operation to the infra cache
fn apply_create(&mut self, railjson_obj: &RailjsonObject) -> Result<()> {
match railjson_obj {
RailjsonObject::TrackSection { railjson } => {
self.add::<TrackSectionCache>(railjson.clone().into())?
fn apply_create(&mut self, object_cache: ObjectCache) -> Result<()> {
match object_cache {
ObjectCache::TrackSection(track_section) => {
self.add::<TrackSectionCache>(track_section)?;
}
RailjsonObject::Signal { railjson } => {
self.add::<SignalCache>(railjson.clone().into())?
}
RailjsonObject::SpeedSection { railjson } => self.add(railjson.clone())?,
RailjsonObject::NeutralSection { railjson: _ } => {
// TODO
}
RailjsonObject::Switch { railjson } => {
self.add::<SwitchCache>(railjson.clone().into())?
}
RailjsonObject::SwitchType { railjson } => self.add::<SwitchType>(railjson.clone())?,
RailjsonObject::Detector { railjson } => {
self.add::<DetectorCache>(railjson.clone().into())?
}
RailjsonObject::BufferStop { railjson } => {
self.add::<BufferStopCache>(railjson.clone().into())?
}
RailjsonObject::Route { railjson } => self.add::<Route>(railjson.clone())?,
RailjsonObject::OperationalPoint { railjson } => {
self.add::<OperationalPointCache>(railjson.clone().into())?
ObjectCache::Signal(signal) => self.add::<SignalCache>(signal)?,
ObjectCache::SpeedSection(speed_section) => self.add(speed_section)?,
ObjectCache::Switch(switch) => self.add::<SwitchCache>(switch)?,
ObjectCache::SwitchType(switch_type) => self.add::<SwitchType>(switch_type)?,
ObjectCache::Detector(detector) => self.add::<DetectorCache>(detector)?,
ObjectCache::BufferStop(buffer_stop) => self.add::<BufferStopCache>(buffer_stop)?,
ObjectCache::Route(route) => self.add::<Route>(route)?,
ObjectCache::OperationalPoint(operational_point) => {
self.add::<OperationalPointCache>(operational_point)?;
}
RailjsonObject::Catenary { railjson } => self.add::<Catenary>(railjson.clone())?,
ObjectCache::Catenary(catenary) => self.add::<Catenary>(catenary)?,
}
Ok(())
}
Expand All @@ -550,8 +560,8 @@ impl InfraCache {
for op_res in operations {
match op_res {
CacheOperation::Delete(obj_ref) => self.apply_delete(obj_ref)?,
CacheOperation::Update(railjson_obj) => self.apply_update(railjson_obj)?,
CacheOperation::Create(railjson_obj) => self.apply_create(railjson_obj)?,
CacheOperation::Update(object_cache) => self.apply_update(object_cache.clone())?,
CacheOperation::Create(object_cache) => self.apply_create(object_cache.clone())?,
}
}
Ok(())
Expand Down Expand Up @@ -1000,7 +1010,7 @@ pub mod tests {
for id in 'A'..='D' {
infra_cache
.add(create_track_section_cache(id.to_string(), 500.))
.unwrap()
.unwrap();
}

infra_cache
Expand Down
7 changes: 4 additions & 3 deletions editoast/src/schema/operation/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ pub enum RailjsonObject {
Catenary { railjson: Catenary },
}

pub async fn apply_create_operation(
railjson_object: &RailjsonObject,
pub async fn apply_create_operation<'r>(
railjson_object: &'r RailjsonObject,
infra_id: i64,
conn: &mut PgConnection,
) -> Result<usize> {
) -> Result<(usize, &'r RailjsonObject)> {
if railjson_object.get_id().is_empty() {
return Err(OperationError::EmptyId.into());
}
Expand All @@ -44,6 +44,7 @@ pub async fn apply_create_operation(
.bind::<Json, _>(railjson_object.get_data())
.execute(conn)
.await
.map(|idx| (idx, railjson_object))
.map_err(|err| err.into())
}

Expand Down
Loading

0 comments on commit ccd6085

Please sign in to comment.