Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 122 additions & 15 deletions c_glib/arrow-glib/file-system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* under the License.
*/

#include <string>

#include <gio/gio.h>

#include <arrow-glib/enums.h>

#include <arrow-glib/error.hpp>
Expand Down Expand Up @@ -545,6 +549,40 @@ garrow_file_selector_class_init(GArrowFileSelectorClass *klass)

/* arrow::fs::FileSystem */

static const gchar garrow_file_system_extension_point_name[] = "arrow-file-system";

// GIO's extension lists need synchronization; lazy module loading may reenter.
static GRecMutex garrow_file_system_extension_point_mutex;

static GIOExtensionPoint *
garrow_file_system_get_extension_point(void)
{
static auto extension_point = [] {
auto point = g_io_extension_point_register(garrow_file_system_extension_point_name);
g_io_extension_point_set_required_type(point, GARROW_TYPE_FILE_SYSTEM);
const struct
{
const gchar *name;
GType gtype;
} builtins[] = {
{"local", GARROW_TYPE_LOCAL_FILE_SYSTEM},
{"hdfs", GARROW_TYPE_HDFS_FILE_SYSTEM},
{"s3", GARROW_TYPE_S3_FILE_SYSTEM},
{"gcs", GARROW_TYPE_GCS_FILE_SYSTEM},
{"abfs", GARROW_TYPE_AZURE_FILE_SYSTEM},
{"mock", GARROW_TYPE_MOCK_FILE_SYSTEM},
};
for (const auto &builtin : builtins) {
g_io_extension_point_implement(garrow_file_system_extension_point_name,
builtin.gtype,
builtin.name,
0);
}
return point;
}();
return extension_point;
}

typedef struct GArrowFileSystemPrivate_
{
std::shared_ptr<arrow::fs::FileSystem> file_system;
Expand All @@ -554,7 +592,7 @@ enum {
PROP_FILE_SYSTEM = 1
};

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowFileSystem, garrow_file_system, G_TYPE_OBJECT)
G_DEFINE_TYPE_WITH_PRIVATE(GArrowFileSystem, garrow_file_system, G_TYPE_OBJECT)

#define GARROW_FILE_SYSTEM_GET_PRIVATE(obj) \
static_cast<GArrowFileSystemPrivate *>( \
Expand Down Expand Up @@ -614,6 +652,79 @@ garrow_file_system_class_init(GArrowFileSystemClass *klass)
g_object_class_install_property(gobject_class, PROP_FILE_SYSTEM, spec);
}

/**
* garrow_file_system_register_type:
* @type_name: A non-empty Arrow C++ file system type name.
* @gtype: A non-abstract #GArrowFileSystem type.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Registers @gtype as the GLib wrapper type for @type_name. The type name
* is copied and the registration lasts for the lifetime of the process.
* Built-in file system wrapper types are registered automatically.
* Registering a name that is already registered is an error.
* Each wrapper type can be registered under only one name.
*
* This function is thread-safe.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 27.0.0
*/
gboolean
garrow_file_system_register_type(const gchar *type_name, GType gtype, GError **error)
{
const gchar *context = "[file-system][register-type]";
if (!type_name || !*type_name) {
g_set_error(error,
GARROW_ERROR,
GARROW_ERROR_INVALID,
"%s type name must not be NULL or empty",
context);
return FALSE;
}
if (!g_type_is_a(gtype, GARROW_TYPE_FILE_SYSTEM) || G_TYPE_IS_ABSTRACT(gtype)) {
g_set_error(error,
GARROW_ERROR,
GARROW_ERROR_INVALID,
"%s GType must be a non-abstract GArrowFileSystem type",
context);
return FALSE;
}

auto extension_point = garrow_file_system_get_extension_point();
g_rec_mutex_lock(&garrow_file_system_extension_point_mutex);
const auto duplicate =
g_io_extension_point_get_extension_by_name(extension_point, type_name) != NULL;
GIOExtension *extension = NULL;
if (!duplicate) {
extension = g_io_extension_point_implement(garrow_file_system_extension_point_name,
gtype,
type_name,
0);
}
g_rec_mutex_unlock(&garrow_file_system_extension_point_mutex);

if (duplicate) {
g_set_error(error,
GARROW_ERROR,
GARROW_ERROR_KEY,
"%s file system type is already registered: %s",
context,
type_name);
return FALSE;
}
if (g_strcmp0(g_io_extension_get_name(extension), type_name) != 0) {
g_set_error(error,
GARROW_ERROR,
GARROW_ERROR_KEY,
"%s wrapper type is already registered under another name: %s",
context,
g_io_extension_get_name(extension));
return FALSE;
}
return TRUE;
}

/**
* garrow_file_system_create:
* @uri: An URI to specify file system with options. If you only have an
Expand All @@ -623,8 +734,8 @@ garrow_file_system_class_init(GArrowFileSystemClass *klass)
* This is a factory function to create a specific #GArrowFileSystem
* object.
*
* Returns: (nullable) (transfer full): The newly created file system
* that is an object of a subclass of #GArrowFileSystem.
* Returns: (nullable) (transfer full): The newly created #GArrowFileSystem
* or an object of one of its subclasses.
*
* Since: 3.0.0
*/
Expand Down Expand Up @@ -1598,20 +1709,16 @@ garrow_file_system_new_raw(std::shared_ptr<arrow::fs::FileSystem> *arrow_file_sy
const auto &type_name = (*arrow_file_system)->type_name();

GType file_system_type = GARROW_TYPE_FILE_SYSTEM;
if (type_name == "local") {
file_system_type = GARROW_TYPE_LOCAL_FILE_SYSTEM;
} else if (type_name == "hdfs") {
file_system_type = GARROW_TYPE_HDFS_FILE_SYSTEM;
} else if (type_name == "s3") {
file_system_type = GARROW_TYPE_S3_FILE_SYSTEM;
} else if (type_name == "gcs") {
file_system_type = GARROW_TYPE_GCS_FILE_SYSTEM;
} else if (type_name == "abfs") {
file_system_type = GARROW_TYPE_AZURE_FILE_SYSTEM;
} else if (type_name == "mock") {
file_system_type = GARROW_TYPE_MOCK_FILE_SYSTEM;
auto extension_point = garrow_file_system_get_extension_point();
g_rec_mutex_lock(&garrow_file_system_extension_point_mutex);
auto extension =
g_io_extension_point_get_extension_by_name(extension_point, type_name.c_str());
if (extension) {
file_system_type = g_io_extension_get_type(extension);
}
g_rec_mutex_unlock(&garrow_file_system_extension_point_mutex);

// Wrapper class initialization may register additional types.
return GARROW_FILE_SYSTEM(
g_object_new(file_system_type, "file-system", arrow_file_system, NULL));
}
Expand Down
4 changes: 4 additions & 0 deletions c_glib/arrow-glib/file-system.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ struct _GArrowFileSystemClass
GObjectClass parent_class;
};

GARROW_AVAILABLE_IN_27_0
gboolean
garrow_file_system_register_type(const gchar *type_name, GType gtype, GError **error);

GARROW_AVAILABLE_IN_3_0
GArrowFileSystem *
garrow_file_system_create(const gchar *uri, GError **error);
Expand Down
1 change: 1 addition & 0 deletions c_glib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ if parquet.found()
subdir('parquet-glib')
endif
subdir('example')
subdir('test')

if get_option('doc')
subdir('doc')
Expand Down
23 changes: 23 additions & 0 deletions c_glib/test/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

file_system_test = executable(
'file-system-test',
'test-file-system.cpp',
dependencies: arrow_glib,
)
test('filesystem registry', file_system_test)
164 changes: 164 additions & 0 deletions c_glib/test/test-file-system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <memory>
#include <string>
#include <utility>

#include <gio/gio.h>

#include <arrow/filesystem/localfs.h>

#include <arrow-glib/error.h>
#include <arrow-glib/file-system.hpp>
#include <arrow-glib/local-file-system.h>

namespace {
class TestFileSystem : public arrow::fs::LocalFileSystem {
public:
explicit TestFileSystem(std::string type_name) : type_name_(std::move(type_name)) {}

std::string
type_name() const override
{
return type_name_;
}

private:
std::string type_name_;
};

void
test_builtin()
{
std::shared_ptr<arrow::fs::FileSystem> file_system =
std::make_shared<arrow::fs::LocalFileSystem>();
auto wrapper = garrow_file_system_new_raw(&file_system);
g_assert_true(GARROW_IS_LOCAL_FILE_SYSTEM(wrapper));
g_assert_true(garrow_file_system_get_raw(wrapper) == file_system);
g_object_unref(wrapper);
}

void
test_registered()
{
auto wrapper_type = g_type_register_static_simple(GARROW_TYPE_FILE_SYSTEM,
"TestRegisteredFileSystem",
sizeof(GArrowFileSystemClass),
nullptr,
sizeof(GArrowFileSystem),
nullptr,
static_cast<GTypeFlags>(0));
std::string type_name = "test-registered";
GError *error = nullptr;
g_assert_true(
garrow_file_system_register_type(type_name.c_str(), wrapper_type, &error));
g_assert_no_error(error);
type_name = "changed-after-registration";

std::shared_ptr<arrow::fs::FileSystem> file_system =
std::make_shared<TestFileSystem>("test-registered");
auto wrapper = garrow_file_system_new_raw(&file_system);
g_assert_true(G_OBJECT_TYPE(wrapper) == wrapper_type);
g_assert_true(garrow_file_system_get_raw(wrapper) == file_system);
g_object_unref(wrapper);
}

void
test_gio_registered()
{
std::shared_ptr<arrow::fs::FileSystem> file_system =
std::make_shared<TestFileSystem>("test-gio-registered");
auto fallback = garrow_file_system_new_raw(&file_system);
g_assert_true(G_OBJECT_TYPE(fallback) == GARROW_TYPE_FILE_SYSTEM);
g_object_unref(fallback);

auto point = g_io_extension_point_lookup("arrow-file-system");
g_assert_nonnull(point);
g_assert_true(g_io_extension_point_get_required_type(point) ==
GARROW_TYPE_FILE_SYSTEM);
auto wrapper_type = g_type_register_static_simple(GARROW_TYPE_FILE_SYSTEM,
"TestGIORegisteredFileSystem",
sizeof(GArrowFileSystemClass),
nullptr,
sizeof(GArrowFileSystem),
nullptr,
static_cast<GTypeFlags>(0));
g_io_extension_point_implement("arrow-file-system",
wrapper_type,
"test-gio-registered",
0);

auto wrapper = garrow_file_system_new_raw(&file_system);
g_assert_true(G_OBJECT_TYPE(wrapper) == wrapper_type);
g_assert_true(garrow_file_system_get_raw(wrapper) == file_system);
g_object_unref(wrapper);
}

void
test_fallback()
{
std::shared_ptr<arrow::fs::FileSystem> file_system =
std::make_shared<TestFileSystem>("test-unregistered");
auto wrapper = garrow_file_system_new_raw(&file_system);
g_assert_true(G_OBJECT_TYPE(wrapper) == GARROW_TYPE_FILE_SYSTEM);
g_assert_true(garrow_file_system_get_raw(wrapper) == file_system);

auto type_name = garrow_file_system_get_type_name(wrapper);
g_assert_cmpstr(type_name, ==, "test-unregistered");
g_free(type_name);

GError *error = nullptr;
auto info = garrow_file_system_get_file_info(wrapper, g_get_tmp_dir(), &error);
g_assert_no_error(error);
g_assert_nonnull(info);
g_assert_true(garrow_file_info_is_dir(info));
g_object_unref(info);
g_object_unref(wrapper);
}

void
test_abstract_type()
{
auto wrapper_type = g_type_register_static_simple(GARROW_TYPE_FILE_SYSTEM,
"TestAbstractFileSystem",
sizeof(GArrowFileSystemClass),
nullptr,
sizeof(GArrowFileSystem),
nullptr,
G_TYPE_FLAG_ABSTRACT);
GError *error = nullptr;
g_assert_false(
garrow_file_system_register_type("test-abstract", wrapper_type, &error));
g_assert_error(error, GARROW_ERROR, GARROW_ERROR_INVALID);
g_clear_error(&error);
}
} // namespace

int
main(int argc, char **argv)
{
g_test_init(&argc, &argv, nullptr);
g_test_add_func("/file-system/builtin", test_builtin);
g_test_add_func("/file-system/registered", test_registered);
g_test_add_func("/file-system/gio-registered", test_gio_registered);
g_test_add_func("/file-system/fallback", test_fallback);
g_test_add_func("/file-system/abstract-type", test_abstract_type);
return g_test_run();
}
Loading
Loading