-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursor.cpp
More file actions
156 lines (134 loc) · 2.83 KB
/
Copy pathCursor.cpp
File metadata and controls
156 lines (134 loc) · 2.83 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
151
152
153
154
155
156
/*
* 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 "Cursor.h"
#include "BTree.h"
#include "History.h"
Cursor::Cursor()
{
Active = NULL;
Tree = NULL;
Offset = 0;
Index = 0;
EndPoint = 0;
Hist = NULL;
EoF = false;
}
Cursor::~Cursor()
{
if (Active != NULL)
delete Active;
}
void Cursor::SetPosition(std::streamoff offset, int index)
{
Offset = offset;
Index = index;
}
void Cursor::SetTree(std::shared_ptr<BTree> Trx)
{
Tree = Trx;
}
void Cursor::SetEndpoint(TIMESTAMP ep)
{
EndPoint = ep;
}
int Cursor::Next()
{
while (true)
{
//If we have reached the end, return false.
if (this->EoF == true)
return CURSOR_END;
if (Active == NULL)
{
//First usage of this cursor. Load content.
Active = new BTreeNode(Tree);
Active->Load(Tree->DS.get(), Offset);
if (Index == -1)
Index = 0;
if (Active->KeyCount() == 0)
{
return CURSOR_END;
}
return CURSOR_PRE;
}
else
{
Index++;
//Check to see if we need the next node...
if (Index >= Active->KeyCount())
{
//Destroy the current node.
TIMESTAMP Current = *((TIMESTAMP*)Active->Keys[Index - 1]->Key());
std::streamoff NextPos = Active->Next;
delete Active;
Active = NULL;
Offset = NextPos;
if (Offset != 0)
{
//Load the next node.
Active = new BTreeNode(Tree);
Active->Load(Tree->DS.get(), Offset);
Index = 0;
}
else
{
EoF = true;
}
}
}
if (EoF == true)
return CURSOR_END;
TIMESTAMP tm = *(TIMESTAMP*)Active->Keys[Index]->Payload();
if ((EndPoint > 0) && (tm > EndPoint))
{
EoF = true;
return CURSOR_POST;
}
if (Active->Keys[Index]->Code() & KEYCODE_DELETED == 0)
break;
}
return CURSOR_OK;
}
const void * Cursor::Key()
{
if (Active == NULL)
{
return NULL;
}
return Active->Keys[Index]->Key();
}
const void * Cursor::Value()
{
if (Active == NULL)
{
return NULL;
}
return Active->Keys[Index]->Payload();
}
void Cursor::Delete(bool WriteNow)
{
Active->Keys[Index]->Delete();
if (WriteNow == true) Active->Write();
}
void Cursor::End()
{
EoF = true;
}