Hex Artifact Content
Not logged in

Artifact b19f3743af69cd76e5c35a397fcace602b505df7:

File src/pivot.c part of check-in [5602bbbaff] - Fix a bug in the logic for finding a pivot during a merge. by drh on 2007-07-30 17:05:25.

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 4f 52 20 49 47 4e 4f 52 45 20 49  SERT OR IGNORE I
0a20: 4e 54 4f 20 61 71 75 65 75 65 28 72 69 64 2c 20  NTO aqueue(rid, 
0a30: 6d 74 69 6d 65 2c 20 70 65 6e 64 69 6e 67 2c 20  mtime, pending, 
0a40: 73 72 63 29 22 0a 20 20 20 20 22 20 20 53 45 4c  src)".    "  SEL
0a50: 45 43 54 20 25 64 2c 20 6d 74 69 6d 65 2c 20 31  ECT %d, mtime, 1
0a60: 2c 20 30 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 57  , 0 FROM plink W
0a70: 48 45 52 45 20 63 69 64 3d 25 64 22 2c 0a 20 20  HERE cid=%d",.  
0a80: 20 20 72 69 64 2c 20 72 69 64 0a 20 20 29 3b 0a    rid, rid.  );.
0a90: 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  }../*.** Find th
0aa0: 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 6f  e most recent co
0ab0: 6d 6d 6f 6e 20 61 6e 63 65 73 74 6f 72 20 6f 66  mmon ancestor of
0ac0: 20 74 68 65 20 70 72 69 6d 61 72 79 20 61 6e 64   the primary and
0ad0: 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 73   one of.** the s
0ae0: 65 63 6f 6e 64 61 72 69 65 73 2e 20 20 52 65 74  econdaries.  Ret
0af0: 75 72 6e 20 69 74 73 20 72 69 64 2e 20 20 52 65  urn its rid.  Re
0b00: 74 75 72 6e 20 30 20 69 66 20 6e 6f 20 63 6f 6d  turn 0 if no com
0b10: 6d 6f 6e 20 61 6e 63 65 73 74 6f 72 0a 2a 2a 20  mon ancestor.** 
0b20: 63 61 6e 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f  can be found..*/
0b30: 0a 69 6e 74 20 70 69 76 6f 74 5f 66 69 6e 64 28  .int pivot_find(
0b40: 76 6f 69 64 29 7b 0a 20 20 53 74 6d 74 20 71 31  void){.  Stmt q1
0b50: 2c 20 71 32 2c 20 75 31 2c 20 69 31 3b 0a 20 20  , q2, u1, i1;.  
0b60: 69 6e 74 20 72 69 64 3b 0a 20 20 0a 20 20 2f 2a  int rid;.  .  /*
0b70: 20 61 71 75 65 75 65 20 6d 75 73 74 20 63 6f 6e   aqueue must con
0b80: 74 61 69 6e 20 61 74 20 6c 65 61 73 74 20 6f 6e  tain at least on
0b90: 65 20 70 72 69 6d 61 72 79 20 61 6e 64 20 6f 6e  e primary and on
0ba0: 65 20 6f 74 68 65 72 2e 20 20 4f 74 68 65 72 77  e other.  Otherw
0bb0: 69 73 65 0a 20 20 2a 2a 20 77 65 20 61 62 6f 72  ise.  ** we abor
0bc0: 74 20 65 61 72 6c 79 0a 20 20 2a 2f 0a 20 20 69  t early.  */.  i
0bd0: 66 28 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45  f( db_int(0, "SE
0be0: 4c 45 43 54 20 63 6f 75 6e 74 28 64 69 73 74 69  LECT count(disti
0bf0: 6e 63 74 20 73 72 63 29 20 46 52 4f 4d 20 61 71  nct src) FROM aq
0c00: 75 65 75 65 22 29 3c 32 20 29 7b 0a 20 20 20 20  ueue")<2 ){.    
0c10: 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 6c 61  fossil_panic("la
0c20: 63 6b 20 62 6f 74 68 20 70 72 69 6d 61 72 79 20  ck both primary 
0c30: 61 6e 64 20 73 65 63 6f 6e 64 61 72 79 20 66 69  and secondary fi
0c40: 6c 65 73 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  les");.  }..  /*
0c50: 20 50 72 65 70 61 72 65 20 71 75 65 72 69 65 73   Prepare queries
0c60: 20 77 65 20 77 69 6c 6c 20 62 65 20 6e 65 65 64   we will be need
0c70: 69 6e 67 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ing.  **.  ** Th
0c80: 65 20 66 69 72 73 74 20 71 75 65 72 79 20 66 69  e first query fi
0c90: 6e 64 73 20 74 68 65 20 6f 6c 64 65 73 74 20 70  nds the oldest p
0ca0: 65 6e 64 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f  ending version o
0cb0: 6e 20 74 68 65 20 61 71 75 65 75 65 2e 20 20 54  n the aqueue.  T
0cc0: 68 69 73 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65  his.  ** will be
0cd0: 20 6e 65 78 74 20 6f 6e 65 20 73 65 61 72 63 68   next one search
0ce0: 65 64 2e 0a 20 20 2a 2f 0a 20 20 64 62 5f 70 72  ed..  */.  db_pr
0cf0: 65 70 61 72 65 28 26 71 31 2c 20 22 53 45 4c 45  epare(&q1, "SELE
0d00: 43 54 20 72 69 64 20 46 52 4f 4d 20 61 71 75 65  CT rid FROM aque
0d10: 75 65 20 57 48 45 52 45 20 70 65 6e 64 69 6e 67  ue WHERE pending
0d20: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
0d30: 20 20 20 20 22 20 4f 52 44 45 52 20 42 59 20 70      " ORDER BY p
0d40: 65 6e 64 69 6e 67 20 44 45 53 43 2c 20 6d 74 69  ending DESC, mti
0d50: 6d 65 20 44 45 53 43 22 29 3b 0a 0a 20 20 2f 2a  me DESC");..  /*
0d60: 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
0d70: 20 74 68 65 20 72 65 63 6f 72 64 20 3a 72 69 64   the record :rid
0d80: 20 69 73 20 61 20 63 6f 6d 6d 6f 6e 20 61 6e 63   is a common anc
0d90: 65 73 74 6f 72 2e 20 20 54 68 65 20 72 65 73 75  estor.  The resu
0da0: 6c 74 0a 20 20 2a 2a 20 73 65 74 20 63 6f 6e 74  lt.  ** set cont
0db0: 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ains one or more
0dc0: 20 72 6f 77 73 20 69 66 20 69 74 20 69 73 20 61   rows if it is a
0dd0: 6e 64 20 69 73 20 74 68 65 20 65 6d 70 74 79 20  nd is the empty 
0de0: 73 65 74 20 69 66 20 69 74 0a 20 20 2a 2a 20 69  set if it.  ** i
0df0: 73 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 64 62  s not..  */.  db
0e00: 5f 70 72 65 70 61 72 65 28 26 71 32 2c 0a 20 20  _prepare(&q2,.  
0e10: 20 20 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d    "SELECT 1 FROM
0e20: 20 61 71 75 65 75 65 20 41 2c 20 70 6c 69 6e 6b   aqueue A, plink
0e30: 2c 20 61 71 75 65 75 65 20 42 22 0a 20 20 20 20  , aqueue B".    
0e40: 22 20 57 48 45 52 45 20 70 6c 69 6e 6b 2e 70 69  " WHERE plink.pi
0e50: 64 3d 3a 72 69 64 22 0a 20 20 20 20 22 20 20 20  d=:rid".    "   
0e60: 41 4e 44 20 70 6c 69 6e 6b 2e 63 69 64 3d 42 2e  AND plink.cid=B.
0e70: 72 69 64 22 0a 20 20 20 20 22 20 20 20 41 4e 44  rid".    "   AND
0e80: 20 41 2e 72 69 64 3d 3a 72 69 64 22 0a 20 20 20   A.rid=:rid".   
0e90: 20 22 20 20 20 41 4e 44 20 41 2e 73 72 63 21 3d   "   AND A.src!=
0ea0: 42 2e 73 72 63 22 0a 20 20 29 3b 0a 0a 20 20 2f  B.src".  );..  /
0eb0: 2a 20 4d 61 72 6b 20 74 68 65 20 3a 72 69 64 20  * Mark the :rid 
0ec0: 72 65 63 6f 72 64 20 68 61 73 20 68 61 76 69 6e  record has havin
0ed0: 67 20 62 65 65 6e 20 63 68 65 63 6b 65 64 2e 20  g been checked. 
0ee0: 20 49 74 20 69 73 20 6e 6f 74 20 74 68 65 0a 20   It is not the. 
0ef0: 20 2a 2a 20 63 6f 6d 6d 6f 6e 20 61 6e 63 65 73   ** common ances
0f00: 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 64 62 5f 70  tor..  */.  db_p
0f10: 72 65 70 61 72 65 28 26 75 31 2c 0a 20 20 20 20  repare(&u1,.    
0f20: 22 55 50 44 41 54 45 20 61 71 75 65 75 65 20 53  "UPDATE aqueue S
0f30: 45 54 20 70 65 6e 64 69 6e 67 3d 30 20 57 48 45  ET pending=0 WHE
0f40: 52 45 20 72 69 64 3d 3a 72 69 64 22 0a 20 20 29  RE rid=:rid".  )
0f50: 3b 0a 0a 20 20 2f 2a 20 41 64 64 20 74 6f 20 74  ;..  /* Add to t
0f60: 68 65 20 71 75 65 75 65 20 61 6c 6c 20 61 6e 63  he queue all anc
0f70: 65 73 74 6f 72 73 20 6f 66 20 3a 72 69 64 2e 0a  estors of :rid..
0f80: 20 20 2a 2f 0a 20 20 64 62 5f 70 72 65 70 61 72    */.  db_prepar
0f90: 65 28 26 69 31 2c 0a 20 20 20 20 22 49 4e 53 45  e(&i1,.    "INSE
0fa0: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54  RT OR IGNORE INT
0fb0: 4f 20 61 71 75 65 75 65 20 22 0a 20 20 20 20 22  O aqueue ".    "
0fc0: 53 45 4c 45 43 54 20 70 6c 69 6e 6b 2e 70 69 64  SELECT plink.pid
0fd0: 2c 22 0a 20 20 20 20 22 20 20 20 20 20 20 20 63  ,".    "       c
0fe0: 6f 61 6c 65 73 63 65 28 28 53 45 4c 45 43 54 20  oalesce((SELECT 
0ff0: 6d 74 69 6d 65 20 46 52 4f 4d 20 70 6c 69 6e 6b  mtime FROM plink
1000: 20 58 20 57 48 45 52 45 20 58 2e 63 69 64 3d 70   X WHERE X.cid=p
1010: 6c 69 6e 6b 2e 70 69 64 29 2c 20 30 2e 30 29 2c  link.pid), 0.0),
1020: 22 0a 20 20 20 20 22 20 20 20 20 20 20 20 31 2c  ".    "       1,
1030: 22 0a 20 20 20 20 22 20 20 20 20 20 20 20 61 71  ".    "       aq
1040: 75 65 75 65 2e 73 72 63 20 22 0a 20 20 20 20 22  ueue.src ".    "
1050: 20 20 46 52 4f 4d 20 70 6c 69 6e 6b 2c 20 61 71    FROM plink, aq
1060: 75 65 75 65 22 0a 20 20 20 20 22 20 57 48 45 52  ueue".    " WHER
1070: 45 20 70 6c 69 6e 6b 2e 63 69 64 3d 3a 72 69 64  E plink.cid=:rid
1080: 22 0a 20 20 20 20 22 20 20 20 41 4e 44 20 61 71  ".    "   AND aq
1090: 75 65 75 65 2e 72 69 64 3d 3a 72 69 64 22 0a 20  ueue.rid=:rid". 
10a0: 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 64 62   );..  while( db
10b0: 5f 73 74 65 70 28 26 71 31 29 3d 3d 53 51 4c 49  _step(&q1)==SQLI
10c0: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 69  TE_ROW ){.    ri
10d0: 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  d = db_column_in
10e0: 74 28 26 71 31 2c 20 30 29 3b 0a 20 20 20 20 64  t(&q1, 0);.    d
10f0: 62 5f 72 65 73 65 74 28 26 71 31 29 3b 0a 20 20  b_reset(&q1);.  
1100: 20 20 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26 71    db_bind_int(&q
1110: 32 2c 20 22 3a 72 69 64 22 2c 20 72 69 64 29 3b  2, ":rid", rid);
1120: 0a 20 20 20 20 69 66 28 20 64 62 5f 73 74 65 70  .    if( db_step
1130: 28 26 71 32 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  (&q2)==SQLITE_RO
1140: 57 20 29 7b 0a 20 20 20 20 20 20 20 62 72 65 61  W ){.       brea
1150: 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f  k;.    }.    db_
1160: 72 65 73 65 74 28 26 71 32 29 3b 0a 20 20 20 20  reset(&q2);.    
1170: 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26 69 31 2c  db_bind_int(&i1,
1180: 20 22 3a 72 69 64 22 2c 20 72 69 64 29 3b 0a 20   ":rid", rid);. 
1190: 20 20 20 64 62 5f 65 78 65 63 28 26 69 31 29 3b     db_exec(&i1);
11a0: 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 69 6e 74  .    db_bind_int
11b0: 28 26 75 31 2c 20 22 3a 72 69 64 22 2c 20 72 69  (&u1, ":rid", ri
11c0: 64 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28  d);.    db_exec(
11d0: 26 75 31 29 3b 0a 20 20 20 20 72 69 64 20 3d 20  &u1);.    rid = 
11e0: 30 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  0;.  }.  db_fina
11f0: 6c 69 7a 65 28 26 71 31 29 3b 0a 20 20 64 62 5f  lize(&q1);.  db_
1200: 66 69 6e 61 6c 69 7a 65 28 26 71 32 29 3b 0a 20  finalize(&q2);. 
1210: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 69 31   db_finalize(&i1
1220: 29 3b 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65  );.  db_finalize
1230: 28 26 75 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  (&u1);.  return 
1240: 72 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  rid;.}../*.** CO
1250: 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 66 69 6e  MMAND:  test-fin
1260: 64 2d 70 69 76 6f 74 0a 2a 2a 0a 2a 2a 20 54 65  d-pivot.**.** Te
1270: 73 74 20 74 68 65 20 70 69 76 6f 74 5f 66 69 6e  st the pivot_fin
1280: 64 28 29 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a  d() procedure..*
1290: 2f 0a 76 6f 69 64 20 74 65 73 74 5f 66 69 6e 64  /.void test_find
12a0: 5f 70 69 76 6f 74 28 76 6f 69 64 29 7b 0a 20 20  _pivot(void){.  
12b0: 69 6e 74 20 69 2c 20 72 69 64 3b 0a 20 20 69 66  int i, rid;.  if
12c0: 28 20 67 2e 61 72 67 63 3c 34 20 29 7b 0a 20 20  ( g.argc<4 ){.  
12d0: 20 20 75 73 61 67 65 28 22 50 52 49 4d 41 52 59    usage("PRIMARY
12e0: 20 53 45 43 4f 4e 44 41 52 59 20 2e 2e 2e 22 29   SECONDARY ...")
12f0: 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 73 74 5f  ;.  }.  db_must_
1300: 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29  be_within_tree()
1310: 3b 0a 20 20 70 69 76 6f 74 5f 73 65 74 5f 70 72  ;.  pivot_set_pr
1320: 69 6d 61 72 79 28 6e 61 6d 65 5f 74 6f 5f 72 69  imary(name_to_ri
1330: 64 28 67 2e 61 72 67 76 5b 32 5d 29 29 3b 0a 20  d(g.argv[2]));. 
1340: 20 66 6f 72 28 69 3d 33 3b 20 69 3c 67 2e 61 72   for(i=3; i<g.ar
1350: 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 69  gc; i++){.    pi
1360: 76 6f 74 5f 73 65 74 5f 73 65 63 6f 6e 64 61 72  vot_set_secondar
1370: 79 28 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e  y(name_to_rid(g.
1380: 61 72 67 76 5b 69 5d 29 29 3b 0a 20 20 7d 0a 20  argv[i]));.  }. 
1390: 20 72 69 64 20 3d 20 70 69 76 6f 74 5f 66 69 6e   rid = pivot_fin
13a0: 64 28 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 70  d();.  printf("p
13b0: 69 76 6f 74 3d 25 73 5c 6e 22 2c 0a 20 20 20 20  ivot=%s\n",.    
13c0: 20 20 20 20 20 64 62 5f 74 65 78 74 28 22 3f 22       db_text("?"
13d0: 2c 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52  ,"SELECT uuid FR
13e0: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69  OM blob WHERE ri
13f0: 64 3d 25 64 22 2c 72 69 64 29 0a 20 20 29 3b 0a  d=%d",rid).  );.
1400: 7d 0a                                            }.