1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2015 Kouhei Sutou <kou@clear-code.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#include <mrn_mysql.h>
21
22#include "mrn_database.hpp"
23#include "mrn_operations.hpp"
24
25// for debug
26#define MRN_CLASS_NAME "mrn::Database"
27
28namespace mrn {
29 Database::Database(grn_ctx *ctx, grn_obj *db)
30 : ctx_(ctx),
31 db_(db),
32 broken_table_names_(NULL),
33 is_broken_(false) {
34 Operations operations(ctx_);
35 broken_table_names_ = operations.collect_processing_table_names();
36 is_broken_ = operations.is_locked();
37 }
38
39 Database::~Database(void) {
40 close();
41 }
42
43 void Database::close() {
44 MRN_DBUG_ENTER_METHOD();
45 if (db_) {
46 grn_hash_close(ctx_, broken_table_names_);
47 broken_table_names_ = NULL;
48 grn_obj_close(ctx_, db_);
49 db_ = NULL;
50 }
51 DBUG_VOID_RETURN;
52 }
53
54 grn_rc Database::remove() {
55 MRN_DBUG_ENTER_METHOD();
56 grn_rc rc = GRN_SUCCESS;
57 if (db_) {
58 grn_hash_close(ctx_, broken_table_names_);
59 broken_table_names_ = NULL;
60 rc = grn_obj_remove(ctx_, db_);
61 if (rc == GRN_SUCCESS) {
62 db_ = NULL;
63 }
64 }
65 DBUG_RETURN(rc);
66 }
67
68 grn_obj *Database::get() {
69 MRN_DBUG_ENTER_METHOD();
70 DBUG_RETURN(db_);
71 }
72
73 bool Database::is_broken() {
74 MRN_DBUG_ENTER_METHOD();
75 DBUG_RETURN(is_broken_);
76 }
77
78 bool Database::is_broken_table(const char *name, size_t name_size) {
79 MRN_DBUG_ENTER_METHOD();
80 grn_id id = grn_hash_get(ctx_, broken_table_names_, name, name_size, NULL);
81 DBUG_RETURN(id != GRN_ID_NIL);
82 }
83
84 void Database::mark_table_repaired(const char *name, size_t name_size) {
85 MRN_DBUG_ENTER_METHOD();
86 grn_hash_delete(ctx_, broken_table_names_, name, name_size, NULL);
87 DBUG_VOID_RETURN;
88 }
89}
90