text-garb.c 5.17 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: text-garb.c,v 0.8 1992/11/22 15:14:52 ceder Exp $
Linus Tolke Y's avatar
Linus Tolke Y committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
29
30
/*
 * This file contains the functions that deletes old texts.
 *
 * Author: Per Cederqvist.
 */

31
static char *rcsid = "$Id: text-garb.c,v 0.8 1992/11/22 15:14:52 ceder Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
32
33


Per Cederqvist's avatar
Per Cederqvist committed
34
35
36
37
38
39
#include <time.h>
#include <kom-types.h>
#include "cache.h"
#include "log.h"
#include "internal-services.h"
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
40
#include "text-garb.h"
Per Cederqvist's avatar
Per Cederqvist committed
41
42
#include <debug.h>

43
44
45
/*
 * This comment is a description of how this _could_ be done in a more
 * efficient way.  It is not yet implemented.  Design by Inge Wallin
46
 * and Per Cederqvist 1992-10-07. +++FIXME.
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
 *
 * Today, all text statuses are read into the core during the garbage
 * collection phase. Due to step 3 below we think that we can reduce
 * the number of text-status-fetches by 95 %.
 *
 * 1) Allocate a bitmap with one bit for each text. Clear all bits.
 *    This bit is set as soon as it is certain that this text should
 *    not be deleted during this garbage collection pass.
 * 2) Loop over all persons:
 *	+ Set the bit for all his/her marked texts.
 *    (This step may, or may not, be efficient. Profile your code!)
 * 3) Loop over all conferences:
 *	+ Loop over all texts in the conference:
 *		+ If the bit is already set, skip the text.
 *		+ Retrieve the text status from the data base.
 *		+ If it is old enough (in all the recipient conferences)
 *			delete it.
 *		  else
 *		        set the bit.
 *		+ If it is too young to be deleted (only
 *    		  considering this conferene)
 *			+ Set the bit for this text and all subsequent
 *    			  texts in this conference.
 * 4) Loop over all remaining texts:
 *	+ If the bit is not set,
 *		+ delete the text. (It has no recipients).
 * 5) Deallocate the bitmap and wait 24 hours.
 */
Per Cederqvist's avatar
Per Cederqvist committed
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

/*
 * Delete old texts.
 *
 * Return value is TRUE if there is nothing more to do right now,
 * FALSE if this function should be called again as soon as the
 * server has some time over.
 */

Bool
garb_text(void)
{
    static Text_no last_checked = 0;
    static time_t  last_start = NO_TIME;
    static long	   deleted_texts = 0;
    BUGDECL;

    Text_stat	  *text_s;
    Misc_info 	  *misc;
    u_short 	   nmisc;
    double	   age;			/* How many seconds is this text? */
    double	   limit = 24 * 3600;
                    
    if ( last_checked == 0 )
    {
	if ( last_start != NO_TIME
	    && difftime(time(NULL), last_start) < GARB_INTERVAL * 60 )
	{
	    return TRUE;
	}
	
	log("MSG: garb started.\n");
	time( &last_start );
    }
109
110

    tell_cache_garb_text(1);
Per Cederqvist's avatar
Per Cederqvist committed
111
112
113
114
115
    last_checked = traverse_text( last_checked );

    if ( last_checked == 0 )
    {
	log("MSG: garb ready. %lu texts deleted.\n", (u_long)deleted_texts);
116
	deleted_texts = 0;
117
	tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
118
119
120
121
122
123
	return FALSE;
    }
    
    if ( (text_s = cached_get_text_stat( last_checked )) == NULL )
    {
	log("ERROR: garb_text(): Can't get text-stat.\n");
124
	tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
125
126
127
128
129
130
131
	return FALSE;
    }

    age = difftime( time(NULL), text_s->creation_time );
    
    if ( text_s->no_of_marks > 0 || age < 24 * 3600 )
    {
132
	tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
133
134
135
136
137
138
139
140
141
142
	return FALSE;
    }

    for (nmisc = text_s->no_of_misc, misc = text_s->misc_items;
	 nmisc > 0;
	 --nmisc, ++misc )
    {
	switch ( misc->type )
	{
	case recpt:
143
144
145
146
147
	    if ( cached_conf_exists( misc->datum.recipient ) )
	    {
		limit = (24 * 3600 *
			 cached_get_garb_nice( misc->datum.recipient ));
		if ( age < limit )	
148
149
		{
		    tell_cache_garb_text(0);
150
		    return FALSE;
151
		}
152
153
	    }

Per Cederqvist's avatar
Per Cederqvist committed
154
155
156
	    break;

	case cc_recpt:
157
158
159
160
161
	    if ( cached_conf_exists( misc->datum.cc_recipient ))
	    {
		limit = ( 24 * 3600
			 * cached_get_garb_nice( misc->datum.cc_recipient ));
		if ( age < limit )
162
163
		{
		    tell_cache_garb_text(0);
164
		    return FALSE;
165
		}
166
	    }
Per Cederqvist's avatar
Per Cederqvist committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

	    break;

	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	    limit = 24 * 3600;
	    break;
	    
	case loc_no:
	case rec_time:
	case sent_by:
	    break;
	    
	case sent_at:
	    if ( difftime ( time(NULL), misc->datum.sent_at )
		< limit )
	    {
186
		tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		return FALSE;
	    }
	    		
	    break;

#ifndef COMPILE_CHECKS
	default:
	    restart_kom("garb_text(): Illegal misc-item.\n");
#endif
	}
    }

    VBUG(("garb_text: deleting %d\n", last_checked));
    do_delete_text ( last_checked, text_s );
    deleted_texts++;
202
    tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
203
204
205
    return FALSE;
}