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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* db_mindex.cc
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <malloc.h>
#include <strings.h>
#include <string.h>
#include <sys/param.h>
#include "db_headers.h"
#include "db.h"
#include "db_mindex.h"
#include "db_pickle.h"
#include "nisdb_mt.h"
#include "nisdb_ldap.h"
#include "ldap_nisdbquery.h"
#include "ldap_map.h"
#include "ldap_ruleval.h"
#include "ldap_scheme.h"
#include "ldap_parse.h"
#include "nis_hashitem.h"
/*
* Constructor: Create new table using scheme defintion supplied.
* (Make copy of scheme and keep it with table.)
*/
db_mindex::db_mindex(db_scheme *how, char *tablePath) : rversion()
{
noWriteThrough.flag = 0;
noLDAPquery.flag = 0;
initialLoad.flag = 0;
objPath.ptr = NULL;
init(how);
if (tablePath != NULL)
configure(tablePath);
}
/* Constructor: Create empty table (no scheme, no table or indices). */
db_mindex::db_mindex() : rversion()
{
scheme = NULL;
table = NULL;
indices.indices_len = 0;
indices.indices_val = NULL;
noWriteThrough.flag = 0;
noLDAPquery.flag = 0;
initialLoad.flag = 0;
objPath.ptr = NULL;
INITRW(mindex);
}
db_mindex::~db_mindex()
{
reset(); /* get rid of data structures first */
DESTROYRW(mindex);
}
/*
* Initialize table using information given in scheme 'how'.
* Record the scheme for later use (make copy of it);
* create the required number of indices; and create table for storing
* entries.
*/
void
db_mindex::init(db_scheme * how)
{
scheme = new db_scheme(how); // make copy
if (scheme == NULL)
FATAL("db_mindex::init: could not allocate space for scheme",
DB_MEMORY_LIMIT);
if (scheme->numkeys() == 0) {
WARNING("db_mindex::init: empty scheme encountered");
/* what action should we take here? */
}
indices.indices_len = how->numkeys();
db_key_desc * keys = how->keyloc();
int i;
/* homogeneous indices for now */
indices.indices_val = new db_index[indices.indices_len];
if (indices.indices_val == NULL) {
delete scheme;
indices.indices_len = 0;
scheme = NULL;
FATAL("db_mindex::init: could not allocate space for indices",
DB_MEMORY_LIMIT);
}
for (i = 0; i < indices.indices_len; i++) {
indices.indices_val[i].init(&(keys[i]));
}
table = new db_table();
if (table == NULL) {
delete scheme;
scheme = NULL;
delete indices.indices_val;
indices.indices_val = NULL;
indices.indices_len = 0;
FATAL("db_mindex::init: could not allocate space for table",
DB_MEMORY_LIMIT);
}
rversion.zero();
INITRW(mindex);
objPath.ptr = NULL;
}
/* empty associated tables associated */
void
db_mindex::reset_tables()
{
int i;
WRITELOCKV(this, "w db_mindex::reset_tables");
/* Add sanity check in case of table corruption */
if (indices.indices_val != NULL) {
for (i = 0; i < indices.indices_len; i++) {
indices.indices_val[i].reset();
}
}
if (table) table->reset();
WRITEUNLOCKV(this, "wu db_mindex::reset_tables");
}
/*
* Return a list of index_entries that satsify the given query 'q'.
* Return the size of the list in 'count'. Return NULL if list is empty.
* Return in 'valid' FALSE if query is not well formed.
*/
db_index_entry_p
db_mindex::satisfy_query(db_query *q, long *count, bool_t *valid) {
return (satisfy_query(q, count, valid, FALSE));
}
db_index_entry_p
db_mindex::satisfy_query(db_query *q, long *count, bool_t *valid,
bool_t fromLDAP) {
db_index_entry_p ret;
bool_t validRequest;
int queryRes;
/* Make sure we have somewhere to store the "request valid" status */
if (valid == NULL)
valid = &validRequest;
/* Prepare for a failed lock */
*count = 0;
*valid = FALSE;
READLOCK(this, NULL, "r db_mindex::satisfy_query");
/*
* Only get data from LDAP if the caller requested it,
* and if we're mapping for this table.
*/
fromLDAP = (fromLDAP && !noLDAPquery.flag &&
(table->mapping.fromLDAP ||
table->mapping.objType != NIS_TABLE_OBJ));
/*
* If we always fetch data from LDAP for query's, then do so now,
* before invoking the "real" satisfy_query().
*/
if (fromLDAP && table->mapping.matchFetch == mat_always) {
int lockcode = 0;
READLOCKNR(table, lockcode,
"r db_mindex::satisfy_query table");
if (lockcode != 0) {
READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
return (NULL);
}
queryRes = queryLDAP(q, 0, 1);
READUNLOCKNR(table, lockcode,
"ru db_mindex::satisfy_query table");
if (lockcode != 0) {
READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
return (NULL);
}
if (queryRes != LDAP_SUCCESS) {
/* queryLDAP() sets error codes etc. */
READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
return (NULL);
}
}
ret = satisfy_query_dbonly(q, count, fromLDAP ? TRUE : FALSE, valid);
/* If we found it, or if we're not mapping, return */
if (ret != NULL || !fromLDAP) {
READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
return (ret);
} else if (ret == NULL && !(*valid)) {
/* No result, and the request wasn't valid */
READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
return (NULL);
}
/* Get data from LDAP */
if (table->mapping.matchFetch != mat_never) {
queryRes = queryLDAP(q, 0, 1);
} else {
/*
* We'll now go on to check for an un-expired entry again,
* even though we're pretty sure that won't work (already
* did that, and nothing's changed). However, we accept that
* slight inefficiency in the interest of keeping the code
* simple; we expect 'mat_never' to be used very rarely.
*/
queryRes = LDAP_SUCCESS;
}
if (queryRes == LDAP_SUCCESS) {
/*
* Check if we've got a match now. If not, try one
* last time for an expired match.
*/
ret = satisfy_query_dbonly(q, count, TRUE, valid);
if (ret == NULL) {
ret = satisfy_query_dbonly(q, count, FALSE, valid);
}
} else {
/*
* Check if we have an expired entry; if so, return
* it with an appropriate status.
*/
ret = satisfy_query_dbonly(q, count, FALSE, valid);
}
READUNLOCK(this, NULL, "ru db_mindex::satisfy_query");
return (ret);
}
db_index_entry_p
db_mindex::satisfy_query_dbonly(db_query *q, long *count,
bool_t checkExpire, bool_t *valid)
{
db_index_entry_p oldres = NULL, newres;
int i, curr_ind;
long num_new, num_old = 0;
int limit = q->size();
db_qcomp * comps = q->queryloc();
if (valid) *valid = TRUE; /* True to begin with. */
/* Add sanity check in case table corrupted */
if (indices.indices_len != 0 && indices.indices_val == NULL) {
WARNING("db_mindex::satisfy_query: table has no indices");
if (valid) *valid = FALSE;
*count = 0;
return (NULL);
}
for (i = 0; i < limit; i++) {
if ((curr_ind = comps[i].which_index) < indices.indices_len) {
newres = indices.indices_val[curr_ind].lookup(
comps[i].index_value, &num_new,
table, checkExpire);
if (newres == NULL) {
*count = 0;
return (NULL);
}
if (oldres == NULL) {
oldres = newres;
num_old = num_new;
} else {
oldres = newres->join(num_new, num_old,
oldres, &num_old);
if (oldres == NULL) {
*count = 0;
return (NULL);
}
}
} else {
WARNING("db_mindex::satisfy_query: index out of range");
if (valid) *valid = FALSE;
*count = 0;
return (NULL);
}
}
*count = num_old;
return (oldres);
}
/*
* Returns an array of size 'count' of 'entry_object_p's, pointing to
* copies of entry_objects named by the result list of db_index_entries 'res'.
* Sets db_status 'statp' if error encountered; otherwise, leaves it unchanged.
*/
entry_object_p *
db_mindex::prepare_results(int count, db_index_entry_p res, db_status *statp)
{
READLOCK(this, NULL, "r db_mindex::prepare_results");
READLOCK2(table, NULL, "r table db_mindex::prepare_results", this);
entry_object_p * entries = new entry_object_p[count];
int i;
if (entries == NULL) {
READUNLOCK2(this, table, NULL, NULL,
"ru db_mindex::prepare_results: could not allocate space",
"ru table db_mindex::prepare_results: could not allocate space");
FATAL3("db_mindex::prepare_results: could not allocate space",
DB_MEMORY_LIMIT, NULL);
}
for (i = 0; i < count; i++) {
if (res == NULL) {
int j;
for (j = 0; j < i; j++) // cleanup
free_entry(entries[j]);
syslog(LOG_ERR,
"db_mindex::prepare_results: incorrect count");
*statp = DB_INTERNAL_ERROR;
} else {
entries[i] =
new_entry(table->get_entry(res->getlocation()));
res = res->getnextresult();
}
}
READUNLOCK2(this, table, entries, entries,
"ru db_mindex::prepare_results",
"ru db_mindex::prepare_results");
return (entries);
}
/*
* Returns a newly created db_query structure containing the index values
* as obtained from the record named by 'recnum'. The record itself, along
* with information on the schema definition of this table, will determine
* which values are extracted from the record and placed into the result.
* Returns NULL if recnum is not a valid entry.
* Note that space is allocated for the query and the index values
* (i.e. do not share pointers with strings in 'obj'.)
*/
db_query *
db_mindex::extract_index_values_from_record(entryp recnum)
{
db_query *ret;
ret = extract_index_values_from_object(table->get_entry(recnum));
return (ret);
}
/*
* Returns a newly created db_query containing the index values as
* obtained from the given object. The object itself,
* along with information on the scheme given, will determine
* which values are extracted from the object and placed into the query.
* Returns an empty query if 'obj' is not a valid entry.
* Note that space is allocated for the query and the index values
* (i.e. do not share pointers with strings in 'obj'.)
*/
db_query *
db_mindex::extract_index_values_from_object(entry_object_p obj)
{
READLOCK(this, NULL, "r db_mindex::extract_index_values_from_object");
if (scheme->numkeys() != indices.indices_len) { // probably built wrong
syslog(LOG_ERR,
"number of keys (%d) does not equal number of indices (%d)",
scheme->numkeys(), indices.indices_len);
READUNLOCK(this, NULL,
"ru db_mindex::extract_index_values_from_object");
return (new db_query()); // null query
} else if (obj == NULL) {
READUNLOCK(this, NULL,
"ru db_mindex::extract_index_values_from_object");
return (NULL);
} else {
db_query* answer = new db_query(scheme, obj);
if (answer) {
/*
* XXX If the unlock fails, and we return NULL,
* we leak 'answer'. On the other hand, if we
* return 'answer', the object may remain locked,
* but the caller doesn't know that anything
* went wrong.
*/
READUNLOCK(this, NULL,
"ru db_mindex::extract_index_values_from_object");
return (answer);
} else {
FATAL3("db_mindex::extract: could not allocate space",
DB_MEMORY_LIMIT, NULL);
}
}
READUNLOCK(this, NULL,
"ru db_mindex::extract_index_values_from_object");
return (NULL);
}
/*
* Returns the first entry found in the table by setting 'answer' to
* point to the a copy of entry_object. Returns DB_SUCCESS if found;
* DB_NOTFOUND otherwise.
*/
db_status
db_mindex::first(entryp *where, entry_object ** answer)
{
db_status ret = DB_SUCCESS;
/*
* table->first_entry() returns a pointer into the table, so
* we must keep the table read locked until we've copied the
* entry_object. In order to maintain lock integrity, we must
* lock the db_mindex (this) before the db_table (table).
*/
READLOCK(this, DB_LOCK_ERROR, "r db_mindex::first");
READLOCK2(table, DB_LOCK_ERROR, "r table db_mindex::first", this);
if (table->mapping.fromLDAP) {
struct timeval now;
(void) gettimeofday(&now, NULL);
if (now.tv_sec >= table->mapping.enumExpire) {
int queryRes = queryLDAP(0, 0, 1);
if (queryRes == LDAP_SUCCESS)
table->mapping.enumExpire = now.tv_sec +
table->mapping.ttl;
else {
READUNLOCK2(this, table,
DB_LOCK_ERROR, DB_LOCK_ERROR,
"ru db_mindex::first LDAP",
"ru table db_mindex::first LDAP");
return (DB_INTERNAL_ERROR);
}
}
}
entry_object_p ptr = table->first_entry(where);
if (ptr == NULL)
ret = DB_NOTFOUND;
else
*answer = new_entry(ptr);
READUNLOCK2(this, table, ret, ret,
"ru db_mindex::first", "ru table db_mindex::first");
return (ret);
}
/*
* Returns the next entry in the table after 'previous' by setting 'answer' to
* point to copy of the entry_object. Returns DB_SUCCESS if 'previous' is
* valid and next entry is found; DB_NOTFOUND otherwise. Sets 'where' to
* location of where entry is found for input as subsequent 'next' operation.
*/
db_status
db_mindex::next(entryp previous, entryp *where, entry_object **answer)
{
db_status ret = DB_SUCCESS;
READLOCK(this, DB_LOCK_ERROR, "r db_mindex::next");
READLOCK2(table, DB_LOCK_ERROR, "r db_mindex::next", this);
if (!(table->entry_exists_p(previous)))
ret = DB_NOTFOUND;
else {
entry_object * ptr = table->next_entry(previous, where);
if (ptr == NULL)
ret = DB_NOTFOUND;
else
*answer = new_entry(ptr);
}
READUNLOCK2(this, table, ret, ret,
"ru db_mindex::next", "ru table db_mindex::next");
return (ret);
}
static void
delete_result_list(db_next_index_desc* orig)
{
db_next_index_desc* curr, *save_next;
for (curr = orig; curr != NULL; 0) {
save_next = curr->next;
delete curr;
curr = save_next;
}
}
static db_next_index_desc *
copy_result_list(db_index_entry* orig)
{
db_next_index_desc *head = NULL, *curr;
db_index_entry *current;
for (current = orig; current != NULL;
current = current->getnextresult()) {
curr = new db_next_index_desc(current->getlocation(), head);
if (curr == NULL) {
FATAL3(
"db_mindex::copy_result_list: could not allocate space",
DB_MEMORY_LIMIT, NULL);
}
head = curr; // list is actually reversed
}
return (head);
}
/*
* Delete the given list of results; used when no longer interested in
* the results of the first/next query that returned this list.
*/
db_status
db_mindex::reset_next(db_next_index_desc *orig)
{
if (orig == NULL)
return (DB_NOTFOUND);
delete_result_list(orig);
return (DB_SUCCESS);
}
/*
* Finds entry that satisfy the query 'q'. Returns the first answer by
* setting the pointer 'answer' to point to a copy of it. 'where' is set
* so that the other answers could be gotten by passing 'where' to 'next'
* successively. Note that the answer is a pointer to a copy of the entry.
* Returns DB_SUCCESS if search was successful; DB_NOTFOUND otherwise.
*/
db_status
db_mindex::first(db_query *q,
db_next_index_desc **where, entry_object ** answer)
{
READLOCK(this, DB_LOCK_ERROR, "r db_mindex::first");
READLOCK2(table, DB_LOCK_ERROR, "r table db_mindex::first", this);
long count;
bool_t valid_query;
db_status ret = DB_SUCCESS;
db_index_entry * rp = satisfy_query(q, &count, &valid_query, TRUE);
if (valid_query != TRUE)
ret = DB_BADQUERY;
else if (rp == NULL) {
*answer = NULL;
ret = DB_NOTFOUND;
} else {
*where = copy_result_list(rp);
entry_object_p ptr = table->get_entry((*where)->location);
if (ptr == NULL)
ret = DB_NOTFOUND;
else
*answer = new_entry(ptr);
}
READUNLOCK2(this, table, ret, ret,
"ru db_mindex::first", "ru table db_mindex::first");
return (ret);
}
/*
* Returns the next entry in the table after 'previous' by setting 'answer' to
* point to copy of the entry_object. Next is next in chain of answers found
* in previous first search with query. Returns DB_SUCCESS if 'previous' is
* valid and next entry is found; DB_NOTFOUND otherwise. Sets 'where' to
* location of where entry is found for input as subsequent 'next' operation.
*/
db_status
db_mindex::next(db_next_index_desc *previous, db_next_index_desc **where,
entry_object **answer)
{
READLOCK(this, DB_LOCK_ERROR, "r db_mindex::next");
READLOCK2(table, DB_LOCK_ERROR, "r table db_mindex::next", this);
db_status ret = DB_SUCCESS;
if (previous == NULL)
ret = DB_NOTFOUND;
else {
// should further check validity of 'previous' pointer
*where = previous->next;
delete previous; // delete previous entry
if (*where == NULL)
ret = DB_NOTFOUND;
else {
entry_object * ptr =
table->get_entry((*where)->location);
if (ptr == NULL)
ret = DB_NOTFOUND;
else {
*answer = new_entry(ptr);
ret = DB_SUCCESS;
}
}
}
READUNLOCK2(this, table, ret, ret,
"ru db_mindex::next", "ru table db_mindex::next");
return (ret);
}
/*
* Finds entry that satisfy the query 'q'. Returns the answer by
* setting the pointer 'rp' to point to the list of answers.
* Note that the answers are pointers to the COPIES of entries.
* Returns the number of answers find in 'count'.
* Returns DB_SUCCESS if search found at least one answer;
* returns DB_NOTFOUND if none is found.
*/
db_status
db_mindex::lookup(db_query *q, long *count, entry_object_p **result)
{
bool_t valid_query;
db_index_entry * rp = satisfy_query(q, count, &valid_query, TRUE);
db_status stat = DB_SUCCESS;
if (valid_query != TRUE)
return (DB_BADQUERY);
if (rp == NULL) {
*result = NULL;
return (DB_NOTFOUND);
}
*result = prepare_results((int)*count, rp, &stat);
return (stat);
}
/*
* Return all entries within table. Returns the answer by
* setting the pointer 'rp' to point to the list of answers.
* Note that the answers are pointers to copies of the entries.
* Returns the number of answers find in 'count'.
* Returns DB_SUCCESS if search found at least one answer;
* returns DB_NOTFOUND if none is found.
*/
db_status
db_mindex::all(long *count, entry_object_p **result)
{
entry_object *ptr;
entryp where;
long how_many, i;
int lret = 0;
if (table == NULL) {
*result = NULL;
return (DB_NOTFOUND);
}
READLOCK(this, DB_LOCK_ERROR, "r db_mindex::all");
/* Read lock 'table' while we're traversing it */
READLOCKNR(table, lret, "r table db_mindex::all");
if (lret != 0) {
READUNLOCK(this, DB_LOCK_ERROR, "ru db_mindex::all");
return (DB_LOCK_ERROR);
}
if (table->mapping.fromLDAP) {
struct timeval now;
(void) gettimeofday(&now, NULL);
if (now.tv_sec >= table->mapping.enumExpire) {
int queryRes = queryLDAP(0, 0, 1);
if (queryRes != LDAP_SUCCESS) {
READUNLOCKNR(table, lret,
"ru table db_mindex::all LDAP");
READUNLOCK(this, DB_LOCK_ERROR,
"ru db_mindex::all LDAP");
return (DB_INTERNAL_ERROR);
}
}
}
if ((how_many = table->fullness()) <= 0) {
/*
* Set '*count' so that the caller avoids putting garbage
* in an 'objects_len' field.
*/
*count = 0;
*result = NULL;
READUNLOCKNR(table, lret, "ru table db_mindex::all");
READUNLOCK(this, DB_NOTFOUND, "ru db_mindex::all");
return (DB_NOTFOUND);
}
entry_object_p * answer = new entry_object_p[how_many];
if (answer == NULL) {
READUNLOCKNR(table, lret, "ru table db_mindex::all");
READUNLOCK(this, DB_MEMORY_LIMIT, "ru db_mindex::all");
FATAL3("db_mindex::all: could not allocate space",
DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
}
*count = how_many;
ptr = table->first_entry(&where);
if (ptr != NULL)
answer[0] = new_entry(ptr);
else {
WARNING("db_mindex::all: null first entry found in all");
answer[0] = NULL;
}
for (i = 1; i < how_many; i++) {
ptr = table->next_entry(where, &where);
if (ptr != NULL)
answer[i] = new_entry(ptr);
else {
WARNING(
"db_mindex::all: null internal entry found in all");
answer[i] = NULL; /* Answer gets null too. -CM */
}
}
READUNLOCKNR(table, lret, "ru table db_mindex::all");
*result = answer;
READUNLOCK(this, DB_SUCCESS, "ru db_mindex::all");
return (DB_SUCCESS);
}
/*
* Remove the entry identified by 'recloc' from:
* 1. all indices, as obtained by extracting the index values from the entry
* 2. table where entry is stored.
*/
db_status
db_mindex::remove_aux(entryp recloc)
{
int i, curr_ind;
db_status res = DB_SUCCESS;
WRITELOCK(this, DB_LOCK_ERROR, "w db_mindex::remove_aux");
/* get index values of this record */
db_query * cq = extract_index_values_from_record(recloc);
if (cq == NULL) {
WRITEUNLOCK(this, DB_MEMORY_LIMIT, "wu db_mindex::remove_aux");
FATAL3("db_mindex::remove_aux: could not allocate space",
DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
}
if (cq->size() != indices.indices_len) { /* something is wrong */
delete cq; // clean up
syslog(LOG_ERR,
"db_mindex::remove_aux: record contains wrong number of indices");
WRITEUNLOCK(this, DB_INTERNAL_ERROR,
"wu db_mindex::remove_aux");
return (DB_INTERNAL_ERROR);
}
if (!noWriteThrough.flag) {
nis_object *o = 0;
entry_object *e = table->get_entry(recloc);
int queryRes, doingModify;
/*
* If the removal is part of a modify operation, we
* defer the LDAP update until the modified NIS+ object
* is added back.
*/
if (saveOldObjForModify((entry_obj *)e, &doingModify) == 0)
res = DB_INTERNAL_ERROR;
if (res == DB_SUCCESS && !doingModify) {
/*
* If we're removing a directory entry, and the
* entry is LDAP-mapped, but the directory isn't,
* we need a copy of the entry object in order
* to remove if from LDAP.
*/
if (e != 0 && e->en_type != 0 &&
strcmp(e->en_type, "IN_DIRECTORY") == 0)
o = unmakePseudoEntryObj(e, 0);
queryRes = removeLDAP(cq, o);
if (queryRes != LDAP_SUCCESS) {
if (table->mapping.storeErrorDisp == abandon)
res = DB_INTERNAL_ERROR;
}
if (o != 0)
nis_destroy_object(o);
}
}
if (res == DB_SUCCESS) {
db_qcomp * comps = cq->queryloc();
/* Add sanity check in case of corrupted table */
if (indices.indices_val != NULL) {
/* update indices */
for (i = 0; i < indices.indices_len; i++) {
/* unnec. if sorted */
curr_ind = comps[i].which_index;
indices.indices_val[curr_ind].remove(
comps[i].index_value, recloc);
}
}
/* update table where record is stored */
table->delete_entry(recloc);
}
/* delete query */
delete cq;
WRITEUNLOCK(this, DB_SUCCESS, "wu db_mindex::remove_aux");
return (res);
}
/*
* Removes the entry in the table named by given query 'q'.
* If a NULL query is supplied, all entries in table are removed.
* Returns DB_NOTFOUND if no entry is found.
* Returns DB_SUCCESS if one entry is found; this entry is removed from
* its record storage, and it is also removed from all the indices of the
* table. If more than one entry satisfying 'q' is found, all are removed.
*/
db_status
db_mindex::remove(db_query *q)
{
long count = 0;
db_index_entry *rp;
db_status rstat;
bool_t valid_query;
WRITELOCK(this, DB_LOCK_ERROR, "w db_mindex::remove");
WRITELOCK2(table, DB_LOCK_ERROR, "w table db_mindex::remove", this);
if (q == NULL) { /* remove all entries in table */
if (table->mapping.toLDAP && !noWriteThrough.flag) {
int queryRes = removeLDAP(q, 0);
#ifdef NISDB_LDAP_DEBUG
if (queryRes != LDAP_SUCCESS)
abort();
#endif /* NISDB_LDAP_DEBUG */
}
if (table != NULL && table->getsize() > 0) {
reset_tables();
WRITEUNLOCK2(table, this, DB_SUCCESS, DB_SUCCESS,
"wu table db_mindex::remove",
"wu db_mindex::remove");
return (DB_SUCCESS);
} else {
WRITEUNLOCK2(table, this, DB_NOTFOUND, DB_NOTFOUND,
"wu table db_mindex::remove",
"wu db_mindex::remove");
return (DB_NOTFOUND);
}
}
rp = satisfy_query(q, &count, &valid_query, FALSE);
if (valid_query != TRUE) {
WRITEUNLOCK2(table, this, DB_BADQUERY, DB_BADQUERY,
"wu table db_mindex::remove", "wu db_mindex::remove");
return (DB_BADQUERY);
}
if (count == 0) { /* not found */
WRITEUNLOCK2(table, this, DB_NOTFOUND, DB_NOTFOUND,
"wu table db_mindex::remove", "wu db_mindex::remove");
return (DB_NOTFOUND);
} else if (count == 1) { /* found, update indices */
db_status s;
s = remove_aux(rp->getlocation());
WRITEUNLOCK2(table, this, s, s,
"wu table db_mindex::remove", "wu db_mindex::remove");
return (s);
} else { /* ambiguous, remove all entries */
int i;
db_index_entry *next_entry;
for (i = 0; i < count; i++) {
if (rp == NULL) {
syslog(LOG_ERR,
"db_mindex::remove: incorrect number of indices");
WRITEUNLOCK2(table, this, DB_INTERNAL_ERROR,
DB_INTERNAL_ERROR,
"wu table db_mindex::remove",
"wu db_mindex::remove");
return (DB_INTERNAL_ERROR);
}
next_entry = rp->getnextresult(); // save before removal
rstat = remove_aux(rp->getlocation());
if (rstat != DB_SUCCESS) {
WRITEUNLOCK2(table, this, rstat, rstat,
"wu table db_mindex::remove",
"wu db_mindex::remove");
return (rstat);
}
rp = next_entry; // go on to next
}
WRITEUNLOCK2(table, this, DB_SUCCESS, DB_SUCCESS,
"wu table db_mindex::remove", "wu db_mindex::remove");
return (DB_SUCCESS);
}
}
/*
* Add copy of given entry to table. Entry is identified by query 'q'.
* The entry (if any) satisfying the query is first deleted, then
* added to the indices (using index values extracted form the given entry)
* and the table.
* Returns DB_NOTUNIQUE if more than one entry satisfies the query.
* Returns DB_NOTFOUND if query is not well-formed.
* Returns DB_SUCCESS if entry can be added.
*/
db_status
db_mindex::add(db_query *q, entry_object * obj)
{
long count = 0;
int i, curr_ind;
bool_t valid;
db_index_entry *rp = NULL;
db_status rstat;
const char *myself = "db_mindex::add";
/*
* The argument q is only NULL when we know that there are
* no objects in the database that match the object.
*/
WRITELOCK(this, DB_LOCK_ERROR, "w db_mindex::add");
WRITELOCK2(table, DB_LOCK_ERROR, "w table db_mindex::add", this);
if (q) {
rp = satisfy_query(q, &count, &valid, FALSE);
if (!valid) {
WRITEUNLOCK2(this, table, DB_LOCK_ERROR, DB_LOCK_ERROR,
"wu db_mindex::add",
"wu table db_mindex::add");
return (DB_BADQUERY);
}
}
if (count == 1) { /* found, first delete */
rstat = remove_aux(rp->getlocation());
if (rstat != DB_SUCCESS) {
WRITEUNLOCK2(this, table, rstat, rstat,
"wu db_mindex::add",
"wu table db_mindex::add");
return (rstat);
}
count = 0; /* fall through to add */
}
if (count == 0) { /* not found, insert */
/* add object to table */
entryp recloc = table->add_entry(obj, initialLoad.flag);
/* get index values of this object, might be same as 'q' */
db_query *cq = extract_index_values_from_object(obj);
if (cq == NULL) {
table->delete_entry(recloc);
WRITEUNLOCK2(this, table,
DB_MEMORY_LIMIT, DB_MEMORY_LIMIT,
"wu db_mindex::add DB_MEMORY_LIMIT",
"wu table db_mindex::add DB_MEMORY_LIMIT");
FATAL3("db_mindex::add: could not allocate space for",
DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
}
if (cq ->size() != indices.indices_len) { /* something wrong */
table->delete_entry(recloc);
delete cq; // clean up
syslog(LOG_ERR,
"db_mindex::add: record contains wrong number of indices");
WRITEUNLOCK2(this, table,
DB_INTERNAL_ERROR, DB_INTERNAL_ERROR,
"wu db_mindex::add DB_INTERNAL_ERROR",
"wu table db_mindex::add DB_INTERNAL_ERROR");
return (DB_INTERNAL_ERROR);
}
db_qcomp * comps = cq->queryloc();
/* update indices */
if (indices.indices_val != NULL) {
for (i = 0; i < indices.indices_len; i++) {
curr_ind = comps[i].which_index;
indices.indices_val[curr_ind].add(
comps[i].index_value, recloc);
}
}
delete cq; // clean up
if (!noWriteThrough.flag) {
int queryRes;
entry_object *e = 0;
if (retrieveOldObjForModify((entry_obj **)&e) == 0) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: Error retrieving old object for LDAP update",
myself);
return (DB_INTERNAL_ERROR);
}
queryRes = storeLDAP(q, obj, 0, e, 0);
if (queryRes != LDAP_SUCCESS) {
if (table->mapping.storeErrorDisp == abandon) {
WRITEUNLOCK2(this, table,
DB_INTERNAL_ERROR,
DB_INTERNAL_ERROR,
"wu db_mindex::add LDAP",
"wu table db_mindex::add LDAP");
return (DB_INTERNAL_ERROR);
} else {
logmsg(MSG_NOTIMECHECK, LOG_WARNING,
"%s: LDAP store failed: %s",
myself,
ldap_err2string(queryRes));
}
}
}
rstat = DB_SUCCESS;
} else /* ambiguous */
rstat = DB_NOTUNIQUE;
WRITEUNLOCK2(this, table, rstat, rstat,
"wu db_mindex::add",
"wu table db_mindex::add");
return (rstat);
}
/* ************************* pickle_mindex ********************* */
/* Does the actual writing to/from file specific for db_mindex structure. */
static bool_t
transfer_aux(XDR* x, pptr rp)
{
return (xdr_db_mindex(x, (db_mindex*) rp));
}
class pickle_mindex: public pickle_file {
public:
pickle_mindex(char *f, pickle_mode m) : pickle_file(f, m) {}
/* Transfers db_mindex structure pointed to by dp to/from file. */
int transfer(db_mindex* dp)
{
int ret;
WRITELOCK(dp, -1, "w pickle_mindex::transfer");
ret = pickle_file::transfer((pptr) dp, &transfer_aux);
WRITEUNLOCK(dp, ret, "wu pickle_mindex::transfer");
return (ret);
}
};
/* Write this structure (table, indices, scheme) into the specified file. */
int
db_mindex::dump(char *file)
{
pickle_mindex f(file, PICKLE_WRITE);
int status = f.transfer(this);
if (status == 1)
return (-1); /* could not open for write */
else
return (status);
}
/*
* Reset the table by: deleting all the indices, table of entries, and its
* scheme.
*/
void
db_mindex::reset()
{
WRITELOCKV(this, "w db_mindex::reset");
reset_tables(); /* clear table contents first */
if (indices.indices_val) {
delete [] indices.indices_val;
indices.indices_val = NULL;
}
if (table) { delete table; table = NULL; }
if (scheme) { delete scheme; scheme = NULL; }
indices.indices_len = 0;
rversion.zero();
if (objPath.ptr != 0) {
free(objPath.ptr);
objPath.ptr = 0;
}
WRITEUNLOCKV(this, "wu db_mindex::reset");
}
/*
* Initialize table using information from specified file.
* The table is first 'reset', then the attempt to load from the file
* is made. If the load failed, the table is again reset.
* Therefore, the table will be modified regardless of the success of the
* load. Returns 0 if successful, 1 if DB disk file couldn't be opened,
* -1 for various other failures.
*/
int
db_mindex::load(char *file)
{
pickle_mindex f(file, PICKLE_READ);
int status;
int init_table = (this->table == NULL);
int init_scheme = (this->scheme == NULL);
WRITELOCK(this, -1, "w db_mindex::load");
reset();
/* load new mindex */
if ((status = f.transfer(this)) != 0) {
/* load failed. Reset. */
reset();
}
/* Initialize the 'scheme' locking */
if (status == 0 && this->scheme != 0 && init_scheme) {
/*
* Since we've added fields to the db_scheme that aren't
* read from disk, we need to re-allocate so that the
* db_scheme instance is large enough.
*/
db_scheme *tmpscheme = new db_scheme();
if (tmpscheme != 0) {
(void) memcpy(tmpscheme, this->scheme,
this->scheme->oldstructsize());
free(this->scheme);
this->scheme = tmpscheme;
} else {
status = -1;
}
}
/*
* If the 'table' field was NULL before the load, but not now,
* initialize the table locking and mapping.
*/
if (status == 0 && this->table != 0 && init_table) {
/*
* As for the db_scheme, make sure the db_table is large
* enough.
*/
db_table *tmptable = new db_table();
if (tmptable != 0) {
(void) memcpy(tmptable, this->table,
this->table->oldstructsize());
free(this->table);
this->table = tmptable;
(void) this->configure(file);
} else {
status = -1;
}
}
if (status == 0 && this->indices.indices_val != NULL) {
/*
* Recreate the db_index instance so that it is
* correctly initialized.
*/
db_index *tmp_indices;
int n_index = this->indices.indices_len;
tmp_indices = new db_index[n_index];
if (tmp_indices != NULL) {
for (int i = 0; i < n_index; i++) {
if (tmp_indices[i].move_xdr_db_index
(&this->indices.indices_val[i]) != DB_SUCCESS) {
status = -1;
break;
}
}
free(this->indices.indices_val);
this->indices.indices_val = tmp_indices;
this->indices.indices_len = n_index;
} else {
status = -1;
}
}
WRITEUNLOCK(this, status, "wu db_mindex::load");
return (status);
}
/*
* Prints statistics of the table. This includes the size of the table,
* the number of entries, and the index sizes.
*/
void
db_mindex::print_stats()
{
long size, count, i;
long *stats = table->stats(TRUE);
printf("table_size = %d\n", stats[0]);
printf("last_used = %d\n", stats[1]);
printf("count = %d\n", stats[2]);
printf("free list size = %d\n", stats[3]);
printf("free list count = %d\n", stats[4]);
for (i = 5; i < 5+stats[4]; i++) {
printf("%d, ", stats[i]);
}
printf("\n");
free((char *)stats);
/* Add sanity check in case of corrupted table */
if (indices.indices_val == NULL) {
printf("No indices to print\n");
return;
}
for (i = 0; i < indices.indices_len; i++) {
printf("***** INDEX %d ******\n", i);
indices.indices_val[i].stats(&size, &count);
printf("index table size = %d\ncount = %d\n", size, count);
}
}
/* Prints statistics about all indices of table. */
void
db_mindex::print_all_indices()
{
int i;
READLOCKV(this, "r db_mindex::print_all_indices");
/* Add sanity check in case of corrupted table */
if (indices.indices_val == NULL) {
printf("No indices to print\n");
READUNLOCKV(this, "ru db_mindex::print_all_indices");
return;
}
for (i = 0; i < indices.indices_len; i++) {
printf("***** INDEX %d ******\n", i);
indices.indices_val[i].print();
}
READUNLOCKV(this, "ru db_mindex::print_all_indices");
}
/* Prints statistics about indices identified by 'n'. */
void
db_mindex::print_index(int n)
{
READLOCKV(this, "r db_mindex::print_index");
if (n >= 0 && n < indices.indices_len)
indices.indices_val[n].print();
READUNLOCKV(this, "ru db_mindex::print_index");
}
|