Skip to content

[CAS] Add OnDiskGraphDB and OnDiskKeyValueDB #114102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: users/cachemeifyoucan/spr/main.cas-add-ondiskgraphdb-and-ondiskkeyvaluedb
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
428 changes: 428 additions & 0 deletions llvm/include/llvm/CAS/OnDiskGraphDB.h

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions llvm/include/llvm/CAS/OnDiskKeyValueDB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===- OnDiskKeyValueDB.h ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CAS_ONDISKKEYVALUEDB_H
#define LLVM_CAS_ONDISKKEYVALUEDB_H

#include "llvm/CAS/OnDiskHashMappedTrie.h"

namespace llvm::cas::ondisk {

/// An on-disk key-value data store with the following properties:
/// * Keys are fixed length binary hashes with expected normal distribution.
/// * Values are buffers of the same size, specified at creation time.
/// * The value of a key cannot be changed once it is set.
/// * The value buffers returned from a key lookup have 8-byte alignment.
class OnDiskKeyValueDB {
public:
/// Associate a value with a key.
///
/// \param Key the hash bytes for the key
/// \param Value the value bytes, same size as \p ValueSize parameter of
/// \p open call.
///
/// \returns the value associated with the \p Key. It may be different than
/// \p Value if another value is already associated with this key.
Expected<ArrayRef<char>> put(ArrayRef<uint8_t> Key, ArrayRef<char> Value);

/// \returns the value associated with the \p Key, or \p std::nullopt if the
/// key does not exist.
Expected<std::optional<ArrayRef<char>>> get(ArrayRef<uint8_t> Key);

/// \returns Total size of stored data.
size_t getStorageSize() const { return Cache.size(); }

/// Open the on-disk store from a directory.
///
/// \param Path directory for the on-disk store. The directory will be created
/// if it doesn't exist.
/// \param HashName Identifier name for the hashing algorithm that is going to
/// be used.
/// \param KeySize Size for the key hash bytes.
/// \param ValueName Identifier name for the values.
/// \param ValueSize Size for the value bytes.
static Expected<std::unique_ptr<OnDiskKeyValueDB>>
open(StringRef Path, StringRef HashName, unsigned KeySize,
StringRef ValueName, size_t ValueSize);

private:
OnDiskKeyValueDB(size_t ValueSize, OnDiskHashMappedTrie Cache)
: ValueSize(ValueSize), Cache(std::move(Cache)) {}

const size_t ValueSize;
OnDiskHashMappedTrie Cache;
};

} // namespace llvm::cas::ondisk

#endif // LLVM_CAS_ONDISKKEYVALUEDB_H
2 changes: 2 additions & 0 deletions llvm/lib/CAS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ add_llvm_component_library(LLVMCAS
MappedFileRegionBumpPtr.cpp
ObjectStore.cpp
OnDiskCommon.cpp
OnDiskKeyValueDB.cpp
OnDiskHashMappedTrie.cpp
OnDiskGraphDB.cpp

ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/CAS
Expand Down
35 changes: 35 additions & 0 deletions llvm/lib/CAS/OnDiskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//===----------------------------------------------------------------------===//

#include "OnDiskCommon.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Process.h"
#include <thread>

#if __has_include(<sys/file.h>)
Expand All @@ -20,6 +23,38 @@

using namespace llvm;

static uint64_t OnDiskCASMaxMappingSize = 0;

Expected<std::optional<uint64_t>> cas::ondisk::getOverriddenMaxMappingSize() {
static std::once_flag Flag;
Error Err = Error::success();
std::call_once(Flag, [&Err] {
ErrorAsOutParameter EAO(&Err);
constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE";
auto Value = sys::Process::GetEnv(EnvVar);
if (!Value)
return;

uint64_t Size;
if (StringRef(*Value).getAsInteger(/*auto*/ 0, Size))
Err = createStringError(inconvertibleErrorCode(),
"invalid value for %s: expected integer", EnvVar);
OnDiskCASMaxMappingSize = Size;
});

if (Err)
return std::move(Err);

if (OnDiskCASMaxMappingSize == 0)
return std::nullopt;

return OnDiskCASMaxMappingSize;
}

void cas::ondisk::setMaxMappingSize(uint64_t Size) {
OnDiskCASMaxMappingSize = Size;
}

std::error_code cas::ondisk::lockFileThreadSafe(int FD, bool Exclusive) {
#if HAVE_FLOCK
if (flock(FD, Exclusive ? LOCK_EX : LOCK_SH) == 0)
Expand Down
13 changes: 13 additions & 0 deletions llvm/lib/CAS/OnDiskCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@
#ifndef LLVM_LIB_CAS_ONDISKCOMMON_H
#define LLVM_LIB_CAS_ONDISKCOMMON_H

#include "llvm/Support/Error.h"
#include <chrono>
#include <optional>

namespace llvm::cas::ondisk {

/// Retrieves an overridden maximum mapping size for CAS files, if any,
/// speicified by LLVM_CAS_MAX_MAPPING_SIZE in the environment or set by
/// `setMaxMappingSize()`. If the value from environment is unreadable, returns
/// an error.
Expected<std::optional<uint64_t>> getOverriddenMaxMappingSize();

/// Set MaxMappingSize for ondisk CAS. This function is not thread-safe and
/// should be set before creaing any ondisk CAS and does not affect CAS already
/// created. Set value 0 to use default size.
void setMaxMappingSize(uint64_t Size);

/// Thread-safe alternative to \c sys::fs::lockFile. This does not support all
/// the platforms that \c sys::fs::lockFile does, so keep it in the CAS library
/// for now.
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.