couchbase 3.0.0.alpha.1 → 3.0.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/tests-6.0.3.yml +49 -0
  3. data/.github/workflows/tests.yml +47 -0
  4. data/.gitmodules +3 -0
  5. data/.idea/dictionaries/gem_terms.xml +5 -0
  6. data/.idea/inspectionProfiles/Project_Default.xml +1 -0
  7. data/.idea/vcs.xml +1 -0
  8. data/Gemfile +1 -0
  9. data/README.md +55 -2
  10. data/Rakefile +18 -0
  11. data/bin/init-cluster +62 -0
  12. data/bin/setup +1 -0
  13. data/couchbase.gemspec +3 -2
  14. data/examples/crud.rb +1 -2
  15. data/examples/managing_buckets.rb +47 -0
  16. data/examples/managing_collections.rb +58 -0
  17. data/examples/managing_query_indexes.rb +63 -0
  18. data/examples/query.rb +3 -2
  19. data/examples/query_with_consistency.rb +76 -0
  20. data/examples/subdocument.rb +23 -1
  21. data/ext/.clang-format +1 -1
  22. data/ext/.idea/dictionaries/couchbase_terms.xml +2 -0
  23. data/ext/.idea/vcs.xml +1 -0
  24. data/ext/CMakeLists.txt +30 -12
  25. data/ext/build_version.hxx.in +26 -0
  26. data/ext/couchbase/bucket.hxx +69 -8
  27. data/ext/couchbase/cluster.hxx +70 -54
  28. data/ext/couchbase/collections_manifest.hxx +3 -3
  29. data/ext/couchbase/configuration.hxx +14 -0
  30. data/ext/couchbase/couchbase.cxx +2044 -383
  31. data/ext/couchbase/{operations/document_id.hxx → document_id.hxx} +5 -4
  32. data/ext/couchbase/io/http_message.hxx +5 -1
  33. data/ext/couchbase/io/http_parser.hxx +2 -1
  34. data/ext/couchbase/io/http_session.hxx +6 -3
  35. data/ext/couchbase/io/{binary_message.hxx → mcbp_message.hxx} +15 -12
  36. data/ext/couchbase/io/mcbp_parser.hxx +99 -0
  37. data/ext/couchbase/io/{key_value_session.hxx → mcbp_session.hxx} +200 -95
  38. data/ext/couchbase/io/session_manager.hxx +37 -22
  39. data/ext/couchbase/mutation_token.hxx +2 -1
  40. data/ext/couchbase/operations.hxx +38 -8
  41. data/ext/couchbase/operations/bucket_create.hxx +138 -0
  42. data/ext/couchbase/operations/bucket_drop.hxx +65 -0
  43. data/ext/couchbase/operations/bucket_flush.hxx +65 -0
  44. data/ext/couchbase/operations/bucket_get.hxx +69 -0
  45. data/ext/couchbase/operations/bucket_get_all.hxx +62 -0
  46. data/ext/couchbase/operations/bucket_settings.hxx +111 -0
  47. data/ext/couchbase/operations/bucket_update.hxx +115 -0
  48. data/ext/couchbase/operations/cluster_developer_preview_enable.hxx +60 -0
  49. data/ext/couchbase/operations/collection_create.hxx +86 -0
  50. data/ext/couchbase/operations/collection_drop.hxx +82 -0
  51. data/ext/couchbase/operations/command.hxx +10 -10
  52. data/ext/couchbase/operations/document_decrement.hxx +80 -0
  53. data/ext/couchbase/operations/document_exists.hxx +80 -0
  54. data/ext/couchbase/operations/{get.hxx → document_get.hxx} +4 -2
  55. data/ext/couchbase/operations/document_get_and_lock.hxx +64 -0
  56. data/ext/couchbase/operations/document_get_and_touch.hxx +64 -0
  57. data/ext/couchbase/operations/document_increment.hxx +80 -0
  58. data/ext/couchbase/operations/document_insert.hxx +74 -0
  59. data/ext/couchbase/operations/{lookup_in.hxx → document_lookup_in.hxx} +2 -2
  60. data/ext/couchbase/operations/{mutate_in.hxx → document_mutate_in.hxx} +11 -2
  61. data/ext/couchbase/operations/{query.hxx → document_query.hxx} +101 -6
  62. data/ext/couchbase/operations/document_remove.hxx +67 -0
  63. data/ext/couchbase/operations/document_replace.hxx +76 -0
  64. data/ext/couchbase/operations/{upsert.hxx → document_touch.hxx} +14 -14
  65. data/ext/couchbase/operations/{remove.hxx → document_unlock.hxx} +12 -10
  66. data/ext/couchbase/operations/document_upsert.hxx +74 -0
  67. data/ext/couchbase/operations/query_index_build_deferred.hxx +85 -0
  68. data/ext/couchbase/operations/query_index_create.hxx +134 -0
  69. data/ext/couchbase/operations/query_index_drop.hxx +108 -0
  70. data/ext/couchbase/operations/query_index_get_all.hxx +106 -0
  71. data/ext/couchbase/operations/scope_create.hxx +81 -0
  72. data/ext/couchbase/operations/scope_drop.hxx +79 -0
  73. data/ext/couchbase/operations/scope_get_all.hxx +72 -0
  74. data/ext/couchbase/protocol/client_opcode.hxx +35 -0
  75. data/ext/couchbase/protocol/client_request.hxx +56 -9
  76. data/ext/couchbase/protocol/client_response.hxx +52 -15
  77. data/ext/couchbase/protocol/cmd_cluster_map_change_notification.hxx +81 -0
  78. data/ext/couchbase/protocol/cmd_decrement.hxx +187 -0
  79. data/ext/couchbase/protocol/cmd_exists.hxx +171 -0
  80. data/ext/couchbase/protocol/cmd_get.hxx +31 -8
  81. data/ext/couchbase/protocol/cmd_get_and_lock.hxx +142 -0
  82. data/ext/couchbase/protocol/cmd_get_and_touch.hxx +142 -0
  83. data/ext/couchbase/protocol/cmd_get_cluster_config.hxx +16 -3
  84. data/ext/couchbase/protocol/cmd_get_collections_manifest.hxx +16 -3
  85. data/ext/couchbase/protocol/cmd_get_error_map.hxx +16 -3
  86. data/ext/couchbase/protocol/cmd_hello.hxx +24 -8
  87. data/ext/couchbase/protocol/cmd_increment.hxx +187 -0
  88. data/ext/couchbase/protocol/cmd_info.hxx +1 -0
  89. data/ext/couchbase/protocol/cmd_insert.hxx +172 -0
  90. data/ext/couchbase/protocol/cmd_lookup_in.hxx +28 -13
  91. data/ext/couchbase/protocol/cmd_mutate_in.hxx +65 -13
  92. data/ext/couchbase/protocol/cmd_remove.hxx +59 -4
  93. data/ext/couchbase/protocol/cmd_replace.hxx +172 -0
  94. data/ext/couchbase/protocol/cmd_sasl_auth.hxx +15 -3
  95. data/ext/couchbase/protocol/cmd_sasl_list_mechs.hxx +15 -3
  96. data/ext/couchbase/protocol/cmd_sasl_step.hxx +15 -3
  97. data/ext/couchbase/protocol/cmd_select_bucket.hxx +14 -2
  98. data/ext/couchbase/protocol/cmd_touch.hxx +102 -0
  99. data/ext/couchbase/protocol/cmd_unlock.hxx +95 -0
  100. data/ext/couchbase/protocol/cmd_upsert.hxx +50 -14
  101. data/ext/couchbase/protocol/durability_level.hxx +67 -0
  102. data/ext/couchbase/protocol/frame_info_id.hxx +187 -0
  103. data/ext/couchbase/protocol/hello_feature.hxx +137 -0
  104. data/ext/couchbase/protocol/server_opcode.hxx +57 -0
  105. data/ext/couchbase/protocol/server_request.hxx +122 -0
  106. data/ext/couchbase/protocol/unsigned_leb128.h +15 -15
  107. data/ext/couchbase/utils/byteswap.hxx +1 -2
  108. data/ext/couchbase/utils/url_codec.hxx +225 -0
  109. data/ext/couchbase/version.hxx +3 -1
  110. data/ext/extconf.rb +4 -1
  111. data/ext/test/main.cxx +37 -113
  112. data/ext/third_party/snappy/.appveyor.yml +36 -0
  113. data/ext/third_party/snappy/.gitignore +8 -0
  114. data/ext/third_party/snappy/.travis.yml +98 -0
  115. data/ext/third_party/snappy/AUTHORS +1 -0
  116. data/ext/third_party/snappy/CMakeLists.txt +345 -0
  117. data/ext/third_party/snappy/CONTRIBUTING.md +26 -0
  118. data/ext/third_party/snappy/COPYING +54 -0
  119. data/ext/third_party/snappy/NEWS +188 -0
  120. data/ext/third_party/snappy/README.md +148 -0
  121. data/ext/third_party/snappy/cmake/SnappyConfig.cmake.in +33 -0
  122. data/ext/third_party/snappy/cmake/config.h.in +59 -0
  123. data/ext/third_party/snappy/docs/README.md +72 -0
  124. data/ext/third_party/snappy/format_description.txt +110 -0
  125. data/ext/third_party/snappy/framing_format.txt +135 -0
  126. data/ext/third_party/snappy/snappy-c.cc +90 -0
  127. data/ext/third_party/snappy/snappy-c.h +138 -0
  128. data/ext/third_party/snappy/snappy-internal.h +315 -0
  129. data/ext/third_party/snappy/snappy-sinksource.cc +121 -0
  130. data/ext/third_party/snappy/snappy-sinksource.h +182 -0
  131. data/ext/third_party/snappy/snappy-stubs-internal.cc +42 -0
  132. data/ext/third_party/snappy/snappy-stubs-internal.h +493 -0
  133. data/ext/third_party/snappy/snappy-stubs-public.h.in +63 -0
  134. data/ext/third_party/snappy/snappy-test.cc +613 -0
  135. data/ext/third_party/snappy/snappy-test.h +526 -0
  136. data/ext/third_party/snappy/snappy.cc +1770 -0
  137. data/ext/third_party/snappy/snappy.h +209 -0
  138. data/ext/third_party/snappy/snappy_compress_fuzzer.cc +60 -0
  139. data/ext/third_party/snappy/snappy_uncompress_fuzzer.cc +58 -0
  140. data/ext/third_party/snappy/snappy_unittest.cc +1512 -0
  141. data/ext/third_party/snappy/testdata/alice29.txt +3609 -0
  142. data/ext/third_party/snappy/testdata/asyoulik.txt +4122 -0
  143. data/ext/third_party/snappy/testdata/baddata1.snappy +0 -0
  144. data/ext/third_party/snappy/testdata/baddata2.snappy +0 -0
  145. data/ext/third_party/snappy/testdata/baddata3.snappy +0 -0
  146. data/ext/third_party/snappy/testdata/fireworks.jpeg +0 -0
  147. data/ext/third_party/snappy/testdata/geo.protodata +0 -0
  148. data/ext/third_party/snappy/testdata/html +1 -0
  149. data/ext/third_party/snappy/testdata/html_x_4 +1 -0
  150. data/ext/third_party/snappy/testdata/kppkn.gtb +0 -0
  151. data/ext/third_party/snappy/testdata/lcet10.txt +7519 -0
  152. data/ext/third_party/snappy/testdata/paper-100k.pdf +600 -2
  153. data/ext/third_party/snappy/testdata/plrabn12.txt +10699 -0
  154. data/ext/third_party/snappy/testdata/urls.10K +10000 -0
  155. data/lib/couchbase/binary_collection.rb +33 -76
  156. data/lib/couchbase/binary_collection_options.rb +94 -0
  157. data/lib/couchbase/bucket.rb +9 -3
  158. data/lib/couchbase/cluster.rb +161 -23
  159. data/lib/couchbase/collection.rb +108 -191
  160. data/lib/couchbase/collection_options.rb +430 -0
  161. data/lib/couchbase/errors.rb +136 -134
  162. data/lib/couchbase/json_transcoder.rb +32 -0
  163. data/lib/couchbase/management/analytics_index_manager.rb +185 -9
  164. data/lib/couchbase/management/bucket_manager.rb +84 -33
  165. data/lib/couchbase/management/collection_manager.rb +166 -1
  166. data/lib/couchbase/management/query_index_manager.rb +261 -0
  167. data/lib/couchbase/management/search_index_manager.rb +291 -0
  168. data/lib/couchbase/management/user_manager.rb +12 -10
  169. data/lib/couchbase/management/view_index_manager.rb +151 -1
  170. data/lib/couchbase/mutation_state.rb +11 -1
  171. data/lib/couchbase/scope.rb +4 -4
  172. data/lib/couchbase/version.rb +1 -1
  173. metadata +113 -18
  174. data/.travis.yml +0 -7
  175. data/ext/couchbase/io/binary_parser.hxx +0 -64
  176. data/lib/couchbase/results.rb +0 -307
@@ -0,0 +1,526 @@
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
+ // Various stubs for the unit tests for the open-source version of Snappy.
30
+
31
+ #ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_
32
+ #define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_
33
+
34
+ #include <cstdarg>
35
+ #include <cstdio>
36
+ #include <iostream>
37
+ #include <string>
38
+
39
+ #include "snappy-stubs-internal.h"
40
+
41
+ #ifdef HAVE_SYS_MMAN_H
42
+ #include <sys/mman.h>
43
+ #endif
44
+
45
+ #ifdef HAVE_SYS_RESOURCE_H
46
+ #include <sys/resource.h>
47
+ #endif
48
+
49
+ #ifdef HAVE_SYS_TIME_H
50
+ #include <sys/time.h>
51
+ #endif
52
+
53
+ #ifdef HAVE_WINDOWS_H
54
+ #include <windows.h>
55
+ #endif
56
+
57
+ #ifdef HAVE_GTEST
58
+
59
+ #include <gtest/gtest.h>
60
+ #undef TYPED_TEST
61
+ #define TYPED_TEST TEST
62
+ #define INIT_GTEST(argc, argv) ::testing::InitGoogleTest(argc, *argv)
63
+
64
+ #else
65
+
66
+ // Stubs for if the user doesn't have Google Test installed.
67
+
68
+ #define TEST(test_case, test_subcase) \
69
+ void Test_ ## test_case ## _ ## test_subcase()
70
+ #define INIT_GTEST(argc, argv)
71
+
72
+ #define TYPED_TEST TEST
73
+ #define EXPECT_EQ CHECK_EQ
74
+ #define EXPECT_NE CHECK_NE
75
+ #define EXPECT_FALSE(cond) CHECK(!(cond))
76
+
77
+ #endif
78
+
79
+ #ifdef HAVE_GFLAGS
80
+
81
+ #include <gflags/gflags.h>
82
+
83
+ // This is tricky; both gflags and Google Test want to look at the command line
84
+ // arguments. Google Test seems to be the most happy with unknown arguments,
85
+ // though, so we call it first and hope for the best.
86
+ #define InitGoogle(argv0, argc, argv, remove_flags) \
87
+ INIT_GTEST(argc, argv); \
88
+ google::ParseCommandLineFlags(argc, argv, remove_flags);
89
+
90
+ #else
91
+
92
+ // If we don't have the gflags package installed, these can only be
93
+ // changed at compile time.
94
+ #define DEFINE_int32(flag_name, default_value, description) \
95
+ static int FLAGS_ ## flag_name = default_value;
96
+
97
+ #define InitGoogle(argv0, argc, argv, remove_flags) \
98
+ INIT_GTEST(argc, argv)
99
+
100
+ #endif
101
+
102
+ #ifdef HAVE_LIBZ
103
+ #include "zlib.h"
104
+ #endif
105
+
106
+ #ifdef HAVE_LIBLZO2
107
+ #include "lzo/lzo1x.h"
108
+ #endif
109
+
110
+ namespace {
111
+
112
+ namespace file {
113
+ int Defaults() { return 0; }
114
+
115
+ class DummyStatus {
116
+ public:
117
+ void CheckSuccess() { }
118
+ };
119
+
120
+ DummyStatus GetContents(
121
+ const std::string& filename, std::string* data, int /*unused*/) {
122
+ FILE* fp = std::fopen(filename.c_str(), "rb");
123
+ if (fp == NULL) {
124
+ std::perror(filename.c_str());
125
+ std::exit(1);
126
+ }
127
+
128
+ data->clear();
129
+ while (!feof(fp)) {
130
+ char buf[4096];
131
+ size_t ret = fread(buf, 1, 4096, fp);
132
+ if (ret == 0 && ferror(fp)) {
133
+ std::perror("fread");
134
+ std::exit(1);
135
+ }
136
+ data->append(std::string(buf, ret));
137
+ }
138
+
139
+ std::fclose(fp);
140
+
141
+ return DummyStatus();
142
+ }
143
+
144
+ inline DummyStatus SetContents(
145
+ const std::string& filename, const std::string& str, int /*unused*/) {
146
+ FILE* fp = std::fopen(filename.c_str(), "wb");
147
+ if (fp == NULL) {
148
+ std::perror(filename.c_str());
149
+ std::exit(1);
150
+ }
151
+
152
+ int ret = std::fwrite(str.data(), str.size(), 1, fp);
153
+ if (ret != 1) {
154
+ std::perror("fwrite");
155
+ std::exit(1);
156
+ }
157
+
158
+ std::fclose(fp);
159
+
160
+ return DummyStatus();
161
+ }
162
+ } // namespace file
163
+
164
+ } // namespace
165
+
166
+ namespace snappy {
167
+
168
+ #define FLAGS_test_random_seed 301
169
+ using TypeParam = std::string;
170
+
171
+ void Test_CorruptedTest_VerifyCorrupted();
172
+ void Test_Snappy_SimpleTests();
173
+ void Test_Snappy_MaxBlowup();
174
+ void Test_Snappy_RandomData();
175
+ void Test_Snappy_FourByteOffset();
176
+ void Test_SnappyCorruption_TruncatedVarint();
177
+ void Test_SnappyCorruption_UnterminatedVarint();
178
+ void Test_SnappyCorruption_OverflowingVarint();
179
+ void Test_Snappy_ReadPastEndOfBuffer();
180
+ void Test_Snappy_FindMatchLength();
181
+ void Test_Snappy_FindMatchLengthRandom();
182
+
183
+ std::string ReadTestDataFile(const std::string& base, size_t size_limit);
184
+
185
+ std::string ReadTestDataFile(const std::string& base);
186
+
187
+ // A std::sprintf() variant that returns a std::string.
188
+ // Not safe for general use due to truncation issues.
189
+ std::string StrFormat(const char* format, ...);
190
+
191
+ // A wall-time clock. This stub is not super-accurate, nor resistant to the
192
+ // system time changing.
193
+ class CycleTimer {
194
+ public:
195
+ CycleTimer() : real_time_us_(0) {}
196
+
197
+ void Start() {
198
+ #ifdef WIN32
199
+ QueryPerformanceCounter(&start_);
200
+ #else
201
+ gettimeofday(&start_, NULL);
202
+ #endif
203
+ }
204
+
205
+ void Stop() {
206
+ #ifdef WIN32
207
+ LARGE_INTEGER stop;
208
+ LARGE_INTEGER frequency;
209
+ QueryPerformanceCounter(&stop);
210
+ QueryPerformanceFrequency(&frequency);
211
+
212
+ double elapsed = static_cast<double>(stop.QuadPart - start_.QuadPart) /
213
+ frequency.QuadPart;
214
+ real_time_us_ += elapsed * 1e6 + 0.5;
215
+ #else
216
+ struct timeval stop;
217
+ gettimeofday(&stop, NULL);
218
+
219
+ real_time_us_ += 1000000 * (stop.tv_sec - start_.tv_sec);
220
+ real_time_us_ += (stop.tv_usec - start_.tv_usec);
221
+ #endif
222
+ }
223
+
224
+ double Get() {
225
+ return real_time_us_ * 1e-6;
226
+ }
227
+
228
+ private:
229
+ int64_t real_time_us_;
230
+ #ifdef WIN32
231
+ LARGE_INTEGER start_;
232
+ #else
233
+ struct timeval start_;
234
+ #endif
235
+ };
236
+
237
+ // Minimalistic microbenchmark framework.
238
+
239
+ typedef void (*BenchmarkFunction)(int, int);
240
+
241
+ class Benchmark {
242
+ public:
243
+ Benchmark(const std::string& name, BenchmarkFunction function)
244
+ : name_(name), function_(function) {}
245
+
246
+ Benchmark* DenseRange(int start, int stop) {
247
+ start_ = start;
248
+ stop_ = stop;
249
+ return this;
250
+ }
251
+
252
+ void Run();
253
+
254
+ private:
255
+ const std::string name_;
256
+ const BenchmarkFunction function_;
257
+ int start_, stop_;
258
+ };
259
+ #define BENCHMARK(benchmark_name) \
260
+ Benchmark* Benchmark_ ## benchmark_name = \
261
+ (new Benchmark(#benchmark_name, benchmark_name))
262
+
263
+ extern Benchmark* Benchmark_BM_UFlat;
264
+ extern Benchmark* Benchmark_BM_UIOVec;
265
+ extern Benchmark* Benchmark_BM_UValidate;
266
+ extern Benchmark* Benchmark_BM_ZFlat;
267
+ extern Benchmark* Benchmark_BM_ZFlatAll;
268
+ extern Benchmark* Benchmark_BM_ZFlatIncreasingTableSize;
269
+
270
+ void ResetBenchmarkTiming();
271
+ void StartBenchmarkTiming();
272
+ void StopBenchmarkTiming();
273
+ void SetBenchmarkLabel(const std::string& str);
274
+ void SetBenchmarkBytesProcessed(int64_t bytes);
275
+
276
+ #ifdef HAVE_LIBZ
277
+
278
+ // Object-oriented wrapper around zlib.
279
+ class ZLib {
280
+ public:
281
+ ZLib();
282
+ ~ZLib();
283
+
284
+ // Wipe a ZLib object to a virgin state. This differs from Reset()
285
+ // in that it also breaks any state.
286
+ void Reinit();
287
+
288
+ // Call this to make a zlib buffer as good as new. Here's the only
289
+ // case where they differ:
290
+ // CompressChunk(a); CompressChunk(b); CompressChunkDone(); vs
291
+ // CompressChunk(a); Reset(); CompressChunk(b); CompressChunkDone();
292
+ // You'll want to use Reset(), then, when you interrupt a compress
293
+ // (or uncompress) in the middle of a chunk and want to start over.
294
+ void Reset();
295
+
296
+ // According to the zlib manual, when you Compress, the destination
297
+ // buffer must have size at least src + .1%*src + 12. This function
298
+ // helps you calculate that. Augment this to account for a potential
299
+ // gzip header and footer, plus a few bytes of slack.
300
+ static int MinCompressbufSize(int uncompress_size) {
301
+ return uncompress_size + uncompress_size/1000 + 40;
302
+ }
303
+
304
+ // Compresses the source buffer into the destination buffer.
305
+ // sourceLen is the byte length of the source buffer.
306
+ // Upon entry, destLen is the total size of the destination buffer,
307
+ // which must be of size at least MinCompressbufSize(sourceLen).
308
+ // Upon exit, destLen is the actual size of the compressed buffer.
309
+ //
310
+ // This function can be used to compress a whole file at once if the
311
+ // input file is mmap'ed.
312
+ //
313
+ // Returns Z_OK if success, Z_MEM_ERROR if there was not
314
+ // enough memory, Z_BUF_ERROR if there was not enough room in the
315
+ // output buffer. Note that if the output buffer is exactly the same
316
+ // size as the compressed result, we still return Z_BUF_ERROR.
317
+ // (check CL#1936076)
318
+ int Compress(Bytef *dest, uLongf *destLen,
319
+ const Bytef *source, uLong sourceLen);
320
+
321
+ // Uncompresses the source buffer into the destination buffer.
322
+ // The destination buffer must be long enough to hold the entire
323
+ // decompressed contents.
324
+ //
325
+ // Returns Z_OK on success, otherwise, it returns a zlib error code.
326
+ int Uncompress(Bytef *dest, uLongf *destLen,
327
+ const Bytef *source, uLong sourceLen);
328
+
329
+ // Uncompress data one chunk at a time -- ie you can call this
330
+ // more than once. To get this to work you need to call per-chunk
331
+ // and "done" routines.
332
+ //
333
+ // Returns Z_OK if success, Z_MEM_ERROR if there was not
334
+ // enough memory, Z_BUF_ERROR if there was not enough room in the
335
+ // output buffer.
336
+
337
+ int UncompressAtMost(Bytef *dest, uLongf *destLen,
338
+ const Bytef *source, uLong *sourceLen);
339
+
340
+ // Checks gzip footer information, as needed. Mostly this just
341
+ // makes sure the checksums match. Whenever you call this, it
342
+ // will assume the last 8 bytes from the previous UncompressChunk
343
+ // call are the footer. Returns true iff everything looks ok.
344
+ bool UncompressChunkDone();
345
+
346
+ private:
347
+ int InflateInit(); // sets up the zlib inflate structure
348
+ int DeflateInit(); // sets up the zlib deflate structure
349
+
350
+ // These init the zlib data structures for compressing/uncompressing
351
+ int CompressInit(Bytef *dest, uLongf *destLen,
352
+ const Bytef *source, uLong *sourceLen);
353
+ int UncompressInit(Bytef *dest, uLongf *destLen,
354
+ const Bytef *source, uLong *sourceLen);
355
+ // Initialization method to be called if we hit an error while
356
+ // uncompressing. On hitting an error, call this method before
357
+ // returning the error.
358
+ void UncompressErrorInit();
359
+
360
+ // Helper function for Compress
361
+ int CompressChunkOrAll(Bytef *dest, uLongf *destLen,
362
+ const Bytef *source, uLong sourceLen,
363
+ int flush_mode);
364
+ int CompressAtMostOrAll(Bytef *dest, uLongf *destLen,
365
+ const Bytef *source, uLong *sourceLen,
366
+ int flush_mode);
367
+
368
+ // Likewise for UncompressAndUncompressChunk
369
+ int UncompressChunkOrAll(Bytef *dest, uLongf *destLen,
370
+ const Bytef *source, uLong sourceLen,
371
+ int flush_mode);
372
+
373
+ int UncompressAtMostOrAll(Bytef *dest, uLongf *destLen,
374
+ const Bytef *source, uLong *sourceLen,
375
+ int flush_mode);
376
+
377
+ // Initialization method to be called if we hit an error while
378
+ // compressing. On hitting an error, call this method before
379
+ // returning the error.
380
+ void CompressErrorInit();
381
+
382
+ int compression_level_; // compression level
383
+ int window_bits_; // log base 2 of the window size used in compression
384
+ int mem_level_; // specifies the amount of memory to be used by
385
+ // compressor (1-9)
386
+ z_stream comp_stream_; // Zlib stream data structure
387
+ bool comp_init_; // True if we have initialized comp_stream_
388
+ z_stream uncomp_stream_; // Zlib stream data structure
389
+ bool uncomp_init_; // True if we have initialized uncomp_stream_
390
+
391
+ // These are used only with chunked compression.
392
+ bool first_chunk_; // true if we need to emit headers with this chunk
393
+ };
394
+
395
+ #endif // HAVE_LIBZ
396
+
397
+ } // namespace snappy
398
+
399
+ DECLARE_bool(run_microbenchmarks);
400
+
401
+ static inline void RunSpecifiedBenchmarks() {
402
+ if (!FLAGS_run_microbenchmarks) {
403
+ return;
404
+ }
405
+
406
+ std::fprintf(stderr, "Running microbenchmarks.\n");
407
+ #ifndef NDEBUG
408
+ std::fprintf(stderr,
409
+ "WARNING: Compiled with assertions enabled, will be slow.\n");
410
+ #endif
411
+ #ifndef __OPTIMIZE__
412
+ std::fprintf(stderr,
413
+ "WARNING: Compiled without optimization, will be slow.\n");
414
+ #endif
415
+ std::fprintf(stderr, "Benchmark Time(ns) CPU(ns) Iterations\n");
416
+ std::fprintf(stderr, "---------------------------------------------------\n");
417
+
418
+ snappy::Benchmark_BM_UFlat->Run();
419
+ snappy::Benchmark_BM_UIOVec->Run();
420
+ snappy::Benchmark_BM_UValidate->Run();
421
+ snappy::Benchmark_BM_ZFlat->Run();
422
+ snappy::Benchmark_BM_ZFlatAll->Run();
423
+ snappy::Benchmark_BM_ZFlatIncreasingTableSize->Run();
424
+
425
+ std::fprintf(stderr, "\n");
426
+ }
427
+
428
+ #ifndef HAVE_GTEST
429
+
430
+ static inline int RUN_ALL_TESTS() {
431
+ std::fprintf(stderr, "Running correctness tests.\n");
432
+ snappy::Test_CorruptedTest_VerifyCorrupted();
433
+ snappy::Test_Snappy_SimpleTests();
434
+ snappy::Test_Snappy_MaxBlowup();
435
+ snappy::Test_Snappy_RandomData();
436
+ snappy::Test_Snappy_FourByteOffset();
437
+ snappy::Test_SnappyCorruption_TruncatedVarint();
438
+ snappy::Test_SnappyCorruption_UnterminatedVarint();
439
+ snappy::Test_SnappyCorruption_OverflowingVarint();
440
+ snappy::Test_Snappy_ReadPastEndOfBuffer();
441
+ snappy::Test_Snappy_FindMatchLength();
442
+ snappy::Test_Snappy_FindMatchLengthRandom();
443
+ std::fprintf(stderr, "All tests passed.\n");
444
+
445
+ return 0;
446
+ }
447
+
448
+ #endif // HAVE_GTEST
449
+
450
+ // For main().
451
+ namespace snappy {
452
+
453
+ // Logging.
454
+
455
+ #define LOG(level) LogMessage()
456
+ #define VLOG(level) true ? (void)0 : \
457
+ snappy::LogMessageVoidify() & snappy::LogMessage()
458
+
459
+ class LogMessage {
460
+ public:
461
+ LogMessage() { }
462
+ ~LogMessage() {
463
+ std::cerr << std::endl;
464
+ }
465
+
466
+ LogMessage& operator<<(const std::string& msg) {
467
+ std::cerr << msg;
468
+ return *this;
469
+ }
470
+ LogMessage& operator<<(int x) {
471
+ std::cerr << x;
472
+ return *this;
473
+ }
474
+ };
475
+
476
+ // Asserts, both versions activated in debug mode only,
477
+ // and ones that are always active.
478
+
479
+ #define CRASH_UNLESS(condition) \
480
+ SNAPPY_PREDICT_TRUE(condition) ? (void)0 : \
481
+ snappy::LogMessageVoidify() & snappy::LogMessageCrash()
482
+
483
+ #ifdef _MSC_VER
484
+ // ~LogMessageCrash calls std::abort() and therefore never exits. This is by
485
+ // design, so temporarily disable warning C4722.
486
+ #pragma warning(push)
487
+ #pragma warning(disable:4722)
488
+ #endif
489
+
490
+ class LogMessageCrash : public LogMessage {
491
+ public:
492
+ LogMessageCrash() { }
493
+ ~LogMessageCrash() {
494
+ std::cerr << std::endl;
495
+ std::abort();
496
+ }
497
+ };
498
+
499
+ #ifdef _MSC_VER
500
+ #pragma warning(pop)
501
+ #endif
502
+
503
+ // This class is used to explicitly ignore values in the conditional
504
+ // logging macros. This avoids compiler warnings like "value computed
505
+ // is not used" and "statement has no effect".
506
+
507
+ class LogMessageVoidify {
508
+ public:
509
+ LogMessageVoidify() { }
510
+ // This has to be an operator with a precedence lower than << but
511
+ // higher than ?:
512
+ void operator&(const LogMessage&) { }
513
+ };
514
+
515
+ #define CHECK(cond) CRASH_UNLESS(cond)
516
+ #define CHECK_LE(a, b) CRASH_UNLESS((a) <= (b))
517
+ #define CHECK_GE(a, b) CRASH_UNLESS((a) >= (b))
518
+ #define CHECK_EQ(a, b) CRASH_UNLESS((a) == (b))
519
+ #define CHECK_NE(a, b) CRASH_UNLESS((a) != (b))
520
+ #define CHECK_LT(a, b) CRASH_UNLESS((a) < (b))
521
+ #define CHECK_GT(a, b) CRASH_UNLESS((a) > (b))
522
+ #define CHECK_OK(cond) (cond).CheckSuccess()
523
+
524
+ } // namespace snappy
525
+
526
+ #endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_