- Notifications
You must be signed in to change notification settings - Fork328
Batch upsert documents#1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
levkk wants to merge1 commit intomasterChoose a base branch fromlevkk-sdk-batching
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletionpgml-sdks/pgml/Cargo.lock
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
2 changes: 1 addition & 1 deletionpgml-sdks/pgml/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletionspgml-sdks/pgml/src/batch.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
//! Upsert documents in batches. | ||
#[cfg(feature = "rust_bridge")] | ||
use rust_bridge::{alias, alias_methods}; | ||
use tracing::instrument; | ||
use crate::{types::Json, Collection}; | ||
#[cfg(feature = "python")] | ||
use crate::{collection::CollectionPython, types::JsonPython}; | ||
#[cfg(feature = "c")] | ||
use crate::{collection::CollectionC, languages::c::JsonC}; | ||
/// A batch of documents staged for upsert | ||
#[cfg_attr(feature = "rust_bridge", derive(alias))] | ||
#[derive(Debug, Clone)] | ||
pub struct Batch { | ||
collection: Collection, | ||
pub(crate) documents: Vec<Json>, | ||
pub(crate) size: i64, | ||
pub(crate) args: Option<Json>, | ||
} | ||
#[cfg_attr(feature = "rust_bridge", alias_methods(new, upsert_documents, finish,))] | ||
impl Batch { | ||
/// Create a new upsert batch. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `collection` - The collection to upsert documents to. | ||
/// * `size` - The size of the batch. | ||
/// * `args` - Optional arguments to pass to the upsert operation. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use pgml::{Collection, Batch}; | ||
/// | ||
/// let collection = Collection::new("my_collection"); | ||
/// let batch = Batch::new(&collection, 100, None); | ||
/// ``` | ||
pub fn new(collection: &Collection, size: i64, args: Option<Json>) -> Self { | ||
Self { | ||
collection: collection.clone(), | ||
args, | ||
documents: Vec::new(), | ||
size, | ||
} | ||
} | ||
/// Upsert documents into the collection. If the batch is full, save the documents. | ||
/// | ||
/// When using this method, remember to call [finish](Batch::finish) to save any remaining documents | ||
/// in the last batch. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `documents` - The documents to upsert. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use pgml::{Collection, Batch}; | ||
/// use serde_json::json; | ||
/// | ||
/// let collection = Collection::new("my_collection"); | ||
/// let mut batch = Batch::new(&collection, 100, None); | ||
/// | ||
/// batch.upsert_documents(vec![json!({"id": 1}), json!({"id": 2})]).await?; | ||
/// batch.finish().await?; | ||
/// ``` | ||
#[instrument(skip(self))] | ||
pub async fn upsert_documents(&mut self, documents: Vec<Json>) -> anyhow::Result<()> { | ||
for document in documents { | ||
if self.size as usize >= self.documents.len() { | ||
self.collection | ||
.upsert_documents(std::mem::take(&mut self.documents), self.args.clone()) | ||
.await?; | ||
self.documents.clear(); | ||
} | ||
self.documents.push(document); | ||
} | ||
Ok(()) | ||
} | ||
/// Save any remaining documents in the last batch. | ||
#[instrument(skip(self))] | ||
pub async fn finish(&mut self) -> anyhow::Result<()> { | ||
if !self.documents.is_empty() { | ||
self.collection | ||
.upsert_documents(std::mem::take(&mut self.documents), self.args.clone()) | ||
.await?; | ||
} | ||
Ok(()) | ||
} | ||
} |
56 changes: 40 additions & 16 deletionspgml-sdks/pgml/src/collection.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletionspgml-sdks/pgml/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.