1111import operator
1212import os
1313
14+ import blosc2
1415from blosc2 .ctable import CTable
1516from blosc2 .ctable_storage import RemoteTableStorage
1617from blosc2 .remote_array import CACHE_POLICY_DEFAULT , RemoteMetadataMapping
@@ -110,40 +111,13 @@ def __new__(
110111 _filesystem_resolver = None ,
111112 _batch_validator = None ,
112113 ):
113- if source_format == "parquet" or (
114+ parquet = source_format == "parquet" or (
114115 isinstance (urlpath , (str , os .PathLike ))
115116 and os .fspath (urlpath ).split ("?" , 1 )[0 ].lower ().endswith (".parquet" )
116- ):
117- if source_format not in (None , "parquet" ):
118- raise ValueError ("source_format conflicts with the .parquet suffix" )
119- from blosc2 .remote_parquet import RemoteParquetCTable
120-
121- return RemoteParquetCTable (
122- os .fspath (urlpath ),
123- storage_options = storage_options ,
124- parquet_options = parquet_options ,
125- columns = columns ,
126- max_rows = max_rows ,
127- string_max_length = string_max_length ,
128- null_storage = null_storage ,
129- auto_null_sentinels = auto_null_sentinels ,
130- separate_nested_cols = separate_nested_cols ,
131- list_serializer = list_serializer ,
132- blosc2_batch_size = blosc2_batch_size ,
133- blosc2_items_per_block = blosc2_items_per_block ,
134- batch_size = batch_size ,
135- cparams = cparams ,
136- dparams = dparams ,
137- validate = validate ,
138- max_cache_bytes = max_cache_bytes ,
139- cache_policy = cache_policy ,
140- cache_dir = cache_dir ,
141- shared_cache = shared_cache ,
142- max_concurrency = max_concurrency ,
143- metadata_buffer_bytes = metadata_buffer_bytes ,
144- row_buffer_bytes = row_buffer_bytes ,
145- )
146- if (
117+ )
118+ if parquet and source_format not in (None , "parquet" ):
119+ raise ValueError ("source_format conflicts with the .parquet suffix" )
120+ if not parquet and (
147121 source_format is not None
148122 or any (
149123 value is not None
@@ -180,20 +154,61 @@ def __new__(
180154
181155 from blosc2 .remote_store import RemoteStore
182156
183- store = RemoteStore (
184- urlpath ,
185- dataset = dataset ,
186- path = path ,
187- storage_options = storage_options ,
188- cache_policy = cache_policy ,
189- max_cache_bytes = max_cache_bytes ,
190- cache_dir = cache_dir ,
191- hdf5_index = hdf5_index ,
192- _allow_array_root = True ,
193- _filesystem = _filesystem ,
194- _filesystem_resolver = _filesystem_resolver ,
195- _batch_validator = _batch_validator ,
196- )
157+ conversion = None
158+ if parquet :
159+ conversion = {
160+ "parquet_options" : parquet_options ,
161+ "columns" : columns ,
162+ "max_rows" : max_rows ,
163+ "string_max_length" : string_max_length ,
164+ "null_storage" : null_storage ,
165+ "auto_null_sentinels" : auto_null_sentinels ,
166+ "separate_nested_cols" : separate_nested_cols ,
167+ "list_serializer" : list_serializer ,
168+ "blosc2_batch_size" : blosc2_batch_size ,
169+ "blosc2_items_per_block" : blosc2_items_per_block ,
170+ "batch_size" : batch_size ,
171+ "cparams" : cparams ,
172+ "dparams" : dparams ,
173+ "validate" : validate ,
174+ }
175+
176+ if shared_cache :
177+ if cache_dir is None or (
178+ cache_policy is not CACHE_POLICY_DEFAULT and cache_policy is not blosc2 .CachePolicy .DISK
179+ ):
180+ raise ValueError ("shared_cache=True requires a disk cache" )
181+ store = RemoteStore .with_sparse_cache (
182+ urlpath ,
183+ cache_dir ,
184+ dataset = dataset ,
185+ path = path ,
186+ storage_options = storage_options ,
187+ max_cache_bytes = max_cache_bytes ,
188+ _filesystem = _filesystem ,
189+ _filesystem_resolver = _filesystem_resolver ,
190+ _batch_validator = _batch_validator ,
191+ _source_format = "parquet" if parquet else None ,
192+ _parquet_conversion = conversion ,
193+ )
194+ else :
195+ store = RemoteStore (
196+ urlpath ,
197+ dataset = dataset ,
198+ path = path ,
199+ storage_options = storage_options ,
200+ cache_policy = cache_policy ,
201+ max_cache_bytes = max_cache_bytes ,
202+ cache_dir = cache_dir ,
203+ hdf5_index = hdf5_index ,
204+ _allow_array_root = True ,
205+ _filesystem = _filesystem ,
206+ _filesystem_resolver = _filesystem_resolver ,
207+ _batch_validator = _batch_validator ,
208+ _source_format = "parquet" if parquet else None ,
209+ _parquet_conversion = conversion ,
210+ _allow_local_source = parquet ,
211+ )
197212 try :
198213 _ , full = store ._resolve ("" )
199214 kind , diagnostic = store ._owner .nodes [full ]
@@ -211,12 +226,16 @@ def __init__(self, *args, **kwargs):
211226
212227 @classmethod
213228 def open_reference (cls , path , * , storage_options = None , parquet_options = None ):
214- """Reopen a saved Parquet table with replacement runtime options."""
215- from blosc2 .remote_parquet import RemoteParquetCTable
229+ """Reopen a saved Parquet RemoteStore archive."""
230+ if parquet_options is not None :
231+ raise TypeError ("Parquet reader options are frozen in the saved reference" )
232+ from blosc2 .remote_store import RemoteStore
216233
217- return RemoteParquetCTable .open_reference (
218- path , storage_options = storage_options , parquet_options = parquet_options
219- )
234+ table = RemoteStore ._open_artifact (path , storage_options = storage_options )
235+ if not isinstance (table , cls ):
236+ table .close ()
237+ raise ValueError ("Reference does not contain a remote CTable" )
238+ return table
220239
221240 @classmethod
222241 def with_sparse_cache (
@@ -239,6 +258,7 @@ def with_sparse_cache(
239258 _source_validator = None ,
240259 _manifest_validator = None ,
241260 _max_nodes = None ,
261+ source_format = None ,
242262 ):
243263 """Attach a remote CTable to a sparse disk cache shared across processes.
244264
@@ -247,37 +267,6 @@ def with_sparse_cache(
247267 ``max_cache_bytes=None`` for unlimited retention. For ordinary shared
248268 caching, prefer ``blosc2.open(url, cache_dir=..., shared_cache=True)``.
249269 """
250- if isinstance (urlpath , (str , os .PathLike )) and os .fspath (urlpath ).split ("?" , 1 )[0 ].lower ().endswith (
251- ".parquet"
252- ):
253- if any (
254- value is not None
255- for value in (
256- dataset ,
257- path ,
258- manifest ,
259- carrier ,
260- _filesystem ,
261- _filesystem_resolver ,
262- _batch_validator ,
263- _source_validator ,
264- _manifest_validator ,
265- _max_nodes ,
266- )
267- ):
268- raise ValueError ("Parquet sparse caching accepts a single file and cache directory" )
269- from blosc2 .remote_parquet import RemoteParquetCTable
270-
271- return RemoteParquetCTable (
272- os .fspath (urlpath ),
273- cache_dir = runtime_cache_path ,
274- shared_cache = True ,
275- max_cache_bytes = max_cache_bytes ,
276- storage_options = storage_options ,
277- max_concurrency = max_concurrency ,
278- metadata_buffer_bytes = metadata_buffer_bytes ,
279- row_buffer_bytes = row_buffer_bytes ,
280- )
281270 settings = {
282271 name : _positive_integer (name , value )
283272 for name , value in {
@@ -304,6 +293,7 @@ def with_sparse_cache(
304293 _source_validator = _source_validator ,
305294 _manifest_validator = _manifest_validator ,
306295 _max_nodes = _max_nodes ,
296+ _source_format = source_format ,
307297 )
308298 try :
309299 _ , full = store ._resolve ("" )
@@ -319,7 +309,14 @@ def with_sparse_cache(
319309 @classmethod
320310 def _from_owner (cls , owner , full_path , ** settings ):
321311 settings = {name : _positive_integer (name , value ) for name , value in settings .items ()}
322- storage = RemoteTableStorage (owner , full_path , ** settings )
312+ if owner .format == "parquet" :
313+ from blosc2 .remote_parquet import ParquetTableStorage
314+
315+ storage = ParquetTableStorage (
316+ owner , owner .parquet_schema , owner .parquet_physical , owner .parquet_length , ** settings
317+ )
318+ else :
319+ storage = RemoteTableStorage (owner , full_path , ** settings )
323320 try :
324321 return cls ._open_from_storage (storage )
325322 except BaseException :
@@ -408,10 +405,12 @@ def attrs(self):
408405 @property
409406 def source (self ):
410407 storage = self ._remote_storage ()
408+ from blosc2 .remote_store import public_source_url
409+
411410 return {
412411 "kind" : storage ._owner .format ,
413412 "version" : 1 ,
414- "urlpath" : storage ._owner .urlpath ,
413+ "urlpath" : public_source_url ( storage ._owner .urlpath ) ,
415414 "dataset" : storage ._root_key ,
416415 "assume_immutable" : True ,
417416 }
0 commit comments