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