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 " .
*
*/
2015-08-11 17:19:37 -04:00
/*****
* # define RS_DATA_SERVICE_DEBUG 1
* # define RS_DATA_SERVICE_DEBUG_TIME 1
2016-07-01 23:38:10 -04:00
* # define RS_DATA_SERVICE_DEBUG_CACHE 1
2015-08-11 17:19:37 -04:00
* * * */
2012-08-06 17:00:38 -04:00
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
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-27 14:22:31 -05:00
# include <util/rstime.h>
2015-08-11 17:19:37 -04:00
# endif
2012-05-21 18:07:43 -04:00
# include "rsdataservice.h"
2018-03-01 05:45:54 -05:00
# include "retroshare/rsgxsflags.h"
2015-08-16 07:27:04 -04:00
# include "util/rsstring.h"
2012-05-21 18:07:43 -04:00
# define MSG_TABLE_NAME std::string("MESSAGES")
# define GRP_TABLE_NAME std::string("GROUPS")
2015-08-16 07:27:04 -04:00
# define DATABASE_RELEASE_TABLE_NAME std::string("DATABASE_RELEASE")
2012-05-21 18:07:43 -04:00
2013-11-23 18:39:55 -05:00
# define GRP_LAST_POST_UPDATE_TRIGGER std::string("LAST_POST_UPDATE")
2015-08-10 07:58:19 -04:00
# define MSG_INDEX_GRPID std::string("INDEX_MESSAGES_GRPID")
2012-05-21 18:07:43 -04:00
// generic
2015-10-25 17:16:16 -04:00
# define KEY_NXS_DATA std::string("nxsData")
# define KEY_NXS_DATA_LEN std::string("nxsDataLen")
# define KEY_NXS_IDENTITY std::string("identity")
# define KEY_GRP_ID std::string("grpId")
# define KEY_ORIG_GRP_ID std::string("origGrpId")
# define KEY_PARENT_GRP_ID std::string("parentGrpId")
# define KEY_SIGN_SET std::string("signSet")
# define KEY_TIME_STAMP std::string("timeStamp")
# define KEY_NXS_FLAGS std::string("flags")
# define KEY_NXS_META std::string("meta")
2012-08-19 18:15:37 -04:00
# define KEY_NXS_SERV_STRING std::string("serv_str")
2015-10-25 17:16:16 -04:00
# define KEY_NXS_HASH std::string("hash")
# define KEY_RECV_TS std::string("recv_time_stamp")
2012-06-26 15:52:01 -04:00
2015-08-16 07:27:04 -04:00
// remove later
# define KEY_NXS_FILE_OLD std::string("nxsFile")
# define KEY_NXS_FILE_OFFSET_OLD std::string("fileOffset")
# define KEY_NXS_FILE_LEN_OLD std::string("nxsFileLen")
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
2015-10-25 17:16:16 -04:00
# define KEY_MSG_STATUS std::string("msgStatus")
# define KEY_CHILD_TS std::string("childTs")
2012-05-21 18:07:43 -04:00
2015-08-16 07:27:04 -04:00
// database release columns
# define KEY_DATABASE_RELEASE_ID std::string("id")
# define KEY_DATABASE_RELEASE_ID_VALUE 1
# define KEY_DATABASE_RELEASE std::string("release")
2012-05-21 18:07:43 -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
2018-01-15 17:13:42 -05:00
static const uint32_t CACHE_ENTRY_GRACE_PERIOD = 600 ; // 10 minutes
2015-08-23 13:05:37 -04:00
static int addColumn ( std : : list < std : : string > & list , const std : : string & attribute )
{
list . push_back ( attribute ) ;
return list . size ( ) - 1 ;
}
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 )
2015-08-16 07:27:04 -04:00
: RsGeneralDataService ( ) , mDbMutex ( " RsDataService " ) , mServiceDir ( serviceDir ) , mDbName ( dbName ) , mDbPath ( mServiceDir + " / " + dbName ) , mServType ( serviceType ) , mDb ( NULL )
{
bool isNewDatabase = ! RsDirUtil : : fileExists ( mDbPath ) ;
2016-07-01 23:38:10 -04:00
mGrpMetaDataCache_ContainsAllDatabase = false ;
2015-08-16 07:27:04 -04:00
mDb = new RetroDb ( mDbPath , RetroDb : : OPEN_READWRITE_CREATE , key ) ;
2012-06-05 10:46:18 -04:00
2015-08-16 07:27:04 -04:00
initialise ( isNewDatabase ) ;
2012-06-05 10:46:18 -04:00
2012-06-26 15:52:01 -04:00
// for retrieving msg meta
2015-10-25 17:16:16 -04:00
mColMsgMeta_GrpId = addColumn ( mMsgMetaColumns , KEY_GRP_ID ) ;
mColMsgMeta_TimeStamp = addColumn ( mMsgMetaColumns , KEY_TIME_STAMP ) ;
mColMsgMeta_NxsFlags = addColumn ( mMsgMetaColumns , KEY_NXS_FLAGS ) ;
mColMsgMeta_SignSet = addColumn ( mMsgMetaColumns , KEY_SIGN_SET ) ;
mColMsgMeta_NxsIdentity = addColumn ( mMsgMetaColumns , KEY_NXS_IDENTITY ) ;
mColMsgMeta_NxsHash = addColumn ( mMsgMetaColumns , KEY_NXS_HASH ) ;
mColMsgMeta_MsgId = addColumn ( mMsgMetaColumns , KEY_MSG_ID ) ;
mColMsgMeta_OrigMsgId = addColumn ( mMsgMetaColumns , KEY_ORIG_MSG_ID ) ;
mColMsgMeta_MsgStatus = addColumn ( mMsgMetaColumns , KEY_MSG_STATUS ) ;
mColMsgMeta_ChildTs = addColumn ( mMsgMetaColumns , KEY_CHILD_TS ) ;
mColMsgMeta_MsgParentId = addColumn ( mMsgMetaColumns , KEY_MSG_PARENT_ID ) ;
mColMsgMeta_MsgThreadId = addColumn ( mMsgMetaColumns , KEY_MSG_THREAD_ID ) ;
mColMsgMeta_Name = addColumn ( mMsgMetaColumns , KEY_MSG_NAME ) ;
2015-08-23 13:05:37 -04:00
mColMsgMeta_NxsServString = addColumn ( mMsgMetaColumns , KEY_NXS_SERV_STRING ) ;
2015-10-25 17:16:16 -04:00
mColMsgMeta_RecvTs = addColumn ( mMsgMetaColumns , KEY_RECV_TS ) ;
mColMsgMeta_NxsDataLen = addColumn ( mMsgMetaColumns , KEY_NXS_DATA_LEN ) ;
2012-06-26 15:52:01 -04:00
// for retrieving actual data
2015-08-23 13:05:37 -04:00
mColMsg_GrpId = addColumn ( mMsgColumns , KEY_GRP_ID ) ;
mColMsg_NxsData = addColumn ( mMsgColumns , KEY_NXS_DATA ) ;
mColMsg_MetaData = addColumn ( mMsgColumns , KEY_NXS_META ) ;
mColMsg_MsgId = addColumn ( mMsgColumns , KEY_MSG_ID ) ;
2012-06-26 15:52:01 -04:00
2015-09-05 06:46:18 -04:00
// for retrieving msg data with meta
mMsgColumnsWithMeta = mMsgColumns ;
mColMsg_WithMetaOffset = mMsgColumnsWithMeta . size ( ) ;
mMsgColumnsWithMeta . insert ( mMsgColumnsWithMeta . end ( ) , mMsgMetaColumns . begin ( ) , mMsgMetaColumns . end ( ) ) ;
2012-06-26 15:52:01 -04:00
// for retrieving grp meta data
2015-10-25 17:16:16 -04:00
mColGrpMeta_GrpId = addColumn ( mGrpMetaColumns , KEY_GRP_ID ) ;
mColGrpMeta_TimeStamp = addColumn ( mGrpMetaColumns , KEY_TIME_STAMP ) ;
mColGrpMeta_NxsFlags = addColumn ( mGrpMetaColumns , KEY_NXS_FLAGS ) ;
2015-08-23 13:05:37 -04:00
// mColGrpMeta_SignSet = addColumn(mGrpMetaColumns, KEY_SIGN_SET);
mColGrpMeta_NxsIdentity = addColumn ( mGrpMetaColumns , KEY_NXS_IDENTITY ) ;
2015-10-25 17:16:16 -04:00
mColGrpMeta_NxsHash = addColumn ( mGrpMetaColumns , KEY_NXS_HASH ) ;
mColGrpMeta_KeySet = addColumn ( mGrpMetaColumns , KEY_KEY_SET ) ;
mColGrpMeta_SubscrFlag = addColumn ( mGrpMetaColumns , KEY_GRP_SUBCR_FLAG ) ;
mColGrpMeta_Pop = addColumn ( mGrpMetaColumns , KEY_GRP_POP ) ;
mColGrpMeta_MsgCount = addColumn ( mGrpMetaColumns , KEY_MSG_COUNT ) ;
mColGrpMeta_Status = addColumn ( mGrpMetaColumns , KEY_GRP_STATUS ) ;
mColGrpMeta_Name = addColumn ( mGrpMetaColumns , KEY_GRP_NAME ) ;
mColGrpMeta_LastPost = addColumn ( mGrpMetaColumns , KEY_GRP_LAST_POST ) ;
mColGrpMeta_OrigGrpId = addColumn ( mGrpMetaColumns , KEY_ORIG_GRP_ID ) ;
mColGrpMeta_ServString = addColumn ( mGrpMetaColumns , KEY_NXS_SERV_STRING ) ;
mColGrpMeta_SignFlags = addColumn ( mGrpMetaColumns , KEY_GRP_SIGN_FLAGS ) ;
mColGrpMeta_CircleId = addColumn ( mGrpMetaColumns , KEY_GRP_CIRCLE_ID ) ;
mColGrpMeta_CircleType = addColumn ( mGrpMetaColumns , KEY_GRP_CIRCLE_TYPE ) ;
2015-08-23 13:05:37 -04:00
mColGrpMeta_InternCircle = addColumn ( mGrpMetaColumns , KEY_GRP_INTERNAL_CIRCLE ) ;
2015-10-25 17:16:16 -04:00
mColGrpMeta_Originator = addColumn ( mGrpMetaColumns , KEY_GRP_ORIGINATOR ) ;
2015-08-23 13:05:37 -04:00
mColGrpMeta_AuthenFlags = addColumn ( mGrpMetaColumns , KEY_GRP_AUTHEN_FLAGS ) ;
mColGrpMeta_ParentGrpId = addColumn ( mGrpMetaColumns , KEY_PARENT_GRP_ID ) ;
2015-10-25 17:16:16 -04:00
mColGrpMeta_RecvTs = addColumn ( mGrpMetaColumns , KEY_RECV_TS ) ;
mColGrpMeta_RepCutoff = addColumn ( mGrpMetaColumns , KEY_GRP_REP_CUTOFF ) ;
mColGrpMeta_NxsDataLen = addColumn ( mGrpMetaColumns , KEY_NXS_DATA_LEN ) ;
2012-06-26 15:52:01 -04:00
// for retrieving actual grp data
2015-08-23 13:05:37 -04:00
mColGrp_GrpId = addColumn ( mGrpColumns , KEY_GRP_ID ) ;
mColGrp_NxsData = addColumn ( mGrpColumns , KEY_NXS_DATA ) ;
mColGrp_MetaData = addColumn ( mGrpColumns , KEY_NXS_META ) ;
2013-09-11 16:42:56 -04:00
2015-09-05 06:46:18 -04:00
// for retrieving grp data with meta
mGrpColumnsWithMeta = mGrpColumns ;
mColGrp_WithMetaOffset = mGrpColumnsWithMeta . size ( ) ;
mGrpColumnsWithMeta . insert ( mGrpColumnsWithMeta . end ( ) , mGrpMetaColumns . begin ( ) , mGrpMetaColumns . end ( ) ) ;
2015-08-23 13:05:37 -04:00
// Group id columns
mColGrpId_GrpId = addColumn ( mGrpIdColumn , KEY_GRP_ID ) ;
2014-04-26 06:07:25 -04:00
2015-08-23 13:05:37 -04:00
// Msg id columns
mColMsgId_MsgId = addColumn ( mMsgIdColumn , 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
2016-10-29 11:26:08 -04:00
std : : cerr < < " RsDataService::~RsDataService() " ;
std : : cerr < < std : : endl ;
2014-04-27 09:14:07 -04:00
# endif
2012-06-05 10:46:18 -04:00
mDb - > closeDb ( ) ;
delete mDb ;
}
2012-05-21 18:07:43 -04:00
2015-08-16 07:27:04 -04:00
static bool moveDataFromFileToDatabase ( RetroDb * db , const std : : string serviceDir , const std : : string & tableName , const std : : string & keyId , std : : list < std : : string > & files )
{
bool ok = true ;
// Move message data
std : : list < std : : string > columns ;
columns . push_back ( keyId ) ;
columns . push_back ( KEY_NXS_FILE_OLD ) ;
columns . push_back ( KEY_NXS_FILE_OFFSET_OLD ) ;
columns . push_back ( KEY_NXS_FILE_LEN_OLD ) ;
RetroCursor * c = db - > sqlQuery ( tableName , columns , " " , " " ) ;
if ( c )
{
bool valid = c - > moveToFirst ( ) ;
while ( ok & & valid ) {
std : : string dataFile ;
c - > getString ( 1 , dataFile ) ;
if ( ! dataFile . empty ( ) ) {
bool fileOk = true ;
// first try to find the file in the service dir
if ( RsDirUtil : : fileExists ( serviceDir + " / " + dataFile ) ) {
dataFile . insert ( 0 , serviceDir + " / " ) ;
} else if ( RsDirUtil : : fileExists ( dataFile ) ) {
// use old way for backward compatibility
//TODO: can be removed later
} else {
fileOk = false ;
std : : cerr < < " moveDataFromFileToDatabase() cannot find file " < < dataFile ;
std : : cerr < < std : : endl ;
}
if ( fileOk ) {
std : : string id ;
c - > getString ( 0 , id ) ;
uint32_t offset = c - > getInt32 ( 2 ) ;
uint32_t data_len = c - > getInt32 ( 3 ) ;
char * data = new char [ data_len ] ;
std : : ifstream istrm ( dataFile . c_str ( ) , std : : ios : : binary ) ;
istrm . seekg ( offset , std : : ios : : beg ) ;
istrm . read ( data , data_len ) ;
istrm . close ( ) ;
ContentValue cv ;
// insert new columns
cv . put ( KEY_NXS_DATA , data_len , data ) ;
cv . put ( KEY_NXS_DATA_LEN , ( int32_t ) data_len ) ;
// clear old columns
cv . put ( KEY_NXS_FILE_OLD , " " ) ;
cv . put ( KEY_NXS_FILE_OFFSET_OLD , 0 ) ;
cv . put ( KEY_NXS_FILE_LEN_OLD , 0 ) ;
ok = db - > sqlUpdate ( tableName , keyId + " =' " + id + " ' " , cv ) ;
delete [ ] data ;
if ( std : : find ( files . begin ( ) , files . end ( ) , dataFile ) = = files . end ( ) ) {
files . push_back ( dataFile ) ;
}
}
}
valid = c - > moveToNext ( ) ;
}
delete c ;
}
return ok ;
}
void RsDataService : : initialise ( bool isNewDatabase )
{
const int databaseRelease = 1 ;
int currentDatabaseRelease = 0 ;
bool ok = true ;
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
2015-08-16 07:27:04 -04:00
if ( isNewDatabase | | ! mDb - > tableExists ( DATABASE_RELEASE_TABLE_NAME ) ) {
// create table for database release
mDb - > execSQL ( " CREATE TABLE " + DATABASE_RELEASE_TABLE_NAME + " ( " +
KEY_DATABASE_RELEASE_ID + " INT PRIMARY KEY, " +
KEY_DATABASE_RELEASE + " INT); " ) ;
}
if ( isNewDatabase ) {
// create table for msg data
mDb - > execSQL ( " CREATE TABLE " + MSG_TABLE_NAME + " ( " +
KEY_MSG_ID + " TEXT PRIMARY KEY, " +
KEY_GRP_ID + " TEXT, " +
KEY_NXS_FLAGS + " INT, " +
KEY_ORIG_MSG_ID + " TEXT, " +
KEY_TIME_STAMP + " INT, " +
KEY_NXS_IDENTITY + " TEXT, " +
KEY_SIGN_SET + " BLOB, " +
KEY_NXS_DATA + " BLOB, " +
KEY_NXS_DATA_LEN + " 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, " +
KEY_NXS_SERV_STRING + " TEXT, " +
KEY_NXS_HASH + " TEXT, " +
KEY_RECV_TS + " INT); " ) ;
// create table for grp data
mDb - > execSQL ( " CREATE TABLE " + GRP_TABLE_NAME + " ( " +
KEY_GRP_ID + " TEXT PRIMARY KEY, " +
KEY_TIME_STAMP + " INT, " +
KEY_NXS_DATA + " BLOB, " +
KEY_NXS_DATA_LEN + " INT, " +
KEY_KEY_SET + " BLOB, " +
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, " +
KEY_NXS_SERV_STRING + " TEXT, " +
KEY_NXS_FLAGS + " INT, " +
KEY_GRP_AUTHEN_FLAGS + " INT, " +
KEY_GRP_SIGN_FLAGS + " INT, " +
KEY_GRP_CIRCLE_ID + " TEXT, " +
KEY_GRP_CIRCLE_TYPE + " INT, " +
KEY_GRP_INTERNAL_CIRCLE + " TEXT, " +
KEY_GRP_ORIGINATOR + " TEXT, " +
KEY_NXS_HASH + " TEXT, " +
KEY_RECV_TS + " INT, " +
KEY_PARENT_GRP_ID + " TEXT, " +
KEY_GRP_REP_CUTOFF + " INT, " +
KEY_SIGN_SET + " BLOB); " ) ;
mDb - > execSQL ( " CREATE TRIGGER " + GRP_LAST_POST_UPDATE_TRIGGER +
" INSERT ON " + MSG_TABLE_NAME +
std : : string ( " BEGIN " ) +
" UPDATE " + GRP_TABLE_NAME + " SET " + KEY_GRP_LAST_POST + " = new. "
+ KEY_RECV_TS + " WHERE " + KEY_GRP_ID + " =new. " + KEY_GRP_ID + " ; "
+ std : : string ( " END; " ) ) ;
mDb - > execSQL ( " CREATE INDEX " + MSG_INDEX_GRPID + " ON " + MSG_TABLE_NAME + " ( " + KEY_GRP_ID + " ); " ) ;
// Insert release, no need to upgrade
ContentValue cv ;
cv . put ( KEY_DATABASE_RELEASE_ID , KEY_DATABASE_RELEASE_ID_VALUE ) ;
cv . put ( KEY_DATABASE_RELEASE , databaseRelease ) ;
mDb - > sqlInsert ( DATABASE_RELEASE_TABLE_NAME , " " , cv ) ;
currentDatabaseRelease = databaseRelease ;
} else {
// check release
{
// try to select the release
std : : list < std : : string > columns ;
columns . push_back ( KEY_DATABASE_RELEASE ) ;
std : : string where ;
rs_sprintf ( where , " %s=%d " , KEY_DATABASE_RELEASE_ID . c_str ( ) , KEY_DATABASE_RELEASE_ID_VALUE ) ;
RetroCursor * c = mDb - > sqlQuery ( DATABASE_RELEASE_TABLE_NAME , columns , where , " " ) ;
if ( c ) {
ok = c - > moveToFirst ( ) ;
if ( ok ) {
currentDatabaseRelease = c - > getInt32 ( 0 ) ;
}
delete c ;
if ( ! ok ) {
// No record found ... insert the record
ContentValue cv ;
cv . put ( KEY_DATABASE_RELEASE_ID , KEY_DATABASE_RELEASE_ID_VALUE ) ;
cv . put ( KEY_DATABASE_RELEASE , currentDatabaseRelease ) ;
ok = mDb - > sqlInsert ( DATABASE_RELEASE_TABLE_NAME , " " , cv ) ;
}
} else {
ok = false ;
}
}
// Release 1
int newRelease = 1 ;
if ( ok & & currentDatabaseRelease < newRelease ) {
// Update database
std : : list < std : : string > files ;
ok = startReleaseUpdate ( newRelease ) ;
// Move data in files into database
ok = ok & & mDb - > execSQL ( " ALTER TABLE " + GRP_TABLE_NAME + " ADD COLUMN " + KEY_NXS_DATA + " BLOB; " ) ;
ok = ok & & mDb - > execSQL ( " ALTER TABLE " + GRP_TABLE_NAME + " ADD COLUMN " + KEY_NXS_DATA_LEN + " INT; " ) ;
ok = ok & & mDb - > execSQL ( " ALTER TABLE " + MSG_TABLE_NAME + " ADD COLUMN " + KEY_NXS_DATA + " BLOB; " ) ;
ok = ok & & mDb - > execSQL ( " ALTER TABLE " + MSG_TABLE_NAME + " ADD COLUMN " + KEY_NXS_DATA_LEN + " INT; " ) ;
ok = ok & & moveDataFromFileToDatabase ( mDb , mServiceDir , GRP_TABLE_NAME , KEY_GRP_ID , files ) ;
ok = ok & & moveDataFromFileToDatabase ( mDb , mServiceDir , MSG_TABLE_NAME , KEY_MSG_ID , files ) ;
// SQLite doesn't support DROP COLUMN
// ok = ok && mDb->execSQL("ALTER TABLE " + GRP_TABLE_NAME + " DROP COLUMN " + KEY_NXS_FILE_OLD + ";");
// ok = ok && mDb->execSQL("ALTER TABLE " + GRP_TABLE_NAME + " DROP COLUMN " + KEY_NXS_FILE_OFFSET_OLD + ";");
// ok = ok && mDb->execSQL("ALTER TABLE " + GRP_TABLE_NAME + " DROP COLUMN " + KEY_NXS_FILE_LEN_OLD + ";");
// ok = ok && mDb->execSQL("ALTER TABLE " + MSG_TABLE_NAME + " DROP COLUMN " + KEY_NXS_FILE_OLD + ";");
// ok = ok && mDb->execSQL("ALTER TABLE " + MSG_TABLE_NAME + " DROP COLUMN " + KEY_NXS_FILE_OFFSET_OLD + ";");
// ok = ok && mDb->execSQL("ALTER TABLE " + MSG_TABLE_NAME + " DROP COLUMN " + KEY_NXS_FILE_LEN_OLD + ";");
ok = finishReleaseUpdate ( newRelease , ok ) ;
if ( ok ) {
// Remove transfered files
std : : list < std : : string > : : const_iterator file ;
for ( file = files . begin ( ) ; file ! = files . end ( ) ; + + file ) {
remove ( file - > c_str ( ) ) ;
}
currentDatabaseRelease = newRelease ;
}
}
}
if ( ok ) {
std : : cerr < < " Database " < < mDbName < < " release " < < currentDatabaseRelease < < " successfully initialised. " < < std : : endl ;
} else {
std : : cerr < < " Database " < < mDbName < < " initialisation failed. " < < std : : endl ;
}
}
bool RsDataService : : startReleaseUpdate ( int release )
{
// Update database
std : : cerr < < " Database " < < mDbName < < " update to release " < < release < < " . " < < std : : endl ;
return mDb - > beginTransaction ( ) ;
}
bool RsDataService : : finishReleaseUpdate ( int release , bool result )
{
if ( result ) {
std : : string where ;
rs_sprintf ( where , " %s=%d " , KEY_DATABASE_RELEASE_ID . c_str ( ) , KEY_DATABASE_RELEASE_ID_VALUE ) ;
ContentValue cv ;
cv . put ( KEY_DATABASE_RELEASE , release ) ;
result = mDb - > sqlUpdate ( DATABASE_RELEASE_TABLE_NAME , where , cv ) ;
}
if ( result ) {
result = mDb - > commitTransaction ( ) ;
} else {
result = mDb - > rollbackTransaction ( ) ;
}
if ( result ) {
std : : cerr < < " Database " < < mDbName < < " successfully updated to release " < < release < < " . " < < std : : endl ;
} else {
std : : cerr < < " Database " < < mDbName < < " update to release " < < release < < " failed. " < < std : : endl ;
}
2012-05-21 18:07:43 -04:00
2015-08-16 07:27:04 -04:00
return result ;
2012-05-24 18:38:59 -04:00
}
2012-05-21 18:07:43 -04:00
2018-01-09 18:10:08 -05:00
RsGxsGrpMetaData * RsDataService : : locked_getGrpMeta ( RetroCursor & c , int colOffset , bool use_cache )
2012-06-26 15:52:01 -04:00
{
2014-04-27 09:14:07 -04:00
# ifdef RS_DATA_SERVICE_DEBUG
2016-10-29 11:26:08 -04:00
std : : cerr < < " RsDataService::locked_getGrpMeta() " ;
std : : cerr < < std : : endl ;
2014-04-27 09:14:07 -04:00
# endif
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 ;
2015-09-05 06:46:18 -04:00
c . getString ( mColGrpMeta_GrpId + colOffset , tempId ) ;
2018-01-09 18:10:08 -05:00
RsGxsGrpMetaData * grpMeta ;
RsGxsGroupId grpId ( tempId ) ;
if ( use_cache )
{
auto it = mGrpMetaDataCache . find ( grpId ) ;
if ( it ! = mGrpMetaDataCache . end ( ) )
grpMeta = it - > second ;
else
{
grpMeta = new RsGxsGrpMetaData ( ) ;
mGrpMetaDataCache [ grpId ] = grpMeta ;
}
}
else
grpMeta = new RsGxsGrpMetaData ( ) ;
2014-04-20 12:34:26 -04:00
grpMeta - > mGroupId = RsGxsGroupId ( tempId ) ;
2015-09-05 06:46:18 -04:00
c . getString ( mColGrpMeta_NxsIdentity + colOffset , tempId ) ;
2014-04-20 12:34:26 -04:00
grpMeta - > mAuthorId = RsGxsId ( tempId ) ;
2014-04-27 09:14:07 -04:00
2015-09-05 06:46:18 -04:00
c . getString ( mColGrpMeta_Name + colOffset , grpMeta - > mGroupName ) ;
c . getString ( mColGrpMeta_OrigGrpId + colOffset , tempId ) ;
2014-04-20 12:34:26 -04:00
grpMeta - > mOrigGrpId = RsGxsGroupId ( tempId ) ;
2015-09-05 06:46:18 -04:00
c . getString ( mColGrpMeta_ServString + colOffset , grpMeta - > mServiceString ) ;
2014-03-17 16:56:06 -04:00
std : : string temp ;
2015-09-05 06:46:18 -04:00
c . getString ( mColGrpMeta_NxsHash + colOffset , temp ) ;
2014-04-20 12:34:26 -04:00
grpMeta - > mHash = RsFileHash ( temp ) ;
2015-09-05 06:46:18 -04:00
grpMeta - > mReputationCutOff = c . getInt32 ( mColGrpMeta_RepCutoff + colOffset ) ;
grpMeta - > mSignFlags = c . getInt32 ( mColGrpMeta_SignFlags + colOffset ) ;
2012-06-03 14:09:38 -04:00
2015-09-05 06:46:18 -04:00
grpMeta - > mPublishTs = c . getInt32 ( mColGrpMeta_TimeStamp + colOffset ) ;
grpMeta - > mGroupFlags = c . getInt32 ( mColGrpMeta_NxsFlags + colOffset ) ;
grpMeta - > mGrpSize = c . getInt32 ( mColGrpMeta_NxsDataLen + colOffset ) ;
2012-05-21 18:07:43 -04:00
2012-06-26 15:52:01 -04:00
offset = 0 ; data = NULL ; data_len = 0 ;
2015-09-05 06:46:18 -04:00
data = ( char * ) c . getData ( mColGrpMeta_KeySet + colOffset , 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 ) ;
2016-10-29 11:26:08 -04:00
else
grpMeta - > keys . TlvClear ( ) ;
2012-06-03 14:09:38 -04:00
2012-06-26 15:52:01 -04:00
// local meta
2015-09-05 06:46:18 -04:00
grpMeta - > mSubscribeFlags = c . getInt32 ( mColGrpMeta_SubscrFlag + colOffset ) ;
grpMeta - > mPop = c . getInt32 ( mColGrpMeta_Pop + colOffset ) ;
grpMeta - > mVisibleMsgCount = c . getInt32 ( mColGrpMeta_MsgCount + colOffset ) ;
grpMeta - > mLastPost = c . getInt32 ( mColGrpMeta_LastPost + colOffset ) ;
grpMeta - > mGroupStatus = c . getInt32 ( mColGrpMeta_Status + colOffset ) ;
2012-05-21 18:07:43 -04:00
2015-09-05 06:46:18 -04:00
c . getString ( mColGrpMeta_CircleId + colOffset , tempId ) ;
2014-04-20 12:34:26 -04:00
grpMeta - > mCircleId = RsGxsCircleId ( tempId ) ;
2015-09-05 06:46:18 -04:00
grpMeta - > mCircleType = c . getInt32 ( mColGrpMeta_CircleType + colOffset ) ;
c . getString ( mColGrpMeta_InternCircle + colOffset , tempId ) ;
2014-04-20 12:34:26 -04:00
grpMeta - > mInternalCircle = RsGxsCircleId ( tempId ) ;
2014-03-17 16:56:06 -04:00
2015-09-05 06:46:18 -04:00
std : : string s ; c . getString ( mColGrpMeta_Originator + colOffset , s ) ;
2014-03-17 16:56:06 -04:00
grpMeta - > mOriginator = RsPeerId ( s ) ;
2015-09-05 06:46:18 -04:00
grpMeta - > mAuthenFlags = c . getInt32 ( mColGrpMeta_AuthenFlags + colOffset ) ;
grpMeta - > mRecvTS = c . getInt32 ( mColGrpMeta_RecvTs + colOffset ) ;
2012-12-02 14:40:17 -05:00
2012-06-26 15:52:01 -04:00
2015-08-23 13:05:37 -04:00
c . getString ( mColGrpMeta_ParentGrpId , tempId ) ;
2014-04-20 12:34:26 -04:00
grpMeta - > mParentGrpId = RsGxsGroupId ( tempId ) ;
2014-01-19 18:38:48 -05:00
2018-03-01 05:45:54 -05:00
// make sure that flags and keys are actually consistent
bool have_private_admin_key = false ;
bool have_private_publish_key = false ;
for ( auto mit = grpMeta - > keys . private_keys . begin ( ) ; mit ! = grpMeta - > keys . private_keys . end ( ) ; + + mit )
{
if ( mit - > second . keyFlags = = ( RSTLV_KEY_DISTRIB_PUBLISH | RSTLV_KEY_TYPE_FULL ) ) have_private_publish_key = true ;
if ( mit - > second . keyFlags = = ( RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL ) ) have_private_admin_key = true ;
}
if ( have_private_admin_key & & ! ( grpMeta - > mSubscribeFlags & GXS_SERV : : GROUP_SUBSCRIBE_ADMIN ) )
{
std : : cerr < < " (WW) inconsistency in group " < < grpMeta - > mGroupId < < " : group does not have flag ADMIN but an admin key was found. Updating the flags. " < < std : : endl ;
grpMeta - > mSubscribeFlags | = GXS_SERV : : GROUP_SUBSCRIBE_ADMIN ;
}
if ( ! have_private_admin_key & & ( grpMeta - > mSubscribeFlags & GXS_SERV : : GROUP_SUBSCRIBE_ADMIN ) )
{
std : : cerr < < " (WW) inconsistency in group " < < grpMeta - > mGroupId < < " : group has flag ADMIN but no admin key found. Updating the flags. " < < std : : endl ;
grpMeta - > mSubscribeFlags & = ~ GXS_SERV : : GROUP_SUBSCRIBE_ADMIN ;
}
if ( have_private_publish_key & & ! ( grpMeta - > mSubscribeFlags & GXS_SERV : : GROUP_SUBSCRIBE_PUBLISH ) )
{
std : : cerr < < " (WW) inconsistency in group " < < grpMeta - > mGroupId < < " : group does not have flag PUBLISH but an admin key was found. Updating the flags. " < < std : : endl ;
grpMeta - > mSubscribeFlags | = GXS_SERV : : GROUP_SUBSCRIBE_PUBLISH ;
}
if ( ! have_private_publish_key & & ( grpMeta - > mSubscribeFlags & GXS_SERV : : GROUP_SUBSCRIBE_PUBLISH ) )
{
std : : cerr < < " (WW) inconsistency in group " < < grpMeta - > mGroupId < < " : group has flag PUBLISH but no admin key found. Updating the flags. " < < std : : endl ;
grpMeta - > mSubscribeFlags & = ~ GXS_SERV : : GROUP_SUBSCRIBE_PUBLISH ;
}
2012-06-26 15:52:01 -04:00
if ( ok )
return grpMeta ;
else
2018-01-07 15:41:41 -05:00
{
2018-01-09 18:10:08 -05:00
if ( ! use_cache )
delete grpMeta ;
2018-01-07 15:41:41 -05:00
return NULL ;
}
2012-06-26 15:52:01 -04:00
}
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
2015-08-23 13:05:37 -04:00
c . getStringT < RsGxsGroupId > ( mColGrp_GrpId , grp - > grpId ) ;
2014-03-17 16:56:06 -04:00
ok & = ! grp - > grpId . isNull ( ) ;
2012-06-26 15:52:01 -04:00
offset = 0 ; data_len = 0 ;
if ( ok ) {
2015-08-23 13:05:37 -04:00
data = ( char * ) c . getData ( mColGrp_MetaData , data_len ) ;
2012-06-26 15:52:01 -04:00
if ( data )
grp - > meta . GetTlv ( data , data_len , & offset ) ;
2012-05-21 18:07:43 -04:00
}
2015-08-16 07:27:04 -04:00
/* now retrieve grp data */
offset = 0 ; data_len = 0 ;
2012-05-21 18:07:43 -04:00
if ( ok ) {
2015-08-23 13:05:37 -04:00
data = ( char * ) c . getData ( mColGrp_NxsData , data_len ) ;
2015-08-16 07:27:04 -04:00
if ( data )
ok & = grp - > grp . GetTlv ( data , data_len , & offset ) ;
2012-05-21 18:07:43 -04:00
}
if ( ok )
return grp ;
else
delete grp ;
return NULL ;
}
2015-09-05 06:46:18 -04:00
RsGxsMsgMetaData * RsDataService : : locked_getMsgMeta ( RetroCursor & c , int colOffset )
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 ;
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_GrpId + colOffset , gId ) ;
2014-03-17 16:56:06 -04:00
msgMeta - > mGroupId = RsGxsGroupId ( gId ) ;
std : : string temp ;
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_MsgId + colOffset , 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
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_OrigMsgId + colOffset , temp ) ;
2014-04-20 12:34:26 -04:00
msgMeta - > mOrigMsgId = RsGxsMessageId ( temp ) ;
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_NxsIdentity + colOffset , temp ) ;
2014-04-20 12:34:26 -04:00
msgMeta - > mAuthorId = RsGxsId ( temp ) ;
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_Name + colOffset , msgMeta - > mMsgName ) ;
c . getString ( mColMsgMeta_NxsServString + colOffset , msgMeta - > mServiceString ) ;
2014-03-17 16:56:06 -04:00
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_NxsHash + colOffset , temp ) ;
2014-04-20 12:34:26 -04:00
msgMeta - > mHash = RsFileHash ( temp ) ;
2015-09-05 06:46:18 -04:00
msgMeta - > recvTS = c . getInt32 ( mColMsgMeta_RecvTs + colOffset ) ;
2012-10-01 16:57:56 -04:00
offset = 0 ;
2015-09-05 06:46:18 -04:00
data = ( char * ) c . getData ( mColMsgMeta_SignSet + colOffset , data_len ) ;
2012-10-01 16:57:56 -04:00
msgMeta - > signSet . GetTlv ( data , data_len , & offset ) ;
2015-09-05 06:46:18 -04:00
msgMeta - > mMsgSize = c . getInt32 ( mColMsgMeta_NxsDataLen + colOffset ) ;
2012-05-21 18:07:43 -04:00
2015-09-05 06:46:18 -04:00
msgMeta - > mMsgFlags = c . getInt32 ( mColMsgMeta_NxsFlags + colOffset ) ;
msgMeta - > mPublishTs = c . getInt32 ( mColMsgMeta_TimeStamp + colOffset ) ;
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
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_MsgThreadId + colOffset , temp ) ;
2014-04-20 12:34:26 -04:00
msgMeta - > mThreadId = RsGxsMessageId ( temp ) ;
2015-09-05 06:46:18 -04:00
c . getString ( mColMsgMeta_MsgParentId + colOffset , temp ) ;
2014-04-20 12:34:26 -04:00
msgMeta - > mParentId = RsGxsMessageId ( temp ) ;
2012-06-26 15:52:01 -04:00
// local meta
2015-09-05 06:46:18 -04:00
msgMeta - > mMsgStatus = c . getInt32 ( mColMsgMeta_MsgStatus + colOffset ) ;
msgMeta - > mChildTs = c . getInt32 ( mColMsgMeta_ChildTs + colOffset ) ;
2012-06-26 15:52:01 -04:00
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 ;
2015-08-23 13:05:37 -04:00
c . getStringT < RsGxsGroupId > ( mColMsg_GrpId , msg - > grpId ) ;
2014-03-17 16:56:06 -04:00
std : : string temp ;
2015-08-23 13:05:37 -04:00
c . getString ( mColMsg_MsgId , 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 ) {
2015-08-23 13:05:37 -04:00
data = ( char * ) c . getData ( mColMsg_MetaData , data_len ) ;
2012-06-26 15:52:01 -04:00
if ( data )
msg - > meta . GetTlv ( data , data_len , & offset ) ;
}
2015-08-16 07:27:04 -04:00
/* now retrieve msg data */
offset = 0 ; data_len = 0 ;
2012-05-21 18:07:43 -04:00
if ( ok ) {
2015-08-23 13:05:37 -04:00
data = ( char * ) c . getData ( mColMsg_NxsData , data_len ) ;
2015-08-16 07:27:04 -04:00
if ( data )
ok & = msg - > msg . GetTlv ( data , data_len , & offset ) ;
2012-05-21 18:07:43 -04:00
}
if ( ok )
return msg ;
else
delete msg ;
return NULL ;
}
2017-07-24 18:07:53 -04:00
int RsDataService : : storeMessage ( const std : : list < RsNxsMsg * > & msg )
2012-06-26 15:52:01 -04:00
{
2012-10-15 14:52:47 -04:00
RsStackMutex stack ( mDbMutex ) ;
2012-06-26 15:52:01 -04:00
// start a transaction
2015-08-15 11:06:06 -04:00
mDb - > beginTransaction ( ) ;
2012-05-21 18:07:43 -04:00
2017-07-24 18:07:53 -04:00
for ( std : : list < RsNxsMsg * > : : const_iterator mit = msg . begin ( ) ; mit ! = msg . end ( ) ; + + mit )
2016-08-02 10:25:00 -04:00
{
2017-07-24 18:07:53 -04:00
RsNxsMsg * msgPtr = * mit ;
RsGxsMsgMetaData * msgMetaPtr = msgPtr - > metaData ;
assert ( msgMetaPtr ! = NULL ) ;
2012-05-21 18:07:43 -04:00
2016-10-29 11:26:08 -04:00
# ifdef RS_DATA_SERVICE_DEBUG
2016-08-02 10:25:00 -04:00
std : : cerr < < " RsDataService::storeMessage() " ;
std : : cerr < < " GroupId: " < < msgMetaPtr - > mGroupId . toStdString ( ) ;
std : : cerr < < " MessageId: " < < msgMetaPtr - > mMsgId . toStdString ( ) ;
std : : cerr < < std : : endl ;
2014-05-04 08:48:42 -04:00
# 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 ) )
2016-08-02 10:25:00 -04:00
{
std : : cerr < < " RsDataService::storeMessage() ERROR invalid size " ;
std : : cerr < < std : : endl ;
continue ;
}
2013-02-07 18:04:16 -05:00
2012-05-21 18:07:43 -04:00
ContentValue cv ;
2012-06-26 15:52:01 -04:00
2015-08-16 07:27:04 -04:00
uint32_t dataLen = msgPtr - > msg . TlvSize ( ) ;
char msgData [ dataLen ] ;
uint32_t offset = 0 ;
msgPtr - > msg . SetTlv ( msgData , dataLen , & offset ) ;
cv . put ( KEY_NXS_DATA , dataLen , msgData ) ;
cv . put ( KEY_NXS_DATA_LEN , ( int32_t ) dataLen ) ;
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
2014-05-04 08:48:42 -04:00
if ( ! mDb - > sqlInsert ( MSG_TABLE_NAME , " " , cv ) )
2016-08-02 10:25:00 -04:00
{
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 ;
}
// This is needed so that mLastPost is correctly updated in the group meta when it is re-loaded.
locked_clearGrpMetaCache ( msgMetaPtr - > mGroupId ) ;
2012-05-21 18:07:43 -04:00
}
2012-06-26 15:52:01 -04:00
// finish transaction
2015-08-15 11:06:06 -04:00
bool ret = mDb - > commitTransaction ( ) ;
2012-09-27 17:42:00 -04:00
return ret ;
2012-05-21 18:07:43 -04:00
}
2013-02-07 18:04:16 -05:00
bool RsDataService : : validSize ( RsNxsMsg * msg ) const
{
2016-10-29 11:26:08 -04:00
if ( ( msg - > msg . TlvSize ( ) + msg - > meta . TlvSize ( ) ) < = GXS_MAX_ITEM_SIZE ) return true ;
2013-02-07 18:04:16 -05:00
2016-10-29 11:26:08 -04:00
return false ;
2013-02-07 18:04:16 -05:00
}
2012-05-21 18:07:43 -04:00
2017-07-24 18:07:53 -04:00
int RsDataService : : storeGroup ( const std : : list < RsNxsGrp * > & grp )
2012-06-26 15:52:01 -04:00
{
2012-10-15 14:52:47 -04:00
RsStackMutex stack ( mDbMutex ) ;
2012-06-26 15:52:01 -04:00
// begin transaction
2015-08-15 11:06:06 -04:00
mDb - > beginTransaction ( ) ;
2012-05-21 18:07:43 -04:00
2017-07-24 18:07:53 -04:00
for ( std : : list < RsNxsGrp * > : : const_iterator sit = grp . begin ( ) ; sit ! = grp . end ( ) ; + + sit )
{
RsNxsGrp * grpPtr = * sit ;
RsGxsGrpMetaData * grpMetaPtr = grpPtr - > metaData ;
2012-05-21 18:07:43 -04:00
2017-07-24 18:07:53 -04:00
assert ( grpMetaPtr ! = NULL ) ;
2012-05-21 18:07:43 -04:00
2017-07-24 18:07:53 -04:00
// if data is larger than max item size do not add
if ( ! validSize ( grpPtr ) ) continue ;
2013-02-07 18:04:16 -05:00
2016-10-29 11:26:08 -04:00
# ifdef RS_DATA_SERVICE_DEBUG
2017-07-24 18:07:53 -04:00
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 ;
2014-05-04 08:48:42 -04:00
# endif
2017-07-24 18:07:53 -04:00
/*!
* STORE data , data len ,
* grpId , flags , publish time stamp , identity ,
* id signature , admin signatue , key set , last posting ts
* and meta data
* */
ContentValue cv ;
uint32_t dataLen = grpPtr - > grp . TlvSize ( ) ;
char grpData [ dataLen ] ;
uint32_t offset = 0 ;
grpPtr - > grp . SetTlv ( grpData , dataLen , & offset ) ;
cv . put ( KEY_NXS_DATA , dataLen , grpData ) ;
cv . put ( KEY_NXS_DATA_LEN , ( int32_t ) dataLen ) ;
cv . put ( KEY_GRP_ID , grpPtr - > grpId . toStdString ( ) ) ;
cv . put ( KEY_GRP_NAME , grpMetaPtr - > mGroupName ) ;
cv . put ( KEY_ORIG_GRP_ID , grpMetaPtr - > mOrigGrpId . toStdString ( ) ) ;
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 ) ;
cv . put ( KEY_GRP_CIRCLE_ID , grpMetaPtr - > mCircleId . toStdString ( ) ) ;
cv . put ( KEY_GRP_CIRCLE_TYPE , ( int32_t ) grpMetaPtr - > mCircleType ) ;
cv . put ( KEY_GRP_INTERNAL_CIRCLE , grpMetaPtr - > mInternalCircle . toStdString ( ) ) ;
cv . put ( KEY_GRP_ORIGINATOR , grpMetaPtr - > mOriginator . toStdString ( ) ) ;
cv . put ( KEY_GRP_AUTHEN_FLAGS , ( int32_t ) grpMetaPtr - > mAuthenFlags ) ;
cv . put ( KEY_PARENT_GRP_ID , grpMetaPtr - > mParentGrpId . toStdString ( ) ) ;
cv . put ( KEY_NXS_HASH , grpMetaPtr - > mHash . toStdString ( ) ) ;
cv . put ( KEY_RECV_TS , ( int32_t ) grpMetaPtr - > mRecvTS ) ;
cv . put ( KEY_GRP_REP_CUTOFF , ( int32_t ) grpMetaPtr - > mReputationCutOff ) ;
cv . put ( KEY_NXS_IDENTITY , grpMetaPtr - > mAuthorId . toStdString ( ) ) ;
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 - > mVisibleMsgCount ) ;
cv . put ( KEY_GRP_STATUS , ( int32_t ) grpMetaPtr - > mGroupStatus ) ;
cv . put ( KEY_GRP_LAST_POST , ( int32_t ) grpMetaPtr - > mLastPost ) ;
2018-01-09 18:10:08 -05:00
locked_updateGrpMetaCache ( * grpMetaPtr ) ;
2017-07-24 18:07:53 -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-06-26 15:52:01 -04:00
// finish transaction
2015-08-15 11:06:06 -04:00
bool ret = mDb - > commitTransaction ( ) ;
2012-09-27 17:42:00 -04:00
return ret ;
2012-05-21 18:07:43 -04:00
}
2018-01-09 18:10:08 -05:00
void RsDataService : : locked_updateGrpMetaCache ( const RsGxsGrpMetaData & meta )
{
auto it = mGrpMetaDataCache . find ( meta . mGroupId ) ;
if ( it ! = mGrpMetaDataCache . end ( ) )
* ( it - > second ) = meta ;
}
2016-07-22 17:49:07 -04:00
void RsDataService : : locked_clearGrpMetaCache ( const RsGxsGroupId & gid )
{
2018-01-15 17:13:42 -05:00
time_t now = time ( NULL ) ;
2018-01-09 18:10:08 -05:00
auto it = mGrpMetaDataCache . find ( gid ) ;
2018-01-07 15:41:41 -05:00
2018-01-15 17:13:42 -05:00
// We dont actually delete the item, because it might be used by a calling client.
// In this case, the memory will not be used for long, so we keep it into a list for a safe amount
// of time and delete it later. Using smart pointers here would be more elegant, but that would need
// to be implemented thread safe, which is difficult in this case.
2018-01-07 15:41:41 -05:00
if ( it ! = mGrpMetaDataCache . end ( ) )
{
2018-02-23 08:28:22 -05:00
# ifdef RS_DATA_SERVICE_DEBUG
2018-01-15 17:13:42 -05:00
std : : cerr < < " (II) moving database cache entry " < < ( void * ) ( * it ) . second < < " to dead list. " < < std : : endl ;
2018-02-23 08:28:22 -05:00
# endif
2018-01-15 17:13:42 -05:00
mOldCachedItems . push_back ( std : : make_pair ( now , it - > second ) ) ;
2018-01-07 15:41:41 -05:00
mGrpMetaDataCache . erase ( it ) ;
mGrpMetaDataCache_ContainsAllDatabase = false ;
}
2018-01-15 17:13:42 -05:00
// We also take that opportunity to delete old entries.
auto it2 ( mOldCachedItems . begin ( ) ) ;
while ( it2 ! = mOldCachedItems . end ( ) & & ( * it2 ) . first + CACHE_ENTRY_GRACE_PERIOD < now )
{
2018-02-23 08:28:22 -05:00
# ifdef RS_DATA_SERVICE_DEBUG
2018-01-15 17:13:42 -05:00
std : : cerr < < " (II) deleting old GXS database cache entry " < < ( void * ) ( * it2 ) . second < < " , " < < now - ( * it2 ) . first < < " seconds old. " < < std : : endl ;
2018-02-23 08:28:22 -05:00
# endif
2018-01-15 17:13:42 -05:00
delete ( * it2 ) . second ;
it2 = mOldCachedItems . erase ( it2 ) ;
}
2016-07-22 17:49:07 -04:00
}
2017-07-24 18:07:53 -04:00
int RsDataService : : updateGroup ( const std : : list < RsNxsGrp * > & grp )
2013-09-11 16:42:56 -04:00
{
RsStackMutex stack ( mDbMutex ) ;
// begin transaction
2015-08-15 11:06:06 -04:00
mDb - > beginTransaction ( ) ;
2013-09-11 16:42:56 -04:00
2017-07-24 18:07:53 -04:00
for ( std : : list < RsNxsGrp * > : : const_iterator sit = grp . begin ( ) ; sit ! = grp . end ( ) ; + + sit )
2013-09-11 16:42:56 -04:00
{
2017-07-24 18:07:53 -04:00
RsNxsGrp * grpPtr = * sit ;
RsGxsGrpMetaData * grpMetaPtr = grpPtr - > metaData ;
assert ( grpMetaPtr ! = NULL ) ;
2013-09-11 16:42:56 -04:00
// if data is larger than max item size do not add
if ( ! validSize ( grpPtr ) ) continue ;
/*!
2015-08-16 07:27:04 -04:00
* STORE data , data len ,
2013-09-11 16:42:56 -04:00
* grpId , flags , publish time stamp , identity ,
* id signature , admin signatue , key set , last posting ts
* and meta data
* */
ContentValue cv ;
2015-08-16 07:27:04 -04:00
uint32_t dataLen = grpPtr - > grp . TlvSize ( ) ;
char grpData [ dataLen ] ;
uint32_t offset = 0 ;
grpPtr - > grp . SetTlv ( grpData , dataLen , & offset ) ;
cv . put ( KEY_NXS_DATA , dataLen , grpData ) ;
cv . put ( KEY_NXS_DATA_LEN , ( int32_t ) dataLen ) ;
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 ( ) ) ;
2016-02-24 16:51:25 -05:00
cv . put ( KEY_RECV_TS , ( int32_t ) grpMetaPtr - > mRecvTS ) ;
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 ) ;
2014-12-16 01:54:15 -05:00
cv . put ( KEY_MSG_COUNT , ( int32_t ) grpMetaPtr - > mVisibleMsgCount ) ;
2013-09-11 16:42:56 -04:00
cv . put ( KEY_GRP_STATUS , ( int32_t ) grpMetaPtr - > mGroupStatus ) ;
cv . put ( KEY_GRP_LAST_POST , ( int32_t ) grpMetaPtr - > mLastPost ) ;
2014-03-17 16:56:06 -04:00
mDb - > sqlUpdate ( GRP_TABLE_NAME , " grpId=' " + grpPtr - > grpId . toStdString ( ) + " ' " , cv ) ;
2016-07-22 17:49:07 -04:00
2018-01-09 18:10:08 -05:00
locked_updateGrpMetaCache ( * grpMetaPtr ) ;
2013-09-11 16:42:56 -04:00
}
// finish transaction
2015-08-15 11:06:06 -04:00
bool ret = mDb - > commitTransaction ( ) ;
2013-09-11 16:42:56 -04:00
return ret ;
}
2014-10-05 15:14:05 -04:00
int RsDataService : : updateGroupKeys ( const RsGxsGroupId & grpId , const RsTlvSecurityKeySet & keys , uint32_t subscribe_flags )
{
RsStackMutex stack ( mDbMutex ) ;
// begin transaction
2015-08-15 11:06:06 -04:00
mDb - > beginTransaction ( ) ;
2014-10-05 15:14:05 -04:00
/*!
* STORE key set
* */
ContentValue cv ;
//cv.put(KEY_NXS_FLAGS, (int32_t)grpMetaPtr->mGroupFlags); ?
uint32_t offset = 0 ;
char keySetData [ keys . TlvSize ( ) ] ;
keys . SetTlv ( keySetData , keys . TlvSize ( ) , & offset ) ;
cv . put ( KEY_KEY_SET , keys . TlvSize ( ) , keySetData ) ;
cv . put ( KEY_GRP_SUBCR_FLAG , ( int32_t ) subscribe_flags ) ;
mDb - > sqlUpdate ( GRP_TABLE_NAME , " grpId=' " + grpId . toStdString ( ) + " ' " , cv ) ;
// finish transaction
2015-08-15 11:06:06 -04:00
return mDb - > commitTransaction ( ) ;
2014-10-05 15:14:05 -04:00
}
2013-09-11 16:42:56 -04:00
2013-02-07 18:04:16 -05:00
bool RsDataService : : validSize ( RsNxsGrp * grp ) const
{
2016-10-29 11:26:08 -04:00
if ( ( grp - > grp . TlvSize ( ) + grp - > meta . TlvSize ( ) ) < = GXS_MAX_ITEM_SIZE ) return true ;
return false ;
2013-02-07 18:04:16 -05:00
}
2015-08-11 17:19:37 -04:00
int RsDataService : : retrieveNxsGrps ( std : : map < RsGxsGroupId , RsNxsGrp * > & grp , bool withMeta , bool /* cache */ )
{
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-27 14:22:31 -05:00
rstime : : RsScopeTimer timer ( " " ) ;
2015-08-11 17:19:37 -04:00
int resultCount = 0 ;
int requestedGroups = grp . size ( ) ;
# endif
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 ) ;
2015-09-05 06:46:18 -04:00
RetroCursor * c = mDb - > sqlQuery ( GRP_TABLE_NAME , withMeta ? mGrpColumnsWithMeta : mGrpColumns , " " , " " ) ;
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
2015-09-05 06:46:18 -04:00
locked_retrieveGroups ( c , grps , withMeta ? mColGrp_WithMetaOffset : 0 ) ;
2012-10-15 14:52:47 -04:00
std : : vector < RsNxsGrp * > : : iterator vit = grps . begin ( ) ;
2012-07-14 13:59:54 -04:00
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount = grps . size ( ) ;
# endif
2014-10-24 18:07:26 -04:00
for ( ; vit ! = grps . end ( ) ; + + vit )
2012-10-15 14:52:47 -04:00
{
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
2014-10-24 18:07:26 -04:00
for ( ; mit ! = grp . end ( ) ; + + mit )
2012-10-15 14:52:47 -04:00
{
2014-03-17 16:56:06 -04:00
const RsGxsGroupId & grpId = mit - > first ;
2015-09-05 06:46:18 -04:00
RetroCursor * c = mDb - > sqlQuery ( GRP_TABLE_NAME , withMeta ? mGrpColumnsWithMeta : mGrpColumns , " 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 ;
2015-09-05 06:46:18 -04:00
locked_retrieveGroups ( c , grps , withMeta ? mColGrp_WithMetaOffset : 0 ) ;
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 ;
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
+ + resultCount ;
# endif
2012-10-15 14:52:47 -04:00
} 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
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2015-08-13 05:03:57 -04:00
std : : cerr < < " RsDataService::retrieveNxsGrps() " < < mDbName < < " , Requests: " < < requestedGroups < < " , Results: " < < resultCount < < " , Time: " < < timer . duration ( ) < < std : : endl ;
2015-08-11 17:19:37 -04:00
# endif
2012-10-15 14:52:47 -04:00
return 1 ;
2012-07-14 13:59:54 -04:00
}
2015-09-05 06:46:18 -04:00
void RsDataService : : locked_retrieveGroups ( RetroCursor * c , std : : vector < RsNxsGrp * > & grps , int metaOffset ) {
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 )
{
2015-09-05 06:46:18 -04:00
if ( metaOffset ) {
2018-01-09 18:10:08 -05:00
g - > metaData = locked_getGrpMeta ( * c , metaOffset , false ) ;
2015-09-05 06:46:18 -04:00
}
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
{
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-27 14:22:31 -05:00
rstime : : RsScopeTimer timer ( " " ) ;
2015-08-11 17:19:37 -04:00
int resultCount = 0 ;
# endif
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
2014-10-24 18:07:26 -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
// 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 ) ;
2015-09-05 06:46:18 -04:00
RetroCursor * c = mDb - > sqlQuery ( MSG_TABLE_NAME , withMeta ? mMsgColumnsWithMeta : mMsgColumns , KEY_GRP_ID + " =' " + grpId . toStdString ( ) + " ' " , " " ) ;
2012-05-21 18:07:43 -04:00
2012-06-26 15:52:01 -04:00
if ( c )
2015-08-11 17:19:37 -04:00
{
2015-09-05 06:46:18 -04:00
locked_retrieveMessages ( c , msgSet , withMeta ? mColMsg_WithMetaOffset : 0 ) ;
2015-08-11 17:19:37 -04:00
}
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
2014-10-24 18:07:26 -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 ) ;
2015-09-05 06:46:18 -04:00
RetroCursor * c = mDb - > sqlQuery ( MSG_TABLE_NAME , withMeta ? mMsgColumnsWithMeta : mMsgColumns , KEY_GRP_ID + " =' " + grpId . toStdString ( )
2014-03-17 16:56:06 -04:00
+ " ' AND " + KEY_MSG_ID + " =' " + msgId . toStdString ( ) + " ' " , " " ) ;
2012-07-05 17:26:14 -04:00
if ( c )
2015-08-11 17:19:37 -04:00
{
2015-09-05 06:46:18 -04:00
locked_retrieveMessages ( c , msgSet , withMeta ? mColMsg_WithMetaOffset : 0 ) ;
2015-08-11 17:19:37 -04:00
}
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
}
2015-09-07 16:27:04 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount + = msgSet . size ( ) ;
# endif
2012-08-19 18:15:37 -04:00
2012-06-26 15:52:01 -04:00
msg [ grpId ] = msgSet ;
2012-08-19 18:15:37 -04:00
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
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2015-08-13 09:34:44 -04:00
std : : cerr < < " RsDataService::retrieveNxsMsgs() " < < mDbName < < " , Requests: " < < reqIds . size ( ) < < " , Results: " < < resultCount < < " , Time: " < < timer . duration ( ) < < std : : endl ;
2015-08-11 17:19:37 -04:00
# endif
2012-08-19 18:15:37 -04:00
return 1 ;
2012-05-21 18:07:43 -04:00
}
2015-09-05 06:46:18 -04:00
void RsDataService : : locked_retrieveMessages ( RetroCursor * c , std : : vector < RsNxsMsg * > & msgs , int metaOffset )
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 ) {
2015-09-05 06:46:18 -04:00
if ( metaOffset ) {
m - > metaData = locked_getMsgMeta ( * c , metaOffset ) ;
}
2015-08-11 17:19:37 -04:00
msgs . push_back ( m ) ;
2012-06-26 15:52:01 -04:00
}
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 ) ;
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-27 14:22:31 -05:00
rstime : : RsScopeTimer timer ( " " ) ;
2015-08-11 17:19:37 -04:00
int resultCount = 0 ;
# endif
2012-09-20 13:18:26 -04:00
GxsMsgReq : : const_iterator mit = reqIds . begin ( ) ;
2012-05-21 18:07:43 -04:00
2014-10-24 18:07:26 -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 ( ) ) {
2015-08-23 13:05:37 -04:00
RetroCursor * c = mDb - > sqlQuery ( MSG_TABLE_NAME , mMsgMetaColumns , KEY_GRP_ID + " =' " + grpId . toStdString ( ) + " ' " , " " ) ;
2012-08-19 18:15:37 -04:00
2015-08-11 17:19:37 -04:00
if ( c )
{
locked_retrieveMsgMeta ( c , metaSet ) ;
2016-07-01 23:38:10 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
std : : cerr < < " Retrieving (all) Msg metadata grpId= " < < grpId < < " , " < < std : : dec < < metaSet . size ( ) < < " messages " < < std : : endl ;
# endif
2015-08-11 17:19:37 -04:00
}
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
2014-10-24 18:07:26 -04:00
for ( ; sit ! = msgIdV . end ( ) ; + + sit ) {
2014-03-17 16:56:06 -04:00
const RsGxsMessageId & msgId = * sit ;
2015-08-23 13:05:37 -04:00
RetroCursor * c = mDb - > sqlQuery ( MSG_TABLE_NAME , mMsgMetaColumns , KEY_GRP_ID + " =' " + grpId . toStdString ( )
2014-03-17 16:56:06 -04:00
+ " ' AND " + KEY_MSG_ID + " =' " + msgId . toStdString ( ) + " ' " , " " ) ;
2012-06-26 15:52:01 -04:00
2015-08-11 17:19:37 -04:00
if ( c )
{
locked_retrieveMsgMeta ( c , metaSet ) ;
2016-07-01 23:38:10 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
std : : cerr < < " Retrieving Msg metadata grpId= " < < grpId < < " , " < < std : : dec < < metaSet . size ( ) < < " messages " < < std : : endl ;
# endif
2015-08-11 17:19:37 -04:00
}
2012-08-19 18:15:37 -04:00
}
2012-05-21 18:07:43 -04:00
}
2015-09-07 16:27:04 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount + = metaSet . size ( ) ;
# endif
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
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2015-08-13 05:03:57 -04:00
std : : cerr < < " RsDataService::retrieveGxsMsgMetaData() " < < mDbName < < " , Requests: " < < reqIds . size ( ) < < " , Results: " < < resultCount < < " , Time: " < < timer . duration ( ) < < std : : endl ;
2015-08-11 17:19:37 -04:00
# endif
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 ) {
2015-09-05 06:46:18 -04:00
RsGxsMsgMetaData * m = locked_getMsgMeta ( * c , 0 ) ;
2012-08-19 18:15:37 -04:00
if ( m ! = NULL )
msgMeta . push_back ( m ) ;
valid = c - > moveToNext ( ) ;
}
delete c ;
}
}
2018-01-07 15:41:41 -05:00
int RsDataService : : retrieveGxsGrpMetaData ( RsGxsGrpMetaTemporaryMap & grp )
2012-06-26 15:52:01 -04:00
{
2014-04-27 09:14:07 -04:00
# ifdef RS_DATA_SERVICE_DEBUG
2016-10-29 11:26:08 -04:00
std : : cerr < < " RsDataService::retrieveGxsGrpMetaData() " ;
std : : cerr < < std : : endl ;
2014-04-27 09:14:07 -04:00
# 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
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-27 14:22:31 -05:00
rstime : : RsScopeTimer timer ( " " ) ;
2015-08-27 09:05:04 -04:00
int resultCount = 0 ;
int requestedGroups = grp . size ( ) ;
2015-08-11 17:19:37 -04:00
# endif
2016-07-01 23:38:10 -04:00
if ( grp . empty ( ) )
{
if ( mGrpMetaDataCache_ContainsAllDatabase ) // grab all the stash from the cache, so as to avoid decryption costs.
{
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2016-07-03 23:12:56 -04:00
std : : cerr < < ( void * ) this < < " : RsDataService::retrieveGxsGrpMetaData() retrieving all from cache! " < < std : : endl ;
2016-07-01 23:38:10 -04:00
# endif
2016-07-03 23:12:56 -04:00
2018-01-07 15:41:41 -05:00
grp = mGrpMetaDataCache ;
2016-07-01 23:38:10 -04:00
}
else
2018-01-07 15:41:41 -05:00
{
2014-04-27 09:14:07 -04:00
# ifdef RS_DATA_SERVICE_DEBUG
2018-01-07 15:41:41 -05:00
std : : cerr < < " RsDataService::retrieveGxsGrpMetaData() retrieving all " < < std : : endl ;
2014-04-27 09:14:07 -04:00
# endif
2018-01-07 15:41:41 -05:00
// clear the cache
2014-04-27 09:14:07 -04:00
2018-01-07 15:41:41 -05:00
RetroCursor * c = mDb - > sqlQuery ( GRP_TABLE_NAME , mGrpMetaColumns , " " , " " ) ;
if ( c )
{
bool valid = c - > moveToFirst ( ) ;
while ( valid )
{
2018-01-09 18:10:08 -05:00
RsGxsGrpMetaData * g = locked_getGrpMeta ( * c , 0 , true ) ;
2018-01-07 15:41:41 -05:00
if ( g )
{
grp [ g - > mGroupId ] = g ;
2016-07-01 23:38:10 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2018-01-07 15:41:41 -05:00
std : : cerr < < ( void * ) this < < " : Retrieving (all) Grp metadata grpId= " < < g - > mGroupId < < std : : endl ;
2016-07-01 23:38:10 -04:00
# endif
2018-01-07 15:41:41 -05:00
}
valid = c - > moveToNext ( ) ;
2015-08-27 09:05:04 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-07 15:41:41 -05:00
+ + resultCount ;
2015-08-27 09:05:04 -04:00
# endif
2018-01-07 15:41:41 -05:00
}
delete c ;
}
2016-07-03 23:12:56 -04:00
2018-01-07 15:41:41 -05:00
mGrpMetaDataCache_ContainsAllDatabase = true ;
}
2012-08-26 10:02:47 -04:00
2018-01-07 15:41:41 -05:00
}
else
2012-06-26 15:52:01 -04:00
{
2018-01-09 18:10:08 -05:00
std : : map < RsGxsGroupId , RsGxsGrpMetaData * > : : iterator mit = grp . begin ( ) ;
2012-08-26 10:02:47 -04:00
2014-10-24 18:07:26 -04:00
for ( ; mit ! = grp . end ( ) ; + + mit )
2013-10-22 15:56:01 -04:00
{
2018-01-09 18:10:08 -05:00
std : : map < RsGxsGroupId , RsGxsGrpMetaData * > : : const_iterator itt = mGrpMetaDataCache . find ( mit - > first ) ;
2018-01-07 15:41:41 -05:00
2016-07-01 23:38:10 -04:00
if ( itt ! = mGrpMetaDataCache . end ( ) )
2013-10-22 15:56:01 -04:00
{
2016-07-01 23:38:10 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2016-07-03 23:12:56 -04:00
std : : cerr < < " Retrieving Grp metadata grpId= " < < mit - > first < < " from cache! " < < std : : endl ;
2016-07-01 23:38:10 -04:00
# endif
2018-01-07 15:41:41 -05:00
grp [ mit - > first ] = itt - > second ;
2016-07-01 23:38:10 -04:00
}
else
2018-01-07 15:41:41 -05:00
{
2016-07-01 23:38:10 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2018-01-07 15:41:41 -05:00
std : : cerr < < " Retrieving Grp metadata grpId= " < < mit - > first ;
2016-07-01 23:38:10 -04:00
# endif
2012-08-26 10:02:47 -04:00
2018-01-07 15:41:41 -05:00
const RsGxsGroupId & grpId = mit - > first ;
RetroCursor * c = mDb - > sqlQuery ( GRP_TABLE_NAME , mGrpMetaColumns , " grpId=' " + grpId . toStdString ( ) + " ' " , " " ) ;
2016-07-01 23:38:10 -04:00
2018-01-07 15:41:41 -05:00
if ( c )
{
bool valid = c - > moveToFirst ( ) ;
2016-07-01 23:38:10 -04:00
2016-07-10 23:00:47 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2018-01-07 15:41:41 -05:00
if ( ! valid )
std : : cerr < < " Empty query! GrpId " < < grpId < < " is not in database " < < std : : endl ;
2016-07-10 23:00:47 -04:00
# endif
2018-01-07 15:41:41 -05:00
while ( valid )
{
2018-01-09 18:10:08 -05:00
RsGxsGrpMetaData * g = locked_getGrpMeta ( * c , 0 , true ) ;
2018-01-07 15:41:41 -05:00
if ( g )
{
grp [ g - > mGroupId ] = g ;
2016-07-01 23:38:10 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2018-01-07 15:41:41 -05:00
std : : cerr < < " . Got it. Updating cache. " < < std : : endl ;
2016-07-01 23:38:10 -04:00
# endif
2018-01-07 15:41:41 -05:00
}
valid = c - > moveToNext ( ) ;
2015-08-27 09:05:04 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-07 15:41:41 -05:00
+ + resultCount ;
2015-08-27 09:05:04 -04:00
# endif
2018-01-07 15:41:41 -05:00
}
delete c ;
}
2016-07-01 23:38:10 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2018-01-07 15:41:41 -05:00
else
std : : cerr < < " . not found! " < < std : : endl ;
2016-07-01 23:38:10 -04:00
# endif
2018-01-07 15:41:41 -05: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
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2015-08-27 09:05:04 -04:00
std : : cerr < < " RsDataService::retrieveGxsGrpMetaData() " < < mDbName < < " , Requests: " < < requestedGroups < < " , Results: " < < resultCount < < " , Time: " < < timer . duration ( ) < < std : : endl ;
2015-08-11 17:19:37 -04:00
# endif
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
2012-10-15 14:52:47 -04:00
{
RsStackMutex stack ( mDbMutex ) ;
2012-06-05 10:46:18 -04:00
2015-08-10 07:58:19 -04:00
mDb - > execSQL ( " DROP INDEX " + MSG_INDEX_GRPID ) ;
2015-08-16 07:27:04 -04:00
mDb - > execSQL ( " DROP TABLE " + DATABASE_RELEASE_TABLE_NAME ) ;
2013-10-22 15:56:01 -04:00
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
2015-08-16 07:27:04 -04:00
initialise ( true ) ;
2012-06-05 10:46:18 -04:00
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
{
2016-07-03 23:12:56 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2016-07-01 23:38:10 -04:00
std : : cerr < < ( void * ) this < < " : Updating Grp Meta data: grpId = " < < meta . grpId < < std : : endl ;
2016-07-03 23:12:56 -04:00
# endif
2016-07-01 23:38:10 -04:00
RsStackMutex stack ( mDbMutex ) ;
2012-08-21 17:32:07 -04:00
RsGxsGroupId & grpId = meta . grpId ;
2016-07-03 23:12:56 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2016-07-01 23:38:10 -04:00
std : : cerr < < ( void * ) this < < " : erasing old entry from cache. " < < std : : endl ;
2016-07-03 23:12:56 -04:00
# endif
2016-07-22 17:49:07 -04:00
locked_clearGrpMetaCache ( meta . grpId ) ;
2016-07-03 23:12:56 -04:00
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
{
2016-07-03 23:12:56 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_CACHE
2016-07-01 23:38:10 -04:00
std : : cerr < < ( void * ) this < < " : Updating Msg Meta data: grpId = " < < metaData . msgId . first < < " msgId = " < < metaData . msgId . second < < std : : endl ;
2016-07-03 23:12:56 -04:00
# endif
2016-10-29 11:26:08 -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
}
2012-08-06 17:00:38 -04:00
int RsDataService : : removeMsgs ( const GxsMsgReq & msgIds )
2012-06-26 15:52:01 -04:00
{
2016-10-29 11:26:08 -04:00
RsStackMutex stack ( mDbMutex ) ;
2013-03-20 18:26:24 -04:00
2016-10-29 11:26:08 -04:00
GxsMsgReq : : const_iterator mit = msgIds . begin ( ) ;
2013-03-20 18:26:24 -04:00
2016-10-29 11:26:08 -04:00
for ( ; mit ! = msgIds . end ( ) ; + + mit )
{
const std : : vector < RsGxsMessageId > & msgIdV = mit - > second ;
const RsGxsGroupId & grpId = mit - > first ;
2013-03-20 18:26:24 -04:00
2016-10-29 11:26:08 -04:00
// delete messages
GxsMsgReq msgsToDelete ;
msgsToDelete [ grpId ] = msgIdV ;
locked_removeMessageEntries ( msgsToDelete ) ;
}
2013-03-20 18:26:24 -04:00
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
{
2016-10-29 11:26:08 -04:00
RsStackMutex stack ( mDbMutex ) ;
2013-06-04 17:00:43 -04:00
2016-10-29 11:26:08 -04:00
locked_removeGroupEntries ( grpIds ) ;
2013-06-04 17:00:43 -04:00
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 ) ;
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-27 14:22:31 -05:00
rstime : : RsScopeTimer timer ( " " ) ;
2016-10-29 11:26:08 -04:00
int resultCount = 0 ;
2015-08-11 17:19:37 -04:00
# endif
2016-10-29 11:26:08 -04:00
RetroCursor * c = mDb - > sqlQuery ( GRP_TABLE_NAME , mGrpIdColumn , " " , " " ) ;
2013-09-11 16:42:56 -04:00
2016-10-29 11:26:08 -04:00
if ( c )
{
bool valid = c - > moveToFirst ( ) ;
2013-09-11 16:42:56 -04:00
2016-10-29 11:26:08 -04:00
while ( valid )
{
std : : string grpId ;
c - > getString ( mColGrpId_GrpId , grpId ) ;
grpIds . push_back ( RsGxsGroupId ( grpId ) ) ;
valid = c - > moveToNext ( ) ;
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2016-10-29 11:26:08 -04:00
+ + resultCount ;
2015-08-11 17:19:37 -04:00
# endif
2016-10-29 11:26:08 -04:00
}
delete c ;
} else
{
return 0 ;
}
2013-09-11 16:42:56 -04:00
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2016-10-29 11:26:08 -04:00
std : : cerr < < " RsDataService::retrieveGroupIds() " < < mDbName < < " , Results: " < < resultCount < < " , Time: " < < timer . duration ( ) < < std : : endl ;
2015-08-11 17:19:37 -04:00
# endif
2016-10-29 11:26:08 -04:00
return 1 ;
2013-09-11 16:42:56 -04:00
}
2013-03-20 18:26:24 -04:00
2015-08-11 17:19:37 -04:00
int RsDataService : : retrieveMsgIds ( const RsGxsGroupId & grpId , RsGxsMessageId : : std_vector & msgIds )
{
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2018-01-27 14:22:31 -05:00
rstime : : RsScopeTimer timer ( " " ) ;
2016-10-29 11:26:08 -04:00
int resultCount = 0 ;
2015-08-11 17:19:37 -04:00
# endif
2014-04-26 06:07:25 -04:00
2016-10-29 11:26:08 -04:00
RetroCursor * c = mDb - > sqlQuery ( MSG_TABLE_NAME , mMsgIdColumn , KEY_GRP_ID + " =' " + grpId . toStdString ( ) + " ' " , " " ) ;
2014-04-26 06:07:25 -04:00
2016-10-29 11:26:08 -04:00
if ( c )
{
bool valid = c - > moveToFirst ( ) ;
2014-04-26 06:07:25 -04:00
2016-10-29 11:26:08 -04:00
while ( valid )
{
std : : string msgId ;
2015-08-23 13:05:37 -04:00
c - > getString ( mColMsgId_MsgId , msgId ) ;
2014-09-20 15:58:07 -04:00
if ( c - > columnCount ( ) ! = 1 )
std : : cerr < < " (EE) ********* not retrieving all columns!! " < < std : : endl ;
2016-10-29 11:26:08 -04:00
msgIds . push_back ( RsGxsMessageId ( msgId ) ) ;
valid = c - > moveToNext ( ) ;
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2016-10-29 11:26:08 -04:00
+ + resultCount ;
2015-08-11 17:19:37 -04:00
# endif
2016-10-29 11:26:08 -04:00
}
delete c ;
} else
{
return 0 ;
}
2014-04-26 06:07:25 -04:00
2015-08-11 17:19:37 -04:00
# ifdef RS_DATA_SERVICE_DEBUG_TIME
2016-10-29 11:26:08 -04:00
std : : cerr < < " RsDataService::retrieveNxsGrps() " < < mDbName < < " , Results: " < < resultCount < < " , Time: " < < timer . duration ( ) < < std : : endl ;
2015-08-11 17:19:37 -04:00
# endif
2016-10-29 11:26:08 -04:00
return 1 ;
2014-04-26 06:07:25 -04:00
}
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
2015-08-15 11:06:06 -04:00
bool ret = mDb - > beginTransaction ( ) ;
2013-03-20 18:26:24 -04:00
GxsMsgReq : : const_iterator mit = msgIds . begin ( ) ;
2014-10-24 18:07:26 -04:00
for ( ; mit ! = msgIds . end ( ) ; + + mit )
2013-03-20 18:26:24 -04:00
{
2016-10-29 11:26:08 -04:00
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 ;
mDb - > sqlDelete ( MSG_TABLE_NAME , KEY_GRP_ID + " =' " + grpId . toStdString ( )
2014-03-17 16:56:06 -04:00
+ " ' AND " + KEY_MSG_ID + " =' " + msgId . toStdString ( ) + " ' " , " " ) ;
2016-10-29 11:26:08 -04:00
}
2013-03-20 18:26:24 -04:00
}
2015-08-15 11:06:06 -04:00
ret & = mDb - > commitTransaction ( ) ;
2013-03-20 18:26:24 -04:00
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
2015-08-15 11:06:06 -04:00
bool ret = mDb - > beginTransaction ( ) ;
2013-06-04 17:00:43 -04:00
2014-03-17 16:56:06 -04:00
std : : vector < RsGxsGroupId > : : const_iterator vit = grpIds . begin ( ) ;
2013-06-04 17:00:43 -04:00
2014-10-24 18:07:26 -04:00
for ( ; vit ! = grpIds . end ( ) ; + + vit )
2013-06-04 17:00:43 -04:00
{
2016-10-29 11:26:08 -04:00
const RsGxsGroupId & grpId = * vit ;
mDb - > sqlDelete ( GRP_TABLE_NAME , KEY_GRP_ID + " =' " + grpId . toStdString ( ) + " ' " , " " ) ;
2017-01-20 17:27:18 -05:00
// also remove the group meta from cache.
2018-01-07 15:41:41 -05:00
locked_clearGrpMetaCache ( * vit ) ;
2013-06-04 17:00:43 -04:00
}
2015-08-15 11:06:06 -04:00
ret & = mDb - > commitTransaction ( ) ;
2013-06-04 17:00:43 -04:00
2017-01-20 17:27:18 -05:00
mGrpMetaDataCache_ContainsAllDatabase = false ;
2013-06-04 17:00:43 -04:00
return ret ;
}
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