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;
    LOG_ERROR("LevelDB write failed: %s", s.ToString().c_str());
    return false;
}
Example #2
0
void HandleError(const leveldb::Status &status) throw(leveldb_error) {
    if (status.ok())
        return;
    if (status.IsCorruption())
        throw leveldb_error("Database corrupted");
    if (status.IsIOError())
        throw leveldb_error("Database I/O error");
    if (status.IsNotFound())
        throw leveldb_error("Database entry missing");
    throw leveldb_error("Unknown database error");
}
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;
}
Example #4
0
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;
}
Example #5
0
void HandleError(const leveldb::Status &status) noexcept(false) {
    if (status.ok())
        return;
    LogPrintf("%s\n", status.ToString());
    if (status.IsCorruption())
        throw leveldb_error("Database corrupted");
    if (status.IsIOError())
        throw leveldb_error("Database I/O error");
    if (status.IsNotFound())
        throw leveldb_error("Database entry missing");
    throw leveldb_error("Unknown database error");
}
Example #6
0
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;
}
Example #7
0
bool LevelDBDatabase::get(const LevelDBSlice& key, Vector<char>& value)
{
    std::string result;
    leveldb::ReadOptions readOptions;
    readOptions.verify_checksums = true; // FIXME: Disable this if the performance impact is too great.

    const leveldb::Status s = m_db->Get(readOptions, makeSlice(key), &result);
    if (s.ok()) {
        value = makeVector(result);
        return true;
    }
    if (s.IsNotFound())
        return false;
    LOG_ERROR("LevelDB get failed: %s", s.ToString().c_str());
    return false;
}
Example #8
0
extern ERL_NIF_TERM make_status_tuple(ErlNifEnv* env, leveldb::Status status){
    const char* type;
    if(status.IsNotFound()){
	type = "not_found";
    }
    else if(status.IsCorruption()){
	type = "corruption";
    }
    else if(status.IsIOError()){
	type = "io_error";
    }
    else{
	type = "unspecified";
    }
    const char* stString = status.ToString().c_str();
    return enif_make_tuple2(env, enif_make_atom(env, "error"),
			    enif_make_atom(env, type));
}
Example #9
0
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()) {
        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();
}
Example #10
0
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);
}
Example #11
0
void LevelDB::checkDbError(leveldb::Status aStatus) throw(DbException) {
	if (aStatus.ok())
		return;

	if (aStatus.IsNotFound())
		return;

	string ret = Text::toUtf8(aStatus.ToString());

#ifdef _WIN32
	if (aStatus.IsCorruption() || aStatus.IsIOError()) {
		if (ret.back() != '.')
			ret += ".";
		ret += " " + STRING_F(DB_ERROR_HINT, STRING(HASHING));
	}
#endif

	throw DbException(ret);
}
Example #12
0
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;
}
Example #13
0
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();
}
Example #14
0
PassOwnPtr<LevelDBDatabase> LevelDBDatabase::open(const String& fileName, const LevelDBComparator* comparator)
{
    OwnPtr<ComparatorAdapter> comparatorAdapter = adoptPtr(new ComparatorAdapter(comparator));

    leveldb::Options options;
    options.comparator = comparatorAdapter.get();
    options.create_if_missing = true;
    options.paranoid_checks = true;
    leveldb::DB* db;
    const leveldb::Status s = leveldb::DB::Open(options, fileName.utf8().data(), &db);

    if (!s.ok()) {
        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();
}
Example #15
0
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);
        histogramFreeSpace("Failure", fileName);

        LOG_ERROR("Failed to open LevelDB database from %s: %s", fileName.ascii().data(), s.ToString().c_str());
        return nullptr;
    }

    histogramFreeSpace("Success", fileName);

    OwnPtr<LevelDBDatabase> result = adoptPtr(new LevelDBDatabase);
    result->m_db = adoptPtr(db);
    result->m_comparatorAdapter = comparatorAdapter.release();
    result->m_comparator = comparator;

    return result.release();
}
Example #16
0
bool KeyValueStorage::process_status(const leveldb::Status & status, bool reopen) {
	if (status.ok()) {
		return true;
	}
	std::lock_guard<Mutex> lock(mutex);
	error = status.ToString();
	if (status.IsCorruption()) {
		if (++repairs > 2)
			return false;
		errorstream << "Trying to repair database [" << db_name << "] try=" << repairs << " [" << error << "]" << std::endl;
		leveldb::Options options;
		options.create_if_missing = true;
		leveldb::Status status_repair;
		try {
			status_repair = leveldb::RepairDB(fullpath, options);
		} catch (std::exception &e) {
			errorstream << "First repair [" << db_name << "] exception [" << e.what() << "]" << std::endl;
			auto options_repair = options;
			options_repair.paranoid_checks = true;
			try {
				status_repair = leveldb::RepairDB(fullpath, options_repair);
			} catch (std::exception &e) {
				errorstream << "Second repair [" << db_name << "] exception [" << e.what() << "]" << std::endl;
			}
		}
		if (!status.ok()) {
			error = status.ToString();
			errorstream << "Repair [" << db_name << "] fail [" << error << "]" << std::endl;
			delete db;
			db = nullptr;
			return false;
		}
		if (reopen) {
			auto status_open = leveldb::DB::Open(options, fullpath, &db);
			if (!status_open.ok()) {
				error = status_open.ToString();
				errorstream << "Trying to reopen database [" << db_name << "] fail [" << error << "]" << std::endl;
				delete db;
				db = nullptr;
				return false;
			}
		}
	}
	return status.ok();
}
Example #17
0
void IteratorImpl::checkStatus()
{
    const leveldb::Status s = m_iterator->status();
    if (!s.ok())
        LOG_ERROR("LevelDB iterator error: %s", s.ToString().c_str());
}
Example #18
0
bool CheckStatus(leveldb::Status& status) {
	if (!status.ok()) {
		std::cerr << status.ToString() << std::endl;
	}
	return status.ok();
}