forked from Restream/reindexer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
861f0c4
commit 076baef
Showing
165 changed files
with
3,896 additions
and
1,683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package builtinserver | ||
|
||
// #include "server/cbinding/server_c.h" | ||
// #include <stdlib.h> | ||
import "C" | ||
import ( | ||
"fmt" | ||
"net/url" | ||
"reflect" | ||
"time" | ||
"unsafe" | ||
|
||
"github.com/restream/reindexer/bindings" | ||
"github.com/restream/reindexer/bindings/builtin" | ||
"github.com/restream/reindexer/bindings/builtinserver/config" | ||
) | ||
|
||
var defaultStartupTimeout time.Duration = time.Minute * 3 | ||
|
||
func init() { | ||
C.init_reindexer_server() | ||
bindings.RegisterBinding("builtinserver", new(BuiltinServer)) | ||
} | ||
|
||
func err2go(ret C.reindexer_error) error { | ||
if ret.what != nil { | ||
defer C.free(unsafe.Pointer(ret.what)) | ||
return bindings.NewError("rq:"+C.GoString(ret.what), int(ret.code)) | ||
} | ||
return nil | ||
} | ||
|
||
func str2c(str string) C.reindexer_string { | ||
hdr := (*reflect.StringHeader)(unsafe.Pointer(&str)) | ||
return C.reindexer_string{p: unsafe.Pointer(hdr.Data), n: C.int(hdr.Len)} | ||
} | ||
|
||
func checkStorageReady() bool { | ||
return int(C.check_server_ready()) == 1 | ||
} | ||
|
||
type BuiltinServer struct { | ||
builtin bindings.RawBinding | ||
ready chan bool | ||
} | ||
|
||
func (server *BuiltinServer) Init(u *url.URL, options ...interface{}) error { | ||
server.builtin = &builtin.Builtin{} | ||
|
||
startupTimeout := defaultStartupTimeout | ||
serverCfg := config.DefaultServerConfig() | ||
|
||
for _, option := range options { | ||
switch v := option.(type) { | ||
case bindings.OptionCgoLimit: | ||
case bindings.OptionBuiltinWithServer: | ||
if v.StartupTimeout != 0 { | ||
startupTimeout = v.StartupTimeout | ||
} | ||
if v.ServerConfig != nil { | ||
serverCfg = v.ServerConfig | ||
} | ||
default: | ||
fmt.Printf("Unknown builtinserver option: %v\n", option) | ||
} | ||
} | ||
|
||
yamlStr, err := serverCfg.GetYamlString() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
err := err2go(C.start_reindexer_server(str2c(yamlStr))) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
tTimeout := time.Now().Add(startupTimeout) | ||
for !checkStorageReady() { | ||
if time.Now().After(tTimeout) { | ||
panic(bindings.NewError("Server startup timeout expired.", bindings.ErrLogic)) | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
|
||
pass, _ := u.User.Password() | ||
server.builtin.(*builtin.Builtin).SetReindexerInstance( | ||
unsafe.Pointer(C.get_reindexer_instance(str2c(u.Host), str2c(u.User.Username()), str2c(pass))), | ||
) | ||
|
||
url := *u | ||
url.Path = "" | ||
|
||
if err := server.builtin.Init(&url, options...); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (server *BuiltinServer) OpenNamespace(namespace string, enableStorage, dropOnFileFormatError bool, cacheMode uint8) error { | ||
return server.builtin.OpenNamespace(namespace, enableStorage, dropOnFileFormatError, cacheMode) | ||
} | ||
|
||
func (server *BuiltinServer) CloseNamespace(namespace string) error { | ||
return server.builtin.CloseNamespace(namespace) | ||
} | ||
|
||
func (server *BuiltinServer) DropNamespace(namespace string) error { | ||
return server.builtin.DropNamespace(namespace) | ||
} | ||
|
||
func (server *BuiltinServer) EnableStorage(namespace string) error { | ||
return server.builtin.EnableStorage(namespace) | ||
} | ||
|
||
func (server *BuiltinServer) AddIndex(namespace, index, jsonPath, indexType, fieldType string, opts bindings.IndexOptions, collateMode int, sortOrderStr string) error { | ||
return server.builtin.AddIndex(namespace, index, jsonPath, indexType, fieldType, opts, collateMode, sortOrderStr) | ||
} | ||
|
||
func (server *BuiltinServer) DropIndex(namespace, index string) error { | ||
return server.builtin.DropIndex(namespace, index) | ||
} | ||
|
||
func (server *BuiltinServer) ConfigureIndex(namespace, index, config string) error { | ||
return server.ConfigureIndex(namespace, index, config) | ||
} | ||
|
||
func (server *BuiltinServer) PutMeta(namespace, key, data string) error { | ||
return server.builtin.PutMeta(namespace, key, data) | ||
} | ||
|
||
func (server *BuiltinServer) GetMeta(namespace, key string) (bindings.RawBuffer, error) { | ||
return server.builtin.GetMeta(namespace, key) | ||
} | ||
|
||
func (server *BuiltinServer) ModifyItem(nsHash int, data []byte, mode int) (bindings.RawBuffer, error) { | ||
return server.builtin.ModifyItem(nsHash, data, mode) | ||
} | ||
|
||
func (server *BuiltinServer) Select(query string, withItems bool, ptVersions []int32, fetchCount int) (bindings.RawBuffer, error) { | ||
return server.builtin.Select(query, withItems, ptVersions, fetchCount) | ||
} | ||
|
||
func (server *BuiltinServer) SelectQuery(rawQuery []byte, withItems bool, ptVersions []int32, fetchCount int) (bindings.RawBuffer, error) { | ||
return server.builtin.SelectQuery(rawQuery, withItems, ptVersions, fetchCount) | ||
} | ||
|
||
func (server *BuiltinServer) DeleteQuery(nsHash int, rawQuery []byte) (bindings.RawBuffer, error) { | ||
return server.builtin.DeleteQuery(nsHash, rawQuery) | ||
} | ||
|
||
func (server *BuiltinServer) Commit(namespace string) error { | ||
return server.builtin.Commit(namespace) | ||
} | ||
|
||
func (server *BuiltinServer) EnableLogger(logger bindings.Logger) { | ||
server.builtin.EnableLogger(logger) | ||
} | ||
|
||
func (server *BuiltinServer) DisableLogger() { | ||
server.builtin.DisableLogger() | ||
} | ||
|
||
func (server *BuiltinServer) Ping() error { | ||
return server.builtin.Ping() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build !windows | ||
//go:generate sh -c "cd ../.. && mkdir -p build && cd build && cmake -DLINK_RESOURCES=On -DWITH_GPERF=Off .. && make reindexer_server_library -j4" | ||
|
||
package builtinserver | ||
|
||
// #cgo CXXFLAGS: -std=c++11 -g -O2 -Wall -Wpedantic -Wextra -I../../cpp_src | ||
// #cgo CFLAGS: -std=c99 -g -O2 -Wall -Wpedantic -Wno-unused-variable -I../../cpp_src | ||
// #cgo LDFLAGS: -L${SRCDIR}/../../build/cpp_src/ -L${SRCDIR}/../../build/cpp_src/server/ -lreindexer_server_library -lresources -lreindexer -lleveldb -lsnappy -lstdc++ -lm -g | ||
import "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build windows | ||
//go:generate cmd /c cd ..\.. && mkdir build & cd build && cmake -G "MinGW Makefiles" -DLINK_RESOURCES=On -DWITH_GPERF=Off -DCMAKE_BUILD_TYPE=Release .. && cmake --build . --target reindexer reindexer_server_library -- -j4 | ||
|
||
package builtin | ||
|
||
// #cgo CXXFLAGS: -std=c++11 -g -O2 -Wall -Wpedantic -Wextra -I../../cpp_src | ||
// #cgo CFLAGS: -std=c99 -g -O2 -Wall -Wpedantic -Wno-unused-variable -I../../cpp_src | ||
// #cgo LDFLAGS: -L${SRCDIR}/../../build/cpp_src/ -L${SRCDIR}/../../build/cpp_src/server/ -lreindexer -lreindexer_server_library -lresources -lleveldb -lsnappy -lstdc++ -g -lshlwapi -ldbghelp | ||
import "C" |
Oops, something went wrong.