Skip to content

Commit

Permalink
SimpleDOM support for XML (with RapidXml) and JSON (with rapidjson) (a…
Browse files Browse the repository at this point in the history
…libaba#445)

* and change ```integration``` into ```ecosystem```
* Use fetchcontent instead of copy code

---------

Co-authored-by: Coldwings <coldwings@me.com>
  • Loading branch information
lihuiba and Coldwings authored Apr 9, 2024
1 parent 499c28b commit e8db8d2
Show file tree
Hide file tree
Showing 27 changed files with 793 additions and 391 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ set(PHOTON_FSTACK_SOURCE "" CACHE STRING "")
set(PHOTON_E2FS_SOURCE "" CACHE STRING "")
set(PHOTON_GFLAGS_SOURCE "https://github.com/gflags/gflags/archive/refs/tags/v2.2.2.tar.gz" CACHE STRING "")
set(PHOTON_GOOGLETEST_SOURCE "https://github.com/google/googletest/archive/refs/tags/release-1.12.1.tar.gz" CACHE STRING "")
set(PHOTON_RAPIDJSON_SOURCE "https://github.com/Tencent/rapidjson/archive/refs/tags/v1.1.0.zip" CACHE STRING "")
set(PHOTON_RAPIDXML_SOURCE "https://sourceforge.net/projects/rapidxml/files/rapidxml/rapidxml%201.13/rapidxml-1.13.zip/download" CACHE STRING "")
set(PHOTON_CPP_REDIS_SOURCE "https://github.com/cpp-redis/cpp_redis/archive/refs/tags/4.3.1.tar.gz" CACHE STRING "")

# Get CPU arch and number
execute_process(COMMAND uname -m OUTPUT_VARIABLE ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
Expand Down Expand Up @@ -156,6 +159,7 @@ endforeach ()
################################################################################

add_subdirectory(third_party)
add_subdirectory(ecosystem)

# Compile photon objects
file(GLOB PHOTON_SRC
Expand Down Expand Up @@ -183,7 +187,7 @@ file(GLOB PHOTON_SRC
net/security-context/tls-stream.cpp
rpc/*.cpp
thread/*.cpp
integration/*.cpp
ecosystem/*.cpp
)
if (APPLE)
list(APPEND PHOTON_SRC io/kqueue.cpp)
Expand All @@ -209,6 +213,7 @@ endif ()

# An object library compiles source files but does not archive or link their object files.
add_library(photon_obj OBJECT ${PHOTON_SRC})
target_link_libraries(photon_obj PRIVATE ecosystem_deps)
target_include_directories(photon_obj PRIVATE include ${OPENSSL_INCLUDE_DIRS} ${AIO_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS}
)
Expand Down Expand Up @@ -355,6 +360,7 @@ if (PHOTON_BUILD_TESTING)
add_subdirectory(rpc/test)
add_subdirectory(thread/test)
add_subdirectory(net/security-context/test)
add_subdirectory(ecosystem/test)
if (PHOTON_ENABLE_EXTFS)
add_subdirectory(fs/extfs/test)
endif ()
Expand Down
5 changes: 3 additions & 2 deletions common/enumerable.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ struct Enumerable
{
T* obj;
explicit iterator(T* obj) : obj(obj) {
if (obj && obj->next() < 0)
if (obj && !obj->valid())
this->obj = nullptr;
}
using R = typename std::result_of<decltype(&T::get)(T)>::type;
R operator*() { return obj->get(); }
R operator*() { return obj ? obj->get() : nullptr; }
bool operator==(const iterator& rhs) const { return obj == rhs.obj; }
bool operator!=(const iterator& rhs) const { return !(*this == rhs); }
iterator& operator++()
Expand Down Expand Up @@ -92,6 +92,7 @@ inline void __example_of_enumerable__()
{
struct exam
{
bool valid() { return false; }
int next() { return -1; } // move to next, return 0 for success, -1 for failure
double* get() { return nullptr; } // get current result
};
Expand Down
2 changes: 2 additions & 0 deletions common/iovector.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ limitations under the License.
#include <photon/common/io-alloc.h>

#pragma GCC diagnostic push
#if __GNUC__ >= 13
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wzero-length-bounds"
#endif

inline bool operator == (const iovec& a, const iovec& b)
{
Expand Down
3 changes: 3 additions & 0 deletions common/retval.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ struct retval : public retval_base {
operator T() const {
return get();
}
T operator->() {
return get();
}
T get() const {
return _val;
}
Expand Down
67 changes: 0 additions & 67 deletions common/simple_dom.cpp

This file was deleted.

78 changes: 0 additions & 78 deletions common/simple_dom_impl.h

This file was deleted.

2 changes: 2 additions & 0 deletions common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ struct ptr_array_t
T* pend;
T* begin() { return pbegin; }
T* end() { return pend; }
T& front() { return pbegin[0]; }
T& back() { return pend[-1]; }
};

template<typename T>
Expand Down
57 changes: 57 additions & 0 deletions ecosystem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Fetch depend code for ecosystem
# Here for header-only parts.

include(FetchContent)

# Rapidjson
FetchContent_Declare(
rapidjson
URL ${PHOTON_RAPIDJSON_SOURCE}
URL_HASH SHA256=8e00c38829d6785a2dfb951bb87c6974fa07dfe488aa5b25deec4b8bc0f6a3ab
PATCH_COMMAND patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/patches/rapidjson.patch
DOWNLOAD_EXTRACT_TIMESTAMP ON
UPDATE_DISCONNECTED 1)
# Pre-set rapidjson build option, prevent building docs, examples and tests
set(RAPIDJSON_BUILD_DOC OFF CACHE BOOL "Build rapidjson documentation.")
set(RAPIDJSON_BUILD_EXAMPLES OFF CACHE BOOL "Build rapidjson documentation.")
set(RAPIDJSON_BUILD_TESTS OFF CACHE BOOL "Build rapidjson perftests and unittests.")
FetchContent_MakeAvailable(rapidjson)
message(STATUS "Rapidjson source dir: ${rapidjson_SOURCE_DIR}")

# RapidXml
FetchContent_Declare(
rapidxml
URL ${PHOTON_RAPIDXML_SOURCE}
URL_HASH
SHA256=c3f0b886374981bb20fabcf323d755db4be6dba42064599481da64a85f5b3571
DOWNLOAD_EXTRACT_TIMESTAMP ON
UPDATE_DISCONNECTED 1)
FetchContent_MakeAvailable(rapidxml)
message(STATUS "Rapidxml source dir: ${rapidxml_SOURCE_DIR}")

# cpp-redis
FetchContent_Declare(
cpp-redis
URL ${PHOTON_CPP_REDIS_SOURCE}
URL_HASH
SHA256=3859289d8254685fc775bda73de03dad27df923423b8ceb375b02d036c03b02f
DOWNLOAD_EXTRACT_TIMESTAMP ON
UPDATE_DISCONNECTED 1)
# uses only a simple header, so do not add sub directory to avoid unnecessary build
# do not use FetchContent_MakeAvailable, just populate it.
FetchContent_GetProperties(cpp-redis)
if(NOT cpp-redis_POPULATED)
FetchContent_Populate(cpp-redis)
endif()
message(STATUS "cpp-redis source dir: ${cpp-redis_SOURCE_DIR}")

add_library(ecosystem_deps INTERFACE)
target_include_directories(
ecosystem_deps
INTERFACE ${rapidjson_SOURCE_DIR}/include ${rapidxml_SOURCE_DIR}
${cpp-redis_SOURCE_DIR}/includes)
get_property(
incs
TARGET ecosystem_deps
PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
message(STATUS "ecosystem_deps incs: ${incs}")
61 changes: 61 additions & 0 deletions ecosystem/patches/rapidjson.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h
index 19f8849b..618492a4 100644
--- a/include/rapidjson/reader.h
+++ b/include/rapidjson/reader.h
@@ -153,6 +153,7 @@ enum ParseFlag {
kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings.
kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays.
kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles.
+ kParseBoolsAsStringFlag = 512, //!< Parse all booleans (true/false) as strings.
kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS
};

@@ -201,6 +202,8 @@ struct BaseReaderHandler {
bool Default() { return true; }
bool Null() { return static_cast<Override&>(*this).Default(); }
bool Bool(bool) { return static_cast<Override&>(*this).Default(); }
+ // enabled via kParseBoolsAsStringsFlag, string is not null-terminated (use length)
+ bool RawBool(const Ch* str, SizeType len, bool copy) { return static_cast<Override&>(*this).Default(); }
bool Int(int) { return static_cast<Override&>(*this).Default(); }
bool Uint(unsigned) { return static_cast<Override&>(*this).Default(); }
bool Int64(int64_t) { return static_cast<Override&>(*this).Default(); }
@@ -714,13 +717,22 @@ private:
RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell());
}

+ template<unsigned parseFlags, typename InputStream, typename Handler>
+ void ParseRawBools(InputStream& is, Handler& handler) {
+
+ }
+
template<unsigned parseFlags, typename InputStream, typename Handler>
void ParseTrue(InputStream& is, Handler& handler) {
RAPIDJSON_ASSERT(is.Peek() == 't');
+ auto begin = is.PutBegin();
is.Take();

if (RAPIDJSON_LIKELY(Consume(is, 'r') && Consume(is, 'u') && Consume(is, 'e'))) {
- if (RAPIDJSON_UNLIKELY(!handler.Bool(true)))
+ auto copy = !(parseFlags & kParseInsituFlag);
+ bool ret = (parseFlags & kParseBoolsAsStringFlag) ?
+ handler.RawBool(begin, 4, copy) : handler.Bool(true);
+ if (RAPIDJSON_UNLIKELY(!ret))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
}
else
@@ -730,10 +742,14 @@ private:
template<unsigned parseFlags, typename InputStream, typename Handler>
void ParseFalse(InputStream& is, Handler& handler) {
RAPIDJSON_ASSERT(is.Peek() == 'f');
+ auto begin = is.PutBegin();
is.Take();

if (RAPIDJSON_LIKELY(Consume(is, 'a') && Consume(is, 'l') && Consume(is, 's') && Consume(is, 'e'))) {
- if (RAPIDJSON_UNLIKELY(!handler.Bool(false)))
+ auto copy = !(parseFlags & kParseInsituFlag);
+ bool ret = (parseFlags & kParseBoolsAsStringFlag) ?
+ handler.RawBool(begin, 5, copy) : handler.Bool(false);
+ if (RAPIDJSON_UNLIKELY(!ret))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
}
else
4 changes: 2 additions & 2 deletions integration/redis.cpp → ecosystem/redis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include <photon/integration/redis.h>
#include <photon/ecosystem/redis.h>
#include <inttypes.h>
#include <memory>
#include <photon/net/socket.h>
#include <photon/common/alog.h>
#include "cpp-redis/network/tcp_client_iface.hpp"
#include <cpp_redis/network/tcp_client_iface.hpp>

namespace photon {
using namespace net;
Expand Down
File renamed without changes.
Loading

0 comments on commit e8db8d2

Please sign in to comment.