Navigation Menu

Skip to content

Commit

Permalink
Add Groonga::Database#recover
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 6, 2015
1 parent 0119b3c commit 90784ff
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
34 changes: 33 additions & 1 deletion ext/groonga/rb-grn-database.c
@@ -1,6 +1,6 @@
/* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009-2011 Kouhei Sutou <kou@clear-code.com>
Copyright (C) 2009-2015 Kouhei Sutou <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -562,6 +562,37 @@ rb_grn_database_defrag (int argc, VALUE *argv, VALUE self)
return INT2NUM(n_segments);
}

/*
* Recovers database.
*
* @overload recover()
*
* If the database is broken, try to recover the database. If the
* database can't be recovered, an {Groonga::Error} family exception
* is raised.
*
* If the database isn't broken, it does nothing.
*
* @return [void]
*
* @since 4.0.8
*/
static VALUE
rb_grn_database_recover (VALUE self)
{
grn_rc rc;
grn_ctx *context;
grn_obj *database;

rb_grn_database_deconstruct(SELF(self), &database, &context,
NULL, NULL, NULL, NULL);
rc = grn_db_recover(context, database);
rb_grn_context_check(context, self);
rb_grn_rc_check(rc, self);

return Qnil;
}

void
rb_grn_init_database (VALUE mGrn)
{
Expand Down Expand Up @@ -593,4 +624,5 @@ rb_grn_init_database (VALUE mGrn)

rb_define_method(rb_cGrnDatabase, "touch", rb_grn_database_touch, 0);
rb_define_method(rb_cGrnDatabase, "defrag", rb_grn_database_defrag, -1);
rb_define_method(rb_cGrnDatabase, "recover", rb_grn_database_recover, 0);
}
29 changes: 28 additions & 1 deletion test/test-database.rb
@@ -1,4 +1,4 @@
# Copyright (C) 2009-2014 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2009-2015 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -171,6 +171,33 @@ def test_defrag
assert_equal(2, @database.defrag)
end

def test_recover
setup_database
Groonga::Schema.define do |schema|
schema.create_table("Users") do |table|
table.short_text("name")
end

schema.create_table("Terms",
:type => :patricia_trie,
:key_type => :short_text,
:default_tokenizer => "TokenBigram",
:normalizer => "NormalizerAuto") do |table|
table.index("Users.name")
end
end

index = context["Terms.Users_name"]
index.lock
assert do
index.locked?
end
@database.recover
assert do
not index.locked?
end
end

def test_tables
setup_database
Groonga::Schema.define do |schema|
Expand Down

0 comments on commit 90784ff

Please sign in to comment.