Skip to content

Commit 32f40d0

Browse files
authored
Merge pull request #9 from codex-storage/feat/has-block
feat: exists feature
2 parents 9aa9977 + 84d3e29 commit 32f40d0

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

codex/storage.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import (
2424
static int cGoCodexStorageDelete(void* codexCtx, char* cid, void* resp) {
2525
return codex_storage_delete(codexCtx, cid, (CodexCallback) callback, resp);
2626
}
27+
28+
static int cGoCodexStorageExists(void* codexCtx, char* cid, void* resp) {
29+
return codex_storage_exists(codexCtx, cid, (CodexCallback) callback, resp);
30+
}
2731
*/
2832
import "C"
2933

@@ -142,3 +146,19 @@ func (node CodexNode) Delete(cid string) error {
142146
_, err := bridge.wait()
143147
return err
144148
}
149+
150+
// Exists checks if a given cid exists in the local storage.
151+
func (node CodexNode) Exists(cid string) (bool, error) {
152+
bridge := newBridgeCtx()
153+
defer bridge.free()
154+
155+
var cCid = C.CString(cid)
156+
defer C.free(unsafe.Pointer(cCid))
157+
158+
if C.cGoCodexStorageExists(node.ctx, cCid, bridge.resp) != C.RET_OK {
159+
return false, bridge.callError("cGoCodexStorageExists")
160+
}
161+
162+
result, err := bridge.wait()
163+
return result == "true", err
164+
}

codex/storage_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,30 @@ func TestDelete(t *testing.T) {
119119
t.Fatal("expected manifests to be empty after deletion")
120120
}
121121
}
122+
123+
func TestExists(t *testing.T) {
124+
codex := newCodexNode(t)
125+
126+
cid, _ := uploadHelper(t, codex)
127+
128+
exists, err := codex.Exists(cid)
129+
if err != nil {
130+
t.Fatal(err)
131+
}
132+
if !exists {
133+
t.Fatal("expected cid to exist")
134+
}
135+
136+
err = codex.Delete(cid)
137+
if err != nil {
138+
t.Fatal(err)
139+
}
140+
141+
exists, err = codex.Exists(cid)
142+
if err != nil {
143+
t.Fatal(err)
144+
}
145+
if exists {
146+
t.Fatal("expected cid to not exist after deletion")
147+
}
148+
}

0 commit comments

Comments
 (0)