TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
blobstorage.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <common/language.h>
11 
12 #include <autogradpp/autograd.h>
13 #include <nlohmann/json.hpp>
14 
15 namespace cpid {
16 
17 class Cpid2kWorker;
18 
19 /**
20  * A simple interface for key-value data blob stores.
21  *
22  * Keys are required to be unique, and calling `put()` twice with the same data
23  * will result in an exception. The reasoning is that we want to do local
24  * in-memory caching in ModelStorage and don't want to write cache invalidation
25  * logic (well, *I* don't want to, at least).
26  */
27 class BlobStorage {
28  public:
29  virtual ~BlobStorage() = default;
30  virtual void put(std::string const& key, std::vector<char> const& data) = 0;
31  virtual std::vector<char> get(std::string const& key) = 0;
32 };
33 
34 class BlobStorageDisk : public BlobStorage {
35  public:
36  BlobStorageDisk(std::string root);
37  virtual ~BlobStorageDisk() override = default;
38 
39  virtual void put(std::string const& key, std::vector<char> const& data)
40  override;
41  virtual std::vector<char> get(std::string const& key) override;
42 
43  private:
44  std::string root_;
45 };
46 
47 /**
48  * Blob storage in Redis.
49  *
50  * Note that Cpid2kWorker is only used to obtain a database connection in a
51  * thread-safe manner. Models are stored directly under the separately specified
52  * prefix to ease data access across jobs.
53  */
54 class BlobStorageRedis : public BlobStorage {
55  public:
57  std::shared_ptr<Cpid2kWorker> worker,
58  std::string prefix = "blob");
59  virtual ~BlobStorageRedis() override;
60 
61  virtual void put(std::string const& key, std::vector<char> const& data)
62  override;
63  virtual std::vector<char> get(std::string const& key) override;
64 
65  private:
66  std::string dbkey(std::string const& key);
67 
68  std::shared_ptr<Cpid2kWorker> worker_;
69  std::string prefix_;
70 };
71 
72 } // namespace cpid
A simple interface for key-value data blob stores.
Definition: blobstorage.h:27
virtual ~BlobStorage()=default
Definition: blobstorage.h:34
virtual void put(std::string const &key, std::vector< char > const &data)=0
Blob storage in Redis.
Definition: blobstorage.h:54
The TorchCraftAI training library.
Definition: batcher.cpp:15