-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement snapshot restore logic (#2398)
* Implement basic restore logic * Implement restore logic for accounts * Implement restore logic for ontology * Implement restore logic for entities * Satisfy rustfmt * Move ontology metadata into own module * Use `INTEGER` instead of `INT4` * Update apps/hash-graph/lib/graph/src/snapshot.rs Co-authored-by: Ahmad Sattar <thehabbos007@gmail.com> * Clarify documentation on `restore` function * Use `try_fold` instead of `fold` --------- Co-authored-by: Ahmad Sattar <thehabbos007@gmail.com>
- Loading branch information
1 parent
5df489a
commit a92b712
Showing
31 changed files
with
2,812 additions
and
199 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,9 @@ | ||
mod batch; | ||
mod channel; | ||
mod table; | ||
|
||
pub use self::{ | ||
batch::AccountRowBatch, | ||
channel::{channel, AccountReceiver, AccountSender}, | ||
table::AccountRow, | ||
}; |
This file contains 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,73 @@ | ||
use async_trait::async_trait; | ||
use error_stack::{IntoReport, Result, ResultExt}; | ||
use tokio_postgres::GenericClient; | ||
|
||
use crate::{ | ||
snapshot::{account::AccountRow, WriteBatch}, | ||
store::{AsClient, InsertionError, PostgresStore}, | ||
}; | ||
|
||
pub enum AccountRowBatch { | ||
Accounts(Vec<AccountRow>), | ||
} | ||
|
||
#[async_trait] | ||
impl<C: AsClient> WriteBatch<C> for AccountRowBatch { | ||
async fn begin(postgres_client: &PostgresStore<C>) -> Result<(), InsertionError> { | ||
postgres_client | ||
.as_client() | ||
.client() | ||
.simple_query( | ||
r" | ||
CREATE TEMPORARY TABLE accounts_tmp | ||
(LIKE accounts INCLUDING ALL) | ||
ON COMMIT DROP; | ||
", | ||
) | ||
.await | ||
.into_report() | ||
.change_context(InsertionError)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn write(&self, postgres_client: &PostgresStore<C>) -> Result<(), InsertionError> { | ||
let client = postgres_client.as_client().client(); | ||
match self { | ||
Self::Accounts(accounts) => { | ||
let rows = client | ||
.query( | ||
r" | ||
INSERT INTO accounts_tmp | ||
SELECT DISTINCT * FROM UNNEST($1::accounts[]) | ||
ON CONFLICT DO NOTHING | ||
RETURNING 1; | ||
", | ||
&[accounts], | ||
) | ||
.await | ||
.into_report() | ||
.change_context(InsertionError)?; | ||
if !rows.is_empty() { | ||
tracing::info!("Read {} accounts", rows.len()); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn commit(postgres_client: &PostgresStore<C>) -> Result<(), InsertionError> { | ||
postgres_client | ||
.as_client() | ||
.client() | ||
.simple_query( | ||
r" | ||
INSERT INTO accounts SELECT * FROM accounts_tmp; | ||
", | ||
) | ||
.await | ||
.into_report() | ||
.change_context(InsertionError)?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.