LLVM 23.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.
45
46 /// \returns Total size of stored data.
47 size_t getStorageSize() const { return Cache.size(); }
48
49 /// \returns The precentage of space utilization of hard space limits.
50 ///
51 /// Return value is an integer between 0 and 100 for percentage.
53 return Cache.size() * 100ULL / Cache.capacity();
54 }
55
56 /// Open the on-disk store from a directory.
57 ///
58 /// \param Path directory for the on-disk store. The directory will be created
59 /// if it doesn't exist.
60 /// \param HashName Identifier name for the hashing algorithm that is going to
61 /// be used.
62 /// \param KeySize Size for the key hash bytes.
63 /// \param ValueName Identifier name for the values.
64 /// \param ValueSize Size for the value bytes.
65 /// \param UnifiedCache An optional UnifiedOnDiskCache that manages the size
66 /// and lifetime of the CAS instance and it must owns current initializing
67 /// KeyValueDB after initialized.
69 open(StringRef Path, StringRef HashName, unsigned KeySize,
70 StringRef ValueName, size_t ValueSize,
71 UnifiedOnDiskCache *UnifiedCache = nullptr,
72 std::shared_ptr<OnDiskCASLogger> Logger = nullptr);
73
74 /// Validate the storage.
75 LLVM_ABI Error validate() const;
76
77private:
78 OnDiskKeyValueDB(size_t ValueSize, OnDiskTrieRawHashMap Cache,
79 UnifiedOnDiskCache *UnifiedCache)
80 : ValueSize(ValueSize), Cache(std::move(Cache)),
81 UnifiedCache(UnifiedCache) {}
82
83 const size_t ValueSize;
85 UnifiedOnDiskCache *UnifiedCache = nullptr;
86};
87
88} // namespace llvm::cas::ondisk
89
90#endif // LLVM_CAS_ONDISKKEYVALUEDB_H
#define LLVM_ABI
Definition Compiler.h:215
This file declares interface for OnDiskTrieRawHashMap, a thread-safe and (mostly) lock-free hash map ...
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
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
OnDiskTrieRawHashMap is a persistent trie data structure used as hash maps.
An on-disk key-value data store with the following properties:
static LLVM_ABI Expected< std::unique_ptr< OnDiskKeyValueDB > > open(StringRef Path, StringRef HashName, unsigned KeySize, StringRef ValueName, size_t ValueSize, UnifiedOnDiskCache *UnifiedCache=nullptr, std::shared_ptr< OnDiskCASLogger > Logger=nullptr)
Open the on-disk store from a directory.
LLVM_ABI Expected< ArrayRef< char > > put(ArrayRef< uint8_t > Key, ArrayRef< char > Value)
Associate a value with a key.
LLVM_ABI Expected< std::optional< ArrayRef< char > > > get(ArrayRef< uint8_t > Key)
LLVM_ABI Error validate() const
Validate the storage.
A unified CAS nodes and key-value database, using on-disk storage for both.
StringMapEntry< Value * > ValueName
Definition Value.h:56
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
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:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860