0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 contains code u
0380: 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 sed to find the
0390: 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 6f 6d 6d most recent comm
03a0: 6f 6e 0a 2a 2a 20 61 6e 63 65 73 74 6f 72 20 6f on.** ancestor o
03b0: 66 20 74 69 6d 65 20 76 65 72 73 69 6f 6e 73 20 f time versions
03c0: 6f 66 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 of the same file
03d0: 2e 20 20 57 65 20 63 61 6c 6c 20 74 68 69 73 0a . We call this.
03e0: 2a 2a 20 63 6f 6d 6d 6f 6e 20 61 6e 63 65 73 74 ** common ancest
03f0: 6f 72 20 74 68 65 20 22 70 69 76 6f 74 22 20 69 or the "pivot" i
0400: 6e 20 61 20 33 2d 77 61 79 20 6d 65 72 67 65 2e n a 3-way merge.
0410: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f .*/.#include "co
0420: 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 nfig.h".#include
0430: 20 22 70 69 76 6f 74 2e 68 22 0a 23 69 6e 63 6c "pivot.h".#incl
0440: 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 0a ude <assert.h>..
0450: 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 ./*.** Set the p
0460: 72 69 6d 61 72 79 20 66 69 6c 65 2e 20 20 54 68 rimary file. Th
0470: 65 20 70 72 69 6d 61 72 79 20 76 65 72 73 69 6f e primary versio
0480: 6e 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 n is one of the
0490: 74 77 6f 0a 2a 2a 20 66 69 6c 65 73 20 74 68 61 two.** files tha
04a0: 74 20 68 61 76 65 20 61 20 63 6f 6d 6d 6f 6e 20 t have a common
04b0: 61 6e 63 65 73 74 6f 72 2e 20 20 54 68 65 20 6f ancestor. The o
04c0: 74 68 65 72 20 66 69 6c 65 20 69 73 20 74 68 65 ther file is the
04d0: 20 73 65 63 6f 6e 64 61 72 79 2e 0a 2a 2a 20 54 secondary..** T
04e0: 68 65 72 65 20 63 61 6e 20 62 65 20 6d 75 6c 74 here can be mult
04f0: 69 70 6c 65 20 73 65 63 6f 6e 64 61 72 69 65 73 iple secondaries
0500: 20 62 75 74 20 6f 6e 6c 79 20 61 20 73 69 6e 67 but only a sing
0510: 6c 65 20 70 72 69 6d 61 72 79 2e 0a 2a 2a 20 54 le primary..** T
0520: 68 65 20 70 72 69 6d 61 72 79 20 6d 75 73 74 20 he primary must
0530: 62 65 20 73 65 74 20 66 69 72 73 74 2e 0a 2a 2a be set first..**
0540: 0a 2a 2a 20 49 6e 20 74 68 65 20 6d 65 72 67 65 .** In the merge
0550: 20 61 6c 67 6f 72 69 74 68 6d 2c 20 74 68 65 20 algorithm, the
0560: 66 69 6c 65 20 62 65 69 6e 67 20 6d 65 72 67 65 file being merge
0570: 64 20 69 6e 20 69 73 20 74 68 65 20 70 72 69 6d d in is the prim
0580: 61 72 79 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 ary..** The curr
0590: 65 6e 74 20 63 68 65 63 6b 2d 6f 75 74 20 6f 72 ent check-out or
05a0: 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 61 other files tha
05b0: 74 20 68 61 76 65 20 62 65 65 6e 20 6d 65 72 67 t have been merg
05c0: 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 63 ed into.** the c
05d0: 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 20 urrent checkout
05e0: 61 72 65 20 74 68 65 20 73 65 63 6f 6e 64 61 72 are the secondar
05f0: 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 ies..**.** The a
0600: 63 74 20 6f 66 20 73 65 74 74 69 6e 67 20 74 68 ct of setting th
0610: 65 20 70 72 69 6d 61 72 79 20 72 65 73 65 74 73 e primary resets
0620: 20 74 68 65 20 70 69 76 6f 74 2d 66 69 6e 64 69 the pivot-findi
0630: 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f ng algorithm..*/
0640: 0a 76 6f 69 64 20 70 69 76 6f 74 5f 73 65 74 5f .void pivot_set_
0650: 70 72 69 6d 61 72 79 28 69 6e 74 20 72 69 64 29 primary(int rid)
0660: 7b 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 74 61 {. /* Set up ta
0670: 62 6c 65 20 75 73 65 64 20 74 6f 20 64 6f 20 74 ble used to do t
0680: 68 65 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 64 he search */. d
0690: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
06a0: 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 "CREATE TEMP T
06b0: 41 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 53 ABLE IF NOT EXIS
06c0: 54 53 20 61 71 75 65 75 65 28 22 0a 20 20 20 20 TS aqueue(".
06d0: 22 20 20 72 69 64 20 49 4e 54 45 47 45 52 20 50 " rid INTEGER P
06e0: 52 49 4d 41 52 59 20 4b 45 59 2c 22 20 20 2f 2a RIMARY KEY," /*
06f0: 20 54 68 65 20 72 65 63 6f 72 64 20 69 64 20 66 The record id f
0700: 6f 72 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 or this version
0710: 2a 2f 0a 20 20 20 20 22 20 20 6d 74 69 6d 65 20 */. " mtime
0720: 52 45 41 4c 2c 22 20 20 20 20 20 20 20 20 20 20 REAL,"
0730: 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 /* Time whe
0740: 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 77 n this version w
0750: 61 73 20 63 72 65 61 74 65 64 20 2a 2f 0a 20 20 as created */.
0760: 20 20 22 20 20 70 65 6e 64 69 6e 67 20 42 4f 4f " pending BOO
0770: 4c 45 41 4e 2c 22 20 20 20 20 20 20 20 20 20 20 LEAN,"
0780: 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 68 61 /* True if we ha
0790: 76 65 20 6e 6f 74 20 63 68 65 63 6b 20 74 68 69 ve not check thi
07a0: 73 20 6f 6e 65 20 79 65 74 20 2a 2f 0a 20 20 20 s one yet */.
07b0: 20 22 20 20 73 72 63 20 42 4f 4f 4c 45 41 4e 22 " src BOOLEAN"
07c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
07d0: 2a 20 31 20 66 6f 72 20 70 72 69 6d 61 72 79 2e * 1 for primary.
07e0: 20 20 30 20 66 6f 72 20 6f 74 68 65 72 73 20 2a 0 for others *
07f0: 2f 0a 20 20 20 20 22 29 3b 22 0a 20 20 20 20 22 /. ");". "
0800: 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 71 75 65 DELETE FROM aque
0810: 75 65 3b 22 0a 20 20 20 20 22 43 52 45 41 54 45 ue;". "CREATE
0820: 20 49 4e 44 45 58 20 49 46 20 4e 4f 54 20 45 58 INDEX IF NOT EX
0830: 49 53 54 53 20 61 71 75 65 75 65 5f 69 64 78 31 ISTS aqueue_idx1
0840: 20 4f 4e 20 61 71 75 65 75 65 28 70 65 6e 64 69 ON aqueue(pendi
0850: 6e 67 2c 20 6d 74 69 6d 65 29 3b 22 0a 20 20 29 ng, mtime);". )
0860: 3b 0a 0a 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 ;.. /* Insert t
0870: 68 65 20 70 72 69 6d 61 72 79 20 72 65 63 6f 72 he primary recor
0880: 64 20 2a 2f 0a 20 20 64 62 5f 6d 75 6c 74 69 5f d */. db_multi_
0890: 65 78 65 63 28 20 0a 20 20 20 20 22 49 4e 53 45 exec( . "INSE
08a0: 52 54 20 49 4e 54 4f 20 61 71 75 65 75 65 28 72 RT INTO aqueue(r
08b0: 69 64 2c 20 6d 74 69 6d 65 2c 20 70 65 6e 64 69 id, mtime, pendi
08c0: 6e 67 2c 20 73 72 63 29 22 0a 20 20 20 20 22 20 ng, src)". "
08d0: 20 53 45 4c 45 43 54 20 25 64 2c 20 6d 74 69 6d SELECT %d, mtim
08e0: 65 2c 20 31 2c 20 31 20 46 52 4f 4d 20 70 6c 69 e, 1, 1 FROM pli
08f0: 6e 6b 20 57 48 45 52 45 20 63 69 64 3d 25 64 22 nk WHERE cid=%d"
0900: 2c 0a 20 20 20 20 72 69 64 2c 20 72 69 64 0a 20 ,. rid, rid.
0910: 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 );.}../*.** Set
0920: 20 61 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c a secondary fil
0930: 65 2e 20 20 54 68 65 20 70 72 69 6d 61 72 79 20 e. The primary
0940: 66 69 6c 65 20 6d 75 73 74 20 62 65 20 73 65 74 file must be set
0950: 20 66 69 72 73 74 2e 20 20 54 68 65 72 65 0a 2a first. There.*
0960: 2a 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 * must be at lea
0970: 73 74 20 6f 6e 65 20 73 65 63 6f 6e 64 61 72 79 st one secondary
0980: 20 62 75 74 20 74 68 65 72 65 20 63 61 6e 20 62 but there can b
0990: 65 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 e more than one
09a0: 69 66 0a 2a 2a 20 64 65 73 69 72 65 64 2e 0a 2a if.** desired..*
09b0: 2f 0a 76 6f 69 64 20 70 69 76 6f 74 5f 73 65 74 /.void pivot_set
09c0: 5f 73 65 63 6f 6e 64 61 72 79 28 69 6e 74 20 72 _secondary(int r
09d0: 69 64 29 7b 0a 20 20 2f 2a 20 49 6e 73 65 72 74 id){. /* Insert
09e0: 20 74 68 65 20 70 72 69 6d 61 72 79 20 72 65 63 the primary rec
09f0: 6f 72 64 20 2a 2f 0a 20 20 64 62 5f 6d 75 6c 74 ord */. db_mult
0a00: 69 5f 65 78 65 63 28 20 0a 20 20 20 20 22 49 4e i_exec( . "IN
0a10: 53 45 52 54 20 49 4e 54 4f 20 61 71 75 65 75 65 SERT INTO aqueue
0a20: 28 72 69 64 2c 20 6d 74 69 6d 65 2c 20 70 65 6e (rid, mtime, pen
0a30: 64 69 6e 67 2c 20 73 72 63 29 22 0a 20 20 20 20 ding, src)".
0a40: 22 20 20 53 45 4c 45 43 54 20 25 64 2c 20 6d 74 " SELECT %d, mt
0a50: 69 6d 65 2c 20 31 2c 20 30 20 46 52 4f 4d 20 70 ime, 1, 0 FROM p
0a60: 6c 69 6e 6b 20 57 48 45 52 45 20 63 69 64 3d 25 link WHERE cid=%
0a70: 64 22 2c 0a 20 20 20 20 72 69 64 2c 20 72 69 64 d",. rid, rid
0a80: 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 . );.}../*.** F
0a90: 69 6e 64 20 74 68 65 20 6d 6f 73 74 20 72 65 63 ind the most rec
0aa0: 65 6e 74 20 63 6f 6d 6d 6f 6e 20 61 6e 63 65 73 ent common ances
0ab0: 74 6f 72 20 6f 66 20 74 68 65 20 70 72 69 6d 61 tor of the prima
0ac0: 72 79 20 61 6e 64 20 6f 6e 65 20 6f 66 0a 2a 2a ry and one of.**
0ad0: 20 74 68 65 20 73 65 63 6f 6e 64 61 72 69 65 73 the secondaries
0ae0: 2e 20 20 52 65 74 75 72 6e 20 69 74 73 20 72 69 . Return its ri
0af0: 64 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 d. Return 0 if
0b00: 6e 6f 20 63 6f 6d 6d 6f 6e 20 61 6e 63 65 73 74 no common ancest
0b10: 6f 72 0a 2a 2a 20 63 61 6e 20 62 65 20 66 6f 75 or.** can be fou
0b20: 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 70 69 76 6f 74 nd..*/.int pivot
0b30: 5f 66 69 6e 64 28 76 6f 69 64 29 7b 0a 20 20 53 _find(void){. S
0b40: 74 6d 74 20 71 31 2c 20 71 32 2c 20 75 31 2c 20 tmt q1, q2, u1,
0b50: 69 31 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20 i1;. int rid;.
0b60: 20 0a 20 20 2f 2a 20 61 71 75 65 75 65 20 6d 75 . /* aqueue mu
0b70: 73 74 20 63 6f 6e 74 61 69 6e 20 61 74 20 6c 65 st contain at le
0b80: 61 73 74 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 ast one primary
0b90: 61 6e 64 20 6f 6e 65 20 6f 74 68 65 72 2e 20 20 and one other.
0ba0: 4f 74 68 65 72 77 69 73 65 0a 20 20 2a 2a 20 77 Otherwise. ** w
0bb0: 65 20 61 62 6f 72 74 20 65 61 72 6c 79 0a 20 20 e abort early.
0bc0: 2a 2f 0a 20 20 69 66 28 20 64 62 5f 69 6e 74 28 */. if( db_int(
0bd0: 30 2c 20 22 53 45 4c 45 43 54 20 63 6f 75 6e 74 0, "SELECT count
0be0: 28 64 69 73 74 69 6e 63 74 20 73 72 63 29 20 46 (distinct src) F
0bf0: 52 4f 4d 20 61 71 75 65 75 65 22 29 3c 32 20 29 ROM aqueue")<2 )
0c00: 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e {. fossil_pan
0c10: 69 63 28 22 6c 61 63 6b 20 62 6f 74 68 20 70 72 ic("lack both pr
0c20: 69 6d 61 72 79 20 61 6e 64 20 73 65 63 6f 6e 64 imary and second
0c30: 61 72 79 20 66 69 6c 65 73 22 29 3b 0a 20 20 7d ary files");. }
0c40: 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65 20 71 .. /* Prepare q
0c50: 75 65 72 69 65 73 20 77 65 20 77 69 6c 6c 20 62 ueries we will b
0c60: 65 20 6e 65 65 64 69 6e 67 0a 20 20 2a 2a 0a 20 e needing. **.
0c70: 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 71 75 ** The first qu
0c80: 65 72 79 20 66 69 6e 64 73 20 74 68 65 20 6f 6c ery finds the ol
0c90: 64 65 73 74 20 70 65 6e 64 69 6e 67 20 76 65 72 dest pending ver
0ca0: 73 69 6f 6e 20 6f 6e 20 74 68 65 20 61 71 75 65 sion on the aque
0cb0: 75 65 2e 20 20 54 68 69 73 0a 20 20 2a 2a 20 77 ue. This. ** w
0cc0: 69 6c 6c 20 62 65 20 6e 65 78 74 20 6f 6e 65 20 ill be next one
0cd0: 73 65 61 72 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 searched.. */.
0ce0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 31 2c db_prepare(&q1,
0cf0: 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f "SELECT rid FRO
0d00: 4d 20 61 71 75 65 75 65 20 57 48 45 52 45 20 70 M aqueue WHERE p
0d10: 65 6e 64 69 6e 67 22 0a 20 20 20 20 20 20 20 20 ending".
0d20: 20 20 20 20 20 20 20 20 20 20 22 20 4f 52 44 45 " ORDE
0d30: 52 20 42 59 20 70 65 6e 64 69 6e 67 20 44 45 53 R BY pending DES
0d40: 43 2c 20 6d 74 69 6d 65 20 44 45 53 43 22 29 3b C, mtime DESC");
0d50: 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 .. /* Check to
0d60: 73 65 65 20 69 66 20 74 68 65 20 72 65 63 6f 72 see if the recor
0d70: 64 20 3a 72 69 64 20 69 73 20 61 20 63 6f 6d 6d d :rid is a comm
0d80: 6f 6e 20 61 6e 63 65 73 74 6f 72 2e 20 20 54 68 on ancestor. Th
0d90: 65 20 72 65 73 75 6c 74 0a 20 20 2a 2a 20 73 65 e result. ** se
0da0: 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f t contains one o
0db0: 72 20 6d 6f 72 65 20 72 6f 77 73 20 69 66 20 69 r more rows if i
0dc0: 74 20 69 73 20 61 6e 64 20 69 73 20 74 68 65 20 t is and is the
0dd0: 65 6d 70 74 79 20 73 65 74 20 69 66 20 69 74 0a empty set if it.
0de0: 20 20 2a 2a 20 69 73 20 6e 6f 74 2e 0a 20 20 2a ** is not.. *
0df0: 2f 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 /. db_prepare(&
0e00: 71 32 2c 0a 20 20 20 20 22 53 45 4c 45 43 54 20 q2,. "SELECT
0e10: 31 20 46 52 4f 4d 20 61 71 75 65 75 65 20 41 2c 1 FROM aqueue A,
0e20: 20 70 6c 69 6e 6b 2c 20 61 71 75 65 75 65 20 42 plink, aqueue B
0e30: 22 0a 20 20 20 20 22 20 57 48 45 52 45 20 70 6c ". " WHERE pl
0e40: 69 6e 6b 2e 70 69 64 3d 3a 72 69 64 22 0a 20 20 ink.pid=:rid".
0e50: 20 20 22 20 20 20 41 4e 44 20 70 6c 69 6e 6b 2e " AND plink.
0e60: 63 69 64 3d 42 2e 72 69 64 22 0a 20 20 20 20 22 cid=B.rid". "
0e70: 20 20 20 41 4e 44 20 41 2e 72 69 64 3d 3a 72 69 AND A.rid=:ri
0e80: 64 22 0a 20 20 20 20 22 20 20 20 41 4e 44 20 41 d". " AND A
0e90: 2e 73 72 63 21 3d 42 2e 73 72 63 22 0a 20 20 29 .src!=B.src". )
0ea0: 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 ;.. /* Mark the
0eb0: 20 3a 72 69 64 20 72 65 63 6f 72 64 20 68 61 73 :rid record has
0ec0: 20 68 61 76 69 6e 67 20 62 65 65 6e 20 63 68 65 having been che
0ed0: 63 6b 65 64 2e 20 20 49 74 20 69 73 20 6e 6f 74 cked. It is not
0ee0: 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6d 6d 6f 6e the. ** common
0ef0: 20 61 6e 63 65 73 74 6f 72 2e 0a 20 20 2a 2f 0a ancestor.. */.
0f00: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 75 31 db_prepare(&u1
0f10: 2c 0a 20 20 20 20 22 55 50 44 41 54 45 20 61 71 ,. "UPDATE aq
0f20: 75 65 75 65 20 53 45 54 20 70 65 6e 64 69 6e 67 ueue SET pending
0f30: 3d 30 20 57 48 45 52 45 20 72 69 64 3d 3a 72 69 =0 WHERE rid=:ri
0f40: 64 22 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 64 d". );.. /* Ad
0f50: 64 20 74 6f 20 74 68 65 20 71 75 65 75 65 20 61 d to the queue a
0f60: 6c 6c 20 61 6e 63 65 73 74 6f 72 73 20 6f 66 20 ll ancestors of
0f70: 3a 72 69 64 2e 0a 20 20 2a 2f 0a 20 20 64 62 5f :rid.. */. db_
0f80: 70 72 65 70 61 72 65 28 26 69 31 2c 0a 20 20 20 prepare(&i1,.
0f90: 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f "INSERT OR IGNO
0fa0: 52 45 20 49 4e 54 4f 20 61 71 75 65 75 65 20 22 RE INTO aqueue "
0fb0: 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70 6c 69 . "SELECT pli
0fc0: 6e 6b 2e 70 69 64 2c 22 0a 20 20 20 20 22 20 20 nk.pid,". "
0fd0: 20 20 20 20 20 63 6f 61 6c 65 73 63 65 28 28 53 coalesce((S
0fe0: 45 4c 45 43 54 20 6d 74 69 6d 65 20 46 52 4f 4d ELECT mtime FROM
0ff0: 20 70 6c 69 6e 6b 20 58 20 57 48 45 52 45 20 58 plink X WHERE X
1000: 2e 63 69 64 3d 70 6c 69 6e 6b 2e 70 69 64 29 2c .cid=plink.pid),
1010: 20 30 2e 30 29 2c 22 0a 20 20 20 20 22 20 20 20 0.0),". "
1020: 20 20 20 20 31 2c 22 0a 20 20 20 20 22 20 20 20 1,". "
1030: 20 20 20 20 61 71 75 65 75 65 2e 73 72 63 20 22 aqueue.src "
1040: 0a 20 20 20 20 22 20 20 46 52 4f 4d 20 70 6c 69 . " FROM pli
1050: 6e 6b 2c 20 61 71 75 65 75 65 22 0a 20 20 20 20 nk, aqueue".
1060: 22 20 57 48 45 52 45 20 70 6c 69 6e 6b 2e 63 69 " WHERE plink.ci
1070: 64 3d 3a 72 69 64 22 0a 20 20 20 20 22 20 20 20 d=:rid". "
1080: 41 4e 44 20 61 71 75 65 75 65 2e 72 69 64 3d 3a AND aqueue.rid=:
1090: 72 69 64 22 0a 20 20 29 3b 0a 0a 20 20 77 68 69 rid". );.. whi
10a0: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 31 29 le( db_step(&q1)
10b0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a ==SQLITE_ROW ){.
10c0: 20 20 20 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c rid = db_col
10d0: 75 6d 6e 5f 69 6e 74 28 26 71 31 2c 20 30 29 3b umn_int(&q1, 0);
10e0: 0a 20 20 20 20 64 62 5f 72 65 73 65 74 28 26 71 . db_reset(&q
10f0: 31 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 1);. db_bind_
1100: 69 6e 74 28 26 71 32 2c 20 22 3a 72 69 64 22 2c int(&q2, ":rid",
1110: 20 72 69 64 29 3b 0a 20 20 20 20 69 66 28 20 64 rid);. if( d
1120: 62 5f 73 74 65 70 28 26 71 32 29 3d 3d 53 51 4c b_step(&q2)==SQL
1130: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 ITE_ROW ){.
1140: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1150: 20 20 20 64 62 5f 72 65 73 65 74 28 26 71 32 29 db_reset(&q2)
1160: 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 69 6e ;. db_bind_in
1170: 74 28 26 69 31 2c 20 22 3a 72 69 64 22 2c 20 72 t(&i1, ":rid", r
1180: 69 64 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 id);. db_exec
1190: 28 26 69 31 29 3b 0a 20 20 20 20 64 62 5f 62 69 (&i1);. db_bi
11a0: 6e 64 5f 69 6e 74 28 26 75 31 2c 20 22 3a 72 69 nd_int(&u1, ":ri
11b0: 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 64 62 d", rid);. db
11c0: 5f 65 78 65 63 28 26 75 31 29 3b 0a 20 20 20 20 _exec(&u1);.
11d0: 72 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 64 rid = 0;. }. d
11e0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 31 29 3b b_finalize(&q1);
11f0: 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 . db_finalize(&
1200: 71 32 29 3b 0a 20 20 64 62 5f 66 69 6e 61 6c 69 q2);. db_finali
1210: 7a 65 28 26 69 31 29 3b 0a 20 20 64 62 5f 66 69 ze(&i1);. db_fi
1220: 6e 61 6c 69 7a 65 28 26 75 31 29 3b 0a 20 20 72 nalize(&u1);. r
1230: 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a 0a 2f 2a eturn rid;.}../*
1240: 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 .** COMMAND: te
1250: 73 74 2d 66 69 6e 64 2d 70 69 76 6f 74 0a 2a 2a st-find-pivot.**
1260: 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 70 69 76 .** Test the piv
1270: 6f 74 5f 66 69 6e 64 28 29 20 70 72 6f 63 65 64 ot_find() proced
1280: 75 72 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 ure..*/.void tes
1290: 74 5f 66 69 6e 64 5f 70 69 76 6f 74 28 76 6f 69 t_find_pivot(voi
12a0: 64 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72 69 64 d){. int i, rid
12b0: 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 3c 34 ;. if( g.argc<4
12c0: 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 50 ){. usage("P
12d0: 52 49 4d 41 52 59 20 53 45 43 4f 4e 44 41 52 59 RIMARY SECONDARY
12e0: 20 2e 2e 2e 22 29 3b 0a 20 20 7d 0a 20 20 64 62 ...");. }. db
12f0: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f _must_be_within_
1300: 74 72 65 65 28 29 3b 0a 20 20 70 69 76 6f 74 5f tree();. pivot_
1310: 73 65 74 5f 70 72 69 6d 61 72 79 28 6e 61 6d 65 set_primary(name
1320: 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b 32 _to_rid(g.argv[2
1330: 5d 29 29 3b 0a 20 20 66 6f 72 28 69 3d 33 3b 20 ]));. for(i=3;
1340: 69 3c 67 2e 61 72 67 63 3b 20 69 2b 2b 29 7b 0a i<g.argc; i++){.
1350: 20 20 20 20 70 69 76 6f 74 5f 73 65 74 5f 73 65 pivot_set_se
1360: 63 6f 6e 64 61 72 79 28 6e 61 6d 65 5f 74 6f 5f condary(name_to_
1370: 72 69 64 28 67 2e 61 72 67 76 5b 69 5d 29 29 3b rid(g.argv[i]));
1380: 0a 20 20 7d 0a 20 20 72 69 64 20 3d 20 70 69 76 . }. rid = piv
1390: 6f 74 5f 66 69 6e 64 28 29 3b 0a 20 20 70 72 69 ot_find();. pri
13a0: 6e 74 66 28 22 70 69 76 6f 74 3d 25 73 5c 6e 22 ntf("pivot=%s\n"
13b0: 2c 0a 20 20 20 20 20 20 20 20 20 64 62 5f 74 65 ,. db_te
13c0: 78 74 28 22 3f 22 2c 22 53 45 4c 45 43 54 20 75 xt("?","SELECT u
13d0: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 uid FROM blob WH
13e0: 45 52 45 20 72 69 64 3d 25 64 22 2c 72 69 64 29 ERE rid=%d",rid)
13f0: 0a 20 20 29 3b 0a 7d 0a . );.}.