read_write_util.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "file/read_write_util.h"
  10. #include <sstream>
  11. #include "test_util/sync_point.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. IOStatus NewWritableFile(FileSystem* fs, const std::string& fname,
  14. std::unique_ptr<FSWritableFile>* result,
  15. const FileOptions& options) {
  16. IOStatus s = fs->NewWritableFile(fname, options, result, nullptr);
  17. TEST_KILL_RANDOM("NewWritableFile:0", rocksdb_kill_odds * REDUCE_ODDS2);
  18. return s;
  19. }
  20. bool ReadOneLine(std::istringstream* iss, SequentialFileReader* seq_file_reader,
  21. std::string* output, bool* has_data, Status* result) {
  22. const int kBufferSize = 8192;
  23. char buffer[kBufferSize + 1];
  24. Slice input_slice;
  25. std::string line;
  26. bool has_complete_line = false;
  27. while (!has_complete_line) {
  28. if (std::getline(*iss, line)) {
  29. has_complete_line = !iss->eof();
  30. } else {
  31. has_complete_line = false;
  32. }
  33. if (!has_complete_line) {
  34. // if we're not sure whether we have a complete line,
  35. // further read from the file.
  36. if (*has_data) {
  37. *result = seq_file_reader->Read(kBufferSize, &input_slice, buffer);
  38. }
  39. if (input_slice.size() == 0) {
  40. // meaning we have read all the data
  41. *has_data = false;
  42. break;
  43. } else {
  44. iss->str(line + input_slice.ToString());
  45. // reset the internal state of iss so that we can keep reading it.
  46. iss->clear();
  47. *has_data = (input_slice.size() == kBufferSize);
  48. continue;
  49. }
  50. }
  51. }
  52. *output = line;
  53. return *has_data || has_complete_line;
  54. }
  55. #ifndef NDEBUG
  56. bool IsFileSectorAligned(const size_t off, size_t sector_size) {
  57. return off % sector_size == 0;
  58. }
  59. #endif // NDEBUG
  60. } // namespace ROCKSDB_NAMESPACE