提交 d3db387d authored 作者: blu's avatar blu

init

上级 28260e1d
......@@ -17,19 +17,19 @@ all: evmgr evpuller evpusher evslicer evmlmotion
sqlite3.o: vendor/sqlite/sqlite3.c
gcc -D SQLITE_THREADSAFE=1 -c vendor/sqlite/sqlite3.c
evmgr: evmgr.cpp database.cpp inc/common.hpp inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE)
evmgr: evmgr.cpp database.cpp inc/common.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE)
$(CPP) $(CPPFLAGS) -o evmgr evmgr.cpp $(SQLITE) database.cpp $(HEADERS) $(LIBFFMPEG) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evpuller: evpuller.cpp database.cpp inc/common.hpp inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE)
evpuller: evpuller.cpp database.cpp inc/common.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp $(SQLITE)
$(CPP) $(CPPFLAGS) -o evpuller evpuller.cpp $(SQLITE) database.cpp $(HEADERS) $(LIBFFMPEG) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evpusher: evpusher.cpp inc/common.hpp inc/tinythread.hpp inc/zmqhelper.hpp database.cpp $(SQLITE)
evpusher: evpusher.cpp inc/common.hpp inc/tinythread.hpp inc/database.h inc/zmqhelper.hpp database.cpp $(SQLITE)
$(CPP) $(CPPFLAGS) -o evpusher evpusher.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evslicer: evslicer.cpp inc/common.hpp inc/tinythread.hpp inc/zmqhelper.hpp database.cpp $(SQLITE)
evslicer: evslicer.cpp inc/common.hpp inc/tinythread.hpp inc/database.h inc/zmqhelper.hpp database.cpp $(SQLITE)
$(CPP) $(CPPFLAGS) -o evslicer evslicer.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
evmlmotion: evmlmotion.cpp inc/common.hpp inc/avcvhelpers.hpp inc/zmqhelper.hpp inc/tinythread.hpp database.cpp $(SQLITE)
evmlmotion: evmlmotion.cpp inc/common.hpp inc/avcvhelpers.hpp inc/database.h inc/zmqhelper.hpp inc/tinythread.hpp database.cpp $(SQLITE)
$(CPP) $(CPPFLAGS) -o evmlmotion evmlmotion.cpp database.cpp $(SQLITE) $(LIBFFMPEG) $(HEADERS) $(LIBOPENCV) `pkg-config --cflags --libs vendor/lib/pkgconfig/libzmq.pc` $(LIBS)
rtspr: rtsp-relay.cpp
......
#include "inc/database.h"
//#include "inc/json.hpp"
#include "spdlog/spdlog.h"
#include <cstdlib>
#include <mutex>
#include <map>
using namespace leveldb;
namespace LVDB {
map<string, DB*> mappDB;
DB *_getDB(string fileName) {
DB *pdb = NULL;
int ret = 0;
if(mappDB.count(fileName) == 0) {
//
string mk = string("mkdir -p ") + LVDB_PATH;
ret = system(mk.c_str());
if(ret == -1) {
spdlog::error("failed to create db path: {}", LVDB_PATH);
}else{
Options options;
options.create_if_missing = true;
Status s = DB::Open(options, fileName, &pdb);
if(!s.ok()) {
spdlog::error("failed to open db {}: {}", fileName, s.ToString());
}
mappDB[fileName] = pdb;
}
}else{
pdb = mappDB[fileName];
}
assert(pdb != NULL);
return pdb;
}
int clearDB(string fileName) {
return 0;
}
typedef int (*cb_verify)(json &);
int getValue(json &value, string key, string fileName, cb_verify cb) {
DB* pdb = _getDB(fileName);
string oldVal;
int ret = 0;
json j;
Status s = pdb->Get(leveldb::ReadOptions(), key, &oldVal);
if(!s.ok()) {
spdlog::error("failed to get {} from {}: {}",key, fileName, s.ToString());
return -1;
}
try{
j = json::parse(oldVal);
if(cb != NULL) {
ret = cb(j);
if(ret < 0) {
return ret;
}
}
value = j;
}catch(exception &e) {
spdlog::error("failed to parse {} -> {} {}: {}", key, oldVal, fileName, e.what());
return -2;
}
return 0;
}
int setValue(json &value, string key, string fileName, cb_verify cb) {
int ret = 0;
if(cb != NULL) {
ret = cb(value);
if(ret < 0) {
return ret;
}
}
DB* pdb = _getDB(fileName);
string oldVal;
Status s = pdb->Get(ReadOptions(), key, &oldVal);
if(!s.ok()) {
spdlog::warn("get old {} error {}:{}", key, fileName, s.ToString());
}
s = pdb->Put(leveldb::WriteOptions(), key, value.dump());
if(!s.ok()) {
spdlog::error("failed to put {} -> {}: {}", key, value.dump(), s.ToString());
return -2;
}
if(!oldVal.empty()) {
s = pdb->Put(leveldb::WriteOptions(), key+LVDB_KEY_SUFFIX_BACK, oldVal);
if(!s.ok()) {
spdlog::error("failed to put backup {} -> {}: {}", key, oldVal, s.ToString());
return -2;
}
}
return 0;
}
int delValue(string key, string fileName) {
DB* pdb = _getDB(fileName);
Status s = pdb->Delete(WriteOptions(), key);
if(!s.ok()) {
spdlog::error("failed to delete key {}: {} in {}",s.ToString(), key, fileName);
return -1;
}
return 0;
}
// sn
// {"sn":string, "updatetime": string, "lastboot": string}
int _validateSn(json &info) {
if(info.count("sn") == 0||info.count("updatetime") == 0||info.count("lastboot") == 0) {
spdlog::error("invalid sn config:{}", info.dump());
return -1;
}
return 0;
}
int getSn(json &info, string fileName){
return getValue(info, LVDB_KEY_SN, fileName, _validateSn);
};
int setSn(json &info, string fileName){
return setValue(info, LVDB_KEY_SN, fileName, _validateSn);
};
// config
int _validateConfig(json &config) {
if(config.count("data") == 0|| config["data"].size() == 0) {
spdlog::error("invliad config: {}", config.dump());
return -1;
}
return 0;
}
int getLocalConfig(json &config, string fileName){
return getValue(config, LVDB_KEY_CONFIG, fileName, _validateConfig);
};
int setLocalConfig(json &config, string fileName){
return setValue(config, LVDB_KEY_CONFIG, fileName, _validateConfig);
};
int saveSn(json &info, string fileName){
// slices
int saveSlices(json &slices, string fileName){
return 0;
};
int loadLocalConfig(json &config, string fileName){
int loadSlices(json &slices, string fileName){
return 0;
};
// log
int saveLog(json &log, json &writeOptions, string fileName){
return 0;
};
int savelocalConfig(json &config, string fileName){
int readLog(json &log, json &readOptions, string fileName){
return 0;
};
......
......@@ -7,10 +7,36 @@ using namespace nlohmann;
using namespace std;
namespace LVDB {
#define LVDB_PATH "/opt/lvldb/"
// sn, config
#define LVDB_FILE_GENERAL LVDB_PATH"general.db"
// slices, log
#define LVDB_FILE_LOG LVDB_PATH"log.db"
#define LVDB_KEY_SUFFIX_BACK "_bak"
#define LVDB_KEY_SN "SN"
#define LVDB_KEY_CONFIG "CONFIG"
//
int delValue(string key, string fileName);
// sn, updatetime, boottime
int setSn(json &info, string fileName);
int getSn(json &info, string fileName);
int saveSn(json &info, string fileName);
int loadLocalConfig(json &config, string fileName);
int savelocalConfig(json &config, string fileName);
// cloudutils::config
int getLocalConfig(json &config, string fileName);
int setLocalConfig(json &config, string fileName);
// slices
int getSlices(json &slices, string fileName);
int setSlices(json &slices, string fileName);
// log
int getLog(json &log, json &writeOptions, string fileName);
int setLog(json &log, json &readOptions, string fileName);
}
#endif
\ No newline at end of file
// g++ -std=c++1z test_database.cpp database.cpp -o test_database -lleveldb -Ivendor/include/ -Iinc -Lvendor/lib
#include "inc/database.h"
#include "spdlog/spdlog.h"
int main(){
int ret;
json j, p;
// case table info
spdlog::set_level(spdlog::level::debug);
ret = DB::clearTable("info", "aa.db");
spdlog::info("ret: {}", ret);
ret = DB::clearTable("info", "a.db");
spdlog::info("ret: {}", ret);
ret = DB::setInfo(&p, "a.db");
spdlog::info("ret: {}", ret);
j["sn"] = "sn-aa";
j["lastboot"] = "2019-04-25 10:10:10";
const char *_config = "{\"time\":0,\"code\":0,\"data\":{\"ILSEVMGR1\":{\"sn\":\"ILSEVMGR1\",\"addr\":\"172.31.0.76\",\"addr-cloud\":\"172.31.0.76\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"FWBWTU\",\"status\":1,\"modules\":{\"evpuller\":[{\"sn\":\"ILSEVPULLER1\",\"addr\":\"172.31.0.76\",\"iid\":1,\"port-pub\":5556,\"status\":1}],\"evpusher\":[{\"sn\":\"ILSEVPUSHER1\",\"iid\":1,\"urlDest\":\"rtsp://40.73.41.176:554/test1\",\"user\":\"\",\"password\":\"\",\"token\":\"\",\"enabled\":1,\"status\":1}],\"evslicer\":[{\"sn\":\"ILSEVSLICER1\",\"iid\":1,\"path\":\"slices\",\"enabled\":1,\"status\":1}],\"evml\":[{\"type\":\"motion\",\"sn\":\"ILSEVMLMOTION1\",\"iid\":1,\"enabled\":1,\"status\":1}]}}]}}}";
ret = DB::setInfo(&j, "a.db");
spdlog::info("ret: {}", ret);
ret = DB::getInfo(&j, -1, "a.db");
int main(){
json j;
int ret = 0;
// sn
ret = LVDB::delValue(LVDB_KEY_SN, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret);
j["sn"] = "sn-aa";
j["lastboot"] = "2019-04-25 10:10:17";
ret = DB::setInfo(&j, "a.db");
ret = LVDB::setSn(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret);
ret = DB::getInfo(&j, -1, "a.db");
j["sn"] = "snaaaa";
j["lastboot"] = "2019-08-25 10:10:10";
j["updatetime"] = "2019-08-25 10:10:11";
ret = LVDB::setSn(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret);
j.clear();
j["sn"] = "sn-ab";
j["lastboot"] = "2019-04-25 10:10:17";
ret = LVDB::getSn(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump());
ret = DB::setInfo(&j, "a.db");
spdlog::info("ret: {}", ret);
//
json config = json::parse(_config);
ret = DB::getInfo(&p, -1, "a.db");
j.clear();
ret = LVDB::delValue(LVDB_KEY_CONFIG, LVDB_FILE_GENERAL);
spdlog::info("ret: {}", ret);
ret = LVDB::getLocalConfig(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump());
p.clear();
ret = DB::getInfo(&p, 0, "a.db");
spdlog::info("ret: {}", ret);
ret= LVDB::setLocalConfig(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump());
p.clear();
ret = DB::getInfo(&p, 1, "a.db");
spdlog::info("ret: {}", ret);
ret= LVDB::setLocalConfig(config, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, config.dump());
ret = LVDB::getLocalConfig(j, LVDB_FILE_GENERAL);
spdlog::info("ret: {}, {}", ret, j.dump());
// case table modules
return 0;
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论