Skip to content

Commit

Permalink
fixup: refactored JobControlRecord cache interface
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Oct 15, 2019
1 parent 7f18805 commit 6c047c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
16 changes: 12 additions & 4 deletions core/src/include/jcr.h
Expand Up @@ -681,16 +681,24 @@ extern void b_free_jcr(const char* file, int line, JobControlRecord* jcr);
typedef void(dbg_jcr_hook_t)(JobControlRecord* jcr, FILE* fp);
extern void DbgJcrAddHook(dbg_jcr_hook_t* fct);

struct SessionIdentifier {
uint32_t id_{0};
uint32_t time_{0};
SessionIdentifier(uint32_t id, uint32_t time) : id_(id), time_(time) {}
bool operator==(const SessionIdentifier& rhs) const
{
return id_ == rhs.id_ && time_ == rhs.time_;
}
};

/* new interface */
void InitJcr(std::shared_ptr<JobControlRecord> jcr,
JCR_free_HANDLER* daemon_free_jcr);
std::size_t GetJcrCount();
std::shared_ptr<JobControlRecord> GetJcrById(uint32_t JobId);
std::shared_ptr<JobControlRecord> GetJcrByFullName(char* name_in);
std::shared_ptr<JobControlRecord> GetJcrByPartialName(char* name_in);
std::shared_ptr<JobControlRecord> GetJcrBySession(uint32_t SessionId,
uint32_t SessionTime);
std::shared_ptr<JobControlRecord> GetJcrByFullName(std::string name);
std::shared_ptr<JobControlRecord> GetJcrByPartialName(std::string name);
std::shared_ptr<JobControlRecord> GetJcrBySession(SessionIdentifier identifier);
uint32_t GetJobIdByThreadId(pthread_t tid);
/* ************* */

Expand Down
25 changes: 10 additions & 15 deletions core/src/lib/jcr.cc
Expand Up @@ -529,30 +529,25 @@ std::shared_ptr<JobControlRecord> GetJcrById(uint32_t JobId)
[JobId](const JobControlRecord* jcr) { return jcr->JobId == JobId; });
}

std::shared_ptr<JobControlRecord> GetJcrByFullName(char* name_in)
std::shared_ptr<JobControlRecord> GetJcrByFullName(std::string name)
{
return GetJcr([name_in](const JobControlRecord* jcr) {
std::string name(name_in);
std::string job_name(jcr->Job);
return job_name == name;
return GetJcr([&name](const JobControlRecord* jcr) {
return std::string{jcr->Job} == name;
});
}

std::shared_ptr<JobControlRecord> GetJcrByPartialName(char* name_in)
std::shared_ptr<JobControlRecord> GetJcrByPartialName(std::string name)
{
return GetJcr([name_in](const JobControlRecord* jcr) {
std::string name(name_in);
std::string job_name(jcr->Job);
return job_name.find(name) == 0;
return GetJcr([&name](const JobControlRecord* jcr) {
return std::string{jcr->Job}.find(name) == 0;
});
}

std::shared_ptr<JobControlRecord> GetJcrBySession(uint32_t SessionId,
uint32_t SessionTime)
std::shared_ptr<JobControlRecord> GetJcrBySession(SessionIdentifier identifier)
{
return GetJcr([SessionId, SessionTime](const JobControlRecord* jcr) {
return (jcr->VolSessionId == SessionId &&
jcr->VolSessionTime == SessionTime);
return GetJcr([&identifier](const JobControlRecord* jcr) {
return (SessionIdentifier{jcr->VolSessionId, jcr->VolSessionTime} ==
identifier);
});
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/tests/job_control_record.cc
Expand Up @@ -122,15 +122,15 @@ TEST_F(JobControlRecordTest, get_job_by_partial_name_not_found)

TEST_F(JobControlRecordTest, get_job_by_session)
{
auto found_jcr = GetJcrBySession(11, 101);
auto found_jcr = GetJcrBySession({11, 101});
EXPECT_EQ(jobs[1].get(), found_jcr.get());
}

TEST_F(JobControlRecordTest, get_job_by_session_not_found)
{
auto found_jcr = GetJcrBySession(12, 101);
auto found_jcr = GetJcrBySession({12, 101});
EXPECT_FALSE(found_jcr.get());

found_jcr = GetJcrBySession(11, 103);
found_jcr = GetJcrBySession({11, 103});
EXPECT_FALSE(found_jcr.get());
}

0 comments on commit 6c047c6

Please sign in to comment.