simple-cache.c 66.7 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: simple-cache.c,v 0.102 2003/07/12 00:19:20 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991-2002  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 31 32 33
/*
 * This module contains some simple simulations of the routines in
 * cache.c.
 *
 * Extracted from ram-cache.c and rewritten by ceder.
 *
 * New database format with texts in their own file by Inge Wallin.
 *
 * New save algorithm by ceder.
David Byers's avatar
David Byers committed
34
 * Attempt at newer algorithm by byers (FASTSAVE)
Per Cederqvist's avatar
Per Cederqvist committed
35 36
 */

David Byers's avatar
David Byers committed
37 38 39 40 41

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

Per Cederqvist's avatar
Per Cederqvist committed
42 43
#include <errno.h>
#include <stdio.h>
David Byers's avatar
Server:  
David Byers committed
44 45 46
#ifdef HAVE_STDLIB_H
#   include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
47
#include <sys/types.h>
48 49 50
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
51 52 53
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
54 55 56 57
#ifndef SEEK_END
#  include <fcntl.h>
#endif
#ifndef SEEK_END
Per Cederqvist's avatar
Per Cederqvist committed
58 59 60
#  define SEEK_SET 0
#  define SEEK_END 2
#endif
Per Cederqvist's avatar
Per Cederqvist committed
61 62 63
#include <time.h>
#include <setjmp.h>

64
#include "ldifftime.h"
Per Cederqvist's avatar
Per Cederqvist committed
65 66
#include "exp.h"
#include "misc-types.h"
67
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
68 69 70 71 72 73 74 75 76 77 78 79 80
#include "kom-types.h"
#include "cache-node.h"
#include "cache.h"
#include "parser.h"
#include "ram-parse.h"
#include "ram-output.h"
#include "server/smalloc.h"
#include "kom-memory.h"
#include "lyskomd.h"
#include "debug.h"
#include "kom-errno.h"
#include "log.h"
#include "com.h"
81
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
82 83
#include "connections.h"
#include "send-async.h"
84
#include "param.h"
David Byers's avatar
David Byers committed
85
#include "kom-config.h"
David Byers's avatar
David Byers committed
86
#include "admin.h"
87
#include "unused.h"
88
#include "local-to-global.h"
89
#include "server-time.h"
90
#include "eintr.h"
Per Cederqvist's avatar
Per Cederqvist committed
91

92 93 94 95
#ifdef DEBUG_CALLS
#  include "services.h"
#  include "manipulate.h"
#endif
Per Cederqvist's avatar
Per Cederqvist committed
96
/*
97
 * Possible improvements:
Per Cederqvist's avatar
Per Cederqvist committed
98
 *
99 100 101
 *  FIXME (bug 167): When there are consecutive items in file A that
 *  shall be copied to file B, copy them in one transfer (up to a
 *  certain limit).
Per Cederqvist's avatar
Per Cederqvist committed
102 103 104 105 106 107 108
 */

/*
 * All functions that can fail sets kom_errno to a suitable value
 * if they fail.
 */

David Byers's avatar
David Byers committed
109
static Small_conf      ** small_conf_arr;
110 111
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
112
static Conf_no		  next_free_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
113

114
static Cache_node_mcb 	* text_mcb;
115
static Text_no		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
116 117 118 119 120 121 122 123 124 125 126

/*
 * The elements in the following lists with same index refers to the same
 * conference.
 */
static int    no_of_match_info;
EXPORT Matching_info *match_table = NULL;

static FILE	*text_file= NULL;
static FILE	*file_a = NULL;	/* Current file. */
static FILE	*file_b = NULL;	/* File under construction. */
David Byers's avatar
David Byers committed
127 128 129
#ifdef FASTSAVE
static FILE     *file_b_r = NULL; /* Read from file under construction */
#endif
Per Cederqvist's avatar
Per Cederqvist committed
130 131 132 133 134

/*
 * Four state variables for the background save.
 */
static enum {
135
    sync_idle,
Per Cederqvist's avatar
Per Cederqvist committed
136 137 138 139 140 141 142 143
    sync_save_conf,
    sync_save_pers,
    sync_save_text,
    sync_error,
    sync_wait,
    sync_ready
} sync_state;

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* The state machine sync_state works like this:
 *
 *  Old state              action           new state
 *  none               sync_part called     sync_idle
 *                     for the first time
 *  sync_idle	       SYNC_INTERVAL        sync_save_conf
 *  sync_save_conf     all confs saved      sync_save_pers
 *  sync_save_pers     all persons saved    sync_save_text
 *  sync_save_text     all texts saved      sync_ready
 *  sync_ready         sync_part called     sync_idle
 *  any                error occurs         sync_error
 *  sync_error         sync_part called     sync_wait
 *  sync_wait          SYNC_RETRY_INTERVAL  sync_save_conf
 */

David Byers's avatar
David Byers committed
159
#ifndef FASTSAVE
160 161 162 163 164
/* sync_next should be split in next_text_to_sync and
   next_conf_to_sync to avoid warnings about the different sizes of
   Conf_no and Text_no.  Since that is indeed done if FASTSAVE is set,
   don't bother doing it in the old code that will anyhow soon be
   replaced. */
165
static unsigned long sync_next;
David Byers's avatar
David Byers committed
166
#endif
Per Cederqvist's avatar
Per Cederqvist committed
167

168 169
static Conf_no highest_conf_no = 0;
static Text_no highest_text_no = 0;
Per Cederqvist's avatar
Per Cederqvist committed
170 171 172

BUGDECL;

173 174 175 176 177 178 179
#ifdef DEBUG_CALLS

/* This is used by cache_sync_start() and cache_sync_finish().  */
static int block_after_pre_sync = 0;

#endif

180 181 182 183
/* Define LOGACCESSES if you want to be able to log all accesses to
   the data base. */

#ifdef LOGACCESSES
184
enum log_type {
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
    lt_restart,
    lt_text_stat,
    lt_text_mass,
    lt_conf_stat,
    lt_pers_stat,
    
    lt_text_def,
    lt_conf_def,
    lt_pers_def,

    lt_create_text,
    lt_garb_text,
    lt_delete_text,
    lt_create_conf,
    lt_delete_conf,
    lt_create_pers,
    lt_delete_pers,

    lt_lock_conf,
    lt_unlock_conf,
    lt_lock_pers,
    lt_unlock_pers,

    lt_get_highest,		/* Get highest Local_text_no for a conf. */
    lt_get_conf_type
    /* Note: mark_*_as_changed is not logged. */
211
};
212 213 214 215 216

static FILE *logfile = NULL;
static int syncing_or_saving = 0;
static int garb_running = 0;

217
static void log_access(enum log_type t,
218 219
		       int id)
{
220 221
    extern int putw(int, FILE *);

222 223 224 225 226 227 228 229 230 231 232
    if (garb_running + syncing_or_saving == 0)
    {
	putc(t, logfile);
	putw(id, logfile);
    }
}
#define LOGACC(a,b) {if (logfile) log_access(a, b);}
#else
#define LOGACC(a,b)
#endif

David Byers's avatar
Server:  
David Byers committed
233 234 235 236 237 238 239 240 241

/* Local functions */

static Success build_matching_info(void);
static Success rebuild_matching_info_entry(Conf_no conf_no);
static Matching_info *find_matching_info(Conf_no conf_no);



Per Cederqvist's avatar
Per Cederqvist committed
242 243 244 245 246 247 248 249 250 251 252

/* Macros */

#define TRACE2(format, arg) if ( buglevel > 2 ) printf(format, arg)
#define TRACE1(format) if ( buglevel > 2 ) printf(format)
#define TRACESTR(str)  if ( buglevel > 2 ) s_puts(str)


static Person *
read_person(FILE *fp,
	    long pos,
253
	    long UNUSED(size))	/* FIXME (bug 168): sanity-check the size */
Per Cederqvist's avatar
Per Cederqvist committed
254 255
{
    Person *p;
David Byers's avatar
David Byers committed
256
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
257 258

    p = alloc_person();
259
    fseek(fp, pos+1, SEEK_SET);	/* Skip 'P' */
David Byers's avatar
David Byers committed
260
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274
    if ( fparse_person(fp, p) != OK )
    {
	free_person(p);
	return NULL;
    }
    else
	return p;
}



static Conference *
read_conference(FILE *fp,
		long pos,
275
		long UNUSED(size)) /* FIXME (bug 169): sanity-check the size */
Per Cederqvist's avatar
Per Cederqvist committed
276 277
{
    Conference *c;
David Byers's avatar
David Byers committed
278
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
279 280

    c = alloc_conference();
281
    fseek(fp, pos+1, SEEK_SET);	/* Skip 'C' */
David Byers's avatar
David Byers committed
282
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
283 284 285 286 287 288 289 290 291 292 293 294 295
    if ( fparse_conference(fp, c) != OK )
    {
	free_conference(c);
	return NULL;
    }
    else
	return c;
}
	

static Text_stat *
read_text_stat(FILE *fp,
	       long pos,
296
	       long UNUSED(size)) /* FIXME (bug 170): sanity-check the size */
Per Cederqvist's avatar
Per Cederqvist committed
297 298
{
    Text_stat *t;
David Byers's avatar
David Byers committed
299
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
300 301

    t = alloc_text_stat();
302
    fseek(fp, pos+1, SEEK_SET);	/* Skip 'T' */
David Byers's avatar
David Byers committed
303
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
304 305 306 307 308 309 310 311 312 313
    if ( fparse_text_stat(fp, t) != OK )
    {
	free_text_stat(t);
	return NULL;
    }
    else
	return t;
}

static void
314
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
315
{
316
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
317 318 319
}

static void
320
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
321
{
322
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
323 324 325
}

static void
326
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
327
{
328 329
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
330

331 332 333
static Cache_node *
get_pers_node(Pers_no pers_no)
{
334
    if (pers_no >= next_free_num || pers_no < 1)
335 336
	return NULL;

337
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
338 339 340
}

static void
341
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
342
{
343
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
344 345
}

346 347 348 349 350
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
351 352

static void
353
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
354
{
355 356
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
357

358 359 360
static Cache_node *
get_conf_node(Conf_no conf_no)
{
361
    if (conf_no >= next_free_num || conf_no < 1)
362 363
	return NULL;

364 365
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
366

367 368 369
static Cache_node *
get_text_node(Text_no text_no)
{
370
    if (text_no >= next_text_num || text_no < 1)
371 372
	return NULL;

373
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
}

	
/*
 * Name caching routines
 */

/*
 * change_name changes the cached conference name. It is only called when
 * a conference name is changed or a conference is deleted.
 */
void
cached_change_name( Conf_no name_num,
		    String new_name )
{
    if ( name_num < 1 || name_num >= next_free_num )
390
	restart_kom("cached_change_name(%d, ----): next_free_num==%d\n",
Per Cederqvist's avatar
Per Cederqvist committed
391 392 393 394
		    name_num, next_free_num);

    s_clear( &small_conf_arr[name_num]->name );
    s_strcpy( &small_conf_arr[name_num]->name, new_name);
David Byers's avatar
Server:  
David Byers committed
395
    rebuild_matching_info_entry(name_num);
Per Cederqvist's avatar
Per Cederqvist committed
396 397 398 399 400 401 402 403 404
}


extern Conf_type
cached_get_conf_type (Conf_no conf_no)
{
    if ( conf_no < 1 || conf_no >= next_free_num )
	restart_kom("cached_get_conf_type(%d): next_free_num==%d",
		    conf_no, next_free_num);
405 406 407 408 409

    if ( small_conf_arr [ conf_no ] == NULL )
	restart_kom("cached_get_conf_type(%d): conference does not exist.\n",
		    conf_no);

410 411
    LOGACC(lt_get_conf_type, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
412 413 414
    return small_conf_arr [ conf_no ]->type;
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
extern Conf_no
cached_get_conf_supervisor(Conf_no conf_no)
{
    if (conf_no < 1 || conf_no >= next_free_num)
	restart_kom("cached_get_conf_supervisor(%d): next_free_num==%d\n",
		    conf_no, next_free_num);

    if (small_conf_arr[conf_no] == NULL)
	restart_kom("cached_get_conf_supervisor(%d):"
		    " conference does not exist.\n", conf_no);

    LOGACC(lt_get_conf_supervisor, conf_no);

    return small_conf_arr[conf_no]->supervisor;
}

431 432 433 434 435 436 437 438
/*
 * Return number of conferences present.  (Actually, return a number
 * at least as large as the number of conferences present).
 */
extern Conf_no
cached_no_of_existing_conferences(void)
{
    return next_free_num;	/* This is too large, but who cares? */
439 440 441 442
    /* Actually, this is used in lookup_regexp (and maybe other
       places) to allocate an array this large, and that is pretty
       stupid if many conferences have been deleted.  But it is no big
       deal.  Not yet, anyhow...  */
443
    /* FIXME (bug 164): We should return a better estimate. */
444
}
Per Cederqvist's avatar
Per Cederqvist committed
445 446 447 448 449 450 451 452

/*
 * Various function calls to tell the cache that something is changed.
 */

void
mark_person_as_changed(Pers_no	pers_no)
{
453 454 455 456
    Cache_node *node;

    node = get_pers_node(pers_no);
    
Per Cederqvist's avatar
Per Cederqvist committed
457
    TRACE2("Person %d is changed\n", pers_no);
458
    if ( node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
459 460
	restart_kom("mark_person_as_changed(%d): nonexistent.\n", pers_no);

461
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
462 463 464
    pers_set_mru( pers_no );
}

465 466 467 468 469 470 471 472 473 474 475
/*
 * Mark the conference as dirty, so that it will be written to
 * the disk.
 *
 * Also update all fields in the Small_conf except then name, so that
 * they are always current.
 *
 * NOTE: You must call cached_change_name when the name changes.
 *       It is not necessary to call cached_change_name after
 *       cached_create_conf.
 */
Per Cederqvist's avatar
Per Cederqvist committed
476 477 478 479

void
mark_conference_as_changed(Conf_no	conf_no)
{
480
    Cache_node *node;
481
    Conference *conf_c;
482 483 484

    node = get_conf_node (conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
485
    TRACE2("Conf.  %d is changed\n", conf_no);
486
    if ( node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
487 488
	restart_kom("mark_conference_as_changed(%d): nonexistent.\n", conf_no);

489
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
490
    conf_set_mru( conf_no );
491 492 493

    conf_c = (Conference *) node->ptr;

494
    small_conf_arr[conf_no]->highest_local_no
495
	= l2g_first_appendable_key(&conf_c->texts) - 1;
496

497 498 499
    small_conf_arr[conf_no]->nice = conf_c->nice;
    small_conf_arr[conf_no]->type = conf_c->type;
    small_conf_arr[conf_no]->supervisor = conf_c->supervisor;
Per Cederqvist's avatar
Per Cederqvist committed
500 501 502 503 504
}

void
mark_text_as_changed( Text_no text_no )
{
505 506 507 508
    Cache_node *node;

    node = get_text_node (text_no);

509
    TRACE2("Text %lu is changed.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
510
    if ( text_no < 1 || text_no >= next_text_num
511
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
512
    {
513
	restart_kom("mark_text_as_changed(%lu): nonexistent.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
514 515
    }

516 517
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
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
/*
 * Store and retrieve the highest number used.
 */

static int
write_number_file(void)
{
    FILE *fp;

    if ((fp = i_fopen(param.numberfile_tmp_name, "w")) == NULL)
    {
	kom_log("opening %s: %s\n", param.numberfile_tmp_name,
		strerror(errno));
	return -1;
    }
    fprintf(fp, "Text_no: %lu Conf_no: %lu End.\n",
	    (unsigned long)next_text_num,
	    (unsigned long)next_free_num);
    if (fflush(fp) != 0)
    {
	kom_log("fflush to %s failed\n", param.numberfile_tmp_name);
	i_fclose(fp);
	return -1;
    }

    if (ferror(fp) != 0)
    {
	kom_log("fprintf to %s failed\n", param.numberfile_tmp_name);
	i_fclose(fp);
	return -1;
    }

    if (i_fclose(fp) != 0)
    {
	kom_log("fclose %s failed\n", param.numberfile_tmp_name);
	return -1;
    }

    if (i_rename(param.numberfile_tmp_name, param.numberfile_name) != 0)
    {
	kom_log("failed to rename %s to %s: %s\n",
		param.numberfile_tmp_name, param.numberfile_name,
		strerror(errno));
	return -1;
    }
    return 0;
}

static void
read_number_file(void)
{
    FILE *fp;
    unsigned long txt = 0;
    unsigned long cno = 0;
    Text_no text_no;
    Conf_no conf_no;

    if ((fp = i_fopen(param.numberfile_name, "r")) == NULL)
    {
	if (errno == ENOENT)
	    kom_log("WARN: %s: No such file\n", param.numberfile_name);
	else
	    restart_kom("opening %s: %s\n", param.numberfile_name,
			strerror(errno));
	return;
    }
    if (fscanf(fp, "Text_no: %lu Conf_no: %lu", &txt, &cno) != 2
	|| getc(fp) != ' ' || getc(fp) != 'E' || getc(fp) != 'n'
	|| getc(fp) != 'd' || getc(fp) != '.')
    {
	kom_log("WARN: %s: broken file (ignored)\n", param.numberfile_name);
	i_fclose(fp);
	return;
    }
    i_fclose(fp);

    text_no = txt;
    conf_no = cno;
    
    if (text_no > next_text_num)
    {
	kom_log("WARN: Texts %lu - %lu were lost.\n",
		(unsigned long)next_text_num,
		(unsigned long)text_no - 1);
	next_text_num = text_no;
    }
    if (conf_no > next_free_num)
    {
	kom_log("WARN: Confs %lu - %lu were lost.\n",
		(unsigned long)next_free_num,
		(unsigned long)conf_no - 1);
	next_free_num = conf_no;
    }
}
Per Cederqvist's avatar
Per Cederqvist committed
615 616 617 618 619 620 621 622 623

/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
624 625
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
626 627 628 629 630 631 632 633
    TRACE2("Person %d is being created.\n", person);

    if ( person < 1 || person >= next_free_num )
    {
	restart_kom("cached_create_person(%d): next_free_num == %d.\n",
		    person, next_free_num);
    }

634
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
635 636 637 638 639
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

640 641 642 643 644
    create_cache_node (pers_mcb, person);
    node = get_pers_node (person);

    if ( node == NULL )
	restart_kom("cached_create_person(): couldn't get cache_node.\n");
645

646 647 648
    node->ptr = alloc_person();
    node->s.dirty = 1;
    node->s.exists = 1;
Per Cederqvist's avatar
Per Cederqvist committed
649
    pers_set_mru( person );
650 651 652

    LOGACC(lt_create_pers, person);

Per Cederqvist's avatar
Per Cederqvist committed
653 654 655 656 657 658 659
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
660 661
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
662 663 664 665
    TRACE2("cached_get_person_stat %d\n", person);

    if ( person == 0 )
    {
666
        err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
667 668 669
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
670

671 672
    if ( person >= next_free_num )
    {
673
        err_stat = person;
674 675 676 677
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

678 679
    node = get_pers_node (person);

680
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
681
    {
682
        err_stat = person;
Per Cederqvist's avatar
Per Cederqvist committed
683 684 685 686
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

687 688
    LOGACC(lt_pers_stat, person);

689
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
690 691
    {
	pers_set_mru( person );
692
	++pers_mcb->hits;
693
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
694 695
    }

696
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
697
    {
698
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
699
	pers_set_mru (person);
700
	++pers_mcb->hits;
701
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
702 703
    }

704
    node->ptr = read_person(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
705

706
    ++pers_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
707
    pers_set_mru (person);
708
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
709 710 711 712 713 714
}


/*
 * Conference-related calls
 */
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730


static int no_of_allocated_small_confs = 0;

static void
free_small_conf (Small_conf *sc)
{
    if ( sc != NULL )
    {
	--no_of_allocated_small_confs;
	s_clear ( &sc->name );
	sfree (sc);
    }
}


731 732 733 734 735
static void
init_small_conf(Small_conf *sc)
{
    sc->name = EMPTY_STRING;
    init_conf_type(&sc->type);
736
    sc->supervisor = 0;
737 738 739 740
    sc->highest_local_no = 0;
    sc->nice = param.default_nice;
}

741 742 743 744 745
static Small_conf *
alloc_small_conf(void)
{
    Small_conf *s;
    s = smalloc(sizeof(Small_conf));
746
    init_small_conf(s);
747 748 749 750 751 752
    ++no_of_allocated_small_confs;

    return s;
}


753 754 755 756 757 758
/*
 * Create a conference.
 *
 * Set up a Conference and cache the name in the small_conf_array.
 */
extern Conf_no
Per Cederqvist's avatar
Per Cederqvist committed
759 760 761 762
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
763
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
764 765 766 767 768

    TRACE1("cached_create_conf( ");
    TRACESTR(name);
    TRACE1(" )\n");

David Byers's avatar
David Byers committed
769
    if ( next_free_num >= param.max_conf )
Per Cederqvist's avatar
Per Cederqvist committed
770
    {
771
        err_stat = next_free_num;
Per Cederqvist's avatar
Per Cederqvist committed
772 773 774 775 776 777
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return 0;
    }
    
    conf_no = next_free_num++;

778 779 780 781 782 783 784 785
    if (write_number_file() < 0)
    {
	next_free_num--;
	err_stat = 0;
	kom_errno = KOM_TEMPFAIL;
	return 0;
    }

786 787 788 789 790 791 792 793 794
    create_cache_node (conf_mcb, conf_no);
    node = get_conf_node (conf_no);

    if ( node == NULL )
	restart_kom("cached_create_conf(): failed to allocate cache_node.\n");
    
    node->s.exists = 1;
    node->s.dirty = 1;
    node->ptr = conf_c = alloc_conference();
Per Cederqvist's avatar
Per Cederqvist committed
795 796
    conf_set_mru(conf_no);    

797
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
798 799 800 801 802 803
    small_conf_arr[ conf_no ] = alloc_small_conf();

    conf_c->name = EMPTY_STRING;
    s_strcpy(&conf_c->name, name);
    cached_change_name( conf_no, name);

804
    TRACE2("Created conference number %d\n", conf_no);
805 806 807

    LOGACC(lt_create_conf, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
808 809 810 811 812 813 814
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
815 816
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
817 818
    if ( conf == 0 )
    {
819
        err_stat = conf;
Per Cederqvist's avatar
Per Cederqvist committed
820 821 822
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
823

824 825
    if ( conf >= next_free_num )
    {
826
        err_stat = conf;
827 828 829 830
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

831 832
    node = get_conf_node (conf);

833
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
834
    {
835
        err_stat = conf;
Per Cederqvist's avatar
Per Cederqvist committed
836 837 838 839
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

840
    if ( node->lock_cnt > 0 )
David Byers's avatar
David Byers committed
841
	kom_log("WNG: cached_delete_conf(%d): lock_cnt === %d\n",
842
	    conf, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
843

David Byers's avatar
David Byers committed
844 845 846
    free_small_conf(small_conf_arr[conf]);
    small_conf_arr[conf] = NULL;

847 848 849
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
850

851 852
    LOGACC(lt_delete_conf, conf);

David Byers's avatar
Server:  
David Byers committed
853
    rebuild_matching_info_entry(conf);
Per Cederqvist's avatar
Per Cederqvist committed
854 855 856 857 858 859
    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
860 861
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
862 863
    if ( pers == 0 )
    {
864
        err_stat = pers;
Per Cederqvist's avatar
Per Cederqvist committed
865 866 867
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
868

869 870
    if ( pers >= next_free_num )
    {
David Byers's avatar
David Byers committed
871
	kom_log("cached_delete_person(%lu): next_free_num == %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
872
	    (unsigned long)pers, (unsigned long)next_free_num);
873
        err_stat = pers;
874 875 876 877
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

878 879 880
    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
881
    {
David Byers's avatar
David Byers committed
882
	kom_log("cached_delete_person(): attempt to delete void person.\n");
883
        err_stat = pers;
Per Cederqvist's avatar
Per Cederqvist committed
884 885 886 887
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

888
    if ( node->lock_cnt > 0 )
David Byers's avatar
David Byers committed
889
	kom_log("cached_delete_pers(%lu): lock_cnt === %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
890
	    (unsigned long)pers, (unsigned long)node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
891

892 893
    LOGACC(lt_delete_pers, pers);

894 895 896
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
897 898 899 900 901 902
    return OK;
}

Success
cached_delete_text(Text_no text)
{
903 904
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
905 906
    if ( text == 0 )
    {
907
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
908 909 910
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
911 912 913 914 915

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
916
    {
David Byers's avatar
David Byers committed
917
	kom_log("cached_delete_text(): attempt to delete void text %lu.\n", text);
918
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
919 920 921 922
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

923
    if ( node->lock_cnt > 0 )
David Byers's avatar
David Byers committed
924
	kom_log("cached_delete_text(%lu): lock_cnt === %d\n",
925
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
926 927


928 929 930
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
931

932 933 934
#ifdef LOGACCESSES
    if (garb_running)
    {
935
	LOGACC(lt_garb_text, text);
936 937
    }
    else
938
	LOGACC(lt_delete_text, text);
939
#endif
Per Cederqvist's avatar
Per Cederqvist committed
940 941 942 943 944
    return OK;
}


extern Conference *
945
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
946
{
947 948
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
949 950 951 952
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
953
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
954 955 956
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
957 958 959 960

    node = get_conf_node (conf_no);

    if ( conf_no >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
961
    {
962
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
963 964 965 966
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

967 968
    LOGACC(lt_conf_stat, conf_no);

969
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
970
    {
971
	conf_set_mru (conf_no);
972
	++conf_mcb->hits;
973
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
974 975
    }

976
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
977
    {
978
	node->ptr = copy_conference (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
979
	conf_set_mru (conf_no);
980
	++conf_mcb->hits;
981
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
982 983
    }

984
    node->ptr = read_conference(file_a, node->pos, node->size);
985
    ++conf_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
986 987

    conf_set_mru (conf_no);
988
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
989 990 991 992 993 994 995 996
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
997 998 999
    if (conf_no == 0 || conf_no >= next_free_num )
	return FALSE;

David Byers's avatar
David Byers committed
1000
    return small_conf_arr[conf_no] != NULL ? TRUE : FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
1001 1002 1003 1004 1005 1006 1007 1008
}

    
/*
 * Calls to handle texts
 */

/*
1009
 * FIXME (bug 171): cached_get_text() should return Success.
Per Cederqvist's avatar
Per Cederqvist committed
1010 1011 1012 1013 1014 1015 1016
 */
extern String
cached_get_text( Text_no text )
{
    String  the_string;
    Text_stat *t_stat;

1017
    TRACE2("cached_get_text %lu\n", text);
Per Cederqvist's avatar
Per Cederqvist committed
1018 1019 1020 1021 1022

    if ( (t_stat = cached_get_text_stat (text)) == NULL )
	return EMPTY_STRING;
    else
    {
1023
	LOGACC(lt_text_mass, text);
Per Cederqvist's avatar
Per Cederqvist committed
1024 1025 1026 1027 1028
	the_string.string = tmp_alloc( t_stat->no_of_chars );
	the_string.len = t_stat->no_of_chars;
	fseek(text_file, t_stat->file_pos, SEEK_SET);

	if ( fread(the_string.string, sizeof(char), the_string.len, text_file)
David Byers's avatar
David Byers committed
1029
	    != (size_t)the_string.len )
Per Cederqvist's avatar
Per Cederqvist committed
1030
	{
David Byers's avatar
David Byers committed
1031
	    kom_log("WARNING: cached_get_text: premature end on text %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
		text);
	    return EMPTY_STRING;
	}
		    
	return the_string;
    }
}


extern Text_stat *	/* NULL on error */
cached_get_text_stat(	Text_no		text )
{
1044 1045
    Cache_node *node;

1046
    TRACE2("cached_get_text_stat(%lu);  next_text_num == ", text);
1047
    TRACE2("%lu\n", (unsigned long)next_text_num);
Per Cederqvist's avatar
Per Cederqvist committed
1048 1049 1050

    if ( text == 0 )
    {
1051
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
1052 1053 1054
	kom_errno = KOM_TEXT_ZERO;
	return NULL;
    }
1055 1056