Skip to content

Commit eb3ecfa

Browse files
committed
Wrap FileHandle
1 parent 90f9ee9 commit eb3ecfa

File tree

7 files changed

+952
-844
lines changed

7 files changed

+952
-844
lines changed

tiledb/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
Attr,
4444
ArraySchema,
4545
TileDBError,
46-
VFS,
47-
FileIO,
4846
consolidate,
4947
group_create,
5048
object_type,
@@ -103,6 +101,8 @@
103101

104102
from .schema_evolution import ArraySchemaEvolution
105103

104+
from .vfs import VFS
105+
106106
# TODO restricted imports
107107
from .dataframe_ import from_csv, from_pandas, open_dataframe
108108
from .multirange_indexing import EmptyRange

tiledb/cc/enum.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ void init_enums(py::module &m) {
6161
py::module_local()) DENUM(LT) DENUM(LE)
6262
DENUM(GT) DENUM(GE) DENUM(EQ) DENUM(NE);
6363

64+
#define DVENUM(x) .value(#x, TILEDB_VFS_##x)
65+
py::enum_<tiledb_vfs_mode_t>(m, "VFSMode") DVENUM(READ) DVENUM(WRITE)
66+
DVENUM(APPEND);
67+
6468
// test helpers to check enum name against typed value
6569
m.def("_enum_string", &tiledb::impl::type_to_str);
6670
m.def("_enum_string",

tiledb/cc/tiledbcpp.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ void init_attribute(py::module &);
1818
void init_schema(py::module &);
1919
void init_context(py::module &);
2020
void init_config(py::module &);
21+
void init_file_handle(py::module &);
2122
void init_filter(py::module &);
2223
void init_enums(py::module &);
2324
void init_domain(py::module &m);
2425
void init_query(py::module &m);
26+
void init_vfs(py::module &m);
2527

2628
PYBIND11_MODULE(cc, m) {
2729

@@ -31,9 +33,11 @@ PYBIND11_MODULE(cc, m) {
3133
init_context(m);
3234
init_config(m);
3335
init_enums(m);
36+
init_file_handle(m);
3437
init_filter(m);
3538
init_domain(m);
3639
init_query(m);
40+
init_vfs(m);
3741

3842
py::register_exception<TileDBError>(m, "TileDBError");
3943
}

tiledb/cc/vfs.cc

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <tiledb/tiledb>
2+
3+
#include <pybind11/numpy.h>
4+
#include <pybind11/pybind11.h>
5+
#include <pybind11/pytypes.h>
6+
#include <pybind11/stl.h>
7+
8+
#include "common.h"
9+
10+
namespace libtiledbcpp {
11+
12+
using namespace tiledb;
13+
using namespace tiledbpy::common;
14+
namespace py = pybind11;
15+
16+
void init_vfs(py::module &m) {
17+
py::class_<VFS>(m, "VFS")
18+
.def(py::init<const Context &>(), py::keep_alive<1, 2>())
19+
.def(py::init<const Context &, const Config &>(), py::keep_alive<1, 2>())
20+
21+
.def("create_bucket", &VFS::create_bucket)
22+
.def("remove_bucket", &VFS::remove_bucket)
23+
.def("is_bucket", &VFS::is_bucket)
24+
.def("empty_bucket", &VFS::empty_bucket)
25+
.def("is_empty_bucket", &VFS::is_empty_bucket)
26+
27+
.def("create_dir", &VFS::create_dir)
28+
.def("is_dir", &VFS::is_dir)
29+
.def("remove_dir", &VFS::remove_dir)
30+
.def("dir_size", &VFS::dir_size)
31+
.def("move_dir", &VFS::move_dir)
32+
.def("copy_dir", &VFS::copy_dir)
33+
34+
.def("is_file", &VFS::is_file)
35+
.def("remove_file", &VFS::remove_file)
36+
.def("file_size", &VFS::file_size)
37+
.def("move_file", &VFS::move_file)
38+
.def("copy_file", &VFS::copy_file)
39+
40+
.def("ls", &VFS::ls)
41+
.def("touch", &VFS::touch);
42+
}
43+
44+
class FileHandle {
45+
private:
46+
tiledb_ctx_t *_ctx;
47+
tiledb_vfs_fh_t *_fh;
48+
49+
public:
50+
// TODO ERROR CHECKING
51+
52+
FileHandle(const Context &ctx, const VFS &vfs, std::string uri,
53+
tiledb_vfs_mode_t mode)
54+
: _ctx(ctx.ptr().get()) {
55+
tiledb_vfs_open(this->_ctx, vfs.ptr().get(), uri.c_str(), mode, &this->_fh);
56+
}
57+
58+
~FileHandle() { tiledb_vfs_close(this->_ctx, this->_fh); }
59+
60+
py::bytes read(uint64_t offset, uint64_t nbytes) {
61+
py::array data = py::array(py::dtype::of<std::byte>(), nbytes);
62+
py::buffer_info buffer = data.request();
63+
tiledb_vfs_read(this->_ctx, this->_fh, offset, buffer.ptr, nbytes);
64+
65+
auto np = py::module::import("numpy");
66+
auto to_bytes = np.attr("ndarray").attr("tobytes");
67+
68+
return to_bytes(data);
69+
}
70+
71+
void write(py::buffer data) {
72+
py::buffer_info buffer = data.request();
73+
tiledb_vfs_write(this->_ctx, this->_fh, buffer.ptr, buffer.shape[0]);
74+
}
75+
76+
void flush() { tiledb_vfs_sync(this->_ctx, this->_fh); }
77+
78+
bool closed() {
79+
int32_t is_closed;
80+
tiledb_vfs_fh_is_closed(this->_ctx, this->_fh, &is_closed);
81+
return is_closed;
82+
}
83+
};
84+
85+
void init_file_handle(py::module &m) {
86+
py::class_<FileHandle>(m, "FileHandle")
87+
.def(py::init<const Context &, const VFS &, std::string,
88+
tiledb_vfs_mode_t>(),
89+
py::keep_alive<1, 2>())
90+
91+
.def_property_readonly("closed", &FileHandle::closed)
92+
93+
.def("read", &FileHandle::read)
94+
.def("write", &FileHandle::write)
95+
.def("flush", &FileHandle::flush);
96+
}
97+
98+
} // namespace libtiledbcpp

tiledb/libtiledb.pxd

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,19 +1251,19 @@ cdef class SparseArrayImpl(Array):
12511251
cdef class DenseArrayImpl(Array):
12521252
cdef _read_dense_subarray(self, list subarray, list attr_names, object attr_cond, tiledb_layout_t layout, bint include_coords)
12531253

1254-
cdef class FileHandle(object):
1255-
cdef Ctx ctx
1256-
cdef VFS vfs
1257-
cdef unicode uri
1258-
cdef tiledb_vfs_fh_t* ptr
1259-
1260-
@staticmethod
1261-
cdef from_ptr(VFS vfs, unicode uri, tiledb_vfs_fh_t* fh_ptr)
1262-
cpdef closed(self)
1263-
1264-
cdef class VFS(object):
1265-
cdef Ctx ctx
1266-
cdef tiledb_vfs_t* ptr
1254+
# cdef class FileHandle(object):
1255+
# cdef Ctx ctx
1256+
# cdef VFS vfs
1257+
# cdef unicode uri
1258+
# cdef tiledb_vfs_fh_t* ptr
1259+
1260+
# @staticmethod
1261+
# cdef from_ptr(VFS vfs, unicode uri, tiledb_vfs_fh_t* fh_ptr)
1262+
# cpdef closed(self)
1263+
1264+
# cdef class VFS(object):
1265+
# cdef Ctx ctx
1266+
# cdef tiledb_vfs_t* ptr
12671267

12681268
cdef class Query(object):
12691269
cdef Array array

0 commit comments

Comments
 (0)