From e661cd50e923f4edeb38260f0987b235434df67b Mon Sep 17 00:00:00 2001 From: mmichalek Date: Fri, 9 Oct 2015 18:44:35 -0400 Subject: [PATCH] Implementing syncTriggers and everything related. --- symmetric-client-clib/inc/core/SymEngine.h | 2 + .../inc/db/platform/DatabasePlatform.h | 1 + .../inc/model/NodeGroupLink.h | 2 +- symmetric-client-clib/inc/model/Router.h | 34 +++++ symmetric-client-clib/inc/model/Trigger.h | 34 +++++ .../inc/model/TriggerRouter.h | 34 +++++ .../inc/service/ConfigurationService.h | 4 + .../inc/service/TriggerRouterService.h | 46 ++++++- symmetric-client-clib/src/core/SymEngine.c | 19 +-- .../src/db/platform/DatabasePlatform.c | 5 + .../src/db/platform/sqlite/SqlitePlatform.c | 2 +- symmetric-client-clib/src/model/Router.c | 34 +++++ symmetric-client-clib/src/model/Trigger.c | 34 +++++ .../src/model/TriggerRouter.c | 33 +++++ .../src/service/ConfigurationService.c | 25 +++- .../src/service/TriggerRouterService.c | 123 +++++++++++++++++- 16 files changed, 413 insertions(+), 19 deletions(-) create mode 100644 symmetric-client-clib/inc/model/Router.h create mode 100644 symmetric-client-clib/inc/model/Trigger.h create mode 100644 symmetric-client-clib/inc/model/TriggerRouter.h create mode 100644 symmetric-client-clib/src/model/Router.c create mode 100644 symmetric-client-clib/src/model/Trigger.c create mode 100644 symmetric-client-clib/src/model/TriggerRouter.c diff --git a/symmetric-client-clib/inc/core/SymEngine.h b/symmetric-client-clib/inc/core/SymEngine.h index 633a6671eb..6a65c2d3ba 100644 --- a/symmetric-client-clib/inc/core/SymEngine.h +++ b/symmetric-client-clib/inc/core/SymEngine.h @@ -37,6 +37,7 @@ #include "service/RegistrationService.h" #include "service/DataLoaderService.h" #include "service/IncomingBatchService.h" +#include "service/ConfigurationService.h" #include "transport/TransportManagerFactory.h" #include "transport/TransportManager.h" #include "common/Constants.h" @@ -54,6 +55,7 @@ typedef struct SymEngine { SymPullService *pullService; SymNodeService *nodeService; SymIncomingBatchService *incomingBatchService; + SymConfigurationService *configurationService; unsigned short (*start)(struct SymEngine *this); unsigned short (*stop)(struct SymEngine *this); diff --git a/symmetric-client-clib/inc/db/platform/DatabasePlatform.h b/symmetric-client-clib/inc/db/platform/DatabasePlatform.h index 099d9fad76..ca2a55eb27 100644 --- a/symmetric-client-clib/inc/db/platform/DatabasePlatform.h +++ b/symmetric-client-clib/inc/db/platform/DatabasePlatform.h @@ -48,6 +48,7 @@ typedef struct SymDatabasePlatform { SymSqlTemplate * (*getSqlTemplate)(struct SymDatabasePlatform *this); SymTable * (*getTableFromCache)(struct SymDatabasePlatform *this, char *catalog, char *schema, char *tableName, unsigned int forceReread); SymTable * (*readTableFromDatabase)(struct SymDatabasePlatform *this, char *catalog, char *schema, char *tableName); + void (*resetCachedTableModel)(struct SymDatabasePlatform *this); void (*destroy)(void *this); } SymDatabasePlatform; diff --git a/symmetric-client-clib/inc/model/NodeGroupLink.h b/symmetric-client-clib/inc/model/NodeGroupLink.h index 01d9c92d6e..46eec1ad94 100644 --- a/symmetric-client-clib/inc/model/NodeGroupLink.h +++ b/symmetric-client-clib/inc/model/NodeGroupLink.h @@ -19,7 +19,7 @@ * under the License. */ #ifndef SYM_NODE_GROUP_LINK_H -#define SYM_NODE_GROU_LINK_H +#define SYM_NODE_GROUP_LINK_H #include #include "util/Date.h" diff --git a/symmetric-client-clib/inc/model/Router.h b/symmetric-client-clib/inc/model/Router.h new file mode 100644 index 0000000000..8108f4afdc --- /dev/null +++ b/symmetric-client-clib/inc/model/Router.h @@ -0,0 +1,34 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#ifndef INC_MODEL_ROUTER_H_ +#define INC_MODEL_ROUTER_H_ + +#include + + +typedef struct SymRouter { + char *routerId; + void (*destroy)(struct SymRouter *this); +} SymRouter; + +SymRouter * SymRouter_new(SymRouter *this); + +#endif /* INC_MODEL_ROUTER_H_ */ diff --git a/symmetric-client-clib/inc/model/Trigger.h b/symmetric-client-clib/inc/model/Trigger.h new file mode 100644 index 0000000000..355dc52ec4 --- /dev/null +++ b/symmetric-client-clib/inc/model/Trigger.h @@ -0,0 +1,34 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#ifndef INC_MODEL_TRIGGER_H_ +#define INC_MODEL_TRIGGER_H_ + +#include + + +typedef struct SymTrigger { + + void (*destroy)(struct SymTrigger *this); +} SymTrigger; + +SymTrigger * SymTrigger_new(SymTrigger *this); + +#endif /* INC_MODEL_TRIGGER_H_ */ diff --git a/symmetric-client-clib/inc/model/TriggerRouter.h b/symmetric-client-clib/inc/model/TriggerRouter.h new file mode 100644 index 0000000000..ebc1566811 --- /dev/null +++ b/symmetric-client-clib/inc/model/TriggerRouter.h @@ -0,0 +1,34 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#ifndef INC_MODEL_TRIGGERROUTER_H_ +#define INC_MODEL_TRIGGERROUTER_H_ + +#include +#include "model/Trigger.h" + +typedef struct SymTriggerRouter { + SymTrigger *trigger; + void (*destroy)(struct SymTriggerRouter *this); +} SymTriggerRouter; + +SymTriggerRouter * SymTriggerRouter_new(SymTriggerRouter *this); + +#endif /* INC_MODEL_TRIGGERROUTER_H_ */ diff --git a/symmetric-client-clib/inc/service/ConfigurationService.h b/symmetric-client-clib/inc/service/ConfigurationService.h index 6fa5b06adb..1f7b435e50 100644 --- a/symmetric-client-clib/inc/service/ConfigurationService.h +++ b/symmetric-client-clib/inc/service/ConfigurationService.h @@ -27,6 +27,10 @@ typedef struct SymConfigurationService { SymList * (*getNodeChannels)(struct SymConfigurationService *this); + SymList * (*clearCache)(struct SymConfigurationService *this); + SymList * (*destroy)(struct SymConfigurationService *this); } SymConfigurationService; +SymConfigurationService * SymConfigurationService_new(SymConfigurationService *this); + #endif diff --git a/symmetric-client-clib/inc/service/TriggerRouterService.h b/symmetric-client-clib/inc/service/TriggerRouterService.h index 66478572f5..c6e6427be3 100644 --- a/symmetric-client-clib/inc/service/TriggerRouterService.h +++ b/symmetric-client-clib/inc/service/TriggerRouterService.h @@ -25,15 +25,57 @@ #include #include "db/model/Table.h" #include "service/ParameterService.h" +#include "service/ConfigurationService.h" +#include "db/platform/DatabasePlatform.h" +#include "util/Map.h" +#include "util/List.h" + +typedef struct SymTriggerSelector { + SymList* triggers; + struct SymList * (*select)(struct SymTriggerSelector *this); + void (*destroy)(struct SymTriggerSelector * this); +} SymTriggerSelector; + +SymTriggerSelector * SymTriggerSelector_new(SymTriggerSelector *this, SymList *triggers); + + +typedef struct SymTriggerRoutersCache { + SymMap* triggerRoutersByTriggerId; + SymMap* routersByRouterId; + void (*destroy)(struct SymTriggerSelector * this); +} SymTriggerRoutersCache; + +SymTriggerRoutersCache * SymTriggerRoutersCache_new(SymTriggerRoutersCache *this); + typedef struct SymTriggerRouterService { + long routersCacheTime; + SymMap* routersCache; + + SymList* triggerRoutersCache; + long triggerRouterPerNodeCacheTime; + + SymMap* triggersCache; + long triggersCacheTime; + long triggerRoutersCacheTime; + + SymMap* triggerRouterCacheByNodeGroupId; + + long triggerRouterPerChannelCacheTime; + SymParameterService *parameterService; + SymConfigurationService *configurationService; + SymDatabasePlatform *platform; int (*syncTriggers)(struct SymTriggerRouterService *this, SymStringBuilder *sqlBuffer, unsigned short force); - int (*syncTriggers_withTable)(struct SymTriggerRouterService *this, SymTable *table, unsigned short force); + int (*syncTriggersWithTable)(struct SymTriggerRouterService *this, SymTable *table, unsigned short force); + void (*clearCache)(struct SymTriggerRouterService *this); + SymMap* (*getTriggerRoutersForCurrentNode)(struct SymTriggerRouterService *this, unsigned short refreshCache); + SymList* (*getTriggersForCurrentNode)(struct SymTriggerRouterService *this, unsigned short refreshCache); + SymTriggerRoutersCache* (*getTriggerRoutersCacheForCurrentNode)(struct SymTriggerRouterService *this, unsigned short refreshCache); void (*destroy)(struct SymTriggerRouterService *this); } SymTriggerRouterService; -SymTriggerRouterService * SymTriggerRouterService_new(SymTriggerRouterService * this); +SymTriggerRouterService * SymTriggerRouterService_new(SymTriggerRouterService * this, SymParameterService *parameterService, SymDatabasePlatform *platform, SymConfigurationService *configurationService); #endif diff --git a/symmetric-client-clib/src/core/SymEngine.c b/symmetric-client-clib/src/core/SymEngine.c index 1123964ec5..e34ec56aea 100644 --- a/symmetric-client-clib/src/core/SymEngine.c +++ b/symmetric-client-clib/src/core/SymEngine.c @@ -52,6 +52,7 @@ unsigned short SymEngine_start(SymEngine *this) { SymNode *node = this->nodeService->findIdentity(this->nodeService); if (node != NULL) { + if (strcmp(node->externalId, this->parameterService->getExternalId(this->parameterService)) != 0 || strcmp(node->nodeGroupId, this->parameterService->getNodeGroupId(this->parameterService)) != 0) { SymLog_error("The configured state does not match recorded database state. The recorded external id is %s while the configured external id is %s. The recorded node group id is %s while the configured node group id is %s", @@ -60,13 +61,12 @@ unsigned short SymEngine_start(SymEngine *this) { } else { SymLog_info("Starting registered node [group=%s, id=%s, externalId=%s]", node->nodeGroupId, node->nodeId, node->externalId); - // TODO -// if (this->parameterService->is(this->parameterService, AUTO_SYNC_TRIGGERS_AT_STARTUP, 0)) { -// this->triggerRouterService->syncTriggers(this->triggerRouterService, NULL, 0); -// } -// else { -// SymLog_info("%s is turned off.", AUTO_SYNC_TRIGGERS_AT_STARTUP); -// } + if (this->parameterService->is(this->parameterService, AUTO_SYNC_TRIGGERS_AT_STARTUP, 1)) { + this->triggerRouterService->syncTriggers(this->triggerRouterService, NULL, 0); + } + else { + SymLog_info("%s is turned off.", AUTO_SYNC_TRIGGERS_AT_STARTUP); + } // TODO: if HEARTBEAT_SYNC_ON_STARTUP } @@ -90,7 +90,6 @@ unsigned short SymEngine_start(SymEngine *this) { } unsigned short SymEngine_stop(SymEngine *this) { - SymLog_info("Stopping\n"); SymLog_info("Stopping SymmetricDS externalId=%s version=%s database=%s", this->parameterService->getExternalId(this->parameterService), SYM_VERSION, this->platform->name); @@ -130,8 +129,10 @@ SymEngine * SymEngine_new(SymEngine *this, SymProperties *properties) { this->platform = SymDatabasePlatformFactory_create(properties); this->dialect = SymDialectFactory_create(this->platform); + this->configurationService = SymConfigurationService_new(NULL); + this->parameterService = SymParameterService_new(NULL, properties); - this->triggerRouterService = SymTriggerRouterService_new(NULL); + this->triggerRouterService = SymTriggerRouterService_new(NULL, this->parameterService, this->platform, this->configurationService); this->transportManager = SymTransportManagerFactory_create(SYM_PROTOCOL_HTTP, this->parameterService); this->nodeService = SymNodeService_new(NULL, this->platform); this->incomingBatchService = SymIncomingBatchService_new(NULL, this->platform, this->parameterService); diff --git a/symmetric-client-clib/src/db/platform/DatabasePlatform.c b/symmetric-client-clib/src/db/platform/DatabasePlatform.c index af5086211d..e0659db1f5 100644 --- a/symmetric-client-clib/src/db/platform/DatabasePlatform.c +++ b/symmetric-client-clib/src/db/platform/DatabasePlatform.c @@ -36,6 +36,10 @@ SymTable * SymDatabasePlatform_readTableFromDatabase(SymDatabasePlatform *this, return table; } +void SymDatabasePlatform_resetCachedTableModel(SymDatabasePlatform *this) { + // TODO: implement along with caching. +} + void SymDatabasePlatform_destroy(SymDatabasePlatform *this) { } @@ -43,6 +47,7 @@ SymDatabasePlatform * SymDatabasePlatform_new(SymDatabasePlatform *this) { if (this != NULL) { this->getTableFromCache = (void *) &SymDatabasePlatform_getTableFromCache; this->readTableFromDatabase = (void *) &SymDatabasePlatform_readTableFromDatabase; + this->resetCachedTableModel = (void *)&SymDatabasePlatform_resetCachedTableModel; this->destroy = (void *) &SymDatabasePlatform_destroy; } return this; diff --git a/symmetric-client-clib/src/db/platform/sqlite/SqlitePlatform.c b/symmetric-client-clib/src/db/platform/sqlite/SqlitePlatform.c index 14a63b5ac5..bcc9bf5bb2 100644 --- a/symmetric-client-clib/src/db/platform/sqlite/SqlitePlatform.c +++ b/symmetric-client-clib/src/db/platform/sqlite/SqlitePlatform.c @@ -81,7 +81,7 @@ SymSqlitePlatform * SymSqlitePlatform_new(SymSqlitePlatform *this, SymProperties if (sqlite3_open_v2(filename, &this->db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, NULL)) { - SymLog_info("Can't open database: %s", sqlite3_errmsg(this->db)); + SymLog_error("Can't open database: %s", sqlite3_errmsg(this->db)); sqlite3_close(this->db); return NULL; } diff --git a/symmetric-client-clib/src/model/Router.c b/symmetric-client-clib/src/model/Router.c new file mode 100644 index 0000000000..7221837720 --- /dev/null +++ b/symmetric-client-clib/src/model/Router.c @@ -0,0 +1,34 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "model/Router.h" + + +void SymRouter_destroy(SymRouter *this) { + free(this); +} + +SymRouter * SymRouter_new(SymRouter *this) { + if (this == NULL) { + this = (SymRouter *) calloc(1, sizeof(SymRouter)); + } + this->destroy = (void *) &SymRouter_destroy; + return this; +} diff --git a/symmetric-client-clib/src/model/Trigger.c b/symmetric-client-clib/src/model/Trigger.c new file mode 100644 index 0000000000..99c0b74786 --- /dev/null +++ b/symmetric-client-clib/src/model/Trigger.c @@ -0,0 +1,34 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "model/Trigger.h" + + +void SymTrigger_destroy(SymTrigger *this) { + free(this); +} + +SymTrigger * SymTrigger_new(SymTrigger *this) { + if (this == NULL) { + this = (SymTrigger *) calloc(1, sizeof(SymTrigger)); + } + this->destroy = (void *) &SymTrigger_destroy; + return this; +} diff --git a/symmetric-client-clib/src/model/TriggerRouter.c b/symmetric-client-clib/src/model/TriggerRouter.c new file mode 100644 index 0000000000..631ca62575 --- /dev/null +++ b/symmetric-client-clib/src/model/TriggerRouter.c @@ -0,0 +1,33 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "model/TriggerRouter.h" + +void SymTriggerRouter_destroy(SymTriggerRouter *this) { + free(this); +} + +SymTriggerRouter * SymTriggerRouter_new(SymTriggerRouter *this) { + if (this == NULL) { + this = (SymTriggerRouter *) calloc(1, sizeof(SymTriggerRouter)); + } + this->destroy = (void *) &SymTriggerRouter_destroy; + return this; +} diff --git a/symmetric-client-clib/src/service/ConfigurationService.c b/symmetric-client-clib/src/service/ConfigurationService.c index 98777629c4..e8d11aa86f 100644 --- a/symmetric-client-clib/src/service/ConfigurationService.c +++ b/symmetric-client-clib/src/service/ConfigurationService.c @@ -21,7 +21,28 @@ #include "service/ConfigurationService.h" #include "common/Log.h" -SymNodeChannel* SymNodeService_getNodeChannels(SymConfigurationService *this) { - SymLog_info("SymNodeService_get_node_channels"); +SymNodeChannel* SymConfigurationService_getNodeChannels(SymConfigurationService *this) { + SymLog_info("SymConfigurationService_getNodeChannels"); return NULL; } + +void SymConfigurationService_clearCache(SymConfigurationService *this) { + // TODO +} + +void SymConfigurationService_destroy(SymConfigurationService * this) { + free(this); +} + +SymConfigurationService * SymConfigurationService_new(SymConfigurationService *this) { + if (this == NULL) { + this = (SymConfigurationService *) calloc(1, sizeof(SymConfigurationService)); + } + this->getNodeChannels = (void *) &SymConfigurationService_getNodeChannels; + this->clearCache = (void *) &SymConfigurationService_clearCache; + this->destroy = (void *) &SymConfigurationService_destroy; + + return this; +} + + diff --git a/symmetric-client-clib/src/service/TriggerRouterService.c b/symmetric-client-clib/src/service/TriggerRouterService.c index 4fb0d9ecd8..539449864f 100644 --- a/symmetric-client-clib/src/service/TriggerRouterService.c +++ b/symmetric-client-clib/src/service/TriggerRouterService.c @@ -21,31 +21,146 @@ #include #include "common/ParameterConstants.h" #include "common/Log.h" +#include "model/TriggerRouter.h" +#include + +SymList * SymTriggerSelector_select(SymTriggerSelector *this) { + SymList *filtered = SymList_new(NULL); + + if (this->triggers == NULL) { + return filtered; + } + + SymIterator *iter = this->triggers->iterator(this->triggers); + while (iter->hasNext(iter)) { + SymTriggerRouter *trigger = (SymTriggerRouter *) iter->next(iter); + + // TODO check for duplicate trigger ID's. -- if (!filtered.contains(trigger.getTrigger())) + filtered->add(filtered, trigger); + } + return filtered; +} + +void SymTriggerSelector_destroy(SymTriggerSelector * this) { + this->triggers->destroy(this->triggers); + free(this); +} + +SymTriggerSelector * SymTriggerSelector_new(SymTriggerSelector *this, SymList *triggers) { + if (this == NULL) { + this = (SymTriggerSelector*) calloc(1, sizeof(SymTriggerSelector)); + } + this->triggers = triggers; + this->select = (void *) &SymTriggerSelector_select; + + this->destroy = (void *) &SymTriggerSelector_destroy; + return this; +} + +void SymTriggerRoutersCache_destroy(SymTriggerSelector * this) { + this->triggers->destroy(this->triggers); + free(this); +} + +SymTriggerRoutersCache * SymTriggerRoutersCache_new(SymTriggerRoutersCache *this) { + if (this == NULL) { + this = (SymTriggerRoutersCache*) calloc(1, sizeof(SymTriggerRoutersCache)); + } + this->destroy = (void *) &SymTriggerRoutersCache_destroy; + return this; +} + int SymTriggerRouterService_syncTriggers (SymTriggerRouterService *this, SymStringBuilder *sqlBuffer, unsigned short force) { + // This gets called by SymEngine at startup. SymLog_info("SymTriggerRouterService_syncTriggers_withTable"); + + unsigned short autoSyncTriggers = this->parameterService->is(this->parameterService, AUTO_SYNC_TRIGGERS, 1); + + if (autoSyncTriggers) { + SymLog_info("Synchronizing triggers"); + this->platform->resetCachedTableModel(this->platform); + this->clearCache(this); + this->configurationService->clearCache(this->configurationService); + + SymList* triggersForCurrentNode = this->getTriggersForCurrentNode(this, 0); + + // TODO >>>>>>>> + } + return 0; } -int SymTriggerRouterService_syncTriggers_withTable(SymTriggerRouterService *this, SymTable *table, unsigned short force) { - +int SymTriggerRouterService_syncTriggersWithTable(SymTriggerRouterService *this, SymTable *table, unsigned short force) { // TODO - unsigned short ignoreCase = this->parameterService->is(this->parameterService, AUTO_SYNC_TRIGGERS_AT_STARTUP, 0); SymLog_info("SymTriggerRouterService_syncTriggers_withTable"); return 0; } +void SymTriggerRouterService_clearCache(SymTriggerRouterService *this) { + this->triggerRouterPerNodeCacheTime = 0; + this->triggerRouterPerChannelCacheTime = 0; + this->triggerRoutersCacheTime = 0; + this->routersCacheTime = 0; + this->triggersCacheTime = 0; +} + +SymMap* SymTriggerRouterService_getTriggerRoutersForCurrentNode(SymTriggerRouterService *this, unsigned short refreshCache) { + return this->getTriggerRoutersCacheForCurrentNode(this, refreshCache)->triggerRoutersByTriggerId; +} + +SymList* SymTriggerRouterService_getTriggersForCurrentNode(SymTriggerRouterService *this, unsigned short refreshCache) { + + // SymTriggerSelector *triggerSelector = SymTriggerSelector_new(NULL, NULL /** todo */); + // return triggerSelector->select(triggerSelector); +// SymMap *triggerRouters = this->getTriggerRoutersCacheForCurrentNode(this, refreshCache); +// SymList *triggers = SymList_new(NULL); + // TODO >>>>>>>> + + return NULL; +} + + +SymTriggerRoutersCache* SymTriggerRouterService_getTriggerRoutersCacheForCurrentNode(SymTriggerRouterService *this, unsigned short refreshCache) { + char* myNodeGroupId = this->parameterService->getNodeGroupId(this->parameterService); + long triggerRouterCacheTimeoutInMs = this->parameterService->getLong(this->parameterService, CACHE_TIMEOUT_TRIGGER_ROUTER_IN_MS, 1000); + SymTriggerRoutersCache *cache = this->triggerRouterCacheByNodeGroupId == NULL ? NULL + : this->triggerRouterCacheByNodeGroupId->get(this->triggerRouterCacheByNodeGroupId, myNodeGroupId); + + // TODO determine a way to get time in ms. to expire the cache. + // clock_t timeInMilliseconds = clock() / (CLOCKS_PER_SEC / 1000); +// clock_t timeInSeconds = clock()/CLOCKS_PER_SEC; +// printf("%ul", time); + + + if (cache == NULL + || refreshCache + /** TODO check timeInMilliseconds */) { + // TODO >>>>>>>> + } + + return NULL; +} + void SymTriggerRouterService_destroy(SymTriggerRouterService * this) { free(this); } -SymTriggerRouterService * SymTriggerRouterService_new(SymTriggerRouterService *this) { +SymTriggerRouterService * SymTriggerRouterService_new(SymTriggerRouterService * this, SymParameterService *parameterService, SymDatabasePlatform *platform, SymConfigurationService *configurationService) { if (this == NULL) { this = (SymTriggerRouterService*) calloc(1, sizeof(SymTriggerRouterService)); } + this->parameterService = parameterService; + this->platform = platform; + this->configurationService = configurationService; this->syncTriggers = (void *) &SymTriggerRouterService_syncTriggers; + this->syncTriggersWithTable = (void *) &SymTriggerRouterService_syncTriggersWithTable; + this->getTriggerRoutersForCurrentNode = (void *) &SymTriggerRouterService_getTriggerRoutersForCurrentNode; + this->getTriggersForCurrentNode = (void *) &SymTriggerRouterService_getTriggersForCurrentNode; + this->getTriggerRoutersCacheForCurrentNode = (void *)SymTriggerRouterService_getTriggerRoutersCacheForCurrentNode; + this->clearCache = (void *) &SymTriggerRouterService_clearCache; this->destroy = (void *) &SymTriggerRouterService_destroy; return this; }