/
Batch.cpp
89 lines (71 loc) · 1.97 KB
/
Batch.cpp
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
// Copyright (C) 2014 Michael Biggs. See the COPYING file at the top-level
// directory of this distribution and at http://shok.io/code/copyright.html
#include "Batch.h"
#include "List.h"
#include "SLog.h"
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;
#include <ostream>
#include <string>
using std::ostream;
using std::string;
using namespace statik;
/* BatchItem public */
std::string Batch::BatchItem::Print() const {
if (!node) {
throw SError("Cannot print defective BatchItem");
}
string s(UnMapBatchOp(op) + " ");
if (OP_DELETE == op) {
s += lexical_cast<string>(node);
} else {
s += lexical_cast<string>(node) + " " + node->Print();
}
if (pos) {
s += " @ " + lexical_cast<string>(pos) + " " + pos->Print();
}
return s;
}
/* Batch public */
std::string Batch::UnMapBatchOp(const BATCH_OP& op) {
switch (op) {
case OP_INSERT: return "OP_INSERT";
case OP_UPDATE: return "OP_UPDATE";
case OP_DELETE: return "OP_DELETE";
default: throw SError("Cannot unmap unknown BATCH_OP");
}
}
void Batch::Insert(const List& inode, const List* pos = NULL) {
m_batch.push_back(BatchItem(inode, OP_INSERT, pos));
}
void Batch::Delete(const List& inode) {
m_batch.push_back(BatchItem(inode, OP_DELETE));
}
void Batch::Update(const List& inode) {
m_batch.push_back(BatchItem(inode, OP_UPDATE));
}
void Batch::Accept(const Batch& batch) {
for (batch_iter i = batch.begin(); i != batch.end(); ++i) {
m_batch.push_back(*i);
}
}
void Batch::Clear() {
g_log.debug() << "Batch: Clearing " << m_batch.size() << " items out of batch";
m_batch.clear();
}
string Batch::Print() const {
string s;
for (batch_iter i = m_batch.begin(); i != m_batch.end(); ++i) {
s += "\n" + i->Print();
}
return s;
}
/* non-member */
ostream& statik::operator<< (ostream& out, const Batch::BatchItem& item) {
out << item.Print();
return out;
}
ostream& statik::operator<< (ostream& out, const Batch& batch) {
out << batch.Print();
return out;
}