mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-07 16:03:13 +00:00
Add exists in lib
This commit is contained in:
parent
58d964bea6
commit
2277729bad
@ -109,6 +109,10 @@ package main
|
|||||||
static int cGoCodexLogLevel(void* codexCtx, char* logLevel, void* resp) {
|
static int cGoCodexLogLevel(void* codexCtx, char* logLevel, void* resp) {
|
||||||
return codex_log_level(codexCtx, logLevel, (CodexCallback) callback, resp);
|
return codex_log_level(codexCtx, logLevel, (CodexCallback) callback, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int cGoCodexExists(void* codexCtx, char* cid, void* resp) {
|
||||||
|
return codex_storage_exists(codexCtx, cid, (CodexCallback) callback, resp);
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
@ -794,14 +798,33 @@ func (node CodexNode) UpdateLogLevel(logLevel string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (node CodexNode) Exists(cid string) (bool, error) {
|
||||||
|
bridge := newBridgeCtx()
|
||||||
|
defer bridge.free()
|
||||||
|
|
||||||
|
var cCid = C.CString(cid)
|
||||||
|
defer C.free(unsafe.Pointer(cCid))
|
||||||
|
|
||||||
|
if C.cGoCodexExists(node.ctx, cCid, bridge.resp) != C.RET_OK {
|
||||||
|
return false, bridge.callError("cGoCodexUploadCancel")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := bridge.wait()
|
||||||
|
return result == "true", err
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
dataDir := os.TempDir() + "/data-dir"
|
||||||
|
|
||||||
node, err := New(Config{
|
node, err := New(Config{
|
||||||
BlockRetries: 5,
|
BlockRetries: 5,
|
||||||
LogLevel: "WARN",
|
LogLevel: "WARN",
|
||||||
|
DataDir: dataDir,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create Codex node: %v", err)
|
log.Fatalf("Failed to create Codex node: %v", err)
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(dataDir)
|
||||||
|
|
||||||
if err := node.Start(); err != nil {
|
if err := node.Start(); err != nil {
|
||||||
log.Fatalf("Failed to start Codex node: %v", err)
|
log.Fatalf("Failed to start Codex node: %v", err)
|
||||||
@ -819,14 +842,33 @@ func main() {
|
|||||||
log.Fatalf("Failed to update log level: %v", err)
|
log.Fatalf("Failed to update log level: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cid := "zDvZRwzmAkhzDRPH5EW242gJBNZ2T7aoH2v1fVH66FxXL4kSbvyM"
|
||||||
|
exists, err := node.Exists(cid)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to check data existence: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
log.Fatalf("The data should not exist")
|
||||||
|
}
|
||||||
|
|
||||||
buf := bytes.NewBuffer([]byte("Hello World!"))
|
buf := bytes.NewBuffer([]byte("Hello World!"))
|
||||||
len := buf.Len()
|
len := buf.Len()
|
||||||
cid, err := node.UploadReader(UploadOptions{Filepath: "hello.txt"}, buf)
|
cid, err = node.UploadReader(UploadOptions{Filepath: "hello.txt"}, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to upload data: %v", err)
|
log.Fatalf("Failed to upload data: %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("Uploaded data with CID: %s (size: %d bytes)", cid, len)
|
log.Printf("Uploaded data with CID: %s (size: %d bytes)", cid, len)
|
||||||
|
|
||||||
|
exists, err = node.Exists(cid)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to check data existence: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
log.Fatalf("The data should exist")
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for a SIGINT or SIGTERM signal
|
// Wait for a SIGINT or SIGTERM signal
|
||||||
ch := make(chan os.Signal, 1)
|
ch := make(chan os.Signal, 1)
|
||||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|||||||
@ -19,7 +19,7 @@ import ../../../codex/stores/repostore
|
|||||||
|
|
||||||
from ../../../codex/codex import CodexServer, node, repoStore
|
from ../../../codex/codex import CodexServer, node, repoStore
|
||||||
from ../../../codex/node import
|
from ../../../codex/node import
|
||||||
iterateManifests, fetchManifest, fetchDatasetAsyncTask, delete
|
iterateManifests, fetchManifest, fetchDatasetAsyncTask, delete, hasLocalBlock
|
||||||
from libp2p import Cid, init, `$`
|
from libp2p import Cid, init, `$`
|
||||||
|
|
||||||
logScope:
|
logScope:
|
||||||
@ -30,6 +30,7 @@ type NodeStorageMsgType* = enum
|
|||||||
DELETE
|
DELETE
|
||||||
FETCH
|
FETCH
|
||||||
SPACE
|
SPACE
|
||||||
|
EXISTS
|
||||||
|
|
||||||
type NodeStorageRequest* = object
|
type NodeStorageRequest* = object
|
||||||
operation: NodeStorageMsgType
|
operation: NodeStorageMsgType
|
||||||
@ -125,6 +126,20 @@ proc space(
|
|||||||
)
|
)
|
||||||
return ok(serde.toJson(space))
|
return ok(serde.toJson(space))
|
||||||
|
|
||||||
|
proc exists(
|
||||||
|
codex: ptr CodexServer, cCid: cstring
|
||||||
|
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||||
|
let cid = Cid.init($cCid)
|
||||||
|
if cid.isErr:
|
||||||
|
return err("Failed to check the data existence: cannot parse cid: " & $cCid)
|
||||||
|
|
||||||
|
try:
|
||||||
|
let node = codex[].node
|
||||||
|
let exists = await node.hasLocalBlock(cid.get())
|
||||||
|
return ok($exists)
|
||||||
|
except CancelledError:
|
||||||
|
return err("Failed to check the data existence: operation cancelled.")
|
||||||
|
|
||||||
proc process*(
|
proc process*(
|
||||||
self: ptr NodeStorageRequest, codex: ptr CodexServer
|
self: ptr NodeStorageRequest, codex: ptr CodexServer
|
||||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||||
@ -155,4 +170,9 @@ proc process*(
|
|||||||
if res.isErr:
|
if res.isErr:
|
||||||
error "Failed to SPACE.", error = res.error
|
error "Failed to SPACE.", error = res.error
|
||||||
return err($res.error)
|
return err($res.error)
|
||||||
|
of NodeStorageMsgType.EXISTS:
|
||||||
|
let res = (await exists(codex, self.cid))
|
||||||
|
if res.isErr:
|
||||||
|
error "Failed to EXISTS.", error = res.error
|
||||||
|
return err($res.error)
|
||||||
return res
|
return res
|
||||||
|
|||||||
@ -172,6 +172,12 @@ int codex_storage_fetch(
|
|||||||
CodexCallback callback,
|
CodexCallback callback,
|
||||||
void* userData);
|
void* userData);
|
||||||
|
|
||||||
|
int codex_storage_exists(
|
||||||
|
void* ctx,
|
||||||
|
const char* cid,
|
||||||
|
CodexCallback callback,
|
||||||
|
void* userData);
|
||||||
|
|
||||||
int codex_start(void* ctx,
|
int codex_start(void* ctx,
|
||||||
CodexCallback callback,
|
CodexCallback callback,
|
||||||
void* userData);
|
void* userData);
|
||||||
|
|||||||
@ -515,6 +515,20 @@ proc codex_storage_fetch(
|
|||||||
|
|
||||||
return callback.okOrError(res, userData)
|
return callback.okOrError(res, userData)
|
||||||
|
|
||||||
|
proc codex_storage_exists(
|
||||||
|
ctx: ptr CodexContext, cid: cstring, callback: CodexCallback, userData: pointer
|
||||||
|
): cint {.dynlib, exportc.} =
|
||||||
|
initializeLibrary()
|
||||||
|
checkLibcodexParams(ctx, callback, userData)
|
||||||
|
|
||||||
|
let req = NodeStorageRequest.createShared(NodeStorageMsgType.EXISTS, cid = cid)
|
||||||
|
|
||||||
|
let res = codex_context.sendRequestToCodexThread(
|
||||||
|
ctx, RequestType.STORAGE, req, callback, userData
|
||||||
|
)
|
||||||
|
|
||||||
|
return callback.okOrError(res, userData)
|
||||||
|
|
||||||
proc codex_start(
|
proc codex_start(
|
||||||
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
|
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
|
||||||
): cint {.dynlib, exportc.} =
|
): cint {.dynlib, exportc.} =
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user