Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BucketTemplate.c 52KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. /*****************************************************************************
  2. Copyright (c) 2001, 2002 Zope Foundation and Contributors.
  3. All Rights Reserved.
  4. This software is subject to the provisions of the Zope Public License,
  5. Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  7. WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  8. WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  9. FOR A PARTICULAR PURPOSE
  10. ****************************************************************************/
  11. #define BUCKETTEMPLATE_C "$Id$\n"
  12. /* Use BUCKET_SEARCH to find the index at which a key belongs.
  13. * INDEX An int lvalue to hold the index i such that KEY belongs at
  14. * SELF->keys[i]. Note that this will equal SELF->len if KEY
  15. * is larger than the bucket's largest key. Else it's the
  16. * smallest i such that SELF->keys[i] >= KEY.
  17. * ABSENT An int lvalue to hold a Boolean result, true (!= 0) if the
  18. * key is absent, false (== 0) if the key is at INDEX.
  19. * SELF A pointer to a Bucket node.
  20. * KEY The key you're looking for, of type KEY_TYPE.
  21. * ONERROR What to do if key comparison raises an exception; for example,
  22. * perhaps 'return NULL'.
  23. *
  24. * See Maintainer.txt for discussion: this is optimized in subtle ways.
  25. * It's recommended that you call this at the start of a routine, waiting
  26. * to check for self->len == 0 after (if an empty bucket is special in
  27. * context; INDEX becomes 0 and ABSENT becomes true if this macro is run
  28. * with an empty SELF, and that may be all the invoker needs to know).
  29. */
  30. #define BUCKET_SEARCH(INDEX, ABSENT, SELF, KEY, ONERROR) { \
  31. int _lo = 0; \
  32. int _hi = (SELF)->len; \
  33. int _i; \
  34. int _cmp = 1; \
  35. for (_i = _hi >> 1; _lo < _hi; _i = (_lo + _hi) >> 1) { \
  36. TEST_KEY_SET_OR(_cmp, (SELF)->keys[_i], (KEY)) \
  37. ONERROR; \
  38. if (_cmp < 0) _lo = _i + 1; \
  39. else if (_cmp == 0) break; \
  40. else _hi = _i; \
  41. } \
  42. (INDEX) = _i; \
  43. (ABSENT) = _cmp; \
  44. }
  45. /*
  46. ** _bucket_get
  47. **
  48. ** Search a bucket for a given key.
  49. **
  50. ** Arguments
  51. ** self The bucket
  52. ** keyarg The key to look for
  53. ** has_key Boolean; if true, return a true/false result; else return
  54. ** the value associated with the key.
  55. **
  56. ** Return
  57. ** If has_key:
  58. ** Returns the Python int 0 if the key is absent, else returns
  59. ** has_key itself as a Python int. A BTree caller generally passes
  60. ** the depth of the bucket for has_key, so a true result returns
  61. ** the bucket depth then.
  62. ** Note that has_key should be true when searching set buckets.
  63. ** If not has_key:
  64. ** If the key is present, returns the associated value, and the
  65. ** caller owns the reference. Else returns NULL and sets KeyError.
  66. ** Whether or not has_key:
  67. ** If a comparison sets an exception, returns NULL.
  68. */
  69. static PyObject *
  70. _bucket_get(Bucket *self, PyObject *keyarg, int has_key)
  71. {
  72. int i, cmp;
  73. KEY_TYPE key;
  74. PyObject *r = NULL;
  75. int copied = 1;
  76. COPY_KEY_FROM_ARG(key, keyarg, copied);
  77. UNLESS (copied) return NULL;
  78. UNLESS (PER_USE(self)) return NULL;
  79. BUCKET_SEARCH(i, cmp, self, key, goto Done);
  80. if (has_key)
  81. r = INT_FROM_LONG(cmp ? 0 : has_key);
  82. else
  83. {
  84. if (cmp == 0)
  85. {
  86. COPY_VALUE_TO_OBJECT(r, self->values[i]);
  87. }
  88. else
  89. PyErr_SetObject(PyExc_KeyError, keyarg);
  90. }
  91. Done:
  92. PER_UNUSE(self);
  93. return r;
  94. }
  95. static PyObject *
  96. bucket_getitem(Bucket *self, PyObject *key)
  97. {
  98. return _bucket_get(self, key, 0);
  99. }
  100. /*
  101. ** Bucket_grow
  102. **
  103. ** Resize a bucket.
  104. **
  105. ** Arguments: self The bucket.
  106. ** newsize The new maximum capacity. If < 0, double the
  107. ** current size unless the bucket is currently empty,
  108. ** in which case use MIN_BUCKET_ALLOC.
  109. ** noval Boolean; if true, allocate only key space and not
  110. ** value space
  111. **
  112. ** Returns: -1 on error, and MemoryError exception is set
  113. ** 0 on success
  114. */
  115. static int
  116. Bucket_grow(Bucket *self, int newsize, int noval)
  117. {
  118. KEY_TYPE *keys;
  119. VALUE_TYPE *values;
  120. if (self->size)
  121. {
  122. if (newsize < 0)
  123. newsize = self->size * 2;
  124. if (newsize < 0) /* int overflow */
  125. goto Overflow;
  126. UNLESS (keys = BTree_Realloc(self->keys, sizeof(KEY_TYPE) * newsize))
  127. return -1;
  128. UNLESS (noval)
  129. {
  130. values = BTree_Realloc(self->values, sizeof(VALUE_TYPE) * newsize);
  131. if (values == NULL)
  132. {
  133. free(keys);
  134. return -1;
  135. }
  136. self->values = values;
  137. }
  138. self->keys = keys;
  139. }
  140. else
  141. {
  142. if (newsize < 0)
  143. newsize = MIN_BUCKET_ALLOC;
  144. UNLESS (self->keys = BTree_Malloc(sizeof(KEY_TYPE) * newsize))
  145. return -1;
  146. UNLESS (noval)
  147. {
  148. self->values = BTree_Malloc(sizeof(VALUE_TYPE) * newsize);
  149. if (self->values == NULL)
  150. {
  151. free(self->keys);
  152. self->keys = NULL;
  153. return -1;
  154. }
  155. }
  156. }
  157. self->size = newsize;
  158. return 0;
  159. Overflow:
  160. PyErr_NoMemory();
  161. return -1;
  162. }
  163. /* So far, bucket_append is called only by multiunion_m(), so is called
  164. * only when MULTI_INT_UNION is defined. Flavors of BTree/Bucket that
  165. * don't support MULTI_INT_UNION don't call bucket_append (yet), and
  166. * gcc complains if bucket_append is compiled in those cases. So only
  167. * compile bucket_append if it's going to be used.
  168. */
  169. #ifdef MULTI_INT_UNION
  170. /*
  171. * Append a slice of the "from" bucket to self.
  172. *
  173. * self Append (at least keys) to this bucket. self must be activated
  174. * upon entry, and remains activated at exit. If copyValues
  175. * is true, self must be empty or already have a non-NULL values
  176. * pointer. self's access and modification times aren't updated.
  177. * from The bucket from which to take keys, and possibly values. from
  178. * must be activated upon entry, and remains activated at exit.
  179. * If copyValues is true, from must have a non-NULL values
  180. * pointer. self and from must not be the same. from's access
  181. * time isn't updated.
  182. * i, n The slice from[i : i+n] is appended to self. Must have
  183. * i >= 0, n > 0 and i+n <= from->len.
  184. * copyValues Boolean. If true, copy values from the slice as well as keys.
  185. * In this case, from must have a non-NULL values pointer, and
  186. * self must too (unless self is empty, in which case a values
  187. * vector will be allocated for it).
  188. * overallocate Boolean. If self doesn't have enough room upon entry to hold
  189. * all the appended stuff, then if overallocate is false exactly
  190. * enough room will be allocated to hold the new stuff, else if
  191. * overallocate is true an excess will be allocated. overallocate
  192. * may be a good idea if you expect to append more stuff to self
  193. * later; else overallocate should be false.
  194. *
  195. * CAUTION: If self is empty upon entry (self->size == 0), and copyValues is
  196. * false, then no space for values will get allocated. This can be a trap if
  197. * the caller intends to copy values itself.
  198. *
  199. * Return
  200. * -1 Error.
  201. * 0 OK.
  202. */
  203. static int
  204. bucket_append(Bucket *self, Bucket *from, int i, int n,
  205. int copyValues, int overallocate)
  206. {
  207. int newlen;
  208. assert(self && from && self != from);
  209. assert(i >= 0);
  210. assert(n > 0);
  211. assert(i+n <= from->len);
  212. /* Make room. */
  213. newlen = self->len + n;
  214. if (newlen > self->size)
  215. {
  216. int newsize = newlen;
  217. if (overallocate) /* boost by 25% -- pretty arbitrary */
  218. newsize += newsize >> 2;
  219. if (Bucket_grow(self, newsize, ! copyValues) < 0)
  220. return -1;
  221. }
  222. assert(newlen <= self->size);
  223. /* Copy stuff. */
  224. memcpy(self->keys + self->len, from->keys + i, n * sizeof(KEY_TYPE));
  225. if (copyValues)
  226. {
  227. assert(self->values);
  228. assert(from->values);
  229. memcpy(self->values + self->len, from->values + i,
  230. n * sizeof(VALUE_TYPE));
  231. }
  232. self->len = newlen;
  233. /* Bump refcounts. */
  234. #ifdef KEY_TYPE_IS_PYOBJECT
  235. {
  236. int j;
  237. PyObject **p = from->keys + i;
  238. for (j = 0; j < n; ++j, ++p)
  239. {
  240. Py_INCREF(*p);
  241. }
  242. }
  243. #endif
  244. #ifdef VALUE_TYPE_IS_PYOBJECT
  245. if (copyValues)
  246. {
  247. int j;
  248. PyObject **p = from->values + i;
  249. for (j = 0; j < n; ++j, ++p)
  250. {
  251. Py_INCREF(*p);
  252. }
  253. }
  254. #endif
  255. return 0;
  256. }
  257. #endif /* MULTI_INT_UNION */
  258. /*
  259. ** _bucket_set: Assign a value to a key in a bucket, delete a key+value
  260. ** pair, or just insert a key.
  261. **
  262. ** Arguments
  263. ** self The bucket
  264. ** keyarg The key to look for
  265. ** v The value to associate with key; NULL means delete the key.
  266. ** If NULL, it's an error (KeyError) if the key isn't present.
  267. ** Note that if this is a set bucket, and you want to insert
  268. ** a new set element, v must be non-NULL although its exact
  269. ** value will be ignored. Passing Py_None is good for this.
  270. ** unique Boolean; when true, don't replace the value if the key is
  271. ** already present.
  272. ** noval Boolean; when true, operate on keys only (ignore values)
  273. ** changed ignored on input
  274. **
  275. ** Return
  276. ** -1 on error
  277. ** 0 on success and the # of bucket entries didn't change
  278. ** 1 on success and the # of bucket entries did change
  279. ** *changed If non-NULL, set to 1 on any mutation of the bucket.
  280. */
  281. static int
  282. _bucket_set(Bucket *self, PyObject *keyarg, PyObject *v,
  283. int unique, int noval, int *changed)
  284. {
  285. int i, cmp;
  286. KEY_TYPE key;
  287. /* Subtle: there may or may not be a value. If there is, we need to
  288. * check its type early, so that in case of error we can get out before
  289. * mutating the bucket. But because value isn't used on all paths, if
  290. * we don't initialize value then gcc gives a nuisance complaint that
  291. * value may be used initialized (it can't be, but gcc doesn't know
  292. * that). So we initialize it. However, VALUE_TYPE can be various types,
  293. * including int, PyObject*, and char[6], so it's a puzzle to spell
  294. * initialization. It so happens that {0} is a valid initializer for all
  295. * these types.
  296. */
  297. VALUE_TYPE value = {0}; /* squash nuisance warning */
  298. int result = -1; /* until proven innocent */
  299. int copied = 1;
  300. COPY_KEY_FROM_ARG(key, keyarg, copied);
  301. UNLESS(copied)
  302. return -1;
  303. #ifdef KEY_CHECK_ON_SET
  304. if (v && !KEY_CHECK_ON_SET(keyarg))
  305. return -1;
  306. #endif
  307. /* Copy the value early (if needed), so that in case of error a
  308. * pile of bucket mutations don't need to be undone.
  309. */
  310. if (v && !noval) {
  311. COPY_VALUE_FROM_ARG(value, v, copied);
  312. UNLESS(copied)
  313. return -1;
  314. }
  315. UNLESS (PER_USE(self))
  316. return -1;
  317. BUCKET_SEARCH(i, cmp, self, key, goto Done);
  318. if (cmp == 0)
  319. {
  320. /* The key exists, at index i. */
  321. if (v)
  322. {
  323. /* The key exists at index i, and there's a new value.
  324. * If unique, we're not supposed to replace it. If noval, or this
  325. * is a set bucket (self->values is NULL), there's nothing to do.
  326. */
  327. if (unique || noval || self->values == NULL)
  328. {
  329. result = 0;
  330. goto Done;
  331. }
  332. /* The key exists at index i, and we need to replace the value. */
  333. #ifdef VALUE_SAME
  334. /* short-circuit if no change */
  335. if (VALUE_SAME(self->values[i], value))
  336. {
  337. result = 0;
  338. goto Done;
  339. }
  340. #endif
  341. if (changed)
  342. *changed = 1;
  343. DECREF_VALUE(self->values[i]);
  344. COPY_VALUE(self->values[i], value);
  345. INCREF_VALUE(self->values[i]);
  346. if (PER_CHANGED(self) >= 0)
  347. result = 0;
  348. goto Done;
  349. }
  350. /* The key exists at index i, and should be deleted. */
  351. DECREF_KEY(self->keys[i]);
  352. self->len--;
  353. if (i < self->len)
  354. memmove(self->keys + i, self->keys + i+1,
  355. sizeof(KEY_TYPE)*(self->len - i));
  356. if (self->values)
  357. {
  358. DECREF_VALUE(self->values[i]);
  359. if (i < self->len)
  360. memmove(self->values + i, self->values + i+1,
  361. sizeof(VALUE_TYPE)*(self->len - i));
  362. }
  363. if (! self->len)
  364. {
  365. self->size = 0;
  366. free(self->keys);
  367. self->keys = NULL;
  368. if (self->values)
  369. {
  370. free(self->values);
  371. self->values = NULL;
  372. }
  373. }
  374. if (changed)
  375. *changed = 1;
  376. if (PER_CHANGED(self) >= 0)
  377. result = 1;
  378. goto Done;
  379. }
  380. /* The key doesn't exist, and belongs at index i. */
  381. if (!v)
  382. {
  383. /* Can't delete a non-existent key. */
  384. PyErr_SetObject(PyExc_KeyError, keyarg);
  385. goto Done;
  386. }
  387. /* The key doesn't exist and should be inserted at index i. */
  388. if (self->len == self->size && Bucket_grow(self, -1, noval) < 0)
  389. goto Done;
  390. if (self->len > i)
  391. {
  392. memmove(self->keys + i + 1, self->keys + i,
  393. sizeof(KEY_TYPE) * (self->len - i));
  394. if (self->values)
  395. {
  396. memmove(self->values + i + 1, self->values + i,
  397. sizeof(VALUE_TYPE) * (self->len - i));
  398. }
  399. }
  400. COPY_KEY(self->keys[i], key);
  401. INCREF_KEY(self->keys[i]);
  402. if (! noval)
  403. {
  404. COPY_VALUE(self->values[i], value);
  405. INCREF_VALUE(self->values[i]);
  406. }
  407. self->len++;
  408. if (changed)
  409. *changed = 1;
  410. if (PER_CHANGED(self) >= 0)
  411. result = 1;
  412. Done:
  413. PER_UNUSE(self);
  414. return result;
  415. }
  416. /*
  417. ** bucket_setitem
  418. **
  419. ** wrapper for _bucket_set (eliminates +1 return code)
  420. **
  421. ** Arguments: self The bucket
  422. ** key The key to insert under
  423. ** v The value to insert
  424. **
  425. ** Returns 0 on success
  426. ** -1 on failure
  427. */
  428. static int
  429. bucket_setitem(Bucket *self, PyObject *key, PyObject *v)
  430. {
  431. if (_bucket_set(self, key, v, 0, 0, 0) < 0)
  432. return -1;
  433. return 0;
  434. }
  435. /**
  436. ** Accepts a sequence of 2-tuples, or any object with an items()
  437. ** method that returns an iterable object producing 2-tuples.
  438. */
  439. static int
  440. update_from_seq(PyObject *map, PyObject *seq)
  441. {
  442. PyObject *iter, *o, *k, *v;
  443. int err = -1;
  444. /* One path creates a new seq object. The other path has an
  445. INCREF of the seq argument. So seq must always be DECREFed on
  446. the way out.
  447. */
  448. /* Use items() if it's not a sequence. Alas, PySequence_Check()
  449. * returns true for a PeristentMapping or PersistentDict, and we
  450. * want to use items() in those cases too.
  451. */
  452. #ifdef PY3K
  453. #define ITERITEMS "items"
  454. #else
  455. #define ITERITEMS "iteritems"
  456. #endif
  457. if (!PySequence_Check(seq) || /* or it "looks like a dict" */
  458. PyObject_HasAttrString(seq, ITERITEMS))
  459. #undef ITERITEMS
  460. {
  461. PyObject *items;
  462. items = PyObject_GetAttrString(seq, "items");
  463. if (items == NULL)
  464. return -1;
  465. seq = PyObject_CallObject(items, NULL);
  466. Py_DECREF(items);
  467. if (seq == NULL)
  468. return -1;
  469. }
  470. else
  471. Py_INCREF(seq);
  472. iter = PyObject_GetIter(seq);
  473. if (iter == NULL)
  474. goto err;
  475. while (1)
  476. {
  477. o = PyIter_Next(iter);
  478. if (o == NULL)
  479. {
  480. if (PyErr_Occurred())
  481. goto err;
  482. else
  483. break;
  484. }
  485. if (!PyTuple_Check(o) || PyTuple_GET_SIZE(o) != 2)
  486. {
  487. Py_DECREF(o);
  488. PyErr_SetString(PyExc_TypeError,
  489. "Sequence must contain 2-item tuples");
  490. goto err;
  491. }
  492. k = PyTuple_GET_ITEM(o, 0);
  493. v = PyTuple_GET_ITEM(o, 1);
  494. if (PyObject_SetItem(map, k, v) < 0)
  495. {
  496. Py_DECREF(o);
  497. goto err;
  498. }
  499. Py_DECREF(o);
  500. }
  501. err = 0;
  502. err:
  503. Py_DECREF(iter);
  504. Py_DECREF(seq);
  505. return err;
  506. }
  507. static PyObject *
  508. Mapping_update(PyObject *self, PyObject *seq)
  509. {
  510. if (update_from_seq(self, seq) < 0)
  511. return NULL;
  512. Py_INCREF(Py_None);
  513. return Py_None;
  514. }
  515. /*
  516. ** bucket_split
  517. **
  518. ** Splits one bucket into two
  519. **
  520. ** Arguments: self The bucket
  521. ** index the index of the key to split at (O.O.B use midpoint)
  522. ** next the new bucket to split into
  523. **
  524. ** Returns: 0 on success
  525. ** -1 on failure
  526. */
  527. static int
  528. bucket_split(Bucket *self, int index, Bucket *next)
  529. {
  530. int next_size;
  531. ASSERT(self->len > 1, "split of empty bucket", -1);
  532. if (index < 0 || index >= self->len)
  533. index = self->len / 2;
  534. next_size = self->len - index;
  535. next->keys = BTree_Malloc(sizeof(KEY_TYPE) * next_size);
  536. if (!next->keys)
  537. return -1;
  538. memcpy(next->keys, self->keys + index, sizeof(KEY_TYPE) * next_size);
  539. if (self->values) {
  540. next->values = BTree_Malloc(sizeof(VALUE_TYPE) * next_size);
  541. if (!next->values) {
  542. free(next->keys);
  543. next->keys = NULL;
  544. return -1;
  545. }
  546. memcpy(next->values, self->values + index,
  547. sizeof(VALUE_TYPE) * next_size);
  548. }
  549. next->size = next_size;
  550. next->len = next_size;
  551. self->len = index;
  552. next->next = self->next;
  553. Py_INCREF(next);
  554. self->next = next;
  555. if (PER_CHANGED(self) < 0)
  556. return -1;
  557. return 0;
  558. }
  559. /* Set self->next to self->next->next, i.e. unlink self's successor from
  560. * the chain.
  561. *
  562. * Return:
  563. * -1 error
  564. * 0 OK
  565. */
  566. static int
  567. Bucket_deleteNextBucket(Bucket *self)
  568. {
  569. int result = -1; /* until proven innocent */
  570. Bucket *successor;
  571. PER_USE_OR_RETURN(self, -1);
  572. successor = self->next;
  573. if (successor)
  574. {
  575. Bucket *next;
  576. /* Before: self -> successor -> next
  577. * After: self --------------> next
  578. */
  579. UNLESS (PER_USE(successor))
  580. goto Done;
  581. next = successor->next;
  582. PER_UNUSE(successor);
  583. Py_XINCREF(next); /* it may be NULL, of course */
  584. self->next = next;
  585. Py_DECREF(successor);
  586. if (PER_CHANGED(self) < 0)
  587. goto Done;
  588. }
  589. result = 0;
  590. Done:
  591. PER_UNUSE(self);
  592. return result;
  593. }
  594. /*
  595. Bucket_findRangeEnd -- Find the index of a range endpoint
  596. (possibly) contained in a bucket.
  597. Arguments: self The bucket
  598. keyarg The key to match against
  599. low Boolean; true for low end of range, false for high
  600. exclude_equal Boolean; if true, don't accept an exact match,
  601. and if there is one then move right if low and
  602. left if !low.
  603. offset The output offset
  604. If low true, *offset <- index of the smallest item >= key,
  605. if low false the index of the largest item <= key. In either case, if there
  606. is no such index, *offset is left alone and 0 is returned.
  607. Return:
  608. 0 No suitable index exists; *offset has not been changed
  609. 1 The correct index was stored into *offset
  610. -1 Error
  611. Example: Suppose the keys are [2, 4], and exclude_equal is false. Searching
  612. for 2 sets *offset to 0 and returns 1 regardless of low. Searching for 4
  613. sets *offset to 1 and returns 1 regardless of low.
  614. Searching for 1:
  615. If low true, sets *offset to 0, returns 1.
  616. If low false, returns 0.
  617. Searching for 3:
  618. If low true, sets *offset to 1, returns 1.
  619. If low false, sets *offset to 0, returns 1.
  620. Searching for 5:
  621. If low true, returns 0.
  622. If low false, sets *offset to 1, returns 1.
  623. The 1, 3 and 5 examples are the same when exclude_equal is true.
  624. */
  625. static int
  626. Bucket_findRangeEnd(Bucket *self, PyObject *keyarg, int low, int exclude_equal,
  627. int *offset)
  628. {
  629. int i, cmp;
  630. int result = -1; /* until proven innocent */
  631. KEY_TYPE key;
  632. int copied = 1;
  633. COPY_KEY_FROM_ARG(key, keyarg, copied);
  634. UNLESS (copied)
  635. return -1;
  636. UNLESS (PER_USE(self))
  637. return -1;
  638. BUCKET_SEARCH(i, cmp, self, key, goto Done);
  639. if (cmp == 0) {
  640. /* exact match at index i */
  641. if (exclude_equal)
  642. {
  643. /* but we don't want an exact match */
  644. if (low)
  645. ++i;
  646. else
  647. --i;
  648. }
  649. }
  650. /* Else keys[i-1] < key < keys[i], picturing infinities at OOB indices,
  651. * and i has the smallest item > key, which is correct for low.
  652. */
  653. else if (! low)
  654. /* i-1 has the largest item < key (unless i-1 is 0OB) */
  655. --i;
  656. result = 0 <= i && i < self->len;
  657. if (result)
  658. *offset = i;
  659. Done:
  660. PER_UNUSE(self);
  661. return result;
  662. }
  663. static PyObject *
  664. Bucket_maxminKey(Bucket *self, PyObject *args, int min)
  665. {
  666. PyObject *key=0;
  667. int rc, offset = 0;
  668. int empty_bucket = 1;
  669. if (args && ! PyArg_ParseTuple(args, "|O", &key))
  670. return NULL;
  671. PER_USE_OR_RETURN(self, NULL);
  672. UNLESS (self->len)
  673. goto empty;
  674. /* Find the low range */
  675. if (key && key != Py_None)
  676. {
  677. if ((rc = Bucket_findRangeEnd(self, key, min, 0, &offset)) <= 0)
  678. {
  679. if (rc < 0)
  680. return NULL;
  681. empty_bucket = 0;
  682. goto empty;
  683. }
  684. }
  685. else if (min)
  686. offset = 0;
  687. else
  688. offset = self->len -1;
  689. COPY_KEY_TO_OBJECT(key, self->keys[offset]);
  690. PER_UNUSE(self);
  691. return key;
  692. empty:
  693. PyErr_SetString(PyExc_ValueError,
  694. empty_bucket ? "empty bucket" :
  695. "no key satisfies the conditions");
  696. PER_UNUSE(self);
  697. return NULL;
  698. }
  699. static PyObject *
  700. Bucket_minKey(Bucket *self, PyObject *args)
  701. {
  702. return Bucket_maxminKey(self, args, 1);
  703. }
  704. static PyObject *
  705. Bucket_maxKey(Bucket *self, PyObject *args)
  706. {
  707. return Bucket_maxminKey(self, args, 0);
  708. }
  709. static int
  710. Bucket_rangeSearch(Bucket *self, PyObject *args, PyObject *kw,
  711. int *low, int *high)
  712. {
  713. PyObject *min = Py_None;
  714. PyObject *max = Py_None;
  715. int excludemin = 0;
  716. int excludemax = 0;
  717. int rc;
  718. if (args)
  719. {
  720. if (! PyArg_ParseTupleAndKeywords(args, kw, "|OOii", search_keywords,
  721. &min,
  722. &max,
  723. &excludemin,
  724. &excludemax))
  725. return -1;
  726. }
  727. UNLESS (self->len)
  728. goto empty;
  729. /* Find the low range */
  730. if (min != Py_None)
  731. {
  732. rc = Bucket_findRangeEnd(self, min, 1, excludemin, low);
  733. if (rc < 0)
  734. return -1;
  735. if (rc == 0)
  736. goto empty;
  737. }
  738. else
  739. {
  740. *low = 0;
  741. if (excludemin)
  742. {
  743. if (self->len < 2)
  744. goto empty;
  745. ++*low;
  746. }
  747. }
  748. /* Find the high range */
  749. if (max != Py_None)
  750. {
  751. rc = Bucket_findRangeEnd(self, max, 0, excludemax, high);
  752. if (rc < 0)
  753. return -1;
  754. if (rc == 0)
  755. goto empty;
  756. }
  757. else
  758. {
  759. *high = self->len - 1;
  760. if (excludemax)
  761. {
  762. if (self->len < 2)
  763. goto empty;
  764. --*high;
  765. }
  766. }
  767. /* If min < max to begin with, it's quite possible that low > high now. */
  768. if (*low <= *high)
  769. return 0;
  770. empty:
  771. *low = 0;
  772. *high = -1;
  773. return 0;
  774. }
  775. /*
  776. ** bucket_keys
  777. **
  778. ** Generate a list of all keys in the bucket
  779. **
  780. ** Arguments: self The Bucket
  781. ** args (unused)
  782. **
  783. ** Returns: list of bucket keys
  784. */
  785. static PyObject *
  786. bucket_keys(Bucket *self, PyObject *args, PyObject *kw)
  787. {
  788. PyObject *r = NULL, *key;
  789. int i, low, high;
  790. PER_USE_OR_RETURN(self, NULL);
  791. if (Bucket_rangeSearch(self, args, kw, &low, &high) < 0)
  792. goto err;
  793. r = PyList_New(high-low+1);
  794. if (r == NULL)
  795. goto err;
  796. for (i=low; i <= high; i++)
  797. {
  798. COPY_KEY_TO_OBJECT(key, self->keys[i]);
  799. if (PyList_SetItem(r, i-low , key) < 0)
  800. goto err;
  801. }
  802. PER_UNUSE(self);
  803. return r;
  804. err:
  805. PER_UNUSE(self);
  806. Py_XDECREF(r);
  807. return NULL;
  808. }
  809. /*
  810. ** bucket_values
  811. **
  812. ** Generate a list of all values in the bucket
  813. **
  814. ** Arguments: self The Bucket
  815. ** args (unused)
  816. **
  817. ** Returns list of values
  818. */
  819. static PyObject *
  820. bucket_values(Bucket *self, PyObject *args, PyObject *kw)
  821. {
  822. PyObject *r=0, *v;
  823. int i, low, high;
  824. PER_USE_OR_RETURN(self, NULL);
  825. if (Bucket_rangeSearch(self, args, kw, &low, &high) < 0)
  826. goto err;
  827. UNLESS (r=PyList_New(high-low+1))
  828. goto err;
  829. for (i=low; i <= high; i++)
  830. {
  831. COPY_VALUE_TO_OBJECT(v, self->values[i]);
  832. UNLESS (v)
  833. goto err;
  834. if (PyList_SetItem(r, i-low, v) < 0)
  835. goto err;
  836. }
  837. PER_UNUSE(self);
  838. return r;
  839. err:
  840. PER_UNUSE(self);
  841. Py_XDECREF(r);
  842. return NULL;
  843. }
  844. /*
  845. ** bucket_items
  846. **
  847. ** Returns a list of all items in a bucket
  848. **
  849. ** Arguments: self The Bucket
  850. ** args (unused)
  851. **
  852. ** Returns: list of all items in the bucket
  853. */
  854. static PyObject *
  855. bucket_items(Bucket *self, PyObject *args, PyObject *kw)
  856. {
  857. PyObject *r=0, *o=0, *item=0;
  858. int i, low, high;
  859. PER_USE_OR_RETURN(self, NULL);
  860. if (Bucket_rangeSearch(self, args, kw, &low, &high) < 0)
  861. goto err;
  862. UNLESS (r=PyList_New(high-low+1))
  863. goto err;
  864. for (i=low; i <= high; i++)
  865. {
  866. UNLESS (item = PyTuple_New(2))
  867. goto err;
  868. COPY_KEY_TO_OBJECT(o, self->keys[i]);
  869. UNLESS (o)
  870. goto err;
  871. PyTuple_SET_ITEM(item, 0, o);
  872. COPY_VALUE_TO_OBJECT(o, self->values[i]);
  873. UNLESS (o)
  874. goto err;
  875. PyTuple_SET_ITEM(item, 1, o);
  876. if (PyList_SetItem(r, i-low, item) < 0)
  877. goto err;
  878. item = 0;
  879. }
  880. PER_UNUSE(self);
  881. return r;
  882. err:
  883. PER_UNUSE(self);
  884. Py_XDECREF(r);
  885. Py_XDECREF(item);
  886. return NULL;
  887. }
  888. static PyObject *
  889. bucket_byValue(Bucket *self, PyObject *omin)
  890. {
  891. PyObject *r=0, *o=0, *item=0;
  892. VALUE_TYPE min;
  893. VALUE_TYPE v;
  894. int i, l, copied=1;
  895. PER_USE_OR_RETURN(self, NULL);
  896. COPY_VALUE_FROM_ARG(min, omin, copied);
  897. UNLESS(copied)
  898. return NULL;
  899. for (i=0, l=0; i < self->len; i++)
  900. if (TEST_VALUE(self->values[i], min) >= 0)
  901. l++;
  902. UNLESS (r=PyList_New(l))
  903. goto err;
  904. for (i=0, l=0; i < self->len; i++)
  905. {
  906. if (TEST_VALUE(self->values[i], min) < 0)
  907. continue;
  908. UNLESS (item = PyTuple_New(2))
  909. goto err;
  910. COPY_KEY_TO_OBJECT(o, self->keys[i]);
  911. UNLESS (o)
  912. goto err;
  913. PyTuple_SET_ITEM(item, 1, o);
  914. COPY_VALUE(v, self->values[i]);
  915. NORMALIZE_VALUE(v, min);
  916. COPY_VALUE_TO_OBJECT(o, v);
  917. DECREF_VALUE(v);
  918. UNLESS (o)
  919. goto err;
  920. PyTuple_SET_ITEM(item, 0, o);
  921. if (PyList_SetItem(r, l, item) < 0)
  922. goto err;
  923. l++;
  924. item = 0;
  925. }
  926. item=PyObject_GetAttr(r,sort_str);
  927. UNLESS (item)
  928. goto err;
  929. ASSIGN(item, PyObject_CallObject(item, NULL));
  930. UNLESS (item)
  931. goto err;
  932. ASSIGN(item, PyObject_GetAttr(r, reverse_str));
  933. UNLESS (item)
  934. goto err;
  935. ASSIGN(item, PyObject_CallObject(item, NULL));
  936. UNLESS (item)
  937. goto err;
  938. Py_DECREF(item);
  939. PER_UNUSE(self);
  940. return r;
  941. err:
  942. PER_UNUSE(self);
  943. Py_XDECREF(r);
  944. Py_XDECREF(item);
  945. return NULL;
  946. }
  947. static int
  948. _bucket_clear(Bucket *self)
  949. {
  950. const int len = self->len;
  951. /* Don't declare i at this level. If neither keys nor values are
  952. * PyObject*, i won't be referenced, and you'll get a nuisance compiler
  953. * wng for declaring it here.
  954. */
  955. self->len = self->size = 0;
  956. if (self->next)
  957. {
  958. Py_DECREF(self->next);
  959. self->next = NULL;
  960. }
  961. /* Silence compiler warning about unused variable len for the case
  962. when neither key nor value is an object, i.e. II. */
  963. (void)len;
  964. if (self->keys)
  965. {
  966. #ifdef KEY_TYPE_IS_PYOBJECT
  967. int i;
  968. for (i = 0; i < len; ++i)
  969. DECREF_KEY(self->keys[i]);
  970. #endif
  971. free(self->keys);
  972. self->keys = NULL;
  973. }
  974. if (self->values)
  975. {
  976. #ifdef VALUE_TYPE_IS_PYOBJECT
  977. int i;
  978. for (i = 0; i < len; ++i)
  979. DECREF_VALUE(self->values[i]);
  980. #endif
  981. free(self->values);
  982. self->values = NULL;
  983. }
  984. return 0;
  985. }
  986. #ifdef PERSISTENT
  987. static PyObject *
  988. bucket__p_deactivate(Bucket *self, PyObject *args, PyObject *keywords)
  989. {
  990. int ghostify = 1;
  991. PyObject *force = NULL;
  992. if (args && PyTuple_GET_SIZE(args) > 0)
  993. {
  994. PyErr_SetString(PyExc_TypeError,
  995. "_p_deactivate takes no positional arguments");
  996. return NULL;
  997. }
  998. if (keywords)
  999. {
  1000. int size = PyDict_Size(keywords);
  1001. force = PyDict_GetItemString(keywords, "force");
  1002. if (force)
  1003. size--;
  1004. if (size) {
  1005. PyErr_SetString(PyExc_TypeError,
  1006. "_p_deactivate only accepts keyword arg force");
  1007. return NULL;
  1008. }
  1009. }
  1010. if (self->jar && self->oid)
  1011. {
  1012. ghostify = self->state == cPersistent_UPTODATE_STATE;
  1013. if (!ghostify && force) {
  1014. if (PyObject_IsTrue(force))
  1015. ghostify = 1;
  1016. if (PyErr_Occurred())
  1017. return NULL;
  1018. }
  1019. if (ghostify) {
  1020. if (_bucket_clear(self) < 0)
  1021. return NULL;
  1022. PER_GHOSTIFY(self);
  1023. }
  1024. }
  1025. Py_INCREF(Py_None);
  1026. return Py_None;
  1027. }
  1028. #endif
  1029. static PyObject *
  1030. bucket_clear(Bucket *self, PyObject *args)
  1031. {
  1032. PER_USE_OR_RETURN(self, NULL);
  1033. if (self->len)
  1034. {
  1035. if (_bucket_clear(self) < 0)
  1036. return NULL;
  1037. if (PER_CHANGED(self) < 0)
  1038. goto err;
  1039. }
  1040. PER_UNUSE(self);
  1041. Py_INCREF(Py_None);
  1042. return Py_None;
  1043. err:
  1044. PER_UNUSE(self);
  1045. return NULL;
  1046. }
  1047. /*
  1048. * Return:
  1049. *
  1050. * For a set bucket (self->values is NULL), a one-tuple or two-tuple. The
  1051. * first element is a tuple of keys, of length self->len. The second element
  1052. * is the next bucket, present if and only if next is non-NULL:
  1053. *
  1054. * (
  1055. * (keys[0], keys[1], ..., keys[len-1]),
  1056. * <self->next iff non-NULL>
  1057. * )
  1058. *
  1059. * For a mapping bucket (self->values is not NULL), a one-tuple or two-tuple.
  1060. * The first element is a tuple interleaving keys and values, of length
  1061. * 2 * self->len. The second element is the next bucket, present iff next is
  1062. * non-NULL:
  1063. *
  1064. * (
  1065. * (keys[0], values[0], keys[1], values[1], ...,
  1066. * keys[len-1], values[len-1]),
  1067. * <self->next iff non-NULL>
  1068. * )
  1069. */
  1070. static PyObject *
  1071. bucket_getstate(Bucket *self)
  1072. {
  1073. PyObject *o = NULL, *items = NULL, *state;
  1074. int i, len, l;
  1075. PER_USE_OR_RETURN(self, NULL);
  1076. len = self->len;
  1077. if (self->values) /* Bucket */
  1078. {
  1079. items = PyTuple_New(len * 2);
  1080. if (items == NULL)
  1081. goto err;
  1082. for (i = 0, l = 0; i < len; i++) {
  1083. COPY_KEY_TO_OBJECT(o, self->keys[i]);
  1084. if (o == NULL)
  1085. goto err;
  1086. PyTuple_SET_ITEM(items, l, o);
  1087. l++;
  1088. COPY_VALUE_TO_OBJECT(o, self->values[i]);
  1089. if (o == NULL)
  1090. goto err;
  1091. PyTuple_SET_ITEM(items, l, o);
  1092. l++;
  1093. }
  1094. }
  1095. else /* Set */
  1096. {
  1097. items = PyTuple_New(len);
  1098. if (items == NULL)
  1099. goto err;
  1100. for (i = 0; i < len; i++) {
  1101. COPY_KEY_TO_OBJECT(o, self->keys[i]);
  1102. if (o == NULL)
  1103. goto err;
  1104. PyTuple_SET_ITEM(items, i, o);
  1105. }
  1106. }
  1107. if (self->next)
  1108. state = Py_BuildValue("OO", items, self->next);
  1109. else
  1110. state = Py_BuildValue("(O)", items);
  1111. Py_DECREF(items);
  1112. PER_UNUSE(self);
  1113. return state;
  1114. err:
  1115. PER_UNUSE(self);
  1116. Py_XDECREF(items);
  1117. return NULL;
  1118. }
  1119. static int
  1120. _bucket_setstate(Bucket *self, PyObject *state)
  1121. {
  1122. PyObject *k, *v, *items;
  1123. Bucket *next = NULL;
  1124. int i, l, len, copied=1;
  1125. KEY_TYPE *keys;
  1126. VALUE_TYPE *values;
  1127. if (!PyArg_ParseTuple(state, "O|O:__setstate__", &items, &next))
  1128. return -1;
  1129. if (!PyTuple_Check(items)) {
  1130. PyErr_SetString(PyExc_TypeError,
  1131. "tuple required for first state element");
  1132. return -1;
  1133. }
  1134. len = PyTuple_Size(items);
  1135. if (len < 0)
  1136. return -1;
  1137. len /= 2;
  1138. for (i = self->len; --i >= 0; ) {
  1139. DECREF_KEY(self->keys[i]);
  1140. DECREF_VALUE(self->values[i]);
  1141. }
  1142. self->len = 0;
  1143. if (self->next) {
  1144. Py_DECREF(self->next);
  1145. self->next = NULL;
  1146. }
  1147. if (len > self->size) {
  1148. keys = BTree_Realloc(self->keys, sizeof(KEY_TYPE)*len);
  1149. if (keys == NULL)
  1150. return -1;
  1151. values = BTree_Realloc(self->values, sizeof(VALUE_TYPE)*len);
  1152. if (values == NULL)
  1153. return -1;
  1154. self->keys = keys;
  1155. self->values = values;
  1156. self->size = len;
  1157. }
  1158. for (i=0, l=0; i < len; i++) {
  1159. k = PyTuple_GET_ITEM(items, l);
  1160. l++;
  1161. v = PyTuple_GET_ITEM(items, l);
  1162. l++;
  1163. COPY_KEY_FROM_ARG(self->keys[i], k, copied);
  1164. if (!copied)
  1165. return -1;
  1166. COPY_VALUE_FROM_ARG(self->values[i], v, copied);
  1167. if (!copied)
  1168. return -1;
  1169. INCREF_KEY(self->keys[i]);
  1170. INCREF_VALUE(self->values[i]);
  1171. }
  1172. self->len = len;
  1173. if (next) {
  1174. self->next = next;
  1175. Py_INCREF(next);
  1176. }
  1177. return 0;
  1178. }
  1179. static PyObject *
  1180. bucket_setstate(Bucket *self, PyObject *state)
  1181. {
  1182. int r;
  1183. PER_PREVENT_DEACTIVATION(self);
  1184. r = _bucket_setstate(self, state);
  1185. PER_UNUSE(self);
  1186. if (r < 0)
  1187. return NULL;
  1188. Py_INCREF(Py_None);
  1189. return Py_None;
  1190. }
  1191. static PyObject *
  1192. bucket_has_key(Bucket *self, PyObject *key)
  1193. {
  1194. return _bucket_get(self, key, 1);
  1195. }
  1196. static PyObject *
  1197. bucket_setdefault(Bucket *self, PyObject *args)
  1198. {
  1199. PyObject *key;
  1200. PyObject *failobj; /* default */
  1201. PyObject *value; /* return value */
  1202. int dummy_changed; /* in order to call _bucket_set */
  1203. if (! PyArg_UnpackTuple(args, "setdefault", 2, 2, &key, &failobj))
  1204. return NULL;
  1205. value = _bucket_get(self, key, 0);
  1206. if (value != NULL)
  1207. return value;
  1208. /* The key isn't in the bucket. If that's not due to a KeyError exception,
  1209. * pass back the unexpected exception.
  1210. */
  1211. if (! PyErr_ExceptionMatches(PyExc_KeyError))
  1212. return NULL;
  1213. PyErr_Clear();
  1214. /* Associate `key` with `failobj` in the bucket, and return `failobj`. */
  1215. value = failobj;
  1216. if (_bucket_set(self, key, failobj, 0, 0, &dummy_changed) < 0)
  1217. value = NULL;
  1218. Py_XINCREF(value);
  1219. return value;
  1220. }
  1221. /* forward declaration */
  1222. static int
  1223. Bucket_length(Bucket *self);
  1224. static PyObject *
  1225. bucket_pop(Bucket *self, PyObject *args)
  1226. {
  1227. PyObject *key;
  1228. PyObject *failobj = NULL; /* default */
  1229. PyObject *value; /* return value */
  1230. int dummy_changed; /* in order to call _bucket_set */
  1231. if (! PyArg_UnpackTuple(args, "pop", 1, 2, &key, &failobj))
  1232. return NULL;
  1233. value = _bucket_get(self, key, 0);
  1234. if (value != NULL) {
  1235. /* Delete key and associated value. */
  1236. if (_bucket_set(self, key, NULL, 0, 0, &dummy_changed) < 0) {
  1237. Py_DECREF(value);
  1238. return NULL;
  1239. }
  1240. return value;
  1241. }
  1242. /* The key isn't in the bucket. If that's not due to a KeyError exception,
  1243. * pass back the unexpected exception.
  1244. */
  1245. if (! PyErr_ExceptionMatches(PyExc_KeyError))
  1246. return NULL;
  1247. if (failobj != NULL) {
  1248. /* Clear the KeyError and return the explicit default. */
  1249. PyErr_Clear();
  1250. Py_INCREF(failobj);
  1251. return failobj;
  1252. }
  1253. /* No default given. The only difference in this case is the error
  1254. * message, which depends on whether the bucket is empty.
  1255. */
  1256. if (Bucket_length(self) == 0)
  1257. PyErr_SetString(PyExc_KeyError, "pop(): Bucket is empty");
  1258. return NULL;
  1259. }
  1260. /* Search bucket self for key. This is the sq_contains slot of the
  1261. * PySequenceMethods.
  1262. *
  1263. * Return:
  1264. * -1 error
  1265. * 0 not found
  1266. * 1 found
  1267. */
  1268. static int
  1269. bucket_contains(Bucket *self, PyObject *key)
  1270. {
  1271. PyObject *asobj = _bucket_get(self, key, 1);
  1272. int result = -1;
  1273. if (asobj != NULL) {
  1274. result = INT_AS_LONG(asobj) ? 1 : 0;
  1275. Py_DECREF(asobj);
  1276. }
  1277. return result;
  1278. }
  1279. /*
  1280. ** bucket_getm
  1281. **
  1282. */
  1283. static PyObject *
  1284. bucket_getm(Bucket *self, PyObject *args)
  1285. {
  1286. PyObject *key, *d=Py_None, *r;
  1287. if (!PyArg_ParseTuple(args, "O|O:get", &key, &d))
  1288. return NULL;
  1289. r = _bucket_get(self, key, 0);
  1290. if (r)
  1291. return r;
  1292. if (!PyErr_ExceptionMatches(PyExc_KeyError))
  1293. return NULL;
  1294. PyErr_Clear();
  1295. Py_INCREF(d);
  1296. return d;
  1297. }
  1298. /**************************************************************************/
  1299. /* Iterator support. */
  1300. /* A helper to build all the iterators for Buckets and Sets.
  1301. * If args is NULL, the iterator spans the entire structure. Else it's an
  1302. * argument tuple, with optional low and high arguments.
  1303. * kind is 'k', 'v' or 'i'.
  1304. * Returns a BTreeIter object, or NULL if error.
  1305. */
  1306. static PyObject *
  1307. buildBucketIter(Bucket *self, PyObject *args, PyObject *kw, char kind)
  1308. {
  1309. BTreeItems *items;
  1310. int lowoffset, highoffset;
  1311. BTreeIter *result = NULL;
  1312. PER_USE_OR_RETURN(self, NULL);
  1313. if (Bucket_rangeSearch(self, args, kw, &lowoffset, &highoffset) < 0)
  1314. goto Done;
  1315. items = (BTreeItems *)newBTreeItems(kind, self, lowoffset,
  1316. self, highoffset);
  1317. if (items == NULL)
  1318. goto Done;
  1319. result = BTreeIter_new(items); /* win or lose, we're done */
  1320. Py_DECREF(items);
  1321. Done:
  1322. PER_UNUSE(self);
  1323. return (PyObject *)result;
  1324. }
  1325. /* The implementation of iter(Bucket_or_Set); the Bucket tp_iter slot. */
  1326. static PyObject *
  1327. Bucket_getiter(Bucket *self)
  1328. {
  1329. return buildBucketIter(self, NULL, NULL, 'k');
  1330. }
  1331. /* The implementation of Bucket.iterkeys(). */
  1332. static PyObject *
  1333. Bucket_iterkeys(Bucket *self, PyObject *args, PyObject *kw)
  1334. {
  1335. return buildBucketIter(self, args, kw, 'k');
  1336. }
  1337. /* The implementation of Bucket.itervalues(). */
  1338. static PyObject *
  1339. Bucket_itervalues(Bucket *self, PyObject *args, PyObject *kw)
  1340. {
  1341. return buildBucketIter(self, args, kw, 'v');
  1342. }
  1343. /* The implementation of Bucket.iteritems(). */
  1344. static PyObject *
  1345. Bucket_iteritems(Bucket *self, PyObject *args, PyObject *kw)
  1346. {
  1347. return buildBucketIter(self, args, kw, 'i');
  1348. }
  1349. /* End of iterator support. */
  1350. #ifdef PERSISTENT
  1351. static PyObject *merge_error(int p1, int p2, int p3, int reason);
  1352. static PyObject *bucket_merge(Bucket *s1, Bucket *s2, Bucket *s3);
  1353. static PyObject *
  1354. _bucket__p_resolveConflict(PyObject *ob_type, PyObject *s[3])
  1355. {
  1356. PyObject *result = NULL; /* guilty until proved innocent */
  1357. Bucket *b[3] = {NULL, NULL, NULL};
  1358. PyObject *meth = NULL;
  1359. PyObject *a = NULL;
  1360. int i;
  1361. for (i = 0; i < 3; i++) {
  1362. PyObject *r;
  1363. b[i] = (Bucket*)PyObject_CallObject((PyObject *)ob_type, NULL);
  1364. if (b[i] == NULL)
  1365. goto Done;
  1366. if (s[i] == Py_None) /* None is equivalent to empty, for BTrees */
  1367. continue;
  1368. meth = PyObject_GetAttr((PyObject *)b[i], __setstate___str);
  1369. if (meth == NULL)
  1370. goto Done;
  1371. a = PyTuple_New(1);
  1372. if (a == NULL)
  1373. goto Done;
  1374. PyTuple_SET_ITEM(a, 0, s[i]);
  1375. Py_INCREF(s[i]);
  1376. r = PyObject_CallObject(meth, a); /* b[i].__setstate__(s[i]) */
  1377. if (r == NULL)
  1378. goto Done;
  1379. Py_DECREF(r);
  1380. Py_DECREF(a);
  1381. Py_DECREF(meth);
  1382. a = meth = NULL;
  1383. }
  1384. if (b[0]->next != b[1]->next || b[0]->next != b[2]->next)
  1385. merge_error(-1, -1, -1, 0);
  1386. else
  1387. result = bucket_merge(b[0], b[1], b[2]);
  1388. Done:
  1389. Py_XDECREF(meth);
  1390. Py_XDECREF(a);
  1391. Py_XDECREF(b[0]);
  1392. Py_XDECREF(b[1]);
  1393. Py_XDECREF(b[2]);
  1394. return result;
  1395. }
  1396. static PyObject *
  1397. bucket__p_resolveConflict(Bucket *self, PyObject *args)
  1398. {
  1399. PyObject *s[3];
  1400. if (!PyArg_ParseTuple(args, "OOO", &s[0], &s[1], &s[2]))
  1401. return NULL;
  1402. return _bucket__p_resolveConflict((PyObject *)Py_TYPE(self), s);
  1403. }
  1404. #endif
  1405. /* Caution: Even though the _next attribute is read-only, a program could
  1406. do arbitrary damage to the btree internals. For example, it could call
  1407. clear() on a bucket inside a BTree.
  1408. We need to decide if the convenience for inspecting BTrees is worth
  1409. the risk.
  1410. */
  1411. static struct PyMemberDef Bucket_members[] = {
  1412. {"_next", T_OBJECT, offsetof(Bucket, next)},
  1413. {NULL}
  1414. };
  1415. static struct PyMethodDef Bucket_methods[] = {
  1416. {"__getstate__", (PyCFunction) bucket_getstate, METH_NOARGS,
  1417. "__getstate__() -- Return the picklable state of the object"},
  1418. {"__setstate__", (PyCFunction) bucket_setstate, METH_O,
  1419. "__setstate__() -- Set the state of the object"},
  1420. {"keys", (PyCFunction) bucket_keys, METH_VARARGS | METH_KEYWORDS,
  1421. "keys([min, max]) -- Return the keys"},
  1422. {"has_key", (PyCFunction) bucket_has_key, METH_O,
  1423. "has_key(key) -- Test whether the bucket contains the given key"},
  1424. {"clear", (PyCFunction) bucket_clear, METH_VARARGS,
  1425. "clear() -- Remove all of the items from the bucket"},
  1426. {"update", (PyCFunction) Mapping_update, METH_O,
  1427. "update(collection) -- Add the items from the given collection"},
  1428. {"maxKey", (PyCFunction) Bucket_maxKey, METH_VARARGS,
  1429. "maxKey([key]) -- Find the maximum key\n\n"
  1430. "If an argument is given, find the maximum <= the argument"},
  1431. {"minKey", (PyCFunction) Bucket_minKey, METH_VARARGS,
  1432. "minKey([key]) -- Find the minimum key\n\n"
  1433. "If an argument is given, find the minimum >= the argument"},
  1434. {"values", (PyCFunction) bucket_values, METH_VARARGS | METH_KEYWORDS,
  1435. "values([min, max]) -- Return the values"},
  1436. {"items", (PyCFunction) bucket_items, METH_VARARGS | METH_KEYWORDS,
  1437. "items([min, max])) -- Return the items"},
  1438. {"byValue", (PyCFunction) bucket_byValue, METH_O,
  1439. "byValue(min) -- "
  1440. "Return value-keys with values >= min and reverse sorted by values"},
  1441. {"get", (PyCFunction) bucket_getm, METH_VARARGS,
  1442. "get(key[,default]) -- Look up a value\n\n"
  1443. "Return the default (or None) if the key is not found."},
  1444. {"setdefault", (PyCFunction) bucket_setdefault, METH_VARARGS,
  1445. "D.setdefault(k, d) -> D.get(k, d), also set D[k]=d if k not in D.\n\n"
  1446. "Return the value like get() except that if key is missing, d is both\n"
  1447. "returned and inserted into the bucket as the value of k."},
  1448. {"pop", (PyCFunction) bucket_pop, METH_VARARGS,
  1449. "D.pop(k[, d]) -> v, remove key and return the corresponding value.\n\n"
  1450. "If key is not found, d is returned if given, otherwise KeyError\n"
  1451. "is raised."},
  1452. {"iterkeys", (PyCFunction) Bucket_iterkeys, METH_VARARGS | METH_KEYWORDS,
  1453. "B.iterkeys([min[,max]]) -> an iterator over the keys of B"},
  1454. {"itervalues",
  1455. (PyCFunction) Bucket_itervalues, METH_VARARGS | METH_KEYWORDS,
  1456. "B.itervalues([min[,max]]) -> an iterator over the values of B"},
  1457. {"iteritems", (PyCFunction) Bucket_iteritems, METH_VARARGS | METH_KEYWORDS,
  1458. "B.iteritems([min[,max]]) -> an iterator over the (key, value) "
  1459. "items of B"},
  1460. #ifdef EXTRA_BUCKET_METHODS
  1461. EXTRA_BUCKET_METHODS
  1462. #endif
  1463. #ifdef PERSISTENT
  1464. {"_p_resolveConflict",
  1465. (PyCFunction) bucket__p_resolveConflict, METH_VARARGS,
  1466. "_p_resolveConflict() -- Reinitialize from a newly created copy"},
  1467. {"_p_deactivate",
  1468. (PyCFunction) bucket__p_deactivate, METH_VARARGS | METH_KEYWORDS,
  1469. "_p_deactivate() -- Reinitialize from a newly created copy"},
  1470. #endif
  1471. {NULL, NULL}
  1472. };
  1473. static int
  1474. Bucket_init(PyObject *self, PyObject *args, PyObject *kwds)
  1475. {
  1476. PyObject *v = NULL;
  1477. if (!PyArg_ParseTuple(args, "|O:" MOD_NAME_PREFIX "Bucket", &v))
  1478. return -1;
  1479. if (v)
  1480. return update_from_seq(self, v);
  1481. else
  1482. return 0;
  1483. }
  1484. static void
  1485. bucket_dealloc(Bucket *self)
  1486. {
  1487. PyObject_GC_UnTrack((PyObject *)self);
  1488. if (self->state != cPersistent_GHOST_STATE) {
  1489. _bucket_clear(self);
  1490. }
  1491. cPersistenceCAPI->pertype->tp_dealloc((PyObject *)self);
  1492. }
  1493. static int
  1494. bucket_traverse(Bucket *self, visitproc visit, void *arg)
  1495. {
  1496. int err = 0;
  1497. int i, len;
  1498. #define VISIT(SLOT) \
  1499. if (SLOT) { \
  1500. err = visit((PyObject *)(SLOT), arg); \
  1501. if (err) \
  1502. goto Done; \
  1503. }
  1504. /* Call our base type's traverse function. Because buckets are
  1505. * subclasses of Peristent, there must be one.
  1506. */
  1507. err = cPersistenceCAPI->pertype->tp_traverse((PyObject *)self, visit, arg);
  1508. if (err)
  1509. goto Done;
  1510. /* If this is registered with the persistence system, cleaning up cycles
  1511. * is the database's problem. It would be horrid to unghostify buckets
  1512. * here just to chase pointers every time gc runs.
  1513. */
  1514. if (self->state == cPersistent_GHOST_STATE)
  1515. goto Done;
  1516. len = self->len;
  1517. /* if neither keys nor values are PyObject*, "i" is otherwise
  1518. unreferenced and we get a nuisance compiler wng */
  1519. (void)i;
  1520. (void)len;
  1521. #ifdef KEY_TYPE_IS_PYOBJECT
  1522. /* Keys are Python objects so need to be traversed. */
  1523. for (i = 0; i < len; i++)
  1524. VISIT(self->keys[i]);
  1525. #endif
  1526. #ifdef VALUE_TYPE_IS_PYOBJECT
  1527. if (self->values != NULL) {
  1528. /* self->values exists (this is a mapping bucket, not a set bucket),
  1529. * and are Python objects, so need to be traversed. */
  1530. for (i = 0; i < len; i++)
  1531. VISIT(self->values[i]);
  1532. }
  1533. #endif
  1534. VISIT(self->next);
  1535. Done:
  1536. return err;
  1537. #undef VISIT
  1538. }
  1539. static int
  1540. bucket_tp_clear(Bucket *self)
  1541. {
  1542. if (self->state != cPersistent_GHOST_STATE)
  1543. _bucket_clear(self);
  1544. return 0;
  1545. }
  1546. /* Code to access Bucket objects as mappings */
  1547. static int
  1548. Bucket_length( Bucket *self)
  1549. {
  1550. int r;
  1551. UNLESS (PER_USE(self))
  1552. return -1;
  1553. r = self->len;
  1554. PER_UNUSE(self);
  1555. return r;
  1556. }
  1557. static PyMappingMethods Bucket_as_mapping = {
  1558. (lenfunc)Bucket_length, /*mp_length*/
  1559. (binaryfunc)bucket_getitem, /*mp_subscript*/
  1560. (objobjargproc)bucket_setitem, /*mp_ass_subscript*/
  1561. };
  1562. static PySequenceMethods Bucket_as_sequence = {
  1563. (lenfunc)0, /* sq_length */
  1564. (binaryfunc)0, /* sq_concat */
  1565. (ssizeargfunc)0, /* sq_repeat */
  1566. (ssizeargfunc)0, /* sq_item */
  1567. (ssizessizeargfunc)0, /* sq_slice */
  1568. (ssizeobjargproc)0, /* sq_ass_item */
  1569. (ssizessizeobjargproc)0, /* sq_ass_slice */
  1570. (objobjproc)bucket_contains, /* sq_contains */
  1571. 0, /* sq_inplace_concat */
  1572. 0, /* sq_inplace_repeat */
  1573. };
  1574. static PyObject *
  1575. bucket_repr(Bucket *self)
  1576. {
  1577. PyObject *i, *r;
  1578. #ifdef PY3K
  1579. PyObject *rb;
  1580. #endif
  1581. char repr[10000];
  1582. int rv;
  1583. i = bucket_items(self, NULL, NULL);
  1584. if (!i)
  1585. {
  1586. return NULL;
  1587. }
  1588. r = PyObject_Repr(i);
  1589. Py_DECREF(i);
  1590. if (!r)
  1591. {
  1592. return NULL;
  1593. }
  1594. #ifdef PY3K
  1595. rb = PyUnicode_AsLatin1String(r);
  1596. rv = PyOS_snprintf(repr, sizeof(repr),
  1597. "%s(%s)", Py_TYPE(self)->tp_name,
  1598. PyBytes_AsString(rb));
  1599. Py_DECREF(rb);
  1600. #else
  1601. rv = PyOS_snprintf(repr, sizeof(repr),
  1602. "%s(%s)", Py_TYPE(self)->tp_name,
  1603. PyBytes_AS_STRING(r));
  1604. #endif
  1605. if (rv > 0 && (size_t)rv < sizeof(repr))
  1606. {
  1607. Py_DECREF(r);
  1608. #ifdef PY3K
  1609. return PyUnicode_DecodeLatin1(repr, strlen(repr), "surrogateescape");
  1610. #else
  1611. return PyBytes_FromStringAndSize(repr, strlen(repr));
  1612. #endif
  1613. }
  1614. else
  1615. {
  1616. /* The static buffer wasn't big enough */
  1617. int size;
  1618. PyObject *s;
  1619. #ifdef PY3K
  1620. PyObject *result;
  1621. #endif
  1622. /* 3 for the parens and the null byte */
  1623. size = strlen(Py_TYPE(self)->tp_name) + PyBytes_GET_SIZE(r) + 3;
  1624. s = PyBytes_FromStringAndSize(NULL, size);
  1625. if (!s) {
  1626. Py_DECREF(r);
  1627. return r;
  1628. }
  1629. PyOS_snprintf(PyBytes_AS_STRING(s), size,
  1630. "%s(%s)", Py_TYPE(self)->tp_name, PyBytes_AS_STRING(r));
  1631. Py_DECREF(r);
  1632. #ifdef PY3K
  1633. result = PyUnicode_FromEncodedObject(s, "latin1", "surrogateescape");
  1634. Py_DECREF(s);
  1635. return result;
  1636. #else
  1637. return s;
  1638. #endif
  1639. }
  1640. }
  1641. static PyTypeObject BucketType = {
  1642. PyVarObject_HEAD_INIT(NULL, 0)
  1643. MODULE_NAME MOD_NAME_PREFIX "Bucket", /* tp_name */
  1644. sizeof(Bucket), /* tp_basicsize */
  1645. 0, /* tp_itemsize */
  1646. (destructor)bucket_dealloc, /* tp_dealloc */
  1647. 0, /* tp_print */
  1648. 0, /* tp_getattr */
  1649. 0, /* tp_setattr */
  1650. 0, /* tp_compare */
  1651. (reprfunc)bucket_repr, /* tp_repr */
  1652. 0, /* tp_as_number */
  1653. &Bucket_as_sequence, /* tp_as_sequence */
  1654. &Bucket_as_mapping, /* tp_as_mapping */
  1655. 0, /* tp_hash */
  1656. 0, /* tp_call */
  1657. 0, /* tp_str */
  1658. 0, /* tp_getattro */
  1659. 0, /* tp_setattro */
  1660. 0, /* tp_as_buffer */
  1661. Py_TPFLAGS_DEFAULT |
  1662. Py_TPFLAGS_HAVE_GC |
  1663. Py_TPFLAGS_BASETYPE, /* tp_flags */
  1664. 0, /* tp_doc */
  1665. (traverseproc)bucket_traverse, /* tp_traverse */
  1666. (inquiry)bucket_tp_clear, /* tp_clear */
  1667. 0, /* tp_richcompare */
  1668. 0, /* tp_weaklistoffset */
  1669. (getiterfunc)Bucket_getiter, /* tp_iter */
  1670. 0, /* tp_iternext */
  1671. Bucket_methods, /* tp_methods */
  1672. Bucket_members, /* tp_members */
  1673. 0, /* tp_getset */
  1674. 0, /* tp_base */
  1675. 0, /* tp_dict */
  1676. 0, /* tp_descr_get */
  1677. 0, /* tp_descr_set */
  1678. 0, /* tp_dictoffset */
  1679. Bucket_init, /* tp_init */
  1680. 0, /* tp_alloc */
  1681. 0, /*PyType_GenericNew,*/ /* tp_new */
  1682. };
  1683. static int
  1684. nextBucket(SetIteration *i)
  1685. {
  1686. if (i->position >= 0)
  1687. {
  1688. UNLESS(PER_USE(BUCKET(i->set)))
  1689. return -1;
  1690. if (i->position)
  1691. {
  1692. DECREF_KEY(i->key);
  1693. DECREF_VALUE(i->value);
  1694. }
  1695. if (i->position < BUCKET(i->set)->len)
  1696. {
  1697. COPY_KEY(i->key, BUCKET(i->set)->keys[i->position]);
  1698. INCREF_KEY(i->key);
  1699. COPY_VALUE(i->value, BUCKET(i->set)->values[i->position]);
  1700. INCREF_VALUE(i->value);
  1701. i->position ++;
  1702. }
  1703. else
  1704. {
  1705. i->position = -1;
  1706. PER_ACCESSED(BUCKET(i->set));
  1707. }
  1708. PER_ALLOW_DEACTIVATION(BUCKET(i->set));
  1709. }
  1710. return 0;
  1711. }