irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

index.cpp
Go to the documentation of this file.
1 /* For copyright information please refer to files in the COPYRIGHT directory
2  */
3 #include "index.hpp"
4 #include "rules.hpp"
5 #include "debug.hpp"
6 #include "configuration.hpp"
7 #define RE_ERROR(x) if(x) { goto error; }
8 
9 #include <assert.h>
10 
14 
16  if ( *ruleIndex != NULL ) {
18  *ruleIndex = NULL;
19  }
20 }
25  if ( ruleIndex == NULL ) {
26  return 0;
27  }
28  for ( int i = 0; i < inRuleStrct->MaxNumOfRules; i++ ) {
29  char *key = inRuleStrct->action[i];
30  int *value = ( int * )malloc( sizeof( int ) );
31  *value = i;
32 
33  if ( 0 == insertIntoHashTable( ruleIndex, key, value ) ) {
34  free( value );
35  return 0;
36  }
37  free( value );
38  }
39  return 1;
40 }
41 
43  /* generate rule condition index */
44  int i;
45  for ( i = 0; i < ruleEngineConfig.coreFuncDescIndex->current->size; i++ ) {
47 
48  struct bucket *resumingBucket = b;
49 
50  while ( resumingBucket != NULL ) {
51 
52  FunctionDesc *fd = ( FunctionDesc * ) resumingBucket->value;
53  resumingBucket = resumingBucket->next;
54  if ( getNodeType( fd ) != N_FD_RULE_INDEX_LIST ) {
55  continue;
56  }
58 #ifdef DEBUG_INDEX
59  printf( "processing rule pack %s into condIndex\n", resumingBucket->key );
60 #endif
61 
62 
63  RuleIndexListNode *currIndexNode = ruleIndexList->head;
64 
65  while ( currIndexNode != NULL ) {
66  Hashtable *processedStrs = newHashTable2( MAX_NUM_RULES * 2, r );
67  RuleIndexListNode *startIndexNode = currIndexNode;
68  RuleIndexListNode *finishIndexNode = NULL;
69  int groupCount = 0;
70  Node *condExp = NULL;
71  Node *params = NULL;
72 
73  while ( currIndexNode != NULL ) {
74  RuleDesc *rd = getRuleDesc( currIndexNode->ruleIndex );
75  Node *ruleNode = rd->node;
76  if ( !(
77  rd->ruleType == RK_REL
78  ) ) {
79  finishIndexNode = currIndexNode;
80  currIndexNode = currIndexNode->next;
81  break;
82  }
83  Node *ruleCond = ruleNode->subtrees[1];
84  while ( getNodeType( ruleCond ) == N_TUPLE && ruleCond->degree == 1 ) {
85  ruleCond = ruleCond->subtrees[0];
86  }
87  if ( !(
88  getNodeType( ruleCond ) == N_APPLICATION &&
89  getNodeType( ruleCond->subtrees[0] ) == TK_TEXT &&
90  strcmp( ruleCond->subtrees[0]->text, "==" ) == 0 && /* comparison */
91  getNodeType( ruleCond->subtrees[1] ) == N_TUPLE &&
92  ruleCond->subtrees[1]->degree == 2 &&
93  getNodeType( ruleCond->subtrees[1]->subtrees[1] ) == TK_STRING /* with a string */
94  ) ) {
95  finishIndexNode = currIndexNode;
96  currIndexNode = currIndexNode->next;
97  break;
98  }
99  char *strVal = ruleCond->subtrees[1]->subtrees[1]->text;
100  if ( lookupFromHashTable( processedStrs, strVal ) != NULL /* no repeated string */
101  ) {
102  finishIndexNode = currIndexNode;
103  break;
104  }
105  int i;
106  if ( condExp == NULL ) {
107  condExp = ruleCond->subtrees[1]->subtrees[0];
108  params = ruleNode->subtrees[0]->subtrees[0];
109 
110  }
111  else if ( RULE_NODE_NUM_PARAMS( ruleNode ) == params->degree ) {
112  Hashtable *varMapping = newHashTable2( 100, r );
113  for ( i = 0; i < params->degree; i++ ) {
114  updateInHashTable( varMapping, params->subtrees[i]->text, ruleNode->subtrees[0]->subtrees[0]->subtrees[i]->text );
115  }
116  if ( !eqExprNodeSyntacticVarMapping( condExp, ruleCond->subtrees[0], varMapping ) ) {
117  finishIndexNode = currIndexNode;
118  break;
119  }
120  }
121  else {
122  finishIndexNode = currIndexNode;
123  break;
124  }
125 
126  insertIntoHashTable( processedStrs, strVal, strVal );
127  groupCount ++;
128 
129  currIndexNode = currIndexNode ->next;
130  }
131 
132  /* generate cond index if there are more than COND_INDEX_THRESHOLD rules in this group */
133  if ( groupCount < COND_INDEX_THRESHOLD ) {
134  continue;
135  }
136 
137 #ifdef DEBUG_INDEX
138  printf( "inserting rule group %s(%d) into ruleEngineConfig.condIndex\n", resumingBucket->key, groupCount );
139 #endif
140  Hashtable *groupHashtable = newHashTable2( groupCount * 2, r );
141  CondIndexVal *civ = newCondIndexVal( condExp, params, groupHashtable, r );
142 
143  RuleIndexListNode *instIndexNode = startIndexNode;
144  while ( instIndexNode != finishIndexNode ) {
145  int ri = instIndexNode->ruleIndex;
146  Node *ruleNode = getRuleDesc( ri )->node;
147  removeNodeFromRuleIndexList( ruleIndexList, instIndexNode );
148  Node *ruleCond = ruleNode->subtrees[1];
149  while ( getNodeType( ruleCond ) == N_TUPLE && ruleCond->degree == 1 ) {
150  ruleCond = ruleCond->subtrees[0];
151  }
152  char *strVal = ruleCond->subtrees[1]->subtrees[1]->text;
153 #ifdef DEBUG_INDEX
154  printf( "inserting rule cond str %s, index %d into ruleEngineConfig.condIndex\n", strVal, ri );
155 #endif
156 
157  insertIntoHashTable( groupHashtable, strVal, instIndexNode );
158  instIndexNode = instIndexNode->next;
159  }
160  insertIntoRuleIndexList( ruleIndexList, startIndexNode->prev, civ, r );
161  }
162 
163  }
164 
165  }
166  return 1;
167 
168 }
170  if ( prev == NULL ) {
171  RuleIndexListNode *n = newRuleIndexListNode2( civ, prev, rd->head, r );
172  rd->head = n;
173  if ( rd->tail == NULL ) {
174  rd->tail = n;
175  }
176  }
177  else {
178  RuleIndexListNode *n = newRuleIndexListNode2( civ, prev, prev->next, r );
179  if ( prev->next == NULL ) {
180  rd->tail = n;
181  }
182  prev->next = n;
183  }
184 }
186  RuleIndexListNode *n = rd->head;
187  while ( n != NULL && n->ruleIndex != i ) {
188  n = n->next;
189  }
190  if ( n != NULL ) {
192  }
193 }
195  if ( n == rd->head ) {
196  rd->head = n->next;
197  }
198  if ( n == rd->tail ) {
199  rd->tail = n->next;
200  }
201  if ( n->next != NULL ) {
202  n->next->prev = n->prev;
203  }
204  if ( n->prev != NULL ) {
205  n->prev->next = n->next;
206  }
207 }
210  list->tail ->next = listNode;
211  list->tail = listNode;
212 
213 }
216  listNode->next = list->head;
217  list->head = listNode;
218 
219 }
223 int createRuleNodeIndex( RuleSet *inRuleSet, Hashtable *ruleIndex, int offset, Region *r ) {
224  /* generate main index */
225  int i;
226  for ( i = 0; i < inRuleSet->len; i++ ) {
227  RuleDesc *rd = inRuleSet->rules[i];
228  Node *ruleNode = rd->node;
229  if ( ruleNode == NULL ) {
230  continue;
231  }
232  RuleType ruleType = rd->ruleType;
233  if ( ruleType == RK_REL || ruleType == RK_FUNC ) {
234  char *key = ruleNode->subtrees[0]->text;
236  if ( fd != NULL ) {
237  /* printf("adding %s\n", key);*/
238  if ( getNodeType( fd ) == N_FD_RULE_INDEX_LIST ) {
240  appendRuleNodeToRuleIndexList( list, i + offset, r );
241  }
242  else if ( getNodeType( fd ) == N_FD_EXTERNAL ) {
243  /* combine N_FD_EXTERNAL with N_FD_RULE_LIST */
244  if ( updateInHashTable( ruleIndex, key, newRuleIndexListFD( newRuleIndexList( key, i + offset, r ), fd->exprType, r ) ) == 0 ) {
245  return 0;
246  }
247  }
248  else {
249  /* todo error handling */
250  return -1;
251  }
252  }
253  else {
254  /* printf("inserting %s\n", key);*/
255  if ( insertIntoHashTable( ruleIndex, key, newRuleIndexListFD( newRuleIndexList( key, i + offset, r ), NULL, r ) ) == 0 ) {
256  return 0;
257  }
258  }
259  }
260  }
261 
262  return 1;
263 }
270  if ( *ruleIndex == NULL ) {
271  return 0;
272  }
273  for ( int i = 0; i < inFuncStrct->MaxNumOfFMaps; i++ ) {
274  char *key = inFuncStrct->funcName[i];
275  int *value = ( int * )malloc( sizeof( int ) );
276  *value = i;
277 
278  if ( 0 == insertIntoHashTable( *ruleIndex, key, value ) ) {
280  *ruleIndex = NULL;
281  free( value );
282  return 0;
283  }
284  free( value );
285  }
286  return 1;
287 }
288 
289 /* find the ith RuleIndexListNode */
291  int k = i;
292  if ( ruleIndex != NULL ) {
294  if ( fd != NULL ) {
295  if ( getNodeType( fd ) != N_FD_RULE_INDEX_LIST ) {
296  return NO_MORE_RULES_ERR;
297  }
299  RuleIndexListNode *b = l->head;
300  while ( k != 0 ) {
301  if ( b != NULL ) {
302  b = b->next;
303  k--;
304  }
305  else {
306  break;
307  }
308  }
309  if ( b != NULL ) {
310  *node = b;
311  return 0;
312  }
313  }
314  return findNextRuleFromIndex( ruleIndex->previous, action, k, node );
315  }
316 
317  return NO_MORE_RULES_ERR;
318 }
322 int findNextRule2( const char *action, int i, RuleIndexListNode **node ) {
325  if ( ii != NO_MORE_RULES_ERR ) {
326  return 0;
327  }
328  else {
329  return NO_MORE_RULES_ERR;
330  }
331  }
332  else {
333  return NO_MORE_RULES_ERR;
334  }
335 }
336 
337 int mapExternalFuncToInternalProc2( char *funcName ) {
338  int *i;
339 
340  if ( appRuleFuncMapDefIndex != NULL && ( i = ( int * )lookupFromHashTable( appRuleFuncMapDefIndex, funcName ) ) != NULL ) {
341  strcpy( funcName, appRuleFuncMapDef.func2CMap[*i] );
342  return 1;
343  }
344  if ( coreRuleFuncMapDefIndex != NULL && ( i = ( int * )lookupFromHashTable( coreRuleFuncMapDefIndex, funcName ) ) != NULL ) {
345  strcpy( funcName, coreRuleFuncMapDef.func2CMap[*i] );
346  return 1;
347  }
348  return 0;
349 }
351  int *i;
352 
353  if ( ( i = ( int * )lookupFromHashTable( microsTableIndex, action ) ) != NULL ) {
354  return *i;
355  }
356 
357  return UNMATCHED_ACTION_ERR;
358 }
361 }
362 
363 char *convertRuleNameArityToKey( char *ruleName, int arity ) {
364  // assume that arity < 100
365  char *key = ( char * )malloc( strlen( ruleName ) + 3 );
366  sprintf( key, "%02d%s", arity, ruleName );
367  return key;
368 }
369 
370 RuleIndexList *newRuleIndexList( char *ruleName, int ruleIndex, Region *r ) {
372  list->ruleName = cpStringExt( ruleName, r );
374  return list;
375 }
376 
379  memset( node, 0, sizeof( RuleIndexListNode ) );
380  node->ruleIndex = ruleIndex;
381  node->secondaryIndex = 0;
382  node->prev = prev;
383  node->next = next;
384  return node;
385 
386 }
387 
390  memset( node, 0, sizeof( RuleIndexListNode ) );
391  node->condIndex = civ;
392  node->secondaryIndex = 1;
393  node->prev = prev;
394  node->next = next;
395  return node;
396 
397 }
398 CondIndexVal *newCondIndexVal( Node *condExp, Node *params, Hashtable *groupHashtable, Region *r ) {
399  CondIndexVal *civ = ( CondIndexVal * )region_alloc( r, sizeof( CondIndexVal ) );
400  civ->condExp = condExp;
401  civ->params = params;
402  civ->valIndex = groupHashtable;
403  return civ;
404 }
ruleSet::rules
RuleDesc * rules[50000]
Definition: restructs.hpp:284
NULL
#define NULL
Definition: rodsDef.h:70
COND_INDEX_THRESHOLD
#define COND_INDEX_THRESHOLD
Definition: index.hpp:14
RK_FUNC
@ RK_FUNC
Definition: restructs.hpp:265
bucket::key
char * key
Definition: irods_hashtable.h:12
FD_RULE_INDEX_LIST
#define FD_RULE_INDEX_LIST(x)
Definition: restructs.hpp:61
coreRuleFuncMapDef
rulefmapdef_t coreRuleFuncMapDef
Definition: libirods_rule_engine_plugin-irods_rule_language.cpp:49
index.hpp
irods.six.next
next
Definition: six.py:518
createCondIndex
int createCondIndex(Region *r)
Definition: index.cpp:42
rulefmapdef_t::MaxNumOfFMaps
int MaxNumOfFMaps
Definition: reGlobalsExtern.hpp:113
listNode
Definition: irods_list.h:8
ruleIndexList::tail
RuleIndexListNode * tail
Definition: restructs.hpp:222
createRuleNodeIndex
int createRuleNodeIndex(RuleSet *inRuleSet, Hashtable *ruleIndex, int offset, Region *r)
Definition: index.cpp:223
region_alloc
void * region_alloc(Region *r, size_t s)
Definition: region.cpp:138
removeNodeFromRuleIndexList2
void removeNodeFromRuleIndexList2(RuleIndexList *rd, int i)
Definition: index.cpp:185
getNodeType
#define getNodeType(x)
Definition: restructs.hpp:69
bucket
Definition: irods_hashtable.h:11
env
Definition: restructs.hpp:226
ruleIndexListNode::prev
struct ruleIndexListNode * prev
Definition: restructs.hpp:214
configuration.hpp
RuleDesc::ruleType
RuleType ruleType
Definition: restructs.hpp:276
ruleSet::len
int len
Definition: restructs.hpp:283
getRuleDesc
RuleDesc * getRuleDesc(int ri)
Definition: rules.cpp:595
RULE_NODE_NUM_PARAMS
#define RULE_NODE_NUM_PARAMS(r)
Definition: restructs.hpp:45
N_APPLICATION
@ N_APPLICATION
Definition: restructs.hpp:128
node
Definition: restructs.hpp:244
ruleIndexList::head
RuleIndexListNode * head
Definition: restructs.hpp:222
condIndexVal::condExp
Node * condExp
Definition: restructs.hpp:209
N_FD_RULE_INDEX_LIST
@ N_FD_RULE_INDEX_LIST
Definition: restructs.hpp:147
ruleIndexListNode::ruleIndex
int ruleIndex
Definition: restructs.hpp:216
run_tests.action
action
Definition: run_tests.py:114
clearIndex
void clearIndex(Hashtable **ruleIndex)
Definition: index.cpp:15
list::tail
ListNode * tail
Definition: irods_list.h:16
isComponentInitialized
#define isComponentInitialized(x)
Definition: configuration.hpp:72
node::text
char * text
Definition: restructs.hpp:252
free_const
void free_const(const void *a)
Definition: hashtable.cpp:287
appRuleFuncMapDefIndex
Hashtable * appRuleFuncMapDefIndex
Definition: index.cpp:12
hashtable::buckets
struct bucket ** buckets
Definition: irods_hashtable.h:17
appendRuleNodeToRuleIndexList
void appendRuleNodeToRuleIndexList(RuleIndexList *list, int i, Region *r)
Definition: index.cpp:208
newHashTable2
Hashtable * newHashTable2(int size, Region *r)
Definition: hashtable.cpp:72
ruleType
ruleType
Definition: restructs.hpp:263
ruleStruct_t::MaxNumOfRules
int MaxNumOfRules
Definition: reGlobalsExtern.hpp:92
TK_TEXT
@ TK_TEXT
Definition: restructs.hpp:114
node::subtrees
struct node ** subtrees
Definition: restructs.hpp:254
MAX_NUM_RULES
#define MAX_NUM_RULES
Definition: reconstants.hpp:27
eqExprNodeSyntacticVarMapping
int eqExprNodeSyntacticVarMapping(Node *a, Node *b, Hashtable *varMapping)
Definition: parser.cpp:2355
Cache::coreFuncDescIndex
Env * coreFuncDescIndex
Definition: configuration.hpp:57
N_FD_EXTERNAL
@ N_FD_EXTERNAL
Definition: restructs.hpp:146
condIndexVal::params
Node * params
Definition: restructs.hpp:208
rulefmapdef_t::funcName
char * funcName[2048]
Definition: reGlobalsExtern.hpp:114
createFuncMapDefIndex
int createFuncMapDefIndex(rulefmapdef_t *inFuncStrct, Hashtable **ruleIndex)
Definition: index.cpp:267
microsTableIndex
Hashtable * microsTableIndex
Definition: index.cpp:13
rules.hpp
ruleIndexList
Definition: restructs.hpp:220
node::degree
int degree
Definition: restructs.hpp:246
UNMATCHED_ACTION_ERR
@ UNMATCHED_ACTION_ERR
Definition: rodsErrorTable.h:595
newRuleIndexListNode2
RuleIndexListNode * newRuleIndexListNode2(CondIndexVal *civ, RuleIndexListNode *prev, RuleIndexListNode *next, Region *r)
Definition: index.cpp:388
coreRuleFuncMapDefIndex
Hashtable * coreRuleFuncMapDefIndex
Definition: index.cpp:11
NO_MORE_RULES_ERR
@ NO_MORE_RULES_ERR
Definition: rodsErrorTable.h:594
insertIntoHashTable
int insertIntoHashTable(Hashtable *h, const char *key, const void *value)
Definition: hashtable.cpp:99
ruleIndexListNode
Definition: restructs.hpp:213
get_irods_version.value
dictionary value
Definition: get_irods_version.py:27
removeNodeFromRuleIndexList
void removeNodeFromRuleIndexList(RuleIndexList *rd, RuleIndexListNode *n)
Definition: index.cpp:194
bucket::next
struct bucket * next
Definition: irods_hashtable.h:14
debug.hpp
Cache::extFuncDescIndexStatus
RuleEngineStatus extFuncDescIndexStatus
Definition: configuration.hpp:46
lookupFromHashTable
const void * lookupFromHashTable(Hashtable *h, const char *key)
Definition: hashtable.cpp:222
mapExternalFuncToInternalProc2
int mapExternalFuncToInternalProc2(char *funcName)
Definition: index.cpp:337
region
Definition: region.h:45
deleteCondIndexVal
void deleteCondIndexVal(CondIndexVal *h)
Definition: index.cpp:359
RuleDesc
Definition: restructs.hpp:272
list::head
ListNode * head
Definition: irods_list.h:15
get_db_schema_version.l
l
Definition: get_db_schema_version.py:19
listNode::next
ListNode * next
Definition: irods_list.h:9
newCondIndexVal
CondIndexVal * newCondIndexVal(Node *condExp, Node *params, Hashtable *groupHashtable, Region *r)
Definition: index.cpp:398
node::exprType
ExprType * exprType
Definition: restructs.hpp:250
N_TUPLE
@ N_TUPLE
Definition: restructs.hpp:127
condIndexVal
Definition: restructs.hpp:207
actionTableLookUp2
int actionTableLookUp2(char *action)
Definition: index.cpp:350
updateInHashTable
const void * updateInHashTable(Hashtable *h, const char *key, const void *value)
Definition: hashtable.cpp:164
insertIntoRuleIndexList
void insertIntoRuleIndexList(RuleIndexList *rd, RuleIndexListNode *prev, CondIndexVal *civ, Region *r)
Definition: index.cpp:169
irods.six.b
def b(s)
Definition: six.py:606
RuleDesc::node
Node * node
Definition: restructs.hpp:275
ruleIndex
Hashtable * ruleIndex
Cache::extFuncDescIndex
Env * extFuncDescIndex
Definition: configuration.hpp:59
rulefmapdef_t
Definition: reGlobalsExtern.hpp:112
findNextRule2
int findNextRule2(const char *action, int i, RuleIndexListNode **node)
Definition: index.cpp:322
env::current
Hashtable * current
Definition: restructs.hpp:227
TK_STRING
@ TK_STRING
Definition: restructs.hpp:115
newHashTable
Hashtable * newHashTable(int size)
Definition: hashtable.cpp:46
hashtable::size
int size
Definition: irods_hashtable.h:18
convertRuleNameArityToKey
char * convertRuleNameArityToKey(char *ruleName, int arity)
Definition: index.cpp:363
createRuleStructIndex
int createRuleStructIndex(ruleStruct_t *inRuleStrct, Hashtable *ruleIndex)
Definition: index.cpp:24
ruleEngineConfig
Cache ruleEngineConfig
Definition: configuration.cpp:68
appRuleFuncMapDef
rulefmapdef_t appRuleFuncMapDef
Definition: libirods_rule_engine_plugin-irods_rule_language.cpp:52
bucket::value
const void * value
Definition: irods_hashtable.h:13
newRuleIndexListNode
RuleIndexListNode * newRuleIndexListNode(int ruleIndex, RuleIndexListNode *prev, RuleIndexListNode *next, Region *r)
Definition: index.cpp:377
newRuleIndexList
RuleIndexList * newRuleIndexList(char *ruleName, int ruleIndex, Region *r)
Definition: index.cpp:370
condIndexVal::valIndex
Hashtable * valIndex
Definition: restructs.hpp:210
hashtable
Definition: irods_hashtable.h:16
ruleIndexListNode::next
struct ruleIndexListNode * next
Definition: restructs.hpp:214
prependRuleNodeToRuleIndexList
void prependRuleNodeToRuleIndexList(RuleIndexList *list, int i, Region *r)
Definition: index.cpp:214
ruleStruct_t
Definition: reGlobalsExtern.hpp:91
rulefmapdef_t::func2CMap
char * func2CMap[2048]
Definition: reGlobalsExtern.hpp:115
deleteHashTable
void deleteHashTable(Hashtable *h, void(*f)(const void *))
Definition: hashtable.cpp:260
RuleType
enum ruleType RuleType
nop
void nop(const void *a)
Definition: hashtable.cpp:284
cpStringExt
char * cpStringExt(const char *str, Region *r)
Definition: utils.cpp:358
ruleStruct_t::action
char * action[2048]
Definition: reGlobalsExtern.hpp:94
list
Definition: irods_list.h:13
newRuleIndexListFD
FunctionDesc * newRuleIndexListFD(RuleIndexList *ruleIndexList, ExprType *, Region *r)
Definition: restructs.cpp:357
findNextRuleFromIndex
int findNextRuleFromIndex(Env *ruleIndex, const char *action, int i, RuleIndexListNode **node)
Definition: index.cpp:290
MAX_NUM_OF_DVARS
#define MAX_NUM_OF_DVARS
Definition: reDefines.h:14
RK_REL
@ RK_REL
Definition: restructs.hpp:264
ruleSet
Definition: restructs.hpp:282