1 /** 2 * Copyright 2019 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_STORAGE_CONTAINER_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_STORAGE_CONTAINER_H_ 18 19 #include <limits.h> 20 #include <unistd.h> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 #include "minddata/dataset/util/system_pool.h" 26 #include "minddata/dataset/util/buddy.h" 27 #include "minddata/dataset/util/path.h" 28 #include "minddata/dataset/util/slice.h" 29 #include "minddata/dataset/util/status.h" 30 31 namespace mindspore { 32 namespace dataset { 33 class StorageManager; 34 35 class StorageContainer { 36 public: 37 friend class StorageManager; 38 39 ~StorageContainer() noexcept; 40 41 StorageContainer(const StorageContainer &) = delete; 42 43 StorageContainer &operator=(const StorageContainer &) = delete; 44 45 friend std::ostream &operator<<(std::ostream &os, const StorageContainer &s); 46 47 Status Open() noexcept; 48 49 Status Close() noexcept; 50 51 Status Insert(const std::vector<ReadableSlice> &buf, off64_t *offset) noexcept; 52 53 Status Write(const ReadableSlice &dest, off64_t offset) const noexcept; 54 55 Status Read(WritableSlice *dest, off64_t offset) const noexcept; 56 57 Status Truncate() const noexcept; 58 IsOpen()59 bool IsOpen() const { return is_open_; } 60 61 static Status CreateStorageContainer(std::shared_ptr<StorageContainer> *out_sc, const std::string &path); 62 63 private: 64 mutable std::mutex mutex_; 65 Path cont_; 66 int fd_; 67 bool is_open_; 68 std::unique_ptr<BuddySpace> bs_; 69 70 // Use the default value of BuddySpace 71 // which can map upto 4G of space. StorageContainer(const std::string & path)72 explicit StorageContainer(const std::string &path) : cont_(path), fd_(-1), is_open_(false), bs_(nullptr) {} 73 74 Status Create(); 75 }; 76 } // namespace dataset 77 } // namespace mindspore 78 79 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_STORAGE_CONTAINER_H_ 80