This repository was archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathleveldb_win.patch
1815 lines (1790 loc) · 47.7 KB
/
leveldb_win.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 7462829e7e53eea7e55c9a40d264f2ad819cd95c Mon Sep 17 00:00:00 2001
From: Andre <[email protected]>
Date: Wed, 31 May 2017 17:26:57 +0200
Subject: [PATCH] leveldb v0.12 windows patch, supported by cmake.
---
CMakeLists.txt | 283 +++++++++++++++++
db/c.cc | 6 +-
db/{c_test.c => c_test.cc} | 15 +-
db/corruption_test.cc | 33 +-
db/db_impl.cc | 2 +-
db/recovery_test.cc | 1 +
db/skiplist.h | 2 +-
db/version_set.h | 4 +-
port/port.h | 4 +-
port/port_win.cc | 188 +++++++++++
port/port_win.h | 170 ++++++++++
util/env_boost.cc | 753 +++++++++++++++++++++++++++++++++++++++++++++
util/win_logger.cc | 79 +++++
util/win_logger.h | 28 ++
14 files changed, 1553 insertions(+), 15 deletions(-)
create mode 100644 CMakeLists.txt
rename db/{c_test.c => c_test.cc} (98%)
create mode 100644 port/port_win.cc
create mode 100644 port/port_win.h
create mode 100644 util/env_boost.cc
create mode 100644 util/win_logger.cc
create mode 100644 util/win_logger.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..1ab6332
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,283 @@
+###########################################################################################
+# This file was taken from https://github.com/bureau14/leveldb/blob/master
+#
+# Keep the original copyright and license of this repository in mind.
+#
+# It was adapted by Andre Netzeband to work with LevelDB version 1.20.
+#
+###########################################################################################
+
+cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
+
+project(leveldb CXX)
+
+include(CTest)
+
+set(CMAKE_DEBUG_POSTFIX "d")
+
+set(Boost_USE_STATIC_LIBS ON)
+set(Boost_USE_MULTITHREAD ON)
+set(Boost_USE_STATIC_RUNTIME OFF)
+
+find_package(Boost COMPONENTS
+ date_time
+ filesystem
+ system
+ thread
+ REQUIRED)
+
+set(SNAPPY_LIBRARY "")
+
+string(REGEX MATCH "clang" CLANG ${CMAKE_CXX_COMPILER})
+
+if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
+ find_library(Pthread_LIBRARY pthread)
+ find_library(Realtime_LIBRARY rt)
+ # find library can be problematic with stdc++ which is why we hardwire the link
+ set(Stdcpp_LIBRARY stdc++)
+else(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
+ set(Pthread_LIBRARY "")
+ set(Realtime_LIBRARY "")
+ set(Stdcpp_LIBRARY "")
+endif(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
+
+include_directories(${Boost_INCLUDE_DIRS}
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ include)
+
+if(MSVC)
+ add_compile_options(
+ /D_CRT_SECURE_NO_WARNINGS
+ /wd4389 # signed/unsigned mismatch
+ /wd4800 # constructor never returns, potential memory leak because of a singleton pattern
+ /wd4722 # unreachable code because of singleton pattern
+ /wd4702 # bool cast performance warning
+ )
+else()
+ add_compile_options(
+ -Wno-sign-compare
+ -std=c++11
+ )
+endif()
+
+add_definitions(
+ -DLEVELDB_ATOMIC_PRESENT
+)
+
+set(LEVEL_DB_FILES
+ include/leveldb/c.h
+ include/leveldb/cache.h
+ include/leveldb/comparator.h
+ include/leveldb/db.h
+ include/leveldb/dumpfile.h
+ include/leveldb/env.h
+ include/leveldb/iterator.h
+ include/leveldb/filter_policy.h
+ include/leveldb/iterator.h
+ include/leveldb/options.h
+ include/leveldb/slice.h
+ include/leveldb/status.h
+ include/leveldb/table.h
+ include/leveldb/table_builder.h
+ include/leveldb/write_batch.h
+
+ db/builder.cc
+ db/builder.h
+ db/c.cc
+ db/db_impl.cc
+ db/db_impl.h
+ db/db_iter.cc
+ db/db_iter.h
+ db/dbformat.cc
+ db/dbformat.h
+ db/dumpfile.cc
+ db/filename.cc
+ db/filename.h
+ db/log_format.h
+ db/log_reader.cc
+ db/log_reader.h
+ db/log_writer.cc
+ db/log_writer.h
+ db/memtable.cc
+ db/memtable.h
+ db/repair.cc
+ db/skiplist.h
+ db/snapshot.h
+ db/table_cache.cc
+ db/table_cache.h
+ db/version_edit.cc
+ db/version_edit.h
+ db/version_set.cc
+ db/version_set.h
+ db/write_batch.cc
+ db/write_batch_internal.h
+
+ table/block.cc
+ table/block.h
+ table/block_builder.cc
+ table/block_builder.h
+ table/filter_block.cc
+ table/filter_block.h
+ table/format.cc
+ table/format.h
+ table/iterator.cc
+ table/iterator_wrapper.h
+ table/merger.cc
+ table/merger.h
+ table/table.cc
+ table/table_builder.cc
+ table/two_level_iterator.cc
+ table/two_level_iterator.h
+
+ util/arena.cc
+ util/arena.h
+ util/bloom.cc
+ util/cache.cc
+ util/coding.cc
+ util/coding.h
+ util/comparator.cc
+ util/crc32c.cc
+ util/crc32c.h
+ util/env.cc
+ util/filter_policy.cc
+ util/hash.cc
+ util/hash.h
+ util/histogram.cc
+ util/histogram.h
+ util/logging.cc
+ util/logging.h
+ util/mutexlock.h
+ util/options.cc
+ util/random.h
+ util/status.cc
+
+ port/port.h
+ )
+
+if(WIN32)
+ MESSAGE(STATUS "Enable Win32 port...")
+ list(APPEND LEVEL_DB_FILES
+ port/port_win.h
+ port/port_win.cc
+ util/win_logger.h
+ util/win_logger.cc
+ util/env_boost.cc)
+else()
+ MESSAGE(STATUS "Enable Posix port...")
+ list(APPEND LEVEL_DB_FILES
+ port/port_posix.h
+ port/port_posix.cc
+ util/posix_logger.h
+ util/env_posix.cc)
+endif()
+
+add_library(leveldb ${LEVEL_DB_FILES})
+
+target_include_directories(leveldb
+ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
+ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+target_link_libraries(leveldb
+ PRIVATE
+ ${Boost_LIBRARIES}
+ ${Pthread_LIBRARY}
+)
+
+add_executable(leveldbutil
+ db/leveldbutil.cc)
+
+target_link_libraries(leveldbutil
+ leveldb)
+
+set_target_properties(leveldbutil PROPERTIES
+ DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
+
+# we distribute the leveldbutil as it might be useful
+install(TARGETS leveldbutil leveldb
+ RUNTIME DESTINATION bin
+ LIBRARY DESTINATION lib
+ ARCHIVE DESTINATION lib)
+
+install(DIRECTORY include/leveldb DESTINATION include)
+
+##################################### TESTS #######################################
+# Every leveldb test file has to be compiled as an independant binary
+# because of the test framework used by leveldb.
+add_library(leveldb_test_rt STATIC
+ util/testutil.h
+ util/testutil.cc
+ util/testharness.h
+ util/testharness.cc)
+
+target_include_directories(leveldb_test_rt
+ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include
+)
+
+if(BUILD_SHARED_LIBS)
+ set_target_properties(leveldb_test_rt PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
+ set_target_properties(leveldb PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
+endif(BUILD_SHARED_LIBS)
+
+add_custom_target(RUN_LEVELDB_UNIT_TESTS
+ COMMAND ${CMAKE_CTEST_COMMAND}
+ --build-config ${CMAKE_CFG_INTDIR}
+ --output-log LevelDB_test_${CMAKE_CFG_INTDIR}.log
+ --output-on-failure
+ --tests-regex leveldb
+ COMMENT "Running all LevelDB unit tests"
+)
+
+function(LEVELDB_ADD_TEST TESTNAME TESTFILE)
+ if(NOT TESTNAME)
+ message(SEND_ERROR "Error: LEVELDB_ADD_TEST called without test name")
+ return()
+ endif(NOT TESTNAME)
+
+ if(NOT TESTFILE)
+ message(SEND_ERROR "Error: LEVELDB_ADD_TEST called without test file")
+ return()
+ endif(NOT TESTFILE)
+
+ add_executable(leveldb_${TESTNAME}_test
+ ${TESTFILE})
+
+ target_link_libraries(leveldb_${TESTNAME}_test
+ leveldb_test_rt
+ leveldb)
+
+ set_target_properties(leveldb_${TESTNAME}_test PROPERTIES
+ DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
+
+ add_test(NAME leveldb_${TESTNAME}_test COMMAND leveldb_${TESTNAME}_test)
+
+ add_dependencies(RUN_LEVELDB_UNIT_TESTS leveldb_${TESTNAME}_test)
+endfunction(LEVELDB_ADD_TEST)
+
+# IMPORTANT: Commented a test that fails randomly.
+LEVELDB_ADD_TEST(autocompact db/autocompact_test.cc)
+LEVELDB_ADD_TEST(c db/c_test.cc)
+LEVELDB_ADD_TEST(corruption db/corruption_test.cc)
+LEVELDB_ADD_TEST(db_bench db/db_bench.cc)
+LEVELDB_ADD_TEST(db db/db_test.cc)
+LEVELDB_ADD_TEST(dbformat db/dbformat_test.cc)
+LEVELDB_ADD_TEST(ddfaultinjection db/fault_injection_test.cc)
+LEVELDB_ADD_TEST(filename db/filename_test.cc)
+LEVELDB_ADD_TEST(log db/log_test.cc)
+LEVELDB_ADD_TEST(dbrecovery db/recovery_test.cc)
+LEVELDB_ADD_TEST(skiplist db/skiplist_test.cc)
+LEVELDB_ADD_TEST(version_edit db/version_edit_test.cc)
+LEVELDB_ADD_TEST(version_set db/version_set_test.cc)
+LEVELDB_ADD_TEST(write_batch db/write_batch_test.cc)
+
+LEVELDB_ADD_TEST(filter_block table/filter_block_test.cc)
+LEVELDB_ADD_TEST(table table/table_test.cc)
+
+LEVELDB_ADD_TEST(arena util/arena_test.cc)
+LEVELDB_ADD_TEST(bloom util/bloom_test.cc)
+LEVELDB_ADD_TEST(cache util/cache_test.cc)
+LEVELDB_ADD_TEST(coding util/coding_test.cc)
+LEVELDB_ADD_TEST(crc32 util/crc32c_test.cc)
+LEVELDB_ADD_TEST(env util/env_test.cc)
+LEVELDB_ADD_TEST(hash util/hash_test.cc)
+
diff --git a/db/c.cc b/db/c.cc
index 08ff0ad..4cc6b25 100644
--- a/db/c.cc
+++ b/db/c.cc
@@ -5,7 +5,11 @@
#include "leveldb/c.h"
#include <stdlib.h>
-#include <unistd.h>
+
+#ifndef WIN32
+# include <unistd.h>
+#endif
+
#include "leveldb/cache.h"
#include "leveldb/comparator.h"
#include "leveldb/db.h"
diff --git a/db/c_test.c b/db/c_test.cc
similarity index 98%
rename from db/c_test.c
rename to db/c_test.cc
index 7cd5ee0..5365728 100644
--- a/db/c_test.c
+++ b/db/c_test.cc
@@ -9,7 +9,12 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
-#include <unistd.h>
+
+#ifndef WIN32
+# include <unistd.h>
+#else
+ uint32_t geteuid(void) { return 0; }
+#endif
const char* phase = "";
static char dbname[200];
@@ -22,7 +27,13 @@ static void StartPhase(const char* name) {
static const char* GetTempDir(void) {
const char* ret = getenv("TEST_TMPDIR");
if (ret == NULL || ret[0] == '\0')
+ {
+#ifndef WIN32
ret = "/tmp";
+#else
+ ret = ".";
+#endif
+ }
return ret;
}
@@ -141,7 +152,7 @@ static char* FilterCreate(
int num_keys,
size_t* filter_length) {
*filter_length = 4;
- char* result = malloc(4);
+ char* result = (char*)malloc(4);
memcpy(result, "fake", 4);
return result;
}
diff --git a/db/corruption_test.cc b/db/corruption_test.cc
index 37a484d..086310f 100644
--- a/db/corruption_test.cc
+++ b/db/corruption_test.cc
@@ -46,14 +46,22 @@ class CorruptionTest {
}
~CorruptionTest() {
- delete db_;
+ Close();
DestroyDB(dbname_, Options());
delete tiny_cache_;
}
+
+ void Close()
+ {
+ if (db_ != NULL)
+ {
+ delete(db_);
+ db_ = NULL;
+ }
+ }
Status TryReopen() {
- delete db_;
- db_ = NULL;
+ Close();
return DB::Open(options_, dbname_, &db_);
}
@@ -62,8 +70,7 @@ class CorruptionTest {
}
void RepairDB() {
- delete db_;
- db_ = NULL;
+ Close();
ASSERT_OK(::leveldb::RepairDB(dbname_, options_));
}
@@ -170,7 +177,7 @@ class CorruptionTest {
contents[i + offset] ^= 0x80;
}
s = WriteStringToFile(Env::Default(), contents, fname);
- ASSERT_TRUE(s.ok()) << s.ToString();
+ ASSERT_TRUE(s.ok()) << s.ToString();
}
int Property(const std::string& name) {
@@ -240,7 +247,9 @@ TEST(CorruptionTest, TableFile) {
dbi->TEST_CompactRange(0, NULL, NULL);
dbi->TEST_CompactRange(1, NULL, NULL);
+ Close();
Corrupt(kTableFile, 100, 1);
+ Reopen();
Check(90, 99);
}
@@ -254,6 +263,7 @@ TEST(CorruptionTest, TableFileRepair) {
dbi->TEST_CompactRange(0, NULL, NULL);
dbi->TEST_CompactRange(1, NULL, NULL);
+ Close();
Corrupt(kTableFile, 100, 1);
RepairDB();
Reopen();
@@ -265,6 +275,7 @@ TEST(CorruptionTest, TableFileIndexData) {
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
dbi->TEST_CompactMemTable();
+ Close();
Corrupt(kTableFile, -2000, 500);
Reopen();
Check(5000, 9999);
@@ -322,7 +333,9 @@ TEST(CorruptionTest, CompactionInputError) {
const int last = config::kMaxMemCompactLevel;
ASSERT_EQ(1, Property("leveldb.num-files-at-level" + NumberToString(last)));
+ Close();
Corrupt(kTableFile, 100, 1);
+ Reopen();
Check(5, 9);
// Force compactions by writing lots of values
@@ -340,7 +353,10 @@ TEST(CorruptionTest, CompactionInputErrorParanoid) {
for (int i = 0; i < 2; i++) {
Build(10);
dbi->TEST_CompactMemTable();
+ Close();
Corrupt(kTableFile, 100, 1);
+ Reopen();
+ dbi = reinterpret_cast<DBImpl*>(db_);
env_.SleepForMicroseconds(100000);
}
dbi->CompactRange(NULL, NULL);
@@ -355,8 +371,11 @@ TEST(CorruptionTest, UnrelatedKeys) {
Build(10);
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
dbi->TEST_CompactMemTable();
+ Close();
Corrupt(kTableFile, 100, 1);
-
+ Reopen();
+ dbi = reinterpret_cast<DBImpl*>(db_);
+
std::string tmp1, tmp2;
ASSERT_OK(db_->Put(WriteOptions(), Key(1000, &tmp1), Value(1000, &tmp2)));
std::string v;
diff --git a/db/db_impl.cc b/db/db_impl.cc
index f43ad76..6b326d2 100644
--- a/db/db_impl.cc
+++ b/db/db_impl.cc
@@ -1081,7 +1081,7 @@ Iterator* DBImpl::NewInternalIterator(const ReadOptions& options,
}
versions_->current()->AddIterators(options, &list);
Iterator* internal_iter =
- NewMergingIterator(&internal_comparator_, &list[0], list.size());
+ NewMergingIterator(&internal_comparator_, &list[0], (int)list.size());
versions_->current()->Ref();
cleanup->mu = &mutex_;
diff --git a/db/recovery_test.cc b/db/recovery_test.cc
index 9596f42..027300f 100644
--- a/db/recovery_test.cc
+++ b/db/recovery_test.cc
@@ -205,6 +205,7 @@ TEST(RecoveryTest, LargeManifestCompacted) {
TEST(RecoveryTest, NoLogFiles) {
ASSERT_OK(Put("foo", "bar"));
+ Close();
ASSERT_EQ(1, DeleteLogFiles());
Open();
ASSERT_EQ("NOT_FOUND", Get("foo"));
diff --git a/db/skiplist.h b/db/skiplist.h
index 8bd7776..9e61ead 100644
--- a/db/skiplist.h
+++ b/db/skiplist.h
@@ -357,7 +357,7 @@ void SkipList<Key,Comparator>::Insert(const Key& key) {
// the loop below. In the former case the reader will
// immediately drop to the next level since NULL sorts after all
// keys. In the latter case the reader will use the new node.
- max_height_.NoBarrier_Store(reinterpret_cast<void*>(height));
+ max_height_.NoBarrier_Store(reinterpret_cast<void*>(height)); // Todo: Is this cast right on 64bit systems?
}
x = NewNode(key, height);
diff --git a/db/version_set.h b/db/version_set.h
index c4e7ac3..7560262 100644
--- a/db/version_set.h
+++ b/db/version_set.h
@@ -108,7 +108,7 @@ class Version {
int PickLevelForMemTableOutput(const Slice& smallest_user_key,
const Slice& largest_user_key);
- int NumFiles(int level) const { return files_[level].size(); }
+ int NumFiles(int level) const { return (int)files_[level].size(); }
// Return a human readable string that describes this version's contents.
std::string DebugString() const;
@@ -334,7 +334,7 @@ class Compaction {
VersionEdit* edit() { return &edit_; }
// "which" must be either 0 or 1
- int num_input_files(int which) const { return inputs_[which].size(); }
+ int num_input_files(int which) const { return (int)inputs_[which].size(); }
// Return the ith input file at "level()+which" ("which" must be 0 or 1).
FileMetaData* input(int which, int i) const { return inputs_[which][i]; }
diff --git a/port/port.h b/port/port.h
index e667db4..0649c7a 100644
--- a/port/port.h
+++ b/port/port.h
@@ -10,7 +10,9 @@
// Include the appropriate platform specific file below. If you are
// porting to a new platform, see "port_example.h" for documentation
// of what the new port_<platform>.h file must provide.
-#if defined(LEVELDB_PLATFORM_POSIX)
+#if defined(WIN32)
+# include "port/port_win.h"
+#elif defined(LEVELDB_PLATFORM_POSIX)
# include "port/port_posix.h"
#elif defined(LEVELDB_PLATFORM_CHROMIUM)
# include "port/port_chromium.h"
diff --git a/port/port_win.cc b/port/port_win.cc
new file mode 100644
index 0000000..f5edc84
--- /dev/null
+++ b/port/port_win.cc
@@ -0,0 +1,188 @@
+// LevelDB Copyright (c) 2011 The LevelDB Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file. See the AUTHORS file for names of contributors.
+//
+// See port_example.h for documentation for the following types/functions.
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+// * Neither the name of the University of California, Berkeley nor the
+// names of its contributors may be used to endorse or promote products
+// derived from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
+// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#include "port/port_win.h"
+
+#include <windows.h>
+#include <cassert>
+
+namespace leveldb {
+namespace port {
+
+Mutex::Mutex() :
+ mutex_(::CreateMutex(NULL, FALSE, NULL)) {
+ assert(mutex_);
+}
+
+Mutex::~Mutex() {
+ assert(mutex_);
+ ::CloseHandle(mutex_);
+}
+
+void Mutex::Lock() {
+ assert(mutex_);
+ ::WaitForSingleObject(mutex_, INFINITE);
+}
+
+void Mutex::Unlock() {
+ assert(mutex_);
+ ::ReleaseMutex(mutex_);
+}
+
+void Mutex::AssertHeld() {
+ assert(mutex_);
+ assert(1);
+}
+
+CondVar::CondVar(Mutex* mu) :
+ waiting_(0),
+ mu_(mu),
+ sema_(::CreateSemaphore(NULL, 0, 0x7fffffff, NULL)),
+ event_(::CreateEvent(NULL, FALSE, FALSE, NULL)),
+ broadcasted_(false){
+ assert(mu_);
+}
+
+CondVar::~CondVar() {
+ ::CloseHandle(sema_);
+ ::CloseHandle(event_);
+}
+
+void CondVar::Wait() {
+ wait_mtx_.Lock();
+ ++waiting_;
+ assert(waiting_ > 0);
+ wait_mtx_.Unlock();
+
+ ::SignalObjectAndWait(mu_->mutex_, sema_, INFINITE, FALSE);
+
+ wait_mtx_.Lock();
+ bool last = broadcasted_ && (--waiting_ == 0);
+ assert(waiting_ >= 0);
+ wait_mtx_.Unlock();
+
+ // we leave this function with the mutex held
+ if (last)
+ {
+ ::SignalObjectAndWait(event_, mu_->mutex_, INFINITE, FALSE);
+ }
+ else
+ {
+ ::WaitForSingleObject(mu_->mutex_, INFINITE);
+ }
+}
+
+void CondVar::Signal() {
+ wait_mtx_.Lock();
+ bool waiters = waiting_ > 0;
+ wait_mtx_.Unlock();
+
+ if (waiters)
+ {
+ ::ReleaseSemaphore(sema_, 1, 0);
+ }
+}
+
+void CondVar::SignalAll() {
+ wait_mtx_.Lock();
+
+ broadcasted_ = (waiting_ > 0);
+
+ if (broadcasted_)
+ {
+ // release all
+ ::ReleaseSemaphore(sema_, waiting_, 0);
+ wait_mtx_.Unlock();
+ ::WaitForSingleObject(event_, INFINITE);
+ broadcasted_ = false;
+ }
+ else
+ {
+ wait_mtx_.Unlock();
+ }
+}
+
+AtomicPointer::AtomicPointer(void* v) {
+ Release_Store(v);
+}
+
+void* AtomicPointer::Acquire_Load() const {
+ void * p = nullptr;
+ InterlockedExchangePointer(&p, rep_);
+ return p;
+}
+
+void AtomicPointer::Release_Store(void* v) {
+ InterlockedExchangePointer(&rep_, v);
+}
+
+void* AtomicPointer::NoBarrier_Load() const {
+ return rep_;
+}
+
+void AtomicPointer::NoBarrier_Store(void* v) {
+ rep_ = v;
+}
+
+enum InitializationState
+{
+ Uninitialized = 0,
+ Running = 1,
+ Initialized = 2
+};
+
+void InitOnce(OnceType* once, void (*initializer)()) {
+
+ static_assert(Uninitialized == LEVELDB_ONCE_INIT, "Invalid uninitialized state value");
+
+ InitializationState state = static_cast<InitializationState>(InterlockedCompareExchange(once, Running, Uninitialized));
+
+ if (state == Uninitialized) {
+ initializer();
+ *once = Initialized;
+ }
+
+ if (state == Running) {
+ while(*once != Initialized) {
+ Sleep(0); // yield
+ }
+ }
+
+ assert(*once == Initialized);
+}
+
+// ToDo: For sure we can also accelerate CRC32 calculation on many windows systems.
+// But this is not the most important feature for now.
+uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size) {
+ return 0;
+}
+
+}
+}
diff --git a/port/port_win.h b/port/port_win.h
new file mode 100644
index 0000000..ea9a487
--- /dev/null
+++ b/port/port_win.h
@@ -0,0 +1,170 @@
+// LevelDB Copyright (c) 2011 The LevelDB Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file. See the AUTHORS file for names of contributors.
+//
+// See port_example.h for documentation for the following types/functions.
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+// * Neither the name of the University of California, Berkeley nor the
+// names of its contributors may be used to endorse or promote products
+// derived from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
+// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#ifndef STORAGE_LEVELDB_PORT_PORT_WIN_H_
+#define STORAGE_LEVELDB_PORT_PORT_WIN_H_
+
+#if _MSC_VER < 1900
+#define snprintf _snprintf
+#endif
+#define close _close
+#define fread_unlocked _fread_nolock
+
+#ifdef SNAPPY
+#include <snappy.h>
+#endif
+
+#include <string>
+
+#include <stdint.h>
+
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
+
+namespace leveldb {
+namespace port {
+
+// Windows is little endian (for now :p)
+static const bool kLittleEndian = true;
+
+class CondVar;
+
+class Mutex {
+ public:
+ Mutex();
+ ~Mutex();
+
+ void Lock();
+ void Unlock();
+ void AssertHeld();
+
+ private:
+ friend class CondVar;
+ // critical sections are more efficient than mutexes
+ // but they are not recursive and can only be used to synchronize threads
+ // within the same process
+ // additionnaly they cannot be used with SignalObjectAndWait that we use for
+ // CondVar
+ // we use opaque void * to avoid including windows.h in port_win.h
+ void* mutex_;
+
+ // No copying
+ Mutex(const Mutex&);
+ void operator=(const Mutex&);
+};
+
+// the Win32 API offers a dependable condition variable mechanism, but only
+// starting with
+// Windows 2008 and Vista
+// no matter what we will implement our own condition variable with a semaphore
+// implementation as described in a paper written by Douglas C. Schmidt and
+// Irfan Pyarali
+class CondVar {
+ public:
+ explicit CondVar(Mutex* mu);
+ ~CondVar();
+ void Wait();
+ void Signal();
+ void SignalAll();
+
+ private:
+ Mutex* mu_;
+
+ Mutex wait_mtx_;
+ long waiting_;
+
+ void* sema_;
+ void* event_;
+
+ bool broadcasted_;
+};
+
+// Storage for a lock-free pointer
+class AtomicPointer {
+ private:
+ void* rep_;
+
+ public:
+ AtomicPointer() : rep_(nullptr) {}
+ explicit AtomicPointer(void* v);
+ void* Acquire_Load() const;
+
+ void Release_Store(void* v);
+
+ void* NoBarrier_Load() const;
+
+ void NoBarrier_Store(void* v);
+};
+
+typedef volatile long OnceType;
+#define LEVELDB_ONCE_INIT (0)
+
+extern void InitOnce(OnceType* once, void (*initializer)());
+
+inline bool Snappy_Compress(const char* input, size_t length,
+ ::std::string* output) {
+#ifdef SNAPPY
+ output->resize(snappy::MaxCompressedLength(length));
+ size_t outlen;
+ snappy::RawCompress(input, length, &(*output)[0], &outlen);
+ output->resize(outlen);
+ return true;
+#endif
+
+ return false;
+}
+
+inline bool Snappy_GetUncompressedLength(const char* input, size_t length,
+ size_t* result) {
+#ifdef SNAPPY
+ return snappy::GetUncompressedLength(input, length, result);
+#else
+ return false;
+#endif
+}
+
+inline bool Snappy_Uncompress(const char* input, size_t length, char* output) {
+#ifdef SNAPPY
+ return snappy::RawUncompress(input, length, output);
+#else
+ return false;
+#endif
+}
+
+inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
+ return false;
+}
+
+uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
+
+}
+}
+
+#endif // STORAGE_LEVELDB_PORT_PORT_WIN_H_
diff --git a/util/env_boost.cc b/util/env_boost.cc
new file mode 100644
index 0000000..76c2cb4
--- /dev/null
+++ b/util/env_boost.cc
@@ -0,0 +1,753 @@
+// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file. See the AUTHORS file for names of contributors.
+
+#include <atomic>
+#include <chrono>
+#include <cstdint>
+#include <deque>
+#include <set>
+#include <thread>
+#include <functional>
+#include <memory>
+#include <condition_variable>
+#include <mutex>
+
+#ifdef WIN32
+#include <windows.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <io.h>
+#else
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <time.h>
+#include <unistd.h>
+#endif
+#if defined(LEVELDB_PLATFORM_ANDROID)
+#include <sys/stat.h>
+#endif
+#include "leveldb/env.h"
+#include "leveldb/slice.h"
+
+#ifdef WIN32
+#include "util/win_logger.h"
+#else
+#include "util/posix_logger.h"
+#endif
+#include "port/port.h"
+#include "util/logging.h"
+
+#ifdef __linux
+#include <sys/sysinfo.h>
+#include <linux/unistd.h>
+#endif
+