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 <my_global.h> |
40 | #include "portability/toku_portability.h" |
41 | |
42 | #include "ft/msg.h" |
43 | #include "ft/txn/xids.h" |
44 | #include "util/dbt.h" |
45 | |
46 | ft_msg::ft_msg(const DBT *key, const DBT *val, enum ft_msg_type t, MSN m, XIDS x) : |
47 | _key(key ? *key : toku_empty_dbt()), |
48 | _val(val ? *val : toku_empty_dbt()), |
49 | _type(t), _msn(m), _xids(x) { |
50 | } |
51 | |
52 | ft_msg ft_msg::deserialize_from_rbuf(struct rbuf *rb, XIDS *x, bool *is_fresh) { |
53 | const void *keyp, *valp; |
54 | uint32_t keylen, vallen; |
55 | enum ft_msg_type t = (enum ft_msg_type) rbuf_char(rb); |
56 | *is_fresh = rbuf_char(rb); |
57 | MSN m = rbuf_MSN(rb); |
58 | toku_xids_create_from_buffer(rb, x); |
59 | rbuf_bytes(rb, &keyp, &keylen); |
60 | rbuf_bytes(rb, &valp, &vallen); |
61 | |
62 | DBT k, v; |
63 | return ft_msg(toku_fill_dbt(&k, keyp, keylen), toku_fill_dbt(&v, valp, vallen), t, m, *x); |
64 | } |
65 | |
66 | ft_msg ft_msg::deserialize_from_rbuf_v13(struct rbuf *rb, MSN m, XIDS *x) { |
67 | const void *keyp, *valp; |
68 | uint32_t keylen, vallen; |
69 | enum ft_msg_type t = (enum ft_msg_type) rbuf_char(rb); |
70 | toku_xids_create_from_buffer(rb, x); |
71 | rbuf_bytes(rb, &keyp, &keylen); |
72 | rbuf_bytes(rb, &valp, &vallen); |
73 | |
74 | DBT k, v; |
75 | return ft_msg(toku_fill_dbt(&k, keyp, keylen), toku_fill_dbt(&v, valp, vallen), t, m, *x); |
76 | } |
77 | |
78 | const DBT *ft_msg::kdbt() const { |
79 | return &_key; |
80 | } |
81 | |
82 | const DBT *ft_msg::vdbt() const { |
83 | return &_val; |
84 | } |
85 | |
86 | enum ft_msg_type ft_msg::type() const { |
87 | return _type; |
88 | } |
89 | |
90 | MSN ft_msg::msn() const { |
91 | return _msn; |
92 | } |
93 | |
94 | XIDS ft_msg::xids() const { |
95 | return _xids; |
96 | } |
97 | |
98 | size_t ft_msg::total_size() const { |
99 | // Must store two 4-byte lengths |
100 | static const size_t key_val_overhead = 8; |
101 | |
102 | // 1 byte type, 1 byte freshness, then 8 byte MSN |
103 | static const size_t msg_overhead = 2 + sizeof(MSN); |
104 | |
105 | static const size_t total_overhead = key_val_overhead + msg_overhead; |
106 | |
107 | const size_t keyval_size = _key.size + _val.size; |
108 | const size_t xids_size = toku_xids_get_serialize_size(xids()); |
109 | return total_overhead + keyval_size + xids_size; |
110 | } |
111 | |
112 | void ft_msg::serialize_to_wbuf(struct wbuf *wb, bool is_fresh) const { |
113 | wbuf_nocrc_char(wb, (unsigned char) _type); |
114 | wbuf_nocrc_char(wb, (unsigned char) is_fresh); |
115 | wbuf_MSN(wb, _msn); |
116 | wbuf_nocrc_xids(wb, _xids); |
117 | wbuf_nocrc_bytes(wb, _key.data, _key.size); |
118 | wbuf_nocrc_bytes(wb, _val.data, _val.size); |
119 | } |
120 | |
121 | |