Add exists in lib

This commit is contained in:
Arnaud 2025-11-02 16:30:26 +01:00 committed by Eric
parent 58d964bea6
commit 2277729bad
No known key found for this signature in database
4 changed files with 84 additions and 2 deletions

View File

@ -109,6 +109,10 @@ package main
static int cGoCodexLogLevel(void* codexCtx, char* logLevel, void* 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 (
@ -794,14 +798,33 @@ func (node CodexNode) UpdateLogLevel(logLevel string) error {
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() {
dataDir := os.TempDir() + "/data-dir"
node, err := New(Config{
BlockRetries: 5,
LogLevel: "WARN",
DataDir: dataDir,
})
if err != nil {
log.Fatalf("Failed to create Codex node: %v", err)
}
defer os.RemoveAll(dataDir)
if err := node.Start(); err != nil {
log.Fatalf("Failed to start Codex node: %v", err)
@ -819,14 +842,33 @@ func main() {
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!"))
len := buf.Len()
cid, err := node.UploadReader(UploadOptions{Filepath: "hello.txt"}, buf)
cid, err = node.UploadReader(UploadOptions{Filepath: "hello.txt"}, buf)
if err != nil {
log.Fatalf("Failed to upload data: %v", err)
}
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
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)

View File

@ -19,7 +19,7 @@ import ../../../codex/stores/repostore
from ../../../codex/codex import CodexServer, node, repoStore
from ../../../codex/node import
iterateManifests, fetchManifest, fetchDatasetAsyncTask, delete
iterateManifests, fetchManifest, fetchDatasetAsyncTask, delete, hasLocalBlock
from libp2p import Cid, init, `$`
logScope:
@ -30,6 +30,7 @@ type NodeStorageMsgType* = enum
DELETE
FETCH
SPACE
EXISTS
type NodeStorageRequest* = object
operation: NodeStorageMsgType
@ -125,6 +126,20 @@ proc 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*(
self: ptr NodeStorageRequest, codex: ptr CodexServer
): Future[Result[string, string]] {.async: (raises: []).} =
@ -155,4 +170,9 @@ proc process*(
if res.isErr:
error "Failed to SPACE.", error = 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

View File

@ -172,6 +172,12 @@ int codex_storage_fetch(
CodexCallback callback,
void* userData);
int codex_storage_exists(
void* ctx,
const char* cid,
CodexCallback callback,
void* userData);
int codex_start(void* ctx,
CodexCallback callback,
void* userData);

View File

@ -515,6 +515,20 @@ proc codex_storage_fetch(
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(
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
): cint {.dynlib, exportc.} =