派生自 development/c++

pansen
2019-03-05 ed223aee5fe5f3b222488549597976f34c679c3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <vector>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
 
#include <jsoncpp/json/json.h>
#include <SemTool.h>
#include <basic/util/app/AppConfig.h>
#include <basic/debug/Debug.h>
 
#include "ErlangDbTool.h"
#include "ShareMemoryTool.hpp"
 
using namespace std;
 
//#erl -name b@192.168.1.164 -setcookie abc -mnesia dir '"/home/firefly/erlang"' -detached -noshell
int main(int argc, char **argv) {
    std::cout << "Hello, World!" << std::endl;
    ENABLEGLOG(GET_STR_CONFIG("logPath").c_str());
 
    if (argc <= 1) {
        ERR("argc size is err");
        exit(-1);
    }
 
    string str_json(argv[1]);
//    cout << str_json << endl;
//    string str_json = "{\"path\":\"/opt/erlang/sub1/\",\"nodeName\":\"sub1@192.168.1.186\",\"cookie\":\"abc\",\"tableName\":\"test\"}";
 
    Json::Value t_json;
    Json::Reader reader;
 
    if (!reader.parse(str_json, t_json)) {
        ERR("json format error :: " << str_json);
        return -1;
    }
 
    int loop = 0;
 
    std::string path = t_json["path"].asString();//"/opt/erlang/za/";
    std::string nodeName = t_json["nodeName"].asString();//"za@192.168.188.128";
    std::string cookie = t_json["cookie"].asString();//"abc";
    std::string tableName = t_json["tableName"].asString();
 
//    SemTool semTool(t_json["key"].asInt());
//
//    semTool.semaphore_p();
 
 
    /***
     * jiazai renlian
     */
//    struct timeval t1, t2;
//    gettimeofday(&t1, NULL);
 
 
    std::string str_node("testNode");
    str_node.append(to_string((int) getpid()));
    ErlangTool::ErlangDbTool erlangDbTool(nodeName, cookie, str_node);
    while (1) {
        /***
     * load fea
     */
 
        auto cache = erlangDbTool.loadFaceFeaData(tableName);
 
//        gettimeofday(&t2, NULL);
        //那么函数f运行所花的时间为
//        auto deltaT = (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec;
//        DBG(" loadData " << deltaT << "us")
//        DBG(tableName << "size is " << cache.size() << " " << loop);
        if (cache.size() <= 0 && loop++ <= 10) {
            usleep(100);
            continue;
        }
        auto size = sizeof(cache[0]) * cache.size();
        try {
            BISTL::shared_memory_object::remove(tableName.c_str());
            BISTL::BiMapFeaData biMapFeaData(tableName, size);
            for (auto &item :cache) {
                biMapFeaData.saveKeyOrValue(item.second.id, item.second.feature, item.second.img, item.second.idcard,
                                            item.second.create_by, item.second.create_time, item.second.update_time);
            }
            auto mymap = biMapFeaData.getMap();
//        for (auto it = mymap->begin(); it != mymap->end(); it++) {
//            std::cout << "feature " << it->second.m_feature << std::endl;
//        }
            DBG("load ok " << tableName << "mymap size is " << mymap->size());
        }
        catch (const std::exception &e) {
            printf("Exception:%s\n", e.what());
            BISTL::shared_memory_object::remove(tableName.c_str());
        }
        break;
    }
 
    return 0;
}