LLVM 22.0.0git
OnDiskKeyValueDB.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This declares OnDiskKeyValueDB, a key value storage database of fixed size
11/// key and value.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CAS_ONDISKKEYVALUEDB_H
16#define LLVM_CAS_ONDISKKEYVALUEDB_H
17
19
20namespace llvm::cas::ondisk {
21
22/// An on-disk key-value data store with the following properties:
23/// * Keys are fixed length binary hashes with expected normal distribution.
24/// * Values are buffers of the same size, specified at creation time.
25/// * The value of a key cannot be changed once it is set.
26/// * The value buffers returned from a key lookup have 8-byte alignment.
27class OnDiskKeyValueDB {
28public:
29 /// Associate a value with a key.
30 ///
31 /// \param Key the hash bytes for the key
32 /// \param Value the value bytes, same size as \p ValueSize parameter of
33 /// \p open call.
34 ///
35 /// \returns the value associated with the \p Key. It may be different than
36 /// \p Value if another value is already associated with this key.
38
39 /// \returns the value associated with the \p Key, or \p std::nullopt if the
40 /// key does not exist.
42
43 /// \returns Total size of stored data.
44 size_t getStorageSize() const { return Cache.size(); }
45
46 /// \returns The precentage of space utilization of hard space limits.
47 ///
48 /// Return value is an integer between 0 and 100 for percentage.
50 return Cache.size() * 100ULL / Cache.capacity();
51 }
52
53 /// Open the on-disk store from a directory.
54 ///
55 /// \param Path directory for the on-disk store. The directory will be created
56 /// if it doesn't exist.
57 /// \param HashName Identifier name for the hashing algorithm that is going to
58 /// be used.
59 /// \param KeySize Size for the key hash bytes.
60 /// \param ValueName Identifier name for the values.
61 /// \param ValueSize Size for the value bytes.
63 open(StringRef Path, StringRef HashName, unsigned KeySize,
64 StringRef ValueName, size_t ValueSize);
65
68 /// Validate the storage with a callback \p CheckValue to check the stored
69 /// value.
70 Error validate(CheckValueT CheckValue) const;
71
72private:
73 OnDiskKeyValueDB(size_t ValueSize, OnDiskTrieRawHashMap Cache)
74 : ValueSize(ValueSize), Cache(std::move(Cache)) {}
75
76 const size_t ValueSize;
78};
79
80} // namespace llvm::cas::ondisk
81
82#endif // LLVM_CAS_ONDISKKEYVALUEDB_H
This file declares interface for OnDiskTrieRawHashMap, a thread-safe and (mostly) lock-free hash map ...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM Value Representation.
Definition Value.h:75
FileOffset is a wrapper around uint64_t to represent the offset of data from the beginning of the fil...
Definition FileOffset.h:24
OnDiskTrieRawHashMap is a persistent trie data structure used as hash maps.
Expected< ArrayRef< char > > put(ArrayRef< uint8_t > Key, ArrayRef< char > Value)
Associate a value with a key.
Expected< std::optional< ArrayRef< char > > > get(ArrayRef< uint8_t > Key)
static Expected< std::unique_ptr< OnDiskKeyValueDB > > open(StringRef Path, StringRef HashName, unsigned KeySize, StringRef ValueName, size_t ValueSize)
Open the on-disk store from a directory.
function_ref< Error(FileOffset Offset, ArrayRef< char > Data)> CheckValueT
Error validate(CheckValueT CheckValue) const
Validate the storage with a callback CheckValue to check the stored value.
An efficient, type-erasing, non-owning reference to a callable.
@ Offset
Definition DWP.cpp:477
StringMapEntry< Value * > ValueName
Definition Value.h:56
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1867
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867