BinaryCacheStore: Support "none" compression method

This commit is contained in:
Eelco Dolstra 2016-04-29 17:02:57 +02:00
parent 8e065c6b3e
commit 5acb691402
5 changed files with 51 additions and 31 deletions

View file

@ -17,6 +17,7 @@ namespace nix {
BinaryCacheStore::BinaryCacheStore(std::shared_ptr<Store> localStore, BinaryCacheStore::BinaryCacheStore(std::shared_ptr<Store> localStore,
const StoreParams & params) const StoreParams & params)
: localStore(localStore) : localStore(localStore)
, compression(get(params, "compression", "xz"))
{ {
auto secretKeyFile = get(params, "secret-key", ""); auto secretKeyFile = get(params, "secret-key", "");
if (secretKeyFile != "") if (secretKeyFile != "")
@ -45,8 +46,7 @@ Path BinaryCacheStore::narInfoFileFor(const Path & storePath)
return storePathToHash(storePath) + ".narinfo"; return storePathToHash(storePath) + ".narinfo";
} }
void BinaryCacheStore::addToCache(const ValidPathInfo & info, void BinaryCacheStore::addToCache(const ValidPathInfo & info, ref<std::string> nar)
const string & nar)
{ {
/* Verify that all references are valid. This may do some .narinfo /* Verify that all references are valid. This may do some .narinfo
reads, but typically they'll already be cached. */ reads, but typically they'll already be cached. */
@ -62,40 +62,40 @@ void BinaryCacheStore::addToCache(const ValidPathInfo & info,
auto narInfoFile = narInfoFileFor(info.path); auto narInfoFile = narInfoFileFor(info.path);
if (fileExists(narInfoFile)) return; if (fileExists(narInfoFile)) return;
assert(nar.compare(0, narMagic.size(), narMagic) == 0); assert(nar->compare(0, narMagic.size(), narMagic) == 0);
auto narInfo = make_ref<NarInfo>(info); auto narInfo = make_ref<NarInfo>(info);
narInfo->narSize = nar.size(); narInfo->narSize = nar->size();
narInfo->narHash = hashString(htSHA256, nar); narInfo->narHash = hashString(htSHA256, *nar);
if (info.narHash && info.narHash != narInfo->narHash) if (info.narHash && info.narHash != narInfo->narHash)
throw Error(format("refusing to copy corrupted path %1% to binary cache") % info.path); throw Error(format("refusing to copy corrupted path %1% to binary cache") % info.path);
/* Compress the NAR. */ /* Compress the NAR. */
narInfo->compression = "xz"; narInfo->compression = compression;
auto now1 = std::chrono::steady_clock::now(); auto now1 = std::chrono::steady_clock::now();
string narXz = compressXZ(nar); auto narCompressed = compress(compression, nar);
auto now2 = std::chrono::steady_clock::now(); auto now2 = std::chrono::steady_clock::now();
narInfo->fileHash = hashString(htSHA256, narXz); narInfo->fileHash = hashString(htSHA256, *narCompressed);
narInfo->fileSize = narXz.size(); narInfo->fileSize = narCompressed->size();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
printMsg(lvlTalkative, format("copying path %1% (%2% bytes, compressed %3$.1f%% in %4% ms) to binary cache") printMsg(lvlTalkative, format("copying path %1% (%2% bytes, compressed %3$.1f%% in %4% ms) to binary cache")
% narInfo->path % narInfo->narSize % narInfo->path % narInfo->narSize
% ((1.0 - (double) narXz.size() / nar.size()) * 100.0) % ((1.0 - (double) narCompressed->size() / nar->size()) * 100.0)
% duration); % duration);
/* Atomically write the NAR file. */ /* Atomically write the NAR file. */
narInfo->url = "nar/" + printHash32(narInfo->fileHash) + ".nar.xz"; narInfo->url = "nar/" + printHash32(narInfo->fileHash) + ".nar.xz";
if (!fileExists(narInfo->url)) { if (!fileExists(narInfo->url)) {
stats.narWrite++; stats.narWrite++;
upsertFile(narInfo->url, narXz); upsertFile(narInfo->url, *narCompressed);
} else } else
stats.narWriteAverted++; stats.narWriteAverted++;
stats.narWriteBytes += nar.size(); stats.narWriteBytes += nar->size();
stats.narWriteCompressedBytes += narXz.size(); stats.narWriteCompressedBytes += narCompressed->size();
stats.narWriteCompressionTimeMs += duration; stats.narWriteCompressionTimeMs += duration;
/* Atomically write the NAR info file.*/ /* Atomically write the NAR info file.*/
@ -137,12 +137,7 @@ void BinaryCacheStore::narFromPath(const Path & storePath, Sink & sink)
/* Decompress the NAR. FIXME: would be nice to have the remote /* Decompress the NAR. FIXME: would be nice to have the remote
side do this. */ side do this. */
if (info->compression == "none") nar = decompress(info->compression, ref<std::string>(nar));
;
else if (info->compression == "xz")
nar = decompressXZ(*nar);
else
throw Error(format("unknown NAR compression type %1%") % info->compression);
stats.narReadBytes += nar->size(); stats.narReadBytes += nar->size();
@ -261,7 +256,7 @@ Path BinaryCacheStore::addToStore(const string & name, const Path & srcPath,
info.path = makeFixedOutputPath(recursive, hashAlgo, h, name); info.path = makeFixedOutputPath(recursive, hashAlgo, h, name);
if (repair || !isValidPath(info.path)) if (repair || !isValidPath(info.path))
addToCache(info, *sink.s); addToCache(info, sink.s);
return info.path; return info.path;
} }
@ -276,7 +271,7 @@ Path BinaryCacheStore::addTextToStore(const string & name, const string & s,
if (repair || !isValidPath(info.path)) { if (repair || !isValidPath(info.path)) {
StringSink sink; StringSink sink;
dumpString(s, sink); dumpString(s, sink);
addToCache(info, *sink.s); addToCache(info, sink.s);
} }
return info.path; return info.path;
@ -306,7 +301,7 @@ void BinaryCacheStore::buildPaths(const PathSet & paths, BuildMode buildMode)
StringSink sink; StringSink sink;
dumpPath(storePath, sink); dumpPath(storePath, sink);
addToCache(*info, *sink.s); addToCache(*info, sink.s);
} }
} }
@ -404,7 +399,7 @@ Path BinaryCacheStore::importPath(Source & source, std::shared_ptr<FSAccessor> a
bool haveSignature = readInt(source) == 1; bool haveSignature = readInt(source) == 1;
assert(!haveSignature); assert(!haveSignature);
addToCache(info, *tee.data); addToCache(info, tee.data);
auto accessor_ = std::dynamic_pointer_cast<BinaryCacheStoreAccessor>(accessor); auto accessor_ = std::dynamic_pointer_cast<BinaryCacheStoreAccessor>(accessor);
if (accessor_) if (accessor_)

View file

@ -19,6 +19,8 @@ private:
std::shared_ptr<Store> localStore; std::shared_ptr<Store> localStore;
std::string compression;
protected: protected:
BinaryCacheStore(std::shared_ptr<Store> localStore, BinaryCacheStore(std::shared_ptr<Store> localStore,
@ -44,7 +46,7 @@ private:
std::string narInfoFileFor(const Path & storePath); std::string narInfoFileFor(const Path & storePath);
void addToCache(const ValidPathInfo & info, const string & nar); void addToCache(const ValidPathInfo & info, ref<std::string> nar);
public: public:

View file

@ -31,7 +31,7 @@ void builtinFetchurl(const BasicDerivation & drv)
auto unpack = drv.env.find("unpack"); auto unpack = drv.env.find("unpack");
if (unpack != drv.env.end() && unpack->second == "1") { if (unpack != drv.env.end() && unpack->second == "1") {
if (string(*data.data, 0, 6) == string("\xfd" "7zXZ\0", 6)) if (string(*data.data, 0, 6) == string("\xfd" "7zXZ\0", 6))
data.data = decompressXZ(*data.data); data.data = decompress("xz", ref<std::string>(data.data));
StringSource source(*data.data); StringSource source(*data.data);
restorePath(storePath, source); restorePath(storePath, source);
} else } else

View file

@ -15,7 +15,7 @@ struct LzmaStream
lzma_stream & operator()() { return strm; } lzma_stream & operator()() { return strm; }
}; };
std::string compressXZ(const std::string & in) static ref<std::string> compressXZ(const std::string & in)
{ {
LzmaStream strm; LzmaStream strm;
@ -28,7 +28,7 @@ std::string compressXZ(const std::string & in)
lzma_action action = LZMA_RUN; lzma_action action = LZMA_RUN;
uint8_t outbuf[BUFSIZ]; uint8_t outbuf[BUFSIZ];
string res; ref<std::string> res = make_ref<std::string>();
strm().next_in = (uint8_t *) in.c_str(); strm().next_in = (uint8_t *) in.c_str();
strm().avail_in = in.size(); strm().avail_in = in.size();
strm().next_out = outbuf; strm().next_out = outbuf;
@ -43,7 +43,7 @@ std::string compressXZ(const std::string & in)
lzma_ret ret = lzma_code(&strm(), action); lzma_ret ret = lzma_code(&strm(), action);
if (strm().avail_out == 0 || ret == LZMA_STREAM_END) { if (strm().avail_out == 0 || ret == LZMA_STREAM_END) {
res.append((char *) outbuf, sizeof(outbuf) - strm().avail_out); res->append((char *) outbuf, sizeof(outbuf) - strm().avail_out);
strm().next_out = outbuf; strm().next_out = outbuf;
strm().avail_out = sizeof(outbuf); strm().avail_out = sizeof(outbuf);
} }
@ -56,7 +56,7 @@ std::string compressXZ(const std::string & in)
} }
} }
ref<std::string> decompressXZ(const std::string & in) static ref<std::string> decompressXZ(const std::string & in)
{ {
LzmaStream strm; LzmaStream strm;
@ -95,4 +95,24 @@ ref<std::string> decompressXZ(const std::string & in)
} }
} }
ref<std::string> compress(const std::string & method, ref<std::string> in)
{
if (method == "none")
return in;
else if (method == "xz")
return compressXZ(*in);
else
throw UnknownCompressionMethod(format("unknown compression method %s") % method);
}
ref<std::string> decompress(const std::string & method, ref<std::string> in)
{
if (method == "none")
return in;
else if (method == "xz")
return decompressXZ(*in);
else
throw UnknownCompressionMethod(format("unknown compression method %s") % method);
}
} }

View file

@ -1,13 +1,16 @@
#pragma once #pragma once
#include "ref.hh" #include "ref.hh"
#include "types.hh"
#include <string> #include <string>
namespace nix { namespace nix {
std::string compressXZ(const std::string & in); ref<std::string> compress(const std::string & method, ref<std::string> in);
ref<std::string> decompressXZ(const std::string & in); ref<std::string> decompress(const std::string & method, ref<std::string> in);
MakeError(UnknownCompressionMethod, Error);
} }