Class: DuckDB::InstanceCache

Inherits:
Object
  • Object
show all
Defined in:
lib/duckdb/instance_cache.rb,
ext/duckdb/instance_cache.c

Overview

The DuckDB::InstanceCache is necessary if a client/program (re)opens multiple databases to the same file within the same statement.

require 'duckdb'
cache = DuckDB::InstanceCache.new
db1 = cache.get(path: 'db.duckdb')
db2 = cache.get(path: 'db.duckdb')

Instance Method Summary collapse

Constructor Details

#initializeObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/duckdb/instance_cache.c', line 36

static VALUE duckdb_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");
    }

    return self;
}

Instance Method Details

#destroyObject



85
86
87
88
89
90
91
92
93
94
95
# File 'ext/duckdb/instance_cache.c', line 85

static VALUE duckdb_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')


20
21
22
# File 'lib/duckdb/instance_cache.rb', line 20

def get(path: nil, config: nil)
  get_or_create(path, config)
end

#get_or_create(*args) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/duckdb/instance_cache.c', line 50

static VALUE duckdb_instance_cache_get_or_create(int argc, VALUE *argv, VALUE self) {
    VALUE vpath = Qnil;
    VALUE vconfig = Qnil;
    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");
        }
    }
    return rbduckdb_create_database_obj(db);
}