summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_prct_access.c
blob: 7330ccbcca7539ea82b5b87ebfb7b8d02e9db86b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/******************************************************************************
 * Project         Persistency
 * (c) copyright   2012
 * Company         XS Embedded GmbH
 *****************************************************************************/
/******************************************************************************
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
 /**
 * @file           persistence_client_library_prct_access.c
 * @ingroup        Persistence client library
 * @author         Ingo Huerner
 * @brief          Implementation of persistence resource configuration config
 *                 table access functions
 * @see
 */


#include "persistence_client_library_prct_access.h"
#include "persistence_client_library_itzam_errors.h"
#include <stdlib.h>



/// pointer to resource table database
itzam_btree gResource_table[PrctDbTableSize];
/// array to hold the information of database is already open
int gResourceOpen[PrctDbTableSize] = {0};


PersistenceRCT_e get_table_id(int ldbid, int* groupId)
{
   PersistenceRCT_e rctType = PersistenceRCT_LastEntry;

   if(ldbid < 0x80)
   {
      // S H A R E D  database
      if(ldbid != 0)
      {
         // shared  G R O U P  database * * * * * * * * * * * * *  * * * * * *
         *groupId = ldbid;  // assign group ID
         rctType = PersistenceRCT_shared_group;
      }
      else
      {
         // shared  P U B L I C  database * * * * * * * * * * * * *  * * * * *
         *groupId = 0;      // no group ID for public data
         rctType = PersistenceRCT_shared_public;
      }
   }
   else
   {
      // L O C A L   database
      *groupId = 0;      // no group ID for local data
      rctType = PersistenceStorage_local;   // we have a local database
   }
   return rctType;
}


itzam_btree* get_resource_cfg_table_by_idx(int i)
{
   return &gResource_table[i];
}


// status: OK
itzam_btree* get_resource_cfg_table(PersistenceRCT_e rct, int group)
{
   int arrayIdx = 0;
   itzam_btree* tree = NULL;

   // create array index: index is a combination of resource config table type and group
   arrayIdx = rct + group;

   if(arrayIdx < PrctDbTableSize)
   {
      if(gResourceOpen[arrayIdx] == 0)   // check if database is already open
      {
         itzam_state  state;
         char filename[DbPathMaxLen];
         memset(filename, 0, DbPathMaxLen);

         switch(rct)    // create db name
         {
         case PersistenceRCT_local:
            snprintf(filename, DbPathMaxLen, gLocalWtPath, gAppId, gResTableCfg);
            break;
         case PersistenceRCT_shared_public:
            snprintf(filename, DbPathMaxLen, gSharedPublicWtPath, gAppId, gResTableCfg);
            break;
         case PersistenceRCT_shared_group:
            snprintf(filename, DbPathMaxLen, gSharedWtPath, gAppId, group, gResTableCfg);
            break;
         default:
            printf("get_resource_cfg_table - error: no valid PersistenceRCT_e\n");
            break;
         }

         //printf("get_resource_cfg_table => %s \n", filename);
         state = itzam_btree_open(&gResource_table[arrayIdx], filename, itzam_comparator_string, error_handler, 0 , 0);
         if(state != ITZAM_OKAY)
         {
            fprintf(stderr, "\nget_resource_cfg_table => Itzam problem: %s\n", STATE_MESSAGES[state]);
         }
         gResourceOpen[arrayIdx] = 1;  // remember the index has an DB entry
      }
      tree = &gResource_table[arrayIdx];
   }

   return tree;
}


// status: OK
int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
{
   int rval = 0, resourceFound = 0, groupId = 0;

   PersistenceRCT_e rct = PersistenceRCT_LastEntry;

   rct = get_table_id(dbContext->context.ldbid, &groupId);

   // get resource configuration table
   itzam_btree* resource_table = get_resource_cfg_table(rct, groupId);

   if(resource_table != NULL)
   {
      PersistenceRctEntry_s search;
      // check if resouce id is in write through table
      if(itzam_true == itzam_btree_find(resource_table, resource_id, &search))
      {
         //printf("get_db_context ==> data: %s\n", search.data);
         memset(dbContext->configKey.reponsible,  0, MaxConfKeyLengthResp);
         memset(dbContext->configKey.custom_name, 0, MaxConfKeyLengthCusName);
         dbContext->configKey.policy      = search.data.policy;
         dbContext->configKey.storage     = search.data.storage;
         dbContext->configKey.permission  = search.data.permission;
         dbContext->configKey.max_size    = search.data.max_size;
         memcpy(dbContext->configKey.reponsible, search.data.reponsible, MaxConfKeyLengthResp);
         memcpy(dbContext->configKey.custom_name, search.data.custom_name, MaxConfKeyLengthCusName);

         if(dbContext->configKey.storage != PersistenceStorage_custom )
         {
            rval = get_db_path_and_key(dbContext, resource_id, isFile, dbKey, dbPath);
         }
         else
         {
            // if customer storage, we use the custom name as path
            strncpy(dbPath, dbContext->configKey.custom_name, strlen(dbContext->configKey.custom_name));
         }
         resourceFound = 1;
      }
      else
      {
         printf("get_db_context - resource_table: no value for key: %s \n", resource_id);
         rval = EPERS_NOKEYDATA;
      }
   }  // resource table
   else
   {
      printf("get_db_context - error resource table\n");
      rval = EPERS_NOPRCTABLE;
   }

   if(resourceFound == 0)
   {
      printf("get_db_context - error resource not found %s \n", resource_id);
      rval = EPERS_NOKEY;
   }

   return rval;
}



// status: OK
int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
{
   int storePolicy = PersistenceStoragePolicy_LastEntry;

   //
   // create resource database key
   //
   if((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF) )
   {
      // The LDBID is used to find the DBID in the resource table.
      if((dbContext->context.user_no == 0) && (dbContext->context.seat_no == 0))
      {
         //
         // Node is added in front of the resource ID as the key string.
         //
         snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
      }
      else
      {
         //
         // Node is added in front of the resource ID as the key string.
         //
         if(dbContext->context.seat_no == 0)
         {
            // /User/<user_no_parameter> is added in front of the resource ID as the key string.
            snprintf(dbKey, DbKeyMaxLen, "%s%d/%s", gUser, dbContext->context.user_no, resource_id);
         }
         else
         {
            // /User/<user_no_parameter>/Seat/<seat_no_parameter> is added in front of the resource ID as the key string.
            snprintf(dbKey, DbKeyMaxLen, "%s%d%s%d/%s", gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
         }
      }
      storePolicy = PersistenceStorage_local;
   }

   if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF))
   {
      // The LDBID is used to find the DBID in the resource table.
      // /<LDBID parameter> is added in front of the resource ID as the key string.
      //  Rational: Creates a namespace within one data base.
      //  Rational: Reduction of number of databases -> reduction of maintenance costs
      // /User/<user_no_parameter> and /Seat/<seat_no_parameter> are add after /<LDBID parameter> if there are different than 0.

      if(dbContext->context.seat_no != 0)
      {
         snprintf(dbKey, DbKeyMaxLen, "/%x%s%d%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
      }
      else
      {
         snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
      }
      storePolicy = PersistenceStorage_local;
   }

   //
   // create resource database path
   //
   if(dbContext->context.ldbid < 0x80)
   {
      // S H A R E D  database

      if(dbContext->context.ldbid != 0)
      {
         // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
         //
         // shared  G R O U P  database * * * * * * * * * * * * *  * * * * * *
         //
         if(PersistencePolicy_wc == dbContext->configKey.policy)
         {
            if(isFile == ResIsNoFile)
               snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, gSharedCached);
            else
               snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, dbKey);
         }
         else if(PersistencePolicy_wt == dbContext->configKey.policy)
         {
            if(isFile == ResIsNoFile)
               snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, gSharedWt);
            else
               snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, dbKey);
         }
      }
      else
      {
         // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
         //
         // shared  P U B L I C  database * * * * * * * * * * * * *  * * * * *
         //
         if(PersistencePolicy_wc == dbContext->configKey.policy)
         {
            if(isFile == ResIsNoFile)
               snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, gSharedCached);
            else
               snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, dbKey);
         }
         else if(PersistencePolicy_wt == dbContext->configKey.policy)
         {
            if(isFile == ResIsNoFile)
               snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, gSharedWt);
            else
               snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, dbKey);
         }
      }

      storePolicy = PersistenceStorage_shared;   // we have a shared database
   }
   else
   {
      // L O C A L   database

      if(PersistencePolicy_wc == dbContext->configKey.policy)
      {
         if(isFile == ResIsNoFile)
            snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, gLocalCached);
         else
            snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, dbKey);
      }
      else if(PersistencePolicy_wt == dbContext->configKey.policy)
      {
         if(isFile == ResIsNoFile)
            snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, gLocalWt);
         else
            snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, dbKey);
      }

      storePolicy = PersistenceStorage_local;   // we have a local database
   }

   //printf("get_db_path_and_key - dbKey  : [key ]: %s \n",  dbKey);
   //printf("get_db_path_and_key - dbPath : [path]: %s\n\n", dbPath);
   return storePolicy;
}