# 2023 July 21
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file contains tests for the content= and content_rowid= options.
#

source [file join [file dirname [info script]] fts5_common.tcl]
set testprefix fts5contentless3

# If SQLITE_ENABLE_FTS5 is defined, omit this file.
ifcapable !fts5 {
  finish_test
  return
}

do_execsql_test 1.0 {
  CREATE VIRTUAL TABLE ft USING fts5(x, content=, contentless_delete=1);
  BEGIN;
    INSERT INTO ft VALUES('one one one');
    INSERT INTO ft VALUES('two two two');
    INSERT INTO ft VALUES('three three three');
    INSERT INTO ft VALUES('four four four');
    INSERT INTO ft VALUES('five five five');
    INSERT INTO ft VALUES('six six six');
    INSERT INTO ft VALUES('seven seven seven');
    INSERT INTO ft VALUES('eight eight eight');
    INSERT INTO ft VALUES('nine nine nine');
  COMMIT;

  DELETE FROM ft WHERE rowid=3;
}

proc myhex {hex} { binary decode hex $hex }
db func myhex myhex

do_execsql_test 1.1 {
  UPDATE ft_data SET block = 
    myhex('04000000 00000001'                   ||
          '01020304 01020304 01020304 01020304' ||
          '01020304 01020304 01020304 01020304'
    )
  WHERE id = (SELECT max(id) FROM ft_data);
}

do_execsql_test 1.2 {
  DELETE FROM ft WHERE rowid=1
}

do_execsql_test 1.3 {
  SELECT rowid FROM ft('two');
} {2}

do_execsql_test 1.3 {
  UPDATE ft_data SET block = 
    myhex('08000000 00000001'                                               ||
      '0000000001020304 0000000001020304 0000000001020304 0000000001020304' ||
      '0000000001020304 0000000001020304 0000000001020304 0000000001020304'
    )
  WHERE id = (SELECT max(id) FROM ft_data);
}

do_execsql_test 1.4 {
  SELECT rowid FROM ft('two');
} {2}

do_execsql_test 1.5 {
  DELETE FROM ft WHERE rowid=4
}

do_execsql_test 1.6 {
  UPDATE ft_data SET block = myhex('04000000 00000000')
    WHERE id = (SELECT max(id) FROM ft_data);
}
do_execsql_test 1.7 {
  SELECT rowid FROM ft('two');
} {2}

do_execsql_test 1.8 {
  UPDATE ft_data SET block = myhex('04000000 00000000')
    WHERE id = (SELECT max(id) FROM ft_data);
}
do_execsql_test 1.9 {
  DELETE FROM ft WHERE rowid=8
} {}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 2.0 {
  CREATE VIRTUAL TABLE ft USING fts5(x, content=, contentless_delete=1);
  INSERT INTO ft VALUES('one one one');
  INSERT INTO ft VALUES('two two two');
  INSERT INTO ft VALUES('three three three');
  INSERT INTO ft VALUES('four four four');
  INSERT INTO ft VALUES('five five five');
  INSERT INTO ft VALUES('six six six');
  INSERT INTO ft VALUES('seven seven seven');
  INSERT INTO ft VALUES('eight eight eight');
  INSERT INTO ft VALUES('nine nine nine');
}

do_execsql_test 2.1 {
  INSERT INTO ft(ft) VALUES('optimize');
}
do_execsql_test 2.2 {
  SELECT count(*) FROM ft_data
} {3}
do_execsql_test 2.3 {
  DELETE FROM ft WHERE rowid=5
}
do_execsql_test 2.4 {
  SELECT count(*) FROM ft_data
} {4}

# Check that an 'optimize' works (rewrites the index) if there is a single
# segment with one or more tombstone hash pages.
do_execsql_test 2.5 {
  INSERT INTO ft(ft) VALUES('optimize');
}
do_execsql_test 2.6 {
  SELECT count(*) FROM ft_data
} {3}

# Check that an 'optimize' is a no-op if there is a single segment
# and no tombstone hash pages.
do_execsql_test 2.7 {
  INSERT INTO ft(ft) VALUES('optimize');
  SELECT rowid FROM ft_data;
} [db eval {SELECT rowid FROM ft_data}]

#-------------------------------------------------------------------------
reset_db
do_execsql_test 3.0 {
  CREATE VIRTUAL TABLE ft USING fts5(x, content=, contentless_delete=1);
  INSERT INTO ft(ft, rank) VALUES('pgsz', 64);
  WITH s(i) AS (
    SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000
  )
  INSERT INTO ft(rowid, x) SELECT i, i||' '||i||' '||i||' '||i FROM s;
  INSERT INTO ft(ft) VALUES('optimize');
}

do_execsql_test 3.1 {
  SELECT count(*) FROM ft_data
} {200}

do_execsql_test 3.2 {
  DELETE FROM ft WHERE (rowid % 50)==0;
  SELECT count(*) FROM ft_data;
} {203}

do_execsql_test 3.3 {
  INSERT INTO ft(ft, rank) VALUES('merge', 500);
  SELECT rowid FROM ft_data;
} [db eval {SELECT rowid FROM ft_data}]

do_execsql_test 3.4 {
  INSERT INTO ft(ft, rank) VALUES('merge', -1000);
  SELECT count(*) FROM ft_data;
} {197}

do_execsql_test 3.5 {
  DELETE FROM ft WHERE (rowid % 50)==1;
  SELECT count(*) FROM ft_data;
} {200}

do_execsql_test 3.6 {
  SELECT level, segment, npgtombstone FROM fts5_structure(
    (SELECT block FROM ft_data WHERE id=10)
  )
} {1 0 3}

do_test 3.6 {
  while 1 {
    set nChange [db total_changes]
    execsql { INSERT INTO ft(ft, rank) VALUES('merge', -5) }
    if {([db total_changes] - $nChange)<2} break
  }
} {}

do_execsql_test 3.7 {
  SELECT level, segment, npgtombstone FROM fts5_structure(
    (SELECT block FROM ft_data WHERE id=10)
  )
} {2 0 0}


finish_test
