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
23
24/// An on-disk key-value data store with the following properties:
25/// * Keys are fixed length binary hashes with expected normal distribution.
26/// * Values are buffers of the same size, specified at creation time.
27/// * The value of a key cannot be changed once it is set.
28/// * The value buffers returned from a key lookup have 8-byte alignment.
29class OnDiskKeyValueDB {
30public:
31 /// Associate a value with a key.
32 ///
33 /// \param Key the hash bytes for the key
34 /// \param Value the value bytes, same size as \p ValueSize parameter of
35 /// \p open call.
36 ///
37 /// \returns the value associated with the \p Key. It may be different than
38 /// \p Value if another value is already associated with this key.
41
42 /// \returns the value associated with the \p Key, or \p std::nullopt if the
43 /// key does not exist.
46
47 /// \returns Total size of stored data.
48 size_t getStorageSize() const { return Cache.size(); }
49
50 /// \returns The precentage of space utilization of hard space limits.
51 ///
52 /// Return value is an integer between 0 and 100 for percentage.
54 return Cache.size() * 100ULL / Cache.capacity();
55 }
56
57 /// Open the on-disk store from a directory.
58 ///
59 /// \param Path directory for the on-disk store. The directory will be created
60 /// if it doesn't exist.
61 /// \param HashName Identifier name for the hashing algorithm that is going to
62 /// be used.
63 /// \param KeySize Size for the key hash bytes.
64 /// \param ValueName Identifier name for the values.
65 /// \param ValueSize Size for the value bytes.
66 /// \param UnifiedCache An optional UnifiedOnDiskCache that manages the size
67 /// and lifetime of the CAS instance and it must owns current initializing
68 /// KeyValueDB after initialized.
70 open(StringRef Path, StringRef HashName, unsigned KeySize,
71 StringRef ValueName, size_t ValueSize,
72 UnifiedOnDiskCache *UnifiedCache = nullptr);
73
76 /// Validate the storage with a callback \p CheckValue to check the stored
77 /// value.
79
80private:
81 OnDiskKeyValueDB(size_t ValueSize, OnDiskTrieRawHashMap Cache,
82 UnifiedOnDiskCache *UnifiedCache)
83 : ValueSize(ValueSize), Cache(std::move(Cache)),
84 UnifiedCache(UnifiedCache) {}
85
86 const size_t ValueSize;
88 UnifiedOnDiskCache *UnifiedCache = nullptr;
89};
90
91} // namespace llvm::cas::ondisk
92
93#endif // LLVM_CAS_ONDISKKEYVALUEDB_H
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:218
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:40
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.
static LLVM_ABI_FOR_TEST Expected< std::unique_ptr< OnDiskKeyValueDB > > open(StringRef Path, StringRef HashName, unsigned KeySize, StringRef ValueName, size_t ValueSize, UnifiedOnDiskCache *UnifiedCache=nullptr)
Open the on-disk store from a directory.
LLVM_ABI_FOR_TEST Expected< ArrayRef< char > > put(ArrayRef< uint8_t > Key, ArrayRef< char > Value)
Associate a value with a key.
LLVM_ABI_FOR_TEST Expected< std::optional< ArrayRef< char > > > get(ArrayRef< uint8_t > Key)
function_ref< Error(FileOffset Offset, ArrayRef< char > Data)> CheckValueT
LLVM_ABI_FOR_TEST Error validate(CheckValueT CheckValue) const
Validate the storage with a callback CheckValue to check the stored value.
A unified CAS nodes and key-value database, using on-disk storage for both.
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