File: enscache.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 571,544 kB
  • sloc: ansic: 460,579; java: 29,439; perl: 13,573; sh: 12,754; makefile: 3,283; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (1182 lines) | stat: -rw-r--r-- 31,465 bytes parent folder | download | duplicates (7)
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
/* @source enscache ***********************************************************
**
** Ensembl Cache functions
**
** @author Copyright (C) 1999 Ensembl Developers
** @author Copyright (C) 2006 Michael K. Schuster
** @version $Revision: 1.40 $
** @modified 2009 by Alan Bleasby for incorporation into EMBOSS core
** @modified $Date: 2013/02/17 13:02:40 $ by $Author: mks $
** @@
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA  02110-1301,  USA.
**
******************************************************************************/

/* ========================================================================= */
/* ============================= include files ============================= */
/* ========================================================================= */

#include "enscache.h"
#include "enstable.h"




/* ========================================================================= */
/* =============================== constants =============================== */
/* ========================================================================= */




/* ========================================================================= */
/* =========================== global variables ============================ */
/* ========================================================================= */




/* ========================================================================= */
/* ============================= private data ============================== */
/* ========================================================================= */

/* @datastatic CachePNode *****************************************************
**
** Ensembl Cache Node.
**
** @alias CacheSNode
** @alias CacheONode
**
** @attr Key [void*] Key data address
** @attr Value [void*] Value data address
** @attr Bytes [size_t] Byte size of this node including key and value data
** @attr Dirty [AjBool] Flag to mark that value data has not been written back
** @attr Padding [ajuint] Padding to alignment boundary
** @@
******************************************************************************/

typedef struct CacheSNode
{
    void *Key;
    void *Value;
    size_t Bytes;
    AjBool Dirty;
    ajuint Padding;
} CacheONode;

#define CachePNode CacheONode*




/* ========================================================================= */
/* =========================== private constants =========================== */
/* ========================================================================= */




/* ========================================================================= */
/* =========================== private variables =========================== */
/* ========================================================================= */




/* ========================================================================= */
/* =========================== private functions =========================== */
/* ========================================================================= */

static CachePNode cacheNodeNew(const EnsPCache cache, void *key, void *value);

static void cacheNodeDel(const EnsPCache cache, CachePNode *Pnode);

static AjBool cacheNodeInsert(EnsPCache cache, CachePNode node);

static AjBool cacheNodeRemove(EnsPCache cache, const CachePNode node);




/* ========================================================================= */
/* ======================= All functions by section ======================== */
/* ========================================================================= */




/* @filesection enscache ******************************************************
**
** @nam1rule ens Function belongs to the Ensembl library
**
******************************************************************************/




/* @funcstatic cacheNodeNew ***************************************************
**
** Default constructor for an Ensembl Cache Node.
**
** The size of the Cache Node will be estimated according to the Ensembl Cache
** type with the function already provided at the Cache initialisation stage.
** This fuction will also reference value data to increment an internal usage
** counter and prevent deletion of value data while in the cache.
**
** @param [r] cache [const EnsPCache] Ensembl Cache
** @param [r] key [void*] Key data address
** @param [r] value [void*] Value data address
**
** @return [CachePNode] Ensembl Cache Node or NULL
**
** @release 6.2.0
** @@
******************************************************************************/

static CachePNode cacheNodeNew(const EnsPCache cache, void *key, void *value)
{
    ajuint *Puintkey = NULL;

    CachePNode node = NULL;

    if (!cache)
        return NULL;

    if (!key)
        return NULL;

    if (!value)
        return NULL;

    AJNEW0(node);

    /* Add the size of the Ensembl Cache Node itself. */

    node->Bytes = sizeof (CacheONode);

    switch (cache->Type)
    {
        case ensECacheTypeNumeric:

            /* Copy AJAX unsigned integer key data. */

            AJNEW0(Puintkey);

            *Puintkey = *((ajuint *) key);

            node->Key = (void *) Puintkey;

            /* Add the size of unsigned integer key data. */

            node->Bytes += sizeof (ajuint);

            break;

        case ensECacheTypeAlphaNumeric:

            /* Copy AJAX String key data. */

            node->Key = (void *) ajStrNewS((AjPStr) key);

            /* Add the size of AJAX String key data. */

            node->Bytes += sizeof (AjOStr);

            node->Bytes += ajStrGetRes((AjPStr) node->Key);

            break;

        default:

            ajWarn("cacheNodeNew got unexpected Cache type %d.\n",
                   cache->Type);
    }

    /* Reference the value data. */

    if (cache->Freference && value)
        node->Value = (*cache->Freference) (value);

    /* Calculate the size of the value data. */

    if (cache->Fsize && node->Value)
        node->Bytes += (*cache->Fsize) (node->Value);

    node->Dirty = ajFalse;

    return node;
}




/* @funcstatic cacheNodeDel ***************************************************
**
** Default destructor for an Ensembl Cache Node.
**
** @param [r] cache [const EnsPCache] Ensembl Cache
** @param [d] Pnode [CachePNode*] Ensembl Cache Node address
**
** @return [void]
**
** @release 6.2.0
** @@
******************************************************************************/

static void cacheNodeDel(const EnsPCache cache, CachePNode *Pnode)
{
    CachePNode pthis = NULL;

    if (!cache)
        return;

    if (!Pnode)
        return;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 1
    if (ajDebugTest("cacheNodeDel"))
    {
        ajDebug("cacheNodeDel\n"
                "  *Pnode %p\n",
                *Pnode);

        /* cacheNodeTrace(*Pnode, 1); */
    }
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 1 */

    if (!(pthis = *Pnode))
        return;

    /* Delete key data. */

    switch (cache->Type)
    {
        case ensECacheTypeNumeric:

            /* Delete AJAX unsigned integer key data. */

            AJFREE(pthis->Key);

            break;

        case ensECacheTypeAlphaNumeric:

            /* Delete AJAX String key data. */

            ajStrDel((AjPStr *) &pthis->Key);

            break;

        default:

            ajWarn("cacheNodeDel got unexpected Cache type %d.\n",
                   cache->Type);
    }

    /* Delete value data. */

    if (cache->Fdelete && pthis->Value)
        (*cache->Fdelete) (&pthis->Value);

    ajMemFree((void **) Pnode);

    return;
}




/* @funcstatic cacheNodeInsert ************************************************
**
** Insert an Ensembl Cache Node into an Ensembl Cache.
**
** @param [u] cache [EnsPCache] Ensembl Cache
** @param [u] node [CachePNode] Ensembl Cache Node
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

static AjBool cacheNodeInsert(EnsPCache cache, CachePNode node)
{
    CachePNode old = NULL;

    if (!cache)
        return ajFalse;

    if (!node)
        return ajFalse;

    if (cache->MaxSize && (node->Bytes > cache->MaxSize))
        return ajFalse;

    /* Insert the node into the AJAX List. */

    ajListPushAppend(cache->List, (void *) node);

    /* Insert the node into the AJAX Table. */

    ajTablePut(cache->Table, node->Key, (void *) node);

    /* Update the cache statistics. */

    cache->Bytes += node->Bytes;

    cache->Count++;

    cache->Stored++;

    /* If the cache is too big, remove the top node(s). */

    while ((cache->MaxBytes && (cache->Bytes > cache->MaxBytes)) ||
           (cache->MaxCount && (cache->Count > cache->MaxCount)))
    {
        /* Remove the top node from the AJAX List. */

        ajListPop(cache->List, (void **) &old);

        /* Remove the node also from the AJAX Table. */

        ajTableRemove(cache->Table, old->Key);

        /* Update the cache statistics. */

        cache->Bytes -= old->Bytes;

        cache->Count--;

        cache->Dropped++;

        /* Write changes of value data to disk if any. */

        if (cache->Fwrite && old->Value && old->Dirty)
            (*cache->Fwrite) (old->Value);

        /* Both, key and value data are deleted via cacheNodeDel. */

        cacheNodeDel(cache, &old);
    }

    return ajTrue;
}




/* @funcstatic cacheNodeRemove ************************************************
**
** Remove an Ensembl Cache Node from an Ensembl Cache.
**
** @param [u] cache [EnsPCache] Ensembl Cache
** @param [r] node [const CachePNode] Ensembl Cache Node
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

static AjBool cacheNodeRemove(EnsPCache cache, const CachePNode node)
{
    AjIList iter = NULL;

    CachePNode lnode = NULL;

    if (!cache)
        return ajFalse;

    if (!node)
        return ajFalse;

    /* Remove the node from the AJAX List. */

    iter = ajListIterNew(cache->List);

    while (!ajListIterDone(iter))
    {
        lnode = (CachePNode) ajListIterGet(iter);

        if (lnode == node)
        {
            ajListIterRemove(iter);

            break;
        }
    }

    ajListIterDel(&iter);

    /* Remove the node from the AJAX Table. */

    ajTableRemove(cache->Table, node->Key);

    /* Update the cache statistics. */

    cache->Bytes -= node->Bytes;

    cache->Count--;

    cache->Removed++;

    return ajTrue;
}




/* @datasection [EnsPCache] Ensembl Cache *************************************
**
** @nam2rule Cache Functions for manipulating Ensembl Cache objects
** @cc Bio::EnsEMBL::Utils::Cache
** @cc CVS Revision: 1.3
** @cc CVS Tag: branch-ensembl-68
**
******************************************************************************/




/* @section constructors ******************************************************
**
** @fdata [EnsPCache]
**
** @nam3rule New Constructor
**
** @argrule New type [const EnsECacheType] Ensembl Cache type
** @argrule New maxbytes [size_t] Maximum number of bytes held in the cache
** @argrule New maxcount [ajuint] Maximum number of objects to be cached
** @argrule New maxsize [size_t] Maximum size of an object to be cached
** @argrule New Freference [void* function] Object-specific referencing
** function
** @argrule New Fdelete [void function] Object-specific deletion function
** @argrule New Fsize [size_t function] Object-specific memory sizing function
** @argrule New Fread [void* function] Object-specific reading function
** @argrule New Fwrite [AjBool function] Object-specific writing function
** @argrule New synchron [AjBool] ajTrue: Immediately write-back value data
** @argrule New label [const char*] Cache label for statistics output
**
** @valrule * [EnsPCache] Ensembl Cache or NULL
**
** @fcategory new
******************************************************************************/




/* @func ensCacheNew **********************************************************
**
** Default constructor for an Ensembl Cache.
**
** @param [r] type [const EnsECacheType] Ensembl Cache type
**                 (ensECacheTypeNumeric or ensECacheTypeAlphaNumeric)
** @param [r] maxbytes [size_t] Maximum number of bytes held in the cache
** @param [r] maxcount [ajuint] Maximum number of objects to be cached
** @param [r] maxsize [size_t] Maximum size of an object to be cached
** @param [f] Freference [void* function] Object-specific referencing function
** @param [f] Fdelete [void function] Object-specific deletion function
** @param [f] Fsize [size_t function] Object-specific memory sizing function
** @param [f] Fread [void* function] Object-specific reading function
** @param [f] Fwrite [AjBool function] Object-specific writing function
** @param [r] synchron [AjBool] ajTrue: Immediately write-back value data
**                              ajFalse: Write-back value data later
** @param [r] label [const char*] Cache label for statistics output
**
** @return [EnsPCache] Ensembl Cache or NULL
**
** @release 6.2.0
** @@
** The maximum size parameter should prevent the cache from purging too many
** objects when very large objects are inserted. If not set it defaults to
** a tenth of the maximum cache size.
**
** Object-specific functions are required to reference objects held in the
** cache or delete objects once purged from the cache, as well as memory sizing
** functions and object-specific read and write back functions.
******************************************************************************/

EnsPCache ensCacheNew(const EnsECacheType type,
                      size_t maxbytes,
                      ajuint maxcount,
                      size_t maxsize,
                      void* (*Freference) (void *value),
                      void (*Fdelete) (void **Pvalue),
                      size_t (*Fsize) (const void *value),
                      void* (*Fread) (const void *key),
                      AjBool (*Fwrite) (const void *value),
                      AjBool synchron,
                      const char *label)
{
    AjBool debug = AJFALSE;

    EnsPCache cache = NULL;

    debug = ajDebugTest("ensCacheNew");

    if (debug)
        ajDebug("ensCacheNew\n"
                "  type %d\n"
                "  maxbytes %Lu\n"
                "  maxcount %u\n"
                "  maxsize %Lu\n"
                "  Freference %p\n"
                "  Fdelete %p\n"
                "  Fsize %p\n"
                "  Fread %p\n"
                "  Fwrite %p\n"
                "  synchron '%B'\n"
                "  label '%s'\n",
                type,
                maxbytes,
                maxcount,
                maxsize,
                Freference,
                Fdelete,
                Fsize,
                Fread,
                Fwrite,
                synchron,
                label);
    /* FIXME: size_t can be shorter than ajulong */

    if ((type < ensECacheTypeNumeric) || (type > ensECacheTypeAlphaNumeric))
        ajFatal("ensCacheNew requires a valid type.\n");

    if ((!maxbytes) && (!maxcount))
        ajFatal("ensCacheNew requires either a "
                "maximum bytes or maximum count limit.\n");

    if (!maxsize)
        maxsize = maxbytes ? maxbytes / 10 + 1 : 0;

    if (maxbytes && (!maxsize))
        ajFatal("ensCacheNew requires a maximum size limit, "
                "when a maximum bytes limit is set.");

    /* TODO: Find and set a sensible value here! */
    /* FIXME: size_t can be shorter than ajulong */
    if (debug)
        ajDebug("ensCacheNew maxbytes %Lu, maxcount %u, maxsize %Lu.\n",
                maxbytes, maxcount, maxsize);

    if (maxbytes && (maxbytes < 1000))
        ajFatal("ensCacheNew cannot set a maximum bytes limit (%Lu) under "
                "1000, as each Cache Node requires %Lu bytes alone.",
                maxbytes, sizeof (CacheONode));

    /* TODO: Find and set a sensible value here! */

    if (maxsize && (maxsize < 3))
        ajFatal("ensCacheNew cannot set a maximum size limit (%Lu) under "
                "3 bytes. maximum bytes %Lu maximum count %u.",
                maxsize, maxbytes, maxcount);

    /*
    ** Pointers to functions for automatic reading of data not yet in the
    ** cache and writing of data modified in cache are not mandatory.
    ** If not specified the cache will simply lack this functionality.
    ** However, the specification of a function deleting stale cache entries
    ** and a function calculating the size of value data are required.
    */

    if (!Freference)
        ajFatal("ensCacheNew requires a referencing function.");

    if (!Fdelete)
        ajFatal("ensCacheNew requires a deletion function.");

    if (maxsize && (!Fsize))
        ajFatal("ensCacheNew requires a memory sizing function "
                "when a maximum size limit has been defined.");

    if (!label)
        ajFatal("ensCacheNew requires a label.");

    AJNEW0(cache);

    cache->Label = ajStrNewC(label);
    cache->List  = ajListNew();

    switch (type)
    {
        case ensECacheTypeNumeric:

            cache->Table = ajTableuintNew(0U);

            break;

        case ensECacheTypeAlphaNumeric:

            cache->Table = ajTablestrNew(0U);

            break;

        default:

            ajWarn("ensCacheNew got unexpected Cache type %d.\n",
                   cache->Type);
    }

    /*
    ** Since the AJAX Table does not use real key or value data,
    ** both, keydel and valdel need setting to NULL.
    */

    ajTableSetDestroy(
        cache->Table,
        (void (*)(void **)) NULL,
        (void (*)(void **)) NULL);

    cache->Freference = Freference;
    cache->Fdelete    = Fdelete;
    cache->Fsize      = Fsize;
    cache->Fread      = Fread;
    cache->Fwrite     = Fwrite;
    cache->Type       = type;
    cache->Synchron   = synchron;
    cache->MaxBytes   = maxbytes;
    cache->MaxCount   = maxcount;
    cache->MaxSize    = maxsize;
    cache->Bytes      = 0;
    cache->Count      = 0;
    cache->Dropped    = 0;
    cache->Removed    = 0;
    cache->Stored     = 0;
    cache->Hit        = 0;
    cache->Miss       = 0;

    return cache;
}




/* @section destructors *******************************************************
**
** Destruction destroys all internal data structures and frees the memory
** allocated for an Ensembl Cache object.
**
** @fdata [EnsPCache]
**
** @nam3rule Del Destroy (free) an Ensembl Cache
**
** @argrule * Pcache [EnsPCache*] Ensembl Cache address
**
** @valrule * [void]
**
** @fcategory delete
******************************************************************************/




/* @func ensCacheDel **********************************************************
**
** Default destructor for an Ensembl Cache.
**
** @param [u] Pcache [EnsPCache*] Ensembl Cache address
**
** @return [void]
**
** @release 6.2.0
** @@
** Value data in Cache Nodes that have not been synchronised are written-back.
** Cache flags are reset for value data before the value data is deleted.
** After deletion of all Cache Nodes a summary statistics is printed and the
** Ensembl Cache is destroyed.
******************************************************************************/

void ensCacheDel(EnsPCache *Pcache)
{
    AjBool debug = AJFALSE;

    EnsPCache pthis = NULL;

    if (!Pcache)
        return;

#if defined(AJ_DEBUG) && AJ_DEBUG >= 1
    debug = ajDebugTest("ensCacheDel");

    if (debug)
        ajDebug("ensCacheDel\n"
                "  *Pcache %p\n",
                *Pcache);
#endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 1 */

    if (!(pthis = *Pcache))
        return;

    ensCacheClear(pthis);

    if (debug)
        ensCacheTrace(pthis, 1);

    ajStrDel(&pthis->Label);

    ajListFree(&pthis->List);

    ajTableFree(&pthis->Table);

    ajMemFree((void **) Pcache);

    return;
}




/* @section clear *************************************************************
**
** Clear an Ensembl Cache.
**
** @fdata [EnsPCache]
**
** @nam3rule Clear Clear an Ensembl Cache
**
** @argrule * cache [EnsPCache] Ensembl Cache
**
** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
**
** @fcategory modify
******************************************************************************/




/* @func ensCacheClear ********************************************************
**
** Clear an Ensembl Cache.
**
** @param [u] cache [EnsPCache] Ensembl Cache
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.5.0
** @@
** Value data in Cache Node objects that have not been synchronised are
** written-back. Cache flags are reset for value data before the value data
** is deleted.
******************************************************************************/

AjBool ensCacheClear(EnsPCache cache)
{
    CachePNode node = NULL;

    if (!cache)
        return ajFalse;

    /* Remove Cache Node objects from the AJAX List. */

    while (ajListPop(cache->List, (void **) &node))
    {
        /* Remove the same Cache Node object from the AJAX Table. */

        (void) ajTableRemove(cache->Table, node->Key);

        /* Update the cache statistics. */

        cache->Count--;

        cache->Bytes -= node->Bytes;

        /* Write changes of value data to disk if any. */

        if (cache->Fwrite && node->Value && node->Dirty)
            (*cache->Fwrite) (node->Value);

        /* Both, key and value data are deleted via cacheNodeDel. */

        cacheNodeDel(cache, &node);
    }

    return ajTrue;
}




/* @section debugging *********************************************************
**
** Functions for reporting of an Ensembl Cache object.
**
** @fdata [EnsPCache]
**
** @nam3rule Trace Report Ensembl Cache members to debug file.
**
** @argrule Trace cache [const EnsPCache] Ensembl Cache
** @argrule Trace level [ajuint] Indentation level
**
** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
**
** @fcategory misc
******************************************************************************/




/* @func ensCacheTrace ********************************************************
**
** Writes debug messages to trace the contents of a cache.
**
** @param [r] cache [const EnsPCache] Ensembl Cache
** @param [r] level [ajuint] Indentation level
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCacheTrace(const EnsPCache cache, ajuint level)
{
    double ratio = 0.0;

    AjPStr indent = NULL;

    if (!cache)
        return ajFalse;

    indent = ajStrNew();

    ajStrAppendCountK(&indent, ' ', level * 2);

    if (cache->Hit || cache->Miss)
        ratio = (double) cache->Hit /
            ((double) cache->Hit + (double) cache->Miss);

    ajDebug("%SensCache trace %p\n"
            "%S  Label '%S'\n"
            "%S  List %p length: %Lu\n"
            "%S  Table %p length: %Lu\n"
            "%S  Type %d\n"
            "%S  Synchron '%B'\n"
            "%S  MaxBytes %Lu\n" /* FIXME: size_t can be shorter than ajulong */
            "%S  MaxCount %u\n"
            "%S  MaxSize %Lu\n" /* FIXME: size_t can be shorter than ajulong */
            "%S  Bytes %Lu\n" /* FIXME: size_t can be shorter than ajulong */
            "%S  Count %u\n"
            "%S  Dropped %u\n"
            "%S  Removed %u\n"
            "%S  Stored %u\n"
            "%S  Hit %u\n"
            "%S  Miss %u\n"
            "%S  Hit/(Hit + Miss) %f\n",
            indent, cache,
            indent, cache->Label,
            indent, cache->List, ajListGetLength(cache->List),
            indent, cache->Table, ajTableGetLength(cache->Table),
            indent, cache->Type,
            indent, cache->Synchron,
            indent, cache->MaxBytes,
            indent, cache->MaxCount,
            indent, cache->MaxSize,
            indent, cache->Bytes,
            indent, cache->Count,
            indent, cache->Dropped,
            indent, cache->Removed,
            indent, cache->Stored,
            indent, cache->Hit,
            indent, cache->Miss,
            indent, ratio);

    ajStrDel(&indent);

    return ajTrue;
}




/* @section modify ************************************************************
**
** Update cache object values
**
** @fdata [EnsPCache]
**
** @nam3rule Fetch Fetch value data from an Ensembl Cache
** @nam3rule Remove Remove value data from an Ensembl Cache
** @nam3rule Store Insert a value into an Ensembl Cache
** @nam3rule Synchronise Synchronise an Ensembl Cache
**
** @argrule Fetch cache [EnsPCache] Ensembl Cache
** @argrule Fetch key [void*] Key data address
** @argrule Fetch Pvalue [void**] Value data address address
** @argrule Remove cache [EnsPCache] Ensembl Cache
** @argrule Remove key [const void*] Key data address
** @argrule Store cache [EnsPCache] Ensembl Cache
** @argrule Store key [void*] Key data address
** @argrule Store Pvalue [void**] Value data address adress
** @argrule Synchronise cache [EnsPCache] Ensembl Cache
**
** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
**
** @fcategory modify
******************************************************************************/




/* @func ensCacheFetch ********************************************************
**
** Fetch a value from an Ensembl Cache via a key. If the value is not already
** in the cache it will be read by the function provided at the Cache
** initialisation stage.
**
** The caller is responsible for deleting the returned object.
**
** @param [u] cache [EnsPCache] Ensembl Cache
** @param [r] key [void*] Key data address
** @param [wP] Pvalue [void**] Value data address address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCacheFetch(EnsPCache cache, void *key, void **Pvalue)
{
    AjIList iter = NULL;

    CachePNode lnode = NULL;
    CachePNode tnode = NULL;

    if (!cache)
        return ajFalse;

    if (!key)
        return ajFalse;

    if (!Pvalue)
        return ajFalse;

    *Pvalue = NULL;

    tnode = (CachePNode) ajTableFetchmodV(cache->Table, key);

    if (tnode)
    {
        cache->Hit++;

        /* Move the Cache Node to the end of the AJAX List. */

        iter = ajListIterNew(cache->List);

        while (!ajListIterDone(iter))
        {
            lnode = (CachePNode) ajListIterGet(iter);

            if (lnode == tnode)
            {
                ajListIterRemove(iter);

                ajListPushAppend(cache->List, (void *) lnode);

                break;
            }
        }

        ajListIterDel(&iter);

        /*
        ** Reference the object when returned by the cache so that external
        ** code has to delete it irrespectively whether it was read from the
        ** cache or instantiated by the cache->Fread function.
        */

        if (cache->Freference && tnode->Value)
            *Pvalue = (*cache->Freference) (tnode->Value);
    }
    else
    {
        cache->Miss++;

        if (cache->Fread)
        {
            *Pvalue = (*cache->Fread) (key);

            if (*Pvalue)
            {
                tnode = cacheNodeNew(cache, key, *Pvalue);

                if (!cacheNodeInsert(cache, tnode))
                    cacheNodeDel(cache, &tnode);
            }
        }
    }

    return ajTrue;
}




/* @func ensCacheRemove *******************************************************
**
** Remove value data from an Ensembl Cache via key data.
**
** @param [u] cache [EnsPCache] Ensembl Cache
** @param [r] key [const void*] Key data address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCacheRemove(EnsPCache cache, const void *key)
{
    CachePNode node = NULL;

    if (!cache)
        return ajFalse;

    if (!key)
        return ajFalse;

    node = (CachePNode) ajTableFetchmodV(cache->Table, key);

    if (node)
    {
        cacheNodeRemove(cache, node);

        /* Both, key and value data are deleted via cacheNodeDel. */

        cacheNodeDel(cache, &node);
    }

    return ajTrue;
}




/* @func ensCacheStore ********************************************************
**
** Insert value data into an Ensembl Cache under key data.
**
** @param [u] cache [EnsPCache] Ensembl Cache
** @param [u] key [void*] Key data address
** @param [w] Pvalue [void**] Value data address address
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCacheStore(EnsPCache cache, void *key, void **Pvalue)
{
    CachePNode node = NULL;

    if (!cache)
        return ajFalse;

    if (!key)
        return ajFalse;

    if (!Pvalue)
        return ajFalse;

    /* Is a node already cached under this key? */

    node = (CachePNode) ajTableFetchmodV(cache->Table, key);

    if (node)
    {
        /*
        ** Delete the Object passed in and increase the reference counter
        ** of the cached Object before assigning it.
        */

        (*cache->Fdelete) (Pvalue);

        *Pvalue = (*cache->Freference) (node->Value);
    }
    else
    {
        node = cacheNodeNew(cache, key, *Pvalue);

        if (cacheNodeInsert(cache, node))
        {
            if (cache->Synchron)
            {
                if (cache->Fwrite && node->Value)
                    (*cache->Fwrite) (node->Value);

                node->Dirty = ajFalse;
            }
            else
                node->Dirty = ajTrue;
        }
        else
        {
            if (cache->Fwrite && node->Value)
                (*cache->Fwrite) (node->Value);

            cacheNodeDel(cache, &node);
        }
    }

    return ajTrue;
}




/* @func ensCacheSynchronise **************************************************
**
** Synchronise an Ensembl Cache by writing-back all value data that have not
** been written before.
**
** @param [u] cache [EnsPCache] Ensembl Cache
**
** @return [AjBool] ajTrue upon success, ajFalse otherwise
**
** @release 6.2.0
** @@
******************************************************************************/

AjBool ensCacheSynchronise(EnsPCache cache)
{
    AjIList iter = NULL;

    CachePNode node = NULL;

    if (!cache)
        return ajFalse;

    iter = ajListIterNew(cache->List);

    while (!ajListIterDone(iter))
    {
        node = (CachePNode) ajListIterGet(iter);

        if (cache->Fwrite && node->Value && node->Dirty)
        {
            (*cache->Fwrite) (node->Value);

            node->Dirty = ajFalse;
        }
    }

    ajListIterDel(&iter);

    return ajTrue;
}