| 
 | 1 | +//===- OnDiskKeyValueDB.h ---------------------------------------*- C++ -*-===//  | 
 | 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 | +#ifndef LLVM_CAS_ONDISKKEYVALUEDB_H  | 
 | 10 | +#define LLVM_CAS_ONDISKKEYVALUEDB_H  | 
 | 11 | + | 
 | 12 | +#include "llvm/CAS/OnDiskTrieRawHashMap.h"  | 
 | 13 | + | 
 | 14 | +namespace llvm::cas::ondisk {  | 
 | 15 | + | 
 | 16 | +/// An on-disk key-value data store with the following properties:  | 
 | 17 | +/// * Keys are fixed length binary hashes with expected normal distribution.  | 
 | 18 | +/// * Values are buffers of the same size, specified at creation time.  | 
 | 19 | +/// * The value of a key cannot be changed once it is set.  | 
 | 20 | +/// * The value buffers returned from a key lookup have 8-byte alignment.  | 
 | 21 | +class OnDiskKeyValueDB {  | 
 | 22 | +public:  | 
 | 23 | +  /// Associate a value with a key.  | 
 | 24 | +  ///  | 
 | 25 | +  /// \param Key the hash bytes for the key  | 
 | 26 | +  /// \param Value the value bytes, same size as \p ValueSize parameter of  | 
 | 27 | +  /// \p open call.  | 
 | 28 | +  ///  | 
 | 29 | +  /// \returns the value associated with the \p Key. It may be different than  | 
 | 30 | +  /// \p Value if another value is already associated with this key.  | 
 | 31 | +  Expected<ArrayRef<char>> put(ArrayRef<uint8_t> Key, ArrayRef<char> Value);  | 
 | 32 | + | 
 | 33 | +  /// \returns the value associated with the \p Key, or \p std::nullopt if the  | 
 | 34 | +  /// key does not exist.  | 
 | 35 | +  Expected<std::optional<ArrayRef<char>>> get(ArrayRef<uint8_t> Key);  | 
 | 36 | + | 
 | 37 | +  /// \returns Total size of stored data.  | 
 | 38 | +  size_t getStorageSize() const {  | 
 | 39 | +    return Cache.size();  | 
 | 40 | +  }  | 
 | 41 | + | 
 | 42 | +  /// \returns The precentage of space utilization of hard space limits.  | 
 | 43 | +  ///  | 
 | 44 | +  /// Return value is an integer between 0 and 100 for percentage.  | 
 | 45 | +  unsigned getHardStorageLimitUtilization() const {  | 
 | 46 | +    return Cache.size() * 100ULL / Cache.capacity();  | 
 | 47 | +  }  | 
 | 48 | + | 
 | 49 | +  /// Open the on-disk store from a directory.  | 
 | 50 | +  ///  | 
 | 51 | +  /// \param Path directory for the on-disk store. The directory will be created  | 
 | 52 | +  /// if it doesn't exist.  | 
 | 53 | +  /// \param HashName Identifier name for the hashing algorithm that is going to  | 
 | 54 | +  /// be used.  | 
 | 55 | +  /// \param KeySize Size for the key hash bytes.  | 
 | 56 | +  /// \param ValueName Identifier name for the values.  | 
 | 57 | +  /// \param ValueSize Size for the value bytes.  | 
 | 58 | +  static Expected<std::unique_ptr<OnDiskKeyValueDB>>  | 
 | 59 | +  open(StringRef Path, StringRef HashName, unsigned KeySize,  | 
 | 60 | +       StringRef ValueName, size_t ValueSize);  | 
 | 61 | + | 
 | 62 | +  using CheckValueT = function_ref<Error(FileOffset Offset, ArrayRef<char>)>;  | 
 | 63 | +  Error validate(CheckValueT CheckValue) const;  | 
 | 64 | + | 
 | 65 | +private:  | 
 | 66 | +  OnDiskKeyValueDB(size_t ValueSize, OnDiskTrieRawHashMap Cache)  | 
 | 67 | +      : ValueSize(ValueSize), Cache(std::move(Cache)) {}  | 
 | 68 | + | 
 | 69 | +  const size_t ValueSize;  | 
 | 70 | +  OnDiskTrieRawHashMap Cache;  | 
 | 71 | +};  | 
 | 72 | + | 
 | 73 | +} // namespace llvm::cas::ondisk  | 
 | 74 | + | 
 | 75 | +#endif // LLVM_CAS_ONDISKKEYVALUEDB_H  | 
0 commit comments