Files
five/hbrdd/dbf/indexer.go
Charles KWON OhJun 59568f3301 Five v0.9 — Harbour + Go fusion language
- Compiler: PP → Lexer → Parser → Analyzer → Gengo pipeline
- Parser: 232/236 (98%) Harbour compatibility, registry-based dispatch
- RTL: 351 Harbour-compatible functions
- RDD: DBF/NTX/CDX engines with Rushmore bitmap optimization
- Go Interop: IMPORT + pkg.Func() + obj:Method() with FastPath (15M calls/sec)
- HB_FUNC API: Full Harbour C API compatible Go bridge
- Concurrency: SPAWN/LAUNCH/GOROUTINE, <-, WATCH, PARALLEL FOR, ASYNC/AWAIT
- Extensions: Multi-return, DEFER, Slice, f-string, Nil-safe ?:, CONST
- Macro Compiler: Runtime AST parsing and evaluation
- Debugger: TUI debugger with source display, breakpoints, stepping
- FRB: Native + Pcode dual mode runtime binary
- Tests: 13 packages ALL PASS

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 09:41:50 +09:00

645 lines
16 KiB
Go

// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
// All rights reserved.
// DBFArea Indexer integration — connects NTX/CDX index engines to DBFArea.
// Implements hbrdd.Indexer interface on DBFArea.
package dbf
import (
"bytes"
"five/hbrt"
"five/hbrdd"
"five/hbrdd/ntx"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
// indexState holds active index state for a DBFArea.
type indexState struct {
indexes []*ntx.Index // open NTX index files
names []string // index file paths
tags []string // tag names (for display)
current int // active index (-1 = natural order)
keyExprs []string // key expressions for each index
}
// ensureIndexState initializes the index state if nil.
func (a *DBFArea) ensureIndexState() {
if a.idxState == nil {
a.idxState = &indexState{current: -1}
}
}
// OrderCreate creates a new index file. Equivalent to INDEX ON.
func (a *DBFArea) OrderCreate(params hbrdd.OrderCreateParams) error {
a.ensureIndexState()
idxPath := params.FilePath
if idxPath == "" {
return fmt.Errorf("index file path required")
}
// Ensure .ntx extension
if !strings.Contains(filepath.Base(idxPath), ".") {
idxPath += ".ntx"
}
// Build key evaluator from expression
keyExpr := strings.ToUpper(params.KeyExpr)
// Determine key length from first record (or default)
keyLen := 10
recCount, _ := a.RecCount()
if recCount > 0 {
sample := a.evalKeyExpr(keyExpr, 1)
if len(sample) > 0 {
keyLen = len(sample)
}
}
// Build key records — apply FOR condition if present
forExpr := strings.TrimSpace(params.ForExpr)
keys := make([]ntx.KeyRecord, 0, recCount)
for r := uint32(1); r <= recCount; r++ {
// FOR condition: skip records that don't match
if forExpr != "" {
if !a.evalForExpr(forExpr, r) {
continue
}
}
k := a.evalKeyExpr(keyExpr, r)
// Pad or trim to keyLen
if len(k) < keyLen {
padded := make([]byte, keyLen)
copy(padded, k)
for j := len(k); j < keyLen; j++ {
padded[j] = ' '
}
k = padded
} else if len(k) > keyLen {
k = k[:keyLen]
}
keys = append(keys, ntx.KeyRecord{Key: k, RecNo: r})
}
// Sort keys before building index
sort.Slice(keys, func(i, j int) bool {
cmp := bytes.Compare(keys[i].Key, keys[j].Key)
if params.Descending {
return cmp > 0
}
return cmp < 0
})
idx, err := ntx.CreateIndex(idxPath, keyExpr, keyLen, params.Unique, params.Descending, keys)
if err != nil {
return fmt.Errorf("create index failed: %w", err)
}
a.idxState.indexes = append(a.idxState.indexes, idx)
a.idxState.names = append(a.idxState.names, idxPath)
a.idxState.tags = append(a.idxState.tags, params.TagName)
a.idxState.keyExprs = append(a.idxState.keyExprs, keyExpr)
a.idxState.current = len(a.idxState.indexes) - 1
return nil
}
// OrderListAdd opens an existing index file.
func (a *DBFArea) OrderListAdd(path string) error {
a.ensureIndexState()
if !strings.Contains(filepath.Base(path), ".") {
path += ".ntx"
}
idx, err := ntx.OpenIndex(path)
if err != nil {
return fmt.Errorf("open index failed: %w", err)
}
a.idxState.indexes = append(a.idxState.indexes, idx)
a.idxState.names = append(a.idxState.names, path)
a.idxState.tags = append(a.idxState.tags, "")
a.idxState.keyExprs = append(a.idxState.keyExprs, "")
a.idxState.current = len(a.idxState.indexes) - 1
return nil
}
// OrderListClear closes all index files.
func (a *DBFArea) OrderListClear() error {
if a.idxState == nil {
return nil
}
for _, idx := range a.idxState.indexes {
idx.Close()
}
a.idxState = &indexState{current: -1}
return nil
}
// OrderListFocus sets the active index by tag name or number.
func (a *DBFArea) OrderListFocus(tagName string) error {
a.ensureIndexState()
if tagName == "" || tagName == "0" {
a.idxState.current = -1 // natural order
return nil
}
upper := strings.ToUpper(tagName)
for i, name := range a.idxState.tags {
if strings.ToUpper(name) == upper {
a.idxState.current = i
return nil
}
}
// Try by file name
for i, name := range a.idxState.names {
base := strings.ToUpper(filepath.Base(name))
if base == upper || strings.TrimSuffix(base, ".NTX") == upper {
a.idxState.current = i
return nil
}
}
return fmt.Errorf("index not found: %s", tagName)
}
// OrderListRebuild rebuilds all indexes.
func (a *DBFArea) OrderListRebuild() error {
// TODO: reindex all open indexes
return nil
}
// OrderDestroy removes an index file.
func (a *DBFArea) OrderDestroy(tagName string) error {
a.ensureIndexState()
upper := strings.ToUpper(tagName)
for i, name := range a.idxState.tags {
if strings.ToUpper(name) == upper {
a.idxState.indexes[i].Close()
os.Remove(a.idxState.names[i])
// Remove from slices
a.idxState.indexes = append(a.idxState.indexes[:i], a.idxState.indexes[i+1:]...)
a.idxState.names = append(a.idxState.names[:i], a.idxState.names[i+1:]...)
a.idxState.tags = append(a.idxState.tags[:i], a.idxState.tags[i+1:]...)
a.idxState.keyExprs = append(a.idxState.keyExprs[:i], a.idxState.keyExprs[i+1:]...)
if a.idxState.current >= len(a.idxState.indexes) {
a.idxState.current = -1
}
return nil
}
}
return fmt.Errorf("index not found: %s", tagName)
}
// OrderInfo returns information about an index order.
func (a *DBFArea) OrderInfo(ordNo int) (*hbrdd.OrderInfo, error) {
a.ensureIndexState()
idx := ordNo - 1
if idx < 0 || idx >= len(a.idxState.indexes) {
return nil, fmt.Errorf("invalid order number: %d", ordNo)
}
return &hbrdd.OrderInfo{
Name: a.idxState.tags[idx],
KeyExpr: a.idxState.keyExprs[idx],
}, nil
}
// Seek searches for a key in the active index.
// Harbour compatible: partial key matching, softseek, space padding.
func (a *DBFArea) Seek(key hbrt.Value, softSeek bool, findLast bool) (bool, error) {
a.ensureIndexState()
if a.idxState.current < 0 || a.idxState.current >= len(a.idxState.indexes) {
return false, fmt.Errorf("no active index")
}
idx := a.idxState.indexes[a.idxState.current]
keyLen := idx.KeyLen()
// Convert key to bytes and track actual search length
var searchKey []byte
var actualLen int
if key.IsString() {
s := key.AsString()
actualLen = len(s)
// Pad with spaces to full key length (Harbour convention)
if actualLen < keyLen {
padded := make([]byte, keyLen)
copy(padded, []byte(s))
for i := actualLen; i < keyLen; i++ {
padded[i] = ' '
}
searchKey = padded
} else {
searchKey = []byte(s[:keyLen])
actualLen = keyLen
}
} else if key.IsNumeric() {
s := fmt.Sprintf("%*d", keyLen, key.AsNumInt())
searchKey = []byte(s)
if len(searchKey) > keyLen {
searchKey = searchKey[:keyLen]
}
actualLen = keyLen
} else {
searchKey = []byte(key.AsString())
actualLen = len(searchKey)
}
// Seek in index
recNo, exactFound := idx.Seek(searchKey)
// If not exact, check partial match: compare only actualLen bytes
if !exactFound && recNo > 0 && actualLen < keyLen {
// Position at the found location and check partial match
curKey := idx.CurKey()
if len(curKey) >= actualLen && bytes.Equal(curKey[:actualLen], searchKey[:actualLen]) {
exactFound = true
}
}
if exactFound && recNo > 0 {
a.GoTo(recNo)
a.FEof = false
a.SetFound(true)
return true, nil
}
if softSeek && recNo > 0 && !idx.IsEOF() {
a.GoTo(recNo)
a.FEof = false
a.SetFound(false)
return false, nil
}
// Not found — go to EOF
rc, _ := a.RecCount()
a.GoTo(rc + 1)
a.FEof = true
a.SetFound(false)
return false, nil
}
// GoTopIndexed positions at the first key in the active index.
func (a *DBFArea) GoTopIndexed() error {
if a.idxState == nil || a.idxState.current < 0 {
return a.GoTop()
}
idx := a.idxState.indexes[a.idxState.current]
idx.GoTop()
if idx.IsEOF() {
rc, _ := a.RecCount()
return a.GoTo(rc + 1)
}
return a.GoTo(idx.CurRecNo())
}
// GoBottomIndexed positions at the last key in the active index.
func (a *DBFArea) GoBottomIndexed() error {
if a.idxState == nil || a.idxState.current < 0 {
return a.GoBottom()
}
idx := a.idxState.indexes[a.idxState.current]
idx.GoBottom()
if idx.IsBOF() {
return a.GoTo(1)
}
return a.GoTo(idx.CurRecNo())
}
// SkipIndexed skips using the active index order.
func (a *DBFArea) SkipIndexed(count int64) error {
if a.idxState == nil || a.idxState.current < 0 {
return a.Skip(count)
}
idx := a.idxState.indexes[a.idxState.current]
if count > 0 {
for i := int64(0); i < count; i++ {
idx.SkipNext()
if idx.IsEOF() || idx.CurRecNo() == 0 {
rc, _ := a.RecCount()
a.GoTo(rc + 1)
a.FEof = true
return nil
}
}
} else if count < 0 {
for i := int64(0); i > count; i-- {
idx.SkipPrev()
if idx.IsBOF() {
return a.GoTo(1)
}
}
}
return a.GoTo(idx.CurRecNo())
}
// evalKeyExpr evaluates an index key expression for a given record.
// Supports: field names, UPPER(), LOWER(), LTRIM(), RTRIM(), ALLTRIM(),
// STR(), DTOS(), SUBSTR(), LEFT(), RIGHT(), PADL(), PADR(),
// field1+field2 (concatenation), nested functions.
func (a *DBFArea) evalKeyExpr(expr string, recNo uint32) []byte {
oldRecNo := a.recNo
a.GoTo(recNo)
result := a.evalKeyExprInner(strings.TrimSpace(expr))
a.GoTo(oldRecNo)
return result
}
func (a *DBFArea) evalKeyExprInner(expr string) []byte {
upper := strings.ToUpper(expr)
// String literal
if len(expr) >= 2 && expr[0] == '"' && expr[len(expr)-1] == '"' {
return []byte(expr[1 : len(expr)-1])
}
// Simple field name
for i := 0; i < a.FieldCount(); i++ {
fi := a.GetFieldInfo(i)
if strings.ToUpper(fi.Name) == upper {
val, _ := a.GetValue(i)
return formatKeyValue(val, fi)
}
}
// Function calls: FUNC(args)
if parenOpen := strings.Index(expr, "("); parenOpen > 0 {
funcName := strings.ToUpper(strings.TrimSpace(expr[:parenOpen]))
// Find matching close paren
parenClose := findMatchingParen(expr, parenOpen)
if parenClose < 0 {
parenClose = len(expr) - 1
}
argsStr := expr[parenOpen+1 : parenClose]
switch funcName {
case "UPPER":
inner := a.evalKeyExprInner(argsStr)
return []byte(strings.ToUpper(string(inner)))
case "LOWER":
inner := a.evalKeyExprInner(argsStr)
return []byte(strings.ToLower(string(inner)))
case "ALLTRIM", "TRIM":
inner := a.evalKeyExprInner(argsStr)
return []byte(strings.TrimSpace(string(inner)))
case "LTRIM":
inner := a.evalKeyExprInner(argsStr)
return []byte(strings.TrimLeft(string(inner), " "))
case "RTRIM":
inner := a.evalKeyExprInner(argsStr)
return []byte(strings.TrimRight(string(inner), " "))
case "LEFT":
args := splitArgs(argsStr)
if len(args) >= 2 {
inner := a.evalKeyExprInner(args[0])
n := parseIntIdx(args[1])
if n > len(inner) {
n = len(inner)
}
return inner[:n]
}
case "RIGHT":
args := splitArgs(argsStr)
if len(args) >= 2 {
inner := a.evalKeyExprInner(args[0])
n := parseIntIdx(args[1])
if n > len(inner) {
n = len(inner)
}
return inner[len(inner)-n:]
}
case "SUBSTR":
args := splitArgs(argsStr)
if len(args) >= 2 {
inner := a.evalKeyExprInner(args[0])
start := parseIntIdx(args[1]) - 1 // 1-based to 0-based
if start < 0 {
start = 0
}
length := len(inner) - start
if len(args) >= 3 {
length = parseIntIdx(args[2])
}
if start+length > len(inner) {
length = len(inner) - start
}
return inner[start : start+length]
}
case "STR":
args := splitArgs(argsStr)
inner := a.evalKeyExprInner(args[0])
if len(args) >= 2 {
width := parseIntIdx(args[1])
s := string(inner)
return []byte(fmt.Sprintf("%*s", width, strings.TrimSpace(s)))
}
return inner
case "DTOS":
inner := a.evalKeyExprInner(argsStr)
// Date → YYYYMMDD sortable string
return inner
case "PADL":
args := splitArgs(argsStr)
if len(args) >= 2 {
inner := string(a.evalKeyExprInner(args[0]))
width := parseIntIdx(args[1])
fill := " "
if len(args) >= 3 {
fill = strings.Trim(args[2], "\"' ")
if fill == "" {
fill = " "
}
}
for len(inner) < width {
inner = fill + inner
}
return []byte(inner[:width])
}
case "PADR":
args := splitArgs(argsStr)
if len(args) >= 2 {
inner := string(a.evalKeyExprInner(args[0]))
width := parseIntIdx(args[1])
for len(inner) < width {
inner = inner + " "
}
return []byte(inner[:width])
}
}
// Unknown function — try to evaluate inner as field
return a.evalKeyExprInner(argsStr)
}
// Concatenation: expr1 + expr2 (find + not inside parens)
if plus := findOperator(expr, '+'); plus > 0 {
left := a.evalKeyExprInner(expr[:plus])
right := a.evalKeyExprInner(expr[plus+1:])
return append(left, right...)
}
// Numeric literal
s := strings.TrimSpace(expr)
if len(s) > 0 && (s[0] >= '0' && s[0] <= '9') {
return []byte(s)
}
return []byte(expr)
}
// evalForExpr evaluates a FOR condition for a given record. Returns true if record matches.
// Supports: FIELD = "value", FIELD = value, FIELD > value, !DELETED(), .T., .F.
func (a *DBFArea) evalForExpr(forExpr string, recNo uint32) bool {
oldRecNo := a.recNo
a.GoTo(recNo)
result := a.evalForInner(strings.TrimSpace(forExpr))
a.GoTo(oldRecNo)
return result
}
func (a *DBFArea) evalForInner(expr string) bool {
upper := strings.ToUpper(strings.TrimSpace(expr))
if upper == ".T." || upper == "TRUE" {
return true
}
if upper == ".F." || upper == "FALSE" {
return false
}
if upper == "!DELETED()" || upper == ".NOT. DELETED()" {
return !a.Deleted()
}
if upper == "DELETED()" {
return a.Deleted()
}
// FIELD = "value" or FIELD = value
for _, op := range []string{"==", "=", "!=", "<>", ">=", "<=", ">", "<"} {
if idx := strings.Index(expr, op); idx > 0 {
leftExpr := strings.TrimSpace(expr[:idx])
rightExpr := strings.TrimSpace(expr[idx+len(op):])
leftVal := string(a.evalKeyExprInner(leftExpr))
rightVal := strings.Trim(rightExpr, "\"' ")
leftTrim := strings.TrimRight(leftVal, " ")
switch op {
case "=", "==":
return leftTrim == rightVal || leftVal == rightVal
case "!=", "<>":
return leftTrim != rightVal && leftVal != rightVal
case ">":
return leftTrim > rightVal
case "<":
return leftTrim < rightVal
case ">=":
return leftTrim >= rightVal
case "<=":
return leftTrim <= rightVal
}
}
}
// .AND. / .OR.
if idx := strings.Index(upper, ".AND."); idx > 0 {
left := a.evalForInner(expr[:idx])
right := a.evalForInner(expr[idx+5:])
return left && right
}
if idx := strings.Index(upper, ".OR."); idx > 0 {
left := a.evalForInner(expr[:idx])
right := a.evalForInner(expr[idx+4:])
return left || right
}
return true // default: include record
}
// Helper: find matching close parenthesis
func findMatchingParen(s string, openPos int) int {
depth := 1
for i := openPos + 1; i < len(s); i++ {
if s[i] == '(' {
depth++
} else if s[i] == ')' {
depth--
if depth == 0 {
return i
}
}
}
return -1
}
// Helper: find operator not inside parentheses
func findOperator(s string, op byte) int {
depth := 0
for i := len(s) - 1; i > 0; i-- {
if s[i] == ')' {
depth++
} else if s[i] == '(' {
depth--
} else if s[i] == op && depth == 0 {
return i
}
}
return -1
}
// Helper: split comma-separated args respecting parentheses
func splitArgs(s string) []string {
var args []string
depth := 0
start := 0
for i := 0; i < len(s); i++ {
if s[i] == '(' {
depth++
} else if s[i] == ')' {
depth--
} else if s[i] == ',' && depth == 0 {
args = append(args, strings.TrimSpace(s[start:i]))
start = i + 1
}
}
args = append(args, strings.TrimSpace(s[start:]))
return args
}
func parseIntIdx(s string) int {
s = strings.TrimSpace(s)
n := 0
for _, c := range s {
if c >= '0' && c <= '9' {
n = n*10 + int(c-'0')
}
}
return n
}
// formatKeyValue converts a Value to index key bytes.
func formatKeyValue(val hbrt.Value, fi hbrdd.FieldInfo) []byte {
switch fi.Type {
case 'C':
s := val.AsString()
// Pad to field length
for len(s) < fi.Len {
s += " "
}
return []byte(s[:fi.Len])
case 'N':
s := fmt.Sprintf("%*.*f", fi.Len, fi.Dec, val.AsNumDouble())
return []byte(s)
case 'D':
return []byte(val.AsString())
default:
return []byte(val.AsString())
}
}