datastore now retrieves a DatastoreRecord struct
The structure is much more user friendly and contains the timestamp
This commit is contained in:
parent
ed63c761d5
commit
ecb9f984ba
4 changed files with 55 additions and 9 deletions
|
@ -73,3 +73,32 @@ int libp2p_datastore_free(struct Datastore* datastore) {
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DatastoreRecord* libp2p_datastore_record_new() {
|
||||||
|
struct DatastoreRecord* rec = (struct DatastoreRecord*) malloc(sizeof(struct DatastoreRecord));
|
||||||
|
if (rec != NULL) {
|
||||||
|
rec->key = NULL;
|
||||||
|
rec->key_size = 0;
|
||||||
|
rec->timestamp = 0;
|
||||||
|
rec->value = NULL;
|
||||||
|
rec->value_size = 0;
|
||||||
|
}
|
||||||
|
return rec;
|
||||||
|
}
|
||||||
|
|
||||||
|
int libp2p_datastore_record_free(struct DatastoreRecord* rec) {
|
||||||
|
if (rec != NULL) {
|
||||||
|
if (rec->key != NULL) {
|
||||||
|
free(rec->key);
|
||||||
|
rec->key = NULL;
|
||||||
|
}
|
||||||
|
rec->key_size = 0;
|
||||||
|
if (rec->value != NULL) {
|
||||||
|
free(rec->value);
|
||||||
|
rec->value = NULL;
|
||||||
|
}
|
||||||
|
rec->value_size = 0;
|
||||||
|
rec->timestamp = 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,14 @@
|
||||||
|
|
||||||
enum DatastoreCursorOp { CURSOR_FIRST, CURSOR_NEXT, CURSOR_LAST, CURSOR_PREVIOUS };
|
enum DatastoreCursorOp { CURSOR_FIRST, CURSOR_NEXT, CURSOR_LAST, CURSOR_PREVIOUS };
|
||||||
|
|
||||||
|
struct DatastoreRecord {
|
||||||
|
uint8_t *key;
|
||||||
|
size_t key_size;
|
||||||
|
uint8_t *value;
|
||||||
|
size_t value_size;
|
||||||
|
unsigned long long timestamp;
|
||||||
|
};
|
||||||
|
|
||||||
struct Datastore {
|
struct Datastore {
|
||||||
char* type;
|
char* type;
|
||||||
char* path;
|
char* path;
|
||||||
|
@ -23,9 +31,7 @@ struct Datastore {
|
||||||
int (*datastore_open)(int argc, char** argv, struct Datastore* datastore);
|
int (*datastore_open)(int argc, char** argv, struct Datastore* datastore);
|
||||||
int (*datastore_close)(struct Datastore* datastore);
|
int (*datastore_close)(struct Datastore* datastore);
|
||||||
int (*datastore_put)(const unsigned char* key, size_t key_size, unsigned char* data, size_t data_length, const struct Datastore* datastore);
|
int (*datastore_put)(const unsigned char* key, size_t key_size, unsigned char* data, size_t data_length, const struct Datastore* datastore);
|
||||||
int (*datastore_get)(const char* key, size_t key_size,
|
int (*datastore_get)(const unsigned char* key, size_t key_size, struct DatastoreRecord** record, const struct Datastore* datastore);
|
||||||
unsigned char* data, size_t max_data_length, size_t* data_length,
|
|
||||||
const struct Datastore* datastore);
|
|
||||||
int (*datastore_cursor_open)(struct Datastore* datastore);
|
int (*datastore_cursor_open)(struct Datastore* datastore);
|
||||||
int (*datastore_cursor_close)(struct Datastore* datastore);
|
int (*datastore_cursor_close)(struct Datastore* datastore);
|
||||||
int (*datastore_cursor_get)(unsigned char** key, int* key_length, unsigned char** value, int* value_length, enum DatastoreCursorOp op, struct Datastore* datastore);
|
int (*datastore_cursor_get)(unsigned char** key, int* key_length, unsigned char** value, int* value_length, enum DatastoreCursorOp op, struct Datastore* datastore);
|
||||||
|
@ -57,3 +63,14 @@ int libp2p_datastore_new(struct Datastore** datastore);
|
||||||
* @returns true(1)
|
* @returns true(1)
|
||||||
*/
|
*/
|
||||||
int libp2p_datastore_free(struct Datastore* datastore);
|
int libp2p_datastore_free(struct Datastore* datastore);
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Create a new DatastoreRecord struct
|
||||||
|
* @returns a newly allocated DatastoreRecord struct
|
||||||
|
*/
|
||||||
|
struct DatastoreRecord* libp2p_datastore_record_new();
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Free resources of a DatastoreRecord
|
||||||
|
*/
|
||||||
|
int libp2p_datastore_record_free(struct DatastoreRecord* record);
|
||||||
|
|
|
@ -88,15 +88,15 @@ int libp2p_providerstore_add(struct ProviderStore* store, const unsigned char* h
|
||||||
int libp2p_providerstore_get(struct ProviderStore* store, const unsigned char* hash, int hash_size, unsigned char** peer_id, int *peer_id_size) {
|
int libp2p_providerstore_get(struct ProviderStore* store, const unsigned char* hash, int hash_size, unsigned char** peer_id, int *peer_id_size) {
|
||||||
struct ProviderEntry* current = NULL;
|
struct ProviderEntry* current = NULL;
|
||||||
// can I provide it locally?
|
// can I provide it locally?
|
||||||
size_t results_size = 65535;
|
struct DatastoreRecord* datastore_record = NULL;
|
||||||
unsigned char results[results_size];
|
if (store->datastore->datastore_get(hash, hash_size, &datastore_record, store->datastore)) {
|
||||||
if (store->datastore->datastore_get((const char*)hash, hash_size, &results[0], results_size, &results_size, store->datastore)) {
|
|
||||||
// we found it locally. Let them know
|
// we found it locally. Let them know
|
||||||
*peer_id = malloc(store->local_peer->id_size);
|
*peer_id = malloc(store->local_peer->id_size);
|
||||||
if (*peer_id == NULL)
|
if (*peer_id == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
*peer_id_size = store->local_peer->id_size;
|
*peer_id_size = store->local_peer->id_size;
|
||||||
memcpy(*peer_id, store->local_peer->id, *peer_id_size);
|
memcpy(*peer_id, store->local_peer->id, *peer_id_size);
|
||||||
|
libp2p_datastore_record_free(datastore_record);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// skip index 0, as we checked above...
|
// skip index 0, as we checked above...
|
||||||
|
|
|
@ -146,10 +146,10 @@ int libp2p_routing_dht_handle_get_providers(struct SessionContext* session, stru
|
||||||
message->provider_peer_head = NULL;
|
message->provider_peer_head = NULL;
|
||||||
|
|
||||||
// Can I provide it locally?
|
// Can I provide it locally?
|
||||||
unsigned char buf[65535];
|
struct DatastoreRecord* datastore_record = NULL;
|
||||||
size_t buf_size = 65535;
|
if (session->datastore->datastore_get((unsigned char*)message->key, message->key_size, &datastore_record, session->datastore)) {
|
||||||
if (session->datastore->datastore_get(message->key, message->key_size, &buf[0], buf_size, &buf_size, session->datastore)) {
|
|
||||||
// we can provide this hash from our datastore
|
// we can provide this hash from our datastore
|
||||||
|
libp2p_datastore_record_free(datastore_record);
|
||||||
libp2p_logger_debug("dht_protocol", "I can provide myself as a provider for this key.\n");
|
libp2p_logger_debug("dht_protocol", "I can provide myself as a provider for this key.\n");
|
||||||
message->provider_peer_head = libp2p_utils_linked_list_new();
|
message->provider_peer_head = libp2p_utils_linked_list_new();
|
||||||
message->provider_peer_head->item = libp2p_peer_copy(libp2p_peerstore_get_local_peer(peerstore));
|
message->provider_peer_head->item = libp2p_peer_copy(libp2p_peerstore_get_local_peer(peerstore));
|
||||||
|
|
Loading…
Reference in a new issue