firestore 0.57.1

Library provides a simple API for Google Firestore and own Serde serializer based on efficient gRPC API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::{
    FirestoreBatch, FirestoreBatchWriteResponse, FirestoreBatchWriter, FirestoreDb,
    FirestoreRequestOptions, FirestoreResult, FirestoreWriteResult,
};
use async_trait::async_trait;
use futures::stream::BoxStream;
use futures::{StreamExt, TryStreamExt};
use gcloud_sdk::google::firestore::v1::{Write, WriteRequest};
use rsb_derive::*;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::{mpsc, RwLock};
use tokio::task::JoinHandle;

use crate::timestamp_utils::from_timestamp;
use tracing::*;

#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct FirestoreStreamingBatchWriteOptions {
    #[default = "Duration::from_millis(500)"]
    pub throttle_batch_duration: Duration,

    /// Request options (e.g. request tags) for the streaming write requests.
    pub request_options: Option<FirestoreRequestOptions>,
}

pub struct FirestoreStreamingBatchWriter {
    pub db: FirestoreDb,
    pub options: FirestoreStreamingBatchWriteOptions,
    pub batch_span: Span,
    finished: Arc<AtomicBool>,
    writer: UnboundedSender<WriteRequest>,
    thread: Option<JoinHandle<()>>,
    last_token: Arc<RwLock<Vec<u8>>>,
    sent_counter: Arc<AtomicU64>,
    received_counter: Arc<AtomicU64>,
    init_wait_reader: UnboundedReceiver<()>,
}

impl Drop for FirestoreStreamingBatchWriter {
    fn drop(&mut self) {
        if !self.finished.load(Ordering::Relaxed) {
            self.batch_span
                .in_scope(|| warn!("Batch was not finished."));
        }
    }
}

impl FirestoreStreamingBatchWriter {
    /// Opens a streaming batch writer against `db` and returns it with the stream of per-batch
    /// responses.
    ///
    /// Most callers get one from [`FirestoreDb::create_streaming_batch_writer`] or
    /// [`FirestoreDb::create_streaming_batch_writer_with_options`] rather than calling this
    /// directly. This backs Firestore's streaming `Write` RPC: writes queued through
    /// [`new_batch`](Self::new_batch) and [`FirestoreBatch::write`] go over one long-lived stream
    /// instead of one request per batch, and `options.throttle_batch_duration` - 500ms by
    /// default - paces how often a batch is flushed onto the wire, which is what keeps a fast
    /// producer under Firestore's per-stream write-rate limit; a
    /// [`FirestoreSimpleBatchWriter`](crate::FirestoreSimpleBatchWriter) has no such limit to
    /// manage and is the simpler choice unless batches are being sent back-to-back. The returned
    /// stream yields one [`FirestoreBatchWriteResponse`] per batch, in send order; consume it
    /// (for example from a spawned task) or responses back up unread. Call
    /// [`finish`](Self::finish) when done - dropping the writer instead only logs a warning and
    /// leaves the stream's background task running until it notices.
    ///
    /// Returns an error if the initial handshake with the `Write` RPC fails.
    pub async fn new<'b>(
        db: FirestoreDb,
        options: FirestoreStreamingBatchWriteOptions,
    ) -> FirestoreResult<(
        FirestoreStreamingBatchWriter,
        BoxStream<'b, FirestoreResult<FirestoreBatchWriteResponse>>,
    )> {
        let batch_span = span!(Level::DEBUG, "Firestore Batch Write");

        let (requests_writer, requests_receiver) = mpsc::unbounded_channel::<WriteRequest>();
        let (responses_writer, responses_receiver) =
            mpsc::unbounded_channel::<FirestoreResult<FirestoreBatchWriteResponse>>();
        let (init_wait_sender, mut init_wait_reader) = mpsc::unbounded_channel::<()>();

        let finished = Arc::new(AtomicBool::new(false));
        let thread_finished = finished.clone();

        let sent_counter = Arc::new(AtomicU64::new(0));
        let thread_sent_counter = sent_counter.clone();

        let received_counter = Arc::new(AtomicU64::new(0));
        let thread_received_counter = received_counter.clone();

        let last_token: Arc<RwLock<Vec<u8>>> = Arc::new(RwLock::new(vec![]));
        let thread_last_token = last_token.clone();

        let mut thread_db_client = db.client().get();
        let thread_options = options.clone();

        let thread = tokio::spawn(async move {
            let stream = {
                use tokio_stream::StreamExt;
                tokio_stream::wrappers::UnboundedReceiverStream::new(requests_receiver)
                    .throttle(thread_options.throttle_batch_duration)
            };
            match thread_db_client.write(stream).await {
                Ok(response) => {
                    let mut response_stream = response.into_inner().boxed();
                    loop {
                        let response_result = response_stream.try_next().await;
                        let received_counter = thread_received_counter.load(Ordering::Relaxed);

                        match response_result {
                            Ok(Some(response)) => {
                                {
                                    let mut locked = thread_last_token.write().await;
                                    *locked = response.stream_token;
                                }

                                if received_counter == 0 {
                                    init_wait_sender.send(()).ok();
                                } else {
                                    let write_results: FirestoreResult<Vec<FirestoreWriteResult>> =
                                        response
                                            .write_results
                                            .into_iter()
                                            .map(|s| s.try_into())
                                            .collect();

                                    match write_results {
                                        Ok(write_results) => {
                                            responses_writer
                                                .send(Ok(FirestoreBatchWriteResponse::new(
                                                    received_counter - 1,
                                                    write_results,
                                                    vec![],
                                                )
                                                .opt_commit_time(
                                                    response
                                                        .commit_time
                                                        .and_then(|ts| from_timestamp(ts).ok()),
                                                )))
                                                .ok();
                                        }
                                        Err(err) => {
                                            error!(
                                                %err,
                                                received_counter,
                                                "Batch write operation failed.",
                                            );
                                            responses_writer.send(Err(err)).ok();
                                            break;
                                        }
                                    }
                                }
                            }
                            Ok(None) => {
                                responses_writer
                                    .send(Ok(FirestoreBatchWriteResponse::new(
                                        received_counter - 1,
                                        vec![],
                                        vec![],
                                    )))
                                    .ok();
                                break;
                            }
                            Err(err) if err.code() == gcloud_sdk::tonic::Code::Cancelled => {
                                debug!(received_counter, "Batch write operation finished.");
                                responses_writer
                                    .send(Ok(FirestoreBatchWriteResponse::new(
                                        received_counter - 1,
                                        vec![],
                                        vec![],
                                    )))
                                    .ok();
                                break;
                            }
                            Err(err) => {
                                error!(
                                    %err,
                                    received_counter,
                                    "Batch write operation failed.",
                                );
                                responses_writer.send(Err(err.into())).ok();
                                break;
                            }
                        }

                        {
                            let _locked = thread_last_token.read().await;
                            if thread_finished.load(Ordering::Relaxed)
                                && thread_sent_counter.load(Ordering::Relaxed) == received_counter
                            {
                                init_wait_sender.send(()).ok();
                                break;
                            }
                        }

                        thread_received_counter.fetch_add(1, Ordering::Relaxed);
                    }

                    {
                        let _locked = thread_last_token.write().await;
                        thread_finished.store(true, Ordering::Relaxed);
                        init_wait_sender.send(()).ok();
                    }
                }
                Err(err) => {
                    error!(
                        %err,
                        "Batch write operation failed.",
                    );
                    responses_writer.send(Err(err.into())).ok();
                }
            }
        });

        requests_writer.send(WriteRequest {
            database: db.get_database_path().to_string(),
            stream_id: "".to_string(),
            writes: vec![],
            stream_token: vec![],
            labels: HashMap::new(),
            request_options: db.resolve_request_options(options.request_options.as_ref()),
        })?;

        init_wait_reader.recv().await;

        let responses_stream =
            tokio_stream::wrappers::UnboundedReceiverStream::new(responses_receiver).boxed();

        Ok((
            Self {
                db,
                options,
                batch_span,
                finished,
                writer: requests_writer,
                thread: Some(thread),
                last_token,
                sent_counter,
                received_counter,
                init_wait_reader,
            },
            responses_stream,
        ))
    }

    /// Waits for every sent write's response, then closes the stream and joins its background
    /// task.
    ///
    /// Always call this instead of letting the writer drop. It waits for pending responses to
    /// arrive, so the response stream returned by [`new`](Self::new) must already be consumed
    /// (for example from a spawned task) before calling this, or it blocks forever waiting for
    /// responses nobody is reading.
    pub async fn finish(mut self) {
        let locked = self.last_token.write().await;

        if !self.finished.load(Ordering::Relaxed) {
            self.finished.store(true, Ordering::Relaxed);

            if self.sent_counter.load(Ordering::Relaxed)
                > self.received_counter.load(Ordering::Relaxed) - 1
            {
                drop(locked);
                debug!("Still waiting to receive responses for batch writes.");
                self.init_wait_reader.recv().await;
            } else {
                drop(locked);
            }

            self.writer
                .send(WriteRequest {
                    database: self.db.get_database_path().to_string(),
                    stream_id: "".to_string(),
                    writes: vec![],
                    stream_token: {
                        let locked = self.last_token.read().await;
                        locked.clone()
                    },
                    labels: HashMap::new(),
                    request_options: self
                        .db
                        .resolve_request_options(self.options.request_options.as_ref()),
                })
                .ok();
        } else {
            drop(locked);
        }

        if let Some(thread) = self.thread.take() {
            let _ = tokio::join!(thread);
        }
    }

    async fn write_iterator<I>(&self, writes: I) -> FirestoreResult<()>
    where
        I: IntoIterator,
        I::Item: Into<Write>,
    {
        self.sent_counter.fetch_add(1, Ordering::Relaxed);

        Ok(self.writer.send(WriteRequest {
            database: self.db.get_database_path().to_string(),
            stream_id: "".to_string(),
            writes: writes.into_iter().map(|write| write.into()).collect(),
            stream_token: {
                let locked = self.last_token.read().await;
                locked.clone()
            },
            labels: HashMap::new(),
            request_options: self
                .db
                .resolve_request_options(self.options.request_options.as_ref()),
        })?)
    }

    /// Starts a new batch of writes against this writer.
    ///
    /// Queue writes on it with [`FirestoreBatch`] methods such as `update_object`, then send it
    /// with [`FirestoreBatch::write`].
    pub fn new_batch(&self) -> FirestoreBatch<'_, FirestoreStreamingBatchWriter> {
        FirestoreBatch::new(&self.db, self)
    }
}

#[async_trait]
impl FirestoreBatchWriter for FirestoreStreamingBatchWriter {
    type WriteResult = ();

    async fn write(&self, writes: Vec<Write>) -> FirestoreResult<()> {
        self.write_iterator(writes).await
    }
}

impl FirestoreDb {
    /// Opens a streaming batch writer with default options.
    ///
    /// Equivalent to
    /// `create_streaming_batch_writer_with_options(FirestoreStreamingBatchWriteOptions::new())`.
    /// See [`FirestoreStreamingBatchWriter::new`] for the throttling behavior, the response
    /// stream and the failure mode, and prefer
    /// [`create_simple_batch_writer`](Self::create_simple_batch_writer) unless enough batches are
    /// sent back-to-back that one `BatchWrite` request per batch becomes the bottleneck.
    ///
    /// # Examples
    /// ```rust,no_run
    /// use firestore::*;
    /// use futures::TryStreamExt;
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Clone, Deserialize, Serialize)]
    /// struct MyTestStructure {
    ///     some_id: String,
    /// }
    ///
    /// # async fn example(db: FirestoreDb) -> FirestoreResult<()> {
    /// let (batch_writer, mut responses) = db.create_streaming_batch_writer().await?;
    ///
    /// let responses_task = tokio::spawn(async move {
    ///     while let Ok(Some(response)) = responses.try_next().await {
    ///         println!("{response:?}");
    ///     }
    /// });
    ///
    /// let mut current_batch = batch_writer.new_batch();
    /// for idx in 0..1000 {
    ///     let doc = MyTestStructure {
    ///         some_id: format!("doc-{idx}"),
    ///     };
    ///
    ///     db.fluent()
    ///         .update()
    ///         .in_col("my-collection")
    ///         .document_id(&doc.some_id)
    ///         .object(&doc)
    ///         .add_to_batch(&mut current_batch)?;
    ///
    ///     if idx % 100 == 0 {
    ///         current_batch.write().await?;
    ///         current_batch = batch_writer.new_batch();
    ///     }
    /// }
    ///
    /// batch_writer.finish().await;
    /// let _ = tokio::join!(responses_task);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create_streaming_batch_writer<'b>(
        &self,
    ) -> FirestoreResult<(
        FirestoreStreamingBatchWriter,
        BoxStream<'b, FirestoreResult<FirestoreBatchWriteResponse>>,
    )> {
        self.create_streaming_batch_writer_with_options(FirestoreStreamingBatchWriteOptions::new())
            .await
    }

    /// Opens a streaming batch writer with explicit `options`.
    ///
    /// Most usefully `options.throttle_batch_duration`, which paces how often queued writes are
    /// flushed onto Firestore's streaming `Write` RPC to stay under its per-stream write-rate
    /// limit; see [`FirestoreStreamingBatchWriter::new`] for the full behavior.
    ///
    /// Returns an error if the initial handshake with the `Write` RPC fails.
    pub async fn create_streaming_batch_writer_with_options<'b>(
        &self,
        options: FirestoreStreamingBatchWriteOptions,
    ) -> FirestoreResult<(
        FirestoreStreamingBatchWriter,
        BoxStream<'b, FirestoreResult<FirestoreBatchWriteResponse>>,
    )> {
        FirestoreStreamingBatchWriter::new(self.clone(), options).await
    }
}