cPickleCache.c 17.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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
/*****************************************************************************
  
  Zope Public License (ZPL) Version 1.0
  -------------------------------------
  
  Copyright (c) Digital Creations.  All rights reserved.
  
  This license has been certified as Open Source(tm).
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  
  1. Redistributions in source code must retain the above copyright
     notice, this list of conditions, and the following disclaimer.
  
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions, and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
  
  3. Digital Creations requests that attribution be given to Zope
     in any manner possible. Zope includes a "Powered by Zope"
     button that is installed by default. While it is not a license
     violation to remove this button, it is requested that the
     attribution remain. A significant investment has been put
     into Zope, and this effort will continue if the Zope community
     continues to grow. This is one way to assure that growth.
  
  4. All advertising materials and documentation mentioning
     features derived from or use of this software must display
     the following acknowledgement:
  
       "This product includes software developed by Digital Creations
       for use in the Z Object Publishing Environment
       (http://www.zope.org/)."
  
     In the event that the product being advertised includes an
     intact Zope distribution (with copyright and license included)
     then this clause is waived.
  
  5. Names associated with Zope or Digital Creations must not be used to
     endorse or promote products derived from this software without
     prior written permission from Digital Creations.
  
  6. Modified redistributions of any form whatsoever must retain
     the following acknowledgment:
  
       "This product includes software developed by Digital Creations
       for use in the Z Object Publishing Environment
       (http://www.zope.org/)."
  
     Intact (re-)distributions of any official Zope release do not
     require an external acknowledgement.
  
  7. Modifications are encouraged but must be packaged separately as
     patches to official Zope releases.  Distributions that do not
     clearly separate the patches from the original work must be clearly
     labeled as unofficial distributions.  Modifications which do not
     carry the name Zope may be packaged in any form, as long as they
     conform to all of the clauses above.
  
  
  Disclaimer
  
    THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
    EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    SUCH DAMAGE.
  
  
  This software consists of contributions made by Digital Creations and
  many individuals on behalf of Digital Creations.  Specific
  attributions are listed in the accompanying credits file.
  
 ****************************************************************************/
85
static char *what_string = "$Id: cPickleCache.c,v 1.27 1999/07/13 21:14:58 jim Exp $";
Jim Fulton's avatar
Jim Fulton committed
86 87 88 89

#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
#define UNLESS(E) if(!(E))
#define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
90
#define OBJECT(O) ((PyObject*)O)
Jim Fulton's avatar
Jim Fulton committed
91

92
#include "cPersistence.h"
Jim Fulton's avatar
Jim Fulton committed
93
#include <time.h>
Jim Fulton's avatar
Jim Fulton committed
94

95 96
#undef Py_FindMethod

97
static PyObject *py_reload, *py__p_jar, *py__p_changed;
98

Jim Fulton's avatar
Jim Fulton committed
99 100 101
typedef struct {
  PyObject_HEAD
  PyObject *data;
Jim Fulton's avatar
Jim Fulton committed
102
  PyObject *jar;
103
  PyObject *setklassstate;
Jim Fulton's avatar
Jim Fulton committed
104 105
  int position;
  int cache_size;
106
  int cache_age;
107 108 109 110 111 112 113 114 115 116
  /* Cache statistics */
  int sum_deal;
  int sum_deac;
  double sum_age;
  int n, na;
  time_t last_check;		/* Time of last gc */
  double mean_age;
  double mean_deal;
  double mean_deac;
  double df, dfa;			/* Degees of freedom for above stats */
Jim Fulton's avatar
Jim Fulton committed
117 118
} ccobject;

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#define WEIGHTING_PERIOD 600

/*
  How to compute weighted means?

  Assume we have two means, a current mean, M, and a mean as of some
  time d seconds in the past, Md.  The means have effective degrees
  of freedom, N, and Nd. Where Nd is adjusted by d is some fashion.
  The combined mean is (M*N+Md*Nd)/(N+Nd).  The degrees of freedom
  of the combined mean, Nc, is N+Nd.  Nd is computed by weighting
  an old degree of freedom with the weight: I/(I+d), where I is some
  suitably chosen constant, which we will call a "weighting period".
  
 */

Jim Fulton's avatar
Jim Fulton committed
134 135 136 137
staticforward PyTypeObject Cctype;

/* ---------------------------------------------------------------- */

138
static int 
Jim Fulton's avatar
Jim Fulton committed
139
gc_item(ccobject *self, PyObject *key, PyObject *v, long now, int dt)
140 141
{

Jim Fulton's avatar
Jim Fulton committed
142
  if (v && key)
143
    {
144
      self->n++;
Jim Fulton's avatar
Jim Fulton committed
145
      if(v->ob_refcnt <= 1)
146
	{
Jim Fulton's avatar
Jim Fulton committed
147 148 149 150
	  self->sum_deal++;
	  return PyDict_DelItem(self->data, key);
	}

Jim Fulton's avatar
Jim Fulton committed
151 152 153 154 155
      if (dt && 
	  (! PyExtensionClass_Check(v)) &&
	  ((cPersistentObject*)v)->jar==self->jar /* I'm paranoid */ &&
	  ((cPersistentObject*)v)->state==cPersistent_UPTODATE_STATE
	  )
Jim Fulton's avatar
Jim Fulton committed
156 157 158 159 160 161
	{
	  now -= ((cPersistentObject*)v)->atime;
	  if (now < 0) now += 65536;
	  self->na++;
	  self->sum_age += now;
	  if (now > dt)
162
	    {
Jim Fulton's avatar
Jim Fulton committed
163 164 165 166 167
	      /* We have a cPersistent object that hasn't been used in
		 a while.  Reinitialize it, hopefully freeing it's
		 state.
	      */
	      self->sum_deac++;
168 169
	      if (PyObject_SetAttr(v,py__p_changed,Py_None) < 0)
		PyErr_Clear();
170 171
	    }
	}
172 173 174
    }
  return 0;
}
Jim Fulton's avatar
Jim Fulton committed
175

176 177 178 179 180 181 182 183 184 185 186 187
static void
update_stats(ccobject *self, time_t now)
{
  double d, deal, deac;

  d=now-self->last_check;
  if(d < 1) return;

  self->df  *= WEIGHTING_PERIOD/(WEIGHTING_PERIOD+d);
  self->dfa *= WEIGHTING_PERIOD/(WEIGHTING_PERIOD+d);

  self->mean_age=((self->mean_age*self->dfa+self->sum_age)/
Jim Fulton's avatar
Jim Fulton committed
188
		  (self->dfa+self->na))*3;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  self->sum_age=0;

  deac=self->sum_deac/d;
  self->sum_deac=0;
  self->mean_deac=((self->mean_deac*self->dfa+deac)/
		   (self->dfa+self->na));
  self->sum_deac=0;

  self->dfa += self->na;
  self->na=0;

  deal=self->sum_deal/d;
  self->sum_deal=0;
  self->mean_deal=((self->mean_deal*self->df +deal)/
		   (self->df +self->n));
  self->sum_deal=0;

  self->df += self->n;
  self->n=0;

  self->last_check=now;
}

Jim Fulton's avatar
Jim Fulton committed
212
static int
213
fullgc(ccobject *self, int idt)
Jim Fulton's avatar
Jim Fulton committed
214 215
{
  PyObject *key, *v;
Jim Fulton's avatar
Jim Fulton committed
216 217 218 219
  int i, dt;
  long now;

  if (self->cache_size < 1) return 0;
220
  if ((i=PyDict_Size(self->data)) < 1) return 0;
Jim Fulton's avatar
Jim Fulton committed
221 222 223 224 225 226 227 228 229 230 231

  now=((long)(time(NULL)/3))%65536;

  if (idt) dt=idt*3;
  else
    {
      i=PyDict_Size(self->data)-3/self->cache_size;
      if(i < 3) i=3;
      dt=self->cache_age*3/i;
      if(dt < 10) dt=10;
    }
Jim Fulton's avatar
Jim Fulton committed
232 233

  for(i=0; PyDict_Next(self->data, &i, &key, &v); )
234
    if(gc_item(self,key,v,now,dt) < 0) return -1;
Jim Fulton's avatar
Jim Fulton committed
235
  self->position=0;
236 237 238

  if(now-self->last_check > 1) update_stats(self, now);
  
Jim Fulton's avatar
Jim Fulton committed
239 240 241
  return 0;
}

Jim Fulton's avatar
Jim Fulton committed
242
static int
243
reallyfullgc(ccobject *self, int dt)
Jim Fulton's avatar
Jim Fulton committed
244 245
{
  PyObject *key, *v;
246
  int i, l, last;
247
  time_t now;
248 249

  if((last=PyDict_Size(self->data)) < 0) return -1;
Jim Fulton's avatar
Jim Fulton committed
250

251
  now=time(NULL);
Jim Fulton's avatar
Jim Fulton committed
252 253
  /* First time through should get refcounts to 1 */
  for(i=0; PyDict_Next(self->data, &i, &key, &v); )
254
    if(gc_item(self,key,v,now,dt) < 0) return -1;
Jim Fulton's avatar
Jim Fulton committed
255

256 257 258 259
  if((l=PyDict_Size(self->data)) < 0) return -1;
  while(l < last)
    {
      for(i=0; PyDict_Next(self->data, &i, &key, &v); )
260
	if(gc_item(self,key,v,now,dt) < 0) return -1;
261 262 263 264
      last=l;
      if((l=PyDict_Size(self->data)) < 0) return -1;
    }

265 266
  if(now-self->last_check > 1) update_stats(self, now);

Jim Fulton's avatar
Jim Fulton committed
267 268 269 270
  self->position=0;
  return 0;
}

Jim Fulton's avatar
Jim Fulton committed
271
static int
272
maybegc(ccobject *self, PyObject *thisv)
Jim Fulton's avatar
Jim Fulton committed
273
{
Jim Fulton's avatar
Jim Fulton committed
274 275
  int n, s, size, dt;
  long now;
Jim Fulton's avatar
Jim Fulton committed
276
  PyObject *key=0, *v=0;
Jim Fulton's avatar
Jim Fulton committed
277

Jim Fulton's avatar
Jim Fulton committed
278 279 280 281 282
  if (self->cache_size < 1) return 0;
  s=PyDict_Size(self->data);
  if (s < 1) return s;

  now=((long)(time(NULL)/3))%65536;
283 284 285

  size=self->cache_size;
  self->cache_size=0;
286 287 288

  n=(s-size)/10;

Jim Fulton's avatar
Jim Fulton committed
289 290 291 292 293
  if (n < 3) n=3;
  s=8*size/s;
  if (s > 100) s=100;
  dt=(long)(self->cache_age*(0.2+0.1*s));
  if (dt < 10) dt=10;
294
  
Jim Fulton's avatar
Jim Fulton committed
295
  while (--n >= 0)
Jim Fulton's avatar
Jim Fulton committed
296
    {
Jim Fulton's avatar
Jim Fulton committed
297
      if (PyDict_Next(self->data, &(self->position), &key, &v))
Jim Fulton's avatar
Jim Fulton committed
298
	{
Jim Fulton's avatar
Jim Fulton committed
299
	  if (v != thisv && gc_item(self,key,v,now,dt) < 0)
300 301 302 303
	    {
	      self->cache_size=size;
	      return -1;
	    }
Jim Fulton's avatar
Jim Fulton committed
304 305 306 307
	}
      else
	self->position=0;
    }
308
  self->cache_size=size;
309

Jim Fulton's avatar
Jim Fulton committed
310
  if (now-self->last_check > 1) update_stats(self, now);
311

Jim Fulton's avatar
Jim Fulton committed
312 313 314 315
  return 0;
}

static PyObject *
316
cc_full_sweep(ccobject *self, PyObject *args)
Jim Fulton's avatar
Jim Fulton committed
317
{
318 319 320
  int dt=0;
  UNLESS(PyArg_ParseTuple(args, "|i", &dt)) return NULL;
  UNLESS(-1 != fullgc(self,dt)) return NULL;
Jim Fulton's avatar
Jim Fulton committed
321 322 323 324
  Py_INCREF(Py_None);
  return Py_None;
}

Jim Fulton's avatar
Jim Fulton committed
325 326 327
static PyObject *
cc_reallyfull_sweep(ccobject *self, PyObject *args)
{
328 329 330
  int dt=0;
  UNLESS(PyArg_ParseTuple(args, "|i", &dt)) return NULL;
  UNLESS(-1 != reallyfullgc(self,dt)) return NULL;
Jim Fulton's avatar
Jim Fulton committed
331 332 333 334
  Py_INCREF(Py_None);
  return Py_None;
}

335 336 337 338 339 340 341 342
static PyObject *
cc_incrgc(ccobject *self, PyObject *args)
{
  if(maybegc(self,NULL) < 0) return NULL;
  Py_INCREF(Py_None);
  return Py_None;
}

343 344 345 346 347 348 349
static void 
_invalidate(ccobject *self, PyObject *key)
{
  PyObject *v;

  if ((v=PyDict_GetItem(self->data, key)))
    {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
      if (PyExtensionClass_Check(v))
	if(v->ob_refcnt <= 1)
	  {
	    self->sum_deal++;
	    if (PyDict_DelItem(self->data, key) < 0) 
	      PyErr_Clear();
	  }
	else
	  {
	    v=PyObject_CallFunction(self->setklassstate,
				    "O", v);
	    if (v) Py_DECREF(v);
	    else PyErr_Clear();
	  }
      else if (PyObject_DelAttr(v,py__p_changed) < 0)
365 366 367 368 369 370 371 372 373
	PyErr_Clear();
    }
  else PyErr_Clear();
}

static PyObject *
cc_invalidate(ccobject *self, PyObject *args)
{
  PyObject *inv, *key, *v;
374
  int i;
375
  
376
  if (PyArg_ParseTuple(args, "O!", &PyDict_Type, &inv)) {
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    for (i=0; PyDict_Next(inv, &i, &key, &v); ) 
      if (key==Py_None)
	{ /* Eek some nitwit invalidated everything! */
	  for (i=0; PyDict_Next(self->data, &i, &key, &v); )
	    _invalidate(self, key);
	  break;
	}
      else
	_invalidate(self, key);
    PyDict_Clear(inv);
  }
  else {
    PyErr_Clear();
    UNLESS (PyArg_ParseTuple(args, "O", &inv)) return NULL;
    if (PyString_Check(inv))
      _invalidate(self, key);
    else if (inv==Py_None)	/* All */
      for (i=0; PyDict_Next(self->data, &i, &key, &v); )
	_invalidate(self, key);
    else {
      int l;

      PyErr_Clear();
      if ((l=PyObject_Length(inv)) < 0) return NULL;
      for(i=l; --i >= 0; )
	{
	  UNLESS (key=PySequence_GetItem(inv, i)) return NULL;
	  _invalidate(self, key);
	  Py_DECREF(key);
	}
      PySequence_DelSlice(inv, 0, l);
    }
  }

  Py_INCREF(Py_None);
  return Py_None;
}
  
Jim Fulton's avatar
Jim Fulton committed
415 416 417 418 419 420 421 422
  
static PyObject *
cc_get(ccobject *self, PyObject *args)
{
  PyObject *r, *key, *d=0;

  UNLESS (PyArg_ParseTuple(args,"O|O", &key, &d)) return NULL;

Jim Fulton's avatar
Jim Fulton committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436
  UNLESS (r=PyDict_GetItem(self->data, key))
    {
      if (d) 
	{
	  PyErr_Clear();
	  r=d;
	}
      else
	{
	  PyErr_SetObject(PyExc_KeyError, key);
	  return NULL;
	}
    }

Jim Fulton's avatar
Jim Fulton committed
437 438 439 440 441 442
  if (maybegc(self,r) < 0) return NULL;

  Py_INCREF(r);
  return r;
}

443

Jim Fulton's avatar
Jim Fulton committed
444
static struct PyMethodDef cc_methods[] = {
Jim Fulton's avatar
Jim Fulton committed
445
  {"full_sweep", (PyCFunction)cc_full_sweep, METH_VARARGS,
446 447 448 449 450
   "full_sweep([age]) -- Perform a full sweep of the cache\n\n"
   "Make a single pass through the cache, removing any objects that are no\n"
   "longer referenced, and deactivating objects that have not been\n"
   "accessed in the number of seconds given by 'age'.  "
   "'age defaults to the cache age.\n"
451
   },
Jim Fulton's avatar
Jim Fulton committed
452
  {"minimize",	(PyCFunction)cc_reallyfull_sweep, METH_VARARGS,
453 454 455 456 457
   "minimize([age]) -- Remove as many objects as possible\n\n"
   "Make multiple passes through the cache, removing any objects that are no\n"
   "longer referenced, and deactivating objects that have not been\n"
   "accessed in the number of seconds given by 'age'.  'age defaults to 0.\n"
   },
Jim Fulton's avatar
Jim Fulton committed
458
  {"incrgc", (PyCFunction)cc_incrgc, METH_VARARGS,
459
   "incrgc() -- Perform incremental garbage collection"},
460 461
  {"invalidate", (PyCFunction)cc_invalidate, METH_VARARGS,
   "invalidate(oids) -- invalidate one, many, or all ids"},
Jim Fulton's avatar
Jim Fulton committed
462 463
  {"get", (PyCFunction)cc_get, METH_VARARGS,
   "get(key [, default]) -- get an item, or a default"},
Jim Fulton's avatar
Jim Fulton committed
464 465 466 467
  {NULL,		NULL}		/* sentinel */
};

static ccobject *
Jim Fulton's avatar
Jim Fulton committed
468
newccobject(PyObject *jar, int cache_size, int cache_age)
Jim Fulton's avatar
Jim Fulton committed
469 470 471 472
{
  ccobject *self;
  
  UNLESS(self = PyObject_NEW(ccobject, &Cctype)) return NULL;
473
  self->setklassstate=self->jar=NULL;
Jim Fulton's avatar
Jim Fulton committed
474 475
  if(self->data=PyDict_New())
    {
Jim Fulton's avatar
Jim Fulton committed
476 477
      self->jar=jar; 
      Py_INCREF(jar);
478 479
      UNLESS (self->setklassstate=PyObject_GetAttrString(jar, "setklassstate"))
	return NULL;
Jim Fulton's avatar
Jim Fulton committed
480
      self->position=0;
481 482
      self->cache_size=cache_size;
      self->cache_age=cache_age < 1 ? 1 : cache_age;
483 484 485 486 487 488 489 490 491 492 493
      self->sum_deal=0;
      self->sum_deac=0;
      self->sum_age=0;
      self->mean_deal=0;
      self->mean_deac=0;
      self->mean_age=0;
      self->df=1;
      self->dfa=1;
      self->n=0;
      self->na=0;
      self->last_check=time(NULL);
Jim Fulton's avatar
Jim Fulton committed
494 495 496 497 498 499 500
      return self;
    }
  Py_DECREF(self);
  return NULL;
}

static void
501
cc_dealloc(ccobject *self)
Jim Fulton's avatar
Jim Fulton committed
502 503
{
  Py_XDECREF(self->data);
Jim Fulton's avatar
Jim Fulton committed
504
  Py_XDECREF(self->jar);
505
  Py_XDECREF(self->setklassstate);
Jim Fulton's avatar
Jim Fulton committed
506 507 508 509
  PyMem_DEL(self);
}

static PyObject *
510
cc_getattr(ccobject *self, char *name)
Jim Fulton's avatar
Jim Fulton committed
511 512
{
  PyObject *r;
513 514 515 516 517 518 519

  if(*name=='c')
    {
      if(strcmp(name,"cache_age")==0)
	return PyInt_FromLong(self->cache_age);
      if(strcmp(name,"cache_size")==0)
	return PyInt_FromLong(self->cache_size);
520 521 522 523 524 525 526 527 528 529 530 531
      if(strcmp(name,"cache_mean_age")==0)
	return PyFloat_FromDouble(self->mean_age);
      if(strcmp(name,"cache_mean_deal")==0)
	return PyFloat_FromDouble(self->mean_deal);
      if(strcmp(name,"cache_mean_deac")==0)
	return PyFloat_FromDouble(self->mean_deac);
      if(strcmp(name,"cache_df")==0)
	return PyFloat_FromDouble(self->df);
      if(strcmp(name,"cache_dfa")==0)
	return PyFloat_FromDouble(self->dfa);
      if(strcmp(name,"cache_last_gc_time")==0)
	return PyFloat_FromDouble(self->last_check);
532 533 534 535 536
      if(strcmp(name,"cache_data")==0)
	{
	  Py_INCREF(self->data);
	  return self->data;
	}
537
    }
Jim Fulton's avatar
Jim Fulton committed
538 539 540 541 542
  if(
     *name=='h' && strcmp(name, "has_key")==0 ||
     *name=='i' && strcmp(name, "items")==0 ||
     *name=='k' && strcmp(name, "keys")==0
     )
Jim Fulton's avatar
Jim Fulton committed
543
    return PyObject_GetAttrString(self->data, name);
544

Jim Fulton's avatar
Jim Fulton committed
545 546 547 548 549 550
  if(r=Py_FindMethod(cc_methods, (PyObject *)self, name))
    return r;
  PyErr_Clear();
  return PyObject_GetAttrString(self->data, name);
}

Jim Fulton's avatar
Jim Fulton committed
551 552 553 554 555 556 557 558 559
static int
cc_setattr(ccobject *self, char *name, PyObject *value)
{
  if(value)
    {
      int v;

      if(strcmp(name,"cache_age")==0)
	{
560
	  UNLESS(PyArg_Parse(value,"i",&v)) return -1;
Jim Fulton's avatar
Jim Fulton committed
561
	  if(v > 0)self->cache_age=v;
562
	  return 0;
Jim Fulton's avatar
Jim Fulton committed
563 564 565 566
	}

      if(strcmp(name,"cache_size")==0)
	{
567
	  UNLESS(PyArg_Parse(value,"i",&v)) return -1;
568
	  self->cache_size=v;
569
	  return 0;
Jim Fulton's avatar
Jim Fulton committed
570 571 572 573 574 575
	}
    }
  PyErr_SetString(PyExc_AttributeError, name);
  return -1;
}

Jim Fulton's avatar
Jim Fulton committed
576
static int
577
cc_length(ccobject *self)
Jim Fulton's avatar
Jim Fulton committed
578 579 580 581 582
{
  return PyObject_Length(self->data);
}
  
static PyObject *
583
cc_subscript(ccobject *self, PyObject *key)
Jim Fulton's avatar
Jim Fulton committed
584 585 586
{
  PyObject *r;

Jim Fulton's avatar
Jim Fulton committed
587
  UNLESS (r=PyDict_GetItem(self->data, key))
Jim Fulton's avatar
Jim Fulton committed
588 589 590 591
  {
    PyErr_SetObject(PyExc_KeyError, key);
    return NULL;
  }
Jim Fulton's avatar
Jim Fulton committed
592 593 594
  if (maybegc(self,r) < 0) return NULL;

  Py_INCREF(r);
Jim Fulton's avatar
Jim Fulton committed
595 596 597 598
  return r;
}

static int
599
cc_ass_sub(ccobject *self, PyObject *key, PyObject *v)
Jim Fulton's avatar
Jim Fulton committed
600
{
Jim Fulton's avatar
Jim Fulton committed
601 602
  if(v) 
    {
603 604 605 606 607 608 609 610
      if (PyExtensionClass_Check(v) 
	  ||
	  (PyExtensionInstance_Check(v) 
	   &&
	   (((PyExtensionClass*)(v->ob_type))->class_flags 
	    & PERSISTENT_TYPE_FLAG)
	   &&
	   (v->ob_type->tp_basicsize >= sizeof(cPersistentObject))
Jim Fulton's avatar
Jim Fulton committed
611 612 613 614 615 616 617 618
	   )
	  )	  
	return PyDict_SetItem(self->data, key, v);

      PyErr_SetString(PyExc_ValueError,
		      "Cache values must be persistent objects or classes.");
      return -1;
    }
Jim Fulton's avatar
Jim Fulton committed
619
  return PyDict_DelItem(self->data, key);
Jim Fulton's avatar
Jim Fulton committed
620 621 622 623 624 625 626 627 628
}

static PyMappingMethods cc_as_mapping = {
  (inquiry)cc_length,		/*mp_length*/
  (binaryfunc)cc_subscript,	/*mp_subscript*/
  (objobjargproc)cc_ass_sub,	/*mp_ass_subscript*/
};

static PyTypeObject Cctype = {
629
  PyObject_HEAD_INIT(NULL)
Jim Fulton's avatar
Jim Fulton committed
630 631 632 633 634 635 636 637
  0,				/*ob_size*/
  "cPickleCache",		/*tp_name*/
  sizeof(ccobject),		/*tp_basicsize*/
  0,				/*tp_itemsize*/
  /* methods */
  (destructor)cc_dealloc,	/*tp_dealloc*/
  (printfunc)0,			/*tp_print*/
  (getattrfunc)cc_getattr,	/*tp_getattr*/
Jim Fulton's avatar
Jim Fulton committed
638
  (setattrfunc)cc_setattr,	/*tp_setattr*/
Jim Fulton's avatar
Jim Fulton committed
639
  (cmpfunc)0,			/*tp_compare*/
Jim Fulton's avatar
Jim Fulton committed
640
  (reprfunc)0,   		/*tp_repr*/
Jim Fulton's avatar
Jim Fulton committed
641 642 643 644 645
  0,				/*tp_as_number*/
  0,				/*tp_as_sequence*/
  &cc_as_mapping,		/*tp_as_mapping*/
  (hashfunc)0,			/*tp_hash*/
  (ternaryfunc)0,		/*tp_call*/
Jim Fulton's avatar
Jim Fulton committed
646
  (reprfunc)0,  		/*tp_str*/
Jim Fulton's avatar
Jim Fulton committed
647 648 649

  /* Space for future expansion */
  0L,0L,0L,0L,
Jim Fulton's avatar
Jim Fulton committed
650
  ""
Jim Fulton's avatar
Jim Fulton committed
651 652 653
};

static PyObject *
654
cCM_new(PyObject *self, PyObject *args)
Jim Fulton's avatar
Jim Fulton committed
655
{
656
  int cache_size=100, cache_age=1000;
Jim Fulton's avatar
Jim Fulton committed
657 658 659 660
  PyObject *jar;

  UNLESS(PyArg_ParseTuple(args, "O|ii", &jar, &cache_size, &cache_age)) return NULL;
  return (PyObject*)newccobject(jar, cache_size,cache_age);
Jim Fulton's avatar
Jim Fulton committed
661 662 663
}

static struct PyMethodDef cCM_methods[] = {
Jim Fulton's avatar
Jim Fulton committed
664
  {"PickleCache",(PyCFunction)cCM_new,	METH_VARARGS, ""},
Jim Fulton's avatar
Jim Fulton committed
665 666 667 668 669 670 671
  {NULL,		NULL}		/* sentinel */
};

void
initcPickleCache()
{
  PyObject *m, *d;
672
  char *rev="$Revision: 1.27 $";
673 674 675

  Cctype.ob_type=&PyType_Type;

Jim Fulton's avatar
Jim Fulton committed
676 677
  UNLESS(ExtensionClassImported) return;

Jim Fulton's avatar
Jim Fulton committed
678
  m = Py_InitModule4("cPickleCache", cCM_methods, "",
Jim Fulton's avatar
Jim Fulton committed
679 680 681
		     (PyObject*)NULL,PYTHON_API_VERSION);

  d = PyModule_GetDict(m);
682 683 684

  py_reload=PyString_FromString("reload");
  py__p_jar=PyString_FromString("_p_jar");
685
  py__p_changed=PyString_FromString("_p_changed");
686

Jim Fulton's avatar
Jim Fulton committed
687 688
  PyDict_SetItemString(d,"__version__",
		       PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
689
  
Jim Fulton's avatar
Jim Fulton committed
690 691
  if (PyErr_Occurred()) Py_FatalError("can't initialize module cCache");
}