summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/api/leveldb/rocks_wt.cc
blob: 6ccab2c1e78c68817bfe67dbd5cfc8ccd4ea7b47 (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
/*-
 * 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 <sys/stat.h>
#include <unistd.h>
#include <sstream>

using leveldb::Cache;
using leveldb::DB;
using leveldb::FlushOptions;
using leveldb::FilterPolicy;
using leveldb::Iterator;
using leveldb::Options;
using leveldb::ReadOptions;
using leveldb::WriteBatch;
using leveldb::WriteOptions;
using leveldb::Range;
using leveldb::Slice;
using leveldb::Snapshot;
using leveldb::Status;

static int
wtrocks_get_cursor(OperationContext *context, ColumnFamilyHandle *cfhp, WT_CURSOR **cursorp, int acquire=0)
{
	ColumnFamilyHandleImpl *cf =
	    static_cast<ColumnFamilyHandleImpl *>(cfhp);
	if (cf == NULL) {
		fprintf(stderr, "Missing column!\n");
		assert(0);
	}
	WT_CURSOR *c = context->GetCursor(cf->GetID());
	if (c == NULL) {
		WT_SESSION *session = context->GetSession();
		int ret;
		if ((ret = session->open_cursor(
		    session, cf->GetURI().c_str(), NULL, NULL, &c)) != 0) {
			fprintf(stderr, "Failed to open cursor on %s: %s\n", cf->GetURI().c_str(), wiredtiger_strerror(ret));
			return (ret);
		}
		if (!acquire)
			context->SetCursor(cf->GetID(), c);
	} else if (acquire)
		context->SetCursor(cf->GetID(), NULL);
	*cursorp = c;
	return (0);
}

Status
DB::ListColumnFamilies(
	Options const &options, std::string const &name,
	std::vector<std::string> *column_families)
{
	std::vector<std::string> cf;
	DB *dbptr;
	Status status = DB::Open(options, name, &dbptr);
	if (!status.ok())
		return status;
	DbImpl *db = static_cast<DbImpl *>(dbptr);
	OperationContext *context = db->GetContext();
	WT_SESSION *session = context->GetSession();
	WT_CURSOR *c;
	int ret = session->open_cursor(session, "metadata:", NULL, NULL, &c);
	if (ret != 0)
		goto err;
	c->set_key(c, "table:");
	/* Position on the first table entry */
	int cmp;
	ret = c->search_near(c, &cmp);
	if (ret != 0 || (cmp < 0 && (ret = c->next(c)) != 0))
		goto err;
	/* Add entries while we are getting "table" URIs. */
	for (; ret == 0; ret = c->next(c)) {
		const char *key;
		if ((ret = c->get_key(c, &key)) != 0)
			goto err;
		if (strncmp(key, "table:", strlen("table:")) != 0)
			break;
		printf("List column families: [%d] = %s\n", (int)cf.size(), key);
		cf.push_back(std::string(key + strlen("table:")));
	}

err:	delete db;
	/*
	 * WT_NOTFOUND is not an error: it just means we got to the end of the
	 * list of tables.
	 */
	if (ret == 0 || ret == WT_NOTFOUND) {
		*column_families = cf;
		ret = 0;
	}
	return WiredTigerErrorToStatus(ret);
}

Status
DB::Open(Options const &options, std::string const &name, const std::vector<ColumnFamilyDescriptor> &column_families, std::vector<ColumnFamilyHandle*> *handles, DB**dbptr)
{
	Status status = Open(options, name, dbptr);
	if (!status.ok())
		return status;
	DbImpl *db = static_cast<DbImpl *>(*dbptr);
	std::vector<ColumnFamilyHandle*> cfhandles(
	    column_families.size());
	for (size_t i = 0; i < column_families.size(); i++) {
		printf("Open column families: [%d] = %s\n", (int)i, column_families[i].name.c_str());
		cfhandles[i] = new ColumnFamilyHandleImpl(
		    db, column_families[i].name, (int)i);
	}
	db->SetColumns(*handles = cfhandles);
	return Status::OK();
}

void
WriteBatch::Handler::Merge(const Slice& key, const Slice& value)
{
}

void
WriteBatch::Handler::LogData(const Slice& blob)
{
}

Status
WriteBatchHandler::PutCF(
    uint32_t column_family_id, const Slice& key, const Slice& value)
{
	WT_CURSOR *cursor;
	int ret = wtrocks_get_cursor(context_, db_->GetCF(column_family_id), &cursor);
	if (ret != 0)
		return WiredTigerErrorToStatus(ret);
	WT_ITEM item;
	item.data = key.data();
	item.size = key.size();
	cursor->set_key(cursor, &item);
	item.data = value.data();
	item.size = value.size();
	cursor->set_value(cursor, &item);
	ret = cursor->insert(cursor);
	return WiredTigerErrorToStatus(ret);
}

Status
WriteBatchHandler::DeleteCF(uint32_t column_family_id, const Slice& key)
{
	WT_CURSOR *cursor;
	int ret = wtrocks_get_cursor(context_, db_->GetCF(column_family_id), &cursor);
	if (ret != 0)
		return WiredTigerErrorToStatus(ret);
	WT_ITEM item;
	item.data = key.data();
	item.size = key.size();
	cursor->set_key(cursor, &item);
	ret = cursor->remove(cursor);
	if (ret == 0) {
		int t_ret = cursor->reset(cursor);
		assert(t_ret == 0);
	} else if (ret == WT_NOTFOUND)
		ret = 0;
	return WiredTigerErrorToStatus(ret);
}

Status
DbImpl::Merge(WriteOptions const&, ColumnFamilyHandle*, Slice const&, Slice const&)
{
	return WiredTigerErrorToStatus(ENOTSUP);
}

Status
DbImpl::CreateColumnFamily(Options const &options, std::string const &name, ColumnFamilyHandle **cfhp)
{
	extern int wtleveldb_create(WT_CONNECTION *,
	    const Options &, std::string const &uri);
	int ret = wtleveldb_create(conn_, options, "table:" + name);
	if (ret != 0)
		return WiredTigerErrorToStatus(ret);
	int id = (int)columns_.size();
	*cfhp = new ColumnFamilyHandleImpl(this, name, id);
	printf("Create column family: [%d] = %s\n", id, name.c_str());
	columns_.push_back(*cfhp);
	return Status::OK();
}

Status
DbImpl::DropColumnFamily(ColumnFamilyHandle *cfhp)
{
	ColumnFamilyHandleImpl *cf =
	    static_cast<ColumnFamilyHandleImpl *>(cfhp);
	WT_SESSION *session = GetContext()->GetSession();
	int ret = session->drop(session, cf->GetURI().c_str(), NULL);
	return WiredTigerErrorToStatus(ret);
}

Status
DbImpl::Delete(WriteOptions const &write_options, ColumnFamilyHandle *cfhp, Slice const &key)
{
	WT_CURSOR *cursor;
	int ret = wtrocks_get_cursor(GetContext(), cfhp, &cursor);
	if (ret != 0)
		return WiredTigerErrorToStatus(ret);
	WT_ITEM item;
	item.data = key.data();
	item.size = key.size();
	cursor->set_key(cursor, &item);
	ret = cursor->remove(cursor);
	// Reset the WiredTiger cursor so it doesn't keep any pages pinned.
	// Track failures in debug builds since we don't expect failure, but
	// don't pass failures on - it's not necessary for correct operation.
	int t_ret = cursor->reset(cursor);
	assert(t_ret == 0);
	return WiredTigerErrorToStatus(ret);
}

Status
DbImpl::Flush(FlushOptions const&, ColumnFamilyHandle* cfhp)
{
	ColumnFamilyHandleImpl *cf =
	    static_cast<ColumnFamilyHandleImpl *>(cfhp);
	WT_SESSION *session = GetContext()->GetSession();
	return WiredTigerErrorToStatus(session->checkpoint(session, ("target=(\"" + cf->GetURI() + "\")").c_str()));
}

Status
DbImpl::Get(ReadOptions const &options, ColumnFamilyHandle *cfhp, Slice const &key, std::string *value)
{
	const char *errmsg = NULL;
	OperationContext *context = GetContext(options);

	WT_CURSOR *cursor;
	int ret = wtrocks_get_cursor(context, cfhp, &cursor);
	if (ret != 0)
		return WiredTigerErrorToStatus(ret);

	WT_ITEM item;
	item.data = key.data();
	item.size = key.size();
	cursor->set_key(cursor, &item);
	if ((ret = cursor->search(cursor)) == 0 &&
	    (ret = cursor->get_value(cursor, &item)) == 0) {
		*value = std::string((const char *)item.data, item.size);
		ret = cursor->reset(cursor);
	}
	if (ret == WT_NOTFOUND)
		errmsg = "DB::Get key not found";
	return WiredTigerErrorToStatus(ret, errmsg);
}

bool
DbImpl::GetProperty(ColumnFamilyHandle*, Slice const&, std::string*)
{
	return false;
}

std::vector<Status>
DbImpl::MultiGet(ReadOptions const&, std::vector<ColumnFamilyHandle*> const&, std::vector<Slice> const&, std::vector<std::string, std::allocator<std::string> >*)
{
	std::vector<Status> ret;
	ret.push_back(WiredTigerErrorToStatus(ENOTSUP));
	return ret;
}

Iterator *
DbImpl::NewIterator(ReadOptions const &options, ColumnFamilyHandle *cfhp)
{
	OperationContext *context = GetContext(options);

	WT_CURSOR *c;
	int ret = wtrocks_get_cursor(context, cfhp, &c, 1);
	assert(ret == 0);
	return new IteratorImpl(this, c,
	    static_cast<ColumnFamilyHandleImpl *>(cfhp)->GetID());
}

Status
DbImpl::Put(WriteOptions const &options, ColumnFamilyHandle *cfhp, Slice const &key, Slice const &value)
{
	WT_CURSOR *cursor;
	int ret = wtrocks_get_cursor(GetContext(), cfhp, &cursor);
	if (ret != 0)
		return WiredTigerErrorToStatus(ret);

	WT_ITEM item;
	item.data = key.data();
	item.size = key.size();
	cursor->set_key(cursor, &item);
	item.data = value.data();
	item.size = value.size();
	cursor->set_value(cursor, &item);
	ret = cursor->insert(cursor);
	return WiredTigerErrorToStatus(ret, NULL);
}