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