<commit_before>//
// Copyright (c) XSharp B.V.  All Rights Reserved.
// Licensed under the Apache License, Version 2.0.
// See License.txt in the project root for license information.
//


USING System
USING System.Collections.Generic
USING System.Text
USING XSharp.RDD
USING XSharp.RDD.Support
USING System.IO
/// <summary>Advantage Index support.</summary>
CLASS XSharp.ADS.ADSIndex INHERIT BaseIndex
    PRIVATE oRDD AS ADSRDD
    PRIVATE _CallbackFn AS CallbackFn
    PRIVATE _iProgress  AS LONG
    #region Helpers

    PROPERTY OrderCondInfo AS DbOrderCondInfo GET oRDD:OrderCondInfo SET oRDD:OrderCondInfo := value
    PROPERTY Index AS IntPtr GET oRDD:_Index SET oRDD:_Index := value
    PROPERTY Table AS IntPtr GET oRDD:_Table
    PROPERTY BagName   AS STRING AUTO GET PRIVATE SET
    PROPERTY OrderName AS STRING AUTO GET PRIVATE SET

    PRIVATE METHOD _CheckError(nResult AS DWORD, gencode AS DWORD) AS LOGIC
        IF nResult != 0
            LOCAL message AS CHAR[]
            LOCAL wBufLen AS WORD
            LOCAL oError AS RddError
            LOCAL strMessage AS STRING
            message := CHAR[]{ACE.ADS_MAX_ERROR_LEN}
            wBufLen := (WORD) message:Length
            IF ACE.AdsGetLastError(OUT VAR lastError, message, REF wBufLen) == 0 .AND. lastError != 0 .AND. wBufLen > 0
                strMessage := STRING{message, 0, wBufLen}
            ELSE
                strMessage := "Unknown Error"
            ENDIF
            oError   := AdsError{strMessage, gencode, lastError, oRDD:Driver , ES_ERROR, ProcName(1), SELF:BagName}
            RuntimeState.LastRddError := oError
            THROW oError
        ENDIF
        RETURN TRUE

        #endregion
    /// <inheritdoc />
    CONSTRUCTOR(oArea AS Workarea)
        SUPER(oArea)
        SELF:oRDD := (ADSRDD) oArea


        /// <inheritdoc />
    VIRTUAL METHOD OrderCondition(info AS DbOrderCondInfo ) AS LOGIC
        SELF:OrderCondInfo := info
        RETURN TRUE

        /// <inheritdoc />
    VIRTUAL METHOD OrderCreate(info AS DbOrderCreateInfo ) AS LOGIC
        LOCAL cCondition AS STRING
        LOCAL nonAdditive AS LOGIC
        LOCAL mustEval  AS LOGIC
        LOCAL mode AS DWORD
        LOCAL cTag AS STRING
        LOCAL fileNameWithoutExtension AS STRING
        LOCAL result AS DWORD
        //
        cCondition := String.Empty
        nonAdditive := TRUE
        mustEval := FALSE
        SELF:oRDD:_SynchronizeSettings()
        mode := 0u
        IF info:Unique
            mode |= ACE.ADS_UNIQUE
        ENDIF
        IF SELF:OrderCondInfo != NULL
            cCondition := SELF:OrderCondInfo:ForExpression
            IF SELF:OrderCondInfo:Custom
                mode |= ACE.ADS_CUSTOM
            ENDIF
            IF SELF:OrderCondInfo:Descending
                mode |= ACE.ADS_DESCENDING
            ENDIF
            nonAdditive := SELF:OrderCondInfo:Additive
            mustEval    := SELF:OrderCondInfo:EvalBlock != NULL
        ENDIF
        IF nonAdditive
            SELF:OrderListDelete(NULL)
        ENDIF
        IF mustEval
            IF SELF:_CallbackFn == NULL
                SELF:_CallbackFn := ProgressCallback
            ENDIF
            ACE.AdsRegisterCallbackFunction(SELF:_CallbackFn, 0)
        ENDIF
        LOCAL hIndex AS IntPtr
        SELF:BagName := info:BagName
        IF info:Order IS STRING
            cTag := (STRING) info:Order
            IF String.IsNullOrEmpty(SELF:BagName)
                SELF:BagName := cTag
            ENDIF
            mode |= ACE.ADS_COMPOUND
            result := ACE.AdsCreateIndex90(SELF:Table, info:BagName, cTag, info:Expression, cCondition, NULL, mode, 0, SELF:oRDD:_Collation, OUT hIndex)
        ELSE
            IF SELF:oRDD:_TableType == ACE.ADS_NTX
                result := ACE.AdsCreateIndex(SELF:Table, info:BagName, NULL, info:Expression, cCondition, NULL, mode, OUT hIndex)
            ELSE
                fileNameWithoutExtension := Path.GetFileNameWithoutExtension(info:BagName)
                mode |= ACE.ADS_COMPOUND
                result := ACE.AdsCreateIndex90(SELF:Table, info:BagName, fileNameWithoutExtension, info:Expression, cCondition, NULL, mode, 0u, SELF:oRDD:_Collation, OUT hIndex)
            ENDIF
        ENDIF
        SELF:Index := hIndex
        SELF:_ReadNames(hIndex)
        IF mustEval
            ACE.AdsClearProgressCallback()
            SELF:_CallbackFn := NULL
        ENDIF
        SELF:_CheckError(result,EG_CREATE)
        RETURN SELF:oRDD:GoTop()

        /// <inheritdoc />
    VIRTUAL METHOD OrderDestroy(info AS DbOrderInfo ) AS LOGIC
        LOCAL hIndex AS IntPtr
        hIndex := SELF:_FindIndex(info,"OrderDestroy")
        IF hIndex != IntPtr.Zero
            SELF:_CheckError(ACE.AdsDeleteIndex(hIndex), EG_CLOSE)<commit_msg>Fixes #794. I wonder why this never caused a problem for Vulcan Advantage users ?<commit_after>//
// Copyright (c) XSharp B.V.  All Rights Reserved.
// Licensed under the Apache License, Version 2.0.
// See License.txt in the project root for license information.
//


USING System
USING System.Collections.Generic
USING System.Text
USING XSharp.RDD
USING XSharp.RDD.Support
USING System.IO
/// <summary>Advantage Index support.</summary>
CLASS XSharp.ADS.ADSIndex INHERIT BaseIndex
    PRIVATE oRDD AS ADSRDD
    PRIVATE _CallbackFn AS CallbackFn
    PRIVATE _iProgress  AS LONG
    #region Helpers

    PROPERTY OrderCondInfo AS DbOrderCondInfo GET oRDD:OrderCondInfo SET oRDD:OrderCondInfo := value
    PROPERTY Index AS IntPtr GET oRDD:_Index SET oRDD:_Index := value
    PROPERTY Table AS IntPtr GET oRDD:_Table
    PROPERTY BagName   AS STRING AUTO GET PRIVATE SET
    PROPERTY OrderName AS STRING AUTO GET PRIVATE SET

    PRIVATE METHOD _CheckError(nResult AS DWORD, gencode AS DWORD) AS LOGIC
        IF nResult != 0
            LOCAL message AS CHAR[]
            LOCAL wBufLen AS WORD
            LOCAL oError AS RddError
            LOCAL strMessage AS STRING
            message := CHAR[]{ACE.ADS_MAX_ERROR_LEN}
            wBufLen := (WORD) message:Length
            IF ACE.AdsGetLastError(OUT VAR lastError, message, REF wBufLen) == 0 .AND. lastError != 0 .AND. wBufLen > 0
                strMessage := STRING{message, 0, wBufLen}
            ELSE
                strMessage := "Unknown Error"
            ENDIF
            oError   := AdsError{strMessage, gencode, lastError, oRDD:Driver , ES_ERROR, ProcName(1), SELF:BagName}
            RuntimeState.LastRddError := oError
            THROW oError
        ENDIF
        RETURN TRUE

        #endregion
    /// <inheritdoc />
    CONSTRUCTOR(oArea AS Workarea)
        SUPER(oArea)
        SELF:oRDD := (ADSRDD) oArea


        /// <inheritdoc />
    VIRTUAL METHOD OrderCondition(info AS DbOrderCondInfo ) AS LOGIC
        SELF:OrderCondInfo := info
        RETURN TRUE

        /// <inheritdoc />
    VIRTUAL METHOD OrderCreate(info AS DbOrderCreateInfo ) AS LOGIC
        LOCAL cCondition AS STRING
        LOCAL nonAdditive AS LOGIC
        LOCAL mustEval  AS LOGIC
        LOCAL mode AS DWORD
        LOCAL cTag AS STRING
        LOCAL fileNameWithoutExtension AS STRING
        LOCAL result AS DWORD
        //
        cCondition := String.Empty
        nonAdditive := TRUE
        mustEval := FALSE
        SELF:oRDD:_SynchronizeSettings()
        mode := 0u
        IF info:Unique
            mode |= ACE.ADS_UNIQUE
        ENDIF
        IF SELF:OrderCondInfo != NULL
            cCondition := SELF:OrderCondInfo:ForExpression
            IF SELF:OrderCondInfo:Custom
                mode |= ACE.ADS_CUSTOM
            ENDIF
            IF SELF:OrderCondInfo:Descending
                mode |= ACE.ADS_DESCENDING
            ENDIF
            nonAdditive := SELF:OrderCondInfo:Additive
            mustEval    := SELF:OrderCondInfo:EvalBlock != NULL
        ENDIF
        IF nonAdditive
            SELF:OrderListDelete(NULL)
        ENDIF
        IF mustEval
            IF SELF:_CallbackFn == NULL
                SELF:_CallbackFn := ProgressCallback
            ENDIF
            ACE.AdsRegisterCallbackFunction(SELF:_CallbackFn, 0)
        ENDIF
        LOCAL hIndex AS IntPtr
        SELF:BagName := info:BagName
        IF info:Order IS STRING
            cTag := (STRING) info:Order
            IF String.IsNullOrEmpty(SELF:BagName)
                SELF:BagName := cTag
            ENDIF
            mode |= ACE.ADS_COMPOUND
            result := ACE.AdsCreateIndex90(SELF:Table, info:BagName, cTag, info:Expression, cCondition, NULL, mode, 0, SELF:oRDD:_Collation, OUT hIndex)
        ELSE
            IF SELF:oRDD:_TableType == ACE.ADS_NTX
                result := ACE.AdsCreateIndex(SELF:Table, info:BagName, NULL, info:Expression, cCondition, NULL, mode, OUT hIndex)
            ELSE
                fileNameWithoutExtension := Path.GetFileNameWithoutExtension(info:BagName)
                mode |= ACE.ADS_COMPOUND
                result := ACE.AdsCreateIndex90(SELF:Table, info:BagName, fileNameWithoutExtension, info:Expression, cCondition, NULL, mode, 0u, SELF:oRDD:_Collation, OUT hIndex)
            ENDIF
        ENDIF
        SELF:Index := hIndex
        SELF:_ReadNames(hIndex)
        IF mustEval
            ACE.AdsClearCallbackFunction()
            SELF:_CallbackFn := NULL
        ENDIF
        SELF:_CheckError(result,EG_CREATE)
        RETURN SELF:oRDD:GoTop()

        /// <inheritdoc />
    VIRTUAL METHOD OrderDestroy(info AS DbOrderInfo ) AS LOGIC
        LOCAL hIndex AS IntPtr
        hIndex := SELF:_FindIndex(info,"OrderDestroy")
        IF hIndex != IntPtr.Zero
            SELF:_CheckError(ACE.AdsDeleteIndex(hIndex), EG_CLOSE)
{'fraction_non_alphanumeric': 0.07081946755407655, 'fraction_numerical': 0.0036397670549084857, 'mean_word_length': 2.1264629388816645, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5383084', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2759, 'n_words': 757}