Class: DuckDB::InstanceCache
- Inherits:
-
Object
- Object
- DuckDB::InstanceCache
- Defined in:
- lib/duckdb/instance_cache.rb,
ext/duckdb/instance_cache.c
Overview
Instance Method Summary collapse
- #destroy ⇒ Object
-
#get(path: nil, config: nil) ⇒ Object
:call-seq: instance_cache.get(path:, config:) -> self.
- #get_or_create(*args) ⇒ Object
- #initialize ⇒ Object constructor
Constructor Details
#initialize ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'ext/duckdb/instance_cache.c', line 80
static VALUE instance_cache_initialize(VALUE self) {
rubyDuckDBInstanceCache *ctx;
TypedData_Get_Struct(self, rubyDuckDBInstanceCache, &instance_cache_data_type, ctx);
ctx->instance_cache = duckdb_create_instance_cache();
if (ctx->instance_cache == NULL) {
rb_raise(eDuckDBError, "Failed to create instance cache");
}
ctx->wrappers = rb_funcall(rb_const_get(rb_const_get(rb_cObject, rb_intern("ObjectSpace")),
rb_intern("WeakMap")),
rb_intern("new"), 0);
return self;
}
|
Instance Method Details
#destroy ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'ext/duckdb/instance_cache.c', line 159
static VALUE instance_cache_destroy(VALUE self) {
rubyDuckDBInstanceCache *ctx;
TypedData_Get_Struct(self, rubyDuckDBInstanceCache, &instance_cache_data_type, ctx);
if (ctx->instance_cache) {
duckdb_destroy_instance_cache(&(ctx->instance_cache));
ctx->instance_cache = NULL;
}
return Qnil;
}
|
#get(path: nil, config: nil) ⇒ Object
:call-seq:
instance_cache.get(path:, config:) -> self
Returns a DuckDB::Database object for the given path and config. db1 = cache.get(path: 'db.duckdb') db2 = cache.get(path: 'db.duckdb')
18 19 20 |
# File 'lib/duckdb/instance_cache.rb', line 18 def get(path: nil, config: nil) get_or_create(path, config) end |
#get_or_create(*args) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'ext/duckdb/instance_cache.c', line 98
static VALUE instance_cache_get_or_create(int argc, VALUE *argv, VALUE self) {
VALUE vpath = Qnil;
VALUE vconfig = Qnil;
VALUE memo_path;
VALUE obj;
const char *path = NULL;
char *error = NULL;
duckdb_config config = NULL;
duckdb_database db;
rubyDuckDBInstanceCache *ctx;
rb_scan_args(argc, argv, "02", &vpath, &vconfig);
if (!NIL_P(vpath)) {
path = StringValuePtr(vpath);
}
if (!NIL_P(vconfig)) {
if (!rb_obj_is_kind_of(vconfig, cDuckDBConfig)) {
rb_raise(rb_eTypeError, "The second argument must be DuckDB::Config object.");
}
rubyDuckDBConfig *ctx_config = get_struct_config(vconfig);
config = ctx_config->config;
}
TypedData_Get_Struct(self, rubyDuckDBInstanceCache, &instance_cache_data_type, ctx);
if (duckdb_get_or_create_from_cache(ctx->instance_cache, path, &db, config, &error) == DuckDBError) {
if (error) {
VALUE message = rb_str_new_cstr(error);
duckdb_free(error);
rb_raise(eDuckDBError, "%s", StringValuePtr(message));
} else {
rb_raise(eDuckDBError, "Failed to get or create database from instance cache");
}
}
/*
* duckdb_get_or_create_from_cache hands back a fresh duckdb_database handle
* over an instance it may already have returned before. Registered functions
* live in that shared instance's catalog, but each wrapper retains them
* separately, so a second wrapper's collection frees functions the first
* one's connections can still resolve. One wrapper per instance is what the
* catalog's lifetime actually is, so reuse the wrapper we already have.
*/
memo_path = memoizable_path(vpath);
if (!NIL_P(memo_path)) {
VALUE cached = find_cached_wrapper(ctx, memo_path);
if (!NIL_P(cached)) {
/* Our existing wrapper keeps the instance alive; this handle is spare. */
duckdb_close(&db);
return cached;
}
}
obj = rbduckdb_create_database_obj(db);
if (!NIL_P(memo_path)) {
rbduckdb_database_set_cache_entry(obj, memo_path, ctx->wrappers);
rb_funcall(ctx->wrappers, rb_intern("[]="), 2, obj, Qtrue);
}
return obj;
}
|