266 lines
7.6 KiB
Go
266 lines
7.6 KiB
Go
package es
|
||
|
||
//
|
||
//import (
|
||
// "fmt"
|
||
// "os"
|
||
// "path/filepath"
|
||
// "syscall"
|
||
// "unicode/utf16"
|
||
// "unsafe"
|
||
//)
|
||
//
|
||
//// EsDLL Elasticsearch工具DLL结构
|
||
//type EsDLL struct {
|
||
// dll *syscall.DLL
|
||
// listAllIndices *syscall.Proc // 查询所有索引
|
||
// getIndicesInfo *syscall.Proc // 获取所有索引的详细信息
|
||
// getIndexDetail *syscall.Proc // 获取单个索引的详细信息
|
||
// createIndex *syscall.Proc // 创建索引
|
||
// deleteIndex *syscall.Proc // 删除索引
|
||
// getDocumentCount *syscall.Proc // 获取索引文档数量
|
||
// createDocument *syscall.Proc // 创建文档
|
||
// getDocument *syscall.Proc // 根据ID获取文档
|
||
// updateDocument *syscall.Proc // 更新文档
|
||
// deleteDocument *syscall.Proc // 删除文档
|
||
// searchDocuments *syscall.Proc // 搜索文档
|
||
// freeCString *syscall.Proc // 释放C字符串
|
||
//}
|
||
//
|
||
//// InitEsDLL 初始化esDLL
|
||
//func InitEsDLL() (*EsDLL, error) {
|
||
// // 尝试多个可能的DLL路径
|
||
// dllPaths := []string{
|
||
// filepath.Join("dll", "es.dll"),
|
||
// filepath.Join("es_dll", "es.dll"),
|
||
// "es.dll",
|
||
// }
|
||
//
|
||
// var dllPath string
|
||
// for _, path := range dllPaths {
|
||
// if _, err := os.Stat(path); err == nil {
|
||
// dllPath = path
|
||
// break
|
||
// }
|
||
// }
|
||
//
|
||
// if dllPath == "" {
|
||
// return nil, fmt.Errorf("es DLL 不存在,已尝试路径: %v", dllPaths)
|
||
// }
|
||
//
|
||
// if dll, err := syscall.LoadDLL(dllPath); err != nil {
|
||
// return nil, fmt.Errorf("加载es DLL 失败: %s", err)
|
||
// } else {
|
||
// return &EsDLL{
|
||
// dll: dll,
|
||
// listAllIndices: dll.MustFindProc("ListAllIndices"),
|
||
// getIndicesInfo: dll.MustFindProc("GetIndicesInfo"),
|
||
// getIndexDetail: dll.MustFindProc("GetIndexDetail"),
|
||
// createIndex: dll.MustFindProc("CreateIndex"),
|
||
// deleteIndex: dll.MustFindProc("DeleteIndex"),
|
||
// getDocumentCount: dll.MustFindProc("GetDocumentCount"),
|
||
// createDocument: dll.MustFindProc("CreateDocument"),
|
||
// getDocument: dll.MustFindProc("GetDocument"),
|
||
// updateDocument: dll.MustFindProc("UpdateDocument"),
|
||
// deleteDocument: dll.MustFindProc("DeleteDocument"),
|
||
// searchDocuments: dll.MustFindProc("SearchDocuments"),
|
||
// freeCString: dll.MustFindProc("FreeCString"),
|
||
// }, nil
|
||
// }
|
||
//}
|
||
//
|
||
//// cStr 获取C字符串
|
||
//func (m *EsDLL) cStr(p uintptr) string {
|
||
// if p == 0 {
|
||
// return ""
|
||
// }
|
||
// b := []byte{}
|
||
// for i := uintptr(0); ; i++ {
|
||
// c := *(*byte)(unsafe.Pointer(p + i))
|
||
// if c == 0 {
|
||
// break
|
||
// }
|
||
// b = append(b, c)
|
||
// }
|
||
// s := string(b)
|
||
// if m.freeCString != nil {
|
||
// m.freeCString.Call(p)
|
||
// }
|
||
// return s
|
||
//}
|
||
//
|
||
//// stringToUTF16Ptr 将Go字符串转换为UTF16指针(Windows)
|
||
//func stringToUTF16Ptr(s string) uintptr {
|
||
// if s == "" {
|
||
// return 0
|
||
// }
|
||
// // 添加空终止符
|
||
// ws := utf16.Encode([]rune(s + "\x00"))
|
||
// return uintptr(unsafe.Pointer(&ws[0]))
|
||
//}
|
||
//
|
||
//// stringToUTF8Ptr 将Go字符串转换为UTF8指针
|
||
//func stringToUTF8Ptr(s string) uintptr {
|
||
// if s == "" {
|
||
// return 0
|
||
// }
|
||
// bytes := []byte(s)
|
||
// // 添加空终止符
|
||
// bytes = append(bytes, 0)
|
||
// return uintptr(unsafe.Pointer(&bytes[0]))
|
||
//}
|
||
//
|
||
//// strPtr 将Go字符串转换为C字符串指针
|
||
//func (m *EsDLL) strPtr(s string) uintptr {
|
||
// return stringToUTF8Ptr(s)
|
||
//}
|
||
//
|
||
//// 1. 查询所有索引 - ListAllIndices
|
||
//func (m *EsDLL) ListAllIndices() (string, error) {
|
||
// proc, err := m.dll.FindProc("ListAllIndices")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 ListAllIndices: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call()
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 2. 获取所有索引的详细信息 - GetIndicesInfo
|
||
//func (m *EsDLL) GetIndicesInfo() (string, error) {
|
||
// proc, err := m.dll.FindProc("GetIndicesInfo")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 GetIndicesInfo: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call()
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 3. 获取单个索引的详细信息 - GetIndexDetail
|
||
//func (m *EsDLL) GetIndexDetail(indexName string) (string, error) {
|
||
// proc, err := m.dll.FindProc("GetIndexDetail")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 GetIndexDetail: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(stringToUTF8Ptr(indexName))
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 4. 创建索引 - CreateIndex
|
||
//func (m *EsDLL) CreateIndex(indexName, mapping string) (string, error) {
|
||
// proc, err := m.dll.FindProc("CreateIndex")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 CreateIndex: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(
|
||
// stringToUTF8Ptr(indexName),
|
||
// stringToUTF8Ptr(mapping),
|
||
// )
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 5. 删除索引 - DeleteIndex
|
||
//func (m *EsDLL) DeleteIndex(indexName string) (string, error) {
|
||
// proc, err := m.dll.FindProc("DeleteIndex")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 DeleteIndex: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(stringToUTF8Ptr(indexName))
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 6. 获取索引文档数量 - GetDocumentCount
|
||
//func (m *EsDLL) GetDocumentCount(indexName string) (string, error) {
|
||
// proc, err := m.dll.FindProc("GetDocumentCount")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 GetDocumentCount: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(stringToUTF8Ptr(indexName))
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 7. 创建文档 - CreateDocument
|
||
//func (m *EsDLL) CreateDocument(indexName, id, doc string) (string, error) {
|
||
// proc, err := m.dll.FindProc("CreateDocument")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 CreateDocument: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(
|
||
// stringToUTF8Ptr(indexName),
|
||
// stringToUTF8Ptr(id),
|
||
// stringToUTF8Ptr(doc),
|
||
// )
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 8. 根据ID获取文档 - GetDocument
|
||
//func (m *EsDLL) GetDocument(indexName, id string) (string, error) {
|
||
// proc, err := m.dll.FindProc("GetDocument")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 GetDocument: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(
|
||
// stringToUTF8Ptr(indexName),
|
||
// stringToUTF8Ptr(id),
|
||
// )
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 9. 更新文档 - UpdateDocument
|
||
//func (m *EsDLL) UpdateDocument(indexName, id, updateData string) (string, error) {
|
||
// proc, err := m.dll.FindProc("UpdateDocument")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 UpdateDocument: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(
|
||
// stringToUTF8Ptr(indexName),
|
||
// stringToUTF8Ptr(id),
|
||
// stringToUTF8Ptr(updateData),
|
||
// )
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 10. 删除文档 - DeleteDocument
|
||
//func (m *EsDLL) DeleteDocument(indexName, id string) (string, error) {
|
||
// proc, err := m.dll.FindProc("DeleteDocument")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 DeleteDocument: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(
|
||
// stringToUTF8Ptr(indexName),
|
||
// stringToUTF8Ptr(id),
|
||
// )
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// 11. 搜索文档 - SearchDocuments
|
||
//func (m *EsDLL) SearchDocuments(indexName, query string) (string, error) {
|
||
// proc, err := m.dll.FindProc("SearchDocuments")
|
||
// if err != nil {
|
||
// return "", fmt.Errorf("找不到函数 SearchDocuments: %v", err)
|
||
// }
|
||
// resultPtr, _, _ := proc.Call(
|
||
// stringToUTF8Ptr(indexName),
|
||
// stringToUTF8Ptr(query),
|
||
// )
|
||
// result := m.cStr(resultPtr)
|
||
// return result, nil
|
||
//}
|
||
//
|
||
//// Close 关闭DLL句柄
|
||
//func (m *EsDLL) Close() error {
|
||
// if m.dll != nil {
|
||
// return m.dll.Release()
|
||
// }
|
||
// return nil
|
||
//}
|