simple-cache.c 37.8 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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.
 */

12
static char *rcsid = "$Id: simple-cache.c,v 0.13 1991/08/30 03:35:52 ceder Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
13 14


Per Cederqvist's avatar
Per Cederqvist committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

/*
 * Possible improvements: +++
 *  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).
 *
 *  In pre_sync: compute size of, and allocate disk space for file B.
 */

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/file.h>

#include <kom-errno.h>
#include <kom-types.h>
Per Cederqvist's avatar
Per Cederqvist committed
36
#include "s-collat-tabs.h"
37
#include <server/smalloc.h>
Per Cederqvist's avatar
Per Cederqvist committed
38
#include <parser.h>
Per Cederqvist's avatar
Per Cederqvist committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "cache.h"
#include <debug.h>
#include "lyskomd.h"
#include "ram-parse.h"
#include "log.h"
#include "com.h"
#include "isc.h"
#include "ram-output.h"
#include "connections.h"
#include "prot-a-output.h"
#include "send-async.h"
#include "memory.h"
#include "exp.h"

53
#include "tmp-limits.h"
54
#include "cache-node.h"
Per Cederqvist's avatar
Per Cederqvist committed
55 56

static Small_conf	* small_conf_arr[ MAX_CONF ];
57 58
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
Per Cederqvist's avatar
Per Cederqvist committed
59 60
static int		  next_free_num = 1;

61 62
static Cache_node_mcb 	* text_mcb;
static int		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
63 64

/* Defined in ramkomd.c */
65 66 67
extern char datafilename[BUFSIZ];
extern char backupfilename[BUFSIZ];
extern char textfilename[BUFSIZ];
Per Cederqvist's avatar
Per Cederqvist committed
68 69 70 71 72 73 74

/*
 * 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;
Per Cederqvist's avatar
Per Cederqvist committed
75
EXPORT Conf_no       *conf_table  = NULL; /* Used in conference.c. */
Per Cederqvist's avatar
Per Cederqvist committed
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


static FILE	*text_file= NULL;
static FILE	*file_a = NULL;	/* Current file. */
static FILE	*file_b = NULL;	/* File under construction. */

/*
 * Four state variables for the background save.
 */
static enum {
    sync_save_conf,
    sync_save_pers,
    sync_save_text,
    sync_error,
    sync_wait,
    sync_ready
} sync_state;

static long sync_next;

static Conf_no highest_conf_no;
static Text_no highest_text_no;

BUGDECL;


/* 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,
	    long size)
{
    Person *p;

    p = alloc_person();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_person(fp, p) != OK )
    {
	free_person(p);
	return NULL;
    }
    else
	return p;
}



static Conference *
read_conference(FILE *fp,
		long pos,
		long size)
{
    Conference *c;

    c = alloc_conference();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_conference(fp, c) != OK )
    {
	free_conference(c);
	return NULL;
    }
    else
	return c;
}
	

static Text_stat *
read_text_stat(FILE *fp,
	       long pos,
	       long size)
{
    Text_stat *t;

    t = alloc_text_stat();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_text_stat(fp, t) != OK )
    {
	free_text_stat(t);
	return NULL;
    }
    else
	return t;
}

static void
167
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
168
{
169
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
170 171 172
}

static void
173
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
174
{
175
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
176 177 178
}

static void
179
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
180
{
181 182
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
183

184 185 186 187
static Cache_node *
get_pers_node(Pers_no pers_no)
{
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
188 189 190
}

static void
191
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
192
{
193
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
194 195
}

196 197 198 199 200
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
201 202

static void
203
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
204
{
205 206
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
207

208 209 210 211 212
static Cache_node *
get_conf_node(Conf_no conf_no)
{
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
213

214 215 216 217
static Cache_node *
get_text_node(Text_no text_no)
{
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
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
}

	
/*
 * 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 )
	restart_kom("cached_change_name(%d, ----): next_free_num==%d",
		    name_num, next_free_num);

    s_clear( &small_conf_arr[name_num]->name );
    s_strcpy( &small_conf_arr[name_num]->name, new_name);
    build_matching_info();
}


extern void
cached_set_conf_type (Conf_no   conf_no,
		      Conf_type type)
{
    if ( conf_no < 1 || conf_no >= next_free_num )
	restart_kom("cached_set_conf_type(%d, ----): next_free_num==%d",
		    conf_no, next_free_num);

    small_conf_arr[ conf_no ]->type = type;
}


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);
261 262 263 264 265

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

Per Cederqvist's avatar
Per Cederqvist committed
266 267 268 269 270 271 272 273 274 275 276 277
    return small_conf_arr [ conf_no ]->type;
}



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

void
mark_person_as_changed(Pers_no	pers_no)
{
278 279 280 281
    Cache_node *node;

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

286
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
287 288 289 290 291 292 293
    pers_set_mru( pers_no );
}


void
mark_conference_as_changed(Conf_no	conf_no)
{
294 295 296 297
    Cache_node *node;

    node = get_conf_node (conf_no);

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

302
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
303 304
    conf_set_mru( conf_no );
    small_conf_arr[ conf_no ]->highest_local_no
305 306
	= (((Conference *)node->ptr)->texts.first_local_no - 1
	   + ((Conference *)node->ptr)->texts.no_of_texts );
Per Cederqvist's avatar
Per Cederqvist committed
307 308 309 310 311
}

void
mark_text_as_changed( Text_no text_no )
{
312 313 314 315
    Cache_node *node;

    node = get_text_node (text_no);

Per Cederqvist's avatar
Per Cederqvist committed
316 317
    TRACE2("Text %d is changed.\n", text_no);
    if ( text_no < 1 || text_no >= next_text_num
318
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
319 320 321 322
    {
	restart_kom("mark_text_as_changed(%d): nonexistent.\n", text_no);
    }

323 324
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
325 326 327 328 329 330 331 332 333 334 335 336
}    



/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
337 338
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
339 340 341 342 343 344 345 346
    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);
    }

347
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
348 349 350 351 352
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

353 354 355 356 357 358 359 360 361
    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");
    
    node->ptr = alloc_person();
    node->s.dirty = 1;
    node->s.exists = 1;
Per Cederqvist's avatar
Per Cederqvist committed
362 363 364 365 366 367 368 369
    pers_set_mru( person );
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
370 371
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
372 373 374 375 376 377 378
    TRACE2("cached_get_person_stat %d\n", person);

    if ( person == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
379 380 381 382 383

    node = get_pers_node (person);

    if ( person >= next_free_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
384 385 386 387 388
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

389
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
390 391
    {
	pers_set_mru( person );
392
	++pers_mcb->hits;
393
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
394 395
    }

396
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
397
    {
398
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
399
	pers_set_mru (person);
400
	++pers_mcb->hits;
401
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
402 403
    }

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

406
    ++pers_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
407
    pers_set_mru (person);
408
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
409 410 411 412 413 414
}


/*
 * Conference-related calls
 */
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


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);
    }
}


static Small_conf *
alloc_small_conf(void)
{
    Small_conf *s;
    s = smalloc(sizeof(Small_conf));
    *s = EMPTY_SMALL_CONF;
    ++no_of_allocated_small_confs;

    return s;
}


Per Cederqvist's avatar
Per Cederqvist committed
443 444 445 446 447
extern Conf_no	/* Also cache the name */
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
448
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
449 450 451 452 453 454 455 456 457 458 459 460 461

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

    if ( next_free_num >= MAX_CONF )
    {
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return 0;
    }
    
    conf_no = next_free_num++;

462 463 464 465 466 467 468 469 470
    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
471 472
    conf_set_mru(conf_no);    

473
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
474 475 476 477 478 479
    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);

480
    TRACE2("Created conference number %d\n", conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
481 482 483 484 485 486 487 488
    
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
489 490
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
491 492 493 494 495
    if ( conf == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
496 497 498 499

    node = get_conf_node (conf);

    if ( conf >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
500 501 502 503 504
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

505 506 507
    if ( node->lock_cnt > 0 )
	log("WNG: cached_delete_conf(%d): lock_cnt === %d\n",
	    conf, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
508

509 510 511
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
512 513 514 515 516 517 518

    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
519 520
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
521 522 523 524 525
    if ( pers == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
526 527 528 529

    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
530 531 532 533 534 535 536
    {
	log("cached_delete_person(): attempt to"
	    " delete non-existing person.\n");
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

537
    if ( node->lock_cnt > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
538
	log("cached_delete_pers(%d): lock_cnt === %d\n",
539
	    pers, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
540

541 542 543
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
544 545 546 547 548 549
    return OK;
}

Success
cached_delete_text(Text_no text)
{
550 551
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
552 553 554 555 556
    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
557 558 559 560 561

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
562 563 564 565 566 567 568
    {
	log("cached_delete_text(): attempt to"
	    " delete non-existing text %d.\n", text);
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

569
    if ( node->lock_cnt > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
570
	log("cached_delete_text(%d): lock_cnt === %d\n",
571
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
572 573


574 575 576
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
577 578 579 580 581 582

    return OK;
}


extern Conference *
583
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
584
{
585 586
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
587 588 589 590 591 592 593
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
594 595 596 597

    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
598 599 600 601 602
    {
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

603
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
604
    {
605
	conf_set_mru (conf_no);
606
	++conf_mcb->hits;
607
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
608 609
    }

610
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
611
    {
612
	node->ptr = copy_conf (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
613
	conf_set_mru (conf_no);
614
	++conf_mcb->hits;
615
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
616 617
    }

618
    node->ptr = read_conference(file_a, node->pos, node->size);
619
    ++conf_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
620 621

    conf_set_mru (conf_no);
622
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
623 624 625 626 627 628 629 630
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
631 632 633 634 635 636 637 638
    Cache_node *node;

    if (conf_no == 0 || conf_no >= next_free_num )
	return FALSE;

    node = get_conf_node (conf_no);

    return node != NULL && node->s.exists != 0;
Per Cederqvist's avatar
Per Cederqvist committed
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
}

    
/*
 * Calls to handle texts
 */

/*
 * +++ Should return Success.
 */
extern String
cached_get_text( Text_no text )
{
    String  the_string;
    Text_stat *t_stat;

    TRACE2("cached_get_text %d\n", text);

    if ( (t_stat = cached_get_text_stat (text)) == NULL )
	return EMPTY_STRING;
    else
    {
	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)
	    != the_string.len )
	{
	    log("WARNING: cached_get_text: "
		"couldn't read enough characters of text %d\n",
		text);
	    return EMPTY_STRING;
	}
		    
	return the_string;
    }
}


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

Per Cederqvist's avatar
Per Cederqvist committed
684 685 686 687 688 689 690 691
    TRACE2("cached_get_text_stat(%d);  next_text_num == ", text);
    TRACE2("%d\n", next_text_num);

    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return NULL;
    }
692 693 694 695

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
696 697 698 699 700 701
    {
	TRACE1("cached_get_text_stat: no such text.\n");
	kom_errno = KOM_NO_SUCH_TEXT;
	return NULL;
    }

702
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
703 704 705
    {
	TRACE1("Found in ptr.\n");
	text_set_mru( text );
706
	++text_mcb->hits;
707
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
708 709
    }

710
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
711 712
    {
	TRACE1("Found in snap_shot\n");
713
	node->ptr = copy_text_stat(node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
714
	text_set_mru (text);
715
	++text_mcb->hits;
716
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
717 718 719
    }

    TRACE1("Found in file A.\n");
720
    node->ptr = read_text_stat(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
721 722

    text_set_mru (text);
723
    ++text_mcb->misses;
724
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
725 726 727 728 729 730 731 732 733 734 735 736 737
}



/*
 * The text is set up with an empty misc-field. The misc field is
 * than initialized by create_text.
 */

extern Text_no
cached_create_text( String message)
{
    Text_no tno;
738
    Cache_node *node;
Per Cederqvist's avatar
Per Cederqvist committed
739 740 741 742 743 744 745 746 747 748 749 750 751

    tno = next_text_num++;

    TRACE2("cached_create_text (len=%d)\n", message.len);

    if ( tno >= MAX_TEXT )
    {
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	next_text_num = MAX_TEXT;
	
	return 0;
    }

752 753
    create_cache_node(text_mcb, tno);
    node = get_text_node (tno);
Per Cederqvist's avatar
Per Cederqvist committed
754

755 756 757 758 759 760 761 762 763 764 765
    if ( node == NULL )
	restart_kom("cached_create_text(): could'nt create cache-node.\n");
    
    node->s.exists = 1;
    node->s.dirty = 1;
    node->ptr = alloc_text_stat();
    ((Text_stat *)node->ptr)->no_of_misc = 0;
    ((Text_stat *)node->ptr)->misc_items = NULL;
    ((Text_stat *)node->ptr)->no_of_marks = 0;
    ((Text_stat *)node->ptr)->no_of_lines = 0;
    ((Text_stat *)node->ptr)->no_of_chars = 0;
Per Cederqvist's avatar
Per Cederqvist committed
766
    fseek(text_file, 0, SEEK_END);
767
    ((Text_stat *)node->ptr)->file_pos = ftell(text_file);
Per Cederqvist's avatar
Per Cederqvist committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

    text_set_mru( tno );

    if ( fwrite(message.string, sizeof(char), message.len, text_file)
	!= message.len ) {
	log("WARNING: cached_create_text: Couldn't write the text %d\n",
	    tno);
    }

    fflush(text_file);
    
    TRACE2("cached_create_text -> %d\n", tno);
    
    return tno;
}


Text_no
traverse_text(Text_no seed)
{
788 789
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
790 791
    seed++;
    
792
    while ( seed < next_text_num )
Per Cederqvist's avatar
Per Cederqvist committed
793
    {
794 795 796 797
	node = get_text_node (seed);
	if ( node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
798 799 800 801 802 803 804 805 806
	seed++;
    }
    
    return (seed >= next_text_num) ? 0 : seed ;
}

Pers_no
traverse_person(Pers_no seed)
{
807 808
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
809 810
    seed++;

811
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
812
    {
813 814 815 816
	node = get_pers_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
817 818 819 820 821 822 823 824 825
	seed++;
    }

    return (seed >= next_free_num) ? 0 : seed ;
}

Conf_no
traverse_conference(Conf_no seed)
{
826 827
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
828 829
    seed++;
    
830
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
831
    {
832 833 834 835
	node = get_conf_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	seed++;
    }

    return (seed >= next_free_num) ? 0 : seed ;
}

extern Garb_nice
cached_get_garb_nice (Conf_no conf_no)
{
    return small_conf_arr [ conf_no ]->nice;
}


extern Local_text_no
cached_get_highest_local_no (Conf_no conf_no)
{
    return small_conf_arr[ conf_no ]->highest_local_no;
}

/* Lock a person struct in memory. Increase a referenc count. */
void
cached_lock_person(Pers_no pers_no)
{
859 860 861 862 863
    Cache_node *node;

    node = get_pers_node(pers_no);
    
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
864 865
	restart_kom("cached_lock_person(%d): nonexistent.\n", pers_no);

866
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
867
    {
868
	Person *pers_stat_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
869

870
	pers_stat_ptr = cached_get_person_stat( pers_no );
Per Cederqvist's avatar
Per Cederqvist committed
871

872
	if ( pers_stat_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
873 874 875
	    restart_kom("cached_lock_person(%d): couldn't read in person.\n",
			pers_no);

876 877 878 879
	if ( pers_stat_ptr != node->ptr )
	    restart_kom("cached_lock_person(%d): pers_stat_ptr == %d, node"
			"->ptr == %d.\n", pers_no, pers_stat_ptr,
			node->ptr);
Per Cederqvist's avatar
Per Cederqvist committed
880 881
    }

882
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
883 884 885 886 887 888
}

/* Decrease reference count. If zero, unlock person. */
void
cached_unlock_person(Pers_no pers_no)
{
889 890 891 892 893 894 895 896
    Cache_node *node;

    node = get_pers_node (pers_no);

    if ( node == NULL )
	restart_kom("cached_unlock_person(): couldn't get cache-node.\n");

    if ( node->lock_cnt <= 0 )
Per Cederqvist's avatar
Per Cederqvist committed
897 898
    {
	log("cached_unlock_person(%d): lock_cnt == %d.\n",
899
	    pers_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
900

901
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
902 903
    }
    else
904
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
905 906 907 908 909 910 911
}


/* Lock a conf struct in memory. Increase a referenc count. */
void
cached_lock_conf(Conf_no conf_no)
{
912 913 914 915 916 917 918 919
    Cache_node *node;

    node = get_conf_node(conf_no);

    if ( node == NULL)
	restart_kom("cached_lock_conf(): can't get cache-node.\n");

    if ( node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
920 921
	restart_kom("cached_lock_conf(%d): nonexistent.\n", conf_no);

922
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
923
    {
924
	Conference *conference_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
925

926
	conference_ptr = cached_get_conf_stat( conf_no );
Per Cederqvist's avatar
Per Cederqvist committed
927

928
	if ( conference_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
929 930 931
	    restart_kom("cached_lock_conf(%d): couldn't read in conf.\n",
			conf_no);

932 933 934 935
	if ( conference_ptr != node->ptr )
	    restart_kom("cached_lock_conf(%d): conference_ptr == %d, node"
			"->ptr == %d.\n", conf_no, conference_ptr,
			node->ptr);
Per Cederqvist's avatar
Per Cederqvist committed
936 937
    }

938
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
939 940 941 942 943 944
}

/* Decrease reference count. If zero, unlock conf. */
void
cached_unlock_conf(Conf_no conf_no)
{
945 946 947 948 949 950 951
    Cache_node *node;

    node = get_conf_node(conf_no);
    if ( node == NULL )
	restart_kom("cached_unlock_conf(): can't get node.\n");

    if ( node->lock_cnt <= 0 )
Per Cederqvist's avatar
Per Cederqvist committed
952 953
    {
	log("cached_unlock_conf(%d): lock_cnt == %d.\n",
954
	    conf_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
955

956
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
957 958
    }
    else
959
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
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
}



/*
 * And here comes some functions to deal with lookup_names.
 */

/* Free the _contents_ of a match_table. The table itself i _not_ freed. */

static void
free_match_table(Matching_info *match)
{
    if ( match == NULL )
	return;
    
    while ( ! s_empty( match->name ))
    {
	free_tokens( match->tokens );
	/* match->name is not freed since it points into conf_arr[]. */

	++match;
    }
}    

extern Success
build_matching_info(void)
{
    Conf_no i;
    Matching_info *match;
    Conf_no	  *conf;

    free_match_table(match_table);
    
    match_table = srealloc(match_table, next_free_num * sizeof(Matching_info));
    conf_table = srealloc(conf_table, next_free_num * sizeof(Conf_no));

    no_of_match_info = 0;

    match = match_table;
    conf = conf_table;
    
    for ( i = 1; i < next_free_num; i++ )
    {
	if ( small_conf_arr[ i ] != NULL
	    && ! s_empty ( small_conf_arr[ i ]->name ) )
	{
	    match->name = small_conf_arr[ i ]->name;
Per Cederqvist's avatar
Per Cederqvist committed
1008
	    match->tokens = tokenize(match->name, s_fcrea_str(WHITESPACE));
Per Cederqvist's avatar
Per Cederqvist committed
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
	    match->priority = 7;
	    *conf = i;
	    ++match;
	    ++conf;
	    ++no_of_match_info;
	}
    }

    match->name = EMPTY_STRING;
    match->tokens = NULL;
    match->priority = 0;
    *conf = 0;
    
    return OK;
}



/* Map conference name to number */
extern Success
cached_lookup_name(const String name,
		   Conf_list_old *result)
{
    Parse_info tmp;
    int i;
    
    tmp = parse(name, match_table, FALSE, FALSE,
Per Cederqvist's avatar
Per Cederqvist committed
1036
		s_fcrea_str(WHITESPACE), DEFAULT_COLLAT_TAB);
Per Cederqvist's avatar
Per Cederqvist committed
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

    if ( tmp.no_of_matches == -1 )
	return FAILURE;
    
    if ( tmp.no_of_matches == 1 && tmp.indexes[ 0 ] == -1 )
    {
	/* Return the entire list. */
	result->no_of_conf_nos = no_of_match_info;
	result->conf_nos = tmp_alloc(no_of_match_info * sizeof(Conf_no));
	result->type_of_conf = tmp_alloc(no_of_match_info * sizeof(Conf_type));

	for ( i = 0; i < no_of_match_info; i++ )
	{
	    result->conf_nos[ i ] = conf_table[ i ];
	    result->type_of_conf[ i ] = small_conf_arr[ conf_table[i] ]->type;
	}
    }
    else
    {
	/* Return the conferences whose conf_nos are in indexes[]. */
	result->no_of_conf_nos = tmp.no_of_matches;
	result->conf_nos = tmp_alloc(tmp.no_of_matches * sizeof(Conf_no));
	result->type_of_conf = tmp_alloc(tmp.no_of_matches
					 * sizeof(Conf_type));

	for ( i = 0; i < tmp.no_of_matches; i++ )
	{
	    result->conf_nos[ i ] = conf_table[ tmp.indexes[ i ] ];
	    result->type_of_conf[ i ]
		= small_conf_arr[ conf_table[ tmp.indexes[ i ] ] ]->type;
	}
    }		
    sfree(tmp.indexes);
    return OK;
}


static Bool
is_clean(const char *fn)
{
    FILE *fp;

    if ( (fp = fopen(fn, "rb")) == NULL )
	return FALSE;

    if ( getc(fp) == 'C' && getc(fp) == 'L' && getc(fp) == 'E'
	&& getc(fp) == 'A' && getc(fp) == 'N' )
    {
	fclose(fp);
	return TRUE;
    }
    else
    {
	fclose(fp);
	return FALSE;
    }
}


extern void
pre_sync(void)
{
    long i;
    Cache_node *node;

    async_sync_db();

    /* Mark up what to save.*/

    BUG(("Sync starting\n"));

    highest_text_no = next_text_num;
    highest_conf_no = next_free_num;

    for ( i = 1; i < highest_conf_no; i++ )
    {
1113
	node = get_conf_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

	if ( node != NULL )
	{
	    node->s.exists_b = node->s.exists;

	    if ( node->s.dirty != 0 && node->s.exists != 0)
	    {
		free_conference(node->snap_shot);

		if ( node->lock_cnt == 0 )
		{
1125
		    unlink_conf_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		    node->snap_shot = node->ptr;
		    node->ptr = NULL;
		}
		else
		{
		    node->snap_shot = copy_conf(node->ptr);
		}
		
		node->s.dirty = 0;
	    }
	}
    }

    for ( i = 1; i < highest_conf_no; i++ )
    {
1141
	node = get_pers_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	if ( node != NULL )
	{
	    node->s.exists_b = node->s.exists;

	    if ( node->s.dirty != 0 && node->s.exists != 0)
	    {
		free_person(node->snap_shot);

		if ( node->lock_cnt == 0 )
		{
1153
		    unlink_pers_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
		    node->snap_shot = node->ptr;
		    node->ptr = NULL;
		}
		else
		{
		    node->snap_shot
			= copy_person(node->ptr);
		}
		
		node->s.dirty = 0;
	    }
	}
    }

    for ( i = 1; i < highest_text_no; i++ )
    {
1170
	node = get_text_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181

	if ( node != NULL )
	{
	    node->s.exists_b = node->s.exists;

	    if ( node->s.dirty != 0 && node->s.exists != 0)
	    {
		free_text_stat(node->snap_shot);

		if ( node->lock_cnt == 0 )
		{
1182
		    unlink_text_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
		    node->snap_shot = node->ptr;
		    node->ptr = NULL;
		}
		else
		{
		    node->snap_shot
			= copy_text_stat(node->ptr);
		}
		
		node->s.dirty = 0;
	    }
	}
    }

    /* All marking is done. Now open file B. */
    
    if ( is_clean(datafilename) )
    {
	if ( rename(datafilename, backupfilename) != 0 )
	    restart_kom("pre_sync: can't backup.\n");
    }
    else
	log("pre_sync: datafile not clean. No backup taken.\n");
1206 1207 1208 1209 1210 1211

    if ( file_b != NULL )
    {
	log("pre_sync: Save in progress aborted.\n");
	fclose(file_b);
    }
Per Cederqvist's avatar
Per Cederqvist committed
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

    if ( (file_b=fopen(datafilename, "wb") ) == NULL )
    {
	log("WARNING: pre_sync: can't open file to save in.\n");
	sync_state = sync_wait;

	return;
    }

    fprintf(file_b, "DIRTY\n");		 /* DIRTY-FLAG */

    fprintf(file_b, "%d\n", highest_conf_no);	  /* NEXT_FREE_NUM */
    sync_state = sync_save_conf;
    sync_next = 1;
}

static void
copy_file(FILE *from,
	  FILE *to,
	  long from_pos,
	  long len)
{
    char *buf;
    long result;

    buf = smalloc(len);
    if ( fseek(from, from_pos, SEEK_SET) == -1 )
    {
	sync_state = sync_error;
	log("sync: copy_file(): fseek failed.\n");
1242
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	return;
    }
    
    if ( (result = fread(buf, 1, len, from)) != len )
    {
	log("sync: copy_file(): fread failed.\n"
	    "from_pos = %d, len = %d, result = %d\n",
	    from_pos, len, result);

	sync_state = sync_error;
1253
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1254 1255 1256 1257 1258 1259 1260
	return;
    }

    if ( fseek(to, 0, SEEK_END) == -1 )
    {
	sync_state = sync_error;
	log("sync: copy_file(): second fseek failed.\n");
1261
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1262 1263 1264 1265 1266 1267 1268
	return;
    }

    if ( fwrite(buf, 1, len, to) != len )
    {
	sync_state = sync_error;
	log("sync: copy_file(): fwrite failed.\n");
1269
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	return;
    }

    sfree(buf);
}    

static void
save_one_conf(void)
{
    Cache_node *cn;
    
    if (sync_next < highest_conf_no)
    {
1283
	cn = get_conf_node (sync_next);
Per Cederqvist's avatar
Per Cederqvist committed
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297