summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/components/kvdb/leveldb.hpp
blob: 05a89a76f767b3ade14be769978874b9badaf3a2 (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_leveldb_hpp
#define tuscany_leveldb_hpp

#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <leveldb/db.h>

#include "string.hpp"
#include "list.hpp"
#include "value.hpp"
#include "monad.hpp"
#include "../../modules/scheme/eval.hpp"

namespace tuscany {
namespace leveldb {

/**
 * A reallocatable buffer.
 */
class buffer {
public:
    operator void*() const throw() {
        return buf;
    }

    operator unsigned char*() const throw() {
        return (unsigned char*)buf;
    }

    operator char*() const throw() {
        return (char*)buf;
    }

private:
    buffer(const unsigned int size, void* buf) : size(size), buf(buf) {
    }

    unsigned int size;
    void* buf;

    friend const buffer mkbuffer(const unsigned int sz);
    friend const buffer mkbuffer(const buffer& b, const unsigned int newsz);
    friend const bool free(const buffer& b);
};

/**
 * Make a new buffer.
 */
const buffer mkbuffer(const unsigned int sz) {
    return buffer(sz, malloc(sz));
}

/**
 * Make a new buffer by reallocating an existing one.
 */
const buffer mkbuffer(const buffer& b, const unsigned int sz) {
    if (sz <= b.size)
        return b;
    return buffer(sz, realloc(b.buf, sz));
}

/**
 * Free a buffer.
 */
const bool free(const buffer&b) {
    ::free(b.buf);
    return true;
}

/**
 * Convert a database name to an absolute path.
 */
const string absdbname(const string& name) {
    if (length(name) == 0 || c_str(name)[0] == '/')
        return name;
    char cwd[512];
    if (getcwd(cwd, sizeof(cwd)) == NULL)
        return name;
    return string(cwd) + "/" + name;
}

/**
 * Represents a LevelDB connection.
 */
class LevelDB {
public:
    LevelDB() : owner(false), fd(-1) {
        debug("leveldb::leveldb");
        st.st_ino = 0;
    }

    LevelDB(const string& name) : owner(true), name(absdbname(name)), fd(-1) {
        debug(name, "leveldb::leveldb::name");
        st.st_ino = 0;
    }

    LevelDB(const LevelDB& c) : owner(false), name(c.name), fd(c.fd) {
        debug("leveldb::leveldb::copy");
        st.st_ino = c.st.st_ino;
    }

    const LevelDB& operator=(const LevelDB& c) {
        debug("leveldb::leveldb::operator=");
        if(this == &c)
            return *this;
        owner = false;
        name = c.name;
        fd = c.fd;
        st.st_ino = c.st.st_ino;
        return *this;
    }

    ~LevelDB() {
        debug("leveldb::~leveldb");
        if (!owner)
            return;
        if (fd == -1)
            return;
        close(fd);
    }

private:
    bool owner;
    string name;
    leveldb::DB* db;
    struct stat st;

    friend const string dbname(const LevelDB& db);
    friend const failable<int> dbopen(LevelDB& db);
    friend const failable<bool> dbclose(LevelDB& db);
};

/**
 * Return the name of the database.
 */
const string dbname(const LevelDB& db) {
    return db.name;
}

/**
 * Open a database.
 */
const failable<int> dbopen(LevelDB& db) {

	// Get database file serial number
	struct stat st;
	const int s = stat(c_str(db.name), &st);
	if (s == -1)
		return mkfailure<int>(string("Couldn't leveldb read database stat: ") + db.name);

	leveldb::DB* ldb;
	leveldb::Options options;
	options.create_if_missing = true;
	leveldb::Status status = leveldb::DB::Open(options, s, &ldb);
	db.db = ldb;
}

/**
 * Close a database.
 */
const failable<bool> dbclose(LevelDB& db) {
	delete db.db;
	db.db = NULL;
    return true;
}


const failable<bool> post(const value& key, const value& val, LevelDB& db) {
    debug(key, "leveldb::post::key");
    debug(val, "leveldb::post::value");
    debug(dbname(db), "leveldb::post::dbname");

    const string ks(scheme::writeValue(key));
    const string vs(scheme::writeValue(val));

    put(ks, vs, db);

    debug(r, "leveldb::post::result");
    return r;
}


const failable<bool> put(const value& key, const value& val, LevelDB& db) {
    debug(key, "leveldb::put::key");
    debug(val, "leveldb::put::value");
    debug(dbname(db), "leveldb::put::dbname");

    const string ks(scheme::writeValue(key));
    const string vs(scheme::writeValue(val));

    debug(r, "leveldb::put::result");
    return r;
}

/**
 * Get an item from the database.
 */
const failable<value> get(const value& key, LevelDB& db) {
    debug(key, "leveldb::get::key");
    debug(dbname(db), "leveldb::get::dbname");

    const string ks(scheme::writeValue(key));

    std::string data;
    db.db->Get(leveldb::ReadOptions(), key, &data);
    const value val(scheme::readValue(string(data)));

    debug(val, "leveldb::get::result");
    return val;
}

/**
 * Delete an item from the database
 */
struct delUpdate {
    const string ks;
    delUpdate(const string& ks) : ks(ks) {
    }
    const failable<bool> operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const {
        if (ks == string((char*)buf, klen))
            return false;
        return true;
    }
};

struct delFinish {
    delFinish() {
    }
    const failable<bool> operator()(unused struct db_make& dbm) const {
        return true;
    }
};

const failable<bool> del(const value& key, LevelDB& db) {
    debug(key, "leveldb::delete::key");
    debug(dbname(db), "leveldb::delete::dbname");

    const string ks(scheme::writeValue(key));

    leveldb::Status s = db.db->Delete(leveldb::WriteOptions(), key);

    debug(r, "leveldb::delete::result");
    return s.ok();
}

}
}

#endif /* tuscany_leveldb_hpp */