This repository was archived by the owner on Oct 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
Support redis versioning #424
Open
pranavk
wants to merge
1
commit into
google:main
Choose a base branch
from
pranavk:redis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,16 +17,72 @@ | |
| #include "llvm/Support/CommandLine.h" | ||
| #include "hiredis.h" | ||
|
|
||
| #include <sstream> | ||
|
|
||
| using namespace llvm; | ||
| using namespace souper; | ||
|
|
||
| static cl::opt<unsigned> RedisPort("souper-redis-port", cl::init(6379), | ||
| cl::desc("Redis server port (default=6379)")); | ||
|
|
||
| namespace { | ||
|
|
||
| std::vector<std::string> getActiveOptions(StringMap<cl::Option*> &Opts) { | ||
| std::vector<std::string> ActiveOptions; | ||
|
|
||
| for (auto &K : Opts.keys()) { | ||
| if (Opts[K]->getNumOccurrences()) { | ||
| std::string StrValue; | ||
|
|
||
| // cl::opt<unsigned> | ||
| if (K == "souper-exhaustive-synthesis-num-instructions") { | ||
| auto Val = static_cast<cl::opt<unsigned> *>(Opts[K]); | ||
| StrValue = K.str() + "=" + std::to_string(Val->getValue()); | ||
| } | ||
| // cl::opt<bool, true> with default=false | ||
| else if (K == "souper-use-alive") { | ||
| auto Val = static_cast<cl::opt<bool, true> *>(Opts[K]); | ||
| StrValue = K.str() + "=" + (Val->getValue() ? "true" : "false"); | ||
| } | ||
| // cl::opt<bool> with default=false | ||
| else if (K == "souper-lsb-pruning" || | ||
| K == "souper-dataflow-pruning" || | ||
| K == "souper-synthesis-const-with-cegis" || | ||
| K == "souper-synthesis-ignore-cost") { | ||
| auto Val = static_cast<cl::opt<bool> *>(Opts[K]); | ||
| if (Val->getValue()) | ||
| StrValue = K.str() + "=true"; | ||
| } | ||
| // cl::opt<int> | ||
| else if (K == "souper-synthesis-comp-num") { | ||
| auto Val = static_cast<cl::opt<int> *>(Opts[K]); | ||
| StrValue = K.str() + "=" + std::to_string(Val->getValue()); | ||
| } | ||
| // cl::opt<std::string> | ||
| else if (K == "souper-synthesis-comps") { | ||
| auto Val = static_cast<cl::opt<std::string> *>(Opts[K]); | ||
| StrValue = K.str() + "=" + Val->getValue(); | ||
| } | ||
|
|
||
| if (!StrValue.empty()) | ||
| ActiveOptions.emplace_back(StrValue); | ||
| } | ||
| } | ||
|
|
||
| return ActiveOptions; | ||
| } | ||
|
|
||
| } // anon ns | ||
|
|
||
| namespace souper { | ||
|
|
||
| class KVStore::KVImpl { | ||
| redisContext *Ctx; | ||
| redisContext *Ctx = 0; | ||
|
|
||
| private: | ||
| // checks if current redis database is compatible with current version of souper | ||
| bool checkCompatibility(); | ||
|
|
||
| public: | ||
| KVImpl(); | ||
| ~KVImpl(); | ||
|
|
@@ -46,12 +102,54 @@ KVStore::KVImpl::KVImpl() { | |
| llvm::report_fatal_error((llvm::StringRef)"Redis connection error: " + | ||
| Ctx->errstr + "\n"); | ||
| } | ||
|
|
||
| if (!checkCompatibility()) { | ||
| llvm::report_fatal_error("Redis cache on port " + std::to_string(RedisPort) + " is incompatible."); | ||
| } | ||
| } | ||
|
|
||
| KVStore::KVImpl::~KVImpl() { | ||
| redisFree(Ctx); | ||
| } | ||
|
|
||
| bool KVStore::KVImpl::checkCompatibility() { | ||
| assert(Ctx && "Cannot check compatibility on an uninitialized database."); | ||
|
|
||
| redisReply *reply = static_cast<redisReply*>(redisCommand(Ctx, "GET cachetype")); | ||
| if (!reply || Ctx->err) { | ||
| llvm::report_fatal_error((llvm::StringRef)"Redis error: " + Ctx->errstr); | ||
| } | ||
|
|
||
| // get all current command line used | ||
| StringMap<cl::Option*> &Opts = cl::getRegisteredOptions(); | ||
| std::vector<std::string> ActiveOptions = getActiveOptions(Opts); | ||
| std::sort(ActiveOptions.begin(), ActiveOptions.end()); | ||
| std::ostringstream ActiveOptionsOSS; | ||
| const char *delim = ";"; | ||
| std::copy(ActiveOptions.begin(), ActiveOptions.end(), | ||
| std::ostream_iterator<std::string>(ActiveOptionsOSS, delim)); | ||
| std::string ActiveOptionsStr = ActiveOptionsOSS.str(); | ||
|
|
||
| switch(reply->type) { | ||
| case REDIS_REPLY_NIL: | ||
| // no version set | ||
| freeReplyObject(reply); | ||
| reply = static_cast<redisReply*>(redisCommand(Ctx, "SET cachetype %s", ActiveOptionsStr.data())); | ||
| // TODO: Factor out all such snippets | ||
| if (!reply || Ctx->err) { | ||
| llvm::report_fatal_error((llvm::StringRef)"Redis error: " + Ctx->errstr); | ||
| } | ||
| break; | ||
| case REDIS_REPLY_STRING: | ||
| if (llvm::StringRef value = reply->str; value != ActiveOptionsStr) | ||
| return false; | ||
| break; | ||
| default: return false; | ||
| } | ||
|
|
||
| return true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is dead code, please remove
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not a dead code.
|
||
| } | ||
|
|
||
| void KVStore::KVImpl::hIncrBy(llvm::StringRef Key, llvm::StringRef Field, | ||
| int Incr) { | ||
| redisReply *reply = (redisReply *)redisCommand(Ctx, "HINCRBY %s %s 1", | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.