1 //===- Caching.h - LLVM Local File Cache ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the CachedFileStream and the localCache function, which
10 // simplifies caching files on the local filesystem in a directory whose
11 // contents are managed by a CachePruningPolicy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_CACHING_H
16 #define LLVM_SUPPORT_CACHING_H
17 
18 #include "llvm/Support/Error.h"
19 
20 namespace llvm {
21 
22 class MemoryBuffer;
23 
24 /// This class wraps an output stream for a file. Most clients should just be
25 /// able to return an instance of this base class from the stream callback, but
26 /// if a client needs to perform some action after the stream is written to,
27 /// that can be done by deriving from this class and overriding the destructor.
28 class CachedFileStream {
29 public:
30   CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS,
31                    std::string OSPath = "")
32       : OS(std::move(OS)), ObjectPathName(OSPath) {}
33   std::unique_ptr<raw_pwrite_stream> OS;
34   std::string ObjectPathName;
35   virtual ~CachedFileStream() = default;
36 };
37 
38 /// This type defines the callback to add a file that is generated on the fly.
39 ///
40 /// Stream callbacks must be thread safe.
41 using AddStreamFn =
42     std::function<Expected<std::unique_ptr<CachedFileStream>>(unsigned Task)>;
43 
44 /// This is the type of a file cache. To request an item from the cache, pass a
45 /// unique string as the Key. For hits, the cached file will be added to the
46 /// link and this function will return AddStreamFn(). For misses, the cache will
47 /// return a stream callback which must be called at most once to produce
48 /// content for the stream. The file stream produced by the stream callback will
49 /// add the file to the link after the stream is written to.
50 ///
51 /// Clients generally look like this:
52 ///
53 /// if (AddStreamFn AddStream = Cache(Task, Key))
54 ///   ProduceContent(AddStream);
55 using FileCache =
56     std::function<Expected<AddStreamFn>(unsigned Task, StringRef Key)>;
57 
58 /// This type defines the callback to add a pre-existing file (e.g. in a cache).
59 ///
60 /// Buffer callbacks must be thread safe.
61 using AddBufferFn =
62     std::function<void(unsigned Task, std::unique_ptr<MemoryBuffer> MB)>;
63 
64 /// Create a local file system cache which uses the given cache name, temporary
65 /// file prefix, cache directory and file callback.  This function does not
66 /// immediately create the cache directory if it does not yet exist; this is
67 /// done lazily the first time a file is added.  The cache name appears in error
68 /// messages for errors during caching. The temporary file prefix is used in the
69 /// temporary file naming scheme used when writing files atomically.
70 Expected<FileCache> localCache(
71     Twine CacheNameRef, Twine TempFilePrefixRef, Twine CacheDirectoryPathRef,
72     AddBufferFn AddBuffer = [](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
73     });
74 } // namespace llvm
75 
76 #endif
77