Skip to content

Commit

Permalink
less invasive but should still work with mvr-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wlmyng committed Nov 19, 2024
1 parent 92760b8 commit cd73e07
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions crates/sui-indexer/src/indexer_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,40 +121,17 @@ impl IndexerReader {

let mut connection = self.pool.get().await?;

let mut history_query = objects_history::table
.filter(objects_history::dsl::object_id.eq(object_id.to_vec()))
.into_boxed();

if let Some(version) = version {
history_query = history_query
.filter(objects_history::dsl::object_version.eq(version.value() as i64));
}

let history_latest = history_query
.order_by(objects_history::dsl::object_version.desc())
.first::<StoredHistoryObject>(&mut connection)
.await
.optional()?;

if let Some(history_record) = history_latest {
return Ok(Some(history_record.try_into()?));
}

let mut snapshot_query = objects_snapshot::table
.filter(objects_snapshot::dsl::object_id.eq(object_id.to_vec()))
let mut query = objects::table
.filter(objects::object_id.eq(object_id.to_vec()))
.into_boxed();

if let Some(version) = version {
snapshot_query = snapshot_query
.filter(objects_snapshot::dsl::object_version.eq(version.value() as i64));
query = query.filter(objects::object_version.eq(version.value() as i64))
}

snapshot_query
.first::<StoredObjectSnapshot>(&mut connection)
query
.first::<StoredObject>(&mut connection)
.await
.optional()?
.map(|o| o.try_into())
.transpose()
.optional()
.map_err(Into::into)
}

Expand Down Expand Up @@ -1480,7 +1457,31 @@ impl ConnectionAsObjectStore {
Ok(Self { inner: connection })
}

fn get_object_from_db(
fn get_object_from_objects(
&self,
object_id: &ObjectID,
version: Option<VersionNumber>,
) -> Result<Option<StoredObject>, IndexerError> {
use diesel::RunQueryDsl;

let mut guard = self.inner.lock().unwrap();
let connection: &mut diesel_async::async_connection_wrapper::AsyncConnectionWrapper<_> =
&mut guard;

let mut query = objects::table
.filter(objects::object_id.eq(object_id.to_vec()))
.into_boxed();
if let Some(version) = version {
query = query.filter(objects::object_version.eq(version.value() as i64))
}

query
.first::<StoredObject>(connection)
.optional()
.map_err(Into::into)
}

fn get_object_from_history(
&self,
object_id: &ObjectID,
version: Option<VersionNumber>,
Expand Down Expand Up @@ -1531,12 +1532,14 @@ impl ConnectionAsObjectStore {
object_id: &ObjectID,
version: Option<VersionNumber>,
) -> Result<Option<Object>, IndexerError> {
let Some(stored_package) = self.get_object_from_db(object_id, version)? else {
return Ok(None);
};
let mut result = self.get_object_from_objects(object_id, version)?;

let object = stored_package.try_into()?;
Ok(Some(object))
// This is for mvr-mode, which doesn't maintain an `objects` table.
if result.is_none() {
result = self.get_object_from_history(object_id, version)?;
}

result.map(|o| o.try_into()).transpose().map_err(Into::into)
}
}

Expand Down

0 comments on commit cd73e07

Please sign in to comment.