| 1 | |
| 2 | // vim:sw=2:ai |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. |
| 6 | * See COPYRIGHT.txt for details. |
| 7 | */ |
| 8 | |
| 9 | #ifndef DENA_DATABASE_HPP |
| 10 | #define DENA_DATABASE_HPP |
| 11 | |
| 12 | #include <string> |
| 13 | #include <memory> |
| 14 | #include <vector> |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | #include "string_buffer.hpp" |
| 18 | #include "string_ref.hpp" |
| 19 | #include "config.hpp" |
| 20 | |
| 21 | namespace dena { |
| 22 | |
| 23 | struct database_i; |
| 24 | typedef std::auto_ptr<volatile database_i> database_ptr; |
| 25 | |
| 26 | struct dbcontext_i; |
| 27 | typedef std::auto_ptr<dbcontext_i> dbcontext_ptr; |
| 28 | |
| 29 | struct database_i { |
| 30 | virtual ~database_i() { } |
| 31 | virtual dbcontext_ptr create_context(bool for_write) volatile = 0; |
| 32 | virtual void stop() volatile = 0; |
| 33 | virtual const config& get_conf() const volatile = 0; |
| 34 | static database_ptr create(const config& conf); |
| 35 | }; |
| 36 | |
| 37 | struct prep_stmt { |
| 38 | typedef std::vector<uint32_t> fields_type; |
| 39 | private: |
| 40 | dbcontext_i *dbctx; /* must be valid while *this is alive */ |
| 41 | size_t table_id; /* a prep_stmt object holds a refcount of the table */ |
| 42 | size_t idxnum; |
| 43 | fields_type ret_fields; |
| 44 | fields_type filter_fields; |
| 45 | public: |
| 46 | prep_stmt(); |
| 47 | prep_stmt(dbcontext_i *c, size_t tbl, size_t idx, const fields_type& rf, |
| 48 | const fields_type& ff); |
| 49 | ~prep_stmt(); |
| 50 | prep_stmt(const prep_stmt& x); |
| 51 | prep_stmt& operator =(const prep_stmt& x); |
| 52 | public: |
| 53 | size_t get_table_id() const { return table_id; } |
| 54 | size_t get_idxnum() const { return idxnum; } |
| 55 | const fields_type& get_ret_fields() const { return ret_fields; } |
| 56 | const fields_type& get_filter_fields() const { return filter_fields; } |
| 57 | }; |
| 58 | |
| 59 | struct dbcallback_i { |
| 60 | virtual ~dbcallback_i () { } |
| 61 | virtual void dbcb_set_prep_stmt(size_t pst_id, const prep_stmt& v) = 0; |
| 62 | virtual const prep_stmt *dbcb_get_prep_stmt(size_t pst_id) const = 0; |
| 63 | virtual void dbcb_resp_short(uint32_t code, const char *msg) = 0; |
| 64 | virtual void dbcb_resp_short_num(uint32_t code, uint32_t value) = 0; |
| 65 | virtual void dbcb_resp_short_num64(uint32_t code, uint64_t value) = 0; |
| 66 | virtual void dbcb_resp_begin(size_t num_flds) = 0; |
| 67 | virtual void dbcb_resp_entry(const char *fld, size_t fldlen) = 0; |
| 68 | virtual void dbcb_resp_end() = 0; |
| 69 | virtual void dbcb_resp_cancel() = 0; |
| 70 | }; |
| 71 | |
| 72 | enum record_filter_type { |
| 73 | record_filter_type_skip = 0, |
| 74 | record_filter_type_break = 1, |
| 75 | }; |
| 76 | |
| 77 | struct record_filter { |
| 78 | record_filter_type filter_type; |
| 79 | string_ref op; |
| 80 | uint32_t ff_offset; /* offset in filter_fields */ |
| 81 | string_ref val; |
| 82 | record_filter() : filter_type(record_filter_type_skip), ff_offset(0) { } |
| 83 | }; |
| 84 | |
| 85 | struct cmd_open_args { |
| 86 | size_t pst_id; |
| 87 | const char *dbn; |
| 88 | const char *tbl; |
| 89 | const char *idx; |
| 90 | const char *retflds; |
| 91 | const char *filflds; |
| 92 | cmd_open_args() : pst_id(0), dbn(0), tbl(0), idx(0), retflds(0), |
| 93 | filflds(0) { } |
| 94 | }; |
| 95 | |
| 96 | struct cmd_exec_args { |
| 97 | const prep_stmt *pst; |
| 98 | string_ref op; |
| 99 | const string_ref *kvals; |
| 100 | size_t kvalslen; |
| 101 | uint32_t limit; |
| 102 | uint32_t skip; |
| 103 | string_ref mod_op; |
| 104 | const string_ref *uvals; /* size must be pst->retfieelds.size() */ |
| 105 | const record_filter *filters; |
| 106 | int invalues_keypart; |
| 107 | const string_ref *invalues; |
| 108 | size_t invalueslen; |
| 109 | cmd_exec_args() : pst(0), kvals(0), kvalslen(0), limit(0), skip(0), |
| 110 | uvals(0), filters(0), invalues_keypart(-1), invalues(0), invalueslen(0) { } |
| 111 | }; |
| 112 | |
| 113 | struct dbcontext_i { |
| 114 | virtual ~dbcontext_i() { } |
| 115 | virtual void init_thread(const void *stack_bottom, |
| 116 | volatile int& shutdown_flag) = 0; |
| 117 | virtual void term_thread() = 0; |
| 118 | virtual bool check_alive() = 0; |
| 119 | virtual void lock_tables_if() = 0; |
| 120 | virtual void unlock_tables_if() = 0; |
| 121 | virtual bool get_commit_error() = 0; |
| 122 | virtual void clear_error() = 0; |
| 123 | virtual void close_tables_if() = 0; |
| 124 | virtual void table_addref(size_t tbl_id) = 0; /* TODO: hide */ |
| 125 | virtual void table_release(size_t tbl_id) = 0; /* TODO: hide */ |
| 126 | virtual void cmd_open(dbcallback_i& cb, const cmd_open_args& args) = 0; |
| 127 | virtual void cmd_exec(dbcallback_i& cb, const cmd_exec_args& args) = 0; |
| 128 | virtual void set_statistics(size_t num_conns, size_t num_active) = 0; |
| 129 | }; |
| 130 | |
| 131 | }; |
| 132 | |
| 133 | extern unsigned long long int open_tables_count; |
| 134 | extern unsigned long long int close_tables_count; |
| 135 | extern unsigned long long int lock_tables_count; |
| 136 | extern unsigned long long int unlock_tables_count; |
| 137 | #if 0 |
| 138 | extern unsigned long long int index_exec_count; |
| 139 | #endif |
| 140 | |
| 141 | #endif |
| 142 | |
| 143 | |