This is an automated email from the ASF dual-hosted git repository.

jamesge pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-brpc.git

commit e3782eb8f2e649d1f6eaa00aa5f966a74d564d5d
Author: jamesge <[email protected]>
AuthorDate: Wed Nov 25 20:18:51 2020 +0800

    Add SessionLog to remember session-level kv
---
 src/brpc/session_log.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

diff --git a/src/brpc/session_log.h b/src/brpc/session_log.h
new file mode 100644
index 0000000..049232a
--- /dev/null
+++ b/src/brpc/session_log.h
@@ -0,0 +1,80 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#ifndef BRPC_SESSION_LOG_H
+#define BRPC_SESSION_LOG_H
+
+#include "butil/containers/flat_map.h"
+
+namespace brpc {
+    
+class SessionLog {
+public:
+    class Formatter {
+    public:
+        virtual ~Formatter() {}
+        virtual void Print(std::ostream&, const SessionLog&) = 0;
+    };
+
+    typedef butil::FlatMap<std::string, std::string> Map;
+    typedef Map::const_iterator Iterator;
+
+    SessionLog() {}
+
+    // Exchange internal fields with another SessionLog.
+    void Swap(SessionLog &rhs) { _entries.swap(rhs._entries); }
+
+    // Reset internal fields as if they're just default-constructed.
+    void Clear() { _entries.clear(); }
+
+    // Get value of a key(case-sensitive)
+    // Return pointer to the value, NULL on not found.
+    const std::string* Get(const char* key) const { return _entries.seek(key); 
}
+    const std::string* Get(const std::string& key) const { return 
_entries.seek(key); }
+
+    // Set value of a key
+    void Set(const std::string& key, const std::string& value) { GetOrAdd(key) 
= value; }
+    void Set(const std::string& key, const char* value) { GetOrAdd(key) = 
value; }
+    // Convert other types to string as well
+    template <typename T>
+    void Set(const std::string& key, const T& value) { GetOrAdd(key) = 
std::to_string(value); }
+
+    // Remove a key
+    void Remove(const char* key) { _entries.erase(key); }
+    void Remove(const std::string& key) { _entries.erase(key); }
+
+    // Get iterators to iterate key/value
+    Iterator Begin() const { return _entries.begin(); }
+    Iterator End() const { return _entries.end(); }
+    
+    // number of key/values
+    size_t Count() const { return _entries.size(); }
+
+private:
+    std::string& GetOrAdd(const std::string& key) {
+        if (!_entries.initialized()) {
+            _entries.init(29);
+        }
+        return _entries[key];
+    }
+
+    Map _entries;
+};
+
+} // namespace brpc
+
+#endif // BRPC_SESSION_LOG_H


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to