Skip to content

Commit 422eec4

Browse files
更新简介说明
1 parent bd75ebe commit 422eec4

File tree

7 files changed

+50
-82
lines changed

7 files changed

+50
-82
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ else (WIN32)
3434
set(LEVELDB_PLATFORM_NAME LEVELDB_PLATFORM_POSIX)
3535
endif (WIN32)
3636

37-
option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" ON)
37+
option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" OFF)
3838
option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" ON)
3939
option(LEVELDB_INSTALL "Install LevelDB's header and library" ON)
4040

demo/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ target_include_directories(leveldb_exec
1313
PUBLIC ${PROJECT_SOURCE_DIR}/../include)
1414

1515

16-
add_executable(dataTest test.cpp)
16+
add_executable(test test.cpp)
17+
18+
target_link_libraries(test
19+
PUBLIC leveldb)
20+
21+
target_include_directories(test
22+
PUBLIC ${PROJECT_SOURCE_DIR}/../include)

demo/test.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
#include <utility>
1313
#include <thread>
1414
#include <set>
15-
15+
#include <iostream>
16+
#include <cassert>
17+
#include "leveldb/write_batch.h"
18+
#include "leveldb/db.h"
1619

1720
using namespace std;
1821

@@ -21,24 +24,8 @@ const uint64_t CREATE_SQL_STR_LEN = 1024 * 1024;
2124

2225
int main(int argc, char *argv[]) {
2326

24-
25-
std::set<std::string> newSet{};
26-
newSet.insert("3");
27-
newSet.insert("4");
28-
newSet.insert("5");
29-
newSet.insert("7");
30-
newSet.insert("1");
31-
newSet.insert("2");
32-
newSet.insert("2");
33-
34-
35-
for (auto& s : newSet) {
36-
std::cout << s << std::endl;
37-
}
38-
39-
40-
41-
27+
int *lpData = nullptr;
28+
std::cout << sizeof(*lpData) << std::endl;
4229

4330

4431
return 0;

doc/db/table_format.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ ifndef::rootpath[]
2121
endif::rootpath[]
2222

2323

24-
leveldb File format
25-
===================
24+
== leveldb File format
2625

2726
<beginning_of_file>
2827
[data block 1]
@@ -37,7 +36,7 @@ leveldb File format
3736
[Footer] (fixed size; starts at file_size - sizeof(Footer))
3837
<end_of_file>
3938

40-
The file contains internal pointers. Each such pointer is called
39+
The file contains internal pointers.Each such pointer is called
4140
a BlockHandle and contains the following information:
4241

4342
offset: varint64

doc/instruction/impl.adoc

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,8 @@ ifndef::rootpath[]
2121
endif::rootpath[]
2222

2323

24-
## Files
24+
== 基本概念
2525

2626

27-
### Log files
28-
29-
30-
31-
## Sorted tables
32-
33-
34-
35-
### Manifest
36-
37-
38-
### Current
39-
40-
41-
### Info logs
42-
43-
44-
### Others
45-
46-
47-
## Level 0
48-
49-
50-
51-
## Compactions
52-
53-
54-
55-
### Timing
56-
57-
58-
### Number of files
59-
60-
61-
## Recovery
62-
63-
64-
65-
## Garbage collection of files
66-
6727

6828

doc/instruction/readme.adoc

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ options.error_if_exists = true;
148148

149149
=== 执行结果 Status
150150

151-
在levelBD中你随处可见Status,该对象就是levelDB对返回值进行封装时候的结果,用来返回各种接口的执行情况
151+
在levelDB中你随处可见Status,该对象就是levelDB对返回值进行封装时候的结果,用来返回各种接口的执行情况
152152

153153
[source, cpp]
154154
----
@@ -157,35 +157,41 @@ if (!s.ok()) cerr << s.ToString() << endl;
157157
----
158158

159159

160-
## Closing A Database
160+
== 如何关闭一个levelDB数据库
161161

162-
When you are done with a database, just delete the database object. Example:
162+
当想关闭一个levelDB数据库时只需要调用delete将db删除即可。
163163

164-
```c++
164+
[source,c++]
165+
----
165166
... open the db as described above ...
166167
... do something with db ...
167168
delete db;
168-
```
169+
----
169170

170-
## Reads And Writes
171+
== 数据库的读写
171172

172-
The database provides Put, Delete, and Get methods to modify/query the database.
173-
For example, the following code moves the value stored under key1 to key2.
173+
levelDB提供了Put/Delete/Get等接口来对数据库进行操作。
174174

175-
```c++
175+
[source,c++]
176+
----
176177
std::string value;
178+
// 1. 去除key1的值
177179
leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);
180+
// 将key2的值设置为key1的值
178181
if (s.ok()) s = db->Put(leveldb::WriteOptions(), key2, value);
182+
// 删除key1
179183
if (s.ok()) s = db->Delete(leveldb::WriteOptions(), key1);
180-
```
184+
----
181185

182-
## Atomic Updates
186+
== 原子操作(事务类型)
183187

184-
Note that if the process dies after the Put of key2 but before the delete of
185-
key1, the same value may be left stored under multiple keys. Such problems can
186-
be avoided by using the `WriteBatch` class to atomically apply a set of updates:
188+
[listing]
189+
....
190+
上述将Key1的值设置给key2并删除Key1总共经过了三步,如果在删除key1之前数据库崩溃或被强制停掉了就会导致value存在key1和key2中。为了避免这种异常情况的出现,我们可以将上述三种情况作为一个原子操作。levelDB中可以通过WriteBatch来实现对多个步骤进行原子操作
191+
....
187192

188-
```c++
193+
[source,c++]
194+
----
189195
#include "leveldb/write_batch.h"
190196
...
191197
std::string value;
@@ -196,10 +202,10 @@ if (s.ok()) {
196202
batch.Put(key2, value);
197203
s = db->Write(leveldb::WriteOptions(), &batch);
198204
}
199-
```
205+
----
200206

201207
The `WriteBatch` holds a sequence of edits to be made to the database, and these
202-
edits within the batch are applied in order. Note that we called Delete before
208+
edits within the batch are applied in order.Note that we called Delete before
203209
Put so that if key1 is identical to key2, we do not end up erroneously dropping
204210
the value entirely.
205211

doc/utils/c++_common_knowledge.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ class NoDestructor {
107107
108108
----
109109

110+
111+
=== sizeof
112+
113+
需要注意的是,sizeof中* + 指针变量,并不是对指针变量的解引用,只是去除指针指向对象类型的意思,因此下面的代码并不会崩溃,而是给出指针指向对象的大小。
114+
[source, cpp]
115+
----
116+
int *lpData = nullptr;
117+
std::cout << sizeof(*lpData) << std::endl;
118+
----
119+
110120
*总结*
111121

112122
在整个levelDB中有很多地方都用了这个特性进行单例的创建,在这个实例的创建中使用了三个比较重要的C++知识点,alignof取结构体对齐长度,`new(placement new)` 来实现预分配内存的new,在结合 `aligned_storage` 来保证已有对象内存绝对大于等于需要申请对象的内存,然后经过饿汉式单例模式封装,经过以上几个步骤之后,一个能长久陪伴数据库句柄的对比工具便诞生了。

0 commit comments

Comments
 (0)