cache.el 11.1 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;;;
;;;;; $Id$
;;;;; 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
31
32
33
34
35
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: cache.el
;;;;
;;;; This file contains all functions which have to do with
;;;; caching various data types in the LysKOM client.
;;;;
;;;;


36
37
38
39
40
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))


Per Cederqvist's avatar
.    
Per Cederqvist committed
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

;;; ================================================================
;;;                     Conf-stat cache.


(defun cache-get-conf-stat (conf-no)
  "Get conf-stat for conference CONF-NO, or nil if nothing is cached."
  (cache-assoc conf-no lyskom-conf-cache))

  
(defun cache-add-conf-stat (conf-stat)
  "Insert a CONF-STAT in the cache."
  (cache-add (conf-stat->conf-no conf-stat) conf-stat 'lyskom-conf-cache))


(defun cache-del-conf-stat (conf-no)
  "Delete a conf-stat from the cache. Args: CONF-NO."
  (cache-del conf-no 'lyskom-conf-cache))



;;; ================================================================
;;;                       Pers-stat cache.


(defun cache-get-pers-stat (pers-no)
  "Get pers-stat for person PERS-NO, or nil if nothing is cached."
  (cache-assoc pers-no lyskom-pers-cache))

  
(defun cache-add-pers-stat (pers-stat)
  "Insert a PERS-STAT in the cache."
  (cache-add (pers-stat->pers-no pers-stat) pers-stat 'lyskom-pers-cache))


(defun cache-del-pers-stat (pers-no)
  "Delete a pers-stat from the cache. Args: PERS-NO."
  (cache-del pers-no 'lyskom-pers-cache))
  


;;; ================================================================
;;;                       Text-stat cache.


(defun cache-get-text-stat (text-no)
  "Get text-stat for texton TEXT-NO, or nil if nothing is cached."
  (cache-assoc text-no lyskom-text-cache))

  
(defun cache-add-text-stat (text-stat)
  "Insert a TEXT-STAT in the cache."
  (cache-add (text-stat->text-no text-stat) text-stat 'lyskom-text-cache))


(defun cache-del-text-stat (text-no)
  "Delete a text-stat from the cache. Args: TEXT-NO."
  (cache-del text-no 'lyskom-text-cache))


  
;;; ================================================================
;;;                 Text cache (the text strings).


(defun cache-get-text (text-no)
  "Get text for textno TEXT-NO, or nil if nothing is cached.
This is a special case:
The entry could be a text or a textpointers"
  (let ((tx (cache-assoc text-no lyskom-text-mass-cache)))
    (cond
     ((lyskom-text-p tx) tx)
     ((lyskom-textpointers-p tx)
      (lyskom-create-text 
       (textpointers->text-no tx)
       (concat (buffer-substring (textpointers->start-subject tx)
				 (textpointers->end-subject tx))
	       (buffer-substring (textpointers->start-text tx)
				 (textpointers->end-text tx))))))))

  
(defun cache-add-text (text)
  "Insert a TEXT in the cache."
  (cache-add (text->text-no text) text 'lyskom-text-mass-cache))


(defun cache-del-text (text-no)
  "Delete a text from the cache. Args: TEXT-NO."
  (cache-del text-no 'lyskom-text-mass-cache))



;;; ================================================================
;;;                      Marked texts cache.


(defun cache-get-marked-texts ()
  "Return the a list of marks for all marked texts."
  lyskom-marked-text-cache)


(defun cache-set-marked-texts (mark-array)
  "Sets the marks for all marked texts. A mark-list is an array, but we
want a list in the cache, so this remakes it into a list."
  (setq lyskom-marked-text-cache (lyskom-array-to-list mark-array)))


(defun cache-text-is-marked (text-no)
  "Return the mark if the text text-no is marked by the current user,
otherwise return nil"
  (let ((marks lyskom-marked-text-cache)
	(mark nil))
    (while (and (not (null marks))
		(null mark))
      (if (equal text-no (mark->text-no (car marks)))
	  (setq mark (car marks))
	(setq marks (cdr marks))))
    mark))


(defun cache-add-marked-text (text-no mark-type)
  "Insert a mark into the cache. If it is already there, replace it."
  (let ((marks lyskom-marked-text-cache)
	(found nil)
	(mark (lyskom-create-mark text-no mark-type)))
    (while (and (not found)
		(not (null marks)))
      (if (and (car marks)
	       (equal text-no (mark->text-no (car marks))))
	  (progn
	    (setcar marks mark)
	    (setq found t))
	(setq marks (cdr marks))))
    (if (not found)
	(setq lyskom-marked-text-cache
	      (cons mark lyskom-marked-text-cache)))))


(defun cache-del-marked-text (text-no)
  "Remove the mark from the cache of marked texts if it is there.
+++BUG: A mark is replaced with a nil and not removed."
  (let ((marks lyskom-marked-text-cache)
	(found nil))
    (while (and (not found)
		(not (null marks)))
      (if (equal text-no (mark->text-no (car marks)))
	  (progn
	    (setcar marks nil)
	    (setq found t))
	(setq marks (cdr marks))))))


;;; ================================================================
;;;                         who-info cache


(defun cache-set-who-info-list (who-info-arr)
  "Sets the cache of who-info items."
  (setq lyskom-who-info-cache (list 'WHO-INFO-LIST))
  (lyskom-save-excursion
Linus Tolke Y's avatar
Linus Tolke Y committed
201
202
   (setq lyskom-who-info-buffer 
	 (get-buffer-create (concat (buffer-name) "-who")))
Per Cederqvist's avatar
.    
Per Cederqvist committed
203
204
205
206
207
208
209
210
211
212
213
   (set-buffer lyskom-who-info-buffer)
   (erase-buffer))
  (mapcar 'cache-add-who-info
	  (sort (lyskom-array-to-list who-info-arr)
		(function (lambda (who1 who2)
			    (< (who-info->connection who1)
			       (who-info->connection who2)))))))


(defun cache-add-who-info (who-info)
  "Adds another entry to the lyskom-who-info-cache. Updating the buffer."
214
215
216
217
218
219
220
221
222
  (if lyskom-who-info-buffer-is-on
      (progn
	(lyskom-collect 'who-buffer)
	(initiate-get-conf-stat
	 'who-buffer nil (who-info->pers-no who-info))
	(initiate-get-conf-stat 
	 'who-buffer nil (who-info->working-conf who-info))
	(lyskom-use 'who-buffer 'lyskom-set-who-info-buffer-2
		    who-info))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
223
224
225
226
227
228
229


(defun cache-add-session-info (session-info)
  "Adds another entry to the lyskom-who-info-cache. Updating the buffer.
ARG: session-info"
  (if (null session-info)
      nil				;+++ Annan felhantering
230
231
    (lyskom-halt 'who-buffer)
    (lyskom-collect 'who-buffer-2)
Per Cederqvist's avatar
.    
Per Cederqvist committed
232
    (initiate-get-conf-stat
233
     'who-buffer-2 nil (session-info->pers-no session-info))
Per Cederqvist's avatar
.    
Per Cederqvist committed
234
    (initiate-get-conf-stat 
235
236
237
238
     'who-buffer-2 nil (session-info->working-conf session-info))
    (lyskom-use 'who-buffer-2 'lyskom-set-session-info
		session-info)
    (lyskom-run 'who-buffer-2 'lyskom-resume 'who-buffer)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
239
240
241
242


(defun cache-del-who-info (session-no)
  "Delete the session SESSION-NO from the lyskom-who-info-cache. Updating buffer."
243
244
245
246
247
248
249
250
251
252
253
254
255
  (if lyskom-who-info-buffer-is-on
      (let ((where (cache-assoc session-no lyskom-who-info-cache)))
	(if where
	    (progn
	      (lyskom-save-excursion
	       (set-buffer lyskom-who-info-buffer)
	       (delete-region (marker-position
			       (who-buffer-info->start-marker where))
			      (marker-position
			       (who-buffer-info->end-marker where))))
	      (set-marker (who-buffer-info->start-marker where) nil)
	      (set-marker (who-buffer-info->end-marker where) nil)
	      (cache-del session-no 'lyskom-who-info-cache))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279


(defun lyskom-set-who-info-buffer-2 (pers-conf-stat conf-conf-stat who-info)
  "Inserts a who-buffer-info into lyskom-who-info-cache"
  ;We can use lyskom-insert (not beautiful)
  ;we insert everything at the end of the buffer.
  (let ((sesno lyskom-session-no)
	min max
	(where (cache-assoc (who-info->connection who-info) 
			    lyskom-who-info-cache)))
    (lyskom-save-excursion
     (set-buffer lyskom-who-info-buffer)
     (save-restriction
       (if where
	   (progn
	     (narrow-to-region (marker-position 
				(who-buffer-info->start-marker where))
			       (1- (marker-position 
				    (who-buffer-info->end-marker where))))
	     (delete-region (point-min) (point-max)))
	 (goto-char (point-max))
	 (insert " ")
	 (narrow-to-region (point-min) (1- (point-max))))
       (setq min (point-max-marker))
280
281
       (lyskom-print-who-info pers-conf-stat conf-conf-stat who-info sesno
			      'insert)
Per Cederqvist's avatar
.    
Per Cederqvist committed
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
       (setq max (point-max-marker))
       (goto-char (point-max)))
     (delete-char 1))
    (cache-add (who-info->connection who-info) 
	       (lyskom-create-who-buffer-info who-info min max)
	       'lyskom-who-info-cache)
    (run-hooks 'lyskom-who-info-has-changed-hook)))


(defun lyskom-set-session-info (pers-conf-stat conf-conf-stat session-info)
  "Inserts a session-info into lyskom-who-info-cache"
  (lyskom-set-who-info-buffer-2 pers-conf-stat conf-conf-stat
				(lyskom-create-who-info
				 (session-info->pers-no session-info)
				 (session-info->working-conf session-info)
				 (session-info->connection session-info)
				 (session-info->doing session-info)
				 (session-info->username session-info))))


;;; ================================================================
;;;                     Generic cache routines

;;; BUGS: The cache is never emptied. +++


(defun cache-assoc (key cache)
  "Get data for item with key KEY from CACHE.
CACHE is an assoc-list in this implementation."
  (cdr-safe (assoc key cache)))


(defun cache-add (__key__ __data__ __cache__)
  "Add DATA to CACHE under the key KEY.
Args: KEY DATA CACHE.
CACHE is a (the only one) quoted variable pointing to the cache (an alist).
The variable might be changed."
  (cond
   ((null (eval __cache__))
    (set __cache__ (list (cons __key__ __data__))))
   (t (let ((oldval (assoc __key__ (eval __cache__))))
	(cond
	 ((null oldval)
	  (set __cache__
	       (cons (cons __key__ __data__)

		     (eval __cache__))))
	 (t
	  (setcdr oldval __data__)))))))


(defun cache-del (__key__ __cache__)
  "Delete item with key KEY from CACHE.
CACHE is the name of the variable that points to the cache."
  (let ((oldval (assoc __key__ (eval __cache__))))
    (if oldval
	(setcdr oldval nil))))		;A pair (key . nil) will remain.
					;Fix this bug someday. +++


;;; ================================================================
;;;         lyskom-tell-server (this is also a sort of cache)


(defvar lyskom-what-i-am-doing nil
  "What the client thinks the server thinks the user is doing.")


(defun lyskom-tell-server (string)
  "Tell the server what the user is doing. Args: STRING."
  (cond
   ((equal string lyskom-what-i-am-doing))
   (t
    (setq lyskom-what-i-am-doing string)
    (initiate-change-what-i-am-doing 'background nil string))))