summaryrefslogtreecommitdiffstats
path: root/lurker/libesort/Master.cpp
blob: b1b332703696fe36ad75c9fbd6a07bd26f71cde6 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*  $Id: Master.cpp 1649 2009-10-19 14:35:01Z terpstra $
 *  
 *  Master.cpp - Coordinate commit+read interface
 *  
 *  Copyright (C) 2002 - Wesley W. Terpstra
 *  
 *  License: GPL
 *  
 *  Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
 *  
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; version 2.
 *    
 *    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 General Public License for more details.
 *    
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _FILE_OFFSET_BITS 64
  
#include "io.h"

#include "Master.h"
#include "Source.h"
#include "Failer.h"
#include "Transaction.h"

#include <list>
#include <iostream>
#include <cerrno>
#include <cassert>

namespace ESort
{

Master::Master(const Parameters& p)
 : view(p), memory(), man()
{
}

Master::~Master()
{
}

int Master::init(const string& db, int mode)
{
	if (man.dbopen(view, db, mode) != 0) return -1;
	
	return 0;
}

struct CleanupHelper
{
 public:
 	DbMan*	man;
 	int	fd;
 	string	id;
 	
 	CleanupHelper(DbMan* man_) : man(man_), fd(-1) { }
 	~CleanupHelper()
 	{
 		if (fd != -1)
 		{
 			close(fd);
 			man->killSub(id);
 		}
 	}
};

int Master::commit()
{
	if (memory.empty()) return 0;
	
	CleanupHelper scopedData(&man);
	scopedData.fd = man.openNew(scopedData.id);
	if (scopedData.fd == -1) return -1;
	
	Transaction tran(scopedData.fd, &view.params);
	Merger merge(view.params.unique(), true); // forward
	auto_ptr<Source> m(memory.openMemory("", true)); // forward
	int ok = m->advance();
	assert (ok != -1); // memory was already checked -> not empty!
	merge.merge(m.get());
	m.release(); // seperate in case of exception above
	
	// What category is our RAM?
	int category = memory.category(view.params);
//	std::cout << "Category: " << category << std::endl;
	
	View::Files::iterator kill;
	for (kill = view.files.begin(); kill != view.files.end(); ++kill)
	{
//		std::cout << "Merging: " << kill->category << std::endl;
		if (kill->category > category)
			break; // keep this one
		
		// If we have at least one of the same size, roll up as in
		// binary addition
		if (kill->category == category)
			category++;
		
		auto_ptr<Source> f(
			const_cast<File*>(&*kill)->openBlock(0, true));
		if (!f.get()) return -1; // something broke?
		if (f->advance() == -1) return -1; // always has keys?!
		merge.merge(f.get());
		f.release(); // above might throw
	}
	
	// The list of ids which we commit
	std::set<string> keepIds;
	keepIds.insert(scopedData.id); // the new id
	
	// keep all of these ids
	for (View::Files::iterator keep = kill; keep != view.files.end(); ++keep) 
		keepIds.insert(keep->id);
	
	if (merge.skiptill("", true) != 0)
		return -1; // must work?! ram has entries
	
	int dup;
	while ((dup = merge.advance()) != -1)
	{	// there is stuff to merge
		if (tran.write(merge.key.length(), dup, merge.key.c_str()) != 0)
			return -1;
	}
	if (tran.finish() != 0)
		return -1;
	
	// Commit the summary information
	if (man.commit(view.params, keepIds) != 0)
		return -1;
	
	// done with the buffer
	memory.flush();
	
	// Queue useless files for delete
	std::set<string> killIds;
	for (View::Files::iterator zap = view.files.begin(); zap != kill; ++zap)
		killIds.insert(zap->id);
	
	// Purge useless files from the View (closes them)
	view.files.erase(view.files.begin(), kill);
	
	// Now that they are closed, delete them
	for (std::set<string>::iterator del = killIds.begin(); del != killIds.end(); ++del)
		man.killSub(*del);
	
	// Move the transaction to a File; thus becoming a part of the view
	int fd = scopedData.fd;
	scopedData.fd = -1;
	view.files.insert(File(scopedData.id, fd, &view.params));
	
	return 0;
}

int Master::insert(const string& k)
{
	assert (k.length() < view.params.keySize());
	
	// always succeeds
	memory.insert(k);
	return 0;
}

auto_ptr<Walker> Master::seek(const string& k, Direction dir)
{
	assert (dir == Forward || dir == Backward);
	
	auto_ptr<Merger> out(new Merger(view.params.unique(), dir == Forward));
	
	if (view.rawseek(out.get(), k, dir == Forward) != 0)
		return auto_ptr<Walker>(new Failer(errno));
	
	auto_ptr<Source> s = memory.openMemory(k, dir == Forward);
	assert (s.get()); // always works
	
	// only possible error is eof
	if (s->advance() != -1) out->merge(s.release());
	// else kill it on scope out
	
	if (out->skiptill(k, dir == Forward) == -1)
		return auto_ptr<Walker>(new Failer(errno));
	
	return auto_ptr<Walker>(out);
}

auto_ptr<Walker> Master::seek(const string& pfx, const string& k, Direction dir)
{
	assert (dir == Forward || dir == Backward);
	
	auto_ptr<PrefixMerger> out(new PrefixMerger(view.params.unique(), dir == Forward));
	
	if (view.rawseek(out.get(), pfx + k, dir == Forward) != 0)
		return auto_ptr<Walker>(new Failer(errno));
	
	auto_ptr<Source> s = memory.openMemory(k, dir == Forward);
	assert (s.get()); // always works
	
	// only possible error is eof
	if (s->advance() != -1) out->merge(s.release());
	// else kill it on scope out
	
	if (out->skiptill(pfx, k, dir == Forward) == -1)
		return auto_ptr<Walker>(new Failer(errno));
	
	return auto_ptr<Walker>(out);
}

auto_ptr<Writer> Writer::opendb(const string& db, const Parameters& p, int mode)
{
	auto_ptr<Master> m(new Master(p));
	
	if (m->init(db, mode) != 0)
		return auto_ptr<Writer>(0);
	
	return auto_ptr<Writer>(m);
}

}