row_loader.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use crate::services::row::decode_cell_data_from_type_option_cell_data;
  2. use flowy_error::FlowyResult;
  3. use flowy_grid_data_model::entities::{
  4. Cell, CellMeta, FieldMeta, GridBlock, GridBlockOrder, RepeatedGridBlock, Row, RowMeta, RowOrder,
  5. };
  6. use std::collections::HashMap;
  7. use std::sync::Arc;
  8. pub struct GridBlockSnapshot {
  9. pub(crate) block_id: String,
  10. pub row_metas: Vec<Arc<RowMeta>>,
  11. }
  12. pub(crate) fn group_row_orders(row_orders: Vec<RowOrder>) -> Vec<GridBlockOrder> {
  13. let mut map: HashMap<String, GridBlockOrder> = HashMap::new();
  14. row_orders.into_iter().for_each(|row_order| {
  15. // Memory Optimization: escape clone block_id
  16. let block_id = row_order.block_id.clone();
  17. map.entry(block_id)
  18. .or_insert_with(|| GridBlockOrder::new(&row_order.block_id))
  19. .row_orders
  20. .push(row_order);
  21. });
  22. map.into_values().collect::<Vec<_>>()
  23. }
  24. #[inline(always)]
  25. pub fn make_cell_by_field_id(
  26. field_map: &HashMap<&String, &FieldMeta>,
  27. field_id: String,
  28. cell_meta: CellMeta,
  29. ) -> Option<(String, Cell)> {
  30. let field_meta = field_map.get(&field_id)?;
  31. let (raw, content) =
  32. decode_cell_data_from_type_option_cell_data(cell_meta.data, field_meta, &field_meta.field_type).split();
  33. let cell = Cell::new(&field_id, content, raw);
  34. Some((field_id, cell))
  35. }
  36. pub fn make_cell(field_id: &str, field_meta: &FieldMeta, row_meta: &RowMeta) -> Option<Cell> {
  37. let cell_meta = row_meta.cells.get(field_id)?.clone();
  38. let (raw, content) =
  39. decode_cell_data_from_type_option_cell_data(cell_meta.data, field_meta, &field_meta.field_type).split();
  40. Some(Cell::new(field_id, content, raw))
  41. }
  42. pub(crate) fn make_row_orders_from_row_metas(row_metas: &[Arc<RowMeta>]) -> Vec<RowOrder> {
  43. row_metas.iter().map(RowOrder::from).collect::<Vec<_>>()
  44. }
  45. pub(crate) fn make_row_from_row_meta(fields: &[FieldMeta], row_meta: Arc<RowMeta>) -> Option<Row> {
  46. make_rows_from_row_metas(fields, &[row_meta]).pop()
  47. }
  48. pub(crate) fn make_rows_from_row_metas(fields: &[FieldMeta], row_metas: &[Arc<RowMeta>]) -> Vec<Row> {
  49. let field_meta_map = fields
  50. .iter()
  51. .map(|field_meta| (&field_meta.id, field_meta))
  52. .collect::<HashMap<&String, &FieldMeta>>();
  53. let make_row = |row_meta: &Arc<RowMeta>| {
  54. let cell_by_field_id = row_meta
  55. .cells
  56. .clone()
  57. .into_iter()
  58. .flat_map(|(field_id, cell_meta)| make_cell_by_field_id(&field_meta_map, field_id, cell_meta))
  59. .collect::<HashMap<String, Cell>>();
  60. Row {
  61. id: row_meta.id.clone(),
  62. cell_by_field_id,
  63. height: row_meta.height,
  64. }
  65. };
  66. row_metas.iter().map(make_row).collect::<Vec<_>>()
  67. }
  68. pub(crate) fn make_grid_blocks(
  69. block_ids: Option<Vec<String>>,
  70. block_snapshots: Vec<GridBlockSnapshot>,
  71. ) -> FlowyResult<RepeatedGridBlock> {
  72. match block_ids {
  73. None => Ok(block_snapshots
  74. .into_iter()
  75. .map(|snapshot| {
  76. let row_orders = make_row_orders_from_row_metas(&snapshot.row_metas);
  77. GridBlock::new(&snapshot.block_id, row_orders)
  78. })
  79. .collect::<Vec<GridBlock>>()
  80. .into()),
  81. Some(block_ids) => {
  82. let block_meta_data_map: HashMap<&String, &Vec<Arc<RowMeta>>> = block_snapshots
  83. .iter()
  84. .map(|data| (&data.block_id, &data.row_metas))
  85. .collect();
  86. let mut grid_blocks = vec![];
  87. for block_id in block_ids {
  88. match block_meta_data_map.get(&block_id) {
  89. None => {}
  90. Some(row_metas) => {
  91. let row_orders = make_row_orders_from_row_metas(row_metas);
  92. grid_blocks.push(GridBlock::new(&block_id, row_orders));
  93. }
  94. }
  95. }
  96. Ok(grid_blocks.into())
  97. }
  98. }
  99. }