CUBRID Engine  latest
record_descriptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // record_descriptor - RECDES extended functionality
21 //
22 
23 #ifndef _RECORD_DESCRIPTOR_HPP_
24 #define _RECORD_DESCRIPTOR_HPP_
25 
26 #include "mem_block.hpp"
27 #include "memory_alloc.h"
29 #include "packable_object.hpp"
30 #include "storage_common.h"
31 
32 #include <cinttypes>
33 
34 // forward definitions
35 namespace cubthread
36 {
37  class entry;
38 };
39 
40 // record_descriptor extends functionality for recdes:
41 //
42 // typedef struct recdes RECDES; /* RECORD DESCRIPTOR */
43 // struct recdes
44 // {
45 // int area_size; /* Length of the allocated area. It includes only the data field. The value is negative
46 // * if data is inside buffer. For example, peeking in a slotted page. */
47 // int length; /* Length of the data. Does not include the length and type fields */
48 // INT16 type; /* Type of record */
49 // char *data; /* The data */
50 // };
51 //
52 
53 // explicit aliases for PEEK/COPY
54 enum class record_get_mode
55 {
56  PEEK_RECORD = PEEK,
58 };
59 
61 {
62  public:
63 
64  // constructors
65 
66  // default
68  ~record_descriptor (void);
69 
70  record_descriptor (const char *data, std::size_t size);
71 
72  // based on recdes
74 
76 
77  void set_recdes (const recdes &rec);
78 
79  // peek record from page; changes into record data will not be permitted
80  int peek (cubthread::entry *thread_p, PAGE_PTR page, PGSLOTID slotid);
81 
82  // copy record from page
83  int copy (cubthread::entry *thread_p, PAGE_PTR page, PGSLOTID slotid);
84 
85  // get record from page with peek or copy mode
86  int get (cubthread::entry *thread_p, PAGE_PTR page, PGSLOTID slotid, record_get_mode mode);
87 
88  // getters
89  const recdes &get_recdes (void) const; // get recdes
90 
91  const char *get_data (void) const; // get record data
92  std::size_t get_size (void) const; // get record size
93  char *get_data_for_modify (void);
94 
95  // setters
96  void set_data (const char *data, std::size_t size); // set record data to byte array
97  template <typename T>
98  void set_data_to_object (const T &t); // set record data to object
99 
100  void set_record_length (std::size_t length);
101  void set_type (std::int16_t type);
102 
103  //
104  // manipulate record data
105  //
106 
107  // replace old_size bytes at offset with new_size bytes from new_data
108  void modify_data (std::size_t offset, std::size_t old_size, std::size_t new_size, const char *new_data);
109 
110  // delete data_size bytes from offset
111  void delete_data (std::size_t offset, std::size_t data_size);
112 
113  // insert new_size bytes from new_data at offset
114  void insert_data (std::size_t offset, std::size_t new_size, const char *new_data);
115 
116  // move record data starting from source_offset to dest_offset
117  void move_data (std::size_t dest_offset, std::size_t source_offset);
118 
119  void pack (cubpacking::packer &packer) const override;
120  void unpack (cubpacking::unpacker &unpacker) override;
121  std::size_t get_packed_size (cubpacking::packer &packer, std::size_t curr_offset) const override;
122 
123  //
124  // manipulate record memory buffer
125  //
126 
127  // resize record buffer
128  void resize_buffer (std::size_t size);
129  // set external buffer; record type is set to new automatically
130  void set_external_buffer (char *buf, std::size_t buf_size);
131  template <std::size_t S>
132  void set_external_buffer (cubmem::stack_block<S> &membuf);
133  void release_buffer (char *&data, std::size_t &size);
134 
135  private:
136 
137  // debug function to check if data changes are permitted; e.g. changes into peeked records are not permitted
138  void check_changes_are_permitted (void) const;
139  bool is_mutable () const;
140 
141  void update_source_after_get (record_get_mode mode);
142 
143  // source of record data
144  enum class data_source
145  {
146  INVALID, // invalid data
147  PEEKED, // record data peeked from page
148  COPIED, // record data copied from page or another record
149  NEW, // record data is new
150  IMMUTABLE // record data is a constant buffer or object
151  };
152 
153  recdes m_recdes; // underlaying recdes
155  // destruction
156  data_source m_data_source; // source of record data
157 };
158 
160 // template/inline
162 
163 template <std::size_t S>
164 void
166 {
167  m_own_data.freemem ();
168  m_recdes.area_size = membuf.SIZE;
169  m_recdes.data = membuf.get_ptr ();
170  m_data_source = data_source::NEW;
171 }
172 
173 template <typename T>
174 void
176 {
177  set_data (reinterpret_cast<const char *> (&t), sizeof (t));
178 }
179 
180 #endif // !_RECORD_DESCRIPTOR_HPP_
char * PAGE_PTR
void set_external_buffer(char *buf, std::size_t buf_size)
void set_data_to_object(const T &t)
const block_allocator PRIVATE_BLOCK_ALLOCATOR
char * get_ptr(void)
Definition: mem_block.hpp:286
cubmem::extensible_block m_own_data
static enum scanner_mode mode
static const size_t SIZE
Definition: mem_block.hpp:82
record_get_mode
static const bool COPY
INT16 PGSLOTID
#define PEEK
Definition: file_io.h:74