jamesge commented on a change in pull request #972: Redis server protocol
URL: https://github.com/apache/incubator-brpc/pull/972#discussion_r355397799
 
 

 ##########
 File path: src/brpc/redis_reply.h
 ##########
 @@ -160,6 +212,87 @@ inline int64_t RedisReply::integer() const {
     return 0;
 }
 
+inline bool RedisReply::SetNilString() {
+    if (!_arena || _has_set) return false;
+    _type = REDIS_REPLY_STRING;
+    _length = npos;
+    _has_set = true;
+    return true;
+}
+
+inline bool RedisReply::SetArray(int size) {
+    if (!_arena || _has_set) {
+        return false;
+    }
+    _type = REDIS_REPLY_ARRAY;
+    if (size < 0) {
+        _length = npos;
+        return true;
+    } else if (size == 0) {
+        _length = 0;
+        return true;
+    }
+    RedisReply* subs = (RedisReply*)_arena->allocate(sizeof(RedisReply) * 
size);
+    if (!subs) {
+        LOG(FATAL) << "Fail to allocate RedisReply[" << size << "]";
+        return false;
+    }
+    for (int i = 0; i < size; ++i) {
+        new (&subs[i]) RedisReply(_arena);
+    }
+    _length = size;
+    _data.array.replies = subs;
+    _has_set = true;
+    return true;
+}
+
+inline bool RedisReply::SetBasicString(const std::string& str, RedisReplyType 
type) {
+    if (!_arena || _has_set) {
+        return false;
+    }
+    const size_t size = str.size();
+    if (size < sizeof(_data.short_str)) {
+        memcpy(_data.short_str, str.c_str(), size);
+        _data.short_str[size] = '\0';
+    } else {
+        char* d = (char*)_arena->allocate((size/8 + 1) * 8);
+        if (!d) {
+            LOG(FATAL) << "Fail to allocate string[" << size << "]";
+            return false;
+        }
+        memcpy(d, str.c_str(), size);
+        d[size] = '\0';
+        _data.long_str = d;
+    }
+    _type = type;
+    _length = size;
+    _has_set = true;
+    return true;
+}
+
+inline bool RedisReply::SetStatus(const std::string& str) {
+    return SetBasicString(str, REDIS_REPLY_STATUS);
+}
+
+inline bool RedisReply::SetError(const std::string& str) {
+    return SetBasicString(str, REDIS_REPLY_ERROR);
+}
+
+inline bool RedisReply::SetInteger(int64_t value) {
 
 Review comment:
   nice-to-have:SetInteger是一个常见操作,越简单越好

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to