1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/data/service/dataset_store.h"
17 
18 #include <memory>
19 
20 #include "absl/memory/memory.h"
21 #include "tensorflow/core/data/service/common.pb.h"
22 #include "tensorflow/core/data/service/utils.h"
23 #include "tensorflow/core/lib/io/record_reader.h"
24 #include "tensorflow/core/lib/io/record_writer.h"
25 #include "tensorflow/core/platform/env.h"
26 #include "tensorflow/core/platform/errors.h"
27 #include "tensorflow/core/platform/file_system.h"
28 #include "tensorflow/core/platform/path.h"
29 
30 namespace tensorflow {
31 namespace data {
32 
FileSystemDatasetStore(const std::string & datasets_dir)33 FileSystemDatasetStore::FileSystemDatasetStore(const std::string& datasets_dir)
34     : datasets_dir_(datasets_dir) {}
35 
Put(const std::string & key,const DatasetDef & dataset)36 Status FileSystemDatasetStore::Put(const std::string& key,
37                                    const DatasetDef& dataset) {
38   std::string path_to_write = io::JoinPath(datasets_dir_, key);
39 
40   if (Env::Default()->FileExists(path_to_write).ok()) {
41     return errors::AlreadyExists("File ", path_to_write, " already exists");
42   }
43   TF_RETURN_IF_ERROR(WriteDatasetDef(path_to_write, dataset));
44   return Status::OK();
45 }
46 
Get(const std::string & key,std::shared_ptr<const DatasetDef> & dataset_def)47 Status FileSystemDatasetStore::Get(
48     const std::string& key, std::shared_ptr<const DatasetDef>& dataset_def) {
49   std::string path = io::JoinPath(datasets_dir_, key);
50   TF_RETURN_IF_ERROR(Env::Default()->FileExists(path));
51   DatasetDef def;
52   TF_RETURN_IF_ERROR(ReadDatasetDef(path, def));
53   dataset_def = std::make_shared<const DatasetDef>(def);
54   return Status::OK();
55 }
56 
MemoryDatasetStore()57 MemoryDatasetStore::MemoryDatasetStore() {}
58 
Put(const std::string & key,const DatasetDef & dataset)59 Status MemoryDatasetStore::Put(const std::string& key,
60                                const DatasetDef& dataset) {
61   auto& stored_dataset = datasets_[key];
62   if (stored_dataset) {
63     return errors::AlreadyExists("Dataset with key ", key,
64                                  " is already stored.");
65   }
66   stored_dataset = std::make_shared<const DatasetDef>(dataset);
67   return Status::OK();
68 }
69 
Get(const std::string & key,std::shared_ptr<const DatasetDef> & dataset_def)70 Status MemoryDatasetStore::Get(const std::string& key,
71                                std::shared_ptr<const DatasetDef>& dataset_def) {
72   auto& stored_dataset = datasets_[key];
73   if (!stored_dataset) {
74     return errors::NotFound("Dataset with key ", key, " not found");
75   }
76   dataset_def = stored_dataset;
77   return Status::OK();
78 }
79 
80 }  // namespace data
81 }  // namespace tensorflow
82