75b9604b63303a31d1eeb5691ed8a1f7c7ec93a7
[idzebra-moved-to-github.git] / bfile / commit.c
1 /*
2  * Copyright (C) 1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: commit.c,v $
7  * Revision 1.8  1996-02-07 14:03:49  adam
8  * Work on flat indexed shadow files.
9  *
10  * Revision 1.7  1996/02/07  10:08:46  adam
11  * Work on flat shadow (not finished yet).
12  *
13  * Revision 1.6  1995/12/15  12:36:53  adam
14  * Moved hash file information to union.
15  * Renamed commit files.
16  *
17  * Revision 1.5  1995/12/12  15:57:55  adam
18  * Implemented mf_unlink. cf_unlink uses mf_unlink.
19  *
20  * Revision 1.4  1995/12/11  09:03:55  adam
21  * New function: cf_unlink.
22  * New member of commit file head: state (0) deleted, (1) hash file.
23  *
24  * Revision 1.3  1995/12/01  16:24:29  adam
25  * Commit files use separate meta file area.
26  *
27  * Revision 1.2  1995/12/01  11:37:24  adam
28  * Cached/commit files implemented as meta-files.
29  *
30  * Revision 1.1  1995/11/30  08:33:13  adam
31  * Started work on commit facility.
32  *
33  */
34
35 #include <assert.h>
36 #include <stdlib.h>
37
38 #include <alexutil.h>
39 #include <mfile.h>
40 #include "cfile.h"
41
42 void cf_unlink (CFile cf)
43 {
44     if (cf->bucket_in_memory)
45     {
46         logf (LOG_FATAL, "Cannot unlink potential dirty cache");
47         exit (1);
48     }
49     cf->head.state = 0;
50     cf->dirty = 1;
51     mf_unlink (cf->block_mf);
52     mf_unlink (cf->hash_mf);
53 }
54
55    
56 static void cf_commit_hash (CFile cf)
57
58     int i, bucket_no;
59     int hash_bytes;
60     struct CFile_ph_bucket *p;
61
62     p = xmalloc (sizeof(*p));
63     hash_bytes = cf->head.hash_size * sizeof(int);
64     bucket_no = cf->head.first_bucket;
65     for (; bucket_no < cf->head.next_bucket; bucket_no++)
66     {
67         if (!mf_read (cf->hash_mf, bucket_no, 0, 0, p))
68         {
69             logf (LOG_FATAL, "read commit hash");
70             exit (1);
71         }
72         for (i = 0; i<HASH_BUCKET && p->vno[i]; i++)
73         {
74             if (!mf_read (cf->block_mf, p->vno[i], 0, 0, cf->iobuf))
75             {
76                 logf (LOG_FATAL, "read commit block");
77                 exit (1);
78             }
79             mf_write (cf->rmf, p->no[i], 0, 0, cf->iobuf);
80         }
81     }
82     xfree (p);
83 }
84
85 static void cf_commit_flat (CFile cf)
86 {
87     int *fp;
88     int hno;
89     int i, vno = 0;
90
91     fp = xmalloc (HASH_BSIZE);
92     for (hno = cf->head.next_bucket; hno < cf->head.flat_bucket; hno++)
93     {
94         mf_read (cf->hash_mf, hno, 0, 0, fp);
95         for (i = 0; i < (HASH_BSIZE/sizeof(int)); i++)
96         {
97             if (fp[i])
98             {
99                 if (!mf_read (cf->block_mf, fp[i], 0, 0, cf->iobuf))
100                 {
101                     logf (LOG_FATAL, "read commit block at %d (->%d)",
102                           fp[i], vno);
103                     exit (1);
104                 }
105                 mf_write (cf->rmf, vno, 0, 0, cf->iobuf);
106             }
107             vno++;
108         }
109     }
110     xfree (fp);
111 }
112
113 void cf_commit (CFile cf)
114 {
115
116     if (cf->bucket_in_memory)
117     {
118         logf (LOG_FATAL, "Cannot commit potential dirty cache");
119         exit (1);
120     }
121     if (cf->head.state == 1)
122         cf_commit_hash (cf);
123     else if (cf->head.state == 2)
124         cf_commit_flat (cf);
125 }
126