
' DeleteKeyTest
' Test code to investigate what happens when all Keys are deleted
' from a chain of LookupNodes.

' This code can be used with Cheetah DLL's that have either full
' database support compiled into it, or with DLL's that only have
' the index routines compiled in them.

' This demo uses the FreeBASIC (0.24) Console compiler.


#Include Once "..\..\include\Cheetah4_FreeBasic.bi"
#Include Once "vbcompat.bi"


   Dim idxHandle As Long
   Dim tagHandle1 As Long
   
   Dim errcode   As Long
   Dim sData     As String
   Dim sKey      As String
   Dim nRecNum   As Long
   Dim NumRead   As Long
   Dim NumRecs   As Long
   Dim x         As Long
   Dim StartTime As Single
   Dim EndTime   As Single
                            
   Cls
   
   sData = "disk = deletekeytest.btx;"  
   errcode = xdbCreateIndex( sData ) 
   If errcode Then GoTo ExitOut

   ' In order to use the index without an associated database, you
   ' must specify the "NoDatabase" property and set it to "TRUE".
   sData = "disk=deletekeytest.btx; nodatabase=true;" 
   errcode = xdbOpenIndex( sData, 0, idxHandle ) 
   If errcode Then GoTo exitout

   Print "Create/open index. Handle:"; Str$(idxHandle)
   

   ' CREATE AN INDEX TAG
   ' When you create a Tag, it is your responsibility to specify
   ' the size of the keys.
   sData = "TagName=mytag; Size=50; Uppercase=TRUE"
   errcode = xdbCreateTag( sData, idxHandle ) 
   If errcode Then GoTo exitout

   errcode = xdbGetTag( "mytag", idxHandle, tagHandle1 ) 
   If errcode Then GoTo exitout

   Print "GetTag. 'mytag' Handle:"; Str$(tagHandle1)
   
   
   NumRecs = 100
   
   Print
   Print "Adding "; Format$(NumRecs); " Index keys and values"

   StartTime = Timer
   
   ' ADD INDEX KEYS 
   For x = 1 To NumRecs             
      sKey    = Format$(Int(Rnd(1) * 999999) + 100000)
      nRecNum = x
      If xdbAddKey( tagHandle1, sKey, nRecNum ) Then GoTo exitout
   Next
   
   EndTime = Timer
   
   Print Format$(NumRecs); " Index keys and values added. Time: "; Format$(EndTime-StartTime, "0.000"); " seconds."
   Print
   Print "Press any key to delete the keys, one by one..."
   Sleep
   
   
   ' DELETE ALL OF THE KEYS ONE BY ONE
   NumRead = 0
   Do
      errcode = xdbMove( 0, tagHandle1, XDB_MOVEFIRST, nRecNum )
      If errcode Then Exit Do

      errcode = xdbGetKey( tagHandle1, sKey, nRecNum )
      If errcode Then 
         Print "GetKey error:" & Str$(errcode)
         GoTo ExitOut
      End If   
      
      
      errcode = xdbDeleteKey( tagHandle1, sKey, nRecNum )
      If errcode Then 
         Print "DeleteKey error:" & Str$(errcode)
         GoTo ExitOut
      Else                           
         NumRead = NumRead + 1  
         Print Str$(NumRead); "  Deleted Key:"; Left$(sKey,6); "  nRecNum:"; Str$(nRecNum)
      End If   
      
   Loop
   
   Print
   Print Str$(NumRead); " keys deleted"

   
ExitOut:

   
' CLOSE THE INDEX 
   xdbCloseIndex idxHandle

' SHUTDOWN THE SYSTEM 
   xdbShutdown

   Print
   Print "Completed"
   
   Sleep
        

