Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RConcurrentHashColl.cxx
Go to the documentation of this file.
2 #include <ROOT/RMakeUnique.hxx>
3 #include <ROOT/TRWSpinLock.hxx>
4 #include <ROOT/TSeq.hxx>
5 #include <ROOT/RSha256.hxx>
6 
7 #include <set>
8 
9 namespace ROOT {
10 namespace Internal {
11 
12 
13 std::ostream &operator<<(std::ostream &os, const RConcurrentHashColl::HashValue &h)
14 {
15  auto digest = h.Get();
16  os << digest[0] << "-" << digest[1] << "-" << digest[2] << "-" << digest[3];
17  return os;
18 }
19 
20 RConcurrentHashColl::HashValue::HashValue(const char *data, int len)
21 {
22  // The cast here is because in the TBuffer ecosystem, the type used is char*
23  Sha256(reinterpret_cast<const unsigned char *>(data), len, fDigest);
24 }
25 
26 struct RHashSet {
27  std::set<ROOT::Internal::RConcurrentHashColl::HashValue> fSet;
28 };
29 
30 RConcurrentHashColl::RConcurrentHashColl()
31  : fHashSet(std::make_unique<RHashSet>()), fRWLock(std::make_unique<ROOT::TRWSpinLock>()){};
32 
33 RConcurrentHashColl::~RConcurrentHashColl() = default;
34 
35 /// Return true if the hash is already in already there
36 bool RConcurrentHashColl::Find(const HashValue &hash) const
37 {
38  ROOT::TRWSpinLockReadGuard rg(*fRWLock);
39  return (fHashSet->fSet.end() != fHashSet->fSet.find(hash));
40 }
41 
42 /// If the buffer is there, return false. Otherwise, insert the hash and return true
43 RConcurrentHashColl::HashValue RConcurrentHashColl::Hash(char *buffer, int len)
44 {
45  return HashValue(buffer, len);
46 }
47 
48 /// If the buffer is there, return false. Otherwise, insert the hash and return true
49 bool RConcurrentHashColl::Insert(char *buffer, int len) const
50 {
51  HashValue hash(buffer, len);
52 
53  {
54  ROOT::TRWSpinLockReadGuard rg(*fRWLock);
55  if (fHashSet->fSet.end() != fHashSet->fSet.find(hash))
56  return false;
57  }
58  {
59  ROOT::TRWSpinLockWriteGuard wg(*fRWLock);
60  fHashSet->fSet.insert(hash);
61  return true;
62  }
63 }
64 
65 /// If the buffer is there, return false. Otherwise, insert the hash and return true
66 bool RConcurrentHashColl::Insert(const HashValue &hash) const
67 {
68  ROOT::TRWSpinLockWriteGuard wg(*fRWLock);
69  auto ret = fHashSet->fSet.insert(hash);
70  return ret.second;
71 }
72 
73 } // End NS Internal
74 } // End NS ROOT