2016-11-30 16:46:41 +00:00
|
|
|
#include "libp2p/crypto/encoding/base32.h"
|
|
|
|
#include "ipfs/datastore/ds_helper.h"
|
|
|
|
#include "ipfs/blocks/block.h"
|
|
|
|
#include "ipfs/repo/config/config.h"
|
|
|
|
#include "ipfs/repo/fsrepo/fs_repo.h"
|
2017-08-21 19:49:21 +00:00
|
|
|
#include "ipfs/repo/fsrepo/journalstore.h"
|
2016-11-30 16:46:41 +00:00
|
|
|
|
2016-12-05 18:11:22 +00:00
|
|
|
#include "../test_helper.h"
|
|
|
|
|
2016-11-30 16:46:41 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2016-12-05 11:54:21 +00:00
|
|
|
/**
|
|
|
|
* create a repository and put a record in the datastore and a block in the blockstore
|
|
|
|
*/
|
2016-11-30 16:46:41 +00:00
|
|
|
int test_ipfs_datastore_put() {
|
|
|
|
struct Block* block;
|
2016-12-05 22:23:58 +00:00
|
|
|
int retVal = 0;
|
|
|
|
const unsigned char* input = (unsigned char*)"Hello, world!";
|
2016-11-30 16:46:41 +00:00
|
|
|
|
|
|
|
// build the ipfs repository, then shut it down, so we can start fresh
|
2017-04-13 14:31:58 +00:00
|
|
|
retVal = drop_and_build_repository("/tmp/.ipfs", 4001, NULL, NULL);
|
2016-11-30 16:46:41 +00:00
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// build the block
|
2017-07-27 17:05:41 +00:00
|
|
|
block = ipfs_block_new();
|
2017-07-20 20:03:49 +00:00
|
|
|
if (block == NULL)
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
retVal = ipfs_blocks_block_add_data(input, strlen((char*)input), block);
|
|
|
|
if (retVal == 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:46:41 +00:00
|
|
|
// generate the key
|
|
|
|
size_t key_length = libp2p_crypto_encoding_base32_encode_size(block->data_length);
|
|
|
|
unsigned char key[key_length];
|
|
|
|
retVal = ipfs_datastore_helper_ds_key_from_binary(block->data, block->data_length, &key[0], key_length, &key_length);
|
2016-12-14 17:07:43 +00:00
|
|
|
if (retVal == 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
|
|
|
|
// open the repository
|
2016-12-05 18:11:22 +00:00
|
|
|
struct FSRepo* fs_repo;
|
2016-11-30 16:46:41 +00:00
|
|
|
retVal = ipfs_repo_fsrepo_new("/tmp/.ipfs", NULL, &fs_repo);
|
2016-12-14 17:07:43 +00:00
|
|
|
if (retVal == 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
retVal = ipfs_repo_fsrepo_open(fs_repo);
|
2016-12-14 17:07:43 +00:00
|
|
|
if (retVal == 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
// send to Put with key
|
2017-09-13 17:40:48 +00:00
|
|
|
retVal = ipfs_datastore_helper_add_block_to_datastore(block, fs_repo->config->datastore);
|
2016-12-14 17:07:43 +00:00
|
|
|
if (retVal == 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
|
2016-12-01 18:08:30 +00:00
|
|
|
// clean up
|
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-12-01 18:08:30 +00:00
|
|
|
|
2016-11-30 16:46:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-08-21 19:49:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List what is in the journal
|
|
|
|
*/
|
|
|
|
int test_datastore_list_journal() {
|
2017-09-07 23:45:09 +00:00
|
|
|
int recCount = 0;
|
2017-08-21 19:49:21 +00:00
|
|
|
libp2p_logger_add_class("test_datastore");
|
|
|
|
libp2p_logger_add_class("lmdb_datastore");
|
2017-09-07 23:45:09 +00:00
|
|
|
|
|
|
|
// need to run test_import_small_file first
|
|
|
|
|
2017-08-21 19:49:21 +00:00
|
|
|
// open database
|
|
|
|
struct FSRepo* fs_repo;
|
2017-09-07 23:45:09 +00:00
|
|
|
if (ipfs_repo_fsrepo_new("/tmp/.ipfs", NULL, &fs_repo) == 0) {
|
2017-08-21 19:49:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ipfs_repo_fsrepo_open(fs_repo) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// open cursor
|
2017-09-04 22:10:57 +00:00
|
|
|
struct lmdb_trans_cursor *crsr = NULL;
|
2017-09-07 23:45:09 +00:00
|
|
|
if (!lmdb_journalstore_cursor_open(fs_repo->config->datastore->datastore_context, &crsr, NULL)) {
|
2017-08-21 19:49:21 +00:00
|
|
|
ipfs_repo_fsrepo_free(fs_repo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// grab records
|
|
|
|
struct JournalRecord* record = NULL;
|
|
|
|
enum DatastoreCursorOp op = CURSOR_FIRST;
|
|
|
|
do {
|
2017-09-04 22:10:57 +00:00
|
|
|
if (lmdb_journalstore_cursor_get(crsr, op, &record) == 0) {
|
2017-09-04 18:33:56 +00:00
|
|
|
lmdb_journal_record_free(record);
|
2017-08-21 19:49:21 +00:00
|
|
|
record = NULL;
|
2017-09-07 23:45:09 +00:00
|
|
|
break;
|
2017-08-21 19:49:21 +00:00
|
|
|
}
|
2017-09-07 23:45:09 +00:00
|
|
|
recCount++;
|
2017-08-21 19:49:21 +00:00
|
|
|
// display record
|
|
|
|
libp2p_logger_debug("test_datastore", "Timestamp: %llu.\n", record->timestamp);
|
|
|
|
libp2p_logger_debug("test_datastore", "Pin: %s.\n", record->pin == 1 ? "Y" : "N");
|
|
|
|
// free record
|
2017-09-04 18:33:56 +00:00
|
|
|
lmdb_journal_record_free(record);
|
2017-08-21 19:49:21 +00:00
|
|
|
record = NULL;
|
|
|
|
op = CURSOR_NEXT;
|
|
|
|
} while (record != NULL);
|
2017-09-07 23:45:09 +00:00
|
|
|
libp2p_logger_error("test_datastore", "Found %d records.\n", recCount);
|
2017-08-21 19:49:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|