Code Monkey home page Code Monkey logo

mario-1's Introduction

Overview

A library that make the write from synchronous to asynchronous.

It is thread safe and easily used.

Idea

The base idea of the Mario is that if you have operators that cost lots of time. But you want quickly return from the operator, and leave the work to the background.

So mario library can easily solve your problem. You just need the implement your own consume message function. Then mario will create background thread do the other things.

Two engine type

  • memory

This type will leave the cache buffer in memory. The advantage is the Insert speed is faster. But when your application shutdown before you have consume the data in the buffer, The data will lost.

  • file

Thie type will make a write2file in the local log path. When you put message, you will write the data in the write2file. The write is append operator, so it is also very fast. The application don't initiativly flush the data to disk, it flush according to the operator system.

If there is still some data havn't been consumed after shutdown you application, it doesn't matter. it will leave a menifest file in the log path, after you restart you application. it will first get the menifest file, and get the offset. Then it consume the data from the offset

Usage

make ENGINE=memory

then you will get a output path in this path, it include the lib and the include file.

You also can see how to used in the example path

Example


#include "mario.h"
#include "consumer.h"
#include "mutexlock.h"
#include "port.h"

/**
 * @brief The handler inherit from the Consumer::Handler
 * The processMsg is pure virtual function, so you need implementation your own
 * version
 */
class FileHandler : public mario::Consumer::Handler
{
    public:
        FileHandler() {};
        virtual void processMsg(const std::string &item) {
            log_info("consume data %s", item.data());
        }
};

int main()
{
    mario::Status s;
    FileHandler *fh = new FileHandler();
    /**
     * @brief
     *
     * @param 1 is the thread number
     * @param fh is the handler that you implement. It tell the mario how
     * to consume the data
     *
     * @return
     */
    mario::Mario *m = new mario::Mario(1, fh);

    std::string item = "Put data in mario";
    s = m->Put(item);
    if (!s.ok()) {
        log_err("Put error");
        exit(-1);
    }

    delete m;
    delete fh;
    return 0;
}

mario-1's People

Contributors

baotiao avatar wenduo avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.