Skip to content

Commit 91425fc

Browse files
authored
Merge pull request suketa#938 from suketa/instance_cache
create DuckDB::InstanceCache class.
2 parents 7976906 + b670275 commit 91425fc

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

ext/duckdb/duckdb.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ Init_duckdb_native(void) {
3939
rbduckdb_init_duckdb_config();
4040
rbduckdb_init_duckdb_converter();
4141
rbduckdb_init_duckdb_extracted_statements();
42+
#ifdef HAVE_DUCKDB_H_GE_V1_2_0
43+
rbduckdb_init_duckdb_instance_cache();
44+
#endif
4245
}

ext/duckdb/instance_cache.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "ruby-duckdb.h"
2+
3+
#ifdef HAVE_DUCKDB_H_GE_V1_2_0
4+
VALUE cDuckDBInstanceCache;
5+
6+
static void deallocate(void * ctx);
7+
static VALUE allocate(VALUE klass);
8+
static size_t memsize(const void *p);
9+
static VALUE duckdb_instance_cache_initialize(VALUE self);
10+
static VALUE duckdb_instance_cache_destroy(VALUE self);
11+
12+
static const rb_data_type_t instance_cache_data_type = {
13+
"DuckDB/InstanceCache",
14+
{NULL, deallocate, memsize,},
15+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
16+
};
17+
18+
static void deallocate(void * ctx) {
19+
rubyDuckDBInstanceCache *p = (rubyDuckDBInstanceCache *)ctx;
20+
21+
if (p->instance_cache) {
22+
duckdb_destroy_instance_cache(&(p->instance_cache));
23+
}
24+
xfree(p);
25+
}
26+
27+
static size_t memsize(const void *p) {
28+
return sizeof(rubyDuckDBInstanceCache);
29+
}
30+
31+
static VALUE allocate(VALUE klass) {
32+
rubyDuckDBInstanceCache *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBInstanceCache));
33+
return TypedData_Wrap_Struct(klass, &instance_cache_data_type, ctx);
34+
}
35+
36+
static VALUE duckdb_instance_cache_initialize(VALUE self) {
37+
rubyDuckDBInstanceCache *ctx;
38+
39+
TypedData_Get_Struct(self, rubyDuckDBInstanceCache, &instance_cache_data_type, ctx);
40+
41+
ctx->instance_cache = duckdb_create_instance_cache();
42+
if (ctx->instance_cache == NULL) {
43+
rb_raise(eDuckDBError, "Failed to create instance cache");
44+
}
45+
46+
return self;
47+
}
48+
49+
static VALUE duckdb_instance_cache_destroy(VALUE self) {
50+
rubyDuckDBInstanceCache *ctx;
51+
TypedData_Get_Struct(self, rubyDuckDBInstanceCache, &instance_cache_data_type, ctx);
52+
53+
if (ctx->instance_cache) {
54+
duckdb_destroy_instance_cache(&(ctx->instance_cache));
55+
ctx->instance_cache = NULL;
56+
}
57+
58+
return Qnil;
59+
}
60+
61+
void rbduckdb_init_duckdb_instance_cache(void) {
62+
#if 0
63+
VALUE mDuckDB = rb_define_module("DuckDB");
64+
#endif
65+
cDuckDBInstanceCache = rb_define_class_under(mDuckDB, "InstanceCache", rb_cObject);
66+
rb_define_method(cDuckDBInstanceCache, "initialize", duckdb_instance_cache_initialize, 0);
67+
rb_define_method(cDuckDBInstanceCache, "destroy", duckdb_instance_cache_destroy, 0);
68+
rb_define_alloc_func(cDuckDBInstanceCache, allocate);
69+
}
70+
#endif

ext/duckdb/instance_cache.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef RUBY_DUCKDB_INSTANCE_CACHE_H
2+
#define RUBY_DUCKDB_INSTANCE_CACHE_H
3+
4+
#ifdef HAVE_DUCKDB_H_GE_V1_2_0
5+
6+
struct _rubyDuckDBInstanceCache {
7+
duckdb_instance_cache instance_cache;
8+
};
9+
10+
typedef struct _rubyDuckDBInstanceCache rubyDuckDBInstanceCache;
11+
12+
void rbduckdb_init_duckdb_instance_cache(void);
13+
14+
#endif
15+
16+
#endif
17+

ext/duckdb/ruby-duckdb.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "./appender.h"
3333
#include "./config.h"
3434

35+
#ifdef HAVE_DUCKDB_H_GE_V1_2_0
36+
#include "./instance_cache.h"
37+
#endif
38+
3539
extern VALUE mDuckDB;
3640
extern VALUE cDuckDBDatabase;
3741
extern VALUE cDuckDBConnection;
@@ -43,4 +47,8 @@ extern VALUE cDuckDBPreparedStatement;
4347
extern VALUE PositiveInfinity;
4448
extern VALUE NegativeInfinity;
4549

50+
#ifdef HAVE_DUCKDB_H_GE_V1_2_0
51+
extern VALUE cDuckDBInstanceCache;
52+
#endif
53+
4654
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
if defined?(DuckDB::InstanceCache)
6+
7+
module DuckDBTest
8+
class InstanceCacheTest < Minitest::Test
9+
def test_s_new
10+
assert_instance_of DuckDB::InstanceCache, DuckDB::InstanceCache.new
11+
end
12+
13+
def test_destroy
14+
cache = DuckDB::InstanceCache.new
15+
assert_nil cache.destroy
16+
end
17+
end
18+
end
19+
20+
end

0 commit comments

Comments
 (0)