summaryrefslogtreecommitdiffstats
path: root/usr.sbin/nscd/cachelib.c
blob: ab95b2939fe42bf64cf61dd50c036ce34bb3f627 (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
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
/*-
 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/time.h>

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "cachelib.h"
#include "debug.h"

#define INITIAL_ENTRIES_CAPACITY 32
#define ENTRIES_CAPACITY_STEP 32

#define STRING_SIMPLE_HASH_BODY(in_var, var, a, M)		\
	for ((var) = 0; *(in_var) != '\0'; ++(in_var))		\
		(var) = ((a)*(var) + *(in_var)) % (M)

#define STRING_SIMPLE_MP2_HASH_BODY(in_var, var, a, M)		\
	for ((var) = 0; *(in_var) != 0; ++(in_var))		\
		(var) = ((a)*(var) + *(in_var)) & (M - 1)

static int cache_elemsize_common_continue_func(struct cache_common_entry_ *,
	struct cache_policy_item_ *);
static int cache_lifetime_common_continue_func(struct cache_common_entry_ *,
	struct cache_policy_item_ *);
static void clear_cache_entry(struct cache_entry_ *);
static void destroy_cache_entry(struct cache_entry_ *);
static void destroy_cache_mp_read_session(struct cache_mp_read_session_ *);
static void destroy_cache_mp_write_session(struct cache_mp_write_session_ *);
static int entries_bsearch_cmp_func(const void *, const void *);
static int entries_qsort_cmp_func(const void *, const void *);
static struct cache_entry_ ** find_cache_entry_p(struct cache_ *,
	const char *);
static void flush_cache_entry(struct cache_entry_ *);
static void flush_cache_policy(struct cache_common_entry_ *,
	struct cache_policy_ *, struct cache_policy_ *,
		int (*)(struct cache_common_entry_ *,
		struct cache_policy_item_ *));
static int ht_items_cmp_func(const void *, const void *);
static int ht_items_fixed_size_left_cmp_func(const void *, const void *);
static hashtable_index_t ht_item_hash_func(const void *, size_t);

/*
 * Hashing and comparing routines, that are used with the hash tables
 */
static int
ht_items_cmp_func(const void *p1, const void *p2)
{
    	struct cache_ht_item_data_ *hp1, *hp2;
	size_t min_size;
	int result;

	hp1 = (struct cache_ht_item_data_ *)p1;
	hp2 = (struct cache_ht_item_data_ *)p2;

	assert(hp1->key != NULL);
	assert(hp2->key != NULL);

	if (hp1->key_size != hp2->key_size) {
		min_size = (hp1->key_size < hp2->key_size) ? hp1->key_size :
			hp2->key_size;
		result = memcmp(hp1->key, hp2->key, min_size);

		if (result == 0)
			return ((hp1->key_size < hp2->key_size) ? -1 : 1);
		else
			return (result);
	} else
		return (memcmp(hp1->key, hp2->key, hp1->key_size));
}

static int
ht_items_fixed_size_left_cmp_func(const void *p1, const void *p2)
{
    	struct cache_ht_item_data_ *hp1, *hp2;
	size_t min_size;
	int result;

	hp1 = (struct cache_ht_item_data_ *)p1;
	hp2 = (struct cache_ht_item_data_ *)p2;

	assert(hp1->key != NULL);
	assert(hp2->key != NULL);

	if (hp1->key_size != hp2->key_size) {
		min_size = (hp1->key_size < hp2->key_size) ? hp1->key_size :
			hp2->key_size;
		result = memcmp(hp1->key, hp2->key, min_size);

		if (result == 0)
			if (min_size == hp1->key_size)
			    return (0);
			else
			    return ((hp1->key_size < hp2->key_size) ? -1 : 1);
		else
			return (result);
	} else
		return (memcmp(hp1->key, hp2->key, hp1->key_size));
}

static hashtable_index_t
ht_item_hash_func(const void *p, size_t cache_entries_size)
{
    	struct cache_ht_item_data_ *hp;
	size_t i;

	hashtable_index_t retval;

	hp = (struct cache_ht_item_data_ *)p;
	assert(hp->key != NULL);

	retval = 0;
	for (i = 0; i < hp->key_size; ++i)
	    retval = (127 * retval + (unsigned char)hp->key[i]) %
		cache_entries_size;

	return retval;
}

HASHTABLE_PROTOTYPE(cache_ht_, cache_ht_item_, struct cache_ht_item_data_);
HASHTABLE_GENERATE(cache_ht_, cache_ht_item_, struct cache_ht_item_data_, data,
	ht_item_hash_func, ht_items_cmp_func);

/*
 * Routines to sort and search the entries by name
 */
static int
entries_bsearch_cmp_func(const void *key, const void *ent)
{

	assert(key != NULL);
	assert(ent != NULL);

	return (strcmp((char const *)key,
		(*(struct cache_entry_ const **)ent)->name));
}

static int
entries_qsort_cmp_func(const void *e1, const void *e2)
{

	assert(e1 != NULL);
	assert(e2 != NULL);

	return (strcmp((*(struct cache_entry_ const **)e1)->name,
		(*(struct cache_entry_ const **)e2)->name));
}

static struct cache_entry_ **
find_cache_entry_p(struct cache_ *the_cache, const char *entry_name)
{

	return ((struct cache_entry_ **)(bsearch(entry_name, the_cache->entries,
		the_cache->entries_size, sizeof(struct cache_entry_ *),
		entries_bsearch_cmp_func)));
}

static void
destroy_cache_mp_write_session(struct cache_mp_write_session_ *ws)
{

	struct cache_mp_data_item_	*data_item;

	TRACE_IN(destroy_cache_mp_write_session);
	assert(ws != NULL);
	while (!TAILQ_EMPTY(&ws->items)) {
		data_item = TAILQ_FIRST(&ws->items);
		TAILQ_REMOVE(&ws->items, data_item, entries);
		free(data_item->value);
		free(data_item);
	}

	free(ws);
	TRACE_OUT(destroy_cache_mp_write_session);
}

static void
destroy_cache_mp_read_session(struct cache_mp_read_session_ *rs)
{

	TRACE_IN(destroy_cache_mp_read_session);
	assert(rs != NULL);
	free(rs);
	TRACE_OUT(destroy_cache_mp_read_session);
}

static void
destroy_cache_entry(struct cache_entry_ *entry)
{
	struct cache_common_entry_	*common_entry;
	struct cache_mp_entry_		*mp_entry;
	struct cache_mp_read_session_	*rs;
	struct cache_mp_write_session_	*ws;
	struct cache_ht_item_ *ht_item;
	struct cache_ht_item_data_ *ht_item_data;

	TRACE_IN(destroy_cache_entry);
	assert(entry != NULL);

	if (entry->params->entry_type == CET_COMMON) {
		common_entry = (struct cache_common_entry_ *)entry;

		HASHTABLE_FOREACH(&(common_entry->items), ht_item) {
			HASHTABLE_ENTRY_FOREACH(ht_item, data, ht_item_data)
			{
				free(ht_item_data->key);
				free(ht_item_data->value);
			}
			HASHTABLE_ENTRY_CLEAR(ht_item, data);
		}

		HASHTABLE_DESTROY(&(common_entry->items), data);

		/* FIFO policy is always first */
		destroy_cache_fifo_policy(common_entry->policies[0]);
		switch (common_entry->common_params.policy) {
		case CPT_LRU:
			destroy_cache_lru_policy(common_entry->policies[1]);
			break;
		case CPT_LFU:
			destroy_cache_lfu_policy(common_entry->policies[1]);
			break;
		default:
		break;
		}
		free(common_entry->policies);
	} else {
		mp_entry = (struct cache_mp_entry_ *)entry;

		while (!TAILQ_EMPTY(&mp_entry->ws_head)) {
			ws = TAILQ_FIRST(&mp_entry->ws_head);
			TAILQ_REMOVE(&mp_entry->ws_head, ws, entries);
			destroy_cache_mp_write_session(ws);
		}

		while (!TAILQ_EMPTY(&mp_entry->rs_head)) {
			rs = TAILQ_FIRST(&mp_entry->rs_head);
			TAILQ_REMOVE(&mp_entry->rs_head, rs, entries);
			destroy_cache_mp_read_session(rs);
		}

		if (mp_entry->completed_write_session != NULL)
			destroy_cache_mp_write_session(
				mp_entry->completed_write_session);

		if (mp_entry->pending_write_session != NULL)
			destroy_cache_mp_write_session(
				mp_entry->pending_write_session);
	}

	free(entry->name);
	free(entry);
	TRACE_OUT(destroy_cache_entry);
}

static void
clear_cache_entry(struct cache_entry_ *entry)
{
	struct cache_mp_entry_		*mp_entry;
	struct cache_common_entry_	*common_entry;
	struct cache_ht_item_ *ht_item;
	struct cache_ht_item_data_ *ht_item_data;
	struct cache_policy_ *policy;
	struct cache_policy_item_ *item, *next_item;
	size_t entry_size;
	unsigned int i;

	if (entry->params->entry_type == CET_COMMON) {
		common_entry = (struct cache_common_entry_ *)entry;

		entry_size = 0;
		HASHTABLE_FOREACH(&(common_entry->items), ht_item) {
			HASHTABLE_ENTRY_FOREACH(ht_item, data, ht_item_data)
			{
				free(ht_item_data->key);
				free(ht_item_data->value);
			}
			entry_size += HASHTABLE_ENTRY_SIZE(ht_item, data);
			HASHTABLE_ENTRY_CLEAR(ht_item, data);
		}

		common_entry->items_size -= entry_size;
		for (i = 0; i < common_entry->policies_size; ++i) {
			policy = common_entry->policies[i];

			next_item = NULL;
			item = policy->get_first_item_func(policy);
			while (item != NULL) {
				next_item = policy->get_next_item_func(policy,
			    		item);
				policy->remove_item_func(policy, item);
				policy->destroy_item_func(item);
				item = next_item;
			}
		}
	} else {
		mp_entry = (struct cache_mp_entry_ *)entry;

		if (mp_entry->rs_size == 0) {
			if (mp_entry->completed_write_session != NULL) {
				destroy_cache_mp_write_session(
					mp_entry->completed_write_session);
				mp_entry->completed_write_session = NULL;
			}

			memset(&mp_entry->creation_time, 0,
				sizeof(struct timeval));
			memset(&mp_entry->last_request_time, 0,
				sizeof(struct timeval));
		}
	}
}

/*
 * When passed to the flush_cache_policy, ensures that all old elements are
 * deleted.
 */
static int
cache_lifetime_common_continue_func(struct cache_common_entry_ *entry,
	struct cache_policy_item_ *item)
{

	return ((item->last_request_time.tv_sec - item->creation_time.tv_sec >
		entry->common_params.max_lifetime.tv_sec) ? 1: 0);
}

/*
 * When passed to the flush_cache_policy, ensures that all elements, that
 * exceed the size limit, are deleted.
 */
static int
cache_elemsize_common_continue_func(struct cache_common_entry_ *entry,
	struct cache_policy_item_ *item)
{

	return ((entry->items_size > entry->common_params.satisf_elemsize) ? 1
    		: 0);
}

/*
 * Removes the elements from the cache entry, while the continue_func returns 1.
 */
static void
flush_cache_policy(struct cache_common_entry_ *entry,
	struct cache_policy_ *policy,
	struct cache_policy_ *connected_policy,
	int (*continue_func)(struct cache_common_entry_ *,
		struct cache_policy_item_ *))
{
	struct cache_policy_item_ *item, *next_item, *connected_item;
	struct cache_ht_item_ *ht_item;
	struct cache_ht_item_data_ *ht_item_data, ht_key;
	hashtable_index_t hash;

	assert(policy != NULL);

	next_item = NULL;
	item = policy->get_first_item_func(policy);
	while ((item != NULL) && (continue_func(entry, item) == 1)) {
		next_item = policy->get_next_item_func(policy, item);

		connected_item = item->connected_item;
		policy->remove_item_func(policy, item);

		memset(&ht_key, 0, sizeof(struct cache_ht_item_data_));
		ht_key.key = item->key;
		ht_key.key_size = item->key_size;

		hash = HASHTABLE_CALCULATE_HASH(cache_ht_, &entry->items,
			&ht_key);
		assert(hash < HASHTABLE_ENTRIES_COUNT(&entry->items));

		ht_item = HASHTABLE_GET_ENTRY(&(entry->items), hash);
		ht_item_data = HASHTABLE_ENTRY_FIND(cache_ht_, ht_item,
			&ht_key);
		assert(ht_item_data != NULL);
		free(ht_item_data->key);
		free(ht_item_data->value);
		HASHTABLE_ENTRY_REMOVE(cache_ht_, ht_item, ht_item_data);
		--entry->items_size;

		policy->destroy_item_func(item);

		if (connected_item != NULL) {
			connected_policy->remove_item_func(connected_policy,
				connected_item);
			connected_policy->destroy_item_func(connected_item);
		}

		item = next_item;
	}
}

static void
flush_cache_entry(struct cache_entry_ *entry)
{
	struct cache_mp_entry_		*mp_entry;
	struct cache_common_entry_	*common_entry;
	struct cache_policy_ *policy, *connected_policy;

	connected_policy = NULL;
	if (entry->params->entry_type == CET_COMMON) {
		common_entry = (struct cache_common_entry_ *)entry;
		if ((common_entry->common_params.max_lifetime.tv_sec != 0) ||
		    (common_entry->common_params.max_lifetime.tv_usec != 0)) {

			policy = common_entry->policies[0];
			if (common_entry->policies_size > 1)
				connected_policy = common_entry->policies[1];

			flush_cache_policy(common_entry, policy,
				connected_policy,
				cache_lifetime_common_continue_func);
		}


		if ((common_entry->common_params.max_elemsize != 0) &&
			common_entry->items_size >
			common_entry->common_params.max_elemsize) {

			if (common_entry->policies_size > 1) {
				policy = common_entry->policies[1];
				connected_policy = common_entry->policies[0];
			} else {
				policy = common_entry->policies[0];
				connected_policy = NULL;
			}

			flush_cache_policy(common_entry, policy,
				connected_policy,
				cache_elemsize_common_continue_func);
		}
	} else {
		mp_entry = (struct cache_mp_entry_ *)entry;

		if ((mp_entry->mp_params.max_lifetime.tv_sec != 0)
			|| (mp_entry->mp_params.max_lifetime.tv_usec != 0)) {

			if (mp_entry->last_request_time.tv_sec -
				mp_entry->last_request_time.tv_sec >
				mp_entry->mp_params.max_lifetime.tv_sec)
				clear_cache_entry(entry);
		}
	}
}

struct cache_ *
init_cache(struct cache_params const *params)
{
	struct cache_ *retval;

	TRACE_IN(init_cache);
	assert(params != NULL);

	retval = calloc(1, sizeof(*retval));
	assert(retval != NULL);

	assert(params != NULL);
	memcpy(&retval->params, params, sizeof(struct cache_params));

	retval->entries = calloc(1,
		sizeof(*retval->entries) * INITIAL_ENTRIES_CAPACITY);
	assert(retval->entries != NULL);

	retval->entries_capacity = INITIAL_ENTRIES_CAPACITY;
	retval->entries_size = 0;

	TRACE_OUT(init_cache);
	return (retval);
}

void
destroy_cache(struct cache_ *the_cache)
{

	TRACE_IN(destroy_cache);
	assert(the_cache != NULL);

	if (the_cache->entries != NULL) {
		size_t i;
		for (i = 0; i < the_cache->entries_size; ++i)
			destroy_cache_entry(the_cache->entries[i]);

		free(the_cache->entries);
	}

	free(the_cache);
	TRACE_OUT(destroy_cache);
}

int
register_cache_entry(struct cache_ *the_cache,
	struct cache_entry_params const *params)
{
	int policies_size;
	size_t entry_name_size;
	struct cache_common_entry_	*new_common_entry;
	struct cache_mp_entry_		*new_mp_entry;

	TRACE_IN(register_cache_entry);
	assert(the_cache != NULL);

	if (find_cache_entry(the_cache, params->entry_name) != NULL) {
		TRACE_OUT(register_cache_entry);
		return (-1);
	}

	if (the_cache->entries_size == the_cache->entries_capacity) {
		struct cache_entry_ **new_entries;
		size_t	new_capacity;

		new_capacity = the_cache->entries_capacity +
			ENTRIES_CAPACITY_STEP;
		new_entries = calloc(1,
			sizeof(*new_entries) * new_capacity);
		assert(new_entries != NULL);

		memcpy(new_entries, the_cache->entries,
			sizeof(struct cache_entry_ *)
			* the_cache->entries_size);

		free(the_cache->entries);
		the_cache->entries = new_entries;
	}

	entry_name_size = strlen(params->entry_name) + 1;
	switch (params->entry_type)
	{
	case CET_COMMON:
		new_common_entry = calloc(1,
			sizeof(*new_common_entry));
		assert(new_common_entry != NULL);

		memcpy(&new_common_entry->common_params, params,
			sizeof(struct common_cache_entry_params));
		new_common_entry->params =
		  (struct cache_entry_params *)&new_common_entry->common_params;

		new_common_entry->common_params.cep.entry_name = calloc(1,
			entry_name_size);
		assert(new_common_entry->common_params.cep.entry_name != NULL);
		strlcpy(new_common_entry->common_params.cep.entry_name,
			params->entry_name, entry_name_size);
		new_common_entry->name =
			new_common_entry->common_params.cep.entry_name;

		HASHTABLE_INIT(&(new_common_entry->items),
			struct cache_ht_item_data_, data,
			new_common_entry->common_params.cache_entries_size);

		if (new_common_entry->common_params.policy == CPT_FIFO)
			policies_size = 1;
		else
			policies_size = 2;

		new_common_entry->policies = calloc(1,
			sizeof(*new_common_entry->policies) * policies_size);
		assert(new_common_entry->policies != NULL);

		new_common_entry->policies_size = policies_size;
		new_common_entry->policies[0] = init_cache_fifo_policy();

		if (policies_size > 1) {
			switch (new_common_entry->common_params.policy) {
			case CPT_LRU:
				new_common_entry->policies[1] =
					init_cache_lru_policy();
			break;
			case CPT_LFU:
				new_common_entry->policies[1] =
					init_cache_lfu_policy();
			break;
			default:
			break;
			}
		}

		new_common_entry->get_time_func =
			the_cache->params.get_time_func;
		the_cache->entries[the_cache->entries_size++] =
			(struct cache_entry_ *)new_common_entry;
		break;
	case CET_MULTIPART:
		new_mp_entry = calloc(1,
			sizeof(*new_mp_entry));
		assert(new_mp_entry != NULL);

		memcpy(&new_mp_entry->mp_params, params,
			sizeof(struct mp_cache_entry_params));
		new_mp_entry->params =
			(struct cache_entry_params *)&new_mp_entry->mp_params;

		new_mp_entry->mp_params.cep.entry_name = calloc(1,
			entry_name_size);
		assert(new_mp_entry->mp_params.cep.entry_name != NULL);
		strlcpy(new_mp_entry->mp_params.cep.entry_name, params->entry_name,
			entry_name_size);
		new_mp_entry->name = new_mp_entry->mp_params.cep.entry_name;

		TAILQ_INIT(&new_mp_entry->ws_head);
		TAILQ_INIT(&new_mp_entry->rs_head);

		new_mp_entry->get_time_func = the_cache->params.get_time_func;
		the_cache->entries[the_cache->entries_size++] =
			(struct cache_entry_ *)new_mp_entry;
		break;
	}


	qsort(the_cache->entries, the_cache->entries_size,
		sizeof(struct cache_entry_ *), entries_qsort_cmp_func);

	TRACE_OUT(register_cache_entry);
	return (0);
}

int
unregister_cache_entry(struct cache_ *the_cache, const char *entry_name)
{
	struct cache_entry_ **del_ent;

	TRACE_IN(unregister_cache_entry);
	assert(the_cache != NULL);

	del_ent = find_cache_entry_p(the_cache, entry_name);
	if (del_ent != NULL) {
		destroy_cache_entry(*del_ent);
		--the_cache->entries_size;

		memmove(del_ent, del_ent + 1,
			(&(the_cache->entries[--the_cache->entries_size]) -
	    		del_ent) * sizeof(struct cache_entry_ *));

		TRACE_OUT(unregister_cache_entry);
		return (0);
	} else {
		TRACE_OUT(unregister_cache_entry);
		return (-1);
	}
}

struct cache_entry_ *
find_cache_entry(struct cache_ *the_cache, const char *entry_name)
{
	struct cache_entry_ **result;

	TRACE_IN(find_cache_entry);
	result = find_cache_entry_p(the_cache, entry_name);

	if (result == NULL) {
		TRACE_OUT(find_cache_entry);
		return (NULL);
	} else {
		TRACE_OUT(find_cache_entry);
		return (*result);
	}
}

/*
 * Tries to read the element with the specified key from the cache. If the
 * value_size is too small, it will be filled with the proper number, and
 * the user will need to call cache_read again with the value buffer, that
 * is large enough.
 * Function returns 0 on success, -1 on error, and -2 if the value_size is too
 * small.
 */
int
cache_read(struct cache_entry_ *entry, const char *key, size_t key_size,
	char *value, size_t *value_size)
{
	struct cache_common_entry_	*common_entry;
	struct cache_ht_item_data_	item_data, *find_res;
	struct cache_ht_item_		*item;
	hashtable_index_t	hash;
	struct cache_policy_item_ *connected_item;

	TRACE_IN(cache_read);
	assert(entry != NULL);
	assert(key != NULL);
	assert(value_size != NULL);
	assert(entry->params->entry_type == CET_COMMON);

	common_entry = (struct cache_common_entry_ *)entry;

	memset(&item_data, 0, sizeof(struct cache_ht_item_data_));
	/* can't avoid the cast here */
	item_data.key = (char *)key;
	item_data.key_size = key_size;

	hash = HASHTABLE_CALCULATE_HASH(cache_ht_, &common_entry->items,
		&item_data);
	assert(hash < HASHTABLE_ENTRIES_COUNT(&common_entry->items));

	item = HASHTABLE_GET_ENTRY(&(common_entry->items), hash);
	find_res = HASHTABLE_ENTRY_FIND(cache_ht_, item, &item_data);
	if (find_res == NULL) {
		TRACE_OUT(cache_read);
		return (-1);
	}
	/* pretend that entry was not found if confidence is below threshold*/
	if (find_res->confidence < 
	    common_entry->common_params.confidence_threshold) {
		TRACE_OUT(cache_read);
		return (-1);
	}

	if ((common_entry->common_params.max_lifetime.tv_sec != 0) ||
		(common_entry->common_params.max_lifetime.tv_usec != 0)) {

		if (find_res->fifo_policy_item->last_request_time.tv_sec -
			find_res->fifo_policy_item->creation_time.tv_sec >
			common_entry->common_params.max_lifetime.tv_sec) {

			free(find_res->key);
			free(find_res->value);

			connected_item =
			    find_res->fifo_policy_item->connected_item;
			if (connected_item != NULL) {
				common_entry->policies[1]->remove_item_func(
					common_entry->policies[1],
			    		connected_item);
				common_entry->policies[1]->destroy_item_func(
					connected_item);
			}

			common_entry->policies[0]->remove_item_func(
				common_entry->policies[0],
					find_res->fifo_policy_item);
			common_entry->policies[0]->destroy_item_func(
				find_res->fifo_policy_item);

			HASHTABLE_ENTRY_REMOVE(cache_ht_, item, find_res);
			--common_entry->items_size;
		}
	}

	if ((*value_size < find_res->value_size) || (value == NULL)) {
		*value_size = find_res->value_size;
		TRACE_OUT(cache_read);
		return (-2);
	}

	*value_size = find_res->value_size;
	memcpy(value, find_res->value, find_res->value_size);

	++find_res->fifo_policy_item->request_count;
	common_entry->get_time_func(
		&find_res->fifo_policy_item->last_request_time);
	common_entry->policies[0]->update_item_func(common_entry->policies[0],
		find_res->fifo_policy_item);

	if (find_res->fifo_policy_item->connected_item != NULL) {
		connected_item = find_res->fifo_policy_item->connected_item;
		memcpy(&connected_item->last_request_time,
			&find_res->fifo_policy_item->last_request_time,
			sizeof(struct timeval));
		connected_item->request_count =
			find_res->fifo_policy_item->request_count;

		common_entry->policies[1]->update_item_func(
			common_entry->policies[1], connected_item);
	}

	TRACE_OUT(cache_read);
	return (0);
}

/*
 * Writes the value with the specified key into the cache entry.
 * Functions returns 0 on success, and -1 on error.
 */
int
cache_write(struct cache_entry_ *entry, const char *key, size_t key_size,
    	char const *value, size_t value_size)
{
	struct cache_common_entry_	*common_entry;
	struct cache_ht_item_data_	item_data, *find_res;
	struct cache_ht_item_		*item;
	hashtable_index_t	hash;

	struct cache_policy_		*policy, *connected_policy;
	struct cache_policy_item_	*policy_item;
	struct cache_policy_item_	*connected_policy_item;

	TRACE_IN(cache_write);
	assert(entry != NULL);
	assert(key != NULL);
	assert(value != NULL);
	assert(entry->params->entry_type == CET_COMMON);

	common_entry = (struct cache_common_entry_ *)entry;

	memset(&item_data, 0, sizeof(struct cache_ht_item_data_));
	/* can't avoid the cast here */
	item_data.key = (char *)key;
	item_data.key_size = key_size;

	hash = HASHTABLE_CALCULATE_HASH(cache_ht_, &common_entry->items,
		&item_data);
	assert(hash < HASHTABLE_ENTRIES_COUNT(&common_entry->items));

	item = HASHTABLE_GET_ENTRY(&(common_entry->items), hash);
	find_res = HASHTABLE_ENTRY_FIND(cache_ht_, item, &item_data);
	if (find_res != NULL) {
		if (find_res->confidence < common_entry->common_params.confidence_threshold) {
		  	/* duplicate entry is no error, if confidence is low */
			if ((find_res->value_size == value_size) &&
			    (memcmp(find_res->value, value, value_size) == 0)) {
				/* increase confidence on exact match (key and values) */
				find_res->confidence++;
			} else {
				/* create new entry with low confidence, if value changed */
				free(item_data.value);
				item_data.value = malloc(value_size);
				assert(item_data.value != NULL);
				memcpy(item_data.value, value, value_size);
				item_data.value_size = value_size;
				find_res->confidence = 1;
			}
			TRACE_OUT(cache_write);
			return (0);
		}
		TRACE_OUT(cache_write);
		return (-1);
	}

	item_data.key = malloc(key_size);
	memcpy(item_data.key, key, key_size);

	item_data.value = malloc(value_size);
	assert(item_data.value != NULL);

	memcpy(item_data.value, value, value_size);
	item_data.value_size = value_size;

	item_data.confidence = 1;

	policy_item = common_entry->policies[0]->create_item_func();
	policy_item->key = item_data.key;
	policy_item->key_size = item_data.key_size;
	common_entry->get_time_func(&policy_item->creation_time);

	if (common_entry->policies_size > 1) {
		connected_policy_item =
			common_entry->policies[1]->create_item_func();
		memcpy(&connected_policy_item->creation_time,
			&policy_item->creation_time,
			sizeof(struct timeval));
		connected_policy_item->key = policy_item->key;
		connected_policy_item->key_size = policy_item->key_size;

		connected_policy_item->connected_item = policy_item;
		policy_item->connected_item = connected_policy_item;
	}

	item_data.fifo_policy_item = policy_item;

	common_entry->policies[0]->add_item_func(common_entry->policies[0],
		policy_item);
	if (common_entry->policies_size > 1)
		common_entry->policies[1]->add_item_func(
			common_entry->policies[1], connected_policy_item);

	HASHTABLE_ENTRY_STORE(cache_ht_, item, &item_data);
	++common_entry->items_size;

	if ((common_entry->common_params.max_elemsize != 0) &&
		(common_entry->items_size >
		common_entry->common_params.max_elemsize)) {
		if (common_entry->policies_size > 1) {
			policy = common_entry->policies[1];
			connected_policy = common_entry->policies[0];
		} else {
			policy = common_entry->policies[0];
			connected_policy = NULL;
		}

		flush_cache_policy(common_entry, policy, connected_policy,
			cache_elemsize_common_continue_func);
	}

	TRACE_OUT(cache_write);
	return (0);
}

/*
 * Initializes the write session for the specified multipart entry. This
 * session then should be filled with data either committed or abandoned by
 * using close_cache_mp_write_session or abandon_cache_mp_write_session
 * respectively.
 * Returns NULL on errors (when there are too many opened write sessions for
 * the entry).
 */
struct cache_mp_write_session_ *
open_cache_mp_write_session(struct cache_entry_ *entry)
{
	struct cache_mp_entry_	*mp_entry;
	struct cache_mp_write_session_	*retval;

	TRACE_IN(open_cache_mp_write_session);
	assert(entry != NULL);
	assert(entry->params->entry_type == CET_MULTIPART);
	mp_entry = (struct cache_mp_entry_ *)entry;

	if ((mp_entry->mp_params.max_sessions > 0) &&
		(mp_entry->ws_size == mp_entry->mp_params.max_sessions)) {
		TRACE_OUT(open_cache_mp_write_session);
		return (NULL);
	}

	retval = calloc(1,
		sizeof(*retval));
	assert(retval != NULL);

	TAILQ_INIT(&retval->items);
	retval->parent_entry = mp_entry;

	TAILQ_INSERT_HEAD(&mp_entry->ws_head, retval, entries);
	++mp_entry->ws_size;

	TRACE_OUT(open_cache_mp_write_session);
	return (retval);
}

/*
 * Writes data to the specified session. Return 0 on success and -1 on errors
 * (when write session size limit is exceeded).
 */
int
cache_mp_write(struct cache_mp_write_session_ *ws, char *data,
	size_t data_size)
{
	struct cache_mp_data_item_	*new_item;

	TRACE_IN(cache_mp_write);
	assert(ws != NULL);
	assert(ws->parent_entry != NULL);
	assert(ws->parent_entry->params->entry_type == CET_MULTIPART);

	if ((ws->parent_entry->mp_params.max_elemsize > 0) &&
		(ws->parent_entry->mp_params.max_elemsize == ws->items_size)) {
		TRACE_OUT(cache_mp_write);
		return (-1);
	}

	new_item = calloc(1,
		sizeof(*new_item));
	assert(new_item != NULL);

	new_item->value = malloc(data_size);
	assert(new_item->value != NULL);
	memcpy(new_item->value, data, data_size);
	new_item->value_size = data_size;

	TAILQ_INSERT_TAIL(&ws->items, new_item, entries);
	++ws->items_size;

	TRACE_OUT(cache_mp_write);
	return (0);
}

/*
 * Abandons the write session and frees all the connected resources.
 */
void
abandon_cache_mp_write_session(struct cache_mp_write_session_ *ws)
{

	TRACE_IN(abandon_cache_mp_write_session);
	assert(ws != NULL);
	assert(ws->parent_entry != NULL);
	assert(ws->parent_entry->params->entry_type == CET_MULTIPART);

	TAILQ_REMOVE(&ws->parent_entry->ws_head, ws, entries);
	--ws->parent_entry->ws_size;

	destroy_cache_mp_write_session(ws);
	TRACE_OUT(abandon_cache_mp_write_session);
}

/*
 * Commits the session to the entry, for which it was created.
 */
void
close_cache_mp_write_session(struct cache_mp_write_session_ *ws)
{

	TRACE_IN(close_cache_mp_write_session);
	assert(ws != NULL);
	assert(ws->parent_entry != NULL);
	assert(ws->parent_entry->params->entry_type == CET_MULTIPART);

	TAILQ_REMOVE(&ws->parent_entry->ws_head, ws, entries);
	--ws->parent_entry->ws_size;

	if (ws->parent_entry->completed_write_session == NULL) {
		/*
		 * If there is no completed session yet, this will be the one
		 */
		ws->parent_entry->get_time_func(
	    		&ws->parent_entry->creation_time);
		ws->parent_entry->completed_write_session = ws;
	} else {
		/*
		 * If there is a completed session, then we'll save our session
		 * as a pending session. If there is already a pending session,
		 * it would be destroyed.
		 */
		if (ws->parent_entry->pending_write_session != NULL)
			destroy_cache_mp_write_session(
				ws->parent_entry->pending_write_session);

		ws->parent_entry->pending_write_session = ws;
	}
	TRACE_OUT(close_cache_mp_write_session);
}

/*
 * Opens read session for the specified entry. Returns NULL on errors (when
 * there are no data in the entry, or the data are obsolete).
 */
struct cache_mp_read_session_ *
open_cache_mp_read_session(struct cache_entry_ *entry)
{
	struct cache_mp_entry_			*mp_entry;
	struct cache_mp_read_session_	*retval;

	TRACE_IN(open_cache_mp_read_session);
	assert(entry != NULL);
	assert(entry->params->entry_type == CET_MULTIPART);
	mp_entry = (struct cache_mp_entry_ *)entry;

	if (mp_entry->completed_write_session == NULL) {
		TRACE_OUT(open_cache_mp_read_session);
		return (NULL);
	}

	if ((mp_entry->mp_params.max_lifetime.tv_sec != 0)
		|| (mp_entry->mp_params.max_lifetime.tv_usec != 0)) {
		if (mp_entry->last_request_time.tv_sec -
			mp_entry->last_request_time.tv_sec >
			mp_entry->mp_params.max_lifetime.tv_sec) {
			flush_cache_entry(entry);
			TRACE_OUT(open_cache_mp_read_session);
			return (NULL);
		}
	}

	retval = calloc(1,
		sizeof(*retval));
	assert(retval != NULL);

	retval->parent_entry = mp_entry;
	retval->current_item = TAILQ_FIRST(
		&mp_entry->completed_write_session->items);

	TAILQ_INSERT_HEAD(&mp_entry->rs_head, retval, entries);
	++mp_entry->rs_size;

	mp_entry->get_time_func(&mp_entry->last_request_time);
	TRACE_OUT(open_cache_mp_read_session);
	return (retval);
}

/*
 * Reads the data from the read session - step by step.
 * Returns 0 on success, -1 on error (when there are no more data), and -2 if
 * the data_size is too small.  In the last case, data_size would be filled
 * the proper value.
 */
int
cache_mp_read(struct cache_mp_read_session_ *rs, char *data, size_t *data_size)
{

	TRACE_IN(cache_mp_read);
	assert(rs != NULL);

	if (rs->current_item == NULL) {
		TRACE_OUT(cache_mp_read);
		return (-1);
	}

	if (rs->current_item->value_size > *data_size) {
		*data_size = rs->current_item->value_size;
		if (data == NULL) {
			TRACE_OUT(cache_mp_read);
			return (0);
		}

		TRACE_OUT(cache_mp_read);
		return (-2);
	}

	*data_size = rs->current_item->value_size;
	memcpy(data, rs->current_item->value, rs->current_item->value_size);
	rs->current_item = TAILQ_NEXT(rs->current_item, entries);

	TRACE_OUT(cache_mp_read);
	return (0);
}

/*
 * Closes the read session. If there are no more read sessions and there is
 * a pending write session, it will be committed and old
 * completed_write_session will be destroyed.
 */
void
close_cache_mp_read_session(struct cache_mp_read_session_ *rs)
{

	TRACE_IN(close_cache_mp_read_session);
	assert(rs != NULL);
	assert(rs->parent_entry != NULL);

	TAILQ_REMOVE(&rs->parent_entry->rs_head, rs, entries);
	--rs->parent_entry->rs_size;

	if ((rs->parent_entry->rs_size == 0) &&
		(rs->parent_entry->pending_write_session != NULL)) {
		destroy_cache_mp_write_session(
			rs->parent_entry->completed_write_session);
		rs->parent_entry->completed_write_session =
			rs->parent_entry->pending_write_session;
		rs->parent_entry->pending_write_session = NULL;
	}

	destroy_cache_mp_read_session(rs);
	TRACE_OUT(close_cache_mp_read_session);
}

int
transform_cache_entry(struct cache_entry_ *entry,
	enum cache_transformation_t transformation)
{

	TRACE_IN(transform_cache_entry);
	switch (transformation) {
	case CTT_CLEAR:
		clear_cache_entry(entry);
		TRACE_OUT(transform_cache_entry);
		return (0);
	case CTT_FLUSH:
		flush_cache_entry(entry);
		TRACE_OUT(transform_cache_entry);
		return (0);
	default:
		TRACE_OUT(transform_cache_entry);
		return (-1);
	}
}

int
transform_cache_entry_part(struct cache_entry_ *entry,
	enum cache_transformation_t transformation, const char *key_part,
	size_t key_part_size, enum part_position_t part_position)
{
	struct cache_common_entry_ *common_entry;
	struct cache_ht_item_ *ht_item;
	struct cache_ht_item_data_ *ht_item_data, ht_key;

	struct cache_policy_item_ *item, *connected_item;

	TRACE_IN(transform_cache_entry_part);
	if (entry->params->entry_type != CET_COMMON) {
		TRACE_OUT(transform_cache_entry_part);
		return (-1);
	}

	if (transformation != CTT_CLEAR) {
		TRACE_OUT(transform_cache_entry_part);
		return (-1);
	}

	memset(&ht_key, 0, sizeof(struct cache_ht_item_data_));
	ht_key.key = (char *)key_part;	/* can't avoid casting here */
	ht_key.key_size = key_part_size;

	common_entry = (struct cache_common_entry_ *)entry;
	HASHTABLE_FOREACH(&(common_entry->items), ht_item) {
		do {
			ht_item_data = HASHTABLE_ENTRY_FIND_SPECIAL(cache_ht_,
				ht_item, &ht_key,
				ht_items_fixed_size_left_cmp_func);

			if (ht_item_data != NULL) {
			    item = ht_item_data->fifo_policy_item;
			    connected_item = item->connected_item;

			    common_entry->policies[0]->remove_item_func(
				common_entry->policies[0],
				item);

			    free(ht_item_data->key);
			    free(ht_item_data->value);
			    HASHTABLE_ENTRY_REMOVE(cache_ht_, ht_item,
				ht_item_data);
			    --common_entry->items_size;

			    common_entry->policies[0]->destroy_item_func(
				item);
			    if (common_entry->policies_size == 2) {
				common_entry->policies[1]->remove_item_func(
				    common_entry->policies[1],
				    connected_item);
				common_entry->policies[1]->destroy_item_func(
				    connected_item);
			    }
			}
		} while (ht_item_data != NULL);
	}

	TRACE_OUT(transform_cache_entry_part);
	return (0);
}
OpenPOWER on IntegriCloud