summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/leveldb
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2011-06-02 12:07:03 +0100
committerBen Murdoch <benm@google.com>2011-06-10 10:47:21 +0100
commit2daae5fd11344eaa88a0d92b0f6d65f8d2255c00 (patch)
treee4964fbd1cb70599f7718ff03e50ea1dab33890b /Source/WebCore/platform/leveldb
parent87bdf0060a247bfbe668342b87e0874182e0ffa9 (diff)
downloadexternal_webkit-2daae5fd11344eaa88a0d92b0f6d65f8d2255c00.zip
external_webkit-2daae5fd11344eaa88a0d92b0f6d65f8d2255c00.tar.gz
external_webkit-2daae5fd11344eaa88a0d92b0f6d65f8d2255c00.tar.bz2
Merge WebKit at r84325: Initial merge by git.
Change-Id: Ic1a909300ecc0a13ddc6b4e784371d2ac6e3d59b
Diffstat (limited to 'Source/WebCore/platform/leveldb')
-rw-r--r--Source/WebCore/platform/leveldb/LevelDBComparator.h48
-rw-r--r--Source/WebCore/platform/leveldb/LevelDBDatabase.cpp159
-rw-r--r--Source/WebCore/platform/leveldb/LevelDBDatabase.h66
-rw-r--r--Source/WebCore/platform/leveldb/LevelDBIterator.cpp94
-rw-r--r--Source/WebCore/platform/leveldb/LevelDBIterator.h65
-rw-r--r--Source/WebCore/platform/leveldb/LevelDBSlice.h67
6 files changed, 499 insertions, 0 deletions
diff --git a/Source/WebCore/platform/leveldb/LevelDBComparator.h b/Source/WebCore/platform/leveldb/LevelDBComparator.h
new file mode 100644
index 0000000..08ac261
--- /dev/null
+++ b/Source/WebCore/platform/leveldb/LevelDBComparator.h
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+#ifndef LevelDBComparator_h
+#define LevelDBComparator_h
+
+#if ENABLE(LEVELDB)
+
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+class LevelDBSlice;
+
+class LevelDBComparator {
+public:
+ virtual ~LevelDBComparator() {}
+
+ virtual int compare(const LevelDBSlice&, const LevelDBSlice&) const = 0;
+ virtual const char* name() const = 0;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(LEVELDB)
+#endif // LevelDBComparator_h
diff --git a/Source/WebCore/platform/leveldb/LevelDBDatabase.cpp b/Source/WebCore/platform/leveldb/LevelDBDatabase.cpp
new file mode 100644
index 0000000..4587631
--- /dev/null
+++ b/Source/WebCore/platform/leveldb/LevelDBDatabase.cpp
@@ -0,0 +1,159 @@
+/*
+ * 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 ENABLE(LEVELDB)
+
+#include "LevelDBComparator.h"
+#include "LevelDBIterator.h"
+#include "LevelDBSlice.h"
+#include <leveldb/comparator.h>
+#include <leveldb/db.h>
+#include <leveldb/slice.h>
+#include <string>
+#include <wtf/PassOwnPtr.h>
+#include <wtf/text/CString.h>
+#include <wtf/text/WTFString.h>
+
+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());
+}
+
+static Vector<char> makeVector(const std::string& s)
+{
+ Vector<char> res;
+ res.append(s.c_str(), s.length());
+ return res;
+}
+
+namespace {
+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;
+};
+}
+
+LevelDBDatabase::LevelDBDatabase()
+ : m_db(0)
+{
+}
+
+LevelDBDatabase::~LevelDBDatabase()
+{
+}
+
+LevelDBDatabase* LevelDBDatabase::open(const String& fileName, const LevelDBComparator* comparator)
+{
+ OwnPtr<ComparatorAdapter> comparatorAdapter(new ComparatorAdapter(comparator));
+
+ LevelDBDatabase* result = new LevelDBDatabase();
+
+ leveldb::Options options;
+ options.comparator = comparatorAdapter.get();
+ options.create_if_missing = true;
+ leveldb::DB* db;
+ leveldb::Status s = leveldb::DB::Open(options, fileName.utf8().data(), &db);
+
+ if (!s.ok()) {
+ delete result;
+ return 0;
+ }
+
+ result->m_db = WTF::adoptPtr(db);
+ result->m_comparatorAdapter = comparatorAdapter.release();
+
+ return result;
+}
+
+
+bool LevelDBDatabase::put(const LevelDBSlice& key, const Vector<char>& value)
+{
+ leveldb::WriteOptions writeOptions;
+ writeOptions.sync = false;
+
+ return m_db->Put(writeOptions, makeSlice(key), makeSlice(value)).ok();
+}
+
+bool LevelDBDatabase::remove(const LevelDBSlice& key)
+{
+ leveldb::WriteOptions writeOptions;
+ writeOptions.sync = false;
+
+ return m_db->Delete(writeOptions, makeSlice(key)).ok();
+}
+
+bool LevelDBDatabase::get(const LevelDBSlice& key, Vector<char>& value)
+{
+ std::string result;
+ if (!m_db->Get(leveldb::ReadOptions(), makeSlice(key), &result).ok())
+ return false;
+
+ value = makeVector(result);
+ return true;
+}
+
+LevelDBIterator* LevelDBDatabase::newIterator()
+{
+ leveldb::Iterator* i = m_db->NewIterator(leveldb::ReadOptions());
+ if (!i) // FIXME: Double check if we actually need to check this.
+ return 0;
+ return new LevelDBIterator(i);
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(LEVELDB)
diff --git a/Source/WebCore/platform/leveldb/LevelDBDatabase.h b/Source/WebCore/platform/leveldb/LevelDBDatabase.h
new file mode 100644
index 0000000..471f335
--- /dev/null
+++ b/Source/WebCore/platform/leveldb/LevelDBDatabase.h
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+#ifndef LevelDBDatabase_h
+#define LevelDBDatabase_h
+
+#if ENABLE(LEVELDB)
+
+#include "PlatformString.h"
+#include <OwnPtr.h>
+#include <Vector.h>
+
+namespace leveldb {
+class Comparator;
+class DB;
+}
+
+namespace WebCore {
+
+class LevelDBComparator;
+class LevelDBIterator;
+class LevelDBSlice;
+
+class LevelDBDatabase {
+public:
+ static LevelDBDatabase* open(const String& fileName, const LevelDBComparator*);
+ ~LevelDBDatabase();
+
+ bool put(const LevelDBSlice& key, const Vector<char>& value);
+ bool remove(const LevelDBSlice& key);
+ bool get(const LevelDBSlice& key, Vector<char>& value);
+ LevelDBIterator* newIterator();
+
+private:
+ LevelDBDatabase();
+
+ OwnPtr<leveldb::DB> m_db;
+ OwnPtr<leveldb::Comparator> m_comparatorAdapter;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(LEVELDB)
+#endif // LevelDBDatabase_h
diff --git a/Source/WebCore/platform/leveldb/LevelDBIterator.cpp b/Source/WebCore/platform/leveldb/LevelDBIterator.cpp
new file mode 100644
index 0000000..de0f286
--- /dev/null
+++ b/Source/WebCore/platform/leveldb/LevelDBIterator.cpp
@@ -0,0 +1,94 @@
+/*
+ * 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 "LevelDBIterator.h"
+
+#if ENABLE(LEVELDB)
+
+#include <leveldb/iterator.h>
+#include <leveldb/slice.h>
+#include <wtf/text/CString.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+LevelDBIterator::~LevelDBIterator()
+{
+}
+
+LevelDBIterator::LevelDBIterator(leveldb::Iterator* it)
+ : m_iterator(it)
+{
+}
+
+static leveldb::Slice makeSlice(const Vector<char>& value)
+{
+ return leveldb::Slice(value.data(), value.size());
+}
+
+static LevelDBSlice makeLevelDBSlice(leveldb::Slice s)
+{
+ return LevelDBSlice(s.data(), s.data() + s.size());
+}
+
+bool LevelDBIterator::isValid() const
+{
+ return m_iterator->Valid();
+}
+
+void LevelDBIterator::seekToLast()
+{
+ m_iterator->SeekToLast();
+}
+
+void LevelDBIterator::seek(const Vector<char>& target)
+{
+ m_iterator->Seek(makeSlice(target));
+}
+
+void LevelDBIterator::next()
+{
+ m_iterator->Next();
+}
+
+void LevelDBIterator::prev()
+{
+ m_iterator->Prev();
+}
+
+LevelDBSlice LevelDBIterator::key() const
+{
+ return makeLevelDBSlice(m_iterator->key());
+}
+
+LevelDBSlice LevelDBIterator::value() const
+{
+ return makeLevelDBSlice(m_iterator->value());
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(LEVELDB)
diff --git a/Source/WebCore/platform/leveldb/LevelDBIterator.h b/Source/WebCore/platform/leveldb/LevelDBIterator.h
new file mode 100644
index 0000000..ff9cbca
--- /dev/null
+++ b/Source/WebCore/platform/leveldb/LevelDBIterator.h
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+#ifndef LevelDBIterator_h
+#define LevelDBIterator_h
+
+#if ENABLE(LEVELDB)
+
+#include "LevelDBSlice.h"
+#include "PlatformString.h"
+#include <OwnPtr.h>
+#include <Vector.h>
+
+namespace leveldb {
+class Iterator;
+}
+
+namespace WebCore {
+
+class LevelDBIterator {
+public:
+ ~LevelDBIterator();
+
+ bool isValid() const;
+ void seekToLast();
+ void seek(const Vector<char>& target);
+ void next();
+ void prev();
+ LevelDBSlice key() const;
+ LevelDBSlice value() const;
+
+private:
+ LevelDBIterator(leveldb::Iterator*);
+ friend class LevelDBDatabase;
+
+ OwnPtr<leveldb::Iterator> m_iterator;
+};
+
+
+} // namespace WebCore
+
+#endif // ENABLE(LEVELDB)
+#endif // LevelDBIterator_h
diff --git a/Source/WebCore/platform/leveldb/LevelDBSlice.h b/Source/WebCore/platform/leveldb/LevelDBSlice.h
new file mode 100644
index 0000000..15c576c
--- /dev/null
+++ b/Source/WebCore/platform/leveldb/LevelDBSlice.h
@@ -0,0 +1,67 @@
+/*
+ * 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.
+ */
+
+#ifndef LevelDBSlice_h
+#define LevelDBSlice_h
+
+#if ENABLE(LEVELDB)
+
+#include "PlatformString.h"
+#include <Vector.h>
+
+namespace WebCore {
+
+class LevelDBSlice {
+public:
+ LevelDBSlice(const char* begin, const char* end)
+ : m_begin(begin)
+ , m_end(end)
+ {
+ ASSERT(m_end >= m_begin);
+ }
+
+ LevelDBSlice(const Vector<char>& v)
+ : m_begin(v.data())
+ , m_end(m_begin + v.size())
+ {
+ ASSERT(m_end >= m_begin);
+ }
+
+ ~LevelDBSlice()
+ {
+ }
+
+ const char* begin() const { return m_begin; }
+ const char* end() const { return m_end; }
+
+private:
+ const char* m_begin;
+ const char* m_end;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(LEVELDB)
+#endif // LevelDBSlice_h