YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/file_system_mem.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_UTIL_FILE_SYSTEM_MEM_H
15
#define YB_UTIL_FILE_SYSTEM_MEM_H
16
17
#include <vector>
18
19
#include "yb/util/file_system.h"
20
#include "yb/util/malloc.h"
21
#include "yb/util/size_literals.h"
22
23
using namespace yb::size_literals;
24
25
namespace yb {
26
27
class InMemoryFileState {
28
 public:
29
49
  explicit InMemoryFileState(std::string filename) : filename_(std::move(filename)), size_(0) {}
30
31
49
  ~InMemoryFileState() {
32
130
    for (uint8_t* block : blocks_) {
33
130
      delete[] block;
34
130
    }
35
49
  }
36
37
  InMemoryFileState(const InMemoryFileState&) = delete;
38
  void operator=(const InMemoryFileState&) = delete;
39
40
27
  uint64_t Size() const { return size_; }
41
42
  CHECKED_STATUS Read(uint64_t offset, size_t n, Slice* result, uint8_t* scratch) const;
43
44
  CHECKED_STATUS PreAllocate(uint64_t size);
45
46
  CHECKED_STATUS Append(const Slice& data);
47
48
  CHECKED_STATUS AppendRaw(const uint8_t *src, size_t src_len);
49
50
0
  const std::string& filename() const { return filename_; }
51
52
  size_t memory_footprint() const;
53
54
 private:
55
  static constexpr const size_t kBlockSize = 8_KB;
56
57
  const std::string filename_;
58
59
  // The following fields are not protected by any mutex. They are only mutable
60
  // while the file is being written, and concurrent access is not allowed
61
  // to writable files.
62
  std::vector<uint8_t*> blocks_;
63
  uint64_t size_;
64
};
65
66
class InMemorySequentialFile : public SequentialFile {
67
 public:
68
  explicit InMemorySequentialFile(std::shared_ptr<InMemoryFileState> file)
69
11
    : file_(std::move(file)), pos_(0) {}
70
71
11
  ~InMemorySequentialFile() {}
72
73
  CHECKED_STATUS Read(size_t n, Slice* result, uint8_t* scratch) override;
74
75
  CHECKED_STATUS Skip(uint64_t n) override;
76
77
0
  const std::string& filename() const override {
78
0
    return file_->filename();
79
0
  }
80
81
 private:
82
  const std::shared_ptr<InMemoryFileState> file_;
83
  size_t pos_;
84
};
85
86
class InMemoryRandomAccessFile : public RandomAccessFile {
87
 public:
88
  explicit InMemoryRandomAccessFile(std::shared_ptr<InMemoryFileState> file)
89
6
    : file_(std::move(file)) {}
90
91
6
  ~InMemoryRandomAccessFile() {}
92
93
  Status Read(uint64_t offset, size_t n, Slice* result, uint8_t* scratch) const override;
94
95
  Result<uint64_t> Size() const override;
96
97
  Result<uint64_t> INode() const override;
98
99
0
  const std::string& filename() const override {
100
0
    return file_->filename();
101
0
  }
102
103
0
  size_t memory_footprint() const override {
104
0
    // The FileState is actually shared between multiple files, but the double
105
0
    // counting doesn't matter much since MemEnv is only used in tests.
106
0
    return malloc_usable_size(this) + file_->memory_footprint();
107
0
  }
108
109
 private:
110
  const std::shared_ptr<InMemoryFileState> file_;
111
};
112
113
} // namespace yb
114
115
#endif  // YB_UTIL_FILE_SYSTEM_MEM_H