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
|
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "LevelDBDatabase.h"
#if USE(LEVELDB)
#include "HistogramSupport.h"
#include "LevelDBComparator.h"
#include "LevelDBIterator.h"
#include "LevelDBSlice.h"
#include "LevelDBWriteBatch.h"
#include "Logging.h"
#include "NotImplemented.h"
#include <leveldb/comparator.h>
#include <leveldb/db.h>
#include <leveldb/env.h>
#include <helpers/memenv/memenv.h>
#include <leveldb/slice.h>
#include <string>
#include <wtf/PassOwnPtr.h>
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
namespace leveldb {
static Env* IDBEnv()
{
return leveldb::Env::Default();
}
}
namespace WebCore {
static leveldb::Slice makeSlice(const Vector<char>& value)
{
return leveldb::Slice(value.data(), value.size());
}
static leveldb::Slice makeSlice(const LevelDBSlice& s)
{
return leveldb::Slice(s.begin(), s.end() - s.begin());
}
static LevelDBSlice makeLevelDBSlice(const leveldb::Slice& s)
{
return LevelDBSlice(s.data(), s.data() + s.size());
}
class ComparatorAdapter : public leveldb::Comparator {
public:
ComparatorAdapter(const LevelDBComparator* comparator)
: m_comparator(comparator)
{
}
virtual int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const
{
return m_comparator->compare(makeLevelDBSlice(a), makeLevelDBSlice(b));
}
virtual const char* Name() const { return m_comparator->name(); }
// FIXME: Support the methods below in the future.
virtual void FindShortestSeparator(std::string* /* start */, const leveldb::Slice& /* limit */) const { }
virtual void FindShortSuccessor(std::string* /* key */) const { }
private:
const LevelDBComparator* m_comparator;
};
LevelDBSnapshot::LevelDBSnapshot(LevelDBDatabase* db)
: m_db(db->m_db.get())
, m_snapshot(m_db->GetSnapshot())
{
}
LevelDBSnapshot::~LevelDBSnapshot()
{
m_db->ReleaseSnapshot(m_snapshot);
}
LevelDBDatabase::LevelDBDatabase()
{
}
LevelDBDatabase::~LevelDBDatabase()
{
// m_db's destructor uses m_comparatorAdapter; order of deletion is important.
m_db.clear();
m_comparatorAdapter.clear();
m_env.clear();
}
static leveldb::Status openDB(leveldb::Comparator* comparator, leveldb::Env* env, const String& path, leveldb::DB** db)
{
leveldb::Options options;
options.comparator = comparator;
options.create_if_missing = true;
options.paranoid_checks = true;
// 20 max_open_files is the minimum LevelDB allows.
options.max_open_files = 20;
options.env = env;
return leveldb::DB::Open(options, path.utf8().data(), db);
}
bool LevelDBDatabase::destroy(const String& fileName)
{
leveldb::Options options;
options.env = leveldb::IDBEnv();
const leveldb::Status s = leveldb::DestroyDB(fileName.utf8().data(), options);
return s.ok();
}
static void histogramLevelDBError(const char* histogramName, const leveldb::Status& s)
{
ASSERT(!s.ok());
enum {
LevelDBNotFound,
LevelDBCorruption,
LevelDBIOError,
LevelDBOther,
LevelDBMaxError
};
int levelDBError = LevelDBOther;
if (s.IsNotFound())
levelDBError = LevelDBNotFound;
else if (s.IsCorruption())
levelDBError = LevelDBCorruption;
else if (s.IsIOError())
levelDBError = LevelDBIOError;
HistogramSupport::histogramEnumeration(histogramName, levelDBError, LevelDBMaxError);
}
PassOwnPtr<LevelDBDatabase> LevelDBDatabase::open(const String& fileName, const LevelDBComparator* comparator)
{
OwnPtr<ComparatorAdapter> comparatorAdapter = adoptPtr(new ComparatorAdapter(comparator));
leveldb::DB* db;
const leveldb::Status s = openDB(comparatorAdapter.get(), leveldb::IDBEnv(), fileName, &db);
if (!s.ok()) {
histogramLevelDBError("WebCore.IndexedDB.LevelDBOpenErrors", s);
LOG_ERROR("Failed to open LevelDB database from %s: %s", fileName.ascii().data(), s.ToString().c_str());
return nullptr;
}
OwnPtr<LevelDBDatabase> result = adoptPtr(new LevelDBDatabase);
result->m_db = adoptPtr(db);
result->m_comparatorAdapter = comparatorAdapter.release();
result->m_comparator = comparator;
return result.release();
}
PassOwnPtr<LevelDBDatabase> LevelDBDatabase::openInMemory(const LevelDBComparator* comparator)
{
OwnPtr<ComparatorAdapter> comparatorAdapter = adoptPtr(new ComparatorAdapter(comparator));
OwnPtr<leveldb::Env> inMemoryEnv = adoptPtr(leveldb::NewMemEnv(leveldb::IDBEnv()));
leveldb::DB* db;
const leveldb::Status s = openDB(comparatorAdapter.get(), inMemoryEnv.get(), String(), &db);
if (!s.ok()) {
LOG_ERROR("Failed to open in-memory LevelDB database: %s", s.ToString().c_str());
return nullptr;
}
OwnPtr<LevelDBDatabase> result = adoptPtr(new LevelDBDatabase);
result->m_env = inMemoryEnv.release();
result->m_db = adoptPtr(db);
result->m_comparatorAdapter = comparatorAdapter.release();
result->m_comparator = comparator;
return result.release();
}
bool LevelDBDatabase::put(const LevelDBSlice& key, const Vector<char>& value)
{
leveldb::WriteOptions writeOptions;
writeOptions.sync = true;
const leveldb::Status s = m_db->Put(writeOptions, makeSlice(key), makeSlice(value));
if (s.ok())
return true;
LOG_ERROR("LevelDB put failed: %s", s.ToString().c_str());
return false;
}
bool LevelDBDatabase::remove(const LevelDBSlice& key)
{
leveldb::WriteOptions writeOptions;
writeOptions.sync = true;
const leveldb::Status s = m_db->Delete(writeOptions, makeSlice(key));
if (s.ok())
return true;
if (s.IsNotFound())
return false;
LOG_ERROR("LevelDB remove failed: %s", s.ToString().c_str());
return false;
}
bool LevelDBDatabase::safeGet(const LevelDBSlice& key, Vector<char>& value, bool& found, const LevelDBSnapshot* snapshot)
{
found = false;
std::string result;
leveldb::ReadOptions readOptions;
readOptions.verify_checksums = true; // FIXME: Disable this if the performance impact is too great.
readOptions.snapshot = snapshot ? snapshot->m_snapshot : 0;
const leveldb::Status s = m_db->Get(readOptions, makeSlice(key), &result);
if (s.ok()) {
found = true;
value.clear();
value.append(result.c_str(), result.length());
return true;
}
if (s.IsNotFound())
return true;
LOG_ERROR("LevelDB get failed: %s", s.ToString().c_str());
return false;
}
bool LevelDBDatabase::write(LevelDBWriteBatch& writeBatch)
{
leveldb::WriteOptions writeOptions;
writeOptions.sync = true;
const leveldb::Status s = m_db->Write(writeOptions, writeBatch.m_writeBatch.get());
if (s.ok())
return true;
histogramLevelDBError("WebCore.IndexedDB.LevelDBWriteErrors", s);
LOG_ERROR("LevelDB write failed: %s", s.ToString().c_str());
return false;
}
namespace {
class IteratorImpl : public LevelDBIterator {
public:
~IteratorImpl() { };
virtual bool isValid() const;
virtual void seekToLast();
virtual void seek(const LevelDBSlice& target);
virtual void next();
virtual void prev();
virtual LevelDBSlice key() const;
virtual LevelDBSlice value() const;
private:
friend class WebCore::LevelDBDatabase;
IteratorImpl(PassOwnPtr<leveldb::Iterator>);
void checkStatus();
OwnPtr<leveldb::Iterator> m_iterator;
};
}
IteratorImpl::IteratorImpl(PassOwnPtr<leveldb::Iterator> it)
: m_iterator(it)
{
}
void IteratorImpl::checkStatus()
{
const leveldb::Status s = m_iterator->status();
if (!s.ok())
LOG_ERROR("LevelDB iterator error: %s", s.ToString().c_str());
}
bool IteratorImpl::isValid() const
{
return m_iterator->Valid();
}
void IteratorImpl::seekToLast()
{
m_iterator->SeekToLast();
checkStatus();
}
void IteratorImpl::seek(const LevelDBSlice& target)
{
m_iterator->Seek(makeSlice(target));
checkStatus();
}
void IteratorImpl::next()
{
ASSERT(isValid());
m_iterator->Next();
checkStatus();
}
void IteratorImpl::prev()
{
ASSERT(isValid());
m_iterator->Prev();
checkStatus();
}
LevelDBSlice IteratorImpl::key() const
{
ASSERT(isValid());
return makeLevelDBSlice(m_iterator->key());
}
LevelDBSlice IteratorImpl::value() const
{
ASSERT(isValid());
return makeLevelDBSlice(m_iterator->value());
}
PassOwnPtr<LevelDBIterator> LevelDBDatabase::createIterator(const LevelDBSnapshot* snapshot)
{
leveldb::ReadOptions readOptions;
readOptions.verify_checksums = true; // FIXME: Disable this if the performance impact is too great.
readOptions.snapshot = snapshot ? snapshot->m_snapshot : 0;
OwnPtr<leveldb::Iterator> i = adoptPtr(m_db->NewIterator(readOptions));
if (!i) // FIXME: Double check if we actually need to check this.
return nullptr;
return adoptPtr(new IteratorImpl(i.release()));
}
const LevelDBComparator* LevelDBDatabase::comparator() const
{
return m_comparator;
}
}
#endif
|