├── .gitignore
├── Compile-Nuget.ps1
├── LICENSE.txt
├── LevelDBUAP.nuspec
├── LevelDBUAP.props
├── LevelDBUAP.targets
├── LevelDBWinRT.Shared
├── AUTHORS
├── DB.cpp
├── DB.h
├── KeyComparator.h
├── LICENSE
├── LevelDB-Commons.h
├── LevelDB-Iterator.h
├── LevelDBWinRT.Shared.vcxitems
├── LevelDBWinRT.Shared.vcxitems.filters
├── Slice.cpp
├── Slice.h
├── Snappy.h
├── Utils.cpp
├── Utils.h
├── WriteBatch.cpp
├── WriteBatch.h
├── db
│ ├── autocompact_test.cc
│ ├── builder.cc
│ ├── builder.h
│ ├── c.cc
│ ├── c_test.c
│ ├── corruption_test.cc
│ ├── db_bench.cc
│ ├── db_impl.cc
│ ├── db_impl.h
│ ├── db_iter.cc
│ ├── db_iter.h
│ ├── db_test.cc
│ ├── dbformat.cc
│ ├── dbformat.h
│ ├── dbformat_test.cc
│ ├── dumpfile.cc
│ ├── filename.cc
│ ├── filename.h
│ ├── filename_test.cc
│ ├── leveldb_main.cc
│ ├── log_format.h
│ ├── log_reader.cc
│ ├── log_reader.h
│ ├── log_test.cc
│ ├── log_writer.cc
│ ├── log_writer.h
│ ├── memtable.cc
│ ├── memtable.h
│ ├── repair.cc
│ ├── skiplist.h
│ ├── skiplist_test.cc
│ ├── snapshot.h
│ ├── table_cache.cc
│ ├── table_cache.h
│ ├── version_edit.cc
│ ├── version_edit.h
│ ├── version_edit_test.cc
│ ├── version_set.cc
│ ├── version_set.h
│ ├── version_set_test.cc
│ ├── write_batch.cc
│ ├── write_batch_internal.h
│ └── write_batch_test.cc
├── doc
│ ├── bench
│ │ ├── db_bench_sqlite3.cc
│ │ └── db_bench_tree_db.cc
│ ├── benchmark.html
│ ├── doc.css
│ ├── impl.html
│ ├── index.html
│ ├── log_format.txt
│ └── table_format.txt
├── helpers
│ └── memenv
│ │ ├── memenv.cc
│ │ ├── memenv.h
│ │ └── memenv_test.cc
├── leveldb
│ ├── c.h
│ ├── cache.h
│ ├── comparator.h
│ ├── db.h
│ ├── dumpfile.h
│ ├── env.h
│ ├── filter_policy.h
│ ├── iterator.h
│ ├── options.h
│ ├── slice.h
│ ├── status.h
│ ├── table.h
│ ├── table_builder.h
│ └── write_batch.h
├── port
│ ├── README
│ ├── atomic_pointer.h
│ ├── port.h
│ ├── port_example.h
│ ├── port_posix.cc
│ ├── port_posix.h
│ ├── port_win.cc
│ ├── port_win.h
│ ├── thread_annotations.h
│ └── win
│ │ └── stdint.h
├── snappy
│ ├── snappy-c.cc
│ ├── snappy-c.h
│ ├── snappy-internal.h
│ ├── snappy-sinksource.cc
│ ├── snappy-sinksource.h
│ ├── snappy-stubs-internal.cc
│ ├── snappy-stubs-internal.h
│ ├── snappy-stubs-public.h
│ ├── snappy.cc
│ └── snappy.h
├── table
│ ├── block.cc
│ ├── block.h
│ ├── block_builder.cc
│ ├── block_builder.h
│ ├── filter_block.cc
│ ├── filter_block.h
│ ├── filter_block_test.cc
│ ├── format.cc
│ ├── format.h
│ ├── iterator.cc
│ ├── iterator_wrapper.h
│ ├── merger.cc
│ ├── merger.h
│ ├── table.cc
│ ├── table_builder.cc
│ ├── table_test.cc
│ ├── two_level_iterator.cc
│ └── two_level_iterator.h
└── util
│ ├── arena.cc
│ ├── arena.h
│ ├── arena_test.cc
│ ├── bloom.cc
│ ├── bloom_test.cc
│ ├── cache.cc
│ ├── cache_test.cc
│ ├── coding.cc
│ ├── coding.h
│ ├── coding_test.cc
│ ├── comparator.cc
│ ├── crc32c.cc
│ ├── crc32c.h
│ ├── crc32c_test.cc
│ ├── env.cc
│ ├── env_posix.cc
│ ├── env_test.cc
│ ├── env_windows.cc
│ ├── filter_policy.cc
│ ├── hash.cc
│ ├── hash.h
│ ├── hash_test.cc
│ ├── histogram.cc
│ ├── histogram.h
│ ├── logging.cc
│ ├── logging.h
│ ├── mutexlock.h
│ ├── options.cc
│ ├── posix_logger.h
│ ├── random.h
│ ├── status.cc
│ ├── testharness.cc
│ ├── testharness.h
│ ├── testutil.cc
│ ├── testutil.h
│ ├── win_logger.cc
│ └── win_logger.h
├── LevelDBWinRT.Tests
├── Assets
│ ├── LockScreenLogo.scale-200.png
│ ├── SplashScreen.scale-200.png
│ ├── Square150x150Logo.scale-200.png
│ ├── Square44x44Logo.scale-200.png
│ ├── Square44x44Logo.targetsize-24_altform-unplated.png
│ ├── StoreLogo.png
│ └── Wide310x150Logo.scale-200.png
├── BasicTests.cs
├── LevelDBWinRT.Tests.csproj
├── MultiThreadedTests.cs
├── Package.appxmanifest
├── Properties
│ ├── AssemblyInfo.cs
│ └── UnitTestApp.rd.xml
├── UnitTestApp.xaml
├── UnitTestApp.xaml.cs
└── project.json
├── LevelDBWinRT.UAP
├── LevelDBWinRT.UAP.vcxproj
└── LevelDBWinRT.UAP.vcxproj.filters
├── LevelDBWinRT.sln
└── README.md
/Compile-Nuget.ps1:
--------------------------------------------------------------------------------
1 | echo "Creating directory structure for nuget"
2 | New-Item -Path nuget\lib\uap10.0 -ItemType Directory -Force
3 | New-Item -Path nuget\build\native -ItemType Directory -Force
4 |
5 | New-Item -Path nuget\runtimes\win10-arm\native -ItemType Directory -Force
6 | New-Item -Path nuget\runtimes\win10-x86\native -ItemType Directory -Force
7 | New-Item -Path nuget\runtimes\win10-x64\native -ItemType Directory -Force
8 |
9 | echo "Copying over files..."
10 |
11 | Copy-Item -Force Release\x86\*.winmd nuget\lib\uap10.0
12 | Copy-Item -Force LevelDBUAP.targets nuget\build\native
13 |
14 | Copy-Item -Force Release\ARM\*.dll nuget\runtimes\win10-arm\native
15 | Copy-Item -Force Release\x64\*.dll nuget\runtimes\win10-x64\native
16 | Copy-Item -Force Release\x86\*.dll nuget\runtimes\win10-x86\native
17 |
18 | Copy-Item -Force LevelDBUAP.nuspec nuget\
19 |
20 | Push-Location
21 | cd nuget
22 | nuget pack .\LevelDBUAP.nuspec
23 | Pop-Location
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2015, Zohaib Sibte Hassan
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions are met:
6 | * Redistributions of source code must retain the above copyright
7 | notice, this list of conditions and the following disclaimer.
8 | * Redistributions in binary form must reproduce the above copyright
9 | notice, this list of conditions and the following disclaimer in the
10 | documentation and/or other materials provided with the distribution.
11 | * Neither the name of the Zohaib Sibte Hassan nor the
12 | names of its contributors may be used to endorse or promote products
13 | derived from this software without specific prior written permission.
14 |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 | DISCLAIMED. IN NO EVENT SHALL ZOHAIB SIBTE HASSAN BE LIABLE FOR ANY
19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
/LevelDBUAP.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | LevelDB.UWP
5 | 1.18.2
6 | LevelDB for UWP
7 | Zohaib Sibte Hassan
8 | Zohaib Sibte Hassan
9 | http://www.opensource.org/licenses/mit-license.php
10 | https://github.com/maxpert/LevelDBWinRT/
11 | true
12 |
13 |
14 |
15 | - Adding DB.Recover to recover broken databases
16 |
17 | Copyright 2016
18 | leveldb,uwp,nosql,leveldbuap,leveldbuwp,uap,database,keyvalue,key-value,store,
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/LevelDBUAP.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LevelDBUAP.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
10 |
11 |
12 | LevelDBWinRT.dll
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/AUTHORS:
--------------------------------------------------------------------------------
1 | # Names should be added to this file like so:
2 | # Name or Organization
3 |
4 | Google Inc.
5 |
6 | # Initial version authors:
7 | Jeffrey Dean
8 | Sanjay Ghemawat
9 |
10 | # Partial list of contributors:
11 | Kevin Regan
12 | Johan Bilien
13 | Erik Zhang
14 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/DB.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #pragma once
6 |
7 | #include "leveldb\db.h"
8 | #include "Slice.h"
9 | #include "LevelDB-Iterator.h"
10 | #include "LevelDB-Commons.h"
11 | #include "WriteBatch.h"
12 | #include
13 |
14 | namespace LevelDBWinRT {
15 | using namespace Platform;
16 |
17 | public ref class DB sealed
18 | {
19 | private:
20 | const leveldb::Comparator* comparator;
21 |
22 | leveldb::DB* db;
23 | leveldb::Options openOptions;
24 | public:
25 | static bool Repair(Options ^options, String ^path);
26 |
27 | DB(Options^ options, String^ path);
28 | virtual ~DB();
29 |
30 | bool Put(WriteOptions^ writeOptions, Slice^ key, Slice^ value);
31 | bool Delete(WriteOptions^ writeOptions, Slice^ key);
32 | Slice^ Get(ReadOptions^ readOptions, Slice^ key);
33 | bool Write(WriteOptions^ writeOptions, WriteBatch^ batch);
34 |
35 | Iterator^ NewIterator(ReadOptions ^readOptions);
36 | Snapshot^ GetSnapshot();
37 | };
38 | }
39 |
40 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/KeyComparator.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #pragma once
6 |
7 | #include "leveldb\db.h"
8 | #include "Slice.h"
9 | #include
10 |
11 | using namespace Platform;
12 |
13 | namespace LevelDBWinRT {
14 |
15 | public interface class IKeyComparator {
16 | public:
17 | int Compare(Slice^ a, Slice^ b);
18 | property String^ Name {
19 | String^ get();
20 | };
21 | };
22 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 |
3 | Redistribution and use in source and binary forms, with or without
4 | modification, are permitted provided that the following conditions are
5 | met:
6 |
7 | * Redistributions of source code must retain the above copyright
8 | notice, this list of conditions and the following disclaimer.
9 | * Redistributions in binary form must reproduce the above
10 | copyright notice, this list of conditions and the following disclaimer
11 | in the documentation and/or other materials provided with the
12 | distribution.
13 | * Neither the name of Google Inc. nor the names of its
14 | contributors may be used to endorse or promote products derived from
15 | this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/LevelDB-Iterator.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #pragma once
6 |
7 | #include "Slice.h"
8 | #include "leveldb\db.h"
9 |
10 | namespace LevelDBWinRT {
11 | public ref class Iterator sealed
12 | {
13 | private:
14 | leveldb::Iterator* iterator;
15 | internal:
16 | Iterator(leveldb::Iterator *i) {
17 | this->iterator = i;
18 | }
19 |
20 | public:
21 | Iterator() {
22 | this->iterator = leveldb::NewEmptyIterator();
23 | }
24 |
25 | virtual ~Iterator() {
26 | delete this->iterator;
27 | }
28 |
29 | Slice^ Key() {
30 | return ref new Slice(this->iterator->key());
31 | }
32 |
33 | void Next() {
34 | this->iterator->Next();
35 | }
36 |
37 | void Prev() {
38 | this->iterator->Prev();
39 | }
40 |
41 | void Seek(Slice^ target) {
42 | this->iterator->Seek(target->ToLevelDBSlice());
43 | }
44 |
45 | void SeekToFirst() {
46 | this->iterator->SeekToFirst();
47 | }
48 |
49 | void SeekToLast() {
50 | this->iterator->SeekToLast();
51 | }
52 |
53 | bool Valid() {
54 | return this->iterator->Valid();
55 | }
56 |
57 | Slice^ Value() {
58 | return ref new Slice(this->iterator->value());
59 | }
60 | };
61 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/Slice.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "Slice.h"
6 | #include "port\port.h"
7 |
8 | using namespace Platform;
9 | using namespace Platform::Runtime::InteropServices;
10 |
11 | namespace LevelDBWinRT {
12 | Slice::Slice(const leveldb::Slice& slice) {
13 | this->buffer = ref new Array((int)slice.size());
14 | memcpy_s(this->buffer->Data, this->buffer->Length, slice.data(), this->buffer->Length);
15 | }
16 |
17 | Slice::Slice(const Array^ m) {
18 | this->buffer = ref new Array(m->Length);
19 | memcpy_s(this->buffer->Data, this->buffer->Length, m->Data, this->buffer->Length);
20 | }
21 |
22 | Slice::Slice(Slice^ obj) {
23 | this->buffer = obj->buffer;
24 | }
25 |
26 | Slice::Slice(String^ str) {
27 | std::wstring ws(str->Begin());
28 | this->buffer = ref new Array(ws.length() * sizeof(wchar_t));
29 | memcpy_s(this->buffer->Data, this->buffer->Length, ws.c_str(), this->buffer->Length);
30 | }
31 |
32 | leveldb::Slice Slice::ToLevelDBSlice() const {
33 | leveldb::Slice ret((const char *)this->buffer->Data, this->buffer->Length);
34 | return ret;
35 | }
36 |
37 | Array^ Slice::ToByteArray() {
38 | return this->ToByteArray(false);
39 | }
40 |
41 | Array^ Slice::ToByteArray(bool deepCopy) {
42 | if (!deepCopy)
43 | {
44 | return this->buffer;
45 | }
46 |
47 | Array^ c = ref new Array(this->buffer->Length);
48 | memcpy_s(c->Data, c->Length, this->buffer->Data, c->Length);
49 | return c;
50 | }
51 |
52 | String^ Slice::ToString() {
53 | Array^ c = ref new Array(this->buffer->Length);
54 | memcpy_s(c->Data, c->Length, this->buffer->Data, c->Length);
55 | return ref new String(c->Data);
56 | }
57 |
58 | Slice^ Slice::FromString(String^ s) {
59 | return ref new Slice(s);
60 | }
61 |
62 | Slice^ Slice::FromByteArray(const Array^ b) {
63 | return ref new Slice(b);
64 | }
65 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/Slice.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #pragma once
6 |
7 | #include "leveldb\slice.h"
8 | #include
9 |
10 | using namespace Platform;
11 | using namespace Platform::Runtime;
12 |
13 | namespace LevelDBWinRT {
14 | public ref class Slice sealed : public Platform::Object {
15 | internal:
16 | Array^ buffer;
17 | internal:
18 | Slice(const leveldb::Slice& slice);
19 | Slice(const Array^ bytes);
20 | Slice(String^ str);
21 |
22 | leveldb::Slice ToLevelDBSlice() const;
23 |
24 | public:
25 | Slice(Slice^ obj);
26 | Array^ ToByteArray(bool deepCopy);
27 | Array^ ToByteArray();
28 | virtual String^ ToString() override;
29 |
30 | static Slice^ FromString(String^ s);
31 | static Slice^ FromByteArray(const Array^ b);
32 | };
33 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/Snappy.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #pragma once
6 | #include "snappy\snappy.h"
7 | #include
8 |
9 | using namespace Platform;
10 | using namespace Platform::Runtime;
11 |
12 | namespace LevelDBWinRT {
13 | namespace Snappy {
14 | public ref class Snappy sealed {
15 | public:
16 | static Array^ Compress(const Array^ data) {
17 | char *compressed_out = new char[snappy::MaxCompressedLength(data->Length)];
18 | size_t compressed_len = 0;
19 |
20 | snappy::RawCompress((const char *)data->Data, data->Length, compressed_out, &compressed_len);
21 | auto ret = ref new Array(compressed_len);
22 | if (ret == nullptr)
23 | {
24 | return nullptr;
25 | }
26 |
27 | memcpy(ret->Data, compressed_out, compressed_len);
28 | delete[] compressed_out;
29 | return ret;
30 | }
31 |
32 | static Array^ Uncompress(const Array ^data) {
33 | size_t uncompressed_length = 0;
34 | if (!snappy::GetUncompressedLength((const char*)data->Data, data->Length, &uncompressed_length)) {
35 | return nullptr;
36 | }
37 |
38 | char *uncompressed = new char[uncompressed_length];
39 | if (!snappy::RawUncompress((const char *)data->Data, data->Length, uncompressed)) {
40 | delete[] uncompressed;
41 | return nullptr;
42 | }
43 |
44 | auto ret = ref new Array(uncompressed_length);
45 | if (ret == nullptr)
46 | {
47 | return nullptr;
48 | }
49 |
50 | memcpy(ret->Data, uncompressed, uncompressed_length);
51 | delete[] uncompressed;
52 | return ret;
53 | }
54 | };
55 | }
56 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/Utils.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "Utils.h"
6 | #include "port\port.h"
7 |
8 | namespace LevelDBWinRT {
9 | std::string Utils::FromPlatformString(String^ str) {
10 | std::wstring ws(str->Begin());
11 | return leveldb::port::ws2s(ws);
12 | }
13 |
14 | String^ Utils::ToPlatformString(std::string str) {
15 | return ref new String(leveldb::port::s2ws(str).c_str());
16 | }
17 |
18 | COMException^ Utils::ExceptionFromStatus(int errorCode, leveldb::Status& status) {
19 | std::wstring err = leveldb::port::s2ws(status.ToString());
20 | String^ error = ref new String(err.c_str());
21 | return ref new COMException(errorCode, error);
22 | }
23 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/Utils.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #pragma once
6 |
7 | #include
8 | #include "leveldb\status.h"
9 |
10 | namespace LevelDBWinRT {
11 | using namespace Platform;
12 |
13 | ref class Utils sealed {
14 | internal:
15 | static std::string FromPlatformString(String^ str);
16 | static String^ ToPlatformString(std::string str);
17 |
18 | static COMException^ ExceptionFromStatus(int errorCode, leveldb::Status& status);
19 | };
20 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/WriteBatch.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "WriteBatch.h"
6 |
7 | namespace LevelDBWinRT {
8 | WriteBatch::WriteBatch() {
9 | this->batch = new leveldb::WriteBatch();
10 | }
11 |
12 | WriteBatch::~WriteBatch() {
13 | delete this->batch;
14 | }
15 |
16 | void WriteBatch::Put(Slice^ key, Slice^ value) {
17 | if (key == nullptr || value == nullptr) {
18 | throw ref new InvalidArgumentException(L"key or value must not be null");
19 | }
20 |
21 | this->batch->Put(key->ToLevelDBSlice(), value->ToLevelDBSlice());
22 | }
23 |
24 | void WriteBatch::Delete(Slice^ key) {
25 | if (key == nullptr) {
26 | throw ref new InvalidArgumentException(L"key must not be null");
27 | }
28 |
29 | this->batch->Delete(key->ToLevelDBSlice());
30 | }
31 |
32 | void WriteBatch::Clear() {
33 | this->batch->Clear();
34 | }
35 |
36 | leveldb::WriteBatch* WriteBatch::ToLevelDBWriteBatch() {
37 | return this->batch;
38 | }
39 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/WriteBatch.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015 Zohaib Sibte Hassan. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #pragma once
6 |
7 | #include "Slice.h"
8 | #include "leveldb\write_batch.h"
9 |
10 | namespace LevelDBWinRT {
11 | public ref class WriteBatch sealed {
12 | private:
13 | leveldb::WriteBatch* batch;
14 |
15 | internal:
16 | leveldb::WriteBatch* ToLevelDBWriteBatch();
17 |
18 | public:
19 | WriteBatch();
20 | virtual ~WriteBatch();
21 |
22 | void Put(Slice^ key, Slice^ value);
23 | void Delete(Slice^ key);
24 | void Clear();
25 | };
26 | }
27 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/autocompact_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2013 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/db.h"
6 | #include "db/db_impl.h"
7 | #include "leveldb/cache.h"
8 | #include "util/testharness.h"
9 | #include "util/testutil.h"
10 |
11 | namespace leveldb {
12 |
13 | class AutoCompactTest {
14 | public:
15 | std::string dbname_;
16 | Cache* tiny_cache_;
17 | Options options_;
18 | DB* db_;
19 |
20 | AutoCompactTest() {
21 | dbname_ = test::TmpDir() + "/autocompact_test";
22 | tiny_cache_ = NewLRUCache(100);
23 | options_.block_cache = tiny_cache_;
24 | DestroyDB(dbname_, options_);
25 | options_.create_if_missing = true;
26 | options_.compression = kNoCompression;
27 | ASSERT_OK(DB::Open(options_, dbname_, &db_));
28 | }
29 |
30 | ~AutoCompactTest() {
31 | delete db_;
32 | DestroyDB(dbname_, Options());
33 | delete tiny_cache_;
34 | }
35 |
36 | std::string Key(int i) {
37 | char buf[100];
38 | snprintf(buf, sizeof(buf), "key%06d", i);
39 | return std::string(buf);
40 | }
41 |
42 | uint64_t Size(const Slice& start, const Slice& limit) {
43 | Range r(start, limit);
44 | uint64_t size;
45 | db_->GetApproximateSizes(&r, 1, &size);
46 | return size;
47 | }
48 |
49 | void DoReads(int n);
50 | };
51 |
52 | static const int kValueSize = 200 * 1024;
53 | static const int kTotalSize = 100 * 1024 * 1024;
54 | static const int kCount = kTotalSize / kValueSize;
55 |
56 | // Read through the first n keys repeatedly and check that they get
57 | // compacted (verified by checking the size of the key space).
58 | void AutoCompactTest::DoReads(int n) {
59 | std::string value(kValueSize, 'x');
60 | DBImpl* dbi = reinterpret_cast(db_);
61 |
62 | // Fill database
63 | for (int i = 0; i < kCount; i++) {
64 | ASSERT_OK(db_->Put(WriteOptions(), Key(i), value));
65 | }
66 | ASSERT_OK(dbi->TEST_CompactMemTable());
67 |
68 | // Delete everything
69 | for (int i = 0; i < kCount; i++) {
70 | ASSERT_OK(db_->Delete(WriteOptions(), Key(i)));
71 | }
72 | ASSERT_OK(dbi->TEST_CompactMemTable());
73 |
74 | // Get initial measurement of the space we will be reading.
75 | const int64_t initial_size = Size(Key(0), Key(n));
76 | const int64_t initial_other_size = Size(Key(n), Key(kCount));
77 |
78 | // Read until size drops significantly.
79 | std::string limit_key = Key(n);
80 | for (int read = 0; true; read++) {
81 | ASSERT_LT(read, 100) << "Taking too long to compact";
82 | Iterator* iter = db_->NewIterator(ReadOptions());
83 | for (iter->SeekToFirst();
84 | iter->Valid() && iter->key().ToString() < limit_key;
85 | iter->Next()) {
86 | // Drop data
87 | }
88 | delete iter;
89 | // Wait a little bit to allow any triggered compactions to complete.
90 | Env::Default()->SleepForMicroseconds(1000000);
91 | uint64_t size = Size(Key(0), Key(n));
92 | fprintf(stderr, "iter %3d => %7.3f MB [other %7.3f MB]\n",
93 | read+1, size/1048576.0, Size(Key(n), Key(kCount))/1048576.0);
94 | if (size <= initial_size/10) {
95 | break;
96 | }
97 | }
98 |
99 | // Verify that the size of the key space not touched by the reads
100 | // is pretty much unchanged.
101 | const int64_t final_other_size = Size(Key(n), Key(kCount));
102 | ASSERT_LE(final_other_size, initial_other_size + 1048576);
103 | ASSERT_GE(final_other_size, initial_other_size/5 - 1048576);
104 | }
105 |
106 | TEST(AutoCompactTest, ReadAll) {
107 | DoReads(kCount);
108 | }
109 |
110 | TEST(AutoCompactTest, ReadHalf) {
111 | DoReads(kCount/2);
112 | }
113 |
114 | } // namespace leveldb
115 |
116 | int main(int argc, char** argv) {
117 | return leveldb::test::RunAllTests();
118 | }
119 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/builder.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "db/builder.h"
6 |
7 | #include "db/filename.h"
8 | #include "db/dbformat.h"
9 | #include "db/table_cache.h"
10 | #include "db/version_edit.h"
11 | #include "leveldb/db.h"
12 | #include "leveldb/env.h"
13 | #include "leveldb/iterator.h"
14 |
15 | namespace leveldb {
16 |
17 | Status BuildTable(const std::string& dbname,
18 | Env* env,
19 | const Options& options,
20 | TableCache* table_cache,
21 | Iterator* iter,
22 | FileMetaData* meta) {
23 | Status s;
24 | meta->file_size = 0;
25 | iter->SeekToFirst();
26 |
27 | std::string fname = TableFileName(dbname, meta->number);
28 | if (iter->Valid()) {
29 | WritableFile* file;
30 | s = env->NewWritableFile(fname, &file);
31 | if (!s.ok()) {
32 | return s;
33 | }
34 |
35 | TableBuilder* builder = new TableBuilder(options, file);
36 | meta->smallest.DecodeFrom(iter->key());
37 | for (; iter->Valid(); iter->Next()) {
38 | Slice key = iter->key();
39 | meta->largest.DecodeFrom(key);
40 | builder->Add(key, iter->value());
41 | }
42 |
43 | // Finish and check for builder errors
44 | if (s.ok()) {
45 | s = builder->Finish();
46 | if (s.ok()) {
47 | meta->file_size = builder->FileSize();
48 | assert(meta->file_size > 0);
49 | }
50 | } else {
51 | builder->Abandon();
52 | }
53 | delete builder;
54 |
55 | // Finish and check for file errors
56 | if (s.ok()) {
57 | s = file->Sync();
58 | }
59 | if (s.ok()) {
60 | s = file->Close();
61 | }
62 | delete file;
63 | file = NULL;
64 |
65 | if (s.ok()) {
66 | // Verify that the table is usable
67 | Iterator* it = table_cache->NewIterator(ReadOptions(),
68 | meta->number,
69 | meta->file_size);
70 | s = it->status();
71 | delete it;
72 | }
73 | }
74 |
75 | // Check for input iterator errors
76 | if (!iter->status().ok()) {
77 | s = iter->status();
78 | }
79 |
80 | if (s.ok() && meta->file_size > 0) {
81 | // Keep it
82 | } else {
83 | env->DeleteFile(fname);
84 | }
85 | return s;
86 | }
87 |
88 | } // namespace leveldb
89 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/builder.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_BUILDER_H_
6 | #define STORAGE_LEVELDB_DB_BUILDER_H_
7 |
8 | #include "leveldb/status.h"
9 |
10 | namespace leveldb {
11 |
12 | struct Options;
13 | struct FileMetaData;
14 |
15 | class Env;
16 | class Iterator;
17 | class TableCache;
18 | class VersionEdit;
19 |
20 | // Build a Table file from the contents of *iter. The generated file
21 | // will be named according to meta->number. On success, the rest of
22 | // *meta will be filled with metadata about the generated table.
23 | // If no data is present in *iter, meta->file_size will be set to
24 | // zero, and no Table file will be produced.
25 | extern Status BuildTable(const std::string& dbname,
26 | Env* env,
27 | const Options& options,
28 | TableCache* table_cache,
29 | Iterator* iter,
30 | FileMetaData* meta);
31 |
32 | } // namespace leveldb
33 |
34 | #endif // STORAGE_LEVELDB_DB_BUILDER_H_
35 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/db_iter.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_DB_ITER_H_
6 | #define STORAGE_LEVELDB_DB_DB_ITER_H_
7 |
8 | #include
9 | #include "leveldb/db.h"
10 | #include "db/dbformat.h"
11 |
12 | namespace leveldb {
13 |
14 | class DBImpl;
15 |
16 | // Return a new iterator that converts internal keys (yielded by
17 | // "*internal_iter") that were live at the specified "sequence" number
18 | // into appropriate user keys.
19 | extern Iterator* NewDBIterator(
20 | DBImpl* db,
21 | const Comparator* user_key_comparator,
22 | Iterator* internal_iter,
23 | SequenceNumber sequence,
24 | uint32_t seed);
25 |
26 | } // namespace leveldb
27 |
28 | #endif // STORAGE_LEVELDB_DB_DB_ITER_H_
29 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/filename.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // File names used by DB code
6 |
7 | #ifndef STORAGE_LEVELDB_DB_FILENAME_H_
8 | #define STORAGE_LEVELDB_DB_FILENAME_H_
9 |
10 | #include
11 | #include
12 | #include "leveldb/slice.h"
13 | #include "leveldb/status.h"
14 | #include "port/port.h"
15 |
16 | namespace leveldb {
17 |
18 | class Env;
19 |
20 | enum FileType {
21 | kLogFile,
22 | kDBLockFile,
23 | kTableFile,
24 | kDescriptorFile,
25 | kCurrentFile,
26 | kTempFile,
27 | kInfoLogFile // Either the current one, or an old one
28 | };
29 |
30 | // Return the name of the log file with the specified number
31 | // in the db named by "dbname". The result will be prefixed with
32 | // "dbname".
33 | extern std::string LogFileName(const std::string& dbname, uint64_t number);
34 |
35 | // Return the name of the sstable with the specified number
36 | // in the db named by "dbname". The result will be prefixed with
37 | // "dbname".
38 | extern std::string TableFileName(const std::string& dbname, uint64_t number);
39 |
40 | // Return the legacy file name for an sstable with the specified number
41 | // in the db named by "dbname". The result will be prefixed with
42 | // "dbname".
43 | extern std::string SSTTableFileName(const std::string& dbname, uint64_t number);
44 |
45 | // Return the name of the descriptor file for the db named by
46 | // "dbname" and the specified incarnation number. The result will be
47 | // prefixed with "dbname".
48 | extern std::string DescriptorFileName(const std::string& dbname,
49 | uint64_t number);
50 |
51 | // Return the name of the current file. This file contains the name
52 | // of the current manifest file. The result will be prefixed with
53 | // "dbname".
54 | extern std::string CurrentFileName(const std::string& dbname);
55 |
56 | // Return the name of the lock file for the db named by
57 | // "dbname". The result will be prefixed with "dbname".
58 | extern std::string LockFileName(const std::string& dbname);
59 |
60 | // Return the name of a temporary file owned by the db named "dbname".
61 | // The result will be prefixed with "dbname".
62 | extern std::string TempFileName(const std::string& dbname, uint64_t number);
63 |
64 | // Return the name of the info log file for "dbname".
65 | extern std::string InfoLogFileName(const std::string& dbname);
66 |
67 | // Return the name of the old info log file for "dbname".
68 | extern std::string OldInfoLogFileName(const std::string& dbname);
69 |
70 | // If filename is a leveldb file, store the type of the file in *type.
71 | // The number encoded in the filename is stored in *number. If the
72 | // filename was successfully parsed, returns true. Else return false.
73 | extern bool ParseFileName(const std::string& filename,
74 | uint64_t* number,
75 | FileType* type);
76 |
77 | // Make the CURRENT file point to the descriptor file with the
78 | // specified number.
79 | extern Status SetCurrentFile(Env* env, const std::string& dbname,
80 | uint64_t descriptor_number);
81 |
82 |
83 | } // namespace leveldb
84 |
85 | #endif // STORAGE_LEVELDB_DB_FILENAME_H_
86 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/filename_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "db/filename.h"
6 |
7 | #include "db/dbformat.h"
8 | #include "port/port.h"
9 | #include "util/logging.h"
10 | #include "util/testharness.h"
11 |
12 | namespace leveldb {
13 |
14 | class FileNameTest { };
15 |
16 | TEST(FileNameTest, Parse) {
17 | Slice db;
18 | FileType type;
19 | uint64_t number;
20 |
21 | // Successful parses
22 | static struct {
23 | const char* fname;
24 | uint64_t number;
25 | FileType type;
26 | } cases[] = {
27 | { "100.log", 100, kLogFile },
28 | { "0.log", 0, kLogFile },
29 | { "0.sst", 0, kTableFile },
30 | { "0.ldb", 0, kTableFile },
31 | { "CURRENT", 0, kCurrentFile },
32 | { "LOCK", 0, kDBLockFile },
33 | { "MANIFEST-2", 2, kDescriptorFile },
34 | { "MANIFEST-7", 7, kDescriptorFile },
35 | { "LOG", 0, kInfoLogFile },
36 | { "LOG.old", 0, kInfoLogFile },
37 | { "18446744073709551615.log", 18446744073709551615ull, kLogFile },
38 | };
39 | for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
40 | std::string f = cases[i].fname;
41 | ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
42 | ASSERT_EQ(cases[i].type, type) << f;
43 | ASSERT_EQ(cases[i].number, number) << f;
44 | }
45 |
46 | // Errors
47 | static const char* errors[] = {
48 | "",
49 | "foo",
50 | "foo-dx-100.log",
51 | ".log",
52 | "",
53 | "manifest",
54 | "CURREN",
55 | "CURRENTX",
56 | "MANIFES",
57 | "MANIFEST",
58 | "MANIFEST-",
59 | "XMANIFEST-3",
60 | "MANIFEST-3x",
61 | "LOC",
62 | "LOCKx",
63 | "LO",
64 | "LOGx",
65 | "18446744073709551616.log",
66 | "184467440737095516150.log",
67 | "100",
68 | "100.",
69 | "100.lop"
70 | };
71 | for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
72 | std::string f = errors[i];
73 | ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
74 | }
75 | }
76 |
77 | TEST(FileNameTest, Construction) {
78 | uint64_t number;
79 | FileType type;
80 | std::string fname;
81 |
82 | fname = CurrentFileName("foo");
83 | ASSERT_EQ("foo/", std::string(fname.data(), 4));
84 | ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
85 | ASSERT_EQ(0, number);
86 | ASSERT_EQ(kCurrentFile, type);
87 |
88 | fname = LockFileName("foo");
89 | ASSERT_EQ("foo/", std::string(fname.data(), 4));
90 | ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
91 | ASSERT_EQ(0, number);
92 | ASSERT_EQ(kDBLockFile, type);
93 |
94 | fname = LogFileName("foo", 192);
95 | ASSERT_EQ("foo/", std::string(fname.data(), 4));
96 | ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
97 | ASSERT_EQ(192, number);
98 | ASSERT_EQ(kLogFile, type);
99 |
100 | fname = TableFileName("bar", 200);
101 | ASSERT_EQ("bar/", std::string(fname.data(), 4));
102 | ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
103 | ASSERT_EQ(200, number);
104 | ASSERT_EQ(kTableFile, type);
105 |
106 | fname = DescriptorFileName("bar", 100);
107 | ASSERT_EQ("bar/", std::string(fname.data(), 4));
108 | ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
109 | ASSERT_EQ(100, number);
110 | ASSERT_EQ(kDescriptorFile, type);
111 |
112 | fname = TempFileName("tmp", 999);
113 | ASSERT_EQ("tmp/", std::string(fname.data(), 4));
114 | ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
115 | ASSERT_EQ(999, number);
116 | ASSERT_EQ(kTempFile, type);
117 | }
118 |
119 | } // namespace leveldb
120 |
121 | int main(int argc, char** argv) {
122 | return leveldb::test::RunAllTests();
123 | }
124 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/leveldb_main.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include
6 | #include "leveldb/dumpfile.h"
7 | #include "leveldb/env.h"
8 | #include "leveldb/status.h"
9 |
10 | namespace leveldb {
11 | namespace {
12 |
13 | class StdoutPrinter : public WritableFile {
14 | public:
15 | virtual Status Append(const Slice& data) {
16 | fwrite(data.data(), 1, data.size(), stdout);
17 | return Status::OK();
18 | }
19 | virtual Status Close() { return Status::OK(); }
20 | virtual Status Flush() { return Status::OK(); }
21 | virtual Status Sync() { return Status::OK(); }
22 | };
23 |
24 | bool HandleDumpCommand(Env* env, char** files, int num) {
25 | StdoutPrinter printer;
26 | bool ok = true;
27 | for (int i = 0; i < num; i++) {
28 | Status s = DumpFile(env, files[i], &printer);
29 | if (!s.ok()) {
30 | fprintf(stderr, "%s\n", s.ToString().c_str());
31 | ok = false;
32 | }
33 | }
34 | return ok;
35 | }
36 |
37 | } // namespace
38 | } // namespace leveldb
39 |
40 | static void Usage() {
41 | fprintf(
42 | stderr,
43 | "Usage: leveldbutil command...\n"
44 | " dump files... -- dump contents of specified files\n"
45 | );
46 | }
47 |
48 | int main(int argc, char** argv) {
49 | leveldb::Env* env = leveldb::Env::Default();
50 | bool ok = true;
51 | if (argc < 2) {
52 | Usage();
53 | ok = false;
54 | } else {
55 | std::string command = argv[1];
56 | if (command == "dump") {
57 | ok = leveldb::HandleDumpCommand(env, argv+2, argc-2);
58 | } else {
59 | Usage();
60 | ok = false;
61 | }
62 | }
63 | return (ok ? 0 : 1);
64 | }
65 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/log_format.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // Log format information shared by reader and writer.
6 | // See ../doc/log_format.txt for more detail.
7 |
8 | #ifndef STORAGE_LEVELDB_DB_LOG_FORMAT_H_
9 | #define STORAGE_LEVELDB_DB_LOG_FORMAT_H_
10 |
11 | namespace leveldb {
12 | namespace log {
13 |
14 | enum RecordType {
15 | // Zero is reserved for preallocated files
16 | kZeroType = 0,
17 |
18 | kFullType = 1,
19 |
20 | // For fragments
21 | kFirstType = 2,
22 | kMiddleType = 3,
23 | kLastType = 4
24 | };
25 | static const int kMaxRecordType = kLastType;
26 |
27 | static const int kBlockSize = 32768;
28 |
29 | // Header is checksum (4 bytes), length (2 bytes), type (1 byte).
30 | static const int kHeaderSize = 4 + 2 + 1;
31 |
32 | } // namespace log
33 | } // namespace leveldb
34 |
35 | #endif // STORAGE_LEVELDB_DB_LOG_FORMAT_H_
36 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/log_reader.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_LOG_READER_H_
6 | #define STORAGE_LEVELDB_DB_LOG_READER_H_
7 |
8 | #include
9 |
10 | #include "db/log_format.h"
11 | #include "leveldb/slice.h"
12 | #include "leveldb/status.h"
13 |
14 | namespace leveldb {
15 |
16 | class SequentialFile;
17 |
18 | namespace log {
19 |
20 | class Reader {
21 | public:
22 | // Interface for reporting errors.
23 | class Reporter {
24 | public:
25 | virtual ~Reporter();
26 |
27 | // Some corruption was detected. "size" is the approximate number
28 | // of bytes dropped due to the corruption.
29 | virtual void Corruption(size_t bytes, const Status& status) = 0;
30 | };
31 |
32 | // Create a reader that will return log records from "*file".
33 | // "*file" must remain live while this Reader is in use.
34 | //
35 | // If "reporter" is non-NULL, it is notified whenever some data is
36 | // dropped due to a detected corruption. "*reporter" must remain
37 | // live while this Reader is in use.
38 | //
39 | // If "checksum" is true, verify checksums if available.
40 | //
41 | // The Reader will start reading at the first record located at physical
42 | // position >= initial_offset within the file.
43 | Reader(SequentialFile* file, Reporter* reporter, bool checksum,
44 | uint64_t initial_offset);
45 |
46 | ~Reader();
47 |
48 | // Read the next record into *record. Returns true if read
49 | // successfully, false if we hit end of the input. May use
50 | // "*scratch" as temporary storage. The contents filled in *record
51 | // will only be valid until the next mutating operation on this
52 | // reader or the next mutation to *scratch.
53 | bool ReadRecord(Slice* record, std::string* scratch);
54 |
55 | // Returns the physical offset of the last record returned by ReadRecord.
56 | //
57 | // Undefined before the first call to ReadRecord.
58 | uint64_t LastRecordOffset();
59 |
60 | private:
61 | SequentialFile* const file_;
62 | Reporter* const reporter_;
63 | bool const checksum_;
64 | char* const backing_store_;
65 | Slice buffer_;
66 | bool eof_; // Last Read() indicated EOF by returning < kBlockSize
67 |
68 | // Offset of the last record returned by ReadRecord.
69 | uint64_t last_record_offset_;
70 | // Offset of the first location past the end of buffer_.
71 | uint64_t end_of_buffer_offset_;
72 |
73 | // Offset at which to start looking for the first record to return
74 | uint64_t const initial_offset_;
75 |
76 | // Extend record types with the following special values
77 | enum {
78 | kEof = kMaxRecordType + 1,
79 | // Returned whenever we find an invalid physical record.
80 | // Currently there are three situations in which this happens:
81 | // * The record has an invalid CRC (ReadPhysicalRecord reports a drop)
82 | // * The record is a 0-length record (No drop is reported)
83 | // * The record is below constructor's initial_offset (No drop is reported)
84 | kBadRecord = kMaxRecordType + 2
85 | };
86 |
87 | // Skips all blocks that are completely before "initial_offset_".
88 | //
89 | // Returns true on success. Handles reporting.
90 | bool SkipToInitialBlock();
91 |
92 | // Return type, or one of the preceding special values
93 | unsigned int ReadPhysicalRecord(Slice* result);
94 |
95 | // Reports dropped bytes to the reporter.
96 | // buffer_ must be updated to remove the dropped bytes prior to invocation.
97 | void ReportCorruption(uint64_t bytes, const char* reason);
98 | void ReportDrop(uint64_t bytes, const Status& reason);
99 |
100 | // No copying allowed
101 | Reader(const Reader&);
102 | void operator=(const Reader&);
103 | };
104 |
105 | } // namespace log
106 | } // namespace leveldb
107 |
108 | #endif // STORAGE_LEVELDB_DB_LOG_READER_H_
109 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/log_writer.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "db/log_writer.h"
6 |
7 | #include
8 | #include "leveldb/env.h"
9 | #include "util/coding.h"
10 | #include "util/crc32c.h"
11 |
12 | namespace leveldb {
13 | namespace log {
14 |
15 | Writer::Writer(WritableFile* dest)
16 | : dest_(dest),
17 | block_offset_(0) {
18 | for (int i = 0; i <= kMaxRecordType; i++) {
19 | char t = static_cast(i);
20 | type_crc_[i] = crc32c::Value(&t, 1);
21 | }
22 | }
23 |
24 | Writer::~Writer() {
25 | }
26 |
27 | Status Writer::AddRecord(const Slice& slice) {
28 | const char* ptr = slice.data();
29 | size_t left = slice.size();
30 |
31 | // Fragment the record if necessary and emit it. Note that if slice
32 | // is empty, we still want to iterate once to emit a single
33 | // zero-length record
34 | Status s;
35 | bool begin = true;
36 | do {
37 | const int leftover = kBlockSize - block_offset_;
38 | assert(leftover >= 0);
39 | if (leftover < kHeaderSize) {
40 | // Switch to a new block
41 | if (leftover > 0) {
42 | // Fill the trailer (literal below relies on kHeaderSize being 7)
43 | assert(kHeaderSize == 7);
44 | dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover));
45 | }
46 | block_offset_ = 0;
47 | }
48 |
49 | // Invariant: we never leave < kHeaderSize bytes in a block.
50 | assert(kBlockSize - block_offset_ - kHeaderSize >= 0);
51 |
52 | const size_t avail = kBlockSize - block_offset_ - kHeaderSize;
53 | const size_t fragment_length = (left < avail) ? left : avail;
54 |
55 | RecordType type;
56 | const bool end = (left == fragment_length);
57 | if (begin && end) {
58 | type = kFullType;
59 | } else if (begin) {
60 | type = kFirstType;
61 | } else if (end) {
62 | type = kLastType;
63 | } else {
64 | type = kMiddleType;
65 | }
66 |
67 | s = EmitPhysicalRecord(type, ptr, fragment_length);
68 | ptr += fragment_length;
69 | left -= fragment_length;
70 | begin = false;
71 | } while (s.ok() && left > 0);
72 | return s;
73 | }
74 |
75 | Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr, size_t n) {
76 | assert(n <= 0xffff); // Must fit in two bytes
77 | assert(block_offset_ + kHeaderSize + n <= kBlockSize);
78 |
79 | // Format the header
80 | char buf[kHeaderSize];
81 | buf[4] = static_cast(n & 0xff);
82 | buf[5] = static_cast(n >> 8);
83 | buf[6] = static_cast(t);
84 |
85 | // Compute the crc of the record type and the payload.
86 | uint32_t crc = crc32c::Extend(type_crc_[t], ptr, n);
87 | crc = crc32c::Mask(crc); // Adjust for storage
88 | EncodeFixed32(buf, crc);
89 |
90 | // Write the header and the payload
91 | Status s = dest_->Append(Slice(buf, kHeaderSize));
92 | if (s.ok()) {
93 | s = dest_->Append(Slice(ptr, n));
94 | if (s.ok()) {
95 | s = dest_->Flush();
96 | }
97 | }
98 | block_offset_ += kHeaderSize + n;
99 | return s;
100 | }
101 |
102 | } // namespace log
103 | } // namespace leveldb
104 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/log_writer.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_LOG_WRITER_H_
6 | #define STORAGE_LEVELDB_DB_LOG_WRITER_H_
7 |
8 | #include
9 | #include "db/log_format.h"
10 | #include "leveldb/slice.h"
11 | #include "leveldb/status.h"
12 |
13 | namespace leveldb {
14 |
15 | class WritableFile;
16 |
17 | namespace log {
18 |
19 | class Writer {
20 | public:
21 | // Create a writer that will append data to "*dest".
22 | // "*dest" must be initially empty.
23 | // "*dest" must remain live while this Writer is in use.
24 | explicit Writer(WritableFile* dest);
25 | ~Writer();
26 |
27 | Status AddRecord(const Slice& slice);
28 |
29 | private:
30 | WritableFile* dest_;
31 | int block_offset_; // Current offset in block
32 |
33 | // crc32c values for all supported record types. These are
34 | // pre-computed to reduce the overhead of computing the crc of the
35 | // record type stored in the header.
36 | uint32_t type_crc_[kMaxRecordType + 1];
37 |
38 | Status EmitPhysicalRecord(RecordType type, const char* ptr, size_t length);
39 |
40 | // No copying allowed
41 | Writer(const Writer&);
42 | void operator=(const Writer&);
43 | };
44 |
45 | } // namespace log
46 | } // namespace leveldb
47 |
48 | #endif // STORAGE_LEVELDB_DB_LOG_WRITER_H_
49 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/memtable.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_MEMTABLE_H_
6 | #define STORAGE_LEVELDB_DB_MEMTABLE_H_
7 |
8 | #include
9 | #include "leveldb/db.h"
10 | #include "db/dbformat.h"
11 | #include "db/skiplist.h"
12 | #include "util/arena.h"
13 |
14 | namespace leveldb {
15 |
16 | class InternalKeyComparator;
17 | class Mutex;
18 | class MemTableIterator;
19 |
20 | class MemTable {
21 | public:
22 | // MemTables are reference counted. The initial reference count
23 | // is zero and the caller must call Ref() at least once.
24 | explicit MemTable(const InternalKeyComparator& comparator);
25 |
26 | // Increase reference count.
27 | void Ref() { ++refs_; }
28 |
29 | // Drop reference count. Delete if no more references exist.
30 | void Unref() {
31 | --refs_;
32 | assert(refs_ >= 0);
33 | if (refs_ <= 0) {
34 | delete this;
35 | }
36 | }
37 |
38 | // Returns an estimate of the number of bytes of data in use by this
39 | // data structure.
40 | //
41 | // REQUIRES: external synchronization to prevent simultaneous
42 | // operations on the same MemTable.
43 | size_t ApproximateMemoryUsage();
44 |
45 | // Return an iterator that yields the contents of the memtable.
46 | //
47 | // The caller must ensure that the underlying MemTable remains live
48 | // while the returned iterator is live. The keys returned by this
49 | // iterator are internal keys encoded by AppendInternalKey in the
50 | // db/format.{h,cc} module.
51 | Iterator* NewIterator();
52 |
53 | // Add an entry into memtable that maps key to value at the
54 | // specified sequence number and with the specified type.
55 | // Typically value will be empty if type==kTypeDeletion.
56 | void Add(SequenceNumber seq, ValueType type,
57 | const Slice& key,
58 | const Slice& value);
59 |
60 | // If memtable contains a value for key, store it in *value and return true.
61 | // If memtable contains a deletion for key, store a NotFound() error
62 | // in *status and return true.
63 | // Else, return false.
64 | bool Get(const LookupKey& key, std::string* value, Status* s);
65 |
66 | private:
67 | ~MemTable(); // Private since only Unref() should be used to delete it
68 |
69 | struct KeyComparator {
70 | const InternalKeyComparator comparator;
71 | explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) { }
72 | int operator()(const char* a, const char* b) const;
73 | };
74 | friend class MemTableIterator;
75 | friend class MemTableBackwardIterator;
76 |
77 | typedef SkipList Table;
78 |
79 | KeyComparator comparator_;
80 | int refs_;
81 | Arena arena_;
82 | Table table_;
83 |
84 | // No copying allowed
85 | MemTable(const MemTable&);
86 | void operator=(const MemTable&);
87 | };
88 |
89 | } // namespace leveldb
90 |
91 | #endif // STORAGE_LEVELDB_DB_MEMTABLE_H_
92 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/snapshot.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_SNAPSHOT_H_
6 | #define STORAGE_LEVELDB_DB_SNAPSHOT_H_
7 |
8 | #include "leveldb/db.h"
9 |
10 | namespace leveldb {
11 |
12 | class SnapshotList;
13 |
14 | // Snapshots are kept in a doubly-linked list in the DB.
15 | // Each SnapshotImpl corresponds to a particular sequence number.
16 | class SnapshotImpl : public Snapshot {
17 | public:
18 | SequenceNumber number_; // const after creation
19 |
20 | private:
21 | friend class SnapshotList;
22 |
23 | // SnapshotImpl is kept in a doubly-linked circular list
24 | SnapshotImpl* prev_;
25 | SnapshotImpl* next_;
26 |
27 | SnapshotList* list_; // just for sanity checks
28 | };
29 |
30 | class SnapshotList {
31 | public:
32 | SnapshotList() {
33 | list_.prev_ = &list_;
34 | list_.next_ = &list_;
35 | }
36 |
37 | bool empty() const { return list_.next_ == &list_; }
38 | SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
39 | SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
40 |
41 | const SnapshotImpl* New(SequenceNumber seq) {
42 | SnapshotImpl* s = new SnapshotImpl;
43 | s->number_ = seq;
44 | s->list_ = this;
45 | s->next_ = &list_;
46 | s->prev_ = list_.prev_;
47 | s->prev_->next_ = s;
48 | s->next_->prev_ = s;
49 | return s;
50 | }
51 |
52 | void Delete(const SnapshotImpl* s) {
53 | assert(s->list_ == this);
54 | s->prev_->next_ = s->next_;
55 | s->next_->prev_ = s->prev_;
56 | delete s;
57 | }
58 |
59 | private:
60 | // Dummy head of doubly-linked list of snapshots
61 | SnapshotImpl list_;
62 | };
63 |
64 | } // namespace leveldb
65 |
66 | #endif // STORAGE_LEVELDB_DB_SNAPSHOT_H_
67 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/table_cache.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "db/table_cache.h"
6 |
7 | #include "db/filename.h"
8 | #include "leveldb/env.h"
9 | #include "leveldb/table.h"
10 | #include "util/coding.h"
11 |
12 | namespace leveldb {
13 |
14 | struct TableAndFile {
15 | RandomAccessFile* file;
16 | Table* table;
17 | };
18 |
19 | static void DeleteEntry(const Slice& key, void* value) {
20 | TableAndFile* tf = reinterpret_cast(value);
21 | delete tf->table;
22 | delete tf->file;
23 | delete tf;
24 | }
25 |
26 | static void UnrefEntry(void* arg1, void* arg2) {
27 | Cache* cache = reinterpret_cast(arg1);
28 | Cache::Handle* h = reinterpret_cast(arg2);
29 | cache->Release(h);
30 | }
31 |
32 | TableCache::TableCache(const std::string& dbname,
33 | const Options* options,
34 | int entries)
35 | : env_(options->env),
36 | dbname_(dbname),
37 | options_(options),
38 | cache_(NewLRUCache(entries)) {
39 | }
40 |
41 | TableCache::~TableCache() {
42 | delete cache_;
43 | }
44 |
45 | Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
46 | Cache::Handle** handle) {
47 | Status s;
48 | char buf[sizeof(file_number)];
49 | EncodeFixed64(buf, file_number);
50 | Slice key(buf, sizeof(buf));
51 | *handle = cache_->Lookup(key);
52 | if (*handle == NULL) {
53 | std::string fname = TableFileName(dbname_, file_number);
54 | RandomAccessFile* file = NULL;
55 | Table* table = NULL;
56 | s = env_->NewRandomAccessFile(fname, &file);
57 | if (!s.ok()) {
58 | std::string old_fname = SSTTableFileName(dbname_, file_number);
59 | if (env_->NewRandomAccessFile(old_fname, &file).ok()) {
60 | s = Status::OK();
61 | }
62 | }
63 | if (s.ok()) {
64 | s = Table::Open(*options_, file, file_size, &table);
65 | }
66 |
67 | if (!s.ok()) {
68 | assert(table == NULL);
69 | delete file;
70 | // We do not cache error results so that if the error is transient,
71 | // or somebody repairs the file, we recover automatically.
72 | } else {
73 | TableAndFile* tf = new TableAndFile;
74 | tf->file = file;
75 | tf->table = table;
76 | *handle = cache_->Insert(key, tf, 1, &DeleteEntry);
77 | }
78 | }
79 | return s;
80 | }
81 |
82 | Iterator* TableCache::NewIterator(const ReadOptions& options,
83 | uint64_t file_number,
84 | uint64_t file_size,
85 | Table** tableptr) {
86 | if (tableptr != NULL) {
87 | *tableptr = NULL;
88 | }
89 |
90 | Cache::Handle* handle = NULL;
91 | Status s = FindTable(file_number, file_size, &handle);
92 | if (!s.ok()) {
93 | return NewErrorIterator(s);
94 | }
95 |
96 | Table* table = reinterpret_cast(cache_->Value(handle))->table;
97 | Iterator* result = table->NewIterator(options);
98 | result->RegisterCleanup(&UnrefEntry, cache_, handle);
99 | if (tableptr != NULL) {
100 | *tableptr = table;
101 | }
102 | return result;
103 | }
104 |
105 | Status TableCache::Get(const ReadOptions& options,
106 | uint64_t file_number,
107 | uint64_t file_size,
108 | const Slice& k,
109 | void* arg,
110 | void (*saver)(void*, const Slice&, const Slice&)) {
111 | Cache::Handle* handle = NULL;
112 | Status s = FindTable(file_number, file_size, &handle);
113 | if (s.ok()) {
114 | Table* t = reinterpret_cast(cache_->Value(handle))->table;
115 | s = t->InternalGet(options, k, arg, saver);
116 | cache_->Release(handle);
117 | }
118 | return s;
119 | }
120 |
121 | void TableCache::Evict(uint64_t file_number) {
122 | char buf[sizeof(file_number)];
123 | EncodeFixed64(buf, file_number);
124 | cache_->Erase(Slice(buf, sizeof(buf)));
125 | }
126 |
127 | } // namespace leveldb
128 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/table_cache.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // Thread-safe (provides internal synchronization)
6 |
7 | #ifndef STORAGE_LEVELDB_DB_TABLE_CACHE_H_
8 | #define STORAGE_LEVELDB_DB_TABLE_CACHE_H_
9 |
10 | #include
11 | #include
12 | #include "db/dbformat.h"
13 | #include "leveldb/cache.h"
14 | #include "leveldb/table.h"
15 | #include "port/port.h"
16 |
17 | namespace leveldb {
18 |
19 | class Env;
20 |
21 | class TableCache {
22 | public:
23 | TableCache(const std::string& dbname, const Options* options, int entries);
24 | ~TableCache();
25 |
26 | // Return an iterator for the specified file number (the corresponding
27 | // file length must be exactly "file_size" bytes). If "tableptr" is
28 | // non-NULL, also sets "*tableptr" to point to the Table object
29 | // underlying the returned iterator, or NULL if no Table object underlies
30 | // the returned iterator. The returned "*tableptr" object is owned by
31 | // the cache and should not be deleted, and is valid for as long as the
32 | // returned iterator is live.
33 | Iterator* NewIterator(const ReadOptions& options,
34 | uint64_t file_number,
35 | uint64_t file_size,
36 | Table** tableptr = NULL);
37 |
38 | // If a seek to internal key "k" in specified file finds an entry,
39 | // call (*handle_result)(arg, found_key, found_value).
40 | Status Get(const ReadOptions& options,
41 | uint64_t file_number,
42 | uint64_t file_size,
43 | const Slice& k,
44 | void* arg,
45 | void (*handle_result)(void*, const Slice&, const Slice&));
46 |
47 | // Evict any entry for the specified file number
48 | void Evict(uint64_t file_number);
49 |
50 | private:
51 | Env* const env_;
52 | const std::string dbname_;
53 | const Options* options_;
54 | Cache* cache_;
55 |
56 | Status FindTable(uint64_t file_number, uint64_t file_size, Cache::Handle**);
57 | };
58 |
59 | } // namespace leveldb
60 |
61 | #endif // STORAGE_LEVELDB_DB_TABLE_CACHE_H_
62 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/version_edit.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_VERSION_EDIT_H_
6 | #define STORAGE_LEVELDB_DB_VERSION_EDIT_H_
7 |
8 | #include
9 | #include
10 | #include
11 | #include "db/dbformat.h"
12 |
13 | namespace leveldb {
14 |
15 | class VersionSet;
16 |
17 | struct FileMetaData {
18 | int refs;
19 | int allowed_seeks; // Seeks allowed until compaction
20 | uint64_t number;
21 | uint64_t file_size; // File size in bytes
22 | InternalKey smallest; // Smallest internal key served by table
23 | InternalKey largest; // Largest internal key served by table
24 |
25 | FileMetaData() : refs(0), allowed_seeks(1 << 30), file_size(0) { }
26 | };
27 |
28 | class VersionEdit {
29 | public:
30 | VersionEdit() { Clear(); }
31 | ~VersionEdit() { }
32 |
33 | void Clear();
34 |
35 | void SetComparatorName(const Slice& name) {
36 | has_comparator_ = true;
37 | comparator_ = name.ToString();
38 | }
39 | void SetLogNumber(uint64_t num) {
40 | has_log_number_ = true;
41 | log_number_ = num;
42 | }
43 | void SetPrevLogNumber(uint64_t num) {
44 | has_prev_log_number_ = true;
45 | prev_log_number_ = num;
46 | }
47 | void SetNextFile(uint64_t num) {
48 | has_next_file_number_ = true;
49 | next_file_number_ = num;
50 | }
51 | void SetLastSequence(SequenceNumber seq) {
52 | has_last_sequence_ = true;
53 | last_sequence_ = seq;
54 | }
55 | void SetCompactPointer(int level, const InternalKey& key) {
56 | compact_pointers_.push_back(std::make_pair(level, key));
57 | }
58 |
59 | // Add the specified file at the specified number.
60 | // REQUIRES: This version has not been saved (see VersionSet::SaveTo)
61 | // REQUIRES: "smallest" and "largest" are smallest and largest keys in file
62 | void AddFile(int level, uint64_t file,
63 | uint64_t file_size,
64 | const InternalKey& smallest,
65 | const InternalKey& largest) {
66 | FileMetaData f;
67 | f.number = file;
68 | f.file_size = file_size;
69 | f.smallest = smallest;
70 | f.largest = largest;
71 | new_files_.push_back(std::make_pair(level, f));
72 | }
73 |
74 | // Delete the specified "file" from the specified "level".
75 | void DeleteFile(int level, uint64_t file) {
76 | deleted_files_.insert(std::make_pair(level, file));
77 | }
78 |
79 | void EncodeTo(std::string* dst) const;
80 | Status DecodeFrom(const Slice& src);
81 |
82 | std::string DebugString() const;
83 |
84 | private:
85 | friend class VersionSet;
86 |
87 | typedef std::set< std::pair > DeletedFileSet;
88 |
89 | std::string comparator_;
90 | uint64_t log_number_;
91 | uint64_t prev_log_number_;
92 | uint64_t next_file_number_;
93 | SequenceNumber last_sequence_;
94 | bool has_comparator_;
95 | bool has_log_number_;
96 | bool has_prev_log_number_;
97 | bool has_next_file_number_;
98 | bool has_last_sequence_;
99 |
100 | std::vector< std::pair > compact_pointers_;
101 | DeletedFileSet deleted_files_;
102 | std::vector< std::pair > new_files_;
103 | };
104 |
105 | } // namespace leveldb
106 |
107 | #endif // STORAGE_LEVELDB_DB_VERSION_EDIT_H_
108 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/version_edit_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "db/version_edit.h"
6 | #include "util/testharness.h"
7 |
8 | namespace leveldb {
9 |
10 | static void TestEncodeDecode(const VersionEdit& edit) {
11 | std::string encoded, encoded2;
12 | edit.EncodeTo(&encoded);
13 | VersionEdit parsed;
14 | Status s = parsed.DecodeFrom(encoded);
15 | ASSERT_TRUE(s.ok()) << s.ToString();
16 | parsed.EncodeTo(&encoded2);
17 | ASSERT_EQ(encoded, encoded2);
18 | }
19 |
20 | class VersionEditTest { };
21 |
22 | TEST(VersionEditTest, EncodeDecode) {
23 | static const uint64_t kBig = 1ull << 50;
24 |
25 | VersionEdit edit;
26 | for (int i = 0; i < 4; i++) {
27 | TestEncodeDecode(edit);
28 | edit.AddFile(3, kBig + 300 + i, kBig + 400 + i,
29 | InternalKey("foo", kBig + 500 + i, kTypeValue),
30 | InternalKey("zoo", kBig + 600 + i, kTypeDeletion));
31 | edit.DeleteFile(4, kBig + 700 + i);
32 | edit.SetCompactPointer(i, InternalKey("x", kBig + 900 + i, kTypeValue));
33 | }
34 |
35 | edit.SetComparatorName("foo");
36 | edit.SetLogNumber(kBig + 100);
37 | edit.SetNextFile(kBig + 200);
38 | edit.SetLastSequence(kBig + 1000);
39 | TestEncodeDecode(edit);
40 | }
41 |
42 | } // namespace leveldb
43 |
44 | int main(int argc, char** argv) {
45 | return leveldb::test::RunAllTests();
46 | }
47 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/write_batch_internal.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_DB_WRITE_BATCH_INTERNAL_H_
6 | #define STORAGE_LEVELDB_DB_WRITE_BATCH_INTERNAL_H_
7 |
8 | #include "leveldb/write_batch.h"
9 |
10 | namespace leveldb {
11 |
12 | class MemTable;
13 |
14 | // WriteBatchInternal provides static methods for manipulating a
15 | // WriteBatch that we don't want in the public WriteBatch interface.
16 | class WriteBatchInternal {
17 | public:
18 | // Return the number of entries in the batch.
19 | static int Count(const WriteBatch* batch);
20 |
21 | // Set the count for the number of entries in the batch.
22 | static void SetCount(WriteBatch* batch, int n);
23 |
24 | // Return the sequence number for the start of this batch.
25 | static SequenceNumber Sequence(const WriteBatch* batch);
26 |
27 | // Store the specified number as the sequence number for the start of
28 | // this batch.
29 | static void SetSequence(WriteBatch* batch, SequenceNumber seq);
30 |
31 | static Slice Contents(const WriteBatch* batch) {
32 | return Slice(batch->rep_);
33 | }
34 |
35 | static size_t ByteSize(const WriteBatch* batch) {
36 | return batch->rep_.size();
37 | }
38 |
39 | static void SetContents(WriteBatch* batch, const Slice& contents);
40 |
41 | static Status InsertInto(const WriteBatch* batch, MemTable* memtable);
42 |
43 | static void Append(WriteBatch* dst, const WriteBatch* src);
44 | };
45 |
46 | } // namespace leveldb
47 |
48 |
49 | #endif // STORAGE_LEVELDB_DB_WRITE_BATCH_INTERNAL_H_
50 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/db/write_batch_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/db.h"
6 |
7 | #include "db/memtable.h"
8 | #include "db/write_batch_internal.h"
9 | #include "leveldb/env.h"
10 | #include "util/logging.h"
11 | #include "util/testharness.h"
12 |
13 | namespace leveldb {
14 |
15 | static std::string PrintContents(WriteBatch* b) {
16 | InternalKeyComparator cmp(BytewiseComparator());
17 | MemTable* mem = new MemTable(cmp);
18 | mem->Ref();
19 | std::string state;
20 | Status s = WriteBatchInternal::InsertInto(b, mem);
21 | int count = 0;
22 | Iterator* iter = mem->NewIterator();
23 | for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
24 | ParsedInternalKey ikey;
25 | ASSERT_TRUE(ParseInternalKey(iter->key(), &ikey));
26 | switch (ikey.type) {
27 | case kTypeValue:
28 | state.append("Put(");
29 | state.append(ikey.user_key.ToString());
30 | state.append(", ");
31 | state.append(iter->value().ToString());
32 | state.append(")");
33 | count++;
34 | break;
35 | case kTypeDeletion:
36 | state.append("Delete(");
37 | state.append(ikey.user_key.ToString());
38 | state.append(")");
39 | count++;
40 | break;
41 | }
42 | state.append("@");
43 | state.append(NumberToString(ikey.sequence));
44 | }
45 | delete iter;
46 | if (!s.ok()) {
47 | state.append("ParseError()");
48 | } else if (count != WriteBatchInternal::Count(b)) {
49 | state.append("CountMismatch()");
50 | }
51 | mem->Unref();
52 | return state;
53 | }
54 |
55 | class WriteBatchTest { };
56 |
57 | TEST(WriteBatchTest, Empty) {
58 | WriteBatch batch;
59 | ASSERT_EQ("", PrintContents(&batch));
60 | ASSERT_EQ(0, WriteBatchInternal::Count(&batch));
61 | }
62 |
63 | TEST(WriteBatchTest, Multiple) {
64 | WriteBatch batch;
65 | batch.Put(Slice("foo"), Slice("bar"));
66 | batch.Delete(Slice("box"));
67 | batch.Put(Slice("baz"), Slice("boo"));
68 | WriteBatchInternal::SetSequence(&batch, 100);
69 | ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch));
70 | ASSERT_EQ(3, WriteBatchInternal::Count(&batch));
71 | ASSERT_EQ("Put(baz, boo)@102"
72 | "Delete(box)@101"
73 | "Put(foo, bar)@100",
74 | PrintContents(&batch));
75 | }
76 |
77 | TEST(WriteBatchTest, Corruption) {
78 | WriteBatch batch;
79 | batch.Put(Slice("foo"), Slice("bar"));
80 | batch.Delete(Slice("box"));
81 | WriteBatchInternal::SetSequence(&batch, 200);
82 | Slice contents = WriteBatchInternal::Contents(&batch);
83 | WriteBatchInternal::SetContents(&batch,
84 | Slice(contents.data(),contents.size()-1));
85 | ASSERT_EQ("Put(foo, bar)@200"
86 | "ParseError()",
87 | PrintContents(&batch));
88 | }
89 |
90 | TEST(WriteBatchTest, Append) {
91 | WriteBatch b1, b2;
92 | WriteBatchInternal::SetSequence(&b1, 200);
93 | WriteBatchInternal::SetSequence(&b2, 300);
94 | WriteBatchInternal::Append(&b1, &b2);
95 | ASSERT_EQ("",
96 | PrintContents(&b1));
97 | b2.Put("a", "va");
98 | WriteBatchInternal::Append(&b1, &b2);
99 | ASSERT_EQ("Put(a, va)@200",
100 | PrintContents(&b1));
101 | b2.Clear();
102 | b2.Put("b", "vb");
103 | WriteBatchInternal::Append(&b1, &b2);
104 | ASSERT_EQ("Put(a, va)@200"
105 | "Put(b, vb)@201",
106 | PrintContents(&b1));
107 | b2.Delete("foo");
108 | WriteBatchInternal::Append(&b1, &b2);
109 | ASSERT_EQ("Put(a, va)@200"
110 | "Put(b, vb)@202"
111 | "Put(b, vb)@201"
112 | "Delete(foo)@203",
113 | PrintContents(&b1));
114 | }
115 |
116 | } // namespace leveldb
117 |
118 | int main(int argc, char** argv) {
119 | return leveldb::test::RunAllTests();
120 | }
121 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/doc/doc.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin-left: 0.5in;
3 | margin-right: 0.5in;
4 | background: white;
5 | color: black;
6 | }
7 |
8 | h1 {
9 | margin-left: -0.2in;
10 | font-size: 14pt;
11 | }
12 | h2 {
13 | margin-left: -0in;
14 | font-size: 12pt;
15 | }
16 | h3 {
17 | margin-left: -0in;
18 | }
19 | h4 {
20 | margin-left: -0in;
21 | }
22 | hr {
23 | margin-left: -0in;
24 | }
25 |
26 | /* Definition lists: definition term bold */
27 | dt {
28 | font-weight: bold;
29 | }
30 |
31 | address {
32 | text-align: center;
33 | }
34 | code,samp,var {
35 | color: blue;
36 | }
37 | kbd {
38 | color: #600000;
39 | }
40 | div.note p {
41 | float: right;
42 | width: 3in;
43 | margin-right: 0%;
44 | padding: 1px;
45 | border: 2px solid #6060a0;
46 | background-color: #fffff0;
47 | }
48 |
49 | ul {
50 | margin-top: -0em;
51 | margin-bottom: -0em;
52 | }
53 |
54 | ol {
55 | margin-top: -0em;
56 | margin-bottom: -0em;
57 | }
58 |
59 | UL.nobullets {
60 | list-style-type: none;
61 | list-style-image: none;
62 | margin-left: -1em;
63 | }
64 |
65 | p {
66 | margin: 1em 0 1em 0;
67 | padding: 0 0 0 0;
68 | }
69 |
70 | pre {
71 | line-height: 1.3em;
72 | padding: 0.4em 0 0.8em 0;
73 | margin: 0 0 0 0;
74 | border: 0 0 0 0;
75 | color: blue;
76 | }
77 |
78 | .datatable {
79 | margin-left: auto;
80 | margin-right: auto;
81 | margin-top: 2em;
82 | margin-bottom: 2em;
83 | border: 1px solid;
84 | }
85 |
86 | .datatable td,th {
87 | padding: 0 0.5em 0 0.5em;
88 | text-align: right;
89 | }
90 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/doc/log_format.txt:
--------------------------------------------------------------------------------
1 | The log file contents are a sequence of 32KB blocks. The only
2 | exception is that the tail of the file may contain a partial block.
3 |
4 | Each block consists of a sequence of records:
5 | block := record* trailer?
6 | record :=
7 | checksum: uint32 // crc32c of type and data[] ; little-endian
8 | length: uint16 // little-endian
9 | type: uint8 // One of FULL, FIRST, MIDDLE, LAST
10 | data: uint8[length]
11 |
12 | A record never starts within the last six bytes of a block (since it
13 | won't fit). Any leftover bytes here form the trailer, which must
14 | consist entirely of zero bytes and must be skipped by readers.
15 |
16 | Aside: if exactly seven bytes are left in the current block, and a new
17 | non-zero length record is added, the writer must emit a FIRST record
18 | (which contains zero bytes of user data) to fill up the trailing seven
19 | bytes of the block and then emit all of the user data in subsequent
20 | blocks.
21 |
22 | More types may be added in the future. Some Readers may skip record
23 | types they do not understand, others may report that some data was
24 | skipped.
25 |
26 | FULL == 1
27 | FIRST == 2
28 | MIDDLE == 3
29 | LAST == 4
30 |
31 | The FULL record contains the contents of an entire user record.
32 |
33 | FIRST, MIDDLE, LAST are types used for user records that have been
34 | split into multiple fragments (typically because of block boundaries).
35 | FIRST is the type of the first fragment of a user record, LAST is the
36 | type of the last fragment of a user record, and MIDDLE is the type of
37 | all interior fragments of a user record.
38 |
39 | Example: consider a sequence of user records:
40 | A: length 1000
41 | B: length 97270
42 | C: length 8000
43 | A will be stored as a FULL record in the first block.
44 |
45 | B will be split into three fragments: first fragment occupies the rest
46 | of the first block, second fragment occupies the entirety of the
47 | second block, and the third fragment occupies a prefix of the third
48 | block. This will leave six bytes free in the third block, which will
49 | be left empty as the trailer.
50 |
51 | C will be stored as a FULL record in the fourth block.
52 |
53 | ===================
54 |
55 | Some benefits over the recordio format:
56 |
57 | (1) We do not need any heuristics for resyncing - just go to next
58 | block boundary and scan. If there is a corruption, skip to the next
59 | block. As a side-benefit, we do not get confused when part of the
60 | contents of one log file are embedded as a record inside another log
61 | file.
62 |
63 | (2) Splitting at approximate boundaries (e.g., for mapreduce) is
64 | simple: find the next block boundary and skip records until we
65 | hit a FULL or FIRST record.
66 |
67 | (3) We do not need extra buffering for large records.
68 |
69 | Some downsides compared to recordio format:
70 |
71 | (1) No packing of tiny records. This could be fixed by adding a new
72 | record type, so it is a shortcoming of the current implementation,
73 | not necessarily the format.
74 |
75 | (2) No compression. Again, this could be fixed by adding new record types.
76 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/doc/table_format.txt:
--------------------------------------------------------------------------------
1 | File format
2 | ===========
3 |
4 |
5 | [data block 1]
6 | [data block 2]
7 | ...
8 | [data block N]
9 | [meta block 1]
10 | ...
11 | [meta block K]
12 | [metaindex block]
13 | [index block]
14 | [Footer] (fixed size; starts at file_size - sizeof(Footer))
15 |
16 |
17 | The file contains internal pointers. Each such pointer is called
18 | a BlockHandle and contains the following information:
19 | offset: varint64
20 | size: varint64
21 | See https://developers.google.com/protocol-buffers/docs/encoding#varints
22 | for an explanation of varint64 format.
23 |
24 | (1) The sequence of key/value pairs in the file are stored in sorted
25 | order and partitioned into a sequence of data blocks. These blocks
26 | come one after another at the beginning of the file. Each data block
27 | is formatted according to the code in block_builder.cc, and then
28 | optionally compressed.
29 |
30 | (2) After the data blocks we store a bunch of meta blocks. The
31 | supported meta block types are described below. More meta block types
32 | may be added in the future. Each meta block is again formatted using
33 | block_builder.cc and then optionally compressed.
34 |
35 | (3) A "metaindex" block. It contains one entry for every other meta
36 | block where the key is the name of the meta block and the value is a
37 | BlockHandle pointing to that meta block.
38 |
39 | (4) An "index" block. This block contains one entry per data block,
40 | where the key is a string >= last key in that data block and before
41 | the first key in the successive data block. The value is the
42 | BlockHandle for the data block.
43 |
44 | (6) At the very end of the file is a fixed length footer that contains
45 | the BlockHandle of the metaindex and index blocks as well as a magic number.
46 | metaindex_handle: char[p]; // Block handle for metaindex
47 | index_handle: char[q]; // Block handle for index
48 | padding: char[40-p-q]; // zeroed bytes to make fixed length
49 | // (40==2*BlockHandle::kMaxEncodedLength)
50 | magic: fixed64; // == 0xdb4775248b80fb57 (little-endian)
51 |
52 | "filter" Meta Block
53 | -------------------
54 |
55 | If a "FilterPolicy" was specified when the database was opened, a
56 | filter block is stored in each table. The "metaindex" block contains
57 | an entry that maps from "filter." to the BlockHandle for the filter
58 | block where "" is the string returned by the filter policy's
59 | "Name()" method.
60 |
61 | The filter block stores a sequence of filters, where filter i contains
62 | the output of FilterPolicy::CreateFilter() on all keys that are stored
63 | in a block whose file offset falls within the range
64 |
65 | [ i*base ... (i+1)*base-1 ]
66 |
67 | Currently, "base" is 2KB. So for example, if blocks X and Y start in
68 | the range [ 0KB .. 2KB-1 ], all of the keys in X and Y will be
69 | converted to a filter by calling FilterPolicy::CreateFilter(), and the
70 | resulting filter will be stored as the first filter in the filter
71 | block.
72 |
73 | The filter block is formatted as follows:
74 |
75 | [filter 0]
76 | [filter 1]
77 | [filter 2]
78 | ...
79 | [filter N-1]
80 |
81 | [offset of filter 0] : 4 bytes
82 | [offset of filter 1] : 4 bytes
83 | [offset of filter 2] : 4 bytes
84 | ...
85 | [offset of filter N-1] : 4 bytes
86 |
87 | [offset of beginning of offset array] : 4 bytes
88 | lg(base) : 1 byte
89 |
90 | The offset array at the end of the filter block allows efficient
91 | mapping from a data block offset to the corresponding filter.
92 |
93 | "stats" Meta Block
94 | ------------------
95 |
96 | This meta block contains a bunch of stats. The key is the name
97 | of the statistic. The value contains the statistic.
98 | TODO(postrelease): record following stats.
99 | data size
100 | index size
101 | key size (uncompressed)
102 | value size (uncompressed)
103 | number of entries
104 | number of data blocks
105 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/helpers/memenv/memenv.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_
6 | #define STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_
7 |
8 | namespace leveldb {
9 |
10 | class Env;
11 |
12 | // Returns a new environment that stores its data in memory and delegates
13 | // all non-file-storage tasks to base_env. The caller must delete the result
14 | // when it is no longer needed.
15 | // *base_env must remain live while the result is in use.
16 | Env* NewMemEnv(Env* base_env);
17 |
18 | } // namespace leveldb
19 |
20 | #endif // STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_
21 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/cache.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // A Cache is an interface that maps keys to values. It has internal
6 | // synchronization and may be safely accessed concurrently from
7 | // multiple threads. It may automatically evict entries to make room
8 | // for new entries. Values have a specified charge against the cache
9 | // capacity. For example, a cache where the values are variable
10 | // length strings, may use the length of the string as the charge for
11 | // the string.
12 | //
13 | // A builtin cache implementation with a least-recently-used eviction
14 | // policy is provided. Clients may use their own implementations if
15 | // they want something more sophisticated (like scan-resistance, a
16 | // custom eviction policy, variable cache sizing, etc.)
17 |
18 | #ifndef STORAGE_LEVELDB_INCLUDE_CACHE_H_
19 | #define STORAGE_LEVELDB_INCLUDE_CACHE_H_
20 |
21 | #include
22 | #include "leveldb/slice.h"
23 |
24 | namespace leveldb {
25 |
26 | class Cache;
27 |
28 | // Create a new cache with a fixed size capacity. This implementation
29 | // of Cache uses a least-recently-used eviction policy.
30 | extern Cache* NewLRUCache(size_t capacity);
31 |
32 | class Cache {
33 | public:
34 | Cache() { }
35 |
36 | // Destroys all existing entries by calling the "deleter"
37 | // function that was passed to the constructor.
38 | virtual ~Cache();
39 |
40 | // Opaque handle to an entry stored in the cache.
41 | struct Handle { };
42 |
43 | // Insert a mapping from key->value into the cache and assign it
44 | // the specified charge against the total cache capacity.
45 | //
46 | // Returns a handle that corresponds to the mapping. The caller
47 | // must call this->Release(handle) when the returned mapping is no
48 | // longer needed.
49 | //
50 | // When the inserted entry is no longer needed, the key and
51 | // value will be passed to "deleter".
52 | virtual Handle* Insert(const Slice& key, void* value, size_t charge,
53 | void (*deleter)(const Slice& key, void* value)) = 0;
54 |
55 | // If the cache has no mapping for "key", returns NULL.
56 | //
57 | // Else return a handle that corresponds to the mapping. The caller
58 | // must call this->Release(handle) when the returned mapping is no
59 | // longer needed.
60 | virtual Handle* Lookup(const Slice& key) = 0;
61 |
62 | // Release a mapping returned by a previous Lookup().
63 | // REQUIRES: handle must not have been released yet.
64 | // REQUIRES: handle must have been returned by a method on *this.
65 | virtual void Release(Handle* handle) = 0;
66 |
67 | // Return the value encapsulated in a handle returned by a
68 | // successful Lookup().
69 | // REQUIRES: handle must not have been released yet.
70 | // REQUIRES: handle must have been returned by a method on *this.
71 | virtual void* Value(Handle* handle) = 0;
72 |
73 | // If the cache contains entry for key, erase it. Note that the
74 | // underlying entry will be kept around until all existing handles
75 | // to it have been released.
76 | virtual void Erase(const Slice& key) = 0;
77 |
78 | // Return a new numeric id. May be used by multiple clients who are
79 | // sharing the same cache to partition the key space. Typically the
80 | // client will allocate a new id at startup and prepend the id to
81 | // its cache keys.
82 | virtual uint64_t NewId() = 0;
83 |
84 | private:
85 | void LRU_Remove(Handle* e);
86 | void LRU_Append(Handle* e);
87 | void Unref(Handle* e);
88 |
89 | struct Rep;
90 | Rep* rep_;
91 |
92 | // No copying allowed
93 | Cache(const Cache&);
94 | void operator=(const Cache&);
95 | };
96 |
97 | } // namespace leveldb
98 |
99 | #endif // STORAGE_LEVELDB_INCLUDE_CACHE_H_
100 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/comparator.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
6 | #define STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
7 |
8 | #include
9 |
10 | namespace leveldb {
11 |
12 | class Slice;
13 |
14 | // A Comparator object provides a total order across slices that are
15 | // used as keys in an sstable or a database. A Comparator implementation
16 | // must be thread-safe since leveldb may invoke its methods concurrently
17 | // from multiple threads.
18 | class Comparator {
19 | public:
20 | virtual ~Comparator();
21 |
22 | // Three-way comparison. Returns value:
23 | // < 0 iff "a" < "b",
24 | // == 0 iff "a" == "b",
25 | // > 0 iff "a" > "b"
26 | virtual int Compare(const Slice& a, const Slice& b) const = 0;
27 |
28 | // The name of the comparator. Used to check for comparator
29 | // mismatches (i.e., a DB created with one comparator is
30 | // accessed using a different comparator.
31 | //
32 | // The client of this package should switch to a new name whenever
33 | // the comparator implementation changes in a way that will cause
34 | // the relative ordering of any two keys to change.
35 | //
36 | // Names starting with "leveldb." are reserved and should not be used
37 | // by any clients of this package.
38 | virtual const char* Name() const = 0;
39 |
40 | // Advanced functions: these are used to reduce the space requirements
41 | // for internal data structures like index blocks.
42 |
43 | // If *start < limit, changes *start to a short string in [start,limit).
44 | // Simple comparator implementations may return with *start unchanged,
45 | // i.e., an implementation of this method that does nothing is correct.
46 | virtual void FindShortestSeparator(
47 | std::string* start,
48 | const Slice& limit) const = 0;
49 |
50 | // Changes *key to a short string >= *key.
51 | // Simple comparator implementations may return with *key unchanged,
52 | // i.e., an implementation of this method that does nothing is correct.
53 | virtual void FindShortSuccessor(std::string* key) const = 0;
54 | };
55 |
56 | // Return a builtin comparator that uses lexicographic byte-wise
57 | // ordering. The result remains the property of this module and
58 | // must not be deleted.
59 | extern const Comparator* BytewiseComparator();
60 |
61 | } // namespace leveldb
62 |
63 | #endif // STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
64 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/dumpfile.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2014 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_INCLUDE_DUMPFILE_H_
6 | #define STORAGE_LEVELDB_INCLUDE_DUMPFILE_H_
7 |
8 | #include
9 | #include "leveldb/env.h"
10 | #include "leveldb/status.h"
11 |
12 | namespace leveldb {
13 |
14 | // Dump the contents of the file named by fname in text format to
15 | // *dst. Makes a sequence of dst->Append() calls; each call is passed
16 | // the newline-terminated text corresponding to a single item found
17 | // in the file.
18 | //
19 | // Returns a non-OK result if fname does not name a leveldb storage
20 | // file, or if the file cannot be read.
21 | Status DumpFile(Env* env, const std::string& fname, WritableFile* dst);
22 |
23 | } // namespace leveldb
24 |
25 | #endif // STORAGE_LEVELDB_INCLUDE_DUMPFILE_H_
26 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/filter_policy.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // A database can be configured with a custom FilterPolicy object.
6 | // This object is responsible for creating a small filter from a set
7 | // of keys. These filters are stored in leveldb and are consulted
8 | // automatically by leveldb to decide whether or not to read some
9 | // information from disk. In many cases, a filter can cut down the
10 | // number of disk seeks form a handful to a single disk seek per
11 | // DB::Get() call.
12 | //
13 | // Most people will want to use the builtin bloom filter support (see
14 | // NewBloomFilterPolicy() below).
15 |
16 | #ifndef STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
17 | #define STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
18 |
19 | #include
20 |
21 | namespace leveldb {
22 |
23 | class Slice;
24 |
25 | class FilterPolicy {
26 | public:
27 | virtual ~FilterPolicy();
28 |
29 | // Return the name of this policy. Note that if the filter encoding
30 | // changes in an incompatible way, the name returned by this method
31 | // must be changed. Otherwise, old incompatible filters may be
32 | // passed to methods of this type.
33 | virtual const char* Name() const = 0;
34 |
35 | // keys[0,n-1] contains a list of keys (potentially with duplicates)
36 | // that are ordered according to the user supplied comparator.
37 | // Append a filter that summarizes keys[0,n-1] to *dst.
38 | //
39 | // Warning: do not change the initial contents of *dst. Instead,
40 | // append the newly constructed filter to *dst.
41 | virtual void CreateFilter(const Slice* keys, int n, std::string* dst)
42 | const = 0;
43 |
44 | // "filter" contains the data appended by a preceding call to
45 | // CreateFilter() on this class. This method must return true if
46 | // the key was in the list of keys passed to CreateFilter().
47 | // This method may return true or false if the key was not on the
48 | // list, but it should aim to return false with a high probability.
49 | virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const = 0;
50 | };
51 |
52 | // Return a new filter policy that uses a bloom filter with approximately
53 | // the specified number of bits per key. A good value for bits_per_key
54 | // is 10, which yields a filter with ~ 1% false positive rate.
55 | //
56 | // Callers must delete the result after any database that is using the
57 | // result has been closed.
58 | //
59 | // Note: if you are using a custom comparator that ignores some parts
60 | // of the keys being compared, you must not use NewBloomFilterPolicy()
61 | // and must provide your own FilterPolicy that also ignores the
62 | // corresponding parts of the keys. For example, if the comparator
63 | // ignores trailing spaces, it would be incorrect to use a
64 | // FilterPolicy (like NewBloomFilterPolicy) that does not ignore
65 | // trailing spaces in keys.
66 | extern const FilterPolicy* NewBloomFilterPolicy(int bits_per_key);
67 |
68 | }
69 |
70 | #endif // STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
71 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/iterator.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // An iterator yields a sequence of key/value pairs from a source.
6 | // The following class defines the interface. Multiple implementations
7 | // are provided by this library. In particular, iterators are provided
8 | // to access the contents of a Table or a DB.
9 | //
10 | // Multiple threads can invoke const methods on an Iterator without
11 | // external synchronization, but if any of the threads may call a
12 | // non-const method, all threads accessing the same Iterator must use
13 | // external synchronization.
14 |
15 | #ifndef STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
16 | #define STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
17 |
18 | #include "leveldb/slice.h"
19 | #include "leveldb/status.h"
20 |
21 | namespace leveldb {
22 |
23 | class Iterator {
24 | public:
25 | Iterator();
26 | virtual ~Iterator();
27 |
28 | // An iterator is either positioned at a key/value pair, or
29 | // not valid. This method returns true iff the iterator is valid.
30 | virtual bool Valid() const = 0;
31 |
32 | // Position at the first key in the source. The iterator is Valid()
33 | // after this call iff the source is not empty.
34 | virtual void SeekToFirst() = 0;
35 |
36 | // Position at the last key in the source. The iterator is
37 | // Valid() after this call iff the source is not empty.
38 | virtual void SeekToLast() = 0;
39 |
40 | // Position at the first key in the source that at or past target
41 | // The iterator is Valid() after this call iff the source contains
42 | // an entry that comes at or past target.
43 | virtual void Seek(const Slice& target) = 0;
44 |
45 | // Moves to the next entry in the source. After this call, Valid() is
46 | // true iff the iterator was not positioned at the last entry in the source.
47 | // REQUIRES: Valid()
48 | virtual void Next() = 0;
49 |
50 | // Moves to the previous entry in the source. After this call, Valid() is
51 | // true iff the iterator was not positioned at the first entry in source.
52 | // REQUIRES: Valid()
53 | virtual void Prev() = 0;
54 |
55 | // Return the key for the current entry. The underlying storage for
56 | // the returned slice is valid only until the next modification of
57 | // the iterator.
58 | // REQUIRES: Valid()
59 | virtual Slice key() const = 0;
60 |
61 | // Return the value for the current entry. The underlying storage for
62 | // the returned slice is valid only until the next modification of
63 | // the iterator.
64 | // REQUIRES: Valid()
65 | virtual Slice value() const = 0;
66 |
67 | // If an error has occurred, return it. Else return an ok status.
68 | virtual Status status() const = 0;
69 |
70 | // Clients are allowed to register function/arg1/arg2 triples that
71 | // will be invoked when this iterator is destroyed.
72 | //
73 | // Note that unlike all of the preceding methods, this method is
74 | // not abstract and therefore clients should not override it.
75 | typedef void (*CleanupFunction)(void* arg1, void* arg2);
76 | void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
77 |
78 | private:
79 | struct Cleanup {
80 | CleanupFunction function;
81 | void* arg1;
82 | void* arg2;
83 | Cleanup* next;
84 | };
85 | Cleanup cleanup_;
86 |
87 | // No copying allowed
88 | Iterator(const Iterator&);
89 | void operator=(const Iterator&);
90 | };
91 |
92 | // Return an empty iterator (yields nothing).
93 | extern Iterator* NewEmptyIterator();
94 |
95 | // Return an empty iterator with the specified status.
96 | extern Iterator* NewErrorIterator(const Status& status);
97 |
98 | } // namespace leveldb
99 |
100 | #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
101 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/slice.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // Slice is a simple structure containing a pointer into some external
6 | // storage and a size. The user of a Slice must ensure that the slice
7 | // is not used after the corresponding external storage has been
8 | // deallocated.
9 | //
10 | // Multiple threads can invoke const methods on a Slice without
11 | // external synchronization, but if any of the threads may call a
12 | // non-const method, all threads accessing the same Slice must use
13 | // external synchronization.
14 |
15 | #ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
16 | #define STORAGE_LEVELDB_INCLUDE_SLICE_H_
17 |
18 | #include
19 | #include
20 | #include
21 | #include
22 |
23 | namespace leveldb {
24 |
25 | class Slice {
26 | public:
27 | // Create an empty slice.
28 | Slice() : data_(""), size_(0) { }
29 |
30 | // Create a slice that refers to d[0,n-1].
31 | Slice(const char* d, size_t n) : data_(d), size_(n) { }
32 |
33 | // Create a slice that refers to the contents of "s"
34 | Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
35 |
36 | // Create a slice that refers to s[0,strlen(s)-1]
37 | Slice(const char* s) : data_(s), size_(strlen(s)) { }
38 |
39 | // Return a pointer to the beginning of the referenced data
40 | const char* data() const { return data_; }
41 |
42 | // Return the length (in bytes) of the referenced data
43 | size_t size() const { return size_; }
44 |
45 | // Return true iff the length of the referenced data is zero
46 | bool empty() const { return size_ == 0; }
47 |
48 | // Return the ith byte in the referenced data.
49 | // REQUIRES: n < size()
50 | char operator[](size_t n) const {
51 | assert(n < size());
52 | return data_[n];
53 | }
54 |
55 | // Change this slice to refer to an empty array
56 | void clear() { data_ = ""; size_ = 0; }
57 |
58 | // Drop the first "n" bytes from this slice.
59 | void remove_prefix(size_t n) {
60 | assert(n <= size());
61 | data_ += n;
62 | size_ -= n;
63 | }
64 |
65 | // Return a string that contains the copy of the referenced data.
66 | std::string ToString() const { return std::string(data_, size_); }
67 |
68 | // Three-way comparison. Returns value:
69 | // < 0 iff "*this" < "b",
70 | // == 0 iff "*this" == "b",
71 | // > 0 iff "*this" > "b"
72 | int compare(const Slice& b) const;
73 |
74 | // Return true iff "x" is a prefix of "*this"
75 | bool starts_with(const Slice& x) const {
76 | return ((size_ >= x.size_) &&
77 | (memcmp(data_, x.data_, x.size_) == 0));
78 | }
79 |
80 | private:
81 | const char* data_;
82 | size_t size_;
83 |
84 | // Intentionally copyable
85 | };
86 |
87 | inline bool operator==(const Slice& x, const Slice& y) {
88 | return ((x.size() == y.size()) &&
89 | (memcmp(x.data(), y.data(), x.size()) == 0));
90 | }
91 |
92 | inline bool operator!=(const Slice& x, const Slice& y) {
93 | return !(x == y);
94 | }
95 |
96 | inline int Slice::compare(const Slice& b) const {
97 | const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
98 | int r = memcmp(data_, b.data_, min_len);
99 | if (r == 0) {
100 | if (size_ < b.size_) r = -1;
101 | else if (size_ > b.size_) r = +1;
102 | }
103 | return r;
104 | }
105 |
106 | } // namespace leveldb
107 |
108 |
109 | #endif // STORAGE_LEVELDB_INCLUDE_SLICE_H_
110 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/status.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // A Status encapsulates the result of an operation. It may indicate success,
6 | // or it may indicate an error with an associated error message.
7 | //
8 | // Multiple threads can invoke const methods on a Status without
9 | // external synchronization, but if any of the threads may call a
10 | // non-const method, all threads accessing the same Status must use
11 | // external synchronization.
12 |
13 | #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
14 | #define STORAGE_LEVELDB_INCLUDE_STATUS_H_
15 |
16 | #include
17 | #include "leveldb/slice.h"
18 |
19 | namespace leveldb {
20 |
21 | class Status {
22 | public:
23 | // Create a success status.
24 | Status() : state_(NULL) { }
25 | ~Status() { delete[] state_; }
26 |
27 | // Copy the specified status.
28 | Status(const Status& s);
29 | void operator=(const Status& s);
30 |
31 | // Return a success status.
32 | static Status OK() { return Status(); }
33 |
34 | // Return error status of an appropriate type.
35 | static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
36 | return Status(kNotFound, msg, msg2);
37 | }
38 | static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
39 | return Status(kCorruption, msg, msg2);
40 | }
41 | static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
42 | return Status(kNotSupported, msg, msg2);
43 | }
44 | static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
45 | return Status(kInvalidArgument, msg, msg2);
46 | }
47 | static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
48 | return Status(kIOError, msg, msg2);
49 | }
50 |
51 | // Returns true iff the status indicates success.
52 | bool ok() const { return (state_ == NULL); }
53 |
54 | // Returns true iff the status indicates a NotFound error.
55 | bool IsNotFound() const { return code() == kNotFound; }
56 |
57 | // Returns true iff the status indicates a Corruption error.
58 | bool IsCorruption() const { return code() == kCorruption; }
59 |
60 | // Returns true iff the status indicates an IOError.
61 | bool IsIOError() const { return code() == kIOError; }
62 |
63 | // Return a string representation of this status suitable for printing.
64 | // Returns the string "OK" for success.
65 | std::string ToString() const;
66 |
67 | private:
68 | // OK status has a NULL state_. Otherwise, state_ is a new[] array
69 | // of the following form:
70 | // state_[0..3] == length of message
71 | // state_[4] == code
72 | // state_[5..] == message
73 | const char* state_;
74 |
75 | enum Code {
76 | kOk = 0,
77 | kNotFound = 1,
78 | kCorruption = 2,
79 | kNotSupported = 3,
80 | kInvalidArgument = 4,
81 | kIOError = 5
82 | };
83 |
84 | Code code() const {
85 | return (state_ == NULL) ? kOk : static_cast(state_[4]);
86 | }
87 |
88 | Status(Code code, const Slice& msg, const Slice& msg2);
89 | static const char* CopyState(const char* s);
90 | };
91 |
92 | inline Status::Status(const Status& s) {
93 | state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
94 | }
95 | inline void Status::operator=(const Status& s) {
96 | // The following condition catches both aliasing (when this == &s),
97 | // and the common case where both s and *this are ok.
98 | if (state_ != s.state_) {
99 | delete[] state_;
100 | state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
101 | }
102 | }
103 |
104 | } // namespace leveldb
105 |
106 | #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_
107 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/table.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_INCLUDE_TABLE_H_
6 | #define STORAGE_LEVELDB_INCLUDE_TABLE_H_
7 |
8 | #include
9 | #include "leveldb/iterator.h"
10 |
11 | namespace leveldb {
12 |
13 | class Block;
14 | class BlockHandle;
15 | class Footer;
16 | struct Options;
17 | class RandomAccessFile;
18 | struct ReadOptions;
19 | class TableCache;
20 |
21 | // A Table is a sorted map from strings to strings. Tables are
22 | // immutable and persistent. A Table may be safely accessed from
23 | // multiple threads without external synchronization.
24 | class Table {
25 | public:
26 | // Attempt to open the table that is stored in bytes [0..file_size)
27 | // of "file", and read the metadata entries necessary to allow
28 | // retrieving data from the table.
29 | //
30 | // If successful, returns ok and sets "*table" to the newly opened
31 | // table. The client should delete "*table" when no longer needed.
32 | // If there was an error while initializing the table, sets "*table"
33 | // to NULL and returns a non-ok status. Does not take ownership of
34 | // "*source", but the client must ensure that "source" remains live
35 | // for the duration of the returned table's lifetime.
36 | //
37 | // *file must remain live while this Table is in use.
38 | static Status Open(const Options& options,
39 | RandomAccessFile* file,
40 | uint64_t file_size,
41 | Table** table);
42 |
43 | ~Table();
44 |
45 | // Returns a new iterator over the table contents.
46 | // The result of NewIterator() is initially invalid (caller must
47 | // call one of the Seek methods on the iterator before using it).
48 | Iterator* NewIterator(const ReadOptions&) const;
49 |
50 | // Given a key, return an approximate byte offset in the file where
51 | // the data for that key begins (or would begin if the key were
52 | // present in the file). The returned value is in terms of file
53 | // bytes, and so includes effects like compression of the underlying data.
54 | // E.g., the approximate offset of the last key in the table will
55 | // be close to the file length.
56 | uint64_t ApproximateOffsetOf(const Slice& key) const;
57 |
58 | private:
59 | struct Rep;
60 | Rep* rep_;
61 |
62 | explicit Table(Rep* rep) { rep_ = rep; }
63 | static Iterator* BlockReader(void*, const ReadOptions&, const Slice&);
64 |
65 | // Calls (*handle_result)(arg, ...) with the entry found after a call
66 | // to Seek(key). May not make such a call if filter policy says
67 | // that key is not present.
68 | friend class TableCache;
69 | Status InternalGet(
70 | const ReadOptions&, const Slice& key,
71 | void* arg,
72 | void (*handle_result)(void* arg, const Slice& k, const Slice& v));
73 |
74 |
75 | void ReadMeta(const Footer& footer);
76 | void ReadFilter(const Slice& filter_handle_value);
77 |
78 | // No copying allowed
79 | Table(const Table&);
80 | void operator=(const Table&);
81 | };
82 |
83 | } // namespace leveldb
84 |
85 | #endif // STORAGE_LEVELDB_INCLUDE_TABLE_H_
86 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/table_builder.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // TableBuilder provides the interface used to build a Table
6 | // (an immutable and sorted map from keys to values).
7 | //
8 | // Multiple threads can invoke const methods on a TableBuilder without
9 | // external synchronization, but if any of the threads may call a
10 | // non-const method, all threads accessing the same TableBuilder must use
11 | // external synchronization.
12 |
13 | #ifndef STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
14 | #define STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
15 |
16 | #include
17 | #include "leveldb/options.h"
18 | #include "leveldb/status.h"
19 |
20 | namespace leveldb {
21 |
22 | class BlockBuilder;
23 | class BlockHandle;
24 | class WritableFile;
25 |
26 | class TableBuilder {
27 | public:
28 | // Create a builder that will store the contents of the table it is
29 | // building in *file. Does not close the file. It is up to the
30 | // caller to close the file after calling Finish().
31 | TableBuilder(const Options& options, WritableFile* file);
32 |
33 | // REQUIRES: Either Finish() or Abandon() has been called.
34 | ~TableBuilder();
35 |
36 | // Change the options used by this builder. Note: only some of the
37 | // option fields can be changed after construction. If a field is
38 | // not allowed to change dynamically and its value in the structure
39 | // passed to the constructor is different from its value in the
40 | // structure passed to this method, this method will return an error
41 | // without changing any fields.
42 | Status ChangeOptions(const Options& options);
43 |
44 | // Add key,value to the table being constructed.
45 | // REQUIRES: key is after any previously added key according to comparator.
46 | // REQUIRES: Finish(), Abandon() have not been called
47 | void Add(const Slice& key, const Slice& value);
48 |
49 | // Advanced operation: flush any buffered key/value pairs to file.
50 | // Can be used to ensure that two adjacent entries never live in
51 | // the same data block. Most clients should not need to use this method.
52 | // REQUIRES: Finish(), Abandon() have not been called
53 | void Flush();
54 |
55 | // Return non-ok iff some error has been detected.
56 | Status status() const;
57 |
58 | // Finish building the table. Stops using the file passed to the
59 | // constructor after this function returns.
60 | // REQUIRES: Finish(), Abandon() have not been called
61 | Status Finish();
62 |
63 | // Indicate that the contents of this builder should be abandoned. Stops
64 | // using the file passed to the constructor after this function returns.
65 | // If the caller is not going to call Finish(), it must call Abandon()
66 | // before destroying this builder.
67 | // REQUIRES: Finish(), Abandon() have not been called
68 | void Abandon();
69 |
70 | // Number of calls to Add() so far.
71 | uint64_t NumEntries() const;
72 |
73 | // Size of the file generated so far. If invoked after a successful
74 | // Finish() call, returns the size of the final generated file.
75 | uint64_t FileSize() const;
76 |
77 | private:
78 | bool ok() const { return status().ok(); }
79 | void WriteBlock(BlockBuilder* block, BlockHandle* handle);
80 | void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle);
81 |
82 | struct Rep;
83 | Rep* rep_;
84 |
85 | // No copying allowed
86 | TableBuilder(const TableBuilder&);
87 | void operator=(const TableBuilder&);
88 | };
89 |
90 | } // namespace leveldb
91 |
92 | #endif // STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
93 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/leveldb/write_batch.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // WriteBatch holds a collection of updates to apply atomically to a DB.
6 | //
7 | // The updates are applied in the order in which they are added
8 | // to the WriteBatch. For example, the value of "key" will be "v3"
9 | // after the following batch is written:
10 | //
11 | // batch.Put("key", "v1");
12 | // batch.Delete("key");
13 | // batch.Put("key", "v2");
14 | // batch.Put("key", "v3");
15 | //
16 | // Multiple threads can invoke const methods on a WriteBatch without
17 | // external synchronization, but if any of the threads may call a
18 | // non-const method, all threads accessing the same WriteBatch must use
19 | // external synchronization.
20 |
21 | #ifndef STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
22 | #define STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
23 |
24 | #include
25 | #include "leveldb/status.h"
26 |
27 | namespace leveldb {
28 |
29 | class Slice;
30 |
31 | class WriteBatch {
32 | public:
33 | WriteBatch();
34 | ~WriteBatch();
35 |
36 | // Store the mapping "key->value" in the database.
37 | void Put(const Slice& key, const Slice& value);
38 |
39 | // If the database contains a mapping for "key", erase it. Else do nothing.
40 | void Delete(const Slice& key);
41 |
42 | // Clear all updates buffered in this batch.
43 | void Clear();
44 |
45 | // Support for iterating over the contents of a batch.
46 | class Handler {
47 | public:
48 | virtual ~Handler();
49 | virtual void Put(const Slice& key, const Slice& value) = 0;
50 | virtual void Delete(const Slice& key) = 0;
51 | };
52 | Status Iterate(Handler* handler) const;
53 |
54 | private:
55 | friend class WriteBatchInternal;
56 |
57 | std::string rep_; // See comment in write_batch.cc for the format of rep_
58 |
59 | // Intentionally copyable
60 | };
61 |
62 | } // namespace leveldb
63 |
64 | #endif // STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
65 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/port/README:
--------------------------------------------------------------------------------
1 | This directory contains interfaces and implementations that isolate the
2 | rest of the package from platform details.
3 |
4 | Code in the rest of the package includes "port.h" from this directory.
5 | "port.h" in turn includes a platform specific "port_.h" file
6 | that provides the platform specific implementation.
7 |
8 | See port_posix.h for an example of what must be provided in a platform
9 | specific header file.
10 |
11 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/port/port.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_PORT_PORT_H_
6 | #define STORAGE_LEVELDB_PORT_PORT_H_
7 |
8 | #include
9 |
10 | // Include the appropriate platform specific file below. If you are
11 | // porting to a new platform, see "port_example.h" for documentation
12 | // of what the new port_.h file must provide.
13 | #if defined(LEVELDB_PLATFORM_POSIX)
14 | # include "port/port_posix.h"
15 | #elif defined(LEVELDB_PLATFORM_CHROMIUM)
16 | # include "port/port_chromium.h"
17 | #elif defined(LEVELDB_PLATFORM_WINDOWS)
18 | # include "port/port_win.h"
19 | #endif
20 |
21 | #endif // STORAGE_LEVELDB_PORT_PORT_H_
22 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/port/port_posix.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "port/port_posix.h"
6 |
7 | #include
8 | #include
9 | #include
10 | #include "util/logging.h"
11 |
12 | namespace leveldb {
13 | namespace port {
14 |
15 | static void PthreadCall(const char* label, int result) {
16 | if (result != 0) {
17 | fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
18 | abort();
19 | }
20 | }
21 |
22 | Mutex::Mutex() { PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL)); }
23 |
24 | Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
25 |
26 | void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_)); }
27 |
28 | void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_)); }
29 |
30 | CondVar::CondVar(Mutex* mu)
31 | : mu_(mu) {
32 | PthreadCall("init cv", pthread_cond_init(&cv_, NULL));
33 | }
34 |
35 | CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
36 |
37 | void CondVar::Wait() {
38 | PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
39 | }
40 |
41 | void CondVar::Signal() {
42 | PthreadCall("signal", pthread_cond_signal(&cv_));
43 | }
44 |
45 | void CondVar::SignalAll() {
46 | PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
47 | }
48 |
49 | void InitOnce(OnceType* once, void (*initializer)()) {
50 | PthreadCall("once", pthread_once(once, initializer));
51 | }
52 |
53 | } // namespace port
54 | } // namespace leveldb
55 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/port/thread_annotations.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_PORT_THREAD_ANNOTATIONS_H_
6 | #define STORAGE_LEVELDB_PORT_THREAD_ANNOTATIONS_H_
7 |
8 | // Some environments provide custom macros to aid in static thread-safety
9 | // analysis. Provide empty definitions of such macros unless they are already
10 | // defined.
11 |
12 | #ifndef EXCLUSIVE_LOCKS_REQUIRED
13 | #define EXCLUSIVE_LOCKS_REQUIRED(...)
14 | #endif
15 |
16 | #ifndef SHARED_LOCKS_REQUIRED
17 | #define SHARED_LOCKS_REQUIRED(...)
18 | #endif
19 |
20 | #ifndef LOCKS_EXCLUDED
21 | #define LOCKS_EXCLUDED(...)
22 | #endif
23 |
24 | #ifndef LOCK_RETURNED
25 | #define LOCK_RETURNED(x)
26 | #endif
27 |
28 | #ifndef LOCKABLE
29 | #define LOCKABLE
30 | #endif
31 |
32 | #ifndef SCOPED_LOCKABLE
33 | #define SCOPED_LOCKABLE
34 | #endif
35 |
36 | #ifndef EXCLUSIVE_LOCK_FUNCTION
37 | #define EXCLUSIVE_LOCK_FUNCTION(...)
38 | #endif
39 |
40 | #ifndef SHARED_LOCK_FUNCTION
41 | #define SHARED_LOCK_FUNCTION(...)
42 | #endif
43 |
44 | #ifndef EXCLUSIVE_TRYLOCK_FUNCTION
45 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...)
46 | #endif
47 |
48 | #ifndef SHARED_TRYLOCK_FUNCTION
49 | #define SHARED_TRYLOCK_FUNCTION(...)
50 | #endif
51 |
52 | #ifndef UNLOCK_FUNCTION
53 | #define UNLOCK_FUNCTION(...)
54 | #endif
55 |
56 | #ifndef NO_THREAD_SAFETY_ANALYSIS
57 | #define NO_THREAD_SAFETY_ANALYSIS
58 | #endif
59 |
60 | #endif // STORAGE_LEVELDB_PORT_THREAD_ANNOTATIONS_H_
61 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/port/win/stdint.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | // MSVC didn't ship with this file until the 2010 version.
6 |
7 | #ifndef STORAGE_LEVELDB_PORT_WIN_STDINT_H_
8 | #define STORAGE_LEVELDB_PORT_WIN_STDINT_H_
9 |
10 | #if !defined(_MSC_VER)
11 | #error This file should only be included when compiling with MSVC.
12 | #endif
13 |
14 | // Define C99 equivalent types.
15 | typedef signed char int8_t;
16 | typedef signed short int16_t;
17 | typedef signed int int32_t;
18 | typedef signed long long int64_t;
19 | typedef unsigned char uint8_t;
20 | typedef unsigned short uint16_t;
21 | typedef unsigned int uint32_t;
22 | typedef unsigned long long uint64_t;
23 |
24 | #endif // STORAGE_LEVELDB_PORT_WIN_STDINT_H_
25 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/snappy/snappy-c.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2011 Martin Gieseking .
2 | //
3 | // Redistribution and use in source and binary forms, with or without
4 | // modification, are permitted provided that the following conditions are
5 | // met:
6 | //
7 | // * Redistributions of source code must retain the above copyright
8 | // notice, this list of conditions and the following disclaimer.
9 | // * Redistributions in binary form must reproduce the above
10 | // copyright notice, this list of conditions and the following disclaimer
11 | // in the documentation and/or other materials provided with the
12 | // distribution.
13 | // * Neither the name of Google Inc. nor the names of its
14 | // contributors may be used to endorse or promote products derived from
15 | // this software without specific prior written permission.
16 | //
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
29 | #include "snappy.h"
30 | #include "snappy-c.h"
31 |
32 | extern "C" {
33 |
34 | snappy_status snappy_compress(const char* input,
35 | size_t input_length,
36 | char* compressed,
37 | size_t *compressed_length) {
38 | if (*compressed_length < snappy_max_compressed_length(input_length)) {
39 | return SNAPPY_BUFFER_TOO_SMALL;
40 | }
41 | snappy::RawCompress(input, input_length, compressed, compressed_length);
42 | return SNAPPY_OK;
43 | }
44 |
45 | snappy_status snappy_uncompress(const char* compressed,
46 | size_t compressed_length,
47 | char* uncompressed,
48 | size_t* uncompressed_length) {
49 | size_t real_uncompressed_length;
50 | if (!snappy::GetUncompressedLength(compressed,
51 | compressed_length,
52 | &real_uncompressed_length)) {
53 | return SNAPPY_INVALID_INPUT;
54 | }
55 | if (*uncompressed_length < real_uncompressed_length) {
56 | return SNAPPY_BUFFER_TOO_SMALL;
57 | }
58 | if (!snappy::RawUncompress(compressed, compressed_length, uncompressed)) {
59 | return SNAPPY_INVALID_INPUT;
60 | }
61 | *uncompressed_length = real_uncompressed_length;
62 | return SNAPPY_OK;
63 | }
64 |
65 | size_t snappy_max_compressed_length(size_t source_length) {
66 | return snappy::MaxCompressedLength(source_length);
67 | }
68 |
69 | snappy_status snappy_uncompressed_length(const char *compressed,
70 | size_t compressed_length,
71 | size_t *result) {
72 | if (snappy::GetUncompressedLength(compressed,
73 | compressed_length,
74 | result)) {
75 | return SNAPPY_OK;
76 | } else {
77 | return SNAPPY_INVALID_INPUT;
78 | }
79 | }
80 |
81 | snappy_status snappy_validate_compressed_buffer(const char *compressed,
82 | size_t compressed_length) {
83 | if (snappy::IsValidCompressedBuffer(compressed, compressed_length)) {
84 | return SNAPPY_OK;
85 | } else {
86 | return SNAPPY_INVALID_INPUT;
87 | }
88 | }
89 |
90 | } // extern "C"
91 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/snappy/snappy-sinksource.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2011 Google Inc. All Rights Reserved.
2 | //
3 | // Redistribution and use in source and binary forms, with or without
4 | // modification, are permitted provided that the following conditions are
5 | // met:
6 | //
7 | // * Redistributions of source code must retain the above copyright
8 | // notice, this list of conditions and the following disclaimer.
9 | // * Redistributions in binary form must reproduce the above
10 | // copyright notice, this list of conditions and the following disclaimer
11 | // in the documentation and/or other materials provided with the
12 | // distribution.
13 | // * Neither the name of Google Inc. nor the names of its
14 | // contributors may be used to endorse or promote products derived from
15 | // this software without specific prior written permission.
16 | //
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
29 | #include
30 |
31 | #include "snappy-sinksource.h"
32 |
33 | namespace snappy {
34 |
35 | Source::~Source() { }
36 |
37 | Sink::~Sink() { }
38 |
39 | char* Sink::GetAppendBuffer(size_t length, char* scratch) {
40 | return scratch;
41 | }
42 |
43 | char* Sink::GetAppendBufferVariable(
44 | size_t min_size, size_t desired_size_hint, char* scratch,
45 | size_t scratch_size, size_t* allocated_size) {
46 | *allocated_size = scratch_size;
47 | return scratch;
48 | }
49 |
50 | void Sink::AppendAndTakeOwnership(
51 | char* bytes, size_t n,
52 | void (*deleter)(void*, const char*, size_t),
53 | void *deleter_arg) {
54 | Append(bytes, n);
55 | (*deleter)(deleter_arg, bytes, n);
56 | }
57 |
58 | ByteArraySource::~ByteArraySource() { }
59 |
60 | size_t ByteArraySource::Available() const { return left_; }
61 |
62 | const char* ByteArraySource::Peek(size_t* len) {
63 | *len = left_;
64 | return ptr_;
65 | }
66 |
67 | void ByteArraySource::Skip(size_t n) {
68 | left_ -= n;
69 | ptr_ += n;
70 | }
71 |
72 | UncheckedByteArraySink::~UncheckedByteArraySink() { }
73 |
74 | void UncheckedByteArraySink::Append(const char* data, size_t n) {
75 | // Do no copying if the caller filled in the result of GetAppendBuffer()
76 | if (data != dest_) {
77 | memcpy(dest_, data, n);
78 | }
79 | dest_ += n;
80 | }
81 |
82 | char* UncheckedByteArraySink::GetAppendBuffer(size_t len, char* scratch) {
83 | return dest_;
84 | }
85 |
86 | void UncheckedByteArraySink::AppendAndTakeOwnership(
87 | char* data, size_t n,
88 | void (*deleter)(void*, const char*, size_t),
89 | void *deleter_arg) {
90 | if (data != dest_) {
91 | memcpy(dest_, data, n);
92 | (*deleter)(deleter_arg, data, n);
93 | }
94 | dest_ += n;
95 | }
96 |
97 | char* UncheckedByteArraySink::GetAppendBufferVariable(
98 | size_t min_size, size_t desired_size_hint, char* scratch,
99 | size_t scratch_size, size_t* allocated_size) {
100 | *allocated_size = desired_size_hint;
101 | return dest_;
102 | }
103 |
104 | } // namespace snappy
105 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/snappy/snappy-stubs-internal.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2011 Google Inc. All Rights Reserved.
2 | //
3 | // Redistribution and use in source and binary forms, with or without
4 | // modification, are permitted provided that the following conditions are
5 | // met:
6 | //
7 | // * Redistributions of source code must retain the above copyright
8 | // notice, this list of conditions and the following disclaimer.
9 | // * Redistributions in binary form must reproduce the above
10 | // copyright notice, this list of conditions and the following disclaimer
11 | // in the documentation and/or other materials provided with the
12 | // distribution.
13 | // * Neither the name of Google Inc. nor the names of its
14 | // contributors may be used to endorse or promote products derived from
15 | // this software without specific prior written permission.
16 | //
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
29 | #include
30 | #include
31 |
32 | #include "snappy-stubs-internal.h"
33 |
34 | namespace snappy {
35 |
36 | void Varint::Append32(string* s, uint32 value) {
37 | char buf[Varint::kMax32];
38 | const char* p = Varint::Encode32(buf, value);
39 | s->append(buf, p - buf);
40 | }
41 |
42 | } // namespace snappy
43 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/snappy/snappy-stubs-public.h:
--------------------------------------------------------------------------------
1 | // Copyright 2011 Google Inc. All Rights Reserved.
2 | // Author: sesse@google.com (Steinar H. Gunderson)
3 | //
4 | // Redistribution and use in source and binary forms, with or without
5 | // modification, are permitted provided that the following conditions are
6 | // met:
7 | //
8 | // * Redistributions of source code must retain the above copyright
9 | // notice, this list of conditions and the following disclaimer.
10 | // * Redistributions in binary form must reproduce the above
11 | // copyright notice, this list of conditions and the following disclaimer
12 | // in the documentation and/or other materials provided with the
13 | // distribution.
14 | // * Neither the name of Google Inc. nor the names of its
15 | // contributors may be used to endorse or promote products derived from
16 | // this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 | //
30 | // Various type stubs for the open-source version of Snappy.
31 | //
32 | // This file cannot include config.h, as it is included from snappy.h,
33 | // which is a public header. Instead, snappy-stubs-public.h is generated by
34 | // from snappy-stubs-public.h.in at configure time.
35 |
36 | #ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
37 | #define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
38 |
39 | #include
40 | #include
41 |
42 | // ssize_t defination for Visual Studio
43 | #if defined(_MSC_VER)
44 | #include
45 | typedef SSIZE_T ssize_t;
46 | #endif
47 |
48 | #define SNAPPY_MAJOR @SNAPPY_MAJOR@
49 | #define SNAPPY_MINOR @SNAPPY_MINOR@
50 | #define SNAPPY_PATCHLEVEL @SNAPPY_PATCHLEVEL@
51 | #define SNAPPY_VERSION \
52 | ((SNAPPY_MAJOR << 16) | (SNAPPY_MINOR << 8) | SNAPPY_PATCHLEVEL)
53 |
54 | #include
55 |
56 | namespace snappy {
57 |
58 | typedef int8_t int8;
59 | typedef uint8_t uint8;
60 | typedef int16_t int16;
61 | typedef uint16_t uint16;
62 | typedef int32_t int32;
63 | typedef uint32_t uint32;
64 | typedef int64_t int64;
65 | typedef uint64_t uint64;
66 |
67 | typedef std::string string;
68 |
69 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
70 | TypeName(const TypeName&); \
71 | void operator=(const TypeName&)
72 |
73 | struct iovec {
74 | void* iov_base;
75 | size_t iov_len;
76 | };
77 |
78 | } // namespace snappy
79 |
80 | #endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
81 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/block.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_TABLE_BLOCK_H_
6 | #define STORAGE_LEVELDB_TABLE_BLOCK_H_
7 |
8 | #include
9 | #include
10 | #include "leveldb/iterator.h"
11 |
12 | namespace leveldb {
13 |
14 | struct BlockContents;
15 | class Comparator;
16 |
17 | class Block {
18 | public:
19 | // Initialize the block with the specified contents.
20 | explicit Block(const BlockContents& contents);
21 |
22 | ~Block();
23 |
24 | size_t size() const { return size_; }
25 | Iterator* NewIterator(const Comparator* comparator);
26 |
27 | private:
28 | uint32_t NumRestarts() const;
29 |
30 | const char* data_;
31 | size_t size_;
32 | uint32_t restart_offset_; // Offset in data_ of restart array
33 | bool owned_; // Block owns data_[]
34 |
35 | // No copying allowed
36 | Block(const Block&);
37 | void operator=(const Block&);
38 |
39 | class Iter;
40 | };
41 |
42 | } // namespace leveldb
43 |
44 | #endif // STORAGE_LEVELDB_TABLE_BLOCK_H_
45 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/block_builder.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // BlockBuilder generates blocks where keys are prefix-compressed:
6 | //
7 | // When we store a key, we drop the prefix shared with the previous
8 | // string. This helps reduce the space requirement significantly.
9 | // Furthermore, once every K keys, we do not apply the prefix
10 | // compression and store the entire key. We call this a "restart
11 | // point". The tail end of the block stores the offsets of all of the
12 | // restart points, and can be used to do a binary search when looking
13 | // for a particular key. Values are stored as-is (without compression)
14 | // immediately following the corresponding key.
15 | //
16 | // An entry for a particular key-value pair has the form:
17 | // shared_bytes: varint32
18 | // unshared_bytes: varint32
19 | // value_length: varint32
20 | // key_delta: char[unshared_bytes]
21 | // value: char[value_length]
22 | // shared_bytes == 0 for restart points.
23 | //
24 | // The trailer of the block has the form:
25 | // restarts: uint32[num_restarts]
26 | // num_restarts: uint32
27 | // restarts[i] contains the offset within the block of the ith restart point.
28 |
29 | #include "table/block_builder.h"
30 |
31 | #include
32 | #include
33 | #include "leveldb/comparator.h"
34 | #include "leveldb/table_builder.h"
35 | #include "util/coding.h"
36 |
37 | namespace leveldb {
38 |
39 | BlockBuilder::BlockBuilder(const Options* options)
40 | : options_(options),
41 | restarts_(),
42 | counter_(0),
43 | finished_(false) {
44 | assert(options->block_restart_interval >= 1);
45 | restarts_.push_back(0); // First restart point is at offset 0
46 | }
47 |
48 | void BlockBuilder::Reset() {
49 | buffer_.clear();
50 | restarts_.clear();
51 | restarts_.push_back(0); // First restart point is at offset 0
52 | counter_ = 0;
53 | finished_ = false;
54 | last_key_.clear();
55 | }
56 |
57 | size_t BlockBuilder::CurrentSizeEstimate() const {
58 | return (buffer_.size() + // Raw data buffer
59 | restarts_.size() * sizeof(uint32_t) + // Restart array
60 | sizeof(uint32_t)); // Restart array length
61 | }
62 |
63 | Slice BlockBuilder::Finish() {
64 | // Append restart array
65 | for (size_t i = 0; i < restarts_.size(); i++) {
66 | PutFixed32(&buffer_, restarts_[i]);
67 | }
68 | PutFixed32(&buffer_, restarts_.size());
69 | finished_ = true;
70 | return Slice(buffer_);
71 | }
72 |
73 | void BlockBuilder::Add(const Slice& key, const Slice& value) {
74 | Slice last_key_piece(last_key_);
75 | assert(!finished_);
76 | assert(counter_ <= options_->block_restart_interval);
77 | assert(buffer_.empty() // No values yet?
78 | || options_->comparator->Compare(key, last_key_piece) > 0);
79 | size_t shared = 0;
80 | if (counter_ < options_->block_restart_interval) {
81 | // See how much sharing to do with previous string
82 | const size_t min_length = std::min(last_key_piece.size(), key.size());
83 | while ((shared < min_length) && (last_key_piece[shared] == key[shared])) {
84 | shared++;
85 | }
86 | } else {
87 | // Restart compression
88 | restarts_.push_back(buffer_.size());
89 | counter_ = 0;
90 | }
91 | const size_t non_shared = key.size() - shared;
92 |
93 | // Add "" to buffer_
94 | PutVarint32(&buffer_, shared);
95 | PutVarint32(&buffer_, non_shared);
96 | PutVarint32(&buffer_, value.size());
97 |
98 | // Add string delta to buffer_ followed by value
99 | buffer_.append(key.data() + shared, non_shared);
100 | buffer_.append(value.data(), value.size());
101 |
102 | // Update state
103 | last_key_.resize(shared);
104 | last_key_.append(key.data() + shared, non_shared);
105 | assert(Slice(last_key_) == key);
106 | counter_++;
107 | }
108 |
109 | } // namespace leveldb
110 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/block_builder.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_
6 | #define STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_
7 |
8 | #include
9 |
10 | #include
11 | #include "leveldb/slice.h"
12 |
13 | namespace leveldb {
14 |
15 | struct Options;
16 |
17 | class BlockBuilder {
18 | public:
19 | explicit BlockBuilder(const Options* options);
20 |
21 | // Reset the contents as if the BlockBuilder was just constructed.
22 | void Reset();
23 |
24 | // REQUIRES: Finish() has not been called since the last call to Reset().
25 | // REQUIRES: key is larger than any previously added key
26 | void Add(const Slice& key, const Slice& value);
27 |
28 | // Finish building the block and return a slice that refers to the
29 | // block contents. The returned slice will remain valid for the
30 | // lifetime of this builder or until Reset() is called.
31 | Slice Finish();
32 |
33 | // Returns an estimate of the current (uncompressed) size of the block
34 | // we are building.
35 | size_t CurrentSizeEstimate() const;
36 |
37 | // Return true iff no entries have been added since the last Reset()
38 | bool empty() const {
39 | return buffer_.empty();
40 | }
41 |
42 | private:
43 | const Options* options_;
44 | std::string buffer_; // Destination buffer
45 | std::vector restarts_; // Restart points
46 | int counter_; // Number of entries emitted since restart
47 | bool finished_; // Has Finish() been called?
48 | std::string last_key_;
49 |
50 | // No copying allowed
51 | BlockBuilder(const BlockBuilder&);
52 | void operator=(const BlockBuilder&);
53 | };
54 |
55 | } // namespace leveldb
56 |
57 | #endif // STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_
58 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/filter_block.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "table/filter_block.h"
6 |
7 | #include "leveldb/filter_policy.h"
8 | #include "util/coding.h"
9 |
10 | namespace leveldb {
11 |
12 | // See doc/table_format.txt for an explanation of the filter block format.
13 |
14 | // Generate new filter every 2KB of data
15 | static const size_t kFilterBaseLg = 11;
16 | static const size_t kFilterBase = 1 << kFilterBaseLg;
17 |
18 | FilterBlockBuilder::FilterBlockBuilder(const FilterPolicy* policy)
19 | : policy_(policy) {
20 | }
21 |
22 | void FilterBlockBuilder::StartBlock(uint64_t block_offset) {
23 | uint64_t filter_index = (block_offset / kFilterBase);
24 | assert(filter_index >= filter_offsets_.size());
25 | while (filter_index > filter_offsets_.size()) {
26 | GenerateFilter();
27 | }
28 | }
29 |
30 | void FilterBlockBuilder::AddKey(const Slice& key) {
31 | Slice k = key;
32 | start_.push_back(keys_.size());
33 | keys_.append(k.data(), k.size());
34 | }
35 |
36 | Slice FilterBlockBuilder::Finish() {
37 | if (!start_.empty()) {
38 | GenerateFilter();
39 | }
40 |
41 | // Append array of per-filter offsets
42 | const uint32_t array_offset = result_.size();
43 | for (size_t i = 0; i < filter_offsets_.size(); i++) {
44 | PutFixed32(&result_, filter_offsets_[i]);
45 | }
46 |
47 | PutFixed32(&result_, array_offset);
48 | result_.push_back(kFilterBaseLg); // Save encoding parameter in result
49 | return Slice(result_);
50 | }
51 |
52 | void FilterBlockBuilder::GenerateFilter() {
53 | const size_t num_keys = start_.size();
54 | if (num_keys == 0) {
55 | // Fast path if there are no keys for this filter
56 | filter_offsets_.push_back(result_.size());
57 | return;
58 | }
59 |
60 | // Make list of keys from flattened key structure
61 | start_.push_back(keys_.size()); // Simplify length computation
62 | tmp_keys_.resize(num_keys);
63 | for (size_t i = 0; i < num_keys; i++) {
64 | const char* base = keys_.data() + start_[i];
65 | size_t length = start_[i+1] - start_[i];
66 | tmp_keys_[i] = Slice(base, length);
67 | }
68 |
69 | // Generate filter for current set of keys and append to result_.
70 | filter_offsets_.push_back(result_.size());
71 | policy_->CreateFilter(&tmp_keys_[0], num_keys, &result_);
72 |
73 | tmp_keys_.clear();
74 | keys_.clear();
75 | start_.clear();
76 | }
77 |
78 | FilterBlockReader::FilterBlockReader(const FilterPolicy* policy,
79 | const Slice& contents)
80 | : policy_(policy),
81 | data_(NULL),
82 | offset_(NULL),
83 | num_(0),
84 | base_lg_(0) {
85 | size_t n = contents.size();
86 | if (n < 5) return; // 1 byte for base_lg_ and 4 for start of offset array
87 | base_lg_ = contents[n-1];
88 | uint32_t last_word = DecodeFixed32(contents.data() + n - 5);
89 | if (last_word > n - 5) return;
90 | data_ = contents.data();
91 | offset_ = data_ + last_word;
92 | num_ = (n - 5 - last_word) / 4;
93 | }
94 |
95 | bool FilterBlockReader::KeyMayMatch(uint64_t block_offset, const Slice& key) {
96 | uint64_t index = block_offset >> base_lg_;
97 | if (index < num_) {
98 | uint32_t start = DecodeFixed32(offset_ + index*4);
99 | uint32_t limit = DecodeFixed32(offset_ + index*4 + 4);
100 | if (start <= limit && limit <= (offset_ - data_)) {
101 | Slice filter = Slice(data_ + start, limit - start);
102 | return policy_->KeyMayMatch(key, filter);
103 | } else if (start == limit) {
104 | // Empty filters do not match any keys
105 | return false;
106 | }
107 | }
108 | return true; // Errors are treated as potential matches
109 | }
110 |
111 | }
112 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/filter_block.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // A filter block is stored near the end of a Table file. It contains
6 | // filters (e.g., bloom filters) for all data blocks in the table combined
7 | // into a single filter block.
8 |
9 | #ifndef STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
10 | #define STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
11 |
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include "leveldb/slice.h"
17 | #include "util/hash.h"
18 |
19 | namespace leveldb {
20 |
21 | class FilterPolicy;
22 |
23 | // A FilterBlockBuilder is used to construct all of the filters for a
24 | // particular Table. It generates a single string which is stored as
25 | // a special block in the Table.
26 | //
27 | // The sequence of calls to FilterBlockBuilder must match the regexp:
28 | // (StartBlock AddKey*)* Finish
29 | class FilterBlockBuilder {
30 | public:
31 | explicit FilterBlockBuilder(const FilterPolicy*);
32 |
33 | void StartBlock(uint64_t block_offset);
34 | void AddKey(const Slice& key);
35 | Slice Finish();
36 |
37 | private:
38 | void GenerateFilter();
39 |
40 | const FilterPolicy* policy_;
41 | std::string keys_; // Flattened key contents
42 | std::vector start_; // Starting index in keys_ of each key
43 | std::string result_; // Filter data computed so far
44 | std::vector tmp_keys_; // policy_->CreateFilter() argument
45 | std::vector filter_offsets_;
46 |
47 | // No copying allowed
48 | FilterBlockBuilder(const FilterBlockBuilder&);
49 | void operator=(const FilterBlockBuilder&);
50 | };
51 |
52 | class FilterBlockReader {
53 | public:
54 | // REQUIRES: "contents" and *policy must stay live while *this is live.
55 | FilterBlockReader(const FilterPolicy* policy, const Slice& contents);
56 | bool KeyMayMatch(uint64_t block_offset, const Slice& key);
57 |
58 | private:
59 | const FilterPolicy* policy_;
60 | const char* data_; // Pointer to filter data (at block-start)
61 | const char* offset_; // Pointer to beginning of offset array (at block-end)
62 | size_t num_; // Number of entries in offset array
63 | size_t base_lg_; // Encoding parameter (see kFilterBaseLg in .cc file)
64 | };
65 |
66 | }
67 |
68 | #endif // STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_
69 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/filter_block_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "table/filter_block.h"
6 |
7 | #include "leveldb/filter_policy.h"
8 | #include "util/coding.h"
9 | #include "util/hash.h"
10 | #include "util/logging.h"
11 | #include "util/testharness.h"
12 | #include "util/testutil.h"
13 |
14 | namespace leveldb {
15 |
16 | // For testing: emit an array with one hash value per key
17 | class TestHashFilter : public FilterPolicy {
18 | public:
19 | virtual const char* Name() const {
20 | return "TestHashFilter";
21 | }
22 |
23 | virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const {
24 | for (int i = 0; i < n; i++) {
25 | uint32_t h = Hash(keys[i].data(), keys[i].size(), 1);
26 | PutFixed32(dst, h);
27 | }
28 | }
29 |
30 | virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const {
31 | uint32_t h = Hash(key.data(), key.size(), 1);
32 | for (size_t i = 0; i + 4 <= filter.size(); i += 4) {
33 | if (h == DecodeFixed32(filter.data() + i)) {
34 | return true;
35 | }
36 | }
37 | return false;
38 | }
39 | };
40 |
41 | class FilterBlockTest {
42 | public:
43 | TestHashFilter policy_;
44 | };
45 |
46 | TEST(FilterBlockTest, EmptyBuilder) {
47 | FilterBlockBuilder builder(&policy_);
48 | Slice block = builder.Finish();
49 | ASSERT_EQ("\\x00\\x00\\x00\\x00\\x0b", EscapeString(block));
50 | FilterBlockReader reader(&policy_, block);
51 | ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));
52 | ASSERT_TRUE(reader.KeyMayMatch(100000, "foo"));
53 | }
54 |
55 | TEST(FilterBlockTest, SingleChunk) {
56 | FilterBlockBuilder builder(&policy_);
57 | builder.StartBlock(100);
58 | builder.AddKey("foo");
59 | builder.AddKey("bar");
60 | builder.AddKey("box");
61 | builder.StartBlock(200);
62 | builder.AddKey("box");
63 | builder.StartBlock(300);
64 | builder.AddKey("hello");
65 | Slice block = builder.Finish();
66 | FilterBlockReader reader(&policy_, block);
67 | ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));
68 | ASSERT_TRUE(reader.KeyMayMatch(100, "bar"));
69 | ASSERT_TRUE(reader.KeyMayMatch(100, "box"));
70 | ASSERT_TRUE(reader.KeyMayMatch(100, "hello"));
71 | ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));
72 | ASSERT_TRUE(! reader.KeyMayMatch(100, "missing"));
73 | ASSERT_TRUE(! reader.KeyMayMatch(100, "other"));
74 | }
75 |
76 | TEST(FilterBlockTest, MultiChunk) {
77 | FilterBlockBuilder builder(&policy_);
78 |
79 | // First filter
80 | builder.StartBlock(0);
81 | builder.AddKey("foo");
82 | builder.StartBlock(2000);
83 | builder.AddKey("bar");
84 |
85 | // Second filter
86 | builder.StartBlock(3100);
87 | builder.AddKey("box");
88 |
89 | // Third filter is empty
90 |
91 | // Last filter
92 | builder.StartBlock(9000);
93 | builder.AddKey("box");
94 | builder.AddKey("hello");
95 |
96 | Slice block = builder.Finish();
97 | FilterBlockReader reader(&policy_, block);
98 |
99 | // Check first filter
100 | ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));
101 | ASSERT_TRUE(reader.KeyMayMatch(2000, "bar"));
102 | ASSERT_TRUE(! reader.KeyMayMatch(0, "box"));
103 | ASSERT_TRUE(! reader.KeyMayMatch(0, "hello"));
104 |
105 | // Check second filter
106 | ASSERT_TRUE(reader.KeyMayMatch(3100, "box"));
107 | ASSERT_TRUE(! reader.KeyMayMatch(3100, "foo"));
108 | ASSERT_TRUE(! reader.KeyMayMatch(3100, "bar"));
109 | ASSERT_TRUE(! reader.KeyMayMatch(3100, "hello"));
110 |
111 | // Check third filter (empty)
112 | ASSERT_TRUE(! reader.KeyMayMatch(4100, "foo"));
113 | ASSERT_TRUE(! reader.KeyMayMatch(4100, "bar"));
114 | ASSERT_TRUE(! reader.KeyMayMatch(4100, "box"));
115 | ASSERT_TRUE(! reader.KeyMayMatch(4100, "hello"));
116 |
117 | // Check last filter
118 | ASSERT_TRUE(reader.KeyMayMatch(9000, "box"));
119 | ASSERT_TRUE(reader.KeyMayMatch(9000, "hello"));
120 | ASSERT_TRUE(! reader.KeyMayMatch(9000, "foo"));
121 | ASSERT_TRUE(! reader.KeyMayMatch(9000, "bar"));
122 | }
123 |
124 | } // namespace leveldb
125 |
126 | int main(int argc, char** argv) {
127 | return leveldb::test::RunAllTests();
128 | }
129 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/format.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_TABLE_FORMAT_H_
6 | #define STORAGE_LEVELDB_TABLE_FORMAT_H_
7 |
8 | #include
9 | #include
10 | #include "leveldb/slice.h"
11 | #include "leveldb/status.h"
12 | #include "leveldb/table_builder.h"
13 |
14 | namespace leveldb {
15 |
16 | class Block;
17 | class RandomAccessFile;
18 | struct ReadOptions;
19 |
20 | // BlockHandle is a pointer to the extent of a file that stores a data
21 | // block or a meta block.
22 | class BlockHandle {
23 | public:
24 | BlockHandle();
25 |
26 | // The offset of the block in the file.
27 | uint64_t offset() const { return offset_; }
28 | void set_offset(uint64_t offset) { offset_ = offset; }
29 |
30 | // The size of the stored block
31 | uint64_t size() const { return size_; }
32 | void set_size(uint64_t size) { size_ = size; }
33 |
34 | void EncodeTo(std::string* dst) const;
35 | Status DecodeFrom(Slice* input);
36 |
37 | // Maximum encoding length of a BlockHandle
38 | enum { kMaxEncodedLength = 10 + 10 };
39 |
40 | private:
41 | uint64_t offset_;
42 | uint64_t size_;
43 | };
44 |
45 | // Footer encapsulates the fixed information stored at the tail
46 | // end of every table file.
47 | class Footer {
48 | public:
49 | Footer() { }
50 |
51 | // The block handle for the metaindex block of the table
52 | const BlockHandle& metaindex_handle() const { return metaindex_handle_; }
53 | void set_metaindex_handle(const BlockHandle& h) { metaindex_handle_ = h; }
54 |
55 | // The block handle for the index block of the table
56 | const BlockHandle& index_handle() const {
57 | return index_handle_;
58 | }
59 | void set_index_handle(const BlockHandle& h) {
60 | index_handle_ = h;
61 | }
62 |
63 | void EncodeTo(std::string* dst) const;
64 | Status DecodeFrom(Slice* input);
65 |
66 | // Encoded length of a Footer. Note that the serialization of a
67 | // Footer will always occupy exactly this many bytes. It consists
68 | // of two block handles and a magic number.
69 | enum {
70 | kEncodedLength = 2*BlockHandle::kMaxEncodedLength + 8
71 | };
72 |
73 | private:
74 | BlockHandle metaindex_handle_;
75 | BlockHandle index_handle_;
76 | };
77 |
78 | // kTableMagicNumber was picked by running
79 | // echo http://code.google.com/p/leveldb/ | sha1sum
80 | // and taking the leading 64 bits.
81 | static const uint64_t kTableMagicNumber = 0xdb4775248b80fb57ull;
82 |
83 | // 1-byte type + 32-bit crc
84 | static const size_t kBlockTrailerSize = 5;
85 |
86 | struct BlockContents {
87 | Slice data; // Actual contents of data
88 | bool cachable; // True iff data can be cached
89 | bool heap_allocated; // True iff caller should delete[] data.data()
90 | };
91 |
92 | // Read the block identified by "handle" from "file". On failure
93 | // return non-OK. On success fill *result and return OK.
94 | extern Status ReadBlock(RandomAccessFile* file,
95 | const ReadOptions& options,
96 | const BlockHandle& handle,
97 | BlockContents* result);
98 |
99 | // Implementation details follow. Clients should ignore,
100 |
101 | inline BlockHandle::BlockHandle()
102 | : offset_(~static_cast(0)),
103 | size_(~static_cast(0)) {
104 | }
105 |
106 | } // namespace leveldb
107 |
108 | #endif // STORAGE_LEVELDB_TABLE_FORMAT_H_
109 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/iterator.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/iterator.h"
6 |
7 | namespace leveldb {
8 |
9 | Iterator::Iterator() {
10 | cleanup_.function = NULL;
11 | cleanup_.next = NULL;
12 | }
13 |
14 | Iterator::~Iterator() {
15 | if (cleanup_.function != NULL) {
16 | (*cleanup_.function)(cleanup_.arg1, cleanup_.arg2);
17 | for (Cleanup* c = cleanup_.next; c != NULL; ) {
18 | (*c->function)(c->arg1, c->arg2);
19 | Cleanup* next = c->next;
20 | delete c;
21 | c = next;
22 | }
23 | }
24 | }
25 |
26 | void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) {
27 | assert(func != NULL);
28 | Cleanup* c;
29 | if (cleanup_.function == NULL) {
30 | c = &cleanup_;
31 | } else {
32 | c = new Cleanup;
33 | c->next = cleanup_.next;
34 | cleanup_.next = c;
35 | }
36 | c->function = func;
37 | c->arg1 = arg1;
38 | c->arg2 = arg2;
39 | }
40 |
41 | namespace {
42 | class EmptyIterator : public Iterator {
43 | public:
44 | EmptyIterator(const Status& s) : status_(s) { }
45 | virtual bool Valid() const { return false; }
46 | virtual void Seek(const Slice& target) { }
47 | virtual void SeekToFirst() { }
48 | virtual void SeekToLast() { }
49 | virtual void Next() { assert(false); }
50 | virtual void Prev() { assert(false); }
51 | Slice key() const { assert(false); return Slice(); }
52 | Slice value() const { assert(false); return Slice(); }
53 | virtual Status status() const { return status_; }
54 | private:
55 | Status status_;
56 | };
57 | } // namespace
58 |
59 | Iterator* NewEmptyIterator() {
60 | return new EmptyIterator(Status::OK());
61 | }
62 |
63 | Iterator* NewErrorIterator(const Status& status) {
64 | return new EmptyIterator(status);
65 | }
66 |
67 | } // namespace leveldb
68 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/iterator_wrapper.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
6 | #define STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
7 |
8 | namespace leveldb {
9 |
10 | // A internal wrapper class with an interface similar to Iterator that
11 | // caches the valid() and key() results for an underlying iterator.
12 | // This can help avoid virtual function calls and also gives better
13 | // cache locality.
14 | class IteratorWrapper {
15 | public:
16 | IteratorWrapper(): iter_(NULL), valid_(false) { }
17 | explicit IteratorWrapper(Iterator* iter): iter_(NULL) {
18 | Set(iter);
19 | }
20 | ~IteratorWrapper() { delete iter_; }
21 | Iterator* iter() const { return iter_; }
22 |
23 | // Takes ownership of "iter" and will delete it when destroyed, or
24 | // when Set() is invoked again.
25 | void Set(Iterator* iter) {
26 | delete iter_;
27 | iter_ = iter;
28 | if (iter_ == NULL) {
29 | valid_ = false;
30 | } else {
31 | Update();
32 | }
33 | }
34 |
35 |
36 | // Iterator interface methods
37 | bool Valid() const { return valid_; }
38 | Slice key() const { assert(Valid()); return key_; }
39 | Slice value() const { assert(Valid()); return iter_->value(); }
40 | // Methods below require iter() != NULL
41 | Status status() const { assert(iter_); return iter_->status(); }
42 | void Next() { assert(iter_); iter_->Next(); Update(); }
43 | void Prev() { assert(iter_); iter_->Prev(); Update(); }
44 | void Seek(const Slice& k) { assert(iter_); iter_->Seek(k); Update(); }
45 | void SeekToFirst() { assert(iter_); iter_->SeekToFirst(); Update(); }
46 | void SeekToLast() { assert(iter_); iter_->SeekToLast(); Update(); }
47 |
48 | private:
49 | void Update() {
50 | valid_ = iter_->Valid();
51 | if (valid_) {
52 | key_ = iter_->key();
53 | }
54 | }
55 |
56 | Iterator* iter_;
57 | bool valid_;
58 | Slice key_;
59 | };
60 |
61 | } // namespace leveldb
62 |
63 | #endif // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
64 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/merger.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_TABLE_MERGER_H_
6 | #define STORAGE_LEVELDB_TABLE_MERGER_H_
7 |
8 | namespace leveldb {
9 |
10 | class Comparator;
11 | class Iterator;
12 |
13 | // Return an iterator that provided the union of the data in
14 | // children[0,n-1]. Takes ownership of the child iterators and
15 | // will delete them when the result iterator is deleted.
16 | //
17 | // The result does no duplicate suppression. I.e., if a particular
18 | // key is present in K child iterators, it will be yielded K times.
19 | //
20 | // REQUIRES: n >= 0
21 | extern Iterator* NewMergingIterator(
22 | const Comparator* comparator, Iterator** children, int n);
23 |
24 | } // namespace leveldb
25 |
26 | #endif // STORAGE_LEVELDB_TABLE_MERGER_H_
27 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/table/two_level_iterator.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_TABLE_TWO_LEVEL_ITERATOR_H_
6 | #define STORAGE_LEVELDB_TABLE_TWO_LEVEL_ITERATOR_H_
7 |
8 | #include "leveldb/iterator.h"
9 |
10 | namespace leveldb {
11 |
12 | struct ReadOptions;
13 |
14 | // Return a new two level iterator. A two-level iterator contains an
15 | // index iterator whose values point to a sequence of blocks where
16 | // each block is itself a sequence of key,value pairs. The returned
17 | // two-level iterator yields the concatenation of all key/value pairs
18 | // in the sequence of blocks. Takes ownership of "index_iter" and
19 | // will delete it when no longer needed.
20 | //
21 | // Uses a supplied function to convert an index_iter value into
22 | // an iterator over the contents of the corresponding block.
23 | extern Iterator* NewTwoLevelIterator(
24 | Iterator* index_iter,
25 | Iterator* (*block_function)(
26 | void* arg,
27 | const ReadOptions& options,
28 | const Slice& index_value),
29 | void* arg,
30 | const ReadOptions& options);
31 |
32 | } // namespace leveldb
33 |
34 | #endif // STORAGE_LEVELDB_TABLE_TWO_LEVEL_ITERATOR_H_
35 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/arena.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/arena.h"
6 | #include
7 |
8 | namespace leveldb {
9 |
10 | static const int kBlockSize = 4096;
11 |
12 | Arena::Arena() {
13 | blocks_memory_ = 0;
14 | alloc_ptr_ = NULL; // First allocation will allocate a block
15 | alloc_bytes_remaining_ = 0;
16 | }
17 |
18 | Arena::~Arena() {
19 | for (size_t i = 0; i < blocks_.size(); i++) {
20 | delete[] blocks_[i];
21 | }
22 | }
23 |
24 | char* Arena::AllocateFallback(size_t bytes) {
25 | if (bytes > kBlockSize / 4) {
26 | // Object is more than a quarter of our block size. Allocate it separately
27 | // to avoid wasting too much space in leftover bytes.
28 | char* result = AllocateNewBlock(bytes);
29 | return result;
30 | }
31 |
32 | // We waste the remaining space in the current block.
33 | alloc_ptr_ = AllocateNewBlock(kBlockSize);
34 | alloc_bytes_remaining_ = kBlockSize;
35 |
36 | char* result = alloc_ptr_;
37 | alloc_ptr_ += bytes;
38 | alloc_bytes_remaining_ -= bytes;
39 | return result;
40 | }
41 |
42 | char* Arena::AllocateAligned(size_t bytes) {
43 | const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
44 | assert((align & (align-1)) == 0); // Pointer size should be a power of 2
45 | size_t current_mod = reinterpret_cast(alloc_ptr_) & (align-1);
46 | size_t slop = (current_mod == 0 ? 0 : align - current_mod);
47 | size_t needed = bytes + slop;
48 | char* result;
49 | if (needed <= alloc_bytes_remaining_) {
50 | result = alloc_ptr_ + slop;
51 | alloc_ptr_ += needed;
52 | alloc_bytes_remaining_ -= needed;
53 | } else {
54 | // AllocateFallback always returned aligned memory
55 | result = AllocateFallback(bytes);
56 | }
57 | assert((reinterpret_cast(result) & (align-1)) == 0);
58 | return result;
59 | }
60 |
61 | char* Arena::AllocateNewBlock(size_t block_bytes) {
62 | char* result = new char[block_bytes];
63 | blocks_memory_ += block_bytes;
64 | blocks_.push_back(result);
65 | return result;
66 | }
67 |
68 | } // namespace leveldb
69 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/arena.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_UTIL_ARENA_H_
6 | #define STORAGE_LEVELDB_UTIL_ARENA_H_
7 |
8 | #include
9 | #include
10 | #include
11 | #include
12 |
13 | namespace leveldb {
14 |
15 | class Arena {
16 | public:
17 | Arena();
18 | ~Arena();
19 |
20 | // Return a pointer to a newly allocated memory block of "bytes" bytes.
21 | char* Allocate(size_t bytes);
22 |
23 | // Allocate memory with the normal alignment guarantees provided by malloc
24 | char* AllocateAligned(size_t bytes);
25 |
26 | // Returns an estimate of the total memory usage of data allocated
27 | // by the arena (including space allocated but not yet used for user
28 | // allocations).
29 | size_t MemoryUsage() const {
30 | return blocks_memory_ + blocks_.capacity() * sizeof(char*);
31 | }
32 |
33 | private:
34 | char* AllocateFallback(size_t bytes);
35 | char* AllocateNewBlock(size_t block_bytes);
36 |
37 | // Allocation state
38 | char* alloc_ptr_;
39 | size_t alloc_bytes_remaining_;
40 |
41 | // Array of new[] allocated memory blocks
42 | std::vector blocks_;
43 |
44 | // Bytes of memory in blocks allocated so far
45 | size_t blocks_memory_;
46 |
47 | // No copying allowed
48 | Arena(const Arena&);
49 | void operator=(const Arena&);
50 | };
51 |
52 | inline char* Arena::Allocate(size_t bytes) {
53 | // The semantics of what to return are a bit messy if we allow
54 | // 0-byte allocations, so we disallow them here (we don't need
55 | // them for our internal use).
56 | assert(bytes > 0);
57 | if (bytes <= alloc_bytes_remaining_) {
58 | char* result = alloc_ptr_;
59 | alloc_ptr_ += bytes;
60 | alloc_bytes_remaining_ -= bytes;
61 | return result;
62 | }
63 | return AllocateFallback(bytes);
64 | }
65 |
66 | } // namespace leveldb
67 |
68 | #endif // STORAGE_LEVELDB_UTIL_ARENA_H_
69 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/arena_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/arena.h"
6 |
7 | #include "util/random.h"
8 | #include "util/testharness.h"
9 |
10 | namespace leveldb {
11 |
12 | class ArenaTest { };
13 |
14 | TEST(ArenaTest, Empty) {
15 | Arena arena;
16 | }
17 |
18 | TEST(ArenaTest, Simple) {
19 | std::vector > allocated;
20 | Arena arena;
21 | const int N = 100000;
22 | size_t bytes = 0;
23 | Random rnd(301);
24 | for (int i = 0; i < N; i++) {
25 | size_t s;
26 | if (i % (N / 10) == 0) {
27 | s = i;
28 | } else {
29 | s = rnd.OneIn(4000) ? rnd.Uniform(6000) :
30 | (rnd.OneIn(10) ? rnd.Uniform(100) : rnd.Uniform(20));
31 | }
32 | if (s == 0) {
33 | // Our arena disallows size 0 allocations.
34 | s = 1;
35 | }
36 | char* r;
37 | if (rnd.OneIn(10)) {
38 | r = arena.AllocateAligned(s);
39 | } else {
40 | r = arena.Allocate(s);
41 | }
42 |
43 | for (size_t b = 0; b < s; b++) {
44 | // Fill the "i"th allocation with a known bit pattern
45 | r[b] = i % 256;
46 | }
47 | bytes += s;
48 | allocated.push_back(std::make_pair(s, r));
49 | ASSERT_GE(arena.MemoryUsage(), bytes);
50 | if (i > N/10) {
51 | ASSERT_LE(arena.MemoryUsage(), bytes * 1.10);
52 | }
53 | }
54 | for (size_t i = 0; i < allocated.size(); i++) {
55 | size_t num_bytes = allocated[i].first;
56 | const char* p = allocated[i].second;
57 | for (size_t b = 0; b < num_bytes; b++) {
58 | // Check the "i"th allocation for the known bit pattern
59 | ASSERT_EQ(int(p[b]) & 0xff, i % 256);
60 | }
61 | }
62 | }
63 |
64 | } // namespace leveldb
65 |
66 | int main(int argc, char** argv) {
67 | return leveldb::test::RunAllTests();
68 | }
69 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/bloom.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/filter_policy.h"
6 |
7 | #include "leveldb/slice.h"
8 | #include "util/hash.h"
9 |
10 | namespace leveldb {
11 |
12 | namespace {
13 | static uint32_t BloomHash(const Slice& key) {
14 | return Hash(key.data(), key.size(), 0xbc9f1d34);
15 | }
16 |
17 | class BloomFilterPolicy : public FilterPolicy {
18 | private:
19 | size_t bits_per_key_;
20 | size_t k_;
21 |
22 | public:
23 | explicit BloomFilterPolicy(int bits_per_key)
24 | : bits_per_key_(bits_per_key) {
25 | // We intentionally round down to reduce probing cost a little bit
26 | k_ = static_cast(bits_per_key * 0.69); // 0.69 =~ ln(2)
27 | if (k_ < 1) k_ = 1;
28 | if (k_ > 30) k_ = 30;
29 | }
30 |
31 | virtual const char* Name() const {
32 | return "leveldb.BuiltinBloomFilter2";
33 | }
34 |
35 | virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const {
36 | // Compute bloom filter size (in both bits and bytes)
37 | size_t bits = n * bits_per_key_;
38 |
39 | // For small n, we can see a very high false positive rate. Fix it
40 | // by enforcing a minimum bloom filter length.
41 | if (bits < 64) bits = 64;
42 |
43 | size_t bytes = (bits + 7) / 8;
44 | bits = bytes * 8;
45 |
46 | const size_t init_size = dst->size();
47 | dst->resize(init_size + bytes, 0);
48 | dst->push_back(static_cast(k_)); // Remember # of probes in filter
49 | char* array = &(*dst)[init_size];
50 | for (size_t i = 0; i < n; i++) {
51 | // Use double-hashing to generate a sequence of hash values.
52 | // See analysis in [Kirsch,Mitzenmacher 2006].
53 | uint32_t h = BloomHash(keys[i]);
54 | const uint32_t delta = (h >> 17) | (h << 15); // Rotate right 17 bits
55 | for (size_t j = 0; j < k_; j++) {
56 | const uint32_t bitpos = h % bits;
57 | array[bitpos/8] |= (1 << (bitpos % 8));
58 | h += delta;
59 | }
60 | }
61 | }
62 |
63 | virtual bool KeyMayMatch(const Slice& key, const Slice& bloom_filter) const {
64 | const size_t len = bloom_filter.size();
65 | if (len < 2) return false;
66 |
67 | const char* array = bloom_filter.data();
68 | const size_t bits = (len - 1) * 8;
69 |
70 | // Use the encoded k so that we can read filters generated by
71 | // bloom filters created using different parameters.
72 | const size_t k = array[len-1];
73 | if (k > 30) {
74 | // Reserved for potentially new encodings for short bloom filters.
75 | // Consider it a match.
76 | return true;
77 | }
78 |
79 | uint32_t h = BloomHash(key);
80 | const uint32_t delta = (h >> 17) | (h << 15); // Rotate right 17 bits
81 | for (size_t j = 0; j < k; j++) {
82 | const uint32_t bitpos = h % bits;
83 | if ((array[bitpos/8] & (1 << (bitpos % 8))) == 0) return false;
84 | h += delta;
85 | }
86 | return true;
87 | }
88 | };
89 | }
90 |
91 | const FilterPolicy* NewBloomFilterPolicy(int bits_per_key) {
92 | return new BloomFilterPolicy(bits_per_key);
93 | }
94 |
95 | } // namespace leveldb
96 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/comparator.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include
6 | #include
7 | #include "leveldb/comparator.h"
8 | #include "leveldb/slice.h"
9 | #include "port/port.h"
10 | #include "util/logging.h"
11 |
12 | namespace leveldb {
13 |
14 | Comparator::~Comparator() { }
15 |
16 | namespace {
17 | class BytewiseComparatorImpl : public Comparator {
18 | public:
19 | BytewiseComparatorImpl() { }
20 |
21 | virtual const char* Name() const {
22 | return "leveldb.BytewiseComparator";
23 | }
24 |
25 | virtual int Compare(const Slice& a, const Slice& b) const {
26 | return a.compare(b);
27 | }
28 |
29 | virtual void FindShortestSeparator(
30 | std::string* start,
31 | const Slice& limit) const {
32 | // Find length of common prefix
33 | size_t min_length = std::min(start->size(), limit.size());
34 | size_t diff_index = 0;
35 | while ((diff_index < min_length) &&
36 | ((*start)[diff_index] == limit[diff_index])) {
37 | diff_index++;
38 | }
39 |
40 | if (diff_index >= min_length) {
41 | // Do not shorten if one string is a prefix of the other
42 | } else {
43 | uint8_t diff_byte = static_cast((*start)[diff_index]);
44 | if (diff_byte < static_cast(0xff) &&
45 | diff_byte + 1 < static_cast(limit[diff_index])) {
46 | (*start)[diff_index]++;
47 | start->resize(diff_index + 1);
48 | assert(Compare(*start, limit) < 0);
49 | }
50 | }
51 | }
52 |
53 | virtual void FindShortSuccessor(std::string* key) const {
54 | // Find first character that can be incremented
55 | size_t n = key->size();
56 | for (size_t i = 0; i < n; i++) {
57 | const uint8_t byte = (*key)[i];
58 | if (byte != static_cast(0xff)) {
59 | (*key)[i] = byte + 1;
60 | key->resize(i+1);
61 | return;
62 | }
63 | }
64 | // *key is a run of 0xffs. Leave it alone.
65 | }
66 | };
67 | } // namespace
68 |
69 | static port::OnceType once = LEVELDB_ONCE_INIT;
70 | static const Comparator* bytewise;
71 |
72 | static void InitModule() {
73 | bytewise = new BytewiseComparatorImpl;
74 | }
75 |
76 | const Comparator* BytewiseComparator() {
77 | port::InitOnce(&once, InitModule);
78 | return bytewise;
79 | }
80 |
81 | } // namespace leveldb
82 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/crc32c.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_UTIL_CRC32C_H_
6 | #define STORAGE_LEVELDB_UTIL_CRC32C_H_
7 |
8 | #include
9 | #include
10 |
11 | namespace leveldb {
12 | namespace crc32c {
13 |
14 | // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the
15 | // crc32c of some string A. Extend() is often used to maintain the
16 | // crc32c of a stream of data.
17 | extern uint32_t Extend(uint32_t init_crc, const char* data, size_t n);
18 |
19 | // Return the crc32c of data[0,n-1]
20 | inline uint32_t Value(const char* data, size_t n) {
21 | return Extend(0, data, n);
22 | }
23 |
24 | static const uint32_t kMaskDelta = 0xa282ead8ul;
25 |
26 | // Return a masked representation of crc.
27 | //
28 | // Motivation: it is problematic to compute the CRC of a string that
29 | // contains embedded CRCs. Therefore we recommend that CRCs stored
30 | // somewhere (e.g., in files) should be masked before being stored.
31 | inline uint32_t Mask(uint32_t crc) {
32 | // Rotate right by 15 bits and add a constant.
33 | return ((crc >> 15) | (crc << 17)) + kMaskDelta;
34 | }
35 |
36 | // Return the crc whose masked representation is masked_crc.
37 | inline uint32_t Unmask(uint32_t masked_crc) {
38 | uint32_t rot = masked_crc - kMaskDelta;
39 | return ((rot >> 17) | (rot << 15));
40 | }
41 |
42 | } // namespace crc32c
43 | } // namespace leveldb
44 |
45 | #endif // STORAGE_LEVELDB_UTIL_CRC32C_H_
46 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/crc32c_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/crc32c.h"
6 | #include "util/testharness.h"
7 |
8 | namespace leveldb {
9 | namespace crc32c {
10 |
11 | class CRC { };
12 |
13 | TEST(CRC, StandardResults) {
14 | // From rfc3720 section B.4.
15 | char buf[32];
16 |
17 | memset(buf, 0, sizeof(buf));
18 | ASSERT_EQ(0x8a9136aa, Value(buf, sizeof(buf)));
19 |
20 | memset(buf, 0xff, sizeof(buf));
21 | ASSERT_EQ(0x62a8ab43, Value(buf, sizeof(buf)));
22 |
23 | for (int i = 0; i < 32; i++) {
24 | buf[i] = i;
25 | }
26 | ASSERT_EQ(0x46dd794e, Value(buf, sizeof(buf)));
27 |
28 | for (int i = 0; i < 32; i++) {
29 | buf[i] = 31 - i;
30 | }
31 | ASSERT_EQ(0x113fdb5c, Value(buf, sizeof(buf)));
32 |
33 | unsigned char data[48] = {
34 | 0x01, 0xc0, 0x00, 0x00,
35 | 0x00, 0x00, 0x00, 0x00,
36 | 0x00, 0x00, 0x00, 0x00,
37 | 0x00, 0x00, 0x00, 0x00,
38 | 0x14, 0x00, 0x00, 0x00,
39 | 0x00, 0x00, 0x04, 0x00,
40 | 0x00, 0x00, 0x00, 0x14,
41 | 0x00, 0x00, 0x00, 0x18,
42 | 0x28, 0x00, 0x00, 0x00,
43 | 0x00, 0x00, 0x00, 0x00,
44 | 0x02, 0x00, 0x00, 0x00,
45 | 0x00, 0x00, 0x00, 0x00,
46 | };
47 | ASSERT_EQ(0xd9963a56, Value(reinterpret_cast(data), sizeof(data)));
48 | }
49 |
50 | TEST(CRC, Values) {
51 | ASSERT_NE(Value("a", 1), Value("foo", 3));
52 | }
53 |
54 | TEST(CRC, Extend) {
55 | ASSERT_EQ(Value("hello world", 11),
56 | Extend(Value("hello ", 6), "world", 5));
57 | }
58 |
59 | TEST(CRC, Mask) {
60 | uint32_t crc = Value("foo", 3);
61 | ASSERT_NE(crc, Mask(crc));
62 | ASSERT_NE(crc, Mask(Mask(crc)));
63 | ASSERT_EQ(crc, Unmask(Mask(crc)));
64 | ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
65 | }
66 |
67 | } // namespace crc32c
68 | } // namespace leveldb
69 |
70 | int main(int argc, char** argv) {
71 | return leveldb::test::RunAllTests();
72 | }
73 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/env.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/env.h"
6 |
7 | namespace leveldb {
8 |
9 | Env::~Env() {
10 | }
11 |
12 | SequentialFile::~SequentialFile() {
13 | }
14 |
15 | RandomAccessFile::~RandomAccessFile() {
16 | }
17 |
18 | WritableFile::~WritableFile() {
19 | }
20 |
21 | Logger::~Logger() {
22 | }
23 |
24 | FileLock::~FileLock() {
25 | }
26 |
27 | void Log(Logger* info_log, const char* format, ...) {
28 | if (info_log != NULL) {
29 | va_list ap;
30 | va_start(ap, format);
31 | info_log->Logv(format, ap);
32 | va_end(ap);
33 | }
34 | }
35 |
36 | static Status DoWriteStringToFile(Env* env, const Slice& data,
37 | const std::string& fname,
38 | bool should_sync) {
39 | WritableFile* file;
40 | Status s = env->NewWritableFile(fname, &file);
41 | if (!s.ok()) {
42 | return s;
43 | }
44 | s = file->Append(data);
45 | if (s.ok() && should_sync) {
46 | s = file->Sync();
47 | }
48 | if (s.ok()) {
49 | s = file->Close();
50 | }
51 | delete file; // Will auto-close if we did not close above
52 | if (!s.ok()) {
53 | env->DeleteFile(fname);
54 | }
55 | return s;
56 | }
57 |
58 | Status WriteStringToFile(Env* env, const Slice& data,
59 | const std::string& fname) {
60 | return DoWriteStringToFile(env, data, fname, false);
61 | }
62 |
63 | Status WriteStringToFileSync(Env* env, const Slice& data,
64 | const std::string& fname) {
65 | return DoWriteStringToFile(env, data, fname, true);
66 | }
67 |
68 | Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
69 | data->clear();
70 | SequentialFile* file;
71 | Status s = env->NewSequentialFile(fname, &file);
72 | if (!s.ok()) {
73 | return s;
74 | }
75 | static const int kBufferSize = 8192;
76 | char* space = new char[kBufferSize];
77 | while (true) {
78 | Slice fragment;
79 | s = file->Read(kBufferSize, &fragment, space);
80 | if (!s.ok()) {
81 | break;
82 | }
83 | data->append(fragment.data(), fragment.size());
84 | if (fragment.empty()) {
85 | break;
86 | }
87 | }
88 | delete[] space;
89 | delete file;
90 | return s;
91 | }
92 |
93 | EnvWrapper::~EnvWrapper() {
94 | }
95 |
96 | } // namespace leveldb
97 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/env_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/env.h"
6 |
7 | #include "port/port.h"
8 | #include "util/testharness.h"
9 |
10 | namespace leveldb {
11 |
12 | static const int kDelayMicros = 100000;
13 |
14 | class EnvPosixTest {
15 | private:
16 | port::Mutex mu_;
17 | std::string events_;
18 |
19 | public:
20 | Env* env_;
21 | EnvPosixTest() : env_(Env::Default()) { }
22 | };
23 |
24 | static void SetBool(void* ptr) {
25 | reinterpret_cast(ptr)->NoBarrier_Store(ptr);
26 | }
27 |
28 | TEST(EnvPosixTest, RunImmediately) {
29 | port::AtomicPointer called (NULL);
30 | env_->Schedule(&SetBool, &called);
31 | Env::Default()->SleepForMicroseconds(kDelayMicros);
32 | ASSERT_TRUE(called.NoBarrier_Load() != NULL);
33 | }
34 |
35 | TEST(EnvPosixTest, RunMany) {
36 | port::AtomicPointer last_id (NULL);
37 |
38 | struct CB {
39 | port::AtomicPointer* last_id_ptr; // Pointer to shared slot
40 | uintptr_t id; // Order# for the execution of this callback
41 |
42 | CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
43 |
44 | static void Run(void* v) {
45 | CB* cb = reinterpret_cast(v);
46 | void* cur = cb->last_id_ptr->NoBarrier_Load();
47 | ASSERT_EQ(cb->id-1, reinterpret_cast(cur));
48 | cb->last_id_ptr->Release_Store(reinterpret_cast(cb->id));
49 | }
50 | };
51 |
52 | // Schedule in different order than start time
53 | CB cb1(&last_id, 1);
54 | CB cb2(&last_id, 2);
55 | CB cb3(&last_id, 3);
56 | CB cb4(&last_id, 4);
57 | env_->Schedule(&CB::Run, &cb1);
58 | env_->Schedule(&CB::Run, &cb2);
59 | env_->Schedule(&CB::Run, &cb3);
60 | env_->Schedule(&CB::Run, &cb4);
61 |
62 | Env::Default()->SleepForMicroseconds(kDelayMicros);
63 | void* cur = last_id.Acquire_Load();
64 | ASSERT_EQ(4, reinterpret_cast(cur));
65 | }
66 |
67 | struct State {
68 | port::Mutex mu;
69 | int val;
70 | int num_running;
71 | };
72 |
73 | static void ThreadBody(void* arg) {
74 | State* s = reinterpret_cast(arg);
75 | s->mu.Lock();
76 | s->val += 1;
77 | s->num_running -= 1;
78 | s->mu.Unlock();
79 | }
80 |
81 | TEST(EnvPosixTest, StartThread) {
82 | State state;
83 | state.val = 0;
84 | state.num_running = 3;
85 | for (int i = 0; i < 3; i++) {
86 | env_->StartThread(&ThreadBody, &state);
87 | }
88 | while (true) {
89 | state.mu.Lock();
90 | int num = state.num_running;
91 | state.mu.Unlock();
92 | if (num == 0) {
93 | break;
94 | }
95 | Env::Default()->SleepForMicroseconds(kDelayMicros);
96 | }
97 | ASSERT_EQ(state.val, 3);
98 | }
99 |
100 | } // namespace leveldb
101 |
102 | int main(int argc, char** argv) {
103 | return leveldb::test::RunAllTests();
104 | }
105 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/filter_policy.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/filter_policy.h"
6 |
7 | namespace leveldb {
8 |
9 | FilterPolicy::~FilterPolicy() { }
10 |
11 | } // namespace leveldb
12 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/hash.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include
6 | #include "util/coding.h"
7 | #include "util/hash.h"
8 |
9 | // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
10 | // between switch labels. The real definition should be provided externally.
11 | // This one is a fallback version for unsupported compilers.
12 | #ifndef FALLTHROUGH_INTENDED
13 | #define FALLTHROUGH_INTENDED do { } while (0)
14 | #endif
15 |
16 | namespace leveldb {
17 |
18 | uint32_t Hash(const char* data, size_t n, uint32_t seed) {
19 | // Similar to murmur hash
20 | const uint32_t m = 0xc6a4a793;
21 | const uint32_t r = 24;
22 | const char* limit = data + n;
23 | uint32_t h = seed ^ (n * m);
24 |
25 | // Pick up four bytes at a time
26 | while (data + 4 <= limit) {
27 | uint32_t w = DecodeFixed32(data);
28 | data += 4;
29 | h += w;
30 | h *= m;
31 | h ^= (h >> 16);
32 | }
33 |
34 | // Pick up remaining bytes
35 | switch (limit - data) {
36 | case 3:
37 | h += static_cast(data[2]) << 16;
38 | FALLTHROUGH_INTENDED;
39 | case 2:
40 | h += static_cast(data[1]) << 8;
41 | FALLTHROUGH_INTENDED;
42 | case 1:
43 | h += static_cast(data[0]);
44 | h *= m;
45 | h ^= (h >> r);
46 | break;
47 | }
48 | return h;
49 | }
50 |
51 |
52 | } // namespace leveldb
53 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/hash.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // Simple hash function used for internal data structures
6 |
7 | #ifndef STORAGE_LEVELDB_UTIL_HASH_H_
8 | #define STORAGE_LEVELDB_UTIL_HASH_H_
9 |
10 | #include
11 | #include
12 |
13 | namespace leveldb {
14 |
15 | extern uint32_t Hash(const char* data, size_t n, uint32_t seed);
16 |
17 | }
18 |
19 | #endif // STORAGE_LEVELDB_UTIL_HASH_H_
20 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/hash_test.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/hash.h"
6 | #include "util/testharness.h"
7 |
8 | namespace leveldb {
9 |
10 | class HASH { };
11 |
12 | TEST(HASH, SignedUnsignedIssue) {
13 | const unsigned char data1[1] = {0x62};
14 | const unsigned char data2[2] = {0xc3, 0x97};
15 | const unsigned char data3[3] = {0xe2, 0x99, 0xa5};
16 | const unsigned char data4[4] = {0xe1, 0x80, 0xb9, 0x32};
17 | const unsigned char data5[48] = {
18 | 0x01, 0xc0, 0x00, 0x00,
19 | 0x00, 0x00, 0x00, 0x00,
20 | 0x00, 0x00, 0x00, 0x00,
21 | 0x00, 0x00, 0x00, 0x00,
22 | 0x14, 0x00, 0x00, 0x00,
23 | 0x00, 0x00, 0x04, 0x00,
24 | 0x00, 0x00, 0x00, 0x14,
25 | 0x00, 0x00, 0x00, 0x18,
26 | 0x28, 0x00, 0x00, 0x00,
27 | 0x00, 0x00, 0x00, 0x00,
28 | 0x02, 0x00, 0x00, 0x00,
29 | 0x00, 0x00, 0x00, 0x00,
30 | };
31 |
32 | ASSERT_EQ(Hash(0, 0, 0xbc9f1d34), 0xbc9f1d34);
33 | ASSERT_EQ(
34 | Hash(reinterpret_cast(data1), sizeof(data1), 0xbc9f1d34),
35 | 0xef1345c4);
36 | ASSERT_EQ(
37 | Hash(reinterpret_cast(data2), sizeof(data2), 0xbc9f1d34),
38 | 0x5b663814);
39 | ASSERT_EQ(
40 | Hash(reinterpret_cast(data3), sizeof(data3), 0xbc9f1d34),
41 | 0x323c078f);
42 | ASSERT_EQ(
43 | Hash(reinterpret_cast(data4), sizeof(data4), 0xbc9f1d34),
44 | 0xed21633a);
45 | ASSERT_EQ(
46 | Hash(reinterpret_cast(data5), sizeof(data5), 0x12345678),
47 | 0xf333dabb);
48 | }
49 |
50 | } // namespace leveldb
51 |
52 | int main(int argc, char** argv) {
53 | return leveldb::test::RunAllTests();
54 | }
55 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/histogram.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_UTIL_HISTOGRAM_H_
6 | #define STORAGE_LEVELDB_UTIL_HISTOGRAM_H_
7 |
8 | #include
9 |
10 | namespace leveldb {
11 |
12 | class Histogram {
13 | public:
14 | Histogram() { }
15 | ~Histogram() { }
16 |
17 | void Clear();
18 | void Add(double value);
19 | void Merge(const Histogram& other);
20 |
21 | std::string ToString() const;
22 |
23 | private:
24 | double min_;
25 | double max_;
26 | double num_;
27 | double sum_;
28 | double sum_squares_;
29 |
30 | enum { kNumBuckets = 154 };
31 | static const double kBucketLimit[kNumBuckets];
32 | double buckets_[kNumBuckets];
33 |
34 | double Median() const;
35 | double Percentile(double p) const;
36 | double Average() const;
37 | double StandardDeviation() const;
38 | };
39 |
40 | } // namespace leveldb
41 |
42 | #endif // STORAGE_LEVELDB_UTIL_HISTOGRAM_H_
43 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/logging.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/logging.h"
6 |
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include "leveldb/env.h"
12 | #include "leveldb/slice.h"
13 |
14 | namespace leveldb {
15 |
16 | void AppendNumberTo(std::string* str, uint64_t num) {
17 | char buf[30];
18 | snprintf(buf, sizeof(buf), "%llu", (unsigned long long) num);
19 | str->append(buf);
20 | }
21 |
22 | void AppendEscapedStringTo(std::string* str, const Slice& value) {
23 | for (size_t i = 0; i < value.size(); i++) {
24 | char c = value[i];
25 | if (c >= ' ' && c <= '~') {
26 | str->push_back(c);
27 | } else {
28 | char buf[10];
29 | snprintf(buf, sizeof(buf), "\\x%02x",
30 | static_cast(c) & 0xff);
31 | str->append(buf);
32 | }
33 | }
34 | }
35 |
36 | std::string NumberToString(uint64_t num) {
37 | std::string r;
38 | AppendNumberTo(&r, num);
39 | return r;
40 | }
41 |
42 | std::string EscapeString(const Slice& value) {
43 | std::string r;
44 | AppendEscapedStringTo(&r, value);
45 | return r;
46 | }
47 |
48 | bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
49 | uint64_t v = 0;
50 | int digits = 0;
51 | while (!in->empty()) {
52 | char c = (*in)[0];
53 | if (c >= '0' && c <= '9') {
54 | ++digits;
55 | const int delta = (c - '0');
56 | static const uint64_t kMaxUint64 = ~static_cast(0);
57 | if (v > kMaxUint64/10 ||
58 | (v == kMaxUint64/10 && delta > kMaxUint64%10)) {
59 | // Overflow
60 | return false;
61 | }
62 | v = (v * 10) + delta;
63 | in->remove_prefix(1);
64 | } else {
65 | break;
66 | }
67 | }
68 | *val = v;
69 | return (digits > 0);
70 | }
71 |
72 | } // namespace leveldb
73 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/logging.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // Must not be included from any .h files to avoid polluting the namespace
6 | // with macros.
7 |
8 | #ifndef STORAGE_LEVELDB_UTIL_LOGGING_H_
9 | #define STORAGE_LEVELDB_UTIL_LOGGING_H_
10 |
11 | #include
12 | #include
13 | #include
14 | #include "port/port.h"
15 |
16 | namespace leveldb {
17 |
18 | class Slice;
19 | class WritableFile;
20 |
21 | // Append a human-readable printout of "num" to *str
22 | extern void AppendNumberTo(std::string* str, uint64_t num);
23 |
24 | // Append a human-readable printout of "value" to *str.
25 | // Escapes any non-printable characters found in "value".
26 | extern void AppendEscapedStringTo(std::string* str, const Slice& value);
27 |
28 | // Return a human-readable printout of "num"
29 | extern std::string NumberToString(uint64_t num);
30 |
31 | // Return a human-readable version of "value".
32 | // Escapes any non-printable characters found in "value".
33 | extern std::string EscapeString(const Slice& value);
34 |
35 | // Parse a human-readable number from "*in" into *value. On success,
36 | // advances "*in" past the consumed number and sets "*val" to the
37 | // numeric value. Otherwise, returns false and leaves *in in an
38 | // unspecified state.
39 | extern bool ConsumeDecimalNumber(Slice* in, uint64_t* val);
40 |
41 | } // namespace leveldb
42 |
43 | #endif // STORAGE_LEVELDB_UTIL_LOGGING_H_
44 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/mutexlock.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
6 | #define STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
7 |
8 | #include "port/port.h"
9 | #include "port/thread_annotations.h"
10 |
11 | namespace leveldb {
12 |
13 | // Helper class that locks a mutex on construction and unlocks the mutex when
14 | // the destructor of the MutexLock object is invoked.
15 | //
16 | // Typical usage:
17 | //
18 | // void MyClass::MyMethod() {
19 | // MutexLock l(&mu_); // mu_ is an instance variable
20 | // ... some complex code, possibly with multiple return paths ...
21 | // }
22 |
23 | class SCOPED_LOCKABLE MutexLock {
24 | public:
25 | explicit MutexLock(port::Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu)
26 | : mu_(mu) {
27 | this->mu_->Lock();
28 | }
29 | ~MutexLock() UNLOCK_FUNCTION() { this->mu_->Unlock(); }
30 |
31 | private:
32 | port::Mutex *const mu_;
33 | // No copying allowed
34 | MutexLock(const MutexLock&);
35 | void operator=(const MutexLock&);
36 | };
37 |
38 | } // namespace leveldb
39 |
40 |
41 | #endif // STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_
42 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/options.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "leveldb/options.h"
6 |
7 | #include "leveldb/comparator.h"
8 | #include "leveldb/env.h"
9 |
10 | namespace leveldb {
11 |
12 | Options::Options()
13 | : comparator(BytewiseComparator()),
14 | create_if_missing(false),
15 | error_if_exists(false),
16 | paranoid_checks(false),
17 | env(Env::Default()),
18 | info_log(NULL),
19 | write_buffer_size(4<<20),
20 | max_open_files(1000),
21 | block_cache(NULL),
22 | block_size(4096),
23 | block_restart_interval(16),
24 | compression(kSnappyCompression),
25 | filter_policy(NULL) {
26 | }
27 |
28 |
29 | } // namespace leveldb
30 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/posix_logger.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 | //
5 | // Logger implementation that can be shared by all environments
6 | // where enough posix functionality is available.
7 |
8 | #ifndef STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
9 | #define STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
10 |
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include "leveldb/env.h"
16 |
17 | namespace leveldb {
18 |
19 | class PosixLogger : public Logger {
20 | private:
21 | FILE* file_;
22 | uint64_t (*gettid_)(); // Return the thread id for the current thread
23 | public:
24 | PosixLogger(FILE* f, uint64_t (*gettid)()) : file_(f), gettid_(gettid) { }
25 | virtual ~PosixLogger() {
26 | fclose(file_);
27 | }
28 | virtual void Logv(const char* format, va_list ap) {
29 | const uint64_t thread_id = (*gettid_)();
30 |
31 | // We try twice: the first time with a fixed-size stack allocated buffer,
32 | // and the second time with a much larger dynamically allocated buffer.
33 | char buffer[500];
34 | for (int iter = 0; iter < 2; iter++) {
35 | char* base;
36 | int bufsize;
37 | if (iter == 0) {
38 | bufsize = sizeof(buffer);
39 | base = buffer;
40 | } else {
41 | bufsize = 30000;
42 | base = new char[bufsize];
43 | }
44 | char* p = base;
45 | char* limit = base + bufsize;
46 |
47 | struct timeval now_tv;
48 | gettimeofday(&now_tv, NULL);
49 | const time_t seconds = now_tv.tv_sec;
50 | struct tm t;
51 | localtime_r(&seconds, &t);
52 | p += snprintf(p, limit - p,
53 | "%04d/%02d/%02d-%02d:%02d:%02d.%06d %llx ",
54 | t.tm_year + 1900,
55 | t.tm_mon + 1,
56 | t.tm_mday,
57 | t.tm_hour,
58 | t.tm_min,
59 | t.tm_sec,
60 | static_cast(now_tv.tv_usec),
61 | static_cast(thread_id));
62 |
63 | // Print the message
64 | if (p < limit) {
65 | va_list backup_ap;
66 | va_copy(backup_ap, ap);
67 | p += vsnprintf(p, limit - p, format, backup_ap);
68 | va_end(backup_ap);
69 | }
70 |
71 | // Truncate to available space if necessary
72 | if (p >= limit) {
73 | if (iter == 0) {
74 | continue; // Try again with larger buffer
75 | } else {
76 | p = limit - 1;
77 | }
78 | }
79 |
80 | // Add newline if necessary
81 | if (p == base || p[-1] != '\n') {
82 | *p++ = '\n';
83 | }
84 |
85 | assert(p <= limit);
86 | fwrite(base, 1, p - base, file_);
87 | fflush(file_);
88 | if (base != buffer) {
89 | delete[] base;
90 | }
91 | break;
92 | }
93 | }
94 | };
95 |
96 | } // namespace leveldb
97 |
98 | #endif // STORAGE_LEVELDB_UTIL_POSIX_LOGGER_H_
99 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/random.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_UTIL_RANDOM_H_
6 | #define STORAGE_LEVELDB_UTIL_RANDOM_H_
7 |
8 | #include
9 |
10 | namespace leveldb {
11 |
12 | // A very simple random number generator. Not especially good at
13 | // generating truly random bits, but good enough for our needs in this
14 | // package.
15 | class Random {
16 | private:
17 | uint32_t seed_;
18 | public:
19 | explicit Random(uint32_t s) : seed_(s & 0x7fffffffu) {
20 | // Avoid bad seeds.
21 | if (seed_ == 0 || seed_ == 2147483647L) {
22 | seed_ = 1;
23 | }
24 | }
25 | uint32_t Next() {
26 | static const uint32_t M = 2147483647L; // 2^31-1
27 | static const uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0
28 | // We are computing
29 | // seed_ = (seed_ * A) % M, where M = 2^31-1
30 | //
31 | // seed_ must not be zero or M, or else all subsequent computed values
32 | // will be zero or M respectively. For all other values, seed_ will end
33 | // up cycling through every number in [1,M-1]
34 | uint64_t product = seed_ * A;
35 |
36 | // Compute (product % M) using the fact that ((x << 31) % M) == x.
37 | seed_ = static_cast((product >> 31) + (product & M));
38 | // The first reduction may overflow by 1 bit, so we may need to
39 | // repeat. mod == M is not possible; using > allows the faster
40 | // sign-bit-based test.
41 | if (seed_ > M) {
42 | seed_ -= M;
43 | }
44 | return seed_;
45 | }
46 | // Returns a uniformly distributed value in the range [0..n-1]
47 | // REQUIRES: n > 0
48 | uint32_t Uniform(int n) { return Next() % n; }
49 |
50 | // Randomly returns true ~"1/n" of the time, and false otherwise.
51 | // REQUIRES: n > 0
52 | bool OneIn(int n) { return (Next() % n) == 0; }
53 |
54 | // Skewed: pick "base" uniformly from range [0,max_log] and then
55 | // return "base" random bits. The effect is to pick a number in the
56 | // range [0,2^max_log-1] with exponential bias towards smaller numbers.
57 | uint32_t Skewed(int max_log) {
58 | return Uniform(1 << Uniform(max_log + 1));
59 | }
60 | };
61 |
62 | } // namespace leveldb
63 |
64 | #endif // STORAGE_LEVELDB_UTIL_RANDOM_H_
65 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/status.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include
6 | #include "port/port.h"
7 | #include "leveldb/status.h"
8 |
9 | namespace leveldb {
10 |
11 | const char* Status::CopyState(const char* state) {
12 | uint32_t size;
13 | memcpy(&size, state, sizeof(size));
14 | char* result = new char[size + 5];
15 | memcpy(result, state, size + 5);
16 | return result;
17 | }
18 |
19 | Status::Status(Code code, const Slice& msg, const Slice& msg2) {
20 | assert(code != kOk);
21 | const uint32_t len1 = msg.size();
22 | const uint32_t len2 = msg2.size();
23 | const uint32_t size = len1 + (len2 ? (2 + len2) : 0);
24 | char* result = new char[size + 5];
25 | memcpy(result, &size, sizeof(size));
26 | result[4] = static_cast(code);
27 | memcpy(result + 5, msg.data(), len1);
28 | if (len2) {
29 | result[5 + len1] = ':';
30 | result[6 + len1] = ' ';
31 | memcpy(result + 7 + len1, msg2.data(), len2);
32 | }
33 | state_ = result;
34 | }
35 |
36 | std::string Status::ToString() const {
37 | if (state_ == NULL) {
38 | return "OK";
39 | } else {
40 | char tmp[30];
41 | const char* type;
42 | switch (code()) {
43 | case kOk:
44 | type = "OK";
45 | break;
46 | case kNotFound:
47 | type = "NotFound: ";
48 | break;
49 | case kCorruption:
50 | type = "Corruption: ";
51 | break;
52 | case kNotSupported:
53 | type = "Not implemented: ";
54 | break;
55 | case kInvalidArgument:
56 | type = "Invalid argument: ";
57 | break;
58 | case kIOError:
59 | type = "IO error: ";
60 | break;
61 | default:
62 | snprintf(tmp, sizeof(tmp), "Unknown code(%d): ",
63 | static_cast(code()));
64 | type = tmp;
65 | break;
66 | }
67 | std::string result(type);
68 | uint32_t length;
69 | memcpy(&length, state_, sizeof(length));
70 | result.append(state_ + 5, length);
71 | return result;
72 | }
73 | }
74 |
75 | } // namespace leveldb
76 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/testharness.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/testharness.h"
6 |
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | namespace leveldb {
13 | namespace test {
14 |
15 | namespace {
16 | struct Test {
17 | const char* base;
18 | const char* name;
19 | void (*func)();
20 | };
21 | std::vector* tests;
22 | }
23 |
24 | bool RegisterTest(const char* base, const char* name, void (*func)()) {
25 | if (tests == NULL) {
26 | tests = new std::vector;
27 | }
28 | Test t;
29 | t.base = base;
30 | t.name = name;
31 | t.func = func;
32 | tests->push_back(t);
33 | return true;
34 | }
35 |
36 | int RunAllTests() {
37 | const char* matcher = getenv("LEVELDB_TESTS");
38 |
39 | int num = 0;
40 | if (tests != NULL) {
41 | for (size_t i = 0; i < tests->size(); i++) {
42 | const Test& t = (*tests)[i];
43 | if (matcher != NULL) {
44 | std::string name = t.base;
45 | name.push_back('.');
46 | name.append(t.name);
47 | if (strstr(name.c_str(), matcher) == NULL) {
48 | continue;
49 | }
50 | }
51 | fprintf(stderr, "==== Test %s.%s\n", t.base, t.name);
52 | (*t.func)();
53 | ++num;
54 | }
55 | }
56 | fprintf(stderr, "==== PASSED %d tests\n", num);
57 | return 0;
58 | }
59 |
60 | std::string TmpDir() {
61 | std::string dir;
62 | Status s = Env::Default()->GetTestDirectory(&dir);
63 | ASSERT_TRUE(s.ok()) << s.ToString();
64 | return dir;
65 | }
66 |
67 | int RandomSeed() {
68 | const char* env = getenv("TEST_RANDOM_SEED");
69 | int result = (env != NULL ? atoi(env) : 301);
70 | if (result <= 0) {
71 | result = 301;
72 | }
73 | return result;
74 | }
75 |
76 | } // namespace test
77 | } // namespace leveldb
78 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/testutil.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/testutil.h"
6 |
7 | #include "util/random.h"
8 |
9 | namespace leveldb {
10 | namespace test {
11 |
12 | Slice RandomString(Random* rnd, int len, std::string* dst) {
13 | dst->resize(len);
14 | for (int i = 0; i < len; i++) {
15 | (*dst)[i] = static_cast(' ' + rnd->Uniform(95)); // ' ' .. '~'
16 | }
17 | return Slice(*dst);
18 | }
19 |
20 | std::string RandomKey(Random* rnd, int len) {
21 | // Make sure to generate a wide variety of characters so we
22 | // test the boundary conditions for short-key optimizations.
23 | static const char kTestChars[] = {
24 | '\0', '\1', 'a', 'b', 'c', 'd', 'e', '\xfd', '\xfe', '\xff'
25 | };
26 | std::string result;
27 | for (int i = 0; i < len; i++) {
28 | result += kTestChars[rnd->Uniform(sizeof(kTestChars))];
29 | }
30 | return result;
31 | }
32 |
33 |
34 | extern Slice CompressibleString(Random* rnd, double compressed_fraction,
35 | size_t len, std::string* dst) {
36 | int raw = static_cast(len * compressed_fraction);
37 | if (raw < 1) raw = 1;
38 | std::string raw_data;
39 | RandomString(rnd, raw, &raw_data);
40 |
41 | // Duplicate the random data until we have filled "len" bytes
42 | dst->clear();
43 | while (dst->size() < len) {
44 | dst->append(raw_data);
45 | }
46 | dst->resize(len);
47 | return Slice(*dst);
48 | }
49 |
50 | } // namespace test
51 | } // namespace leveldb
52 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/testutil.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_
6 | #define STORAGE_LEVELDB_UTIL_TESTUTIL_H_
7 |
8 | #include "leveldb/env.h"
9 | #include "leveldb/slice.h"
10 | #include "util/random.h"
11 |
12 | namespace leveldb {
13 | namespace test {
14 |
15 | // Store in *dst a random string of length "len" and return a Slice that
16 | // references the generated data.
17 | extern Slice RandomString(Random* rnd, int len, std::string* dst);
18 |
19 | // Return a random key with the specified length that may contain interesting
20 | // characters (e.g. \x00, \xff, etc.).
21 | extern std::string RandomKey(Random* rnd, int len);
22 |
23 | // Store in *dst a string of length "len" that will compress to
24 | // "N*compressed_fraction" bytes and return a Slice that references
25 | // the generated data.
26 | extern Slice CompressibleString(Random* rnd, double compressed_fraction,
27 | size_t len, std::string* dst);
28 |
29 | // A wrapper that allows injection of errors.
30 | class ErrorEnv : public EnvWrapper {
31 | public:
32 | bool writable_file_error_;
33 | int num_writable_file_errors_;
34 |
35 | ErrorEnv() : EnvWrapper(Env::Default()),
36 | writable_file_error_(false),
37 | num_writable_file_errors_(0) { }
38 |
39 | virtual Status NewWritableFile(const std::string& fname,
40 | WritableFile** result) {
41 | if (writable_file_error_) {
42 | ++num_writable_file_errors_;
43 | *result = NULL;
44 | return Status::IOError(fname, "fake error");
45 | }
46 | return target()->NewWritableFile(fname, result);
47 | }
48 | };
49 |
50 | } // namespace test
51 | } // namespace leveldb
52 |
53 | #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_
54 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/win_logger.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | #include "util/win_logger.h"
6 |
7 | #include
8 |
9 | namespace leveldb {
10 |
11 | void WinLogger::Logv(const char* format, va_list ap) {
12 | const uint64_t thread_id = static_cast(::GetCurrentThreadId());
13 |
14 | // We try twice: the first time with a fixed-size stack allocated buffer,
15 | // and the second time with a much larger dynamically allocated buffer.
16 | char buffer[500];
17 |
18 | for (int iter = 0; iter < 2; iter++) {
19 | char* base;
20 | int bufsize;
21 | if (iter == 0) {
22 | bufsize = sizeof(buffer);
23 | base = buffer;
24 | } else {
25 | bufsize = 30000;
26 | base = new char[bufsize];
27 | }
28 |
29 | char* p = base;
30 | char* limit = base + bufsize;
31 |
32 | SYSTEMTIME st;
33 |
34 | // GetSystemTime returns UTC time, we want local time!
35 | ::GetLocalTime(&st);
36 |
37 | p += _snprintf_s(p, limit - p, _TRUNCATE,
38 | "%04d/%02d/%02d-%02d:%02d:%02d.%03d %llx ",
39 | st.wYear,
40 | st.wMonth,
41 | st.wDay,
42 | st.wHour,
43 | st.wMinute,
44 | st.wSecond,
45 | st.wMilliseconds,
46 | static_cast(thread_id));
47 |
48 | // Print the message
49 | if (p < limit) {
50 | va_list backup_ap = ap;
51 | p += vsnprintf(p, limit - p, format, backup_ap);
52 | va_end(backup_ap);
53 | }
54 |
55 | // Truncate to available space if necessary
56 | if (p >= limit) {
57 | if (iter == 0) {
58 | continue; // Try again with larger buffer
59 | } else {
60 | p = limit - 1;
61 | }
62 | }
63 |
64 | // Add newline if necessary
65 | if (p == base || p[-1] != '\n') {
66 | *p++ = '\n';
67 | }
68 |
69 | assert(p <= limit);
70 | fwrite(base, 1, p - base, file_);
71 | fflush(file_);
72 | if (base != buffer) {
73 | delete[] base;
74 | }
75 | break;
76 | }
77 | }
78 |
79 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Shared/util/win_logger.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 |
5 | // Logger implementation for Windows
6 |
7 | #ifndef STORAGE_LEVELDB_UTIL_WIN_LOGGER_H_
8 | #define STORAGE_LEVELDB_UTIL_WIN_LOGGER_H_
9 |
10 | #include
11 | #include "leveldb/env.h"
12 |
13 | namespace leveldb {
14 |
15 | class WinLogger : public Logger {
16 | private:
17 | FILE* file_;
18 | public:
19 | explicit WinLogger(FILE* f) : file_(f) { assert(file_); }
20 | virtual ~WinLogger() {
21 | fclose(file_);
22 | }
23 | virtual void Logv(const char* format, va_list ap);
24 |
25 | };
26 |
27 | }
28 | #endif // STORAGE_LEVELDB_UTIL_WIN_LOGGER_H_
29 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Assets/LockScreenLogo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxpert/LevelDBWinRT/632091525c2753103cdca8646ec0274b40bcc0b0/LevelDBWinRT.Tests/Assets/LockScreenLogo.scale-200.png
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Assets/SplashScreen.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxpert/LevelDBWinRT/632091525c2753103cdca8646ec0274b40bcc0b0/LevelDBWinRT.Tests/Assets/SplashScreen.scale-200.png
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Assets/Square150x150Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxpert/LevelDBWinRT/632091525c2753103cdca8646ec0274b40bcc0b0/LevelDBWinRT.Tests/Assets/Square150x150Logo.scale-200.png
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Assets/Square44x44Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxpert/LevelDBWinRT/632091525c2753103cdca8646ec0274b40bcc0b0/LevelDBWinRT.Tests/Assets/Square44x44Logo.scale-200.png
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Assets/Square44x44Logo.targetsize-24_altform-unplated.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxpert/LevelDBWinRT/632091525c2753103cdca8646ec0274b40bcc0b0/LevelDBWinRT.Tests/Assets/Square44x44Logo.targetsize-24_altform-unplated.png
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Assets/StoreLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxpert/LevelDBWinRT/632091525c2753103cdca8646ec0274b40bcc0b0/LevelDBWinRT.Tests/Assets/StoreLogo.png
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Assets/Wide310x150Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxpert/LevelDBWinRT/632091525c2753103cdca8646ec0274b40bcc0b0/LevelDBWinRT.Tests/Assets/Wide310x150Logo.scale-200.png
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/MultiThreadedTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Threading.Tasks;
3 | using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
4 |
5 | namespace LevelDBWinRT.Tests
6 | {
7 | [TestClass]
8 | public class MultiThreadedTests
9 | {
10 | [TestMethod]
11 | public void TestMultipulteConnectionWrite()
12 | {
13 | var tasks = new List(10);
14 | for (int i = 0; i < 10; i++)
15 | {
16 | tasks.Add(WriteRangeInTask("mtest1.db", i * 100, 50));
17 | }
18 |
19 | Task.WaitAll(tasks.ToArray());
20 | }
21 |
22 | [TestMethod]
23 | public void TestMultipuleWriteAndWriteBatch()
24 | {
25 | var tasks = new List(20);
26 | for (int i = 0; i < 10; i++)
27 | {
28 | tasks.Add(WriteRangeInTask("mtest1.db", i * 100, 50));
29 | }
30 |
31 | for (int i = 0; i < 10; i++)
32 | {
33 | tasks.Add(BatchWriteRangeInTask("mtest1.db", i * 100, 50));
34 | }
35 |
36 | Task.WaitAll(tasks.ToArray());
37 | }
38 |
39 |
40 | [TestMethod]
41 | public void TestMultipule()
42 | {
43 | var tasks = new List(30);
44 | for (int i = 0; i < 10; i++)
45 | {
46 | tasks.Add(WriteRangeInTask("mtest1.db", i * 100, 50));
47 | }
48 |
49 | for (int i = 0; i < 10; i++)
50 | {
51 | tasks.Add(BatchWriteRangeInTask("mtest1.db", i * 100, 50));
52 | }
53 |
54 |
55 | for (int i = 0; i < 10; i++)
56 | {
57 | tasks.Add(ReadRangeInTask("mtest1.db", i * 100, 50));
58 | }
59 |
60 | Task.WaitAll(tasks.ToArray());
61 | }
62 |
63 | private static Task WriteRangeInTask(string dbName, int offset, int limit)
64 | {
65 | return Task.Run(() =>
66 | {
67 | var writeOpts = new WriteOptions();
68 | using (var db = new DB(new Options { CreateIfMissing = true }, dbName))
69 | {
70 | for (int i = offset; i < limit; i++)
71 | {
72 | db.Put(writeOpts, Slice.FromString($"key::{i,20:D20}"), Slice.FromString($"{i,32}"));
73 | }
74 | }
75 | });
76 | }
77 |
78 | private static Task BatchWriteRangeInTask(string dbName, int offset, int limit)
79 | {
80 | return Task.Run(() =>
81 | {
82 | using (var db = new DB(new Options { CreateIfMissing = true }, dbName))
83 | using (var batch = new WriteBatch())
84 | {
85 | for (int i = offset; i < limit; i++)
86 | {
87 | batch.Put(Slice.FromString($"key::{i,20:D20}"), Slice.FromString($"{i,32}"));
88 | }
89 |
90 | db.Write(new WriteOptions { Sync = true }, batch);
91 | }
92 | });
93 | }
94 |
95 | private static Task ReadRangeInTask(string dbName, int offset, int limit)
96 | {
97 | return Task.Run(() =>
98 | {
99 | using (var db = new DB(new Options { CreateIfMissing = true }, dbName))
100 | {
101 | using (var snapshot = db.GetSnapshot())
102 | using (var itr = db.NewIterator(new ReadOptions { Snapshot = snapshot }))
103 | {
104 | itr.Seek(Slice.FromString($"key::{offset,20:D20}"));
105 | while (itr.Valid())
106 | {
107 | itr.Next();
108 | }
109 | }
110 | }
111 | });
112 | }
113 | }
114 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Package.appxmanifest:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
12 |
13 |
14 |
15 | LevelDBWinRT.Tests
16 | Zohaib
17 | Assets\StoreLogo.png
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
31 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("LevelDBWinRT.Tests")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("LevelDBWinRT.Tests")]
13 | [assembly: AssemblyCopyright("Copyright © 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 | [assembly: AssemblyMetadata("TargetPlatform","UAP")]
17 |
18 | // Version information for an assembly consists of the following four values:
19 | //
20 | // Major Version
21 | // Minor Version
22 | // Build Number
23 | // Revision
24 | //
25 | // You can specify all the values or you can default the Build and Revision Numbers
26 | // by using the '*' as shown below:
27 | // [assembly: AssemblyVersion("1.0.*")]
28 | [assembly: AssemblyVersion("1.0.0.0")]
29 | [assembly: AssemblyFileVersion("1.0.0.0")]
30 | [assembly: ComVisible(false)]
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/Properties/UnitTestApp.rd.xml:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 |
20 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/UnitTestApp.xaml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/UnitTestApp.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Runtime.InteropServices.WindowsRuntime;
6 | using Windows.ApplicationModel;
7 | using Windows.ApplicationModel.Activation;
8 | using Windows.Foundation;
9 | using Windows.Foundation.Collections;
10 | using Windows.UI.Xaml;
11 | using Windows.UI.Xaml.Controls;
12 | using Windows.UI.Xaml.Controls.Primitives;
13 | using Windows.UI.Xaml.Data;
14 | using Windows.UI.Xaml.Input;
15 | using Windows.UI.Xaml.Media;
16 | using Windows.UI.Xaml.Navigation;
17 |
18 | namespace LevelDBWinRT.Tests
19 | {
20 | ///
21 | /// Provides application-specific behavior to supplement the default Application class.
22 | ///
23 | sealed partial class App : Application
24 | {
25 | ///
26 | /// Initializes the singleton application object. This is the first line of authored code
27 | /// executed, and as such is the logical equivalent of main() or WinMain().
28 | ///
29 | public App()
30 | {
31 | this.InitializeComponent();
32 | this.Suspending += OnSuspending;
33 | }
34 |
35 | ///
36 | /// Invoked when the application is launched normally by the end user. Other entry points
37 | /// will be used such as when the application is launched to open a specific file.
38 | ///
39 | /// Details about the launch request and process.
40 | protected override void OnLaunched(LaunchActivatedEventArgs e)
41 | {
42 |
43 | #if DEBUG
44 | if (System.Diagnostics.Debugger.IsAttached)
45 | {
46 | this.DebugSettings.EnableFrameRateCounter = true;
47 | }
48 | #endif
49 |
50 | Frame rootFrame = Window.Current.Content as Frame;
51 |
52 | // Do not repeat app initialization when the Window already has content,
53 | // just ensure that the window is active
54 | if (rootFrame == null)
55 | {
56 | // Create a Frame to act as the navigation context and navigate to the first page
57 | rootFrame = new Frame();
58 |
59 | rootFrame.NavigationFailed += OnNavigationFailed;
60 |
61 | if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
62 | {
63 | //TODO: Load state from previously suspended application
64 | }
65 |
66 | // Place the frame in the current Window
67 | Window.Current.Content = rootFrame;
68 | }
69 |
70 | Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI();
71 |
72 | // Ensure the current window is active
73 | Window.Current.Activate();
74 |
75 | Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(e.Arguments);
76 | }
77 |
78 | ///
79 | /// Invoked when Navigation to a certain page fails
80 | ///
81 | /// The Frame which failed navigation
82 | /// Details about the navigation failure
83 | void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
84 | {
85 | throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
86 | }
87 |
88 | ///
89 | /// Invoked when application execution is being suspended. Application state is saved
90 | /// without knowing whether the application will be terminated or resumed with the contents
91 | /// of memory still intact.
92 | ///
93 | /// The source of the suspend request.
94 | /// Details about the suspend request.
95 | private void OnSuspending(object sender, SuspendingEventArgs e)
96 | {
97 | var deferral = e.SuspendingOperation.GetDeferral();
98 | //TODO: Save application state and stop any background activity
99 | deferral.Complete();
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/LevelDBWinRT.Tests/project.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
4 | },
5 | "frameworks": {
6 | "uap10.0": {}
7 | },
8 | "runtimes": {
9 | "win10-arm": {},
10 | "win10-arm-aot": {},
11 | "win10-x86": {},
12 | "win10-x86-aot": {},
13 | "win10-x64": {},
14 | "win10-x64-aot": {}
15 | }
16 | }
--------------------------------------------------------------------------------
/LevelDBWinRT.UAP/LevelDBWinRT.UAP.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 34e81d45-ecd9-4179-9633-3583282b8917
6 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tga;tiff;tif;png;wav;mfcribbon-ms
7 |
8 |
9 |
--------------------------------------------------------------------------------