Skip to content

Commit d4def8c

Browse files
committed
Implement realisation operations on dummy store
1 parent 9c815d7 commit d4def8c

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

src/libstore-tests/dummy-store.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <gtest/gtest.h>
22

3-
#include "nix/store/dummy-store.hh"
3+
#include "nix/store/dummy-store-impl.hh"
44
#include "nix/store/globals.hh"
55
#include "nix/store/realisation.hh"
66

@@ -13,7 +13,7 @@ TEST(DummyStore, realisation_read)
1313
auto store = [] {
1414
auto cfg = make_ref<DummyStoreConfig>(StoreReference::Params{});
1515
cfg->readOnly = false;
16-
return cfg->openStore();
16+
return cfg->openDummyStore();
1717
}();
1818

1919
auto drvHash = Hash::parseExplicitFormatUnprefixed(
@@ -22,6 +22,17 @@ TEST(DummyStore, realisation_read)
2222
auto outputName = "foo";
2323

2424
EXPECT_EQ(store->queryRealisation({drvHash, outputName}), nullptr);
25+
26+
UnkeyedRealisation value{
27+
.outPath = StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"},
28+
};
29+
30+
store->buildTrace.insert({drvHash, {{outputName, make_ref<UnkeyedRealisation>(value)}}});
31+
32+
auto value2 = store->queryRealisation({drvHash, outputName});
33+
34+
ASSERT_TRUE(value2);
35+
EXPECT_EQ(*value2, value);
2536
}
2637

2738
} // namespace nix

src/libstore/dummy-store.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "nix/util/callback.hh"
44
#include "nix/util/memory-source-accessor.hh"
55
#include "nix/store/dummy-store-impl.hh"
6+
#include "nix/store/realisation.hh"
67

78
#include <boost/unordered/concurrent_flat_map.hpp>
89

@@ -251,7 +252,10 @@ struct DummyStoreImpl : DummyStore
251252

252253
void registerDrvOutput(const Realisation & output) override
253254
{
254-
unsupported("registerDrvOutput");
255+
auto ref = make_ref<UnkeyedRealisation>(output);
256+
buildTrace.insert_or_visit({output.id.drvHash, {{output.id.outputName, ref}}}, [&](auto & kv) {
257+
kv.second.insert_or_assign(output.id.outputName, make_ref<UnkeyedRealisation>(output));
258+
});
255259
}
256260

257261
void narFromPath(const StorePath & path, Sink & sink) override
@@ -267,9 +271,18 @@ struct DummyStoreImpl : DummyStore
267271
}
268272

269273
void queryRealisationUncached(
270-
const DrvOutput &, Callback<std::shared_ptr<const UnkeyedRealisation>> callback) noexcept override
274+
const DrvOutput & drvOutput, Callback<std::shared_ptr<const UnkeyedRealisation>> callback) noexcept override
271275
{
272-
callback(nullptr);
276+
bool visited = false;
277+
buildTrace.cvisit(drvOutput.drvHash, [&](const auto & kv) {
278+
if (auto it = kv.second.find(drvOutput.outputName); it != kv.second.end()) {
279+
visited = true;
280+
callback(it->second.get_ptr());
281+
}
282+
});
283+
284+
if (!visited)
285+
callback(nullptr);
273286
}
274287

275288
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath) override

src/libstore/include/nix/store/dummy-store-impl.hh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ struct DummyStore : virtual Store
3030
*/
3131
boost::concurrent_flat_map<StorePath, PathInfoAndContents> contents;
3232

33+
/**
34+
* The build trace maps the pair of a content-addressing (fixed or
35+
* floating) derivations an one of its output to a
36+
* (content-addressed) store object.
37+
*
38+
* It is [curried](https://en.wikipedia.org/wiki/Currying), so we
39+
* instead having a single output with a `DrvOutput` key, we have an
40+
* outer map for the derivation, and inner maps for the outputs of a
41+
* given derivation.
42+
*/
43+
boost::concurrent_flat_map<Hash, std::map<std::string, ref<UnkeyedRealisation>>> buildTrace;
44+
3345
DummyStore(ref<const Config> config)
3446
: Store{*config}
3547
, config(config)

src/libstore/include/nix/store/dummy-store.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "nix/store/store-api.hh"
55

6+
#include <boost/unordered/concurrent_flat_map.hpp>
7+
68
namespace nix {
79

810
struct DummyStore;

src/libutil/include/nix/util/hash.hh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,22 @@ public:
222222
};
223223

224224
} // namespace nix
225+
226+
template<>
227+
struct std::hash<nix::Hash>
228+
{
229+
std::size_t operator()(const nix::Hash & hash) const noexcept
230+
{
231+
assert(hash.hashSize > sizeof(size_t));
232+
return *reinterpret_cast<const std::size_t *>(&hash.hash);
233+
}
234+
};
235+
236+
namespace nix {
237+
238+
inline std::size_t hash_value(const Hash & hash)
239+
{
240+
return std::hash<Hash>{}(hash);
241+
}
242+
243+
} // namespace nix

0 commit comments

Comments
 (0)