-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTreeKey.cpp
More file actions
150 lines (130 loc) · 3.15 KB
/
Copy pathBTreeKey.cpp
File metadata and controls
150 lines (130 loc) · 3.15 KB
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
143
144
145
146
147
148
149
150
/*
* This file is part of PRODNAME.
*
* See the COPYRIGHT file at the top-level directory of this distribution
* for details of code ownership.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "BTreeKey.h"
BTreeKey::BTreeKey(BTreeKeyTemplate *Tmp)
{
_template = Tmp;
_buffer = new char[Tmp->KeySize];
_payload = new char[Tmp->PayloadSize];
_code = 0;
}
BTreeKey::BTreeKey(BTreeKey &Origin)
{
_template = Origin._template;
_buffer = new char[_template->KeySize];
_payload = new char[_template->PayloadSize];
memcpy(_buffer, Origin._buffer, _template->KeySize);
memcpy(_payload, Origin._payload, _template->PayloadSize);
_code = 0;
}
BTreeKey::~BTreeKey()
{
if (_buffer != NULL)
{
delete[] _buffer;
_buffer = NULL;
}
if (_payload != NULL)
{
delete[]_payload;
_payload = NULL;
}
}
int BTreeKey::Write(char* buf)
{
buf[0] = _code;
memcpy(buf + 1, _buffer, _template->KeySize);
memcpy(buf + _template->KeySize + 1, _payload, _template->PayloadSize);
return _template->KeySize + 1 + _template->PayloadSize;
}
int BTreeKey::Read(const char* buf)
{
_code = buf[0];
memcpy(_buffer, buf + 1, _template->KeySize);
memcpy(_payload, buf + _template->KeySize + 1, _template->PayloadSize);
return _template->KeySize + 1 + _template->PayloadSize;
}
int BTreeKey::Length()
{
return _template->PayloadSize + _template->KeySize + 1;
}
const void* BTreeKey::Payload()
{
return _payload;
}
const void* BTreeKey::Key()
{
return _buffer;
}
unsigned char BTreeKey::Code()
{
return _code;
}
void BTreeKey::Delete()
{
_code |= KEYCODE_DELETED;
}
BTreeKeyTemplate::BTreeKeyTemplate()
{
KeySize = 32;
PayloadSize = sizeof(std::streamoff);
CompareInverse = false;
}
int memrcmp(const char* a, const char* b, int ln)
{
for (int x = ln - 1; x >= 0; x--)
{
if (a[x] > b[x])
return 1;
else
{
if (a[x] < b[x])
return -1;
}
}
return 0;
}
BTreeKeyTemplate::~BTreeKeyTemplate()
{
}
int BTreeKeyTemplate::DiskKeySize()
{
return KeySize + PayloadSize + 1;
}
int BTreeKey::CompareTo(BTreeKey* To)
{
if (_template->CompareInverse == false)
return memcmp(_buffer, To->_buffer, _template->KeySize);
else
return memrcmp(_buffer, To->_buffer, _template->KeySize);
}
int BTreeKey::CompareTo(const void *ky)
{
if (_template->CompareInverse == false)
return memcmp(_buffer, ky, _template->KeySize);
else
return memrcmp(_buffer, (const char *)ky, _template->KeySize);
}
void BTreeKey::Set(const void* ky, const void* pl)
{
memcpy(_buffer, ky, _template->KeySize);
memcpy(_payload, pl, _template->PayloadSize);
}