Repository: libsql/libsql
File: libsql/src/local/database.rs (Rust)

Code before:
                        tracing::debug!("got InvalidPushFrameNo, retrying push");
                        self.try_push(&mut sync_ctx, &conn).await
                    } else {
                        Err(Error::Sync(err))
                    }
                }
                Err(e) => Err(e),
            }
        } else {
            self.try_pull(&mut sync_ctx, &conn).await
        }
    }

    #[cfg(feature = "sync")]
    async fn try_push(&self, sync_ctx: &mut SyncContext, conn: &Connection) -> Result<crate::database::Replicated> {
        let page_size = {
            let rows = conn
                .query("PRAGMA page_size", crate::params::Params::None)?
                .unwrap();
            let row = rows.next()?.unwrap();
            let page_size = row.get::<u32>(0)?;
            page_size
        };

        let max_frame_no = conn.wal_frame_count();
        assert!(max_frame_no > 0);

        let generation = sync_ctx.generation(); // TODO: Probe from WAL.
        let start_frame_no = sync_ctx.durable_frame_num() + 1;
        let end_frame_no = max_frame_no;

        let mut frame_no = start_frame_no;
        while frame_no <= end_frame_no {
            let frame = conn.wal_get_frame(frame_no, page_size)?;

            // The server returns its maximum frame number. To avoid resending
            // frames the server already knows about, we need to update the
            // frame number to the one returned by the server.
            let max_frame_no = sync_ctx
                .push_one_frame(frame.freeze(), generation, frame_no)
                .await?;

            if max_frame_no > frame_no {
                frame_no = max_frame_no;
            }
            frame_no += 1;
        }

        sync_ctx.write_metadata().await?;

        // TODO(lucio): this can underflow if the server previously returned a higher max_frame_no

Review comment (suggestion):
Should this be an error instead of an assert? Can we ensure that this assert will never fire, if so I think its worth a comment if you want to keep it to explain why this is safe.

Code after:
                        tracing::debug!("got InvalidPushFrameNo, retrying push");
                        self.try_push(&mut sync_ctx, &conn).await
                    } else {
                        Err(Error::Sync(err))
                    }
                }
                Err(e) => Err(e),
            }
        } else {
            self.try_pull(&mut sync_ctx, &conn).await
        }
    }

    #[cfg(feature = "sync")]
    async fn try_push(&self, sync_ctx: &mut SyncContext, conn: &Connection) -> Result<crate::database::Replicated> {
        let page_size = {
            let rows = conn
                .query("PRAGMA page_size", crate::params::Params::None)?
                .unwrap();
            let row = rows.next()?.unwrap();
            let page_size = row.get::<u32>(0)?;
            page_size
        };

        let max_frame_no = conn.wal_frame_count();
        if max_frame_no == 0 {
            return Ok(crate::database::Replicated {
                frame_no: None,
                frames_synced: 0,
            });
        }

        let generation = sync_ctx.generation(); // TODO: Probe from WAL.
        let start_frame_no = sync_ctx.durable_frame_num() + 1;
        let end_frame_no = max_frame_no;

        let mut frame_no = start_frame_no;
        while frame_no <= end_frame_no {
            let frame = conn.wal_get_frame(frame_no, page_size)?;

            // The server returns its maximum frame number. To avoid resending
            // frames the server already knows about, we need to update the
            // frame number to the one returned by the server.
            let max_frame_no = sync_ctx
                .push_one_frame(frame.freeze(), generation, frame_no)
                .await?;

            if max_frame_no > frame_no {
                frame_no = max_frame_no;
            }
            frame_no += 1;