1use super::*;
2use prost::Message;
3use text::{EditOperation, UndoOperation};
4
5pub struct LeftChannelBuffer {
6 pub channel_id: ChannelId,
7 pub collaborators: Vec<proto::Collaborator>,
8 pub connections: Vec<ConnectionId>,
9}
10
11impl Database {
12 /// Open a channel buffer. Returns the current contents, and adds you to the list of people
13 /// to notify on changes.
14 pub async fn join_channel_buffer(
15 &self,
16 channel_id: ChannelId,
17 user_id: UserId,
18 connection: ConnectionId,
19 ) -> Result<proto::JoinChannelBufferResponse> {
20 self.transaction(|tx| async move {
21 let channel = self.get_channel_internal(channel_id, &*tx).await?;
22 self.check_user_is_channel_participant(&channel, user_id, &tx)
23 .await?;
24
25 let buffer = channel::Model {
26 id: channel_id,
27 ..Default::default()
28 }
29 .find_related(buffer::Entity)
30 .one(&*tx)
31 .await?;
32
33 let buffer = if let Some(buffer) = buffer {
34 buffer
35 } else {
36 let buffer = buffer::ActiveModel {
37 channel_id: ActiveValue::Set(channel_id),
38 ..Default::default()
39 }
40 .insert(&*tx)
41 .await?;
42 buffer_snapshot::ActiveModel {
43 buffer_id: ActiveValue::Set(buffer.id),
44 epoch: ActiveValue::Set(0),
45 text: ActiveValue::Set(String::new()),
46 operation_serialization_version: ActiveValue::Set(
47 storage::SERIALIZATION_VERSION,
48 ),
49 }
50 .insert(&*tx)
51 .await?;
52 buffer
53 };
54
55 // Join the collaborators
56 let mut collaborators = channel_buffer_collaborator::Entity::find()
57 .filter(channel_buffer_collaborator::Column::ChannelId.eq(channel_id))
58 .all(&*tx)
59 .await?;
60 let replica_ids = collaborators
61 .iter()
62 .map(|c| c.replica_id)
63 .collect::<HashSet<_>>();
64 let mut replica_id = ReplicaId(0);
65 while replica_ids.contains(&replica_id) {
66 replica_id.0 += 1;
67 }
68 let collaborator = channel_buffer_collaborator::ActiveModel {
69 channel_id: ActiveValue::Set(channel_id),
70 connection_id: ActiveValue::Set(connection.id as i32),
71 connection_server_id: ActiveValue::Set(ServerId(connection.owner_id as i32)),
72 user_id: ActiveValue::Set(user_id),
73 replica_id: ActiveValue::Set(replica_id),
74 ..Default::default()
75 }
76 .insert(&*tx)
77 .await?;
78 collaborators.push(collaborator);
79
80 let (base_text, operations, max_operation) =
81 self.get_buffer_state(&buffer, &tx).await?;
82
83 // Save the last observed operation
84 if let Some(op) = max_operation {
85 observed_buffer_edits::Entity::insert(observed_buffer_edits::ActiveModel {
86 user_id: ActiveValue::Set(user_id),
87 buffer_id: ActiveValue::Set(buffer.id),
88 epoch: ActiveValue::Set(op.epoch),
89 lamport_timestamp: ActiveValue::Set(op.lamport_timestamp),
90 replica_id: ActiveValue::Set(op.replica_id),
91 })
92 .on_conflict(
93 OnConflict::columns([
94 observed_buffer_edits::Column::UserId,
95 observed_buffer_edits::Column::BufferId,
96 ])
97 .update_columns([
98 observed_buffer_edits::Column::Epoch,
99 observed_buffer_edits::Column::LamportTimestamp,
100 ])
101 .to_owned(),
102 )
103 .exec(&*tx)
104 .await?;
105 }
106
107 Ok(proto::JoinChannelBufferResponse {
108 buffer_id: buffer.id.to_proto(),
109 replica_id: replica_id.to_proto() as u32,
110 base_text,
111 operations,
112 epoch: buffer.epoch as u64,
113 collaborators: collaborators
114 .into_iter()
115 .map(|collaborator| proto::Collaborator {
116 peer_id: Some(collaborator.connection().into()),
117 user_id: collaborator.user_id.to_proto(),
118 replica_id: collaborator.replica_id.0 as u32,
119 })
120 .collect(),
121 })
122 })
123 .await
124 }
125
126 /// Rejoin a channel buffer (after a connection interruption)
127 pub async fn rejoin_channel_buffers(
128 &self,
129 buffers: &[proto::ChannelBufferVersion],
130 user_id: UserId,
131 connection_id: ConnectionId,
132 ) -> Result<Vec<RejoinedChannelBuffer>> {
133 self.transaction(|tx| async move {
134 let mut results = Vec::new();
135 for client_buffer in buffers {
136 let channel = self
137 .get_channel_internal(ChannelId::from_proto(client_buffer.channel_id), &*tx)
138 .await?;
139 if self
140 .check_user_is_channel_participant(&channel, user_id, &*tx)
141 .await
142 .is_err()
143 {
144 log::info!("user is not a member of channel");
145 continue;
146 }
147
148 let buffer = self.get_channel_buffer(channel.id, &*tx).await?;
149 let mut collaborators = channel_buffer_collaborator::Entity::find()
150 .filter(channel_buffer_collaborator::Column::ChannelId.eq(channel.id))
151 .all(&*tx)
152 .await?;
153
154 // If the buffer epoch hasn't changed since the client lost
155 // connection, then the client's buffer can be synchronized with
156 // the server's buffer.
157 if buffer.epoch as u64 != client_buffer.epoch {
158 log::info!("can't rejoin buffer, epoch has changed");
159 continue;
160 }
161
162 // Find the collaborator record for this user's previous lost
163 // connection. Update it with the new connection id.
164 let server_id = ServerId(connection_id.owner_id as i32);
165 let Some(self_collaborator) = collaborators.iter_mut().find(|c| {
166 c.user_id == user_id
167 && (c.connection_lost || c.connection_server_id != server_id)
168 }) else {
169 log::info!("can't rejoin buffer, no previous collaborator found");
170 continue;
171 };
172 let old_connection_id = self_collaborator.connection();
173 *self_collaborator = channel_buffer_collaborator::ActiveModel {
174 id: ActiveValue::Unchanged(self_collaborator.id),
175 connection_id: ActiveValue::Set(connection_id.id as i32),
176 connection_server_id: ActiveValue::Set(ServerId(connection_id.owner_id as i32)),
177 connection_lost: ActiveValue::Set(false),
178 ..Default::default()
179 }
180 .update(&*tx)
181 .await?;
182
183 let client_version = version_from_wire(&client_buffer.version);
184 let serialization_version = self
185 .get_buffer_operation_serialization_version(buffer.id, buffer.epoch, &*tx)
186 .await?;
187
188 let mut rows = buffer_operation::Entity::find()
189 .filter(
190 buffer_operation::Column::BufferId
191 .eq(buffer.id)
192 .and(buffer_operation::Column::Epoch.eq(buffer.epoch)),
193 )
194 .stream(&*tx)
195 .await?;
196
197 // Find the server's version vector and any operations
198 // that the client has not seen.
199 let mut server_version = clock::Global::new();
200 let mut operations = Vec::new();
201 while let Some(row) = rows.next().await {
202 let row = row?;
203 let timestamp = clock::Lamport {
204 replica_id: row.replica_id as u16,
205 value: row.lamport_timestamp as u32,
206 };
207 server_version.observe(timestamp);
208 if !client_version.observed(timestamp) {
209 operations.push(proto::Operation {
210 variant: Some(operation_from_storage(row, serialization_version)?),
211 })
212 }
213 }
214
215 results.push(RejoinedChannelBuffer {
216 old_connection_id,
217 buffer: proto::RejoinedChannelBuffer {
218 channel_id: client_buffer.channel_id,
219 version: version_to_wire(&server_version),
220 operations,
221 collaborators: collaborators
222 .into_iter()
223 .map(|collaborator| proto::Collaborator {
224 peer_id: Some(collaborator.connection().into()),
225 user_id: collaborator.user_id.to_proto(),
226 replica_id: collaborator.replica_id.0 as u32,
227 })
228 .collect(),
229 },
230 });
231 }
232
233 Ok(results)
234 })
235 .await
236 }
237
238 /// Clear out any buffer collaborators who are no longer collaborating.
239 pub async fn clear_stale_channel_buffer_collaborators(
240 &self,
241 channel_id: ChannelId,
242 server_id: ServerId,
243 ) -> Result<RefreshedChannelBuffer> {
244 self.transaction(|tx| async move {
245 let db_collaborators = channel_buffer_collaborator::Entity::find()
246 .filter(channel_buffer_collaborator::Column::ChannelId.eq(channel_id))
247 .all(&*tx)
248 .await?;
249
250 let mut connection_ids = Vec::new();
251 let mut collaborators = Vec::new();
252 let mut collaborator_ids_to_remove = Vec::new();
253 for db_collaborator in &db_collaborators {
254 if !db_collaborator.connection_lost
255 && db_collaborator.connection_server_id == server_id
256 {
257 connection_ids.push(db_collaborator.connection());
258 collaborators.push(proto::Collaborator {
259 peer_id: Some(db_collaborator.connection().into()),
260 replica_id: db_collaborator.replica_id.0 as u32,
261 user_id: db_collaborator.user_id.to_proto(),
262 })
263 } else {
264 collaborator_ids_to_remove.push(db_collaborator.id);
265 }
266 }
267
268 channel_buffer_collaborator::Entity::delete_many()
269 .filter(channel_buffer_collaborator::Column::Id.is_in(collaborator_ids_to_remove))
270 .exec(&*tx)
271 .await?;
272
273 Ok(RefreshedChannelBuffer {
274 connection_ids,
275 collaborators,
276 })
277 })
278 .await
279 }
280
281 /// Close the channel buffer, and stop receiving updates for it.
282 pub async fn leave_channel_buffer(
283 &self,
284 channel_id: ChannelId,
285 connection: ConnectionId,
286 ) -> Result<LeftChannelBuffer> {
287 self.transaction(|tx| async move {
288 self.leave_channel_buffer_internal(channel_id, connection, &*tx)
289 .await
290 })
291 .await
292 }
293
294 /// Close the channel buffer, and stop receiving updates for it.
295 pub async fn channel_buffer_connection_lost(
296 &self,
297 connection: ConnectionId,
298 tx: &DatabaseTransaction,
299 ) -> Result<()> {
300 channel_buffer_collaborator::Entity::update_many()
301 .filter(
302 Condition::all()
303 .add(channel_buffer_collaborator::Column::ConnectionId.eq(connection.id as i32))
304 .add(
305 channel_buffer_collaborator::Column::ConnectionServerId
306 .eq(connection.owner_id as i32),
307 ),
308 )
309 .set(channel_buffer_collaborator::ActiveModel {
310 connection_lost: ActiveValue::set(true),
311 ..Default::default()
312 })
313 .exec(&*tx)
314 .await?;
315 Ok(())
316 }
317
318 /// Close all open channel buffers
319 pub async fn leave_channel_buffers(
320 &self,
321 connection: ConnectionId,
322 ) -> Result<Vec<LeftChannelBuffer>> {
323 self.transaction(|tx| async move {
324 #[derive(Debug, Clone, Copy, EnumIter, DeriveColumn)]
325 enum QueryChannelIds {
326 ChannelId,
327 }
328
329 let channel_ids: Vec<ChannelId> = channel_buffer_collaborator::Entity::find()
330 .select_only()
331 .column(channel_buffer_collaborator::Column::ChannelId)
332 .filter(Condition::all().add(
333 channel_buffer_collaborator::Column::ConnectionId.eq(connection.id as i32),
334 ))
335 .into_values::<_, QueryChannelIds>()
336 .all(&*tx)
337 .await?;
338
339 let mut result = Vec::new();
340 for channel_id in channel_ids {
341 let left_channel_buffer = self
342 .leave_channel_buffer_internal(channel_id, connection, &*tx)
343 .await?;
344 result.push(left_channel_buffer);
345 }
346
347 Ok(result)
348 })
349 .await
350 }
351
352 async fn leave_channel_buffer_internal(
353 &self,
354 channel_id: ChannelId,
355 connection: ConnectionId,
356 tx: &DatabaseTransaction,
357 ) -> Result<LeftChannelBuffer> {
358 let result = channel_buffer_collaborator::Entity::delete_many()
359 .filter(
360 Condition::all()
361 .add(channel_buffer_collaborator::Column::ChannelId.eq(channel_id))
362 .add(channel_buffer_collaborator::Column::ConnectionId.eq(connection.id as i32))
363 .add(
364 channel_buffer_collaborator::Column::ConnectionServerId
365 .eq(connection.owner_id as i32),
366 ),
367 )
368 .exec(&*tx)
369 .await?;
370 if result.rows_affected == 0 {
371 Err(anyhow!("not a collaborator on this project"))?;
372 }
373
374 let mut collaborators = Vec::new();
375 let mut connections = Vec::new();
376 let mut rows = channel_buffer_collaborator::Entity::find()
377 .filter(
378 Condition::all().add(channel_buffer_collaborator::Column::ChannelId.eq(channel_id)),
379 )
380 .stream(&*tx)
381 .await?;
382 while let Some(row) = rows.next().await {
383 let row = row?;
384 let connection = row.connection();
385 connections.push(connection);
386 collaborators.push(proto::Collaborator {
387 peer_id: Some(connection.into()),
388 replica_id: row.replica_id.0 as u32,
389 user_id: row.user_id.to_proto(),
390 });
391 }
392
393 drop(rows);
394
395 if collaborators.is_empty() {
396 self.snapshot_channel_buffer(channel_id, &tx).await?;
397 }
398
399 Ok(LeftChannelBuffer {
400 channel_id,
401 collaborators,
402 connections,
403 })
404 }
405
406 pub async fn get_channel_buffer_collaborators(
407 &self,
408 channel_id: ChannelId,
409 ) -> Result<Vec<UserId>> {
410 self.transaction(|tx| async move {
411 self.get_channel_buffer_collaborators_internal(channel_id, &*tx)
412 .await
413 })
414 .await
415 }
416
417 async fn get_channel_buffer_collaborators_internal(
418 &self,
419 channel_id: ChannelId,
420 tx: &DatabaseTransaction,
421 ) -> Result<Vec<UserId>> {
422 #[derive(Debug, Clone, Copy, EnumIter, DeriveColumn)]
423 enum QueryUserIds {
424 UserId,
425 }
426
427 let users: Vec<UserId> = channel_buffer_collaborator::Entity::find()
428 .select_only()
429 .column(channel_buffer_collaborator::Column::UserId)
430 .filter(
431 Condition::all().add(channel_buffer_collaborator::Column::ChannelId.eq(channel_id)),
432 )
433 .into_values::<_, QueryUserIds>()
434 .all(&*tx)
435 .await?;
436
437 Ok(users)
438 }
439
440 pub async fn update_channel_buffer(
441 &self,
442 channel_id: ChannelId,
443 user: UserId,
444 operations: &[proto::Operation],
445 ) -> Result<(
446 Vec<ConnectionId>,
447 Vec<UserId>,
448 i32,
449 Vec<proto::VectorClockEntry>,
450 )> {
451 self.transaction(move |tx| async move {
452 let channel = self.get_channel_internal(channel_id, &*tx).await?;
453 self.check_user_is_channel_member(&channel, user, &*tx)
454 .await?;
455
456 let buffer = buffer::Entity::find()
457 .filter(buffer::Column::ChannelId.eq(channel_id))
458 .one(&*tx)
459 .await?
460 .ok_or_else(|| anyhow!("no such buffer"))?;
461
462 let serialization_version = self
463 .get_buffer_operation_serialization_version(buffer.id, buffer.epoch, &*tx)
464 .await?;
465
466 let operations = operations
467 .iter()
468 .filter_map(|op| operation_to_storage(op, &buffer, serialization_version))
469 .collect::<Vec<_>>();
470
471 let mut channel_members;
472 let max_version;
473
474 if !operations.is_empty() {
475 let max_operation = operations
476 .iter()
477 .max_by_key(|op| (op.lamport_timestamp.as_ref(), op.replica_id.as_ref()))
478 .unwrap();
479
480 max_version = vec![proto::VectorClockEntry {
481 replica_id: *max_operation.replica_id.as_ref() as u32,
482 timestamp: *max_operation.lamport_timestamp.as_ref() as u32,
483 }];
484
485 // get current channel participants and save the max operation above
486 self.save_max_operation(
487 user,
488 buffer.id,
489 buffer.epoch,
490 *max_operation.replica_id.as_ref(),
491 *max_operation.lamport_timestamp.as_ref(),
492 &*tx,
493 )
494 .await?;
495
496 channel_members = self.get_channel_participants(&channel, &*tx).await?;
497 let collaborators = self
498 .get_channel_buffer_collaborators_internal(channel_id, &*tx)
499 .await?;
500 channel_members.retain(|member| !collaborators.contains(member));
501
502 buffer_operation::Entity::insert_many(operations)
503 .on_conflict(
504 OnConflict::columns([
505 buffer_operation::Column::BufferId,
506 buffer_operation::Column::Epoch,
507 buffer_operation::Column::LamportTimestamp,
508 buffer_operation::Column::ReplicaId,
509 ])
510 .do_nothing()
511 .to_owned(),
512 )
513 .exec(&*tx)
514 .await?;
515 } else {
516 channel_members = Vec::new();
517 max_version = Vec::new();
518 }
519
520 let mut connections = Vec::new();
521 let mut rows = channel_buffer_collaborator::Entity::find()
522 .filter(
523 Condition::all()
524 .add(channel_buffer_collaborator::Column::ChannelId.eq(channel_id)),
525 )
526 .stream(&*tx)
527 .await?;
528 while let Some(row) = rows.next().await {
529 let row = row?;
530 connections.push(ConnectionId {
531 id: row.connection_id as u32,
532 owner_id: row.connection_server_id.0 as u32,
533 });
534 }
535
536 Ok((connections, channel_members, buffer.epoch, max_version))
537 })
538 .await
539 }
540
541 async fn save_max_operation(
542 &self,
543 user_id: UserId,
544 buffer_id: BufferId,
545 epoch: i32,
546 replica_id: i32,
547 lamport_timestamp: i32,
548 tx: &DatabaseTransaction,
549 ) -> Result<()> {
550 use observed_buffer_edits::Column;
551
552 observed_buffer_edits::Entity::insert(observed_buffer_edits::ActiveModel {
553 user_id: ActiveValue::Set(user_id),
554 buffer_id: ActiveValue::Set(buffer_id),
555 epoch: ActiveValue::Set(epoch),
556 replica_id: ActiveValue::Set(replica_id),
557 lamport_timestamp: ActiveValue::Set(lamport_timestamp),
558 })
559 .on_conflict(
560 OnConflict::columns([Column::UserId, Column::BufferId])
561 .update_columns([Column::Epoch, Column::LamportTimestamp, Column::ReplicaId])
562 .action_cond_where(
563 Condition::any().add(Column::Epoch.lt(epoch)).add(
564 Condition::all().add(Column::Epoch.eq(epoch)).add(
565 Condition::any()
566 .add(Column::LamportTimestamp.lt(lamport_timestamp))
567 .add(
568 Column::LamportTimestamp
569 .eq(lamport_timestamp)
570 .and(Column::ReplicaId.lt(replica_id)),
571 ),
572 ),
573 ),
574 )
575 .to_owned(),
576 )
577 .exec_without_returning(tx)
578 .await?;
579
580 Ok(())
581 }
582
583 async fn get_buffer_operation_serialization_version(
584 &self,
585 buffer_id: BufferId,
586 epoch: i32,
587 tx: &DatabaseTransaction,
588 ) -> Result<i32> {
589 Ok(buffer_snapshot::Entity::find()
590 .filter(buffer_snapshot::Column::BufferId.eq(buffer_id))
591 .filter(buffer_snapshot::Column::Epoch.eq(epoch))
592 .select_only()
593 .column(buffer_snapshot::Column::OperationSerializationVersion)
594 .into_values::<_, QueryOperationSerializationVersion>()
595 .one(&*tx)
596 .await?
597 .ok_or_else(|| anyhow!("missing buffer snapshot"))?)
598 }
599
600 pub async fn get_channel_buffer(
601 &self,
602 channel_id: ChannelId,
603 tx: &DatabaseTransaction,
604 ) -> Result<buffer::Model> {
605 Ok(channel::Model {
606 id: channel_id,
607 ..Default::default()
608 }
609 .find_related(buffer::Entity)
610 .one(&*tx)
611 .await?
612 .ok_or_else(|| anyhow!("no such buffer"))?)
613 }
614
615 async fn get_buffer_state(
616 &self,
617 buffer: &buffer::Model,
618 tx: &DatabaseTransaction,
619 ) -> Result<(
620 String,
621 Vec<proto::Operation>,
622 Option<buffer_operation::Model>,
623 )> {
624 let id = buffer.id;
625 let (base_text, version) = if buffer.epoch > 0 {
626 let snapshot = buffer_snapshot::Entity::find()
627 .filter(
628 buffer_snapshot::Column::BufferId
629 .eq(id)
630 .and(buffer_snapshot::Column::Epoch.eq(buffer.epoch)),
631 )
632 .one(&*tx)
633 .await?
634 .ok_or_else(|| anyhow!("no such snapshot"))?;
635
636 let version = snapshot.operation_serialization_version;
637 (snapshot.text, version)
638 } else {
639 (String::new(), storage::SERIALIZATION_VERSION)
640 };
641
642 let mut rows = buffer_operation::Entity::find()
643 .filter(
644 buffer_operation::Column::BufferId
645 .eq(id)
646 .and(buffer_operation::Column::Epoch.eq(buffer.epoch)),
647 )
648 .order_by_asc(buffer_operation::Column::LamportTimestamp)
649 .order_by_asc(buffer_operation::Column::ReplicaId)
650 .stream(&*tx)
651 .await?;
652
653 let mut operations = Vec::new();
654 let mut last_row = None;
655 while let Some(row) = rows.next().await {
656 let row = row?;
657 last_row = Some(buffer_operation::Model {
658 buffer_id: row.buffer_id,
659 epoch: row.epoch,
660 lamport_timestamp: row.lamport_timestamp,
661 replica_id: row.lamport_timestamp,
662 value: Default::default(),
663 });
664 operations.push(proto::Operation {
665 variant: Some(operation_from_storage(row, version)?),
666 });
667 }
668
669 Ok((base_text, operations, last_row))
670 }
671
672 async fn snapshot_channel_buffer(
673 &self,
674 channel_id: ChannelId,
675 tx: &DatabaseTransaction,
676 ) -> Result<()> {
677 let buffer = self.get_channel_buffer(channel_id, tx).await?;
678 let (base_text, operations, _) = self.get_buffer_state(&buffer, tx).await?;
679 if operations.is_empty() {
680 return Ok(());
681 }
682
683 let mut text_buffer = text::Buffer::new(0, 0, base_text);
684 text_buffer
685 .apply_ops(operations.into_iter().filter_map(operation_from_wire))
686 .unwrap();
687
688 let base_text = text_buffer.text();
689 let epoch = buffer.epoch + 1;
690
691 buffer_snapshot::Model {
692 buffer_id: buffer.id,
693 epoch,
694 text: base_text,
695 operation_serialization_version: storage::SERIALIZATION_VERSION,
696 }
697 .into_active_model()
698 .insert(tx)
699 .await?;
700
701 buffer::ActiveModel {
702 id: ActiveValue::Unchanged(buffer.id),
703 epoch: ActiveValue::Set(epoch),
704 ..Default::default()
705 }
706 .save(tx)
707 .await?;
708
709 Ok(())
710 }
711
712 pub async fn observe_buffer_version(
713 &self,
714 buffer_id: BufferId,
715 user_id: UserId,
716 epoch: i32,
717 version: &[proto::VectorClockEntry],
718 ) -> Result<()> {
719 self.transaction(|tx| async move {
720 // For now, combine concurrent operations.
721 let Some(component) = version.iter().max_by_key(|version| version.timestamp) else {
722 return Ok(());
723 };
724 self.save_max_operation(
725 user_id,
726 buffer_id,
727 epoch,
728 component.replica_id as i32,
729 component.timestamp as i32,
730 &*tx,
731 )
732 .await?;
733 Ok(())
734 })
735 .await
736 }
737
738 pub async fn unseen_channel_buffer_changes(
739 &self,
740 user_id: UserId,
741 channel_ids: &[ChannelId],
742 tx: &DatabaseTransaction,
743 ) -> Result<Vec<proto::UnseenChannelBufferChange>> {
744 #[derive(Debug, Clone, Copy, EnumIter, DeriveColumn)]
745 enum QueryIds {
746 ChannelId,
747 Id,
748 }
749
750 let mut channel_ids_by_buffer_id = HashMap::default();
751 let mut rows = buffer::Entity::find()
752 .filter(buffer::Column::ChannelId.is_in(channel_ids.iter().copied()))
753 .stream(&*tx)
754 .await?;
755 while let Some(row) = rows.next().await {
756 let row = row?;
757 channel_ids_by_buffer_id.insert(row.id, row.channel_id);
758 }
759 drop(rows);
760
761 let mut observed_edits_by_buffer_id = HashMap::default();
762 let mut rows = observed_buffer_edits::Entity::find()
763 .filter(observed_buffer_edits::Column::UserId.eq(user_id))
764 .filter(
765 observed_buffer_edits::Column::BufferId
766 .is_in(channel_ids_by_buffer_id.keys().copied()),
767 )
768 .stream(&*tx)
769 .await?;
770 while let Some(row) = rows.next().await {
771 let row = row?;
772 observed_edits_by_buffer_id.insert(row.buffer_id, row);
773 }
774 drop(rows);
775
776 let latest_operations = self
777 .get_latest_operations_for_buffers(channel_ids_by_buffer_id.keys().copied(), &*tx)
778 .await?;
779
780 let mut changes = Vec::default();
781 for latest in latest_operations {
782 if let Some(observed) = observed_edits_by_buffer_id.get(&latest.buffer_id) {
783 if (
784 observed.epoch,
785 observed.lamport_timestamp,
786 observed.replica_id,
787 ) >= (latest.epoch, latest.lamport_timestamp, latest.replica_id)
788 {
789 continue;
790 }
791 }
792
793 if let Some(channel_id) = channel_ids_by_buffer_id.get(&latest.buffer_id) {
794 changes.push(proto::UnseenChannelBufferChange {
795 channel_id: channel_id.to_proto(),
796 epoch: latest.epoch as u64,
797 version: vec![proto::VectorClockEntry {
798 replica_id: latest.replica_id as u32,
799 timestamp: latest.lamport_timestamp as u32,
800 }],
801 });
802 }
803 }
804
805 Ok(changes)
806 }
807
808 /// Returns the latest operations for the buffers with the specified IDs.
809 pub async fn get_latest_operations_for_buffers(
810 &self,
811 buffer_ids: impl IntoIterator<Item = BufferId>,
812 tx: &DatabaseTransaction,
813 ) -> Result<Vec<buffer_operation::Model>> {
814 let mut values = String::new();
815 for id in buffer_ids {
816 if !values.is_empty() {
817 values.push_str(", ");
818 }
819 write!(&mut values, "({})", id).unwrap();
820 }
821
822 if values.is_empty() {
823 return Ok(Vec::default());
824 }
825
826 let sql = format!(
827 r#"
828 SELECT
829 *
830 FROM
831 (
832 SELECT
833 *,
834 row_number() OVER (
835 PARTITION BY buffer_id
836 ORDER BY
837 epoch DESC,
838 lamport_timestamp DESC,
839 replica_id DESC
840 ) as row_number
841 FROM buffer_operations
842 WHERE
843 buffer_id in ({values})
844 ) AS last_operations
845 WHERE
846 row_number = 1
847 "#,
848 );
849
850 let stmt = Statement::from_string(self.pool.get_database_backend(), sql);
851 Ok(buffer_operation::Entity::find()
852 .from_raw_sql(stmt)
853 .all(&*tx)
854 .await?)
855 }
856}
857
858fn operation_to_storage(
859 operation: &proto::Operation,
860 buffer: &buffer::Model,
861 _format: i32,
862) -> Option<buffer_operation::ActiveModel> {
863 let (replica_id, lamport_timestamp, value) = match operation.variant.as_ref()? {
864 proto::operation::Variant::Edit(operation) => (
865 operation.replica_id,
866 operation.lamport_timestamp,
867 storage::Operation {
868 version: version_to_storage(&operation.version),
869 is_undo: false,
870 edit_ranges: operation
871 .ranges
872 .iter()
873 .map(|range| storage::Range {
874 start: range.start,
875 end: range.end,
876 })
877 .collect(),
878 edit_texts: operation.new_text.clone(),
879 undo_counts: Vec::new(),
880 },
881 ),
882 proto::operation::Variant::Undo(operation) => (
883 operation.replica_id,
884 operation.lamport_timestamp,
885 storage::Operation {
886 version: version_to_storage(&operation.version),
887 is_undo: true,
888 edit_ranges: Vec::new(),
889 edit_texts: Vec::new(),
890 undo_counts: operation
891 .counts
892 .iter()
893 .map(|entry| storage::UndoCount {
894 replica_id: entry.replica_id,
895 lamport_timestamp: entry.lamport_timestamp,
896 count: entry.count,
897 })
898 .collect(),
899 },
900 ),
901 _ => None?,
902 };
903
904 Some(buffer_operation::ActiveModel {
905 buffer_id: ActiveValue::Set(buffer.id),
906 epoch: ActiveValue::Set(buffer.epoch),
907 replica_id: ActiveValue::Set(replica_id as i32),
908 lamport_timestamp: ActiveValue::Set(lamport_timestamp as i32),
909 value: ActiveValue::Set(value.encode_to_vec()),
910 })
911}
912
913fn operation_from_storage(
914 row: buffer_operation::Model,
915 _format_version: i32,
916) -> Result<proto::operation::Variant, Error> {
917 let operation =
918 storage::Operation::decode(row.value.as_slice()).map_err(|error| anyhow!("{}", error))?;
919 let version = version_from_storage(&operation.version);
920 Ok(if operation.is_undo {
921 proto::operation::Variant::Undo(proto::operation::Undo {
922 replica_id: row.replica_id as u32,
923 lamport_timestamp: row.lamport_timestamp as u32,
924 version,
925 counts: operation
926 .undo_counts
927 .iter()
928 .map(|entry| proto::UndoCount {
929 replica_id: entry.replica_id,
930 lamport_timestamp: entry.lamport_timestamp,
931 count: entry.count,
932 })
933 .collect(),
934 })
935 } else {
936 proto::operation::Variant::Edit(proto::operation::Edit {
937 replica_id: row.replica_id as u32,
938 lamport_timestamp: row.lamport_timestamp as u32,
939 version,
940 ranges: operation
941 .edit_ranges
942 .into_iter()
943 .map(|range| proto::Range {
944 start: range.start,
945 end: range.end,
946 })
947 .collect(),
948 new_text: operation.edit_texts,
949 })
950 })
951}
952
953fn version_to_storage(version: &Vec<proto::VectorClockEntry>) -> Vec<storage::VectorClockEntry> {
954 version
955 .iter()
956 .map(|entry| storage::VectorClockEntry {
957 replica_id: entry.replica_id,
958 timestamp: entry.timestamp,
959 })
960 .collect()
961}
962
963fn version_from_storage(version: &Vec<storage::VectorClockEntry>) -> Vec<proto::VectorClockEntry> {
964 version
965 .iter()
966 .map(|entry| proto::VectorClockEntry {
967 replica_id: entry.replica_id,
968 timestamp: entry.timestamp,
969 })
970 .collect()
971}
972
973// This is currently a manual copy of the deserialization code in the client's language crate
974pub fn operation_from_wire(operation: proto::Operation) -> Option<text::Operation> {
975 match operation.variant? {
976 proto::operation::Variant::Edit(edit) => Some(text::Operation::Edit(EditOperation {
977 timestamp: clock::Lamport {
978 replica_id: edit.replica_id as text::ReplicaId,
979 value: edit.lamport_timestamp,
980 },
981 version: version_from_wire(&edit.version),
982 ranges: edit
983 .ranges
984 .into_iter()
985 .map(|range| {
986 text::FullOffset(range.start as usize)..text::FullOffset(range.end as usize)
987 })
988 .collect(),
989 new_text: edit.new_text.into_iter().map(Arc::from).collect(),
990 })),
991 proto::operation::Variant::Undo(undo) => Some(text::Operation::Undo(UndoOperation {
992 timestamp: clock::Lamport {
993 replica_id: undo.replica_id as text::ReplicaId,
994 value: undo.lamport_timestamp,
995 },
996 version: version_from_wire(&undo.version),
997 counts: undo
998 .counts
999 .into_iter()
1000 .map(|c| {
1001 (
1002 clock::Lamport {
1003 replica_id: c.replica_id as text::ReplicaId,
1004 value: c.lamport_timestamp,
1005 },
1006 c.count,
1007 )
1008 })
1009 .collect(),
1010 })),
1011 _ => None,
1012 }
1013}
1014
1015fn version_from_wire(message: &[proto::VectorClockEntry]) -> clock::Global {
1016 let mut version = clock::Global::new();
1017 for entry in message {
1018 version.observe(clock::Lamport {
1019 replica_id: entry.replica_id as text::ReplicaId,
1020 value: entry.timestamp,
1021 });
1022 }
1023 version
1024}
1025
1026fn version_to_wire(version: &clock::Global) -> Vec<proto::VectorClockEntry> {
1027 let mut message = Vec::new();
1028 for entry in version.iter() {
1029 message.push(proto::VectorClockEntry {
1030 replica_id: entry.replica_id as u32,
1031 timestamp: entry.value,
1032 });
1033 }
1034 message
1035}
1036
1037#[derive(Debug, Clone, Copy, EnumIter, DeriveColumn)]
1038enum QueryOperationSerializationVersion {
1039 OperationSerializationVersion,
1040}
1041
1042mod storage {
1043 #![allow(non_snake_case)]
1044 use prost::Message;
1045 pub const SERIALIZATION_VERSION: i32 = 1;
1046
1047 #[derive(Message)]
1048 pub struct Operation {
1049 #[prost(message, repeated, tag = "2")]
1050 pub version: Vec<VectorClockEntry>,
1051 #[prost(bool, tag = "3")]
1052 pub is_undo: bool,
1053 #[prost(message, repeated, tag = "4")]
1054 pub edit_ranges: Vec<Range>,
1055 #[prost(string, repeated, tag = "5")]
1056 pub edit_texts: Vec<String>,
1057 #[prost(message, repeated, tag = "6")]
1058 pub undo_counts: Vec<UndoCount>,
1059 }
1060
1061 #[derive(Message)]
1062 pub struct VectorClockEntry {
1063 #[prost(uint32, tag = "1")]
1064 pub replica_id: u32,
1065 #[prost(uint32, tag = "2")]
1066 pub timestamp: u32,
1067 }
1068
1069 #[derive(Message)]
1070 pub struct Range {
1071 #[prost(uint64, tag = "1")]
1072 pub start: u64,
1073 #[prost(uint64, tag = "2")]
1074 pub end: u64,
1075 }
1076
1077 #[derive(Message)]
1078 pub struct UndoCount {
1079 #[prost(uint32, tag = "1")]
1080 pub replica_id: u32,
1081 #[prost(uint32, tag = "2")]
1082 pub lamport_timestamp: u32,
1083 #[prost(uint32, tag = "3")]
1084 pub count: u32,
1085 }
1086}