mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
Merge pull request #14219 from lovesegfault/eval-copy-less
libstore: Avoid copying derivations to the store if they are already valid
This commit is contained in:
commit
0f85ef3677
3 changed files with 77 additions and 17 deletions
|
|
@ -83,6 +83,7 @@ sources = files(
|
||||||
'store-reference.cc',
|
'store-reference.cc',
|
||||||
'uds-remote-store.cc',
|
'uds-remote-store.cc',
|
||||||
'worker-protocol.cc',
|
'worker-protocol.cc',
|
||||||
|
'write-derivation.cc',
|
||||||
)
|
)
|
||||||
|
|
||||||
include_dirs = [ include_directories('.') ]
|
include_dirs = [ include_directories('.') ]
|
||||||
|
|
|
||||||
57
src/libstore-tests/write-derivation.cc
Normal file
57
src/libstore-tests/write-derivation.cc
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
|
||||||
|
#include "nix/util/tests/gmock-matchers.hh"
|
||||||
|
#include "nix/store/derivations.hh"
|
||||||
|
#include "nix/store/dummy-store-impl.hh"
|
||||||
|
#include "nix/store/tests/libstore.hh"
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
class WriteDerivationTest : public LibStoreTest
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
WriteDerivationTest(ref<DummyStoreConfig> config_)
|
||||||
|
: LibStoreTest(config_->openDummyStore())
|
||||||
|
, config(std::move(config_))
|
||||||
|
{
|
||||||
|
config->readOnly = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteDerivationTest()
|
||||||
|
: WriteDerivationTest(make_ref<DummyStoreConfig>(DummyStoreConfig::Params{}))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ref<DummyStoreConfig> config;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Derivation makeSimpleDrv()
|
||||||
|
{
|
||||||
|
Derivation drv;
|
||||||
|
drv.name = "simple-derivation";
|
||||||
|
drv.platform = "system";
|
||||||
|
drv.builder = "foo";
|
||||||
|
drv.args = {"bar", "baz"};
|
||||||
|
drv.env = StringPairs{{"BIG_BAD", "WOLF"}};
|
||||||
|
return drv;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_F(WriteDerivationTest, addToStoreFromDumpCalledOnce)
|
||||||
|
{
|
||||||
|
auto drv = makeSimpleDrv();
|
||||||
|
|
||||||
|
auto path1 = writeDerivation(*store, drv, NoRepair);
|
||||||
|
config->readOnly = true;
|
||||||
|
auto path2 = writeDerivation(*store, drv, NoRepair);
|
||||||
|
EXPECT_EQ(path1, path2);
|
||||||
|
EXPECT_THAT(
|
||||||
|
[&] { writeDerivation(*store, drv, Repair); },
|
||||||
|
::testing::ThrowsMessage<Error>(testing::HasSubstrIgnoreANSIMatcher(
|
||||||
|
"operation 'addToStoreFromDump' is not supported by store 'dummy://'")));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nix
|
||||||
|
|
@ -115,23 +115,25 @@ StorePath writeDerivation(Store & store, const Derivation & drv, RepairFlag repa
|
||||||
held during a garbage collection). */
|
held during a garbage collection). */
|
||||||
auto suffix = std::string(drv.name) + drvExtension;
|
auto suffix = std::string(drv.name) + drvExtension;
|
||||||
auto contents = drv.unparse(store, false);
|
auto contents = drv.unparse(store, false);
|
||||||
return readOnly || settings.readOnlyMode ? store.makeFixedOutputPathFromCA(
|
auto hash = hashString(HashAlgorithm::SHA256, contents);
|
||||||
suffix,
|
auto ca = TextInfo{.hash = hash, .references = references};
|
||||||
TextInfo{
|
auto path = store.makeFixedOutputPathFromCA(suffix, ca);
|
||||||
.hash = hashString(HashAlgorithm::SHA256, contents),
|
|
||||||
.references = std::move(references),
|
if (readOnly || settings.readOnlyMode || (store.isValidPath(path) && !repair))
|
||||||
})
|
return path;
|
||||||
: ({
|
|
||||||
StringSource s{contents};
|
StringSource s{contents};
|
||||||
store.addToStoreFromDump(
|
auto path2 = store.addToStoreFromDump(
|
||||||
s,
|
s,
|
||||||
suffix,
|
suffix,
|
||||||
FileSerialisationMethod::Flat,
|
FileSerialisationMethod::Flat,
|
||||||
ContentAddressMethod::Raw::Text,
|
ContentAddressMethod::Raw::Text,
|
||||||
HashAlgorithm::SHA256,
|
HashAlgorithm::SHA256,
|
||||||
references,
|
references,
|
||||||
repair);
|
repair);
|
||||||
});
|
assert(path2 == path);
|
||||||
|
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue