2012-08-06 17:00:38 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* libretroshare/src/gxs: rsdataservice.cc
|
|
|
|
*
|
|
|
|
* Data Access, interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2011-2011 by Evi-Parker Christopher
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
#include <fstream>
|
2013-03-20 18:26:24 -04:00
|
|
|
#include <util/rsdir.h>
|
|
|
|
#include <algorithm>
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
#include "rsdataservice.h"
|
|
|
|
|
|
|
|
#define MSG_TABLE_NAME std::string("MESSAGES")
|
|
|
|
#define GRP_TABLE_NAME std::string("GROUPS")
|
|
|
|
|
2013-11-23 18:39:55 -05:00
|
|
|
#define GRP_LAST_POST_UPDATE_TRIGGER std::string("LAST_POST_UPDATE")
|
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
// generic
|
2012-06-03 14:09:38 -04:00
|
|
|
#define KEY_NXS_FILE std::string("nxsFile")
|
2012-05-21 18:07:43 -04:00
|
|
|
#define KEY_NXS_FILE_OFFSET std::string("fileOffset")
|
2012-06-03 14:09:38 -04:00
|
|
|
#define KEY_NXS_FILE_LEN std::string("nxsFileLen")
|
2012-05-21 18:07:43 -04:00
|
|
|
#define KEY_NXS_IDENTITY std::string("identity")
|
|
|
|
#define KEY_GRP_ID std::string("grpId")
|
2012-07-05 17:26:14 -04:00
|
|
|
#define KEY_ORIG_GRP_ID std::string("origGrpId")
|
2014-01-19 18:38:48 -05:00
|
|
|
#define KEY_PARENT_GRP_ID std::string("parentGrpId")
|
2012-10-01 16:57:56 -04:00
|
|
|
#define KEY_SIGN_SET std::string("signSet")
|
2012-05-21 18:07:43 -04:00
|
|
|
#define KEY_TIME_STAMP std::string("timeStamp")
|
|
|
|
#define KEY_NXS_FLAGS std::string("flags")
|
2012-06-26 15:52:01 -04:00
|
|
|
#define KEY_NXS_META std::string("meta")
|
2012-08-19 18:15:37 -04:00
|
|
|
#define KEY_NXS_SERV_STRING std::string("serv_str")
|
2013-06-04 17:00:43 -04:00
|
|
|
#define KEY_NXS_HASH std::string("hash")
|
2013-11-23 18:39:55 -05:00
|
|
|
#define KEY_RECV_TS std::string("recv_time_stamp")
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
// grp table columns
|
2012-05-24 18:38:59 -04:00
|
|
|
#define KEY_KEY_SET std::string("keySet")
|
2012-06-26 15:52:01 -04:00
|
|
|
#define KEY_GRP_NAME std::string("grpName")
|
2012-08-26 10:02:47 -04:00
|
|
|
#define KEY_GRP_SIGN_FLAGS std::string("signFlags")
|
2012-12-02 14:40:17 -05:00
|
|
|
#define KEY_GRP_CIRCLE_ID std::string("circleId")
|
|
|
|
#define KEY_GRP_CIRCLE_TYPE std::string("circleType")
|
|
|
|
#define KEY_GRP_INTERNAL_CIRCLE std::string("internalCircle")
|
|
|
|
#define KEY_GRP_ORIGINATOR std::string("originator")
|
2013-06-04 17:00:43 -04:00
|
|
|
#define KEY_GRP_AUTHEN_FLAGS std::string("authenFlags")
|
2012-05-24 18:38:59 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// grp local
|
|
|
|
#define KEY_GRP_SUBCR_FLAG std::string("subscribeFlag")
|
|
|
|
#define KEY_GRP_POP std::string("popularity")
|
|
|
|
#define KEY_MSG_COUNT std::string("msgCount")
|
|
|
|
#define KEY_GRP_STATUS std::string("grpStatus")
|
|
|
|
#define KEY_GRP_LAST_POST std::string("lastPost")
|
2014-02-22 19:08:11 -05:00
|
|
|
#define KEY_GRP_REP_CUTOFF std::string("rep_cutoff")
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
// msg table columns
|
|
|
|
#define KEY_MSG_ID std::string("msgId")
|
2012-06-11 17:56:23 -04:00
|
|
|
#define KEY_ORIG_MSG_ID std::string("origMsgId")
|
2012-06-26 15:52:01 -04:00
|
|
|
#define KEY_MSG_PARENT_ID std::string("parentId")
|
|
|
|
#define KEY_MSG_THREAD_ID std::string("threadId")
|
|
|
|
#define KEY_MSG_NAME std::string("msgName")
|
|
|
|
|
|
|
|
// msg local
|
|
|
|
#define KEY_MSG_STATUS std::string("msgStatus")
|
|
|
|
#define KEY_CHILD_TS std::string("childTs")
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
/*** actual data col numbers ***/
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// generic
|
|
|
|
#define COL_ACT_GROUP_ID 0
|
|
|
|
#define COL_NXS_FILE 1
|
|
|
|
#define COL_NXS_FILE_OFFSET 2
|
|
|
|
#define COL_NXS_FILE_LEN 3
|
|
|
|
#define COL_META_DATA 4
|
|
|
|
#define COL_ACT_MSG_ID 5
|
|
|
|
|
|
|
|
/*** meta column numbers ***/
|
|
|
|
|
|
|
|
// grp col numbers
|
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
#define COL_KEY_SET 6
|
|
|
|
#define COL_GRP_SUBCR_FLAG 7
|
|
|
|
#define COL_GRP_POP 8
|
|
|
|
#define COL_MSG_COUNT 9
|
|
|
|
#define COL_GRP_STATUS 10
|
|
|
|
#define COL_GRP_NAME 11
|
|
|
|
#define COL_GRP_LAST_POST 12
|
|
|
|
#define COL_ORIG_GRP_ID 13
|
|
|
|
#define COL_GRP_SERV_STRING 14
|
|
|
|
#define COL_GRP_SIGN_FLAGS 15
|
|
|
|
#define COL_GRP_CIRCLE_ID 16
|
|
|
|
#define COL_GRP_CIRCL_TYPE 17
|
|
|
|
#define COL_GRP_INTERN_CIRCLE 18
|
|
|
|
#define COL_GRP_ORIGINATOR 19
|
|
|
|
#define COL_GRP_AUTHEN_FLAGS 20
|
2014-01-19 18:38:48 -05:00
|
|
|
#define COL_PARENT_GRP_ID 21
|
|
|
|
#define COL_GRP_RECV_TS 22
|
2014-02-22 19:08:11 -05:00
|
|
|
#define COL_GRP_REP_CUTOFF 23
|
2012-12-02 14:40:17 -05:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
// msg col numbers
|
2013-06-04 17:00:43 -04:00
|
|
|
#define COL_MSG_ID 6
|
|
|
|
#define COL_ORIG_MSG_ID 7
|
|
|
|
#define COL_MSG_STATUS 8
|
|
|
|
#define COL_CHILD_TS 9
|
|
|
|
#define COL_PARENT_ID 10
|
|
|
|
#define COL_THREAD_ID 11
|
|
|
|
#define COL_MSG_NAME 12
|
|
|
|
#define COL_MSG_SERV_STRING 13
|
2013-11-23 18:39:55 -05:00
|
|
|
#define COL_MSG_RECV_TS 14
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
// generic meta shared col numbers
|
|
|
|
#define COL_GRP_ID 0
|
|
|
|
#define COL_TIME_STAMP 1
|
|
|
|
#define COL_NXS_FLAGS 2
|
2012-10-01 16:57:56 -04:00
|
|
|
#define COL_SIGN_SET 3
|
2012-06-26 15:52:01 -04:00
|
|
|
#define COL_IDENTITY 4
|
2013-06-04 17:00:43 -04:00
|
|
|
#define COL_HASH 5
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2014-04-27 09:14:07 -04:00
|
|
|
/*****
|
|
|
|
* #define RS_DATA_SERVICE_DEBUG 1
|
|
|
|
****/
|
2012-06-05 10:46:18 -04:00
|
|
|
|
2012-08-21 17:32:07 -04:00
|
|
|
const std::string RsGeneralDataService::GRP_META_SERV_STRING = KEY_NXS_SERV_STRING;
|
|
|
|
const std::string RsGeneralDataService::GRP_META_STATUS = KEY_GRP_STATUS;
|
|
|
|
const std::string RsGeneralDataService::GRP_META_SUBSCRIBE_FLAG = KEY_GRP_SUBCR_FLAG;
|
2014-02-22 19:08:11 -05:00
|
|
|
const std::string RsGeneralDataService::GRP_META_CUTOFF_LEVEL = KEY_GRP_REP_CUTOFF;
|
2012-08-21 17:32:07 -04:00
|
|
|
|
|
|
|
const std::string RsGeneralDataService::MSG_META_SERV_STRING = KEY_NXS_SERV_STRING;
|
|
|
|
const std::string RsGeneralDataService::MSG_META_STATUS = KEY_MSG_STATUS;
|
|
|
|
|
2013-02-07 18:04:16 -05:00
|
|
|
const uint32_t RsGeneralDataService::GXS_MAX_ITEM_SIZE = 1572864; // 1.5 Mbytes
|
2012-08-21 17:32:07 -04:00
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
RsDataService::RsDataService(const std::string &serviceDir, const std::string &dbName, uint16_t serviceType,
|
2014-03-23 08:13:44 -04:00
|
|
|
RsGxsSearchModule * /* mod */, const std::string& key)
|
|
|
|
: RsGeneralDataService(), mDbMutex("RsDataService"), mServiceDir(serviceDir), mDbName(mServiceDir + "/" + dbName), mServType(serviceType),
|
|
|
|
mDb( new RetroDb(mDbName, RetroDb::OPEN_READWRITE_CREATE, key)) {
|
2012-06-05 10:46:18 -04:00
|
|
|
|
|
|
|
initialise();
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// for retrieving msg meta
|
|
|
|
msgMetaColumns.push_back(KEY_GRP_ID); msgMetaColumns.push_back(KEY_TIME_STAMP); msgMetaColumns.push_back(KEY_NXS_FLAGS);
|
2013-06-04 17:00:43 -04:00
|
|
|
msgMetaColumns.push_back(KEY_SIGN_SET); msgMetaColumns.push_back(KEY_NXS_IDENTITY); msgMetaColumns.push_back(KEY_NXS_HASH);
|
2012-06-26 15:52:01 -04:00
|
|
|
msgMetaColumns.push_back(KEY_MSG_ID); msgMetaColumns.push_back(KEY_ORIG_MSG_ID); msgMetaColumns.push_back(KEY_MSG_STATUS);
|
|
|
|
msgMetaColumns.push_back(KEY_CHILD_TS); msgMetaColumns.push_back(KEY_MSG_PARENT_ID); msgMetaColumns.push_back(KEY_MSG_THREAD_ID);
|
2013-11-23 18:39:55 -05:00
|
|
|
msgMetaColumns.push_back(KEY_MSG_NAME); msgMetaColumns.push_back(KEY_NXS_SERV_STRING); msgMetaColumns.push_back(KEY_RECV_TS);
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
// for retrieving actual data
|
|
|
|
msgColumns.push_back(KEY_GRP_ID); msgColumns.push_back(KEY_NXS_FILE); msgColumns.push_back(KEY_NXS_FILE_OFFSET);
|
|
|
|
msgColumns.push_back(KEY_NXS_FILE_LEN); msgColumns.push_back(KEY_NXS_META); msgColumns.push_back(KEY_MSG_ID);
|
|
|
|
|
|
|
|
// for retrieving grp meta data
|
|
|
|
grpMetaColumns.push_back(KEY_GRP_ID); grpMetaColumns.push_back(KEY_TIME_STAMP); grpMetaColumns.push_back(KEY_NXS_FLAGS);
|
2013-06-04 17:00:43 -04:00
|
|
|
grpMetaColumns.push_back(KEY_SIGN_SET); grpMetaColumns.push_back(KEY_NXS_IDENTITY); grpMetaColumns.push_back(KEY_NXS_HASH);
|
2012-06-26 15:52:01 -04:00
|
|
|
grpMetaColumns.push_back(KEY_KEY_SET); grpMetaColumns.push_back(KEY_GRP_SUBCR_FLAG); grpMetaColumns.push_back(KEY_GRP_POP);
|
|
|
|
grpMetaColumns.push_back(KEY_MSG_COUNT); grpMetaColumns.push_back(KEY_GRP_STATUS); grpMetaColumns.push_back(KEY_GRP_NAME);
|
2012-08-19 18:15:37 -04:00
|
|
|
grpMetaColumns.push_back(KEY_GRP_LAST_POST); grpMetaColumns.push_back(KEY_ORIG_GRP_ID); grpMetaColumns.push_back(KEY_NXS_SERV_STRING);
|
2012-12-02 14:40:17 -05:00
|
|
|
grpMetaColumns.push_back(KEY_GRP_SIGN_FLAGS); grpMetaColumns.push_back(KEY_GRP_CIRCLE_ID); grpMetaColumns.push_back(KEY_GRP_CIRCLE_TYPE);
|
|
|
|
grpMetaColumns.push_back(KEY_GRP_INTERNAL_CIRCLE); grpMetaColumns.push_back(KEY_GRP_ORIGINATOR);
|
2014-02-22 19:08:11 -05:00
|
|
|
grpMetaColumns.push_back(KEY_GRP_AUTHEN_FLAGS); grpMetaColumns.push_back(KEY_PARENT_GRP_ID); grpMetaColumns.push_back(KEY_RECV_TS);
|
|
|
|
grpMetaColumns.push_back(KEY_GRP_REP_CUTOFF);
|
2014-01-19 18:38:48 -05:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
// for retrieving actual grp data
|
|
|
|
grpColumns.push_back(KEY_GRP_ID); grpColumns.push_back(KEY_NXS_FILE); grpColumns.push_back(KEY_NXS_FILE_OFFSET);
|
|
|
|
grpColumns.push_back(KEY_NXS_FILE_LEN); grpColumns.push_back(KEY_NXS_META);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
// for retrieving msg offsets
|
|
|
|
mMsgOffSetColumns.push_back(KEY_MSG_ID); mMsgOffSetColumns.push_back(KEY_NXS_FILE_OFFSET);
|
|
|
|
mMsgOffSetColumns.push_back(KEY_NXS_FILE_LEN);
|
2013-09-11 16:42:56 -04:00
|
|
|
|
|
|
|
grpIdColumn.push_back(KEY_GRP_ID);
|
2014-04-26 06:07:25 -04:00
|
|
|
|
|
|
|
mMsgIdColumn.push_back(KEY_MSG_ID);
|
2012-06-05 10:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RsDataService::~RsDataService(){
|
2014-04-27 09:14:07 -04:00
|
|
|
|
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "RsDataService::~RsDataService()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2012-06-05 10:46:18 -04:00
|
|
|
mDb->closeDb();
|
|
|
|
delete mDb;
|
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-05 10:46:18 -04:00
|
|
|
void RsDataService::initialise(){
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
// initialise database
|
2013-06-04 17:00:43 -04:00
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// create table for msg data
|
2012-07-05 17:26:14 -04:00
|
|
|
mDb->execSQL("CREATE TABLE " + MSG_TABLE_NAME + "(" +
|
2013-02-12 16:45:30 -05:00
|
|
|
KEY_MSG_ID + " TEXT PRIMARY KEY," +
|
2012-07-05 17:26:14 -04:00
|
|
|
KEY_GRP_ID + " TEXT," +
|
|
|
|
KEY_NXS_FLAGS + " INT," +
|
|
|
|
KEY_ORIG_MSG_ID + " TEXT," +
|
|
|
|
KEY_TIME_STAMP + " INT," +
|
|
|
|
KEY_NXS_IDENTITY + " TEXT," +
|
2012-10-01 16:57:56 -04:00
|
|
|
KEY_SIGN_SET + " BLOB," +
|
2012-07-05 17:26:14 -04:00
|
|
|
KEY_NXS_FILE + " TEXT,"+
|
|
|
|
KEY_NXS_FILE_OFFSET + " INT," +
|
|
|
|
KEY_MSG_STATUS + " INT," +
|
|
|
|
KEY_CHILD_TS + " INT," +
|
|
|
|
KEY_NXS_META + " BLOB," +
|
|
|
|
KEY_MSG_THREAD_ID + " TEXT," +
|
|
|
|
KEY_MSG_PARENT_ID + " TEXT,"+
|
|
|
|
KEY_MSG_NAME + " TEXT," +
|
2012-08-19 18:15:37 -04:00
|
|
|
KEY_NXS_SERV_STRING + " TEXT," +
|
2013-06-04 17:00:43 -04:00
|
|
|
KEY_NXS_HASH + " TEXT," +
|
2013-11-23 18:39:55 -05:00
|
|
|
KEY_RECV_TS + " INT," +
|
2012-07-05 17:26:14 -04:00
|
|
|
KEY_NXS_FILE_LEN + " INT);");
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// create table for grp data
|
2012-07-05 17:26:14 -04:00
|
|
|
mDb->execSQL("CREATE TABLE " + GRP_TABLE_NAME + "(" +
|
2013-02-12 16:45:30 -05:00
|
|
|
KEY_GRP_ID + " TEXT PRIMARY KEY," +
|
2012-07-05 17:26:14 -04:00
|
|
|
KEY_TIME_STAMP + " INT," +
|
|
|
|
KEY_NXS_FILE + " TEXT," +
|
|
|
|
KEY_NXS_FILE_OFFSET + " INT," +
|
|
|
|
KEY_KEY_SET + " BLOB," +
|
|
|
|
KEY_NXS_FILE_LEN + " INT," +
|
|
|
|
KEY_NXS_META + " BLOB," +
|
|
|
|
KEY_GRP_NAME + " TEXT," +
|
|
|
|
KEY_GRP_LAST_POST + " INT," +
|
|
|
|
KEY_GRP_POP + " INT," +
|
|
|
|
KEY_MSG_COUNT + " INT," +
|
|
|
|
KEY_GRP_SUBCR_FLAG + " INT," +
|
|
|
|
KEY_GRP_STATUS + " INT," +
|
|
|
|
KEY_NXS_IDENTITY + " TEXT," +
|
|
|
|
KEY_ORIG_GRP_ID + " TEXT," +
|
2012-08-19 18:15:37 -04:00
|
|
|
KEY_NXS_SERV_STRING + " TEXT," +
|
2012-07-05 17:26:14 -04:00
|
|
|
KEY_NXS_FLAGS + " INT," +
|
2013-06-04 17:00:43 -04:00
|
|
|
KEY_GRP_AUTHEN_FLAGS + " INT," +
|
2012-08-26 10:02:47 -04:00
|
|
|
KEY_GRP_SIGN_FLAGS + " INT," +
|
2012-12-02 14:40:17 -05:00
|
|
|
KEY_GRP_CIRCLE_ID + " TEXT," +
|
|
|
|
KEY_GRP_CIRCLE_TYPE + " INT," +
|
|
|
|
KEY_GRP_INTERNAL_CIRCLE + " TEXT," +
|
|
|
|
KEY_GRP_ORIGINATOR + " TEXT," +
|
2013-06-04 17:00:43 -04:00
|
|
|
KEY_NXS_HASH + " TEXT," +
|
2013-11-23 18:39:55 -05:00
|
|
|
KEY_RECV_TS + " INT," +
|
2014-01-19 18:38:48 -05:00
|
|
|
KEY_PARENT_GRP_ID + " TEXT," +
|
2014-02-22 19:08:11 -05:00
|
|
|
KEY_GRP_REP_CUTOFF + " INT," +
|
2012-10-01 16:57:56 -04:00
|
|
|
KEY_SIGN_SET + " BLOB);");
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2013-11-23 18:39:55 -05:00
|
|
|
mDb->execSQL("CREATE TRIGGER " + GRP_LAST_POST_UPDATE_TRIGGER +
|
2013-11-27 16:54:05 -05:00
|
|
|
" INSERT ON " + MSG_TABLE_NAME +
|
2013-11-24 07:37:17 -05:00
|
|
|
std::string(" BEGIN ") +
|
|
|
|
" UPDATE " + GRP_TABLE_NAME + " SET " + KEY_GRP_LAST_POST + "= new."
|
2013-11-27 16:54:05 -05:00
|
|
|
+ KEY_RECV_TS + " WHERE " + KEY_GRP_ID + "=new." + KEY_GRP_ID + ";"
|
2013-11-23 18:39:55 -05:00
|
|
|
+ std::string("END;"));
|
2012-05-24 18:38:59 -04:00
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2014-04-27 09:14:07 -04:00
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "RsDataService::locked_getGrpMeta()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
RsGxsGrpMetaData* grpMeta = new RsGxsGrpMetaData();
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
// for extracting raw data
|
2012-05-21 18:07:43 -04:00
|
|
|
uint32_t offset = 0;
|
|
|
|
char* data = NULL;
|
|
|
|
uint32_t data_len = 0;
|
|
|
|
|
|
|
|
// grpId
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string tempId;
|
|
|
|
c.getString(COL_GRP_ID, tempId);
|
2014-04-20 12:34:26 -04:00
|
|
|
grpMeta->mGroupId = RsGxsGroupId(tempId);
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_IDENTITY, tempId);
|
2014-04-20 12:34:26 -04:00
|
|
|
grpMeta->mAuthorId = RsGxsId(tempId);
|
2014-04-27 09:14:07 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
c.getString(COL_GRP_NAME, grpMeta->mGroupName);
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_ORIG_GRP_ID, tempId);
|
2014-04-20 12:34:26 -04:00
|
|
|
grpMeta->mOrigGrpId = RsGxsGroupId(tempId);
|
2012-08-19 18:15:37 -04:00
|
|
|
c.getString(COL_GRP_SERV_STRING, grpMeta->mServiceString);
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string temp;
|
|
|
|
c.getString(COL_HASH, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
grpMeta->mHash = RsFileHash(temp);
|
2014-02-22 19:08:11 -05:00
|
|
|
grpMeta->mReputationCutOff = c.getInt32(COL_GRP_REP_CUTOFF);
|
2012-12-02 14:40:17 -05:00
|
|
|
grpMeta->mSignFlags = c.getInt32(COL_GRP_SIGN_FLAGS);
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
grpMeta->mPublishTs = c.getInt32(COL_TIME_STAMP);
|
|
|
|
grpMeta->mGroupFlags = c.getInt32(COL_NXS_FLAGS);
|
2014-07-19 04:00:06 -04:00
|
|
|
grpMeta->mGrpSize = c.getInt32(COL_NXS_FILE_LEN);
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-06-03 14:09:38 -04:00
|
|
|
offset = 0;
|
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
offset = 0; data = NULL; data_len = 0;
|
|
|
|
data = (char*)c.getData(COL_KEY_SET, data_len);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
if(data)
|
|
|
|
ok &= grpMeta->keys.GetTlv(data, data_len, &offset);
|
2014-04-05 11:21:22 -04:00
|
|
|
else
|
|
|
|
grpMeta->keys.TlvClear() ;
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// local meta
|
|
|
|
grpMeta->mSubscribeFlags = c.getInt32(COL_GRP_SUBCR_FLAG);
|
|
|
|
grpMeta->mPop = c.getInt32(COL_GRP_POP);
|
|
|
|
grpMeta->mMsgCount = c.getInt32(COL_MSG_COUNT);
|
|
|
|
grpMeta->mLastPost = c.getInt32(COL_GRP_LAST_POST);
|
|
|
|
grpMeta->mGroupStatus = c.getInt32(COL_GRP_STATUS);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_GRP_CIRCLE_ID, tempId);
|
2014-04-20 12:34:26 -04:00
|
|
|
grpMeta->mCircleId = RsGxsCircleId(tempId);
|
2012-12-02 14:40:17 -05:00
|
|
|
grpMeta->mCircleType = c.getInt32(COL_GRP_CIRCL_TYPE);
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_GRP_INTERN_CIRCLE, tempId);
|
2014-04-20 12:34:26 -04:00
|
|
|
grpMeta->mInternalCircle = RsGxsCircleId(tempId);
|
2014-03-17 16:56:06 -04:00
|
|
|
|
|
|
|
std::string s ; c.getString(COL_GRP_ORIGINATOR, s) ;
|
|
|
|
grpMeta->mOriginator = RsPeerId(s);
|
2013-06-04 17:00:43 -04:00
|
|
|
grpMeta->mAuthenFlags = c.getInt32(COL_GRP_AUTHEN_FLAGS);
|
2013-11-23 18:39:55 -05:00
|
|
|
grpMeta->mRecvTS = c.getInt32(COL_GRP_RECV_TS);
|
2012-12-02 14:40:17 -05:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_PARENT_GRP_ID, tempId);
|
2014-04-20 12:34:26 -04:00
|
|
|
grpMeta->mParentGrpId = RsGxsGroupId(tempId);
|
2014-01-19 18:38:48 -05:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
if(ok)
|
|
|
|
return grpMeta;
|
|
|
|
else
|
|
|
|
delete grpMeta;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
RsNxsGrp* RsDataService::locked_getGroup(RetroCursor &c)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
|
|
|
/*!
|
|
|
|
* grpId, pub admin and pub publish key
|
|
|
|
* necessary for successful group
|
|
|
|
*/
|
|
|
|
RsNxsGrp* grp = new RsNxsGrp(mServType);
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
// for manipulating raw data
|
|
|
|
uint32_t offset = 0;
|
|
|
|
char* data = NULL;
|
|
|
|
uint32_t data_len = 0;
|
|
|
|
|
|
|
|
// grpId
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getStringT<RsGxsGroupId>(COL_ACT_GROUP_ID, grp->grpId);
|
|
|
|
ok &= !grp->grpId.isNull();
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
offset = 0; data_len = 0;
|
|
|
|
if(ok){
|
|
|
|
|
|
|
|
data = (char*)c.getData(COL_META_DATA, data_len);
|
|
|
|
if(data)
|
|
|
|
grp->meta.GetTlv(data, data_len, &offset);
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
/* now retrieve grp data from file */
|
2012-05-21 18:07:43 -04:00
|
|
|
std::string grpFile;
|
|
|
|
c.getString(COL_NXS_FILE, grpFile);
|
|
|
|
ok &= !grpFile.empty();
|
|
|
|
|
|
|
|
if(ok){
|
2012-06-03 14:09:38 -04:00
|
|
|
|
|
|
|
data_len = c.getInt32(COL_NXS_FILE_LEN);
|
2012-05-21 18:07:43 -04:00
|
|
|
offset = c.getInt32(COL_NXS_FILE_OFFSET);
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
char grp_data[data_len];
|
2012-06-03 14:09:38 -04:00
|
|
|
std::ifstream istrm(grpFile.c_str(), std::ios::binary);
|
|
|
|
istrm.seekg(offset, std::ios::beg);
|
2012-05-21 18:07:43 -04:00
|
|
|
istrm.read(grp_data, data_len);
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
istrm.close();
|
|
|
|
offset = 0;
|
2012-06-03 14:09:38 -04:00
|
|
|
ok &= grp->grp.GetTlv(grp_data, data_len, &offset);
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
return grp;
|
|
|
|
else
|
|
|
|
delete grp;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
RsGxsMsgMetaData* RsDataService::locked_getMsgMeta(RetroCursor &c)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
RsGxsMsgMetaData* msgMeta = new RsGxsMsgMetaData();
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
uint32_t data_len = 0,
|
|
|
|
offset = 0;
|
|
|
|
char* data = NULL;
|
|
|
|
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string gId;
|
|
|
|
c.getString(COL_GRP_ID, gId);
|
|
|
|
msgMeta->mGroupId = RsGxsGroupId(gId);
|
|
|
|
std::string temp;
|
|
|
|
c.getString(COL_MSG_ID, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msgMeta->mMsgId = RsGxsMessageId(temp);
|
2012-06-26 15:52:01 -04:00
|
|
|
// without these, a msg is meaningless
|
2014-03-17 16:56:06 -04:00
|
|
|
ok &= (!msgMeta->mGroupId.isNull()) && (!msgMeta->mMsgId.isNull());
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_ORIG_MSG_ID, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msgMeta->mOrigMsgId = RsGxsMessageId(temp);
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_IDENTITY, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msgMeta->mAuthorId = RsGxsId(temp);
|
2012-06-26 15:52:01 -04:00
|
|
|
c.getString(COL_MSG_NAME, msgMeta->mMsgName);
|
2012-08-19 18:15:37 -04:00
|
|
|
c.getString(COL_MSG_SERV_STRING, msgMeta->mServiceString);
|
2014-03-17 16:56:06 -04:00
|
|
|
|
|
|
|
c.getString(COL_HASH, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msgMeta->mHash = RsFileHash(temp);
|
2013-11-23 18:39:55 -05:00
|
|
|
msgMeta->recvTS = c.getInt32(COL_MSG_RECV_TS);
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-10-01 16:57:56 -04:00
|
|
|
offset = 0;
|
|
|
|
data = (char*)c.getData(COL_SIGN_SET, data_len);
|
|
|
|
msgMeta->signSet.GetTlv(data, data_len, &offset);
|
2014-07-19 04:00:06 -04:00
|
|
|
msgMeta->mMsgSize = c.getInt32(COL_NXS_FILE_LEN);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
msgMeta->mMsgFlags = c.getInt32(COL_NXS_FLAGS);
|
|
|
|
msgMeta->mPublishTs = c.getInt32(COL_TIME_STAMP);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
offset = 0; data_len = 0;
|
2012-07-05 17:26:14 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// thread and parent id
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_THREAD_ID, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msgMeta->mThreadId = RsGxsMessageId(temp);
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getString(COL_PARENT_ID, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msgMeta->mParentId = RsGxsMessageId(temp);
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
// local meta
|
|
|
|
msgMeta->mMsgStatus = c.getInt32(COL_MSG_STATUS);
|
|
|
|
msgMeta->mChildTs = c.getInt32(COL_CHILD_TS);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
return msgMeta;
|
|
|
|
else
|
|
|
|
delete msgMeta;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
RsNxsMsg* RsDataService::locked_getMessage(RetroCursor &c)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
RsNxsMsg* msg = new RsNxsMsg(mServType);
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
uint32_t data_len = 0,
|
|
|
|
offset = 0;
|
|
|
|
char* data = NULL;
|
2014-03-17 16:56:06 -04:00
|
|
|
c.getStringT<RsGxsGroupId>(COL_ACT_GROUP_ID, msg->grpId);
|
|
|
|
std::string temp;
|
|
|
|
c.getString(COL_ACT_MSG_ID, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msg->msgId = RsGxsMessageId(temp);
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
ok &= (!msg->grpId.isNull()) && (!msg->msgId.isNull());
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
offset = 0; data_len = 0;
|
|
|
|
if(ok){
|
|
|
|
|
|
|
|
data = (char*)c.getData(COL_META_DATA, data_len);
|
|
|
|
if(data)
|
|
|
|
msg->meta.GetTlv(data, data_len, &offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now retrieve grp data from file */
|
2012-05-24 18:38:59 -04:00
|
|
|
std::string msgFile;
|
|
|
|
c.getString(COL_NXS_FILE, msgFile);
|
2012-06-03 14:09:38 -04:00
|
|
|
offset = c.getInt32(COL_NXS_FILE_OFFSET);
|
|
|
|
data_len = c.getInt32(COL_NXS_FILE_LEN);
|
2012-05-21 18:07:43 -04:00
|
|
|
ok &= !msgFile.empty();
|
|
|
|
|
|
|
|
if(ok){
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2012-10-23 17:52:51 -04:00
|
|
|
char* msg_data = new char[data_len];
|
2012-06-03 14:09:38 -04:00
|
|
|
std::ifstream istrm(msgFile.c_str(), std::ios::binary);
|
2012-05-21 18:07:43 -04:00
|
|
|
istrm.seekg(offset, std::ios::beg);
|
2012-06-03 14:09:38 -04:00
|
|
|
istrm.read(msg_data, data_len);
|
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
istrm.close();
|
|
|
|
offset = 0;
|
2012-06-03 14:09:38 -04:00
|
|
|
ok &= msg->msg.GetTlv(msg_data, data_len, &offset);
|
2012-10-23 17:52:51 -04:00
|
|
|
delete[] msg_data;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
return msg;
|
|
|
|
else
|
|
|
|
delete msg;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
int RsDataService::storeMessage(std::map<RsNxsMsg *, RsGxsMsgMetaData *> &msg)
|
|
|
|
{
|
2012-10-15 14:52:47 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
std::map<RsNxsMsg*, RsGxsMsgMetaData* >::iterator mit = msg.begin();
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// start a transaction
|
2012-05-21 18:07:43 -04:00
|
|
|
mDb->execSQL("BEGIN;");
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
for(; mit != msg.end(); mit++){
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
RsNxsMsg* msgPtr = mit->first;
|
|
|
|
RsGxsMsgMetaData* msgMetaPtr = mit->second;
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2014-05-04 08:48:42 -04:00
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "RsDataService::storeMessage() ";
|
|
|
|
std::cerr << " GroupId: " << msgMetaPtr->mGroupId.toStdString();
|
|
|
|
std::cerr << " MessageId: " << msgMetaPtr->mMsgId.toStdString();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2013-02-07 18:04:16 -05:00
|
|
|
// skip msg item if size if greater than
|
2014-05-04 08:48:42 -04:00
|
|
|
if(!validSize(msgPtr))
|
|
|
|
{
|
|
|
|
std::cerr << "RsDataService::storeMessage() ERROR invalid size";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-07 18:04:16 -05:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// create or access file in binary
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string msgFile = mServiceDir + "/" + msgPtr->grpId.toStdString() + "-msgs";
|
2012-05-21 18:07:43 -04:00
|
|
|
std::fstream ostrm(msgFile.c_str(), std::ios::binary | std::ios::app | std::ios::out);
|
|
|
|
ostrm.seekg(0, std::ios::end); // go to end to append
|
2012-05-24 18:38:59 -04:00
|
|
|
uint32_t offset = ostrm.tellg(); // get fill offset
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
ContentValue cv;
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-06-03 14:09:38 -04:00
|
|
|
cv.put(KEY_NXS_FILE_OFFSET, (int32_t)offset);
|
|
|
|
cv.put(KEY_NXS_FILE, msgFile);
|
|
|
|
cv.put(KEY_NXS_FILE_LEN, (int32_t)msgPtr->msg.TlvSize());
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_MSG_ID, msgMetaPtr->mMsgId.toStdString());
|
|
|
|
cv.put(KEY_GRP_ID, msgMetaPtr->mGroupId.toStdString());
|
2012-08-19 18:15:37 -04:00
|
|
|
cv.put(KEY_NXS_SERV_STRING, msgMetaPtr->mServiceString);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_NXS_HASH, msgMetaPtr->mHash.toStdString());
|
2013-11-23 18:39:55 -05:00
|
|
|
cv.put(KEY_RECV_TS, (int32_t)msgMetaPtr->recvTS);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2012-10-01 16:57:56 -04:00
|
|
|
char signSetData[msgMetaPtr->signSet.TlvSize()];
|
|
|
|
offset = 0;
|
|
|
|
msgMetaPtr->signSet.SetTlv(signSetData, msgMetaPtr->signSet.TlvSize(), &offset);
|
|
|
|
cv.put(KEY_SIGN_SET, msgMetaPtr->signSet.TlvSize(), signSetData);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_NXS_IDENTITY, msgMetaPtr->mAuthorId.toStdString());
|
2012-10-01 16:57:56 -04:00
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
cv.put(KEY_NXS_FLAGS, (int32_t) msgMetaPtr->mMsgFlags);
|
|
|
|
cv.put(KEY_TIME_STAMP, (int32_t) msgMetaPtr->mPublishTs);
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
char metaData[msgPtr->meta.TlvSize()];
|
|
|
|
msgPtr->meta.SetTlv(metaData, msgPtr->meta.TlvSize(), &offset);
|
|
|
|
cv.put(KEY_NXS_META, msgPtr->meta.TlvSize(), metaData);
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_MSG_PARENT_ID, msgMetaPtr->mParentId.toStdString());
|
|
|
|
cv.put(KEY_MSG_THREAD_ID, msgMetaPtr->mThreadId.toStdString());
|
|
|
|
cv.put(KEY_ORIG_MSG_ID, msgMetaPtr->mOrigMsgId.toStdString());
|
2012-06-26 15:52:01 -04:00
|
|
|
cv.put(KEY_MSG_NAME, msgMetaPtr->mMsgName);
|
|
|
|
|
|
|
|
// now local meta
|
|
|
|
cv.put(KEY_MSG_STATUS, (int32_t)msgMetaPtr->mMsgStatus);
|
|
|
|
cv.put(KEY_CHILD_TS, (int32_t)msgMetaPtr->mChildTs);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-03 14:09:38 -04:00
|
|
|
offset = 0;
|
2012-10-13 17:10:42 -04:00
|
|
|
char* msgData = new char[msgPtr->msg.TlvSize()];
|
2012-05-24 18:38:59 -04:00
|
|
|
msgPtr->msg.SetTlv(msgData, msgPtr->msg.TlvSize(), &offset);
|
2012-05-21 18:07:43 -04:00
|
|
|
ostrm.write(msgData, msgPtr->msg.TlvSize());
|
|
|
|
ostrm.close();
|
2012-10-13 17:10:42 -04:00
|
|
|
delete[] msgData;
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2014-05-04 08:48:42 -04:00
|
|
|
if (!mDb->sqlInsert(MSG_TABLE_NAME, "", cv))
|
|
|
|
{
|
|
|
|
std::cerr << "RsDataService::storeMessage() sqlInsert Failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t For GroupId: " << msgMetaPtr->mGroupId.toStdString();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t & MessageId: " << msgMetaPtr->mMsgId.toStdString();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// finish transaction
|
2012-09-27 17:42:00 -04:00
|
|
|
bool ret = mDb->execSQL("COMMIT;");
|
|
|
|
|
|
|
|
for(mit = msg.begin(); mit != msg.end(); mit++)
|
|
|
|
{
|
|
|
|
//TODO: API encourages aliasing, remove this abomination
|
|
|
|
if(mit->second != mit->first->metaData)
|
|
|
|
delete mit->second;
|
|
|
|
|
|
|
|
delete mit->first;
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2013-02-07 18:04:16 -05:00
|
|
|
bool RsDataService::validSize(RsNxsMsg* msg) const
|
|
|
|
{
|
|
|
|
if((msg->msg.TlvSize() + msg->meta.TlvSize()) <= GXS_MAX_ITEM_SIZE) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
int RsDataService::storeGroup(std::map<RsNxsGrp *, RsGxsGrpMetaData *> &grp)
|
|
|
|
{
|
2012-10-15 14:52:47 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
std::map<RsNxsGrp*, RsGxsGrpMetaData* >::iterator sit = grp.begin();
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// begin transaction
|
2012-05-21 18:07:43 -04:00
|
|
|
mDb->execSQL("BEGIN;");
|
|
|
|
|
2012-09-27 17:42:00 -04:00
|
|
|
for(; sit != grp.end(); sit++)
|
|
|
|
{
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
RsNxsGrp* grpPtr = sit->first;
|
|
|
|
RsGxsGrpMetaData* grpMetaPtr = sit->second;
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2013-02-07 18:04:16 -05:00
|
|
|
// if data is larger than max item size do not add
|
|
|
|
if(!validSize(grpPtr)) continue;
|
|
|
|
|
2014-05-04 08:48:42 -04:00
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "RsDataService::storeGroup() GrpId: " << grpPtr->grpId.toStdString();
|
|
|
|
std::cerr << " CircleType: " << (uint32_t) grpMetaPtr->mCircleType;
|
|
|
|
std::cerr << " CircleId: " << grpMetaPtr->mCircleId.toStdString();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string grpFile = mServiceDir + "/" + grpPtr->grpId.toStdString();
|
2012-05-21 18:07:43 -04:00
|
|
|
std::fstream ostrm(grpFile.c_str(), std::ios::binary | std::ios::app | std::ios::out);
|
|
|
|
ostrm.seekg(0, std::ios::end); // go to end to append
|
2012-05-24 18:38:59 -04:00
|
|
|
uint32_t offset = ostrm.tellg(); // get fill offset
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
/*!
|
|
|
|
* STORE file offset, file length, file name,
|
|
|
|
* grpId, flags, publish time stamp, identity,
|
|
|
|
* id signature, admin signatue, key set, last posting ts
|
|
|
|
* and meta data
|
|
|
|
**/
|
2012-05-21 18:07:43 -04:00
|
|
|
ContentValue cv;
|
2012-06-03 14:09:38 -04:00
|
|
|
cv.put(KEY_NXS_FILE_OFFSET, (int32_t)offset);
|
|
|
|
cv.put(KEY_NXS_FILE_LEN, (int32_t)grpPtr->grp.TlvSize());
|
|
|
|
cv.put(KEY_NXS_FILE, grpFile);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_GRP_ID, grpPtr->grpId.toStdString());
|
2012-07-05 17:26:14 -04:00
|
|
|
cv.put(KEY_GRP_NAME, grpMetaPtr->mGroupName);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_ORIG_GRP_ID, grpMetaPtr->mOrigGrpId.toStdString());
|
2012-08-19 18:15:37 -04:00
|
|
|
cv.put(KEY_NXS_SERV_STRING, grpMetaPtr->mServiceString);
|
2012-06-26 15:52:01 -04:00
|
|
|
cv.put(KEY_NXS_FLAGS, (int32_t)grpMetaPtr->mGroupFlags);
|
|
|
|
cv.put(KEY_TIME_STAMP, (int32_t)grpMetaPtr->mPublishTs);
|
2012-08-26 10:02:47 -04:00
|
|
|
cv.put(KEY_GRP_SIGN_FLAGS, (int32_t)grpMetaPtr->mSignFlags);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_GRP_CIRCLE_ID, grpMetaPtr->mCircleId.toStdString());
|
2012-12-02 14:40:17 -05:00
|
|
|
cv.put(KEY_GRP_CIRCLE_TYPE, (int32_t)grpMetaPtr->mCircleType);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_GRP_INTERNAL_CIRCLE, grpMetaPtr->mInternalCircle.toStdString());
|
|
|
|
cv.put(KEY_GRP_ORIGINATOR, grpMetaPtr->mOriginator.toStdString());
|
2013-06-04 17:00:43 -04:00
|
|
|
cv.put(KEY_GRP_AUTHEN_FLAGS, (int32_t)grpMetaPtr->mAuthenFlags);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_PARENT_GRP_ID, grpMetaPtr->mParentGrpId.toStdString());
|
|
|
|
cv.put(KEY_NXS_HASH, grpMetaPtr->mHash.toStdString());
|
2013-11-23 18:39:55 -05:00
|
|
|
cv.put(KEY_RECV_TS, (int32_t)grpMetaPtr->mRecvTS);
|
2014-02-22 19:08:11 -05:00
|
|
|
cv.put(KEY_GRP_REP_CUTOFF, (int32_t)grpMetaPtr->mReputationCutOff);
|
2014-04-21 06:13:35 -04:00
|
|
|
cv.put(KEY_NXS_IDENTITY, grpMetaPtr->mAuthorId.toStdString());
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-03 14:09:38 -04:00
|
|
|
offset = 0;
|
2012-06-26 15:52:01 -04:00
|
|
|
char keySetData[grpMetaPtr->keys.TlvSize()];
|
|
|
|
grpMetaPtr->keys.SetTlv(keySetData, grpMetaPtr->keys.TlvSize(), &offset);
|
|
|
|
cv.put(KEY_KEY_SET, grpMetaPtr->keys.TlvSize(), keySetData);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-03 14:09:38 -04:00
|
|
|
offset = 0;
|
2012-06-26 15:52:01 -04:00
|
|
|
char metaData[grpPtr->meta.TlvSize()];
|
|
|
|
grpPtr->meta.SetTlv(metaData, grpPtr->meta.TlvSize(), &offset);
|
|
|
|
cv.put(KEY_NXS_META, grpPtr->meta.TlvSize(), metaData);
|
|
|
|
|
|
|
|
// local meta data
|
|
|
|
cv.put(KEY_GRP_SUBCR_FLAG, (int32_t)grpMetaPtr->mSubscribeFlags);
|
|
|
|
cv.put(KEY_GRP_POP, (int32_t)grpMetaPtr->mPop);
|
|
|
|
cv.put(KEY_MSG_COUNT, (int32_t)grpMetaPtr->mMsgCount);
|
|
|
|
cv.put(KEY_GRP_STATUS, (int32_t)grpMetaPtr->mGroupStatus);
|
|
|
|
cv.put(KEY_GRP_LAST_POST, (int32_t)grpMetaPtr->mLastPost);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-03 14:09:38 -04:00
|
|
|
offset = 0;
|
2012-05-21 18:07:43 -04:00
|
|
|
char grpData[grpPtr->grp.TlvSize()];
|
2012-05-24 18:38:59 -04:00
|
|
|
grpPtr->grp.SetTlv(grpData, grpPtr->grp.TlvSize(), &offset);
|
2012-05-21 18:07:43 -04:00
|
|
|
ostrm.write(grpData, grpPtr->grp.TlvSize());
|
|
|
|
ostrm.close();
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2014-05-04 08:48:42 -04:00
|
|
|
if (!mDb->sqlInsert(GRP_TABLE_NAME, "", cv))
|
|
|
|
{
|
|
|
|
std::cerr << "RsDataService::storeGroup() sqlInsert Failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t For GroupId: " << grpMetaPtr->mGroupId.toStdString();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
2012-06-26 15:52:01 -04:00
|
|
|
// finish transaction
|
2012-09-27 17:42:00 -04:00
|
|
|
bool ret = mDb->execSQL("COMMIT;");
|
|
|
|
|
|
|
|
for(sit = grp.begin(); sit != grp.end(); sit++)
|
|
|
|
{
|
|
|
|
//TODO: API encourages aliasing, remove this abomination
|
|
|
|
if(sit->second != sit->first->metaData)
|
|
|
|
delete sit->second;
|
|
|
|
delete sit->first;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2013-09-11 16:42:56 -04:00
|
|
|
int RsDataService::updateGroup(std::map<RsNxsGrp *, RsGxsGrpMetaData *> &grp)
|
|
|
|
{
|
|
|
|
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
|
|
|
std::map<RsNxsGrp*, RsGxsGrpMetaData* >::iterator sit = grp.begin();
|
|
|
|
|
|
|
|
// begin transaction
|
|
|
|
mDb->execSQL("BEGIN;");
|
|
|
|
|
|
|
|
for(; sit != grp.end(); sit++)
|
|
|
|
{
|
|
|
|
|
|
|
|
RsNxsGrp* grpPtr = sit->first;
|
|
|
|
RsGxsGrpMetaData* grpMetaPtr = sit->second;
|
|
|
|
|
|
|
|
// if data is larger than max item size do not add
|
|
|
|
if(!validSize(grpPtr)) continue;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string grpFile = mServiceDir + "/" + grpPtr->grpId.toStdString();
|
2013-10-22 15:56:01 -04:00
|
|
|
std::ofstream ostrm(grpFile.c_str(), std::ios::binary | std::ios::trunc);
|
2013-10-13 17:49:17 -04:00
|
|
|
uint32_t offset = 0; // get file offset
|
2013-09-11 16:42:56 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* STORE file offset, file length, file name,
|
|
|
|
* grpId, flags, publish time stamp, identity,
|
|
|
|
* id signature, admin signatue, key set, last posting ts
|
|
|
|
* and meta data
|
|
|
|
**/
|
|
|
|
ContentValue cv;
|
|
|
|
cv.put(KEY_NXS_FILE_OFFSET, (int32_t)offset);
|
|
|
|
cv.put(KEY_NXS_FILE_LEN, (int32_t)grpPtr->grp.TlvSize());
|
|
|
|
cv.put(KEY_NXS_FILE, grpFile);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_GRP_ID, grpPtr->grpId.toStdString());
|
2013-09-11 16:42:56 -04:00
|
|
|
cv.put(KEY_GRP_NAME, grpMetaPtr->mGroupName);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_ORIG_GRP_ID, grpMetaPtr->mOrigGrpId.toStdString());
|
2013-09-11 16:42:56 -04:00
|
|
|
cv.put(KEY_NXS_SERV_STRING, grpMetaPtr->mServiceString);
|
|
|
|
cv.put(KEY_NXS_FLAGS, (int32_t)grpMetaPtr->mGroupFlags);
|
|
|
|
cv.put(KEY_TIME_STAMP, (int32_t)grpMetaPtr->mPublishTs);
|
|
|
|
cv.put(KEY_GRP_SIGN_FLAGS, (int32_t)grpMetaPtr->mSignFlags);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_GRP_CIRCLE_ID, grpMetaPtr->mCircleId.toStdString());
|
2013-09-11 16:42:56 -04:00
|
|
|
cv.put(KEY_GRP_CIRCLE_TYPE, (int32_t)grpMetaPtr->mCircleType);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_GRP_INTERNAL_CIRCLE, grpMetaPtr->mInternalCircle.toStdString());
|
|
|
|
cv.put(KEY_GRP_ORIGINATOR, grpMetaPtr->mOriginator.toStdString());
|
2013-09-11 16:42:56 -04:00
|
|
|
cv.put(KEY_GRP_AUTHEN_FLAGS, (int32_t)grpMetaPtr->mAuthenFlags);
|
2014-03-17 16:56:06 -04:00
|
|
|
cv.put(KEY_NXS_HASH, grpMetaPtr->mHash.toStdString());
|
2014-04-21 06:13:35 -04:00
|
|
|
cv.put(KEY_NXS_IDENTITY, grpMetaPtr->mAuthorId.toStdString());
|
2013-09-11 16:42:56 -04:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
char keySetData[grpMetaPtr->keys.TlvSize()];
|
|
|
|
grpMetaPtr->keys.SetTlv(keySetData, grpMetaPtr->keys.TlvSize(), &offset);
|
|
|
|
cv.put(KEY_KEY_SET, grpMetaPtr->keys.TlvSize(), keySetData);
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
char metaData[grpPtr->meta.TlvSize()];
|
|
|
|
grpPtr->meta.SetTlv(metaData, grpPtr->meta.TlvSize(), &offset);
|
|
|
|
cv.put(KEY_NXS_META, grpPtr->meta.TlvSize(), metaData);
|
|
|
|
|
|
|
|
// local meta data
|
|
|
|
cv.put(KEY_GRP_SUBCR_FLAG, (int32_t)grpMetaPtr->mSubscribeFlags);
|
|
|
|
cv.put(KEY_GRP_POP, (int32_t)grpMetaPtr->mPop);
|
|
|
|
cv.put(KEY_MSG_COUNT, (int32_t)grpMetaPtr->mMsgCount);
|
|
|
|
cv.put(KEY_GRP_STATUS, (int32_t)grpMetaPtr->mGroupStatus);
|
|
|
|
cv.put(KEY_GRP_LAST_POST, (int32_t)grpMetaPtr->mLastPost);
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
char grpData[grpPtr->grp.TlvSize()];
|
|
|
|
grpPtr->grp.SetTlv(grpData, grpPtr->grp.TlvSize(), &offset);
|
|
|
|
ostrm.write(grpData, grpPtr->grp.TlvSize());
|
|
|
|
ostrm.close();
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
mDb->sqlUpdate(GRP_TABLE_NAME, "grpId='" + grpPtr->grpId.toStdString() + "'", cv);
|
2013-09-11 16:42:56 -04:00
|
|
|
}
|
|
|
|
// finish transaction
|
|
|
|
bool ret = mDb->execSQL("COMMIT;");
|
|
|
|
|
|
|
|
for(sit = grp.begin(); sit != grp.end(); sit++)
|
|
|
|
{
|
|
|
|
//TODO: API encourages aliasing, remove this abomination
|
|
|
|
if(sit->second != sit->first->metaData)
|
|
|
|
delete sit->second;
|
|
|
|
delete sit->first;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-07 18:04:16 -05:00
|
|
|
bool RsDataService::validSize(RsNxsGrp* grp) const
|
|
|
|
{
|
|
|
|
if((grp->grp.TlvSize() + grp->meta.TlvSize()) <= GXS_MAX_ITEM_SIZE) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:13:44 -04:00
|
|
|
int RsDataService::retrieveNxsGrps(std::map<RsGxsGroupId, RsNxsGrp *> &grp, bool withMeta, bool /* cache */){
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
if(grp.empty()){
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpColumns, "", "");
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
std::vector<RsNxsGrp*> grps;
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_retrieveGroups(c, grps);
|
2012-10-15 14:52:47 -04:00
|
|
|
std::vector<RsNxsGrp*>::iterator vit = grps.begin();
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
for(; vit != grps.end(); vit++)
|
|
|
|
{
|
|
|
|
grp[(*vit)->grpId] = *vit;
|
|
|
|
}
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
delete c;
|
|
|
|
}
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
}else{
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
RsStackMutex stack(mDbMutex);
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsGxsGroupId, RsNxsGrp *>::iterator mit = grp.begin();
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::list<RsGxsGroupId> toRemove;
|
2013-02-10 18:28:06 -05:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
for(; mit != grp.end(); mit++)
|
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
|
|
|
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpColumns, "grpId='" + grpId.toStdString() + "'", "");
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
std::vector<RsNxsGrp*> grps;
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_retrieveGroups(c, grps);
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
if(!grps.empty())
|
|
|
|
{
|
|
|
|
RsNxsGrp* ng = grps.front();
|
|
|
|
grp[ng->grpId] = ng;
|
|
|
|
}else{
|
2013-02-10 18:28:06 -05:00
|
|
|
toRemove.push_back(grpId);
|
2012-10-15 14:52:47 -04:00
|
|
|
}
|
2012-07-14 13:59:54 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
}
|
2013-02-10 18:28:06 -05:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::list<RsGxsGroupId>::iterator grpIdIt;
|
2013-02-10 18:28:06 -05:00
|
|
|
for (grpIdIt = toRemove.begin(); grpIdIt != toRemove.end(); ++grpIdIt)
|
|
|
|
{
|
|
|
|
grp.erase(*grpIdIt);
|
|
|
|
}
|
2012-10-15 14:52:47 -04:00
|
|
|
}
|
2012-07-22 17:38:15 -04:00
|
|
|
|
2013-07-24 07:51:23 -04:00
|
|
|
if(withMeta && !grp.empty())
|
2012-10-15 14:52:47 -04:00
|
|
|
{
|
|
|
|
std::map<RsGxsGroupId, RsGxsGrpMetaData*> metaMap;
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsGxsGroupId, RsNxsGrp *>::iterator mit = grp.begin();
|
2012-10-15 14:52:47 -04:00
|
|
|
for(; mit != grp.end(); mit++)
|
|
|
|
metaMap.insert(std::make_pair(mit->first, (RsGxsGrpMetaData*)(NULL)));
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
retrieveGxsGrpMetaData(metaMap);
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
mit = grp.begin();
|
|
|
|
for(; mit != grp.end(); mit++)
|
|
|
|
{
|
|
|
|
RsNxsGrp* grpPtr = grp[mit->first];
|
|
|
|
grpPtr->metaData = metaMap[mit->first];
|
2014-05-04 08:48:42 -04:00
|
|
|
|
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "RsDataService::retrieveNxsGrps() GrpId: " << mit->first.toStdString();
|
|
|
|
std::cerr << " CircleType: " << (uint32_t) grpPtr->metaData->mCircleType;
|
|
|
|
std::cerr << " CircleId: " << grpPtr->metaData->mCircleId.toStdString();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2012-08-19 18:15:37 -04:00
|
|
|
}
|
2012-10-15 14:52:47 -04:00
|
|
|
}
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
return 1;
|
2012-07-14 13:59:54 -04:00
|
|
|
}
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
void RsDataService::locked_retrieveGroups(RetroCursor* c, std::vector<RsNxsGrp*>& grps){
|
2012-07-14 13:59:54 -04:00
|
|
|
|
|
|
|
if(c){
|
2012-05-21 18:07:43 -04:00
|
|
|
bool valid = c->moveToFirst();
|
|
|
|
|
|
|
|
while(valid){
|
2013-03-23 10:07:52 -04:00
|
|
|
RsNxsGrp* g = locked_getGroup(*c);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
|
|
|
// only add the latest grp info
|
2012-06-26 15:52:01 -04:00
|
|
|
if(g)
|
|
|
|
{
|
2012-07-14 13:59:54 -04:00
|
|
|
grps.push_back(g);
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:13:44 -04:00
|
|
|
int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, bool /* cache */, bool withMeta)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
GxsMsgReq::const_iterator mit = reqIds.begin();
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
GxsMsgReq metaReqIds;// collects metaReqIds if needed
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
for(; mit != reqIds.end(); mit++)
|
|
|
|
{
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
2012-06-26 15:52:01 -04:00
|
|
|
|
|
|
|
// if vector empty then request all messages
|
2014-03-17 16:56:06 -04:00
|
|
|
const std::vector<RsGxsMessageId>& msgIdV = mit->second;
|
2012-06-26 15:52:01 -04:00
|
|
|
std::vector<RsNxsMsg*> msgSet;
|
|
|
|
|
2012-07-18 17:35:41 -04:00
|
|
|
if(msgIdV.empty()){
|
2012-10-15 14:52:47 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, msgColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
if(c)
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_retrieveMessages(c, msgSet);
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
delete c;
|
2012-06-26 15:52:01 -04:00
|
|
|
}else{
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// request each grp
|
2014-03-17 16:56:06 -04:00
|
|
|
std::vector<RsGxsMessageId>::const_iterator sit = msgIdV.begin();
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-07-18 17:35:41 -04:00
|
|
|
for(; sit!=msgIdV.end();sit++){
|
2014-03-17 16:56:06 -04:00
|
|
|
const RsGxsMessageId& msgId = *sit;
|
2012-10-15 14:52:47 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, msgColumns, KEY_GRP_ID+ "='" + grpId.toStdString()
|
|
|
|
+ "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", "");
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
if(c)
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_retrieveMessages(c, msgSet);
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
delete c;
|
2012-06-03 14:09:38 -04:00
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
msg[grpId] = msgSet;
|
2012-08-19 18:15:37 -04:00
|
|
|
|
|
|
|
if(withMeta)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsMessageId> msgIds;
|
|
|
|
|
|
|
|
std::vector<RsNxsMsg*>::iterator lit = msgSet.begin(),
|
|
|
|
lit_end = msgSet.end();
|
|
|
|
|
|
|
|
for(; lit != lit_end; lit++)
|
|
|
|
msgIds.push_back( (*lit)->msgId );
|
|
|
|
|
|
|
|
metaReqIds[grpId] = msgIds;
|
|
|
|
}
|
2013-02-12 16:45:30 -05:00
|
|
|
msgSet.clear();
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
2012-08-19 18:15:37 -04:00
|
|
|
|
|
|
|
// tres expensive !?
|
|
|
|
if(withMeta)
|
|
|
|
{
|
|
|
|
|
|
|
|
GxsMsgMetaResult metaResult;
|
|
|
|
|
|
|
|
// request with meta ids so there is no chance of
|
|
|
|
// a mem leak being left over
|
|
|
|
retrieveGxsMsgMetaData(metaReqIds, metaResult);
|
|
|
|
|
|
|
|
GxsMsgResult::iterator mit2 = msg.begin(), mit2_end = msg.end();
|
|
|
|
|
|
|
|
for(; mit2 != mit2_end; mit2++)
|
|
|
|
{
|
|
|
|
const RsGxsGroupId& grpId = mit2->first;
|
|
|
|
std::vector<RsNxsMsg*>& msgV = msg[grpId];
|
|
|
|
std::vector<RsNxsMsg*>::iterator lit = msgV.begin(),
|
|
|
|
lit_end = msgV.end();
|
|
|
|
|
|
|
|
// as retrieval only attempts to retrieve what was found this elimiates chance
|
|
|
|
// of a memory fault as all are assigned
|
|
|
|
for(; lit != lit_end; lit++)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsMsgMetaData*>& msgMetaV = metaResult[grpId];
|
|
|
|
std::vector<RsGxsMsgMetaData*>::iterator meta_lit = msgMetaV.begin();
|
|
|
|
RsNxsMsg* msgPtr = *lit;
|
|
|
|
for(; meta_lit != msgMetaV.end(); )
|
|
|
|
{
|
|
|
|
RsGxsMsgMetaData* meta = *meta_lit;
|
|
|
|
if(meta->mMsgId == msgPtr->msgId)
|
|
|
|
{
|
|
|
|
msgPtr->metaData = meta;
|
|
|
|
meta_lit = msgMetaV.erase(meta_lit);
|
|
|
|
}else{
|
|
|
|
meta_lit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RsGxsMsgMetaData*>& msgMetaV = metaResult[grpId];
|
|
|
|
std::vector<RsGxsMsgMetaData*>::iterator meta_lit;
|
|
|
|
|
|
|
|
// clean up just in case, should not go in here
|
|
|
|
for(meta_lit = msgMetaV.begin(); meta_lit !=
|
|
|
|
msgMetaV.end(); )
|
|
|
|
{
|
|
|
|
RsGxsMsgMetaData* meta = *meta_lit;
|
|
|
|
delete meta;
|
2012-08-25 11:48:55 -04:00
|
|
|
meta_lit = msgMetaV.erase(meta_lit);
|
2012-08-19 18:15:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
void RsDataService::locked_retrieveMessages(RetroCursor *c, std::vector<RsNxsMsg *> &msgs)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
|
|
|
bool valid = c->moveToFirst();
|
|
|
|
while(valid){
|
2013-03-23 10:07:52 -04:00
|
|
|
RsNxsMsg* m = locked_getMessage(*c);
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
if(m){
|
|
|
|
msgs.push_back(m);;
|
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-09-20 13:18:26 -04:00
|
|
|
int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaResult &msgMeta)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2012-10-15 14:52:47 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
2012-09-20 13:18:26 -04:00
|
|
|
GxsMsgReq::const_iterator mit = reqIds.begin();
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
for(; mit != reqIds.end(); mit++)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
// if vector empty then request all messages
|
|
|
|
const std::vector<RsGxsMessageId>& msgIdV = mit->second;
|
|
|
|
std::vector<RsGxsMsgMetaData*> metaSet;
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
if(msgIdV.empty()){
|
2014-03-17 16:56:06 -04:00
|
|
|
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, msgMetaColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_retrieveMsgMeta(c, metaSet);
|
2012-08-19 18:15:37 -04:00
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// request each grp
|
2014-03-17 16:56:06 -04:00
|
|
|
std::vector<RsGxsMessageId>::const_iterator sit = msgIdV.begin();
|
2012-08-19 18:15:37 -04:00
|
|
|
|
|
|
|
for(; sit!=msgIdV.end();sit++){
|
2014-03-17 16:56:06 -04:00
|
|
|
const RsGxsMessageId& msgId = *sit;
|
|
|
|
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, msgMetaColumns, KEY_GRP_ID+ "='" + grpId.toStdString()
|
|
|
|
+ "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", "");
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_retrieveMsgMeta(c, metaSet);
|
2012-08-19 18:15:37 -04:00
|
|
|
}
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
msgMeta[grpId] = metaSet;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
2012-08-19 18:15:37 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
return 1;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
void RsDataService::locked_retrieveMsgMeta(RetroCursor *c, std::vector<RsGxsMsgMetaData *> &msgMeta)
|
2012-08-19 18:15:37 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
bool valid = c->moveToFirst();
|
|
|
|
while(valid){
|
2013-03-23 10:07:52 -04:00
|
|
|
RsGxsMsgMetaData* m = locked_getMsgMeta(*c);
|
2012-08-19 18:15:37 -04:00
|
|
|
|
|
|
|
if(m != NULL)
|
|
|
|
msgMeta.push_back(m);
|
|
|
|
|
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 10:02:47 -04:00
|
|
|
int RsDataService::retrieveGxsGrpMetaData(std::map<RsGxsGroupId, RsGxsGrpMetaData *>& grp)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2014-04-27 09:14:07 -04:00
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "RsDataService::retrieveGxsGrpMetaData()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
RsStackMutex stack(mDbMutex);
|
2012-08-26 10:02:47 -04:00
|
|
|
|
|
|
|
if(grp.empty()){
|
|
|
|
|
2014-04-27 09:14:07 -04:00
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "RsDataService::retrieveGxsGrpMetaData() retrieving all";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2012-08-26 10:02:47 -04:00
|
|
|
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpMetaColumns, "", "");
|
|
|
|
|
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
bool valid = c->moveToFirst();
|
|
|
|
|
|
|
|
while(valid)
|
|
|
|
{
|
2013-03-23 10:07:52 -04:00
|
|
|
RsGxsGrpMetaData* g = locked_getGrpMeta(*c);
|
2012-08-26 10:02:47 -04:00
|
|
|
if(g)
|
|
|
|
{
|
|
|
|
grp[g->mGroupId] = g;
|
|
|
|
}
|
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2012-08-26 10:02:47 -04:00
|
|
|
std::map<RsGxsGroupId, RsGxsGrpMetaData *>::iterator mit = grp.begin();
|
|
|
|
|
2013-10-22 15:56:01 -04:00
|
|
|
for(; mit != grp.end(); mit++)
|
|
|
|
{
|
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
2014-03-17 16:56:06 -04:00
|
|
|
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpMetaColumns, "grpId='" + grpId.toStdString() + "'", "");
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2013-10-22 15:56:01 -04:00
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
bool valid = c->moveToFirst();
|
2012-08-26 10:02:47 -04:00
|
|
|
|
2013-10-22 15:56:01 -04:00
|
|
|
while(valid)
|
|
|
|
{
|
|
|
|
RsGxsGrpMetaData* g = locked_getGrpMeta(*c);
|
2012-08-26 10:02:47 -04:00
|
|
|
|
2013-10-22 15:56:01 -04:00
|
|
|
if(g)
|
|
|
|
{
|
|
|
|
grp[g->mGroupId] = g;
|
|
|
|
}
|
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
delete c;
|
|
|
|
}
|
2012-08-26 10:02:47 -04:00
|
|
|
|
2013-10-22 15:56:01 -04:00
|
|
|
}
|
2012-08-26 10:02:47 -04:00
|
|
|
|
2013-10-22 15:56:01 -04:00
|
|
|
}
|
2012-08-26 10:02:47 -04:00
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
return 1;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
int RsDataService::resetDataStore()
|
|
|
|
{
|
2012-06-03 14:09:38 -04:00
|
|
|
|
2012-06-05 10:46:18 -04:00
|
|
|
#ifdef RS_DATA_SERVICE_DEBUG
|
|
|
|
std::cerr << "resetDataStore() " << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsGxsGroupId, RsNxsGrp*> grps;
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-07-26 17:48:54 -04:00
|
|
|
retrieveNxsGrps(grps, false, false);
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsGxsGroupId, RsNxsGrp*>::iterator mit
|
2012-06-05 10:46:18 -04:00
|
|
|
= grps.begin();
|
|
|
|
|
2012-10-15 14:52:47 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mDbMutex);
|
2012-06-05 10:46:18 -04:00
|
|
|
|
2013-10-22 15:56:01 -04:00
|
|
|
// remove all grp msgs files from service dir
|
|
|
|
for(; mit != grps.end(); mit++){
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string file = mServiceDir + "/" + mit->first.toStdString();
|
2013-10-22 15:56:01 -04:00
|
|
|
std::string msgFile = file + "-msgs";
|
|
|
|
remove(file.c_str()); // remove group file
|
|
|
|
remove(msgFile.c_str()); // and remove messages file
|
|
|
|
delete mit->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDb->execSQL("DROP TABLE " + MSG_TABLE_NAME);
|
|
|
|
mDb->execSQL("DROP TABLE " + GRP_TABLE_NAME);
|
2013-11-23 18:39:55 -05:00
|
|
|
mDb->execSQL("DROP TRIGGER " + GRP_LAST_POST_UPDATE_TRIGGER);
|
2013-10-22 15:56:01 -04:00
|
|
|
}
|
2012-06-05 10:46:18 -04:00
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
// recreate database
|
2012-06-05 10:46:18 -04:00
|
|
|
initialise();
|
|
|
|
|
|
|
|
return 1;
|
2012-06-03 14:09:38 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
int RsDataService::updateGroupMetaData(GrpLocMetaData &meta)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2013-03-20 18:26:24 -04:00
|
|
|
RsStackMutex stack(mDbMutex);
|
2012-08-21 17:32:07 -04:00
|
|
|
RsGxsGroupId& grpId = meta.grpId;
|
2014-03-17 16:56:06 -04:00
|
|
|
return mDb->sqlUpdate(GRP_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", meta.val) ? 1 : 0;
|
2012-06-03 14:09:38 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 18:15:37 -04:00
|
|
|
int RsDataService::updateMessageMetaData(MsgLocMetaData &metaData)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2013-03-20 18:26:24 -04:00
|
|
|
RsStackMutex stack(mDbMutex);
|
2012-08-21 17:32:07 -04:00
|
|
|
RsGxsGroupId& grpId = metaData.msgId.first;
|
|
|
|
RsGxsMessageId& msgId = metaData.msgId.second;
|
2014-03-17 16:56:06 -04:00
|
|
|
return mDb->sqlUpdate(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString()
|
|
|
|
+ "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", metaData.val) ? 1 : 0;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2013-03-20 18:26:24 -04:00
|
|
|
MsgOffset offSetAccum(const MsgOffset& x, const MsgOffset& y)
|
|
|
|
{
|
|
|
|
MsgOffset m;
|
|
|
|
m.msgLen = y.msgLen + x.msgLen;
|
|
|
|
return m;
|
|
|
|
}
|
2012-06-26 15:52:01 -04:00
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
int RsDataService::removeMsgs(const GxsMsgReq& msgIds)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2013-03-20 18:26:24 -04:00
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
|
|
|
// for each group
|
2013-03-21 19:17:24 -04:00
|
|
|
// get for all msgs their offsets and lengths
|
|
|
|
// for message not contained in msg id vector
|
|
|
|
// store their data file segments in buffer
|
2013-06-04 17:00:43 -04:00
|
|
|
// then recalculate the retained messages'
|
2013-03-21 19:17:24 -04:00
|
|
|
// new offsets, update db with new offsets
|
|
|
|
// replace old msg file with new file
|
|
|
|
// remove messages that were not retained from
|
|
|
|
// db
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
GxsMsgReq::const_iterator mit = msgIds.begin();
|
|
|
|
|
|
|
|
|
|
|
|
for(; mit != msgIds.end(); mit++)
|
|
|
|
{
|
|
|
|
MsgUpdates updates;
|
|
|
|
const std::vector<RsGxsMessageId>& msgIdV = mit->second;
|
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
|
|
|
|
|
|
|
GxsMsgReq reqIds;
|
|
|
|
reqIds.insert(std::make_pair(grpId, std::vector<RsGxsMessageId>() ));
|
|
|
|
|
|
|
|
// can get offsets for each file
|
|
|
|
std::vector<MsgOffset> msgOffsets;
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_getMessageOffsets(grpId, msgOffsets);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string oldFileName = mServiceDir + "/" + grpId.toStdString() + "-msgs";
|
|
|
|
std::string newFileName = mServiceDir + "/" + grpId.toStdString() + "-msgs-temp";
|
2013-03-20 18:26:24 -04:00
|
|
|
std::ifstream in(oldFileName.c_str(), std::ios::binary);
|
|
|
|
std::vector<char> dataBuff, newBuffer;
|
|
|
|
|
2013-03-21 19:17:24 -04:00
|
|
|
std::vector<MsgOffset>::iterator vit = msgOffsets.begin();
|
2013-03-20 18:26:24 -04:00
|
|
|
|
2013-10-11 13:29:38 -04:00
|
|
|
uint32_t maxSize = 0;// größe aller msgs, newbuf könnte aber kleiner sein, weil msgs weggehen
|
2013-03-20 18:26:24 -04:00
|
|
|
for(; vit != msgOffsets.end(); vit++)
|
|
|
|
maxSize += vit->msgLen;
|
|
|
|
|
2013-03-21 19:17:24 -04:00
|
|
|
// may be preferable to determine file len reality
|
|
|
|
// from file? corrupt db?
|
2013-10-11 13:29:38 -04:00
|
|
|
dataBuff.reserve(maxSize);// dataBuff.resize(maxSize);
|
|
|
|
newBuffer.reserve(maxSize);// newBuffer.resize(maxSize);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
dataBuff.insert(dataBuff.end(),
|
|
|
|
std::istreambuf_iterator<char>(in),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
|
|
|
|
in.close();
|
2013-10-11 13:29:38 -04:00
|
|
|
uint32_t newOffset = 0;// am anfang der liste ist offset=0, jetzt gehen wir die msgs liste durch
|
2013-03-21 19:17:24 -04:00
|
|
|
for(std::vector<MsgOffset>::size_type i = 0; i < msgOffsets.size(); i++)
|
2013-03-20 18:26:24 -04:00
|
|
|
{
|
|
|
|
const MsgOffset& m = msgOffsets[i];
|
|
|
|
|
2013-10-11 13:29:38 -04:00
|
|
|
//uint32_t newOffset = 0;//hier ist es zu spät, offset muss hochgezählt werden
|
2013-03-20 18:26:24 -04:00
|
|
|
if(std::find(msgIdV.begin(), msgIdV.end(), m.msgId) == msgIdV.end())
|
|
|
|
{
|
|
|
|
MsgUpdate up;
|
|
|
|
|
|
|
|
uint32_t msgLen = m.msgLen;
|
|
|
|
|
|
|
|
up.msgId = m.msgId;
|
2013-03-21 19:17:24 -04:00
|
|
|
up.cv.put(KEY_NXS_FILE_OFFSET, (int32_t)newOffset);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
newBuffer.insert(newBuffer.end(), dataBuff.begin()+m.msgOffset,
|
2013-03-20 18:26:24 -04:00
|
|
|
dataBuff.begin()+m.msgOffset+m.msgLen);
|
|
|
|
|
|
|
|
newOffset += msgLen;
|
|
|
|
|
2013-03-21 19:17:24 -04:00
|
|
|
up.cv.put(KEY_NXS_FILE_LEN, (int32_t)msgLen);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
// add msg update
|
|
|
|
updates[grpId].push_back(up);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream out(newFileName.c_str(), std::ios::binary);
|
|
|
|
|
|
|
|
std::copy(newBuffer.begin(), newBuffer.end(),
|
|
|
|
std::ostreambuf_iterator<char>(out));
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
// now update the new positions in db
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_updateMessageEntries(updates);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
// then delete removed messages
|
|
|
|
GxsMsgReq msgsToDelete;
|
|
|
|
msgsToDelete[grpId] = msgIdV;
|
2013-03-23 10:07:52 -04:00
|
|
|
locked_removeMessageEntries(msgsToDelete);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
// now replace old file location with new file
|
|
|
|
remove(oldFileName.c_str());
|
|
|
|
RsDirUtil::renameFile(newFileName, oldFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
int RsDataService::removeGroups(const std::vector<RsGxsGroupId> &grpIds)
|
2013-06-04 17:00:43 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
|
|
|
// the grp id is the group file name
|
|
|
|
// first remove file then remove group
|
|
|
|
// from db
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::vector<RsGxsGroupId>::const_iterator vit = grpIds.begin();
|
2013-06-04 17:00:43 -04:00
|
|
|
for(; vit != grpIds.end(); vit++)
|
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
const std::string grpFileName = mServiceDir + "/" + (*vit).toStdString();
|
2013-06-04 17:00:43 -04:00
|
|
|
remove(grpFileName.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
locked_removeGroupEntries(grpIds);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-20 18:26:24 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
int RsDataService::retrieveGroupIds(std::vector<RsGxsGroupId> &grpIds)
|
2013-09-11 16:42:56 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mDbMutex);
|
|
|
|
|
|
|
|
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, grpIdColumn, "", "");
|
|
|
|
|
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
bool valid = c->moveToFirst();
|
|
|
|
|
|
|
|
while(valid)
|
|
|
|
{
|
|
|
|
std::string grpId;
|
|
|
|
c->getString(0, grpId);
|
2014-03-17 16:56:06 -04:00
|
|
|
grpIds.push_back(RsGxsGroupId(grpId));
|
2013-09-11 16:42:56 -04:00
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
delete c;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-20 18:26:24 -04:00
|
|
|
|
2014-04-26 06:07:25 -04:00
|
|
|
int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId,
|
|
|
|
RsGxsMessageId::std_vector& msgIds) {
|
|
|
|
|
|
|
|
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgIdColumn, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
|
|
|
|
|
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
bool valid = c->moveToFirst();
|
|
|
|
|
|
|
|
while(valid)
|
|
|
|
{
|
|
|
|
std::string msgId;
|
|
|
|
c->getString(0, msgId);
|
|
|
|
msgIds.push_back(RsGxsMessageId(msgId));
|
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
delete c;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
bool RsDataService::locked_updateMessageEntries(const MsgUpdates& updates)
|
2013-03-20 18:26:24 -04:00
|
|
|
{
|
|
|
|
// start a transaction
|
|
|
|
bool ret = mDb->execSQL("BEGIN;");
|
|
|
|
|
|
|
|
MsgUpdates::const_iterator mit = updates.begin();
|
|
|
|
|
|
|
|
for(; mit != updates.end(); mit++)
|
|
|
|
{
|
|
|
|
|
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
|
|
|
const std::vector<MsgUpdate>& updateV = mit->second;
|
|
|
|
std::vector<MsgUpdate>::const_iterator vit = updateV.begin();
|
|
|
|
|
|
|
|
for(; vit != updateV.end(); vit++)
|
|
|
|
{
|
|
|
|
const MsgUpdate& update = *vit;
|
2014-03-17 16:56:06 -04:00
|
|
|
mDb->sqlUpdate(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString()
|
|
|
|
+ "' AND " + KEY_MSG_ID + "='" + update.msgId.toStdString() + "'", update.cv);
|
2013-03-20 18:26:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret &= mDb->execSQL("COMMIT;");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-23 10:07:52 -04:00
|
|
|
bool RsDataService::locked_removeMessageEntries(const GxsMsgReq& msgIds)
|
2013-03-20 18:26:24 -04:00
|
|
|
{
|
|
|
|
// start a transaction
|
|
|
|
bool ret = mDb->execSQL("BEGIN;");
|
|
|
|
|
|
|
|
GxsMsgReq::const_iterator mit = msgIds.begin();
|
|
|
|
|
|
|
|
for(; mit != msgIds.end(); mit++)
|
|
|
|
{
|
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
|
|
|
const std::vector<RsGxsMessageId>& msgsV = mit->second;
|
|
|
|
std::vector<RsGxsMessageId>::const_iterator vit = msgsV.begin();
|
|
|
|
|
|
|
|
for(; vit != msgsV.end(); vit++)
|
|
|
|
{
|
|
|
|
const RsGxsMessageId& msgId = *vit;
|
2014-03-17 16:56:06 -04:00
|
|
|
mDb->sqlDelete(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString()
|
|
|
|
+ "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", "");
|
2013-03-20 18:26:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret &= mDb->execSQL("COMMIT;");
|
|
|
|
|
|
|
|
return ret;
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
bool RsDataService::locked_removeGroupEntries(const std::vector<RsGxsGroupId>& grpIds)
|
2013-06-04 17:00:43 -04:00
|
|
|
{
|
|
|
|
// start a transaction
|
|
|
|
bool ret = mDb->execSQL("BEGIN;");
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::vector<RsGxsGroupId>::const_iterator vit = grpIds.begin();
|
2013-06-04 17:00:43 -04:00
|
|
|
|
|
|
|
for(; vit != grpIds.end(); vit++)
|
|
|
|
{
|
|
|
|
|
|
|
|
const RsGxsGroupId& grpId = *vit;
|
2014-03-17 16:56:06 -04:00
|
|
|
mDb->sqlDelete(GRP_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
|
2013-06-04 17:00:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret &= mDb->execSQL("COMMIT;");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2013-03-23 10:07:52 -04:00
|
|
|
void RsDataService::locked_getMessageOffsets(const RsGxsGroupId& grpId, std::vector<MsgOffset>& offsets)
|
2013-03-20 18:26:24 -04:00
|
|
|
{
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgOffSetColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
bool valid = c->moveToFirst();
|
|
|
|
|
|
|
|
while(valid)
|
|
|
|
{
|
|
|
|
RsGxsMessageId msgId;
|
|
|
|
int32_t msgLen;
|
|
|
|
int32_t msgOffSet;
|
2014-03-17 16:56:06 -04:00
|
|
|
std::string temp;
|
|
|
|
c->getString(0, temp);
|
2014-04-20 12:34:26 -04:00
|
|
|
msgId = RsGxsMessageId(temp);
|
2013-03-21 19:17:24 -04:00
|
|
|
msgOffSet = c->getInt32(1);
|
|
|
|
msgLen = c->getInt32(2);
|
2013-03-20 18:26:24 -04:00
|
|
|
|
|
|
|
MsgOffset offset;
|
|
|
|
offset.msgId = msgId;
|
|
|
|
offset.msgLen = msgLen;
|
|
|
|
offset.msgOffset = msgOffSet;
|
|
|
|
offsets.push_back(offset);
|
|
|
|
|
|
|
|
valid = c->moveToNext();
|
|
|
|
}
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 15:52:01 -04:00
|
|
|
uint32_t RsDataService::cacheSize() const {
|
2012-05-21 18:07:43 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:13:44 -04:00
|
|
|
int RsDataService::setCacheSize(uint32_t /* size */)
|
2012-06-26 15:52:01 -04:00
|
|
|
{
|
2012-05-21 18:07:43 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2012-06-26 15:52:01 -04:00
|
|
|
|