Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/rediscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ void RedisCommand::formatHDEL(const std::string& key, const std::vector<std::str
formatArgv(static_cast<int>(args.size()), args.data(), NULL);
}

/* Format EXPIRE key field command */
void RedisCommand::formatEXPIRE(const std::string& key, const int64_t& ttl)
{
return format("EXPIRE %s %d", key.c_str(), ttl);
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%d

hiredis internally implement some int length. ref: https://github.com/redis/hiredis/blob/2d9d77518d012a54ae34f9822e4b4d19823c4b75/hiredis.c#L434

Seems like %lld is for int64_t. Could you check and add unit test? #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I will update it to %lld and add/extend an UT to also issue a set with configured TTL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return in void function? formta() also is void?

}

const char *RedisCommand::c_str() const
{
return temp;
Expand Down
3 changes: 3 additions & 0 deletions common/rediscommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class RedisCommand {
/* Format HDEL key multiple fields command */
void formatHDEL(const std::string& key, const std::vector<std::string>& fields);

/* Format EXPIRE key field command */
void formatEXPIRE(const std::string& key, const int64_t& ttl);

const char *c_str() const;

size_t length() const;
Expand Down
15 changes: 14 additions & 1 deletion common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void Table::hset(const string &key, const std::string &field, const std::string
}

void Table::set(const string &key, const vector<FieldValueTuple> &values,
const string& /*op*/, const string& /*prefix*/)
const string& /*op*/, const string& /*prefix*/, const int64_t &ttl)
{
if (values.size() == 0)
return;
Expand All @@ -136,6 +136,19 @@ void Table::set(const string &key, const vector<FieldValueTuple> &values,
{
m_pipe->flush();
}

if (ttl != DEFAULT_DB_TTL)
{
// Configure the expire time for the entry that was just added
cmd.formatEXPIRE(getKeyName(key), ttl);
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  [](http://example.com/codeflow?start=0&length=6)

Please indent with 4x spaces #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated indents in last commit


m_pipe->push(cmd, REDIS_REPLY_INTEGER);

if (!m_buffered)
{
m_pipe->flush();
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush

Is it possible to flush only once in this function? #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does RedisPipeline support enqueuing of more than one command without it being flushed inbetween?
i.e.:

cmd.formatHMSET(getKeyName(key), values.begin(), values.end());
m_pipe->push(cmd, REDIS_REPLY_STATUS);
cmd.formatEXPIRE(getKeyName(key), ttl);
m_pipe->push(cmd, REDIS_REPLY_INTEGER);
if (!m_buffered)
{
    m_pipe->flush();
}

If it does the code can be changed to:

if (ttl != DEFAULT_DB_TTL)
{
  // Configure the expire time for the entry that was just added
  cmd.formatEXPIRE(getKeyName(key), ttl);

  m_pipe->push(cmd, REDIS_REPLY_INTEGER);

  if (!m_buffered)
  {
      m_pipe->flush();
  }
}
else
{
   if (!m_buffered)
   {
      m_pipe->flush();
   }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By design, yes. In your proposed code, you can further improve by move flush out of if-else.

}
}
}

void Table::del(const string &key, const string& /* op */, const string& /*prefix*/)
Expand Down
9 changes: 7 additions & 2 deletions common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,22 @@ class TableEntryEnumerable {
void getContent(std::vector<KeyOpFieldsValuesTuple> &tuples);
};

/* The default time to live for a DB entry is infinite */
static constexpr int64_t DEFAULT_DB_TTL = -1;

class Table : public TableBase, public TableEntryEnumerable {
public:
Table(const DBConnector *db, const std::string &tableName);
Table(RedisPipeline *pipeline, const std::string &tableName, bool buffered);
~Table() override;

/* Set an entry in the DB directly (op not in use) */
/* Set an entry in the DB directly and configure its TTL, if provided (op not in use) */
virtual void set(const std::string &key,
const std::vector<FieldValueTuple> &values,
const std::string &op = "",
const std::string &prefix = EMPTY_PREFIX);
const std::string &prefix = EMPTY_PREFIX,
const int64_t &ttl = DEFAULT_DB_TTL);

/* Delete an entry in the table */
virtual void del(const std::string &key,
const std::string &op = "",
Expand Down