summaryrefslogtreecommitdiff
path: root/api/leveldb/hyper_wt.cc
blob: 5b532495049c7f89b4d66dc231dd0bd4848fe856 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*-
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "leveldb_wt.h"
#include <errno.h>
#include <sstream>
#include <sys/param.h>
#include <sys/stat.h>

using leveldb::ReplayIterator;
using leveldb::Status;

// Fill in missing methods from the interface
ReplayIterator::ReplayIterator() {}
ReplayIterator::~ReplayIterator() {}

class ReplayIteratorImpl : public ReplayIterator {
 public:
  ReplayIteratorImpl(OperationContext *context) : context_(context), cursor_(NULL) {
    WT_SESSION *session = context_->GetSession();
    int ret = session->open_cursor(
        session, "log:", NULL, NULL, &cursor_);
    status_ = WiredTigerErrorToStatus(ret);
    valid_ = false;
    // Position on first record.  valid_ will be set appropriately.
    Next();
  }

  ReplayIteratorImpl(OperationContext *context, const std::string& timestamp) :
    context_(context), cursor_(NULL) {

    WT_SESSION *session = context_->GetSession();
    int ret = session->open_cursor(
        session, "log:", NULL, NULL, &cursor_);
    status_ = WiredTigerErrorToStatus(ret);
    valid_ = false;
    // Position on requested record.  valid_ will be set appropriately.
    SeekTo(timestamp);
  }

  // An iterator is either positioned at a deleted key, present key/value pair,
  // or not valid.  This method returns true iff the iterator is valid.
  virtual bool Valid() { return valid_; }

  // Moves to the next entry in the source.  After this call, Valid() is
  // true iff the iterator was not positioned at the last entry in the source.
  // REQUIRES: Valid()
  virtual void Next();

  // Position at the first key in the source that at or past target for this
  // pass.  Note that this is unlike the Seek call, as the ReplayIterator is
  // unsorted.
  // The iterator is Valid() after this call iff the source contains
  // an entry that comes at or past target.
  // Per Robert at Hyperdex, the SkipTo functions are hacky optimizations
  // for LevelDB and its key layout.  It is okay for them to be no-ops. 
  virtual void SkipTo(const Slice& target) { }
  virtual void SkipToLast() { }
  virtual void SeekTo(const std::string& timestamp);
  virtual void SeekToLast();

  // Return true if the current entry points to a key-value pair.  If this
  // returns false, it means the current entry is a deleted entry.
  virtual bool HasValue() { 
    assert(Valid());
    if (optype == WT_LOGOP_ROW_PUT ||
	optype == WT_LOGOP_COL_PUT)
	  return true;
    else
	  return false;
  }

  int Compare(ReplayIteratorImpl* other) {
    int cmp;
    assert(Valid());
    // assert(other->Valid());
    int ret = cursor_->compare(cursor_, other->cursor_, &cmp);
    status_ = WiredTigerErrorToStatus(ret);
    return (cmp);
  }

  // Return the key for the current entry.  The underlying storage for
  // the returned slice is valid only until the next modification of
  // the iterator.
  // REQUIRES: Valid()
  virtual Slice key() const { return Slice((const char *)key_.data, key_.size); }

  // Return the value for the current entry.  The underlying storage for
  // the returned slice is valid only until the next modification of
  // the iterator.
  // REQUIRES: !AtEnd() && !AtStart()
  virtual Slice value() const { return Slice((const char *)value_.data, value_.size); }

  // If an error has occurred, return it.  Else return an ok status.
  virtual Status status() const { return status_; }

  // must be released by giving it back to the DB
  virtual ~ReplayIteratorImpl() {
    int ret = Close();
    assert(ret == 0);
  }

  std::string GetTimestamp() {
	char lsn[256];
        assert(Valid());
	snprintf(lsn, sizeof(lsn), WT_TIMESTAMP_FORMAT,
	    lsn_.file, lsn_.offset);
	return (std::string(lsn));
  }

  int Close() {
    int ret = 0;
    if (cursor_ != NULL)
      ret = cursor_->close(cursor_);
    status_ = WiredTigerErrorToStatus(ret);
    valid_ = false;
    cursor_ = NULL;
    return (ret);
  }

 private:
  void SeekTo(WT_LSN *lsn);
  // No copying allowed
  ReplayIteratorImpl(const ReplayIterator&) { }
  void operator=(const ReplayIterator&) { }
  OperationContext *context_;
  Status status_;
  WT_CURSOR *cursor_;
  WT_ITEM key_, value_;
  WT_LSN lsn_;
  bool valid_;
  uint64_t txnid;
  uint32_t fileid, opcount, optype, rectype;
};

void
ReplayIteratorImpl::Next() {
	int ret = 0;

	if (cursor_ != NULL) {
		while ((ret = cursor_->next(cursor_)) == 0) {
			ret = cursor_->get_key(cursor_,
			    &lsn_.file, &lsn_.offset, &opcount);
			if (ret != 0)
				break;
			ret = cursor_->get_value(cursor_,
			    &txnid, &rectype, &optype, &fileid, &key_, &value_);
			if (ret != 0)
				break;
			// Next() is only interested in modification operations.
			// Continue for any other type of record.
			if (WT_VALID_OPERATION(fileid, optype)) {
				valid_ = true;
				break;
			}
		}
		status_ = WiredTigerErrorToStatus(ret);
		if (ret != 0) {
			valid_ = false;
			ret = Close();
			assert(ret == 0);
		}
	}
}

void
ReplayIteratorImpl::SeekToLast() {
	int ret = 0;
	WT_LSN last_lsn;

	last_lsn.file = 0;
	if (cursor_ != NULL) {
		// Walk the log to the end, then set the cursor on the
		// last valid LSN we saw.
		while ((ret = cursor_->next(cursor_)) == 0) {
			ret = cursor_->get_key(cursor_,
			    &lsn_.file, &lsn_.offset, &opcount);
			if (ret != 0)
				break;
			ret = cursor_->get_value(cursor_,
			    &txnid, &rectype, &optype, &fileid, &key_, &value_);
			if (ret != 0)
				break;
			// We're only interested in modification operations.
			// Continue for any other type of record.
			if (WT_VALID_OPERATION(fileid, optype)) {
				valid_ = true;
				last_lsn = lsn_;
			}
		}
		// We reached the end of log
		if (ret != WT_NOTFOUND || last_lsn.file == 0) {
			valid_ = false;
			ret = Close();
			assert(ret == 0);
		} else
			SeekTo(&last_lsn);
	}
}

void
ReplayIteratorImpl::SeekTo(const std::string& timestamp) {
	WT_LSN target_lsn;
	int ret = 0;

	if (timestamp == "all") {
		if (cursor_ != NULL) {
			ret = cursor_->reset(cursor_);
			status_ = WiredTigerErrorToStatus(ret);
			if (ret != 0)
				return;
			Next();
			return;
		}
	}
	if (timestamp == "now") {
		SeekToLast();
		return;
	}
	sscanf(timestamp.c_str(), WT_TIMESTAMP_FORMAT,
	    &target_lsn.file, &target_lsn.offset);
	SeekTo(&target_lsn);
}

// Set the cursor on the first modification record at or after the
// given LSN.
void
ReplayIteratorImpl::SeekTo(WT_LSN *target_lsn) {
	int ret = 0;

	valid_ = false;
	if (cursor_ != NULL) {
		cursor_->set_key(cursor_,
		    target_lsn->file, target_lsn->offset, 0, 0);
		ret = cursor_->search(cursor_);
		status_ = WiredTigerErrorToStatus(ret);
		if (ret != 0)
			return;
		// If we were successful, set up the info.
		ret = cursor_->get_key(cursor_,
		    &lsn_.file, &lsn_.offset, &opcount);
		status_ = WiredTigerErrorToStatus(ret);
		if (ret != 0)
			return;
		ret = cursor_->get_value(cursor_,
		    &txnid, &rectype, &optype, &fileid, &key_, &value_);
		status_ = WiredTigerErrorToStatus(ret);
		if (ret != 0)
			return;
		valid_ = true;
		// We're only interested in modification operations.
		// Continue for any other type of record.
		if (WT_VALID_OPERATION(fileid, optype))
			Next();
	}
}

// Create a live backup of a live LevelDB instance.
// The backup is stored in a directory named "backup-<name>" under the top
// level of the open LevelDB database.  The implementation is permitted, and
// even encouraged, to improve the performance of this call through
// hard-links.
Status
DbImpl::LiveBackup(const Slice& name)
{
	OperationContext *context = GetContext();
	WT_SESSION *session = context->GetSession();
	WT_CURSOR *cursor;
	int ret = session->open_cursor(
            session, "backup:", NULL, NULL, &cursor);
	int t_ret;
	const char *filename;
	const char *home = conn_->get_home(conn_);
	char backup[MAXPATHLEN], buf[MAXPATHLEN * 2];
 
	// If we couldn't open the backup cursor, we're done.
	if (ret != 0)
		return (WiredTigerErrorToStatus(ret));

	// Remove any old directory and create the backup directory.
	// WT single-threads hot backups.  If we get here we already have
	// the backup cursor open and we do not have to worry about other
	// threads trying to remove and recreate the same directory out
	// from under us.
	snprintf(buf, sizeof(buf), "rm -rf %s/backup-%s", home,
	    (char *)name.data());
	if ((ret = system(buf)) != 0)
		return WiredTigerErrorToStatus(ret);
	snprintf(backup, sizeof(backup), "%s/backup-%s", home,
	    (char *)name.data());
	if ((ret = mkdir(backup, 0777)) != 0)
		return WiredTigerErrorToStatus(ret);
	// Copy all files returned by backup cursor.
	while ((ret = cursor->next(cursor)) == 0 &&
	    (ret = cursor->get_key(cursor, &filename)) == 0) {
		snprintf(buf, sizeof(buf), "cp %s/%s %s/%s",
		    home, filename, backup, filename);
		if ((ret = system(buf)) != 0)
			break;
	}
	if (ret == WT_NOTFOUND)
		ret = 0;
	if ((t_ret = cursor->close(cursor)) != 0 && ret == 0)
		ret = t_ret;
	// Does this now require walking the log with a ReplayIterator
	// to apply all the operations onto the backup table?
	return (WiredTigerErrorToStatus(ret));
}

// Return an opaque timestamp that identifies the current point in time of the
// database.  This timestamp may be subsequently presented to the
// NewReplayIterator method to create a ReplayIterator.
void
DbImpl::GetReplayTimestamp(std::string* timestamp)
{
	OperationContext *context = GetContext();
	ReplayIteratorImpl *iter = new ReplayIteratorImpl(context);

	iter->SeekToLast();
	*timestamp = iter->GetTimestamp();
	ReleaseReplayIterator(iter);
}

// Set the lower bound for manual garbage collection.  This method only takes
// effect when Options.manual_garbage_collection is true.
void
DbImpl::AllowGarbageCollectBeforeTimestamp(const std::string& timestamp)
{
}

// Validate the timestamp
bool
DbImpl::ValidateTimestamp(const std::string& timestamp)
{
	bool valid;
	OperationContext *context = GetContext();
	ReplayIteratorImpl *iter = new ReplayIteratorImpl(context);

	// The SeekTo function will handle "all" or "now".
	iter->SeekTo(timestamp);
	valid = iter->Valid();
	ReleaseReplayIterator(iter);
	return valid;
}

// Compare two timestamps and return -1, 0, 1 for lt, eq, gt
int
DbImpl::CompareTimestamps(const std::string& lhs, const std::string& rhs)
{
	OperationContext *context = GetContext();
	ReplayIteratorImpl *lhiter = new ReplayIteratorImpl(context);
	ReplayIteratorImpl *rhiter = new ReplayIteratorImpl(context);
	int cmp = 0;
	
	// The SeekTo function will handle "all" or "now".
	lhiter->SeekTo(lhs);
	rhiter->SeekTo(rhs);
	if (lhiter->Valid() && rhiter->Valid())
		cmp = lhiter->Compare(rhiter);
	ReleaseReplayIterator(lhiter);
	ReleaseReplayIterator(rhiter);
	return cmp;
}

// Return a ReplayIterator that returns every write operation performed after
// the timestamp.
Status
DbImpl::GetReplayIterator(const std::string& timestamp,
			   ReplayIterator** iter)
{
	OperationContext *context = GetContext();
	*iter = new ReplayIteratorImpl(context, timestamp);
	return ((*iter)->status());
}

// Release a previously allocated replay iterator.
void
DbImpl::ReleaseReplayIterator(ReplayIterator* iter)
{
	delete static_cast<ReplayIteratorImpl *>(iter);
}