Skip to content

Commit

Permalink
add web queue
Browse files Browse the repository at this point in the history
grpc support
  • Loading branch information
EfesX authored Jun 5, 2024
1 parent e92cf30 commit b5fffae
Show file tree
Hide file tree
Showing 19 changed files with 819 additions and 129 deletions.
32 changes: 22 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@ on:
push:
branches: [ "main", "develop" ]
pull_request:
branches: [ "main", "develop" ]
branches: [ "main" ]

jobs:
build_job:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: checkout
uses: actions/checkout@v4
with:
submodules: true

- uses: actions/cache@v3
with:
path: build/vcpkg_installed
key: ${{ runner.os }}-vcpkg-${{ hashFiles('./vcpkg.json') }}
restore-keys: ${{ runner.os }}-vcpkg-

- name: installing
run: sudo apt install cmake make g++ clang-tidy
- name: installing
run: |
sudo apt install cmake make g++ clang-tidy
git clone https://github.com/microsoft/vcpkg
- name: configuring
run: cmake -S . -B build
- name: configuring
run: cmake -DCMAKE_TOOLCHAIN_FILE=${PWD}/vcpkg/scripts/buildsystems/vcpkg.cmake -S . -B build

- name: building
run: cmake --build build
- name: building
run: cmake --build build -j $(nproc)

- name: testing
run: ctest -VV --test-dir build/tests
- name: testing
run: ctest -VV --test-dir build/tests
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ build
Testing

/CMakePresets.json

/*.data

/vcpkg_installed
/vcpkg
41 changes: 36 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
cmake_minimum_required(VERSION 3.27)
project(equeue)
cmake_minimum_required(VERSION 3.28)

option(BUILD_TESTS "Build the unit tests" ON)
#set(VCPKG_ROOT "vcpkg" CACHE PATH "vcpkg root dir")
#set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE FILEPATH "toolchain")

set(CMAKE_CXX_STANDARD 20)

add_subdirectory(proto)
option(BUILD_TESTS "Build the unit tests" ON)

project(equeue LANGUAGES C CXX)

file(GLOB_RECURSE ALL_SOURCE_FILES ${CMAKE_SOURCE_DIR}/*.hpp)

#set(CMAKE_CXX_CLANG_TIDY clang-tidy)
add_custom_target(
lint
COMMAND clang-tidy
${ALL_SOURCE_FILES}
--config-file=${CMAKE_SOURCE_DIR}/.clang-tidy
)

#if(UNIX)
# if(NOT EXISTS ${VCPKG_ROOT}/vcpkg)
# execute_process(COMMAND ./bootstrap-vcpkg.sh -disableMetrics WORKING_DIRECTORY ${VCPKG_ROOT})
# endif()
# set(VCPKG_EXEC ${VCPKG_ROOT}/vcpkg CACHE FILEPATH "vcpkg executable")
#elseif(WIN32)
# if(NOT EXISTS ${VCPKG_ROOT}/vcpkg)
# execute_process(COMMAND ./bootstrap-vcpkg.bat -disableMetrics WORKING_DIRECTORY ${VCPKG_ROOT})
# endif()
# set(VCPKG_EXEC ${VCPKG_ROOT}/vcpkg.exe CACHE FILEPATH "vcpkg executable")
#else()
# message(FATAL_ERROR "Not Recognized OS Error")
#endif()
#
#function(LibraryInstall LIB)
# execute_process(COMMAND ${VCPKG_EXEC} install ${LIB})
#endfunction()

#############################################################
add_subdirectory(proto)

add_library(${PROJECT_NAME} INTERFACE)
target_sources(${PROJECT_NAME} INTERFACE
Expand All @@ -20,6 +50,7 @@ target_sources(${PROJECT_NAME} INTERFACE
detail/utils.hpp
queue.hpp
proto/queue.pb.h
proto/queue.grpc.pb.h
)

if(${BUILD_TESTS})
Expand Down
19 changes: 0 additions & 19 deletions equeue.data

This file was deleted.

Binary file removed equeue_md.data
Binary file not shown.
2 changes: 1 addition & 1 deletion include/detail/meta.hpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <type_traits>
#include <concepts>
#include <string>
#include <type_traits>

#include "types.hpp"

Expand Down
22 changes: 17 additions & 5 deletions include/detail/proto_storage.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using node_p_t = std::shared_ptr<QueueStorageNode>;
template<typename T>
using node_container_t = std::list<T>;

bool operator==(const node_p_t& lhs, const node_p_t& rhs){
bool operator==(const node_p_t lhs, const node_p_t rhs){
if (lhs->priority() != rhs->priority()) return false;
//if (lhs->created_at() != rhs->created_at()) return false;

Expand Down Expand Up @@ -64,11 +64,12 @@ class proto_storage
node_p_t node;

wrapper_node() : node(std::make_shared<node_t>()) {}
explicit wrapper_node(const node_t& _node) : node(std::make_shared<node_t>(_node)) {}
wrapper_node(const node_t& _node) {
node = std::make_shared<node_t>(_node);
}

wrapper_node(const wrapper_node&) = delete;
wrapper_node(wrapper_node&&) = delete;
wrapper_node& operator=(wrapper_node&) = delete;
wrapper_node& operator=(wrapper_node&&) = delete;
~wrapper_node() = default;

Expand All @@ -85,10 +86,15 @@ class proto_storage

public:
using node = node_t;
using node_p = node_p_t;
using node_ptr = node_p_t;

proto_storage() = default;
~proto_storage() = default;

proto_storage(const proto_storage&) = delete;
proto_storage(proto_storage&&) = delete;
proto_storage& operator=(proto_storage&&) = delete;
proto_storage operator=(const proto_storage&) = delete;

inline node_p_t& extract(){
return store.back().node;
Expand All @@ -99,6 +105,12 @@ class proto_storage
store.pop_back();
}

void insert(QueueStorageNode& node){
store.emplace_front();
store.front().node = std::make_shared<QueueStorageNode>(node);
if(node.priority() != 0) store.sort();
}

void insert(uint32_t priority, const void* raw, std::size_t size){
//using google::protobuf::util::TimeUtil;

Expand Down Expand Up @@ -162,7 +174,7 @@ class proto_storage
QueueStorage pb_store;

for (auto it = store.rbegin(); it != store.rend(); it++){
node_t* _node = pb_store.add_node();
node* _node = pb_store.add_node();
// is it really nead to copy? may be to swap and to erase?
_node->CopyFrom(*it->node.get());
}
Expand Down
2 changes: 1 addition & 1 deletion include/detail/types.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ enum value_t {

}; // namespace detail
}; // namespace queue
}; // namespace efesx
}; // namespace detail
5 changes: 2 additions & 3 deletions include/queue.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ namespace queue {
using storage_t = detail::proto_storage;

using node_t = storage_t::node;
using node_p_t = storage_t::node_p;
using node_p_t = storage_t::node_ptr;
using node_value_t = node_t::DataCase;

template<typename Storage>
class basic_queue {
private:
Storage storage;
int a = 8;
public:
basic_queue() = default;
~basic_queue() = default;
Expand All @@ -39,7 +38,7 @@ class basic_queue {

/**
* @brief Get top data node from queue
* @return node_p_t& - reference to shared pointer of data node in queue
* @return node_p_t& - the reference to shared pointer of data node in queue
*/
node_p_t& dequeue(){
return storage.extract();
Expand Down
106 changes: 106 additions & 0 deletions include/ts_queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#pragma once

#include <shared_mutex>
#include <any>

#include "detail/proto_storage.hpp"
#include "queue.hpp"


namespace efesx::ts_queue {

using storage_t = queue::detail::proto_storage;

using node_t = storage_t::node;
using node_p_t = storage_t::node_ptr;
using node_value_t = node_t::DataCase;

template<typename BasicQueue>
class basic_ts_queue {
private:
BasicQueue queue;
std::shared_mutex mtx;

public:
basic_ts_queue() = default;
~basic_ts_queue() = default;

basic_ts_queue(const basic_ts_queue&) = delete;
basic_ts_queue(basic_ts_queue&&) = delete;
basic_ts_queue& operator=(basic_ts_queue&&) = delete;
basic_ts_queue operator=(const basic_ts_queue&) = delete;

/**
* @brief Put any (any standard type in C++, except classes, structs and enums) data to queue.
*/
template<typename...Args>
void enqueue(Args...args){
std::unique_lock lck(mtx);
queue.enqueue(args...);
}

/**
* @brief Get top data node from queue
* @return node_p_t& - the reference to shared pointer of data node in queue
*/
std::any dequeue(bool _pop = false){
std::unique_lock lock(mtx);

if (queue.empty()) return std::any();

node_p_t res = queue.dequeue();

if(_pop) queue.pop();

return res;
}

/**
* @brief Delete delete top data node from queue
*/
void pop(){
std::unique_lock lck(mtx);
if (queue.empty()) return;
queue.pop();
}

/**
* @brief Returns amount of data nodes in queue
*/
std::size_t amount(){
std::shared_lock lck(mtx);
return queue.amount();
}

/**
* @brief Is queue empty?
*/
bool empty(){
std::shared_lock lck(mtx);
return queue.empty();
}

/**
* @brief Serialize queue to file
*/
bool save_to_disk(const std::string& file){
std::shared_lock lck(mtx);
return queue.save_to_disk(file);
}

/**
* @brief Deserialize queue to file
*/
bool load_from_disk(const std::string& file){
std::unique_lock lck(mtx);
return queue.load_from_disk(file);
}

bool operator==(const basic_ts_queue<BasicQueue>& oth) const {
return (queue == oth.queue);
}
};

using ts_queue = basic_ts_queue<efesx::queue::basic_queue<storage_t>>;

}; // namespace efesx::ts_queue
Empty file modified include/utils.hpp
100644 → 100755
Empty file.
Loading

0 comments on commit b5fffae

Please sign in to comment.