Hex Artifact Content
Not logged in

Artifact 2953f62a20e8f8584f272b125b6ee4f1e28a9d90:

File src/pivot.c part of check-in [fa0ba20a51] - Fix another bug in the pivot finder. Null-merge with the dan-branch so that we now only have a single leaf on the tree. by drh on 2007-07-30 21:23:44.

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 20  nk WHERE cid=%d 
0900: 4c 49 4d 49 54 20 31 22 2c 0a 20 20 20 20 72 69  LIMIT 1",.    ri
0910: 64 2c 20 72 69 64 0a 20 20 29 3b 0a 7d 0a 0a 2f  d, rid.  );.}../
0920: 2a 0a 2a 2a 20 53 65 74 20 61 20 73 65 63 6f 6e  *.** Set a secon
0930: 64 61 72 79 20 66 69 6c 65 2e 20 20 54 68 65 20  dary file.  The 
0940: 70 72 69 6d 61 72 79 20 66 69 6c 65 20 6d 75 73  primary file mus
0950: 74 20 62 65 20 73 65 74 20 66 69 72 73 74 2e 20  t be set first. 
0960: 20 54 68 65 72 65 0a 2a 2a 20 6d 75 73 74 20 62   There.** must b
0970: 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 73  e at least one s
0980: 65 63 6f 6e 64 61 72 79 20 62 75 74 20 74 68 65  econdary but the
0990: 72 65 20 63 61 6e 20 62 65 20 6d 6f 72 65 20 74  re can be more t
09a0: 68 61 6e 20 6f 6e 65 20 69 66 0a 2a 2a 20 64 65  han one if.** de
09b0: 73 69 72 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 70  sired..*/.void p
09c0: 69 76 6f 74 5f 73 65 74 5f 73 65 63 6f 6e 64 61  ivot_set_seconda
09d0: 72 79 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 2f  ry(int rid){.  /
09e0: 2a 20 49 6e 73 65 72 74 20 74 68 65 20 70 72 69  * Insert the pri
09f0: 6d 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20  mary record */. 
0a00: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 20   db_multi_exec( 
0a10: 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20  .    "INSERT OR 
0a20: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 61 71 75 65  IGNORE INTO aque
0a30: 75 65 28 72 69 64 2c 20 6d 74 69 6d 65 2c 20 70  ue(rid, mtime, p
0a40: 65 6e 64 69 6e 67 2c 20 73 72 63 29 22 0a 20 20  ending, src)".  
0a50: 20 20 22 20 20 53 45 4c 45 43 54 20 25 64 2c 20    "  SELECT %d, 
0a60: 6d 74 69 6d 65 2c 20 31 2c 20 30 20 46 52 4f 4d  mtime, 1, 0 FROM
0a70: 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 63 69 64   plink WHERE cid
0a80: 3d 25 64 22 2c 0a 20 20 20 20 72 69 64 2c 20 72  =%d",.    rid, r
0a90: 69 64 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  id.  );.}../*.**
0aa0: 20 46 69 6e 64 20 74 68 65 20 6d 6f 73 74 20 72   Find the most r
0ab0: 65 63 65 6e 74 20 63 6f 6d 6d 6f 6e 20 61 6e 63  ecent common anc
0ac0: 65 73 74 6f 72 20 6f 66 20 74 68 65 20 70 72 69  estor of the pri
0ad0: 6d 61 72 79 20 61 6e 64 20 6f 6e 65 20 6f 66 0a  mary and one of.
0ae0: 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 61 72 69  ** the secondari
0af0: 65 73 2e 20 20 52 65 74 75 72 6e 20 69 74 73 20  es.  Return its 
0b00: 72 69 64 2e 20 20 52 65 74 75 72 6e 20 30 20 69  rid.  Return 0 i
0b10: 66 20 6e 6f 20 63 6f 6d 6d 6f 6e 20 61 6e 63 65  f no common ance
0b20: 73 74 6f 72 0a 2a 2a 20 63 61 6e 20 62 65 20 66  stor.** can be f
0b30: 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 70 69 76  ound..*/.int piv
0b40: 6f 74 5f 66 69 6e 64 28 76 6f 69 64 29 7b 0a 20  ot_find(void){. 
0b50: 20 53 74 6d 74 20 71 31 2c 20 71 32 2c 20 75 31   Stmt q1, q2, u1
0b60: 2c 20 69 31 3b 0a 20 20 69 6e 74 20 72 69 64 3b  , i1;.  int rid;
0b70: 0a 20 20 0a 20 20 2f 2a 20 61 71 75 65 75 65 20  .  .  /* aqueue 
0b80: 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 61 74 20  must contain at 
0b90: 6c 65 61 73 74 20 6f 6e 65 20 70 72 69 6d 61 72  least one primar
0ba0: 79 20 61 6e 64 20 6f 6e 65 20 6f 74 68 65 72 2e  y and one other.
0bb0: 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20 2a 2a    Otherwise.  **
0bc0: 20 77 65 20 61 62 6f 72 74 20 65 61 72 6c 79 0a   we abort early.
0bd0: 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 5f 69 6e    */.  if( db_in
0be0: 74 28 30 2c 20 22 53 45 4c 45 43 54 20 63 6f 75  t(0, "SELECT cou
0bf0: 6e 74 28 64 69 73 74 69 6e 63 74 20 73 72 63 29  nt(distinct src)
0c00: 20 46 52 4f 4d 20 61 71 75 65 75 65 22 29 3c 32   FROM aqueue")<2
0c10: 20 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70   ){.    fossil_p
0c20: 61 6e 69 63 28 22 6c 61 63 6b 20 62 6f 74 68 20  anic("lack both 
0c30: 70 72 69 6d 61 72 79 20 61 6e 64 20 73 65 63 6f  primary and seco
0c40: 6e 64 61 72 79 20 66 69 6c 65 73 22 29 3b 0a 20  ndary files");. 
0c50: 20 7d 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65   }..  /* Prepare
0c60: 20 71 75 65 72 69 65 73 20 77 65 20 77 69 6c 6c   queries we will
0c70: 20 62 65 20 6e 65 65 64 69 6e 67 0a 20 20 2a 2a   be needing.  **
0c80: 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
0c90: 71 75 65 72 79 20 66 69 6e 64 73 20 74 68 65 20  query finds the 
0ca0: 6f 6c 64 65 73 74 20 70 65 6e 64 69 6e 67 20 76  oldest pending v
0cb0: 65 72 73 69 6f 6e 20 6f 6e 20 74 68 65 20 61 71  ersion on the aq
0cc0: 75 65 75 65 2e 20 20 54 68 69 73 0a 20 20 2a 2a  ueue.  This.  **
0cd0: 20 77 69 6c 6c 20 62 65 20 6e 65 78 74 20 6f 6e   will be next on
0ce0: 65 20 73 65 61 72 63 68 65 64 2e 0a 20 20 2a 2f  e searched..  */
0cf0: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71  .  db_prepare(&q
0d00: 31 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46  1, "SELECT rid F
0d10: 52 4f 4d 20 61 71 75 65 75 65 20 57 48 45 52 45  ROM aqueue WHERE
0d20: 20 70 65 6e 64 69 6e 67 22 0a 20 20 20 20 20 20   pending".      
0d30: 20 20 20 20 20 20 20 20 20 20 20 20 22 20 4f 52              " OR
0d40: 44 45 52 20 42 59 20 70 65 6e 64 69 6e 67 20 44  DER BY pending D
0d50: 45 53 43 2c 20 6d 74 69 6d 65 20 44 45 53 43 22  ESC, mtime DESC"
0d60: 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  );..  /* Check t
0d70: 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 65 63  o see if the rec
0d80: 6f 72 64 20 3a 72 69 64 20 69 73 20 61 20 63 6f  ord :rid is a co
0d90: 6d 6d 6f 6e 20 61 6e 63 65 73 74 6f 72 2e 20 20  mmon ancestor.  
0da0: 54 68 65 20 72 65 73 75 6c 74 0a 20 20 2a 2a 20  The result.  ** 
0db0: 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65  set contains one
0dc0: 20 6f 72 20 6d 6f 72 65 20 72 6f 77 73 20 69 66   or more rows if
0dd0: 20 69 74 20 69 73 20 61 6e 64 20 69 73 20 74 68   it is and is th
0de0: 65 20 65 6d 70 74 79 20 73 65 74 20 69 66 20 69  e empty set if i
0df0: 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 2e 0a 20  t.  ** is not.. 
0e00: 20 2a 2f 0a 20 20 64 62 5f 70 72 65 70 61 72 65   */.  db_prepare
0e10: 28 26 71 32 2c 0a 20 20 20 20 22 53 45 4c 45 43  (&q2,.    "SELEC
0e20: 54 20 31 20 46 52 4f 4d 20 61 71 75 65 75 65 20  T 1 FROM aqueue 
0e30: 41 2c 20 70 6c 69 6e 6b 2c 20 61 71 75 65 75 65  A, plink, aqueue
0e40: 20 42 22 0a 20 20 20 20 22 20 57 48 45 52 45 20   B".    " WHERE 
0e50: 70 6c 69 6e 6b 2e 70 69 64 3d 3a 72 69 64 22 0a  plink.pid=:rid".
0e60: 20 20 20 20 22 20 20 20 41 4e 44 20 70 6c 69 6e      "   AND plin
0e70: 6b 2e 63 69 64 3d 42 2e 72 69 64 22 0a 20 20 20  k.cid=B.rid".   
0e80: 20 22 20 20 20 41 4e 44 20 41 2e 72 69 64 3d 3a   "   AND A.rid=:
0e90: 72 69 64 22 0a 20 20 20 20 22 20 20 20 41 4e 44  rid".    "   AND
0ea0: 20 41 2e 73 72 63 21 3d 42 2e 73 72 63 22 0a 20   A.src!=B.src". 
0eb0: 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74   );..  /* Mark t
0ec0: 68 65 20 3a 72 69 64 20 72 65 63 6f 72 64 20 68  he :rid record h
0ed0: 61 73 20 68 61 76 69 6e 67 20 62 65 65 6e 20 63  as having been c
0ee0: 68 65 63 6b 65 64 2e 20 20 49 74 20 69 73 20 6e  hecked.  It is n
0ef0: 6f 74 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6d 6d  ot the.  ** comm
0f00: 6f 6e 20 61 6e 63 65 73 74 6f 72 2e 0a 20 20 2a  on ancestor..  *
0f10: 2f 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26  /.  db_prepare(&
0f20: 75 31 2c 0a 20 20 20 20 22 55 50 44 41 54 45 20  u1,.    "UPDATE 
0f30: 61 71 75 65 75 65 20 53 45 54 20 70 65 6e 64 69  aqueue SET pendi
0f40: 6e 67 3d 30 20 57 48 45 52 45 20 72 69 64 3d 3a  ng=0 WHERE rid=:
0f50: 72 69 64 22 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20  rid".  );..  /* 
0f60: 41 64 64 20 74 6f 20 74 68 65 20 71 75 65 75 65  Add to the queue
0f70: 20 61 6c 6c 20 61 6e 63 65 73 74 6f 72 73 20 6f   all ancestors o
0f80: 66 20 3a 72 69 64 2e 0a 20 20 2a 2f 0a 20 20 64  f :rid..  */.  d
0f90: 62 5f 70 72 65 70 61 72 65 28 26 69 31 2c 0a 20  b_prepare(&i1,. 
0fa0: 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47     "INSERT OR IG
0fb0: 4e 4f 52 45 20 49 4e 54 4f 20 61 71 75 65 75 65  NORE INTO aqueue
0fc0: 20 22 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70   ".    "SELECT p
0fd0: 6c 69 6e 6b 2e 70 69 64 2c 22 0a 20 20 20 20 22  link.pid,".    "
0fe0: 20 20 20 20 20 20 20 63 6f 61 6c 65 73 63 65 28         coalesce(
0ff0: 28 53 45 4c 45 43 54 20 6d 74 69 6d 65 20 46 52  (SELECT mtime FR
1000: 4f 4d 20 70 6c 69 6e 6b 20 58 20 57 48 45 52 45  OM plink X WHERE
1010: 20 58 2e 63 69 64 3d 70 6c 69 6e 6b 2e 70 69 64   X.cid=plink.pid
1020: 29 2c 20 30 2e 30 29 2c 22 0a 20 20 20 20 22 20  ), 0.0),".    " 
1030: 20 20 20 20 20 20 31 2c 22 0a 20 20 20 20 22 20        1,".    " 
1040: 20 20 20 20 20 20 61 71 75 65 75 65 2e 73 72 63        aqueue.src
1050: 20 22 0a 20 20 20 20 22 20 20 46 52 4f 4d 20 70   ".    "  FROM p
1060: 6c 69 6e 6b 2c 20 61 71 75 65 75 65 22 0a 20 20  link, aqueue".  
1070: 20 20 22 20 57 48 45 52 45 20 70 6c 69 6e 6b 2e    " WHERE plink.
1080: 63 69 64 3d 3a 72 69 64 22 0a 20 20 20 20 22 20  cid=:rid".    " 
1090: 20 20 41 4e 44 20 61 71 75 65 75 65 2e 72 69 64    AND aqueue.rid
10a0: 3d 3a 72 69 64 22 0a 20 20 29 3b 0a 0a 20 20 77  =:rid".  );..  w
10b0: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71  hile( db_step(&q
10c0: 31 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  1)==SQLITE_ROW )
10d0: 7b 0a 20 20 20 20 72 69 64 20 3d 20 64 62 5f 63  {.    rid = db_c
10e0: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 31 2c 20 30  olumn_int(&q1, 0
10f0: 29 3b 0a 20 20 20 20 64 62 5f 72 65 73 65 74 28  );.    db_reset(
1100: 26 71 31 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e  &q1);.    db_bin
1110: 64 5f 69 6e 74 28 26 71 32 2c 20 22 3a 72 69 64  d_int(&q2, ":rid
1120: 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66 28  ", rid);.    if(
1130: 20 64 62 5f 73 74 65 70 28 26 71 32 29 3d 3d 53   db_step(&q2)==S
1140: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1150: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1160: 0a 20 20 20 20 64 62 5f 72 65 73 65 74 28 26 71  .    db_reset(&q
1170: 32 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f  2);.    db_bind_
1180: 69 6e 74 28 26 69 31 2c 20 22 3a 72 69 64 22 2c  int(&i1, ":rid",
1190: 20 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 65 78   rid);.    db_ex
11a0: 65 63 28 26 69 31 29 3b 0a 20 20 20 20 64 62 5f  ec(&i1);.    db_
11b0: 62 69 6e 64 5f 69 6e 74 28 26 75 31 2c 20 22 3a  bind_int(&u1, ":
11c0: 72 69 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20  rid", rid);.    
11d0: 64 62 5f 65 78 65 63 28 26 75 31 29 3b 0a 20 20  db_exec(&u1);.  
11e0: 20 20 72 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 20    rid = 0;.  }. 
11f0: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 31   db_finalize(&q1
1200: 29 3b 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65  );.  db_finalize
1210: 28 26 71 32 29 3b 0a 20 20 64 62 5f 66 69 6e 61  (&q2);.  db_fina
1220: 6c 69 7a 65 28 26 69 31 29 3b 0a 20 20 64 62 5f  lize(&i1);.  db_
1230: 66 69 6e 61 6c 69 7a 65 28 26 75 31 29 3b 0a 20  finalize(&u1);. 
1240: 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a 0a   return rid;.}..
1250: 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20  /*.** COMMAND:  
1260: 74 65 73 74 2d 66 69 6e 64 2d 70 69 76 6f 74 0a  test-find-pivot.
1270: 2a 2a 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 70  **.** Test the p
1280: 69 76 6f 74 5f 66 69 6e 64 28 29 20 70 72 6f 63  ivot_find() proc
1290: 65 64 75 72 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74  edure..*/.void t
12a0: 65 73 74 5f 66 69 6e 64 5f 70 69 76 6f 74 28 76  est_find_pivot(v
12b0: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72  oid){.  int i, r
12c0: 69 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63  id;.  if( g.argc
12d0: 3c 34 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28  <4 ){.    usage(
12e0: 22 50 52 49 4d 41 52 59 20 53 45 43 4f 4e 44 41  "PRIMARY SECONDA
12f0: 52 59 20 2e 2e 2e 22 29 3b 0a 20 20 7d 0a 20 20  RY ...");.  }.  
1300: 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69  db_must_be_withi
1310: 6e 5f 74 72 65 65 28 29 3b 0a 20 20 70 69 76 6f  n_tree();.  pivo
1320: 74 5f 73 65 74 5f 70 72 69 6d 61 72 79 28 6e 61  t_set_primary(na
1330: 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 67 76  me_to_rid(g.argv
1340: 5b 32 5d 29 29 3b 0a 20 20 66 6f 72 28 69 3d 33  [2]));.  for(i=3
1350: 3b 20 69 3c 67 2e 61 72 67 63 3b 20 69 2b 2b 29  ; i<g.argc; i++)
1360: 7b 0a 20 20 20 20 70 69 76 6f 74 5f 73 65 74 5f  {.    pivot_set_
1370: 73 65 63 6f 6e 64 61 72 79 28 6e 61 6d 65 5f 74  secondary(name_t
1380: 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b 69 5d 29  o_rid(g.argv[i])
1390: 29 3b 0a 20 20 7d 0a 20 20 72 69 64 20 3d 20 70  );.  }.  rid = p
13a0: 69 76 6f 74 5f 66 69 6e 64 28 29 3b 0a 20 20 70  ivot_find();.  p
13b0: 72 69 6e 74 66 28 22 70 69 76 6f 74 3d 25 73 5c  rintf("pivot=%s\
13c0: 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 64 62 5f  n",.         db_
13d0: 74 65 78 74 28 22 3f 22 2c 22 53 45 4c 45 43 54  text("?","SELECT
13e0: 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20   uuid FROM blob 
13f0: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 72 69  WHERE rid=%d",ri
1400: 64 29 0a 20 20 29 3b 0a 7d 0a                    d).  );.}.