sync_http_array_read/
sync_http_array_read.rs1#![allow(missing_docs)]
2
3use ndarray::ArrayD;
4use std::sync::Arc;
5
6use zarrs::array::{Array, ArraySubset};
7use zarrs::storage::ReadableStorage;
8use zarrs::storage::storage_adapter::async_to_sync::{
9 AsyncToSyncBlockOn, AsyncToSyncStorageAdapter,
10};
11use zarrs::storage::storage_adapter::usage_log::UsageLogStorageAdapter;
12
13struct TokioBlockOn(tokio::runtime::Runtime);
14
15impl AsyncToSyncBlockOn for TokioBlockOn {
16 fn block_on<F: core::future::Future>(&self, future: F) -> F::Output {
17 self.0.block_on(future)
18 }
19}
20
21enum Backend {
22 ObjectStore,
24}
25
26fn http_array_read(backend: Backend) -> Result<(), Box<dyn std::error::Error>> {
27 const HTTP_URL: &str =
28 "https://raw.githubusercontent.com/zarrs/zarrs/main/zarrs/tests/data/array_write_read.zarr";
29 const ARRAY_PATH: &str = "/group/array";
30
31 let block_on = TokioBlockOn(tokio::runtime::Runtime::new()?);
34 let mut store: ReadableStorage = match backend {
35 Backend::ObjectStore => {
42 let options = object_store::ClientOptions::new().with_allow_http(true);
43 let store = object_store::http::HttpBuilder::new()
44 .with_url(HTTP_URL)
45 .with_client_options(options)
46 .build()?;
47 let store = Arc::new(zarrs_object_store::AsyncObjectStore::new(store));
48 Arc::new(AsyncToSyncStorageAdapter::new(store, block_on))
49 }
50 };
51 if let Some(arg1) = std::env::args().collect::<Vec<_>>().get(1)
52 && arg1 == "--usage-log"
53 {
54 let log_writer = Arc::new(std::sync::Mutex::new(
55 std::io::stdout(),
57 ));
59 store = Arc::new(UsageLogStorageAdapter::new(store, log_writer, || {
60 chrono::Utc::now().format("[%T%.3f] ").to_string()
61 }));
62 }
63
64 let array = Array::open(store, ARRAY_PATH)?;
66
67 println!(
68 "The array metadata is:\n{}\n",
69 array.metadata().to_string_pretty()
70 );
71
72 let data_all: ArrayD<f32> = array.retrieve_array_subset(&array.subset_all())?;
74 println!("The whole array is:\n{data_all}\n");
75
76 let chunk_indices = vec![1, 0];
78 let data_chunk: ArrayD<f32> = array.retrieve_chunk(&chunk_indices)?;
79 println!("Chunk [1,0] is:\n{data_chunk}\n");
80
81 let subset_4x2 = ArraySubset::new_with_ranges(&[2..6, 3..5]); let data_4x2: ArrayD<f32> = array.retrieve_array_subset(&subset_4x2)?;
84 println!("The middle 4x2 subset is:\n{data_4x2}\n");
85
86 Ok(())
87}
88
89fn main() -> Result<(), Box<dyn std::error::Error>> {
90 println!("------------ object_store backend ------------");
91 http_array_read(Backend::ObjectStore)?;
92 Ok(())
95}