Class: Groonga::Database

Inherits:
Object
  • Object
show all
Includes:
Enumerable, GrnEncodingSupport
Defined in:
ext/rb-grn-database.c,
ext/rb-grn-database.c

Overview

テーブルの集合を管理するためのオブジェクト。

コンテキストに結びつけて使用する。通常、アプリケーション 毎に1つのコンテキストを利用するので、データベースも1つだ け利用する。コンテキストと違い、データベースは暗黙のうち に作成されないので明示的に作成する必要がある。

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#==, #[], #[]=, #append, #closed?, #domain, #id, #inspect, #name, #path, #persistent?, #prepend, #range, #remove, #temporary?

Constructor Details

#Groonga::Database.new(path, options = nil) ⇒ Groonga::Database #Groonga::Database.new(path, options = nil) {|database| ... } ⇒ Object

既存のデータベースを開く。ブロックを指定した場合はブロッ クに開いたデータベースを渡し、ブロックを抜けるときに閉じ る。

optionsにはハッシュでオプションを指定する。指定できるオ プションは以下の通り。

:context

データベースを結びつけるコンテキスト。省略すると Groonga::Context.defaultを利用する。

Overloads:

  • #Groonga::Database.new(path, options = nil) ⇒ Groonga::Database
  • #Groonga::Database.new(path, options = nil) {|database| ... } ⇒ Object

    Yields:

    • (database)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/rb-grn-database.c', line 172

static VALUE
rb_grn_database_initialize (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *old_database, *database;
    const char *path;
    VALUE rb_path, options, rb_context;

    rb_scan_args(argc, argv, "11", &rb_path, &options);

    path = StringValuePtr(rb_path);
    rb_grn_scan_options(options,
			"context", &rb_context,
			NULL);

    context = rb_grn_context_ensure(&rb_context);

    old_database = grn_ctx_db(context);
    if (old_database)
	grn_obj_close(context, old_database);
    database = grn_db_open(context, path);
    rb_grn_object_assign(Qnil, self, rb_context, context, database);
    rb_grn_context_check(context, self);

    rb_iv_set(self, "@context", rb_context);
    if (!NIL_P(rb_context))
	rb_iv_set(rb_context, "database", self);

    return Qnil;
}

Class Method Details

.Groonga::Database.create(options = nil) ⇒ Groonga::Database

新しくデータベースを作成する。

optionsにはハッシュでオプションを指定する。指定できるオ プションは以下の通り。

:path

データベースを保存するパス。省略すると一時データベース となる。

:context

データベースを結びつけるコンテキスト。省略すると Groonga::Context.defaultを利用する。

使用例は以下の通り。

一時データベースを作成:

Groonga::Database.create

永続データベースを作成:

Groonga::Database.create(:path => "/tmp/db.groonga")

Returns:



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
# File 'ext/rb-grn-database.c', line 113

static VALUE
rb_grn_database_s_create (int argc, VALUE *argv, VALUE klass)
{
    grn_ctx *context;
    grn_obj *old_database, *database;
    grn_db_create_optarg create_args;
    const char *path = NULL;
    VALUE rb_database;
    VALUE rb_path, options, rb_context, builtin_type_names;

    rb_scan_args(argc, argv, "01", &options);

    rb_grn_scan_options(options,
                        "path", &rb_path,
			"context", &rb_context,
                        "builtin_type_names", &builtin_type_names,
			NULL);

    if (!NIL_P(rb_path))
        path = StringValuePtr(rb_path);
    context = rb_grn_context_ensure(&rb_context);

    create_args.builtin_type_names = NULL;
    create_args.n_builtin_type_names = 0;

    old_database = grn_ctx_db(context);
    if (old_database)
	grn_obj_close(context, old_database);
    database = grn_db_create(context, path, &create_args);
    rb_grn_context_check(context, rb_ary_new4(argc, argv));
    rb_database = GRNOBJECT2RVAL(klass, context, database, RB_GRN_TRUE);
    rb_iv_set(rb_database, "@context", rb_context);
    if (!NIL_P(rb_context))
	rb_iv_set(rb_context, "database", rb_database);
    rb_grn_context_check(context, rb_ary_new4(argc, argv));

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_database,
			 rb_grn_database_close, rb_database);
    else
        return rb_database;
}

.Groonga::Database.open(path, options = nil) ⇒ Groonga::Database .Groonga::Database.open(path, options = nil) {|database| ... } ⇒ Object

既存のデータベースを開く。ブロックを指定した場合はブロッ クに開いたデータベースを渡し、ブロックを抜けるときに閉じ る。

optionsにはハッシュでオプションを指定する。指定できるオ プションは以下の通り。

:context

データベースを結びつけるコンテキスト。省略すると Groonga::Context.defaultを利用する。

Overloads:



219
220
221
222
223
224
225
226
227
228
229
230
# File 'ext/rb-grn-database.c', line 219

static VALUE
rb_grn_database_s_open (int argc, VALUE *argv, VALUE klass)
{
    VALUE database;

    database = rb_obj_alloc(klass);
    rb_grn_database_initialize(argc, argv, database);
    if (rb_block_given_p())
        return rb_ensure(rb_yield, database, rb_grn_database_close, database);
    else
        return database;
}

Instance Method Details

#clear_lockObject

call-seq:

database.clear_lock

databaseのロックを強制的に解除する。



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'ext/rb-grn-database.c', line 355

static VALUE
rb_grn_database_clear_lock (VALUE self)
{
    grn_ctx *context;
    grn_obj *database;

    rb_grn_database_deconstruct(SELF(self), &database, &context,
				NULL, NULL, NULL, NULL);

    grn_obj_clear_lock(context, database);

    return Qnil;
}

#closeObject

call-seq:

database.close

databaseが使用しているリソースを開放する。これ以降databaseを 使うことはできない。



76
77
78
79
80
81
82
83
84
85
86
# File 'ext/rb-grn-database.c', line 76

static VALUE
rb_grn_database_close (VALUE self)
{
    VALUE rb_context;

    rb_context = rb_iv_get(self, "@context");
    if (!NIL_P(rb_context))
	rb_iv_set(rb_context, "database", Qnil);

    return rb_grn_object_close(self);
}

#each {|object| ... } ⇒ Object

データベース内のオブジェクトを順番にブロックに渡す。

すべてのオブジェクトの名前を表示する:

database.each do |object|
  p object.name
end

Yields:

  • (object)


243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/rb-grn-database.c', line 243

static VALUE
rb_grn_database_each (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *database;
    grn_table_cursor *cursor;
    VALUE rb_cursor;
    grn_id id;

    rb_grn_database_deconstruct(SELF(self), &database, &context,
				NULL, NULL, NULL, NULL);
    cursor = grn_table_cursor_open(context, database, NULL, 0, NULL, 0,
				   0, -1, GRN_CURSOR_ASCENDING);
    rb_cursor = GRNTABLECURSOR2RVAL(Qnil, context, cursor);
    rb_iv_set(self, "cursor", rb_cursor);
    while ((id = grn_table_cursor_next(context, cursor)) != GRN_ID_NIL) {
	grn_obj *object;

	object = grn_ctx_at(context, id);
	if (object)
	    rb_yield(GRNOBJECT2RVAL(Qnil, context, object, RB_GRN_FALSE));
    }
    rb_grn_object_close(rb_cursor);
    rb_iv_set(self, "cursor", Qnil);

    return Qnil;
}

#lockObject

call-seq:

database.lock(options={})
database.lock(options={}) {...}

databaseをロックする。ロックに失敗した場合は Groonga::ResourceDeadlockAvoided例外が発生する。

ブロックを指定した場合はブロックを抜けたときにunlockする。

利用可能なオプションは以下の通り。

:timeout

ロックを獲得できなかった場合は:timeout秒間ロックの獲 得を試みる。:timeout秒以内にロックを獲得できなかった 場合は例外が発生する。



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'ext/rb-grn-database.c', line 315

static VALUE
rb_grn_database_lock (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *database;
    int timeout = 0;
    grn_rc rc;
    VALUE options, rb_timeout;

    rb_scan_args(argc, argv, "01",  &options);

    rb_grn_database_deconstruct(SELF(self), &database, &context,
				NULL, NULL, NULL, NULL);

    rb_grn_scan_options(options,
			"timeout", &rb_timeout,
			NULL);

    if (!NIL_P(rb_timeout))
	timeout = NUM2UINT(rb_timeout);

    rc = grn_obj_lock(context, database, GRN_ID_NIL, timeout);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    if (rb_block_given_p()) {
	return rb_ensure(rb_yield, Qnil, rb_grn_database_unlock, self);
    } else {
	return Qnil;
    }
}

#locked?Boolean

call-seq:

database.locked?

databaseがロックされていればtrueを返す。

Returns:

  • (Boolean)


377
378
379
380
381
382
383
384
385
386
387
# File 'ext/rb-grn-database.c', line 377

static VALUE
rb_grn_database_is_locked (VALUE self)
{
    grn_ctx *context;
    grn_obj *database;

    rb_grn_database_deconstruct(SELF(self), &database, &context,
				NULL, NULL, NULL, NULL);

    return CBOOL2RVAL(grn_obj_is_locked(context, database));
}

#unlockObject

call-seq:

database.unlock

databaseのロックを解除する。



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'ext/rb-grn-database.c', line 279

static VALUE
rb_grn_database_unlock (VALUE self)
{
    grn_ctx *context;
    grn_obj *database;
    grn_rc rc;

    rb_grn_database_deconstruct(SELF(self), &database, &context,
				NULL, NULL, NULL, NULL);

    rc = grn_obj_unlock(context, database, GRN_ID_NIL);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return Qnil;
}