Skip to content

Commit

Permalink
Implement update and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
sanketkedia committed Dec 3, 2024
1 parent 2732111 commit 4e2eb67
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
16 changes: 16 additions & 0 deletions rust/index/src/spann/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,22 @@ impl SpannIndexWriter {
self.add_postings_list(id, version, embedding).await
}

pub async fn update(
&self,
id: u32,
embedding: &[f32],
) -> Result<(), SpannIndexWriterConstructionError> {
// Delete and then add.
self.delete(id).await?;
self.add(id, embedding).await
}

pub async fn delete(&self, id: u32) -> Result<(), SpannIndexWriterConstructionError> {
let mut version_map_guard = self.versions_map.write();
version_map_guard.versions_map.insert(id, 0);
Ok(())
}

// TODO(Sanket): Change the error types.
pub async fn commit(self) -> Result<SpannIndexFlusher, SpannIndexWriterConstructionError> {
// Pl list.
Expand Down
36 changes: 33 additions & 3 deletions rust/worker/src/segment/spann_segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ impl SpannSegmentWriter {
.await
.map_err(|_| SpannSegmentWriterError::SpannSegmentWriterAddRecordError)
}

async fn delete(
&self,
record: &MaterializedLogRecord<'_>,
) -> Result<(), SpannSegmentWriterError> {
self.index
.delete(record.offset_id)
.await
.map_err(|_| SpannSegmentWriterError::SpannSegmentWriterAddRecordError)
}

async fn update(
&self,
record: &MaterializedLogRecord<'_>,
) -> Result<(), SpannSegmentWriterError> {
self.index
.update(record.offset_id, record.merged_embeddings())
.await
.map_err(|_| SpannSegmentWriterError::SpannSegmentWriterAddRecordError)
}
}

struct SpannSegmentFlusher {
Expand All @@ -222,10 +242,20 @@ impl<'a> SegmentWriter<'a> for SpannSegmentWriter {
.await
.map_err(|_| ApplyMaterializedLogError::BlockfileSet)?;
}
// TODO(Sanket): Implement other operations.
_ => {
todo!()
MaterializedLogOperation::UpdateExisting
| MaterializedLogOperation::OverwriteExisting => {
self.update(record)
.await
.map_err(|_| ApplyMaterializedLogError::BlockfileUpdate)?;
}
MaterializedLogOperation::DeleteExisting => {
self.delete(record)
.await
.map_err(|_| ApplyMaterializedLogError::BlockfileDelete)?;
}
MaterializedLogOperation::Initial => panic!(
"Invariant violation. Mat records should not contain logs in initial state"
),
}
}
Ok(())
Expand Down

0 comments on commit 4e2eb67

Please sign in to comment.