1 //===--- DraftStore.cpp - File contents container ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DraftStore.h"
11 
12 using namespace clang;
13 using namespace clang::clangd;
14 
15 VersionedDraft DraftStore::getDraft(PathRef File) const {
16   std::lock_guard<std::mutex> Lock(Mutex);
17 
18   auto It = Drafts.find(File);
19   if (It == Drafts.end())
20     return {0, llvm::None};
21   return It->second;
22 }
23 
24 DocVersion DraftStore::getVersion(PathRef File) const {
25   std::lock_guard<std::mutex> Lock(Mutex);
26 
27   auto It = Drafts.find(File);
28   if (It == Drafts.end())
29     return 0;
30   return It->second.Version;
31 }
32 
33 DocVersion DraftStore::updateDraft(PathRef File, StringRef Contents) {
34   std::lock_guard<std::mutex> Lock(Mutex);
35 
36   auto &Entry = Drafts[File];
37   DocVersion NewVersion = ++Entry.Version;
38   Entry.Draft = Contents;
39   return NewVersion;
40 }
41 
42 DocVersion DraftStore::removeDraft(PathRef File) {
43   std::lock_guard<std::mutex> Lock(Mutex);
44 
45   auto &Entry = Drafts[File];
46   DocVersion NewVersion = ++Entry.Version;
47   Entry.Draft = llvm::None;
48   return NewVersion;
49 }
50