Skip to content

Commit 1a2f6bc

Browse files
authored
Allow threads (#15)
* Allow threads * Revert changes
1 parent 6144879 commit 1a2f6bc

File tree

1 file changed

+99
-74
lines changed
  • object-store-internal/src

1 file changed

+99
-74
lines changed

object-store-internal/src/lib.rs

Lines changed: 99 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,13 @@ impl PyObjectStore {
508508

509509
/// Save the provided bytes to the specified location.
510510
#[pyo3(text_signature = "($self, location, bytes)")]
511-
fn put(&self, location: PyPath, bytes: Vec<u8>) -> PyResult<()> {
512-
self.rt
513-
.block_on(self.inner.put(&location.into(), bytes.into()))
514-
.map_err(ObjectStoreError::from)?;
515-
Ok(())
511+
fn put(&self, py: Python, location: PyPath, bytes: Vec<u8>) -> PyResult<()> {
512+
py.allow_threads(|| {
513+
self.rt
514+
.block_on(self.inner.put(&location.into(), bytes.into()))
515+
.map_err(ObjectStoreError::from)?;
516+
Ok(())
517+
})
516518
}
517519

518520
/// Save the provided bytes to the specified location.
@@ -535,13 +537,13 @@ impl PyObjectStore {
535537

536538
/// Return the bytes that are stored at the specified location.
537539
#[pyo3(text_signature = "($self, location)")]
538-
fn get(&self, location: PyPath) -> PyResult<Cow<[u8]>> {
539-
let obj = self
540-
.rt
541-
.block_on(get_bytes(self.inner.as_ref(), &location.into()))
542-
.map_err(ObjectStoreError::from)?;
543-
Ok(Cow::Owned(obj.to_vec()))
544-
}
540+
fn get(&self, py: Python, location: PyPath) -> PyResult<Cow<[u8]>> {
541+
py.allow_threads(|| {
542+
let obj = self
543+
.rt
544+
.block_on(get_bytes(self.inner.as_ref(), &location.into()))
545+
.map_err(ObjectStoreError::from)?;
546+
Ok(Cow::Owned(obj.to_vec()))
545547

546548
/// Return the bytes that are stored at the specified location.
547549
#[pyo3(text_signature = "($self, location)")]
@@ -557,17 +559,24 @@ impl PyObjectStore {
557559

558560
/// Return the bytes that are stored at the specified location in the given byte range
559561
#[pyo3(text_signature = "($self, location, start, length)")]
560-
fn get_range(&self, location: PyPath, start: usize, length: usize) -> PyResult<Cow<[u8]>> {
561-
let range = std::ops::Range {
562-
start,
563-
end: start + length,
564-
};
565-
let obj = self
566-
.rt
567-
.block_on(self.inner.get_range(&location.into(), range))
568-
.map_err(ObjectStoreError::from)?;
569-
Ok(Cow::Owned(obj.to_vec()))
570-
}
562+
fn get_range(
563+
&self,
564+
py: Python,
565+
location: PyPath,
566+
start: usize,
567+
length: usize,
568+
) -> PyResult<Cow<[u8]>> {
569+
py.allow_threads(|| {
570+
let range = std::ops::Range {
571+
start,
572+
end: start + length,
573+
};
574+
let obj = self
575+
.rt
576+
.block_on(self.inner.get_range(&location.into(), range))
577+
.map_err(ObjectStoreError::from)?
578+
.to_vec();
579+
Ok(Cow::Owned(obj.to_vec()))
571580

572581
/// Return the bytes that are stored at the specified location in the given byte range
573582
#[pyo3(text_signature = "($self, location, start, length)")]
@@ -595,12 +604,14 @@ impl PyObjectStore {
595604

596605
/// Return the metadata for the specified location
597606
#[pyo3(text_signature = "($self, location)")]
598-
fn head(&self, location: PyPath) -> PyResult<PyObjectMeta> {
599-
let meta = self
600-
.rt
601-
.block_on(self.inner.head(&location.into()))
602-
.map_err(ObjectStoreError::from)?;
603-
Ok(meta.into())
607+
fn head(&self, py: Python, location: PyPath) -> PyResult<PyObjectMeta> {
608+
py.allow_threads(|| {
609+
let meta = self
610+
.rt
611+
.block_on(self.inner.head(&location.into()))
612+
.map_err(ObjectStoreError::from)?;
613+
Ok(meta.into())
614+
})
604615
}
605616

606617
/// Return the metadata for the specified location
@@ -618,11 +629,13 @@ impl PyObjectStore {
618629

619630
/// Delete the object at the specified location.
620631
#[pyo3(text_signature = "($self, location)")]
621-
fn delete(&self, location: PyPath) -> PyResult<()> {
622-
self.rt
623-
.block_on(self.inner.delete(&location.into()))
624-
.map_err(ObjectStoreError::from)?;
625-
Ok(())
632+
fn delete(&self, py: Python, location: PyPath) -> PyResult<()> {
633+
py.allow_threads(|| {
634+
self.rt
635+
.block_on(self.inner.delete(&location.into()))
636+
.map_err(ObjectStoreError::from)?;
637+
Ok(())
638+
})
626639
}
627640

628641
/// Delete the object at the specified location.
@@ -643,17 +656,19 @@ impl PyObjectStore {
643656
/// Prefixes are evaluated on a path segment basis, i.e. `foo/bar/` is a prefix
644657
/// of `foo/bar/x` but not of `foo/bar_baz/x`.
645658
#[pyo3(text_signature = "($self, prefix)")]
646-
fn list(&self, prefix: Option<PyPath>) -> PyResult<Vec<PyObjectMeta>> {
647-
Ok(self
648-
.rt
649-
.block_on(flatten_list_stream(
650-
self.inner.as_ref(),
651-
prefix.map(Path::from).as_ref(),
652-
))
653-
.map_err(ObjectStoreError::from)?
654-
.into_iter()
655-
.map(PyObjectMeta::from)
656-
.collect())
659+
fn list(&self, py: Python, prefix: Option<PyPath>) -> PyResult<Vec<PyObjectMeta>> {
660+
py.allow_threads(|| {
661+
Ok(self
662+
.rt
663+
.block_on(flatten_list_stream(
664+
self.inner.as_ref(),
665+
prefix.map(Path::from).as_ref(),
666+
))
667+
.map_err(ObjectStoreError::from)?
668+
.into_iter()
669+
.map(PyObjectMeta::from)
670+
.collect())
671+
})
657672
}
658673

659674
/// List all the objects with the given prefix.
@@ -682,15 +697,17 @@ impl PyObjectStore {
682697
/// Prefixes are evaluated on a path segment basis, i.e. `foo/bar/` is a prefix
683698
/// of `foo/bar/x` but not of `foo/bar_baz/x`.
684699
#[pyo3(text_signature = "($self, prefix)")]
685-
fn list_with_delimiter(&self, prefix: Option<PyPath>) -> PyResult<PyListResult> {
686-
let list = self
687-
.rt
688-
.block_on(
689-
self.inner
690-
.list_with_delimiter(prefix.map(Path::from).as_ref()),
691-
)
692-
.map_err(ObjectStoreError::from)?;
693-
Ok(list.into())
700+
fn list_with_delimiter(&self, py: Python, prefix: Option<PyPath>) -> PyResult<PyListResult> {
701+
py.allow_threads(|| {
702+
let list = self
703+
.rt
704+
.block_on(
705+
self.inner
706+
.list_with_delimiter(prefix.map(Path::from).as_ref()),
707+
)
708+
.map_err(ObjectStoreError::from)?;
709+
Ok(list.into())
710+
})
694711
}
695712

696713
/// List objects with the given prefix and an implementation specific
@@ -719,11 +736,13 @@ impl PyObjectStore {
719736
///
720737
/// If there exists an object at the destination, it will be overwritten.
721738
#[pyo3(text_signature = "($self, from, to)")]
722-
fn copy(&self, from: PyPath, to: PyPath) -> PyResult<()> {
723-
self.rt
724-
.block_on(self.inner.copy(&from.into(), &to.into()))
725-
.map_err(ObjectStoreError::from)?;
726-
Ok(())
739+
fn copy(&self, py: Python, from: PyPath, to: PyPath) -> PyResult<()> {
740+
py.allow_threads(|| {
741+
self.rt
742+
.block_on(self.inner.copy(&from.into(), &to.into()))
743+
.map_err(ObjectStoreError::from)?;
744+
Ok(())
745+
})
727746
}
728747

729748
/// Copy an object from one path to another in the same object store.
@@ -745,11 +764,13 @@ impl PyObjectStore {
745764
///
746765
/// Will return an error if the destination already has an object.
747766
#[pyo3(text_signature = "($self, from, to)")]
748-
fn copy_if_not_exists(&self, from: PyPath, to: PyPath) -> PyResult<()> {
749-
self.rt
750-
.block_on(self.inner.copy_if_not_exists(&from.into(), &to.into()))
751-
.map_err(ObjectStoreError::from)?;
752-
Ok(())
767+
fn copy_if_not_exists(&self, py: Python, from: PyPath, to: PyPath) -> PyResult<()> {
768+
py.allow_threads(|| {
769+
self.rt
770+
.block_on(self.inner.copy_if_not_exists(&from.into(), &to.into()))
771+
.map_err(ObjectStoreError::from)?;
772+
Ok(())
773+
})
753774
}
754775

755776
/// Copy an object from one path to another, only if destination is empty.
@@ -779,11 +800,13 @@ impl PyObjectStore {
779800
///
780801
/// If there exists an object at the destination, it will be overwritten.
781802
#[pyo3(text_signature = "($self, from, to)")]
782-
fn rename(&self, from: PyPath, to: PyPath) -> PyResult<()> {
783-
self.rt
784-
.block_on(self.inner.rename(&from.into(), &to.into()))
785-
.map_err(ObjectStoreError::from)?;
786-
Ok(())
803+
fn rename(&self, py: Python, from: PyPath, to: PyPath) -> PyResult<()> {
804+
py.allow_threads(|| {
805+
self.rt
806+
.block_on(self.inner.rename(&from.into(), &to.into()))
807+
.map_err(ObjectStoreError::from)?;
808+
Ok(())
809+
})
787810
}
788811

789812
/// Move an object from one path to another in the same object store.
@@ -808,11 +831,13 @@ impl PyObjectStore {
808831
///
809832
/// Will return an error if the destination already has an object.
810833
#[pyo3(text_signature = "($self, from, to)")]
811-
fn rename_if_not_exists(&self, from: PyPath, to: PyPath) -> PyResult<()> {
812-
self.rt
813-
.block_on(self.inner.rename_if_not_exists(&from.into(), &to.into()))
814-
.map_err(ObjectStoreError::from)?;
815-
Ok(())
834+
fn rename_if_not_exists(&self, py: Python, from: PyPath, to: PyPath) -> PyResult<()> {
835+
py.allow_threads(|| {
836+
self.rt
837+
.block_on(self.inner.rename_if_not_exists(&from.into(), &to.into()))
838+
.map_err(ObjectStoreError::from)?;
839+
Ok(())
840+
})
816841
}
817842

818843
/// Move an object from one path to another in the same object store.

0 commit comments

Comments
 (0)