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
 int
 saf_find_field_tmpls(SAF_ParMode pmode,         /* The parallel mode. */
                      SAF_Db *db,                /* the database context for this search (previously retrieved from base_space) */
                      const char *name,          /* The name of the field template. */
                      SAF_Algebraic *atype,      /* The algebraic type to limit the search to. Pass NULL if you do
                                                  * not want to limit the search by this parameter. */
                      SAF_Basis *basis,          /* The basis to limit the search to. Pass NULL if you do not want to
                                                  * limit the search by this parameter. */
                      SAF_Quantity *quantity,    /* The quantity to search for. Pass NULL if you do not want to
                                                  * limit the search by this parameter. */
                      int *num,                  /* For this and the succeeding argument [see Returned Handles]. */
                      SAF_FieldTmpl **found      /* For this and the preceding argument [see Returned Handles]. */
                      )
 {
     SAF_ENTER(saf_find_field_tmpls, SAF_PRECONDITION_ERROR);
     SAF_KEYMASK(SAF_FieldTmpl, key, mask);
     size_t      nfound;
     ss_scope_t  scope;

     SAF_REQUIRE(_saf_valid_pmode(pmode), SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                 _saf_errmsg("PMODE must be valid"));
     if (!_saf_is_participating_proc(pmode)) SAF_RETURN(-1);

     SAF_REQUIRE(!atype || SS_ALGEBRAIC(atype), SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                 _saf_errmsg("ATYPE must be a valid algebraic handle if supplied"));
     SAF_REQUIRE(!basis || SS_BASIS(basis), SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                 _saf_errmsg("BASIS must be a valid basis handle if supplied"));
     SAF_REQUIRE(!quantity || SS_QUANTITY(quantity), SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                 _saf_errmsg("QUANTITY must be a valid quantity handle if supplied"));

     ss_file_topscope(db, &scope);
     if (name) SAF_SEARCH_S(SAF_FieldTmpl, key, mask, name, name);
     if (quantity) SAF_SEARCH(SAF_FieldTmpl, key, mask, quantity, *quantity);
     if (basis) SAF_SEARCH(SAF_FieldTmpl, key, mask, basis, *basis);
     if (atype) SAF_SEARCH(SAF_FieldTmpl, key, mask, algebraic, *atype);

     if (!found) {
         /* Count the matches */
         assert(num);
         nfound = SS_NOSIZE;
         ss_pers_find(&scope, (ss_pers_t*)key, mask_count?(ss_persobj_t*)&mask:NULL, 0, &nfound, SS_PERS_TEST, NULL);
         *num = nfound;
     } else if (!*found) {
         /* Find all matches; library allocates results */
         nfound = SS_NOSIZE;
         *found = (ss_fieldtmpl_t*)ss_pers_find(&scope, (ss_pers_t*)key, mask_count?(ss_persobj_t*)&mask:NULL, 0, &nfound,
                                                NULL, NULL);
         if (num) *num = nfound;
     } else {
         /* Find limited matches; client allocates result buffer */
         assert(num);
         nfound = *num;
         if (NULL==ss_pers_find(&scope, (ss_pers_t*)key, mask_count?(ss_persobj_t*)&mask:NULL, 0, &nfound, (ss_pers_t*)*found,
                                _SAF_GLOBALS.find_detect_overflow)) {
             SAF_ERROR(SAF_CONSTRAINT_ERROR, _saf_errmsg("found too many matching objects"));
         }
         *num = nfound;
     }

     SAF_LEAVE(SAF_SUCCESS);
 }