| 1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: |
| 3 | #ident "$Id$" |
| 4 | /*====== |
| 5 | This file is part of PerconaFT. |
| 6 | |
| 7 | |
| 8 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
| 9 | |
| 10 | PerconaFT is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License, version 2, |
| 12 | as published by the Free Software Foundation. |
| 13 | |
| 14 | PerconaFT is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | ---------------------------------------- |
| 23 | |
| 24 | PerconaFT is free software: you can redistribute it and/or modify |
| 25 | it under the terms of the GNU Affero General Public License, version 3, |
| 26 | as published by the Free Software Foundation. |
| 27 | |
| 28 | PerconaFT is distributed in the hope that it will be useful, |
| 29 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 | GNU Affero General Public License for more details. |
| 32 | |
| 33 | You should have received a copy of the GNU Affero General Public License |
| 34 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 35 | ======= */ |
| 36 | |
| 37 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
| 38 | |
| 39 | #include <toku_portability.h> |
| 40 | #include <toku_assert.h> |
| 41 | #include <toku_pthread.h> |
| 42 | #include <errno.h> |
| 43 | #include <string.h> |
| 44 | |
| 45 | #include "loader/loader-internal.h" |
| 46 | #include "util/dbt.h" |
| 47 | |
| 48 | toku_instr_key *loader_error_mutex_key; |
| 49 | |
| 50 | static void error_callback_lock(ft_loader_error_callback loader_error) { |
| 51 | toku_mutex_lock(&loader_error->mutex); |
| 52 | } |
| 53 | |
| 54 | static void error_callback_unlock(ft_loader_error_callback loader_error) { |
| 55 | toku_mutex_unlock(&loader_error->mutex); |
| 56 | } |
| 57 | |
| 58 | void ft_loader_init_error_callback(ft_loader_error_callback loader_error) { |
| 59 | memset(loader_error, 0, sizeof *loader_error); |
| 60 | toku_init_dbt(&loader_error->key); |
| 61 | toku_init_dbt(&loader_error->val); |
| 62 | toku_mutex_init(*loader_error_mutex_key, &loader_error->mutex, nullptr); |
| 63 | } |
| 64 | |
| 65 | void ft_loader_destroy_error_callback(ft_loader_error_callback loader_error) { |
| 66 | toku_mutex_destroy(&loader_error->mutex); |
| 67 | toku_destroy_dbt(&loader_error->key); |
| 68 | toku_destroy_dbt(&loader_error->val); |
| 69 | memset(loader_error, 0, sizeof *loader_error); |
| 70 | } |
| 71 | |
| 72 | int ft_loader_get_error(ft_loader_error_callback loader_error) { |
| 73 | error_callback_lock(loader_error); |
| 74 | int r = loader_error->error; |
| 75 | error_callback_unlock(loader_error); |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | void ft_loader_set_error_function(ft_loader_error_callback loader_error, ft_loader_error_func error_function, void *) { |
| 80 | loader_error->error_callback = error_function; |
| 81 | loader_error->extra = error_extra; |
| 82 | } |
| 83 | |
| 84 | int ft_loader_set_error(ft_loader_error_callback loader_error, int error, DB *db, int which_db, DBT *key, DBT *val) { |
| 85 | int r; |
| 86 | error_callback_lock(loader_error); |
| 87 | if (loader_error->error) { // there can be only one |
| 88 | r = EEXIST; |
| 89 | } else { |
| 90 | r = 0; |
| 91 | loader_error->error = error; // set the error |
| 92 | loader_error->db = db; |
| 93 | loader_error->which_db = which_db; |
| 94 | if (key != nullptr) { |
| 95 | toku_clone_dbt(&loader_error->key, *key); |
| 96 | } |
| 97 | if (val != nullptr) { |
| 98 | toku_clone_dbt(&loader_error->val, *val); |
| 99 | } |
| 100 | } |
| 101 | error_callback_unlock(loader_error); |
| 102 | return r; |
| 103 | } |
| 104 | |
| 105 | int ft_loader_call_error_function(ft_loader_error_callback loader_error) { |
| 106 | int r; |
| 107 | error_callback_lock(loader_error); |
| 108 | r = loader_error->error; |
| 109 | if (r && loader_error->error_callback && !loader_error->did_callback) { |
| 110 | loader_error->did_callback = true; |
| 111 | loader_error->error_callback(loader_error->db, |
| 112 | loader_error->which_db, |
| 113 | loader_error->error, |
| 114 | &loader_error->key, |
| 115 | &loader_error->val, |
| 116 | loader_error->extra); |
| 117 | } |
| 118 | error_callback_unlock(loader_error); |
| 119 | return r; |
| 120 | } |
| 121 | |
| 122 | int ft_loader_set_error_and_callback(ft_loader_error_callback loader_error, int error, DB *db, int which_db, DBT *key, DBT *val) { |
| 123 | int r = ft_loader_set_error(loader_error, error, db, which_db, key, val); |
| 124 | if (r == 0) |
| 125 | r = ft_loader_call_error_function(loader_error); |
| 126 | return r; |
| 127 | } |
| 128 | |
| 129 | int ft_loader_init_poll_callback(ft_loader_poll_callback p) { |
| 130 | memset(p, 0, sizeof *p); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | void ft_loader_destroy_poll_callback(ft_loader_poll_callback p) { |
| 135 | memset(p, 0, sizeof *p); |
| 136 | } |
| 137 | |
| 138 | void ft_loader_set_poll_function(ft_loader_poll_callback p, ft_loader_poll_func poll_function, void *) { |
| 139 | p->poll_function = poll_function; |
| 140 | p->poll_extra = poll_extra; |
| 141 | } |
| 142 | |
| 143 | int ft_loader_call_poll_function(ft_loader_poll_callback p, float progress) { |
| 144 | int r = 0; |
| 145 | if (p->poll_function) |
| 146 | r = p->poll_function(p->poll_extra, progress); |
| 147 | return r; |
| 148 | } |
| 149 | |