More storage implementation

Successfully writing to lightningdb. Now to pull it back out. Also need
to write to the blockstore.
This commit is contained in:
John Jones 2016-11-30 11:46:41 -05:00
parent 4626b69381
commit b462d9ef53
29 changed files with 448 additions and 123 deletions

18
blocks/Makefile Normal file
View file

@ -0,0 +1,18 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include
ifdef DEBUG
CFLAGS += -g3
endif
LFLAGS =
DEPS = ../include/blocks/block.h ../include/blocks/blockstore.h
OBJS = block.o blockstore.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
all: $(OBJS)
clean:
rm -f *.o

View file

@ -3,6 +3,7 @@
*/
#include <stdlib.h>
#include <string.h>
#include "libp2p/crypto/sha256.h"
#include "ipfs/blocks/block.h"
@ -15,7 +16,7 @@
* @param block a pointer to the struct Block that will be created
* @returns true(1) on success
*/
int ipfs_blocks_block_new(unsigned char* data, size_t data_size, struct Block** block) {
int ipfs_blocks_block_new(const unsigned char* data, size_t data_size, struct Block** block) {
// allocate memory for structure
(*block) = (struct Block*)malloc(sizeof(struct Block));
@ -23,25 +24,27 @@ int ipfs_blocks_block_new(unsigned char* data, size_t data_size, struct Block**
return 0;
// cid
char hash[32];
if (libp2p_crypto_hashing_sha256(data, hash, 32) == 0) {
unsigned char hash[32];
if (libp2p_crypto_hashing_sha256(data, data_size, &hash[0]) == 0) {
free(*block);
return 0;
}
if (ipfs_cid_new(0, hash, 32, CID_PROTOBUF, (*block)->cid) == 0) {
if (ipfs_cid_new(0, hash, 32, CID_PROTOBUF, &((*block)->cid)) == 0) {
free(*block);
return 0;
}
block->data = malloc(sizeof(unsigned char) * data_size);
if (block->data == NULL) {
ipfs_ci_free((*block)->cid);
(*block)->data_length = data_size;
(*block)->data = malloc(sizeof(unsigned char) * data_size);
if ( (*block)->data == NULL) {
ipfs_cid_free((*block)->cid);
free(*block);
return 0;
}
memcpy(block->data, data, data_size);
memcpy( (*block)->data, data, data_size);
return 1;
}

View file

@ -1,14 +1,18 @@
/***
* a thin wrapper over a datastore for getting and putting block objects
*/
#include "libp2p/crypto/encoding/base32.h"
#include "ipfs/cid/cid.h"
#include "ipfs/blocks/block.h"
#include "ipfs/datastore/ds_helper.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
/**
* Delete a block based on its Cid
* @param cid the Cid to look for
* @param returns true(1) on success
*/
int ipfs_blockstore_delete(struct Cid* cid) {
int ipfs_blockstore_delete(struct Cid* cid, struct FSRepo* fs_repo) {
return 0;
}
@ -17,7 +21,7 @@ int ipfs_blockstore_delete(struct Cid* cid) {
* @param cid the Cid to look for
* @returns true(1) if found
*/
int ipfs_blockstore_has(struct Cid* cid) {
int ipfs_blockstore_has(struct Cid* cid, struct FSRepo* fs_repo) {
return 0;
}
@ -27,7 +31,7 @@ int ipfs_blockstore_has(struct Cid* cid) {
* @param block where to put the data to be returned
* @returns true(1) on success
*/
int ipfs_blockstore_get(struct Cid* cid, struct Block* block) {
int ipfs_blockstore_get(struct Cid* cid, struct Block* block, struct FSRepo* fs_repo) {
return 0;
}
@ -36,9 +40,16 @@ int ipfs_blockstore_get(struct Cid* cid, struct Block* block) {
* @param block the block to store
* @returns true(1) on success
*/
int ipfs_blockstore_put(struct Block* block) {
int ipfs_blockstore_put(struct Block* block, struct FSRepo* fs_repo) {
// from blockstore.go line 118
// TODO: Get Datastore key
// TODO: send to Put with key
// Get Datastore key, which is a base32 key of the binary,
size_t key_length = libp2p_crypto_encoding_base32_encode_size(block->data_length);
unsigned char key[key_length];
int retVal = ipfs_datastore_helper_ds_key_from_binary(block->data, block->data_length, &key[0], key_length, &key_length);
if (retVal == 0)
return 0;
// send to Put with key
fs_repo->config->datastore->datastore_put(key, block, fs_repo->config->datastore);
return 0;
}