165 lines
4.3 KiB
Go
Raw Normal View History

2025-12-24 16:44:53 +04:00
package storage
2025-10-08 13:07:59 +02:00
import (
"encoding/json"
"unsafe"
)
/*
#include "bridge.h"
#include <stdlib.h>
2025-12-24 16:44:53 +04:00
static int cGoStorageStorageList(void* storageCtx, void* resp) {
return storage_list(storageCtx, (StorageCallback) callback, resp);
2025-10-08 13:07:59 +02:00
}
2025-12-24 16:44:53 +04:00
static int cGoStorageStorageFetch(void* storageCtx, char* cid, void* resp) {
return storage_fetch(storageCtx, cid, (StorageCallback) callback, resp);
2025-10-08 13:07:59 +02:00
}
2025-12-24 16:44:53 +04:00
static int cGoStorageStorageSpace(void* storageCtx, void* resp) {
return storage_space(storageCtx, (StorageCallback) callback, resp);
2025-10-08 13:07:59 +02:00
}
2025-12-24 16:44:53 +04:00
static int cGoStorageStorageDelete(void* storageCtx, char* cid, void* resp) {
return storage_delete(storageCtx, cid, (StorageCallback) callback, resp);
2025-10-08 13:07:59 +02:00
}
2025-11-02 17:20:59 +01:00
2025-12-24 16:44:53 +04:00
static int cGoStorageStorageExists(void* storageCtx, char* cid, void* resp) {
return storage_exists(storageCtx, cid, (StorageCallback) callback, resp);
2025-11-02 17:20:59 +01:00
}
2025-10-08 13:07:59 +02:00
*/
import "C"
type manifestWithCid struct {
Cid string `json:"cid"`
Manifest Manifest `json:"manifest"`
}
type Space struct {
// TotalBlocks is the number of blocks stored by the node
TotalBlocks int `json:"totalBlocks"`
// QuotaMaxBytes is the maximum storage space (in bytes) available
2025-12-24 16:44:53 +04:00
// for the node in Logos Storage's local repository.
2025-10-08 13:07:59 +02:00
QuotaMaxBytes int64 `json:"quotaMaxBytes"`
// QuotaUsedBytes is the mount of storage space (in bytes) currently used
2025-12-24 16:44:53 +04:00
// for storing files in Logos Storage's local repository.
2025-10-08 13:07:59 +02:00
QuotaUsedBytes int64 `json:"quotaUsedBytes"`
// QuotaReservedBytes is the amount of storage reserved (in bytes) in the
2025-12-24 16:44:53 +04:00
// Logos Storage's local repository for future use when storage requests will be picked
2025-10-08 13:07:59 +02:00
// up and hosted by the node using node's availabilities.
// This does not include the storage currently in use.
QuotaReservedBytes int64 `json:"quotaReservedBytes"`
}
2025-12-24 16:44:53 +04:00
// Manifests returns the list of all manifests stored by the Logos Storage node.
func (node StorageNode) Manifests() ([]Manifest, error) {
2025-10-08 13:07:59 +02:00
bridge := newBridgeCtx()
defer bridge.free()
2025-12-24 16:44:53 +04:00
if C.cGoStorageStorageList(node.ctx, bridge.resp) != C.RET_OK {
return nil, bridge.callError("cGoStorageStorageList")
2025-10-08 13:07:59 +02:00
}
value, err := bridge.wait()
if err != nil {
return nil, err
}
var items []manifestWithCid
err = json.Unmarshal([]byte(value), &items)
if err != nil {
return nil, err
}
var list []Manifest
for _, item := range items {
item.Manifest.Cid = item.Cid
list = append(list, item.Manifest)
}
return list, err
}
// Fetch download a file from the network and store it to the local node.
2025-12-24 16:44:53 +04:00
func (node StorageNode) Fetch(cid string) (Manifest, error) {
2025-10-08 13:07:59 +02:00
bridge := newBridgeCtx()
defer bridge.free()
var cCid = C.CString(cid)
defer C.free(unsafe.Pointer(cCid))
2025-12-24 16:44:53 +04:00
if C.cGoStorageStorageFetch(node.ctx, cCid, bridge.resp) != C.RET_OK {
return Manifest{}, bridge.callError("cGoStorageStorageFetch")
2025-10-08 13:07:59 +02:00
}
value, err := bridge.wait()
if err != nil {
return Manifest{}, err
}
var manifest Manifest
err = json.Unmarshal([]byte(value), &manifest)
if err != nil {
return Manifest{}, err
}
manifest.Cid = cid
return manifest, nil
}
// Space returns information about the storage space used and available.
2025-12-24 16:44:53 +04:00
func (node StorageNode) Space() (Space, error) {
2025-10-08 13:07:59 +02:00
var space Space
bridge := newBridgeCtx()
defer bridge.free()
2025-12-24 16:44:53 +04:00
if C.cGoStorageStorageSpace(node.ctx, bridge.resp) != C.RET_OK {
return space, bridge.callError("cGoStorageStorageSpace")
2025-10-08 13:07:59 +02:00
}
value, err := bridge.wait()
if err != nil {
return space, err
}
err = json.Unmarshal([]byte(value), &space)
return space, err
}
// Deletes either a single block or an entire dataset
// from the local node. Does nothing if the dataset is not locally available.
2025-12-24 16:44:53 +04:00
func (node StorageNode) Delete(cid string) error {
2025-10-08 13:07:59 +02:00
bridge := newBridgeCtx()
defer bridge.free()
var cCid = C.CString(cid)
defer C.free(unsafe.Pointer(cCid))
2025-12-24 16:44:53 +04:00
if C.cGoStorageStorageDelete(node.ctx, cCid, bridge.resp) != C.RET_OK {
return bridge.callError("cGoStorageStorageDelete")
2025-10-08 13:07:59 +02:00
}
_, err := bridge.wait()
return err
}
2025-11-02 17:20:59 +01:00
// Exists checks if a given cid exists in the local storage.
2025-12-24 16:44:53 +04:00
func (node StorageNode) Exists(cid string) (bool, error) {
2025-11-02 17:20:59 +01:00
bridge := newBridgeCtx()
defer bridge.free()
var cCid = C.CString(cid)
defer C.free(unsafe.Pointer(cCid))
2025-12-24 16:44:53 +04:00
if C.cGoStorageStorageExists(node.ctx, cCid, bridge.resp) != C.RET_OK {
return false, bridge.callError("cGoStorageStorageExists")
2025-11-02 17:20:59 +01:00
}
result, err := bridge.wait()
return result == "true", err
}