null+****@clear*****
null+****@clear*****
2011年 6月 18日 (土) 16:22:59 JST
Daijiro MORI 2011-06-18 07:22:59 +0000 (Sat, 18 Jun 2011) New Revision: 1014333896f594325bde650da7ab3aa801f2396f Log: wrap dat with io Modified files: lib/dat.cpp lib/dat.h Modified: lib/dat.cpp (+87 -45) =================================================================== --- lib/dat.cpp 2011-06-18 05:27:13 +0000 (639565a) +++ lib/dat.cpp 2011-06-18 07:22:59 +0000 (7171933) @@ -14,85 +14,129 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "groonga_in.h" +#include <string.h> +#include "str.h" +#include "io.h" #include "dat.h" #include "util.h" - -namespace grn { -namespace dat { - -class Trie { - public: - static Trie *create(const char *path) { - return new Trie; - } - static Trie *open(const char *path) { - return new Trie; - } - - uint32_t num_keys() const { - return 0; - } - - private: -}; - -} // namespace dat -} // namespace grn +#include "dat/trie.hpp" extern "C" { static void grn_dat_init(grn_dat *dat) { + dat->io = NULL; + dat->header = NULL; + dat->file_id = 0; + dat->encoding = GRN_ENC_DEFAULT; dat->handle = NULL; + dat->tokenizer = NULL; } static void grn_dat_end(grn_dat *dat) { - delete static_cast<grn::dat::Trie *>(dat->handle); - dat->handle = NULL; + if (dat->handle) { + delete static_cast<grn::dat::Trie *>(dat->handle); + dat->handle = NULL; + } +} + +inline static void +gen_pathname(const char *path, char *buffer, int fno) +{ + size_t len = strlen(path); + memcpy(buffer, path, len); + if (fno) { + buffer[len] = '.'; + grn_itoh(fno, buffer + len + 1, 3); + } else { + buffer[len] = '\0'; + } } grn_dat * -grn_dat_create(grn_ctx *ctx, const char *path, unsigned int, - unsigned int, unsigned int) { +grn_dat_create(grn_ctx *ctx, const char *path, uint32_t key_size, + uint32_t value_size, uint32_t flags) +{ + char path2[PATH_MAX]; grn_dat *dat = static_cast<grn_dat *>(GRN_MALLOC(sizeof(grn_dat))); - if (dat == NULL) { - return NULL; - } - grn_dat_init(dat); - dat->handle = grn::dat::Trie::create(path); - if (dat->handle == NULL) { + if (dat) { + grn_dat_init(dat); + if ((dat->io = grn_io_create(ctx, path, sizeof(struct grn_dat_header), + 4096, 0, grn_io_auto, GRN_IO_EXPIRE_SEGMENT))) { + if ((dat->header = (struct grn_dat_header *)grn_io_header(dat->io))) { + return dat; + } + grn_io_close(ctx, dat->io); + grn_io_remove(ctx, path); + } GRN_FREE(dat); - return NULL; + dat = NULL; } return dat; } -grn_dat * -grn_dat_open(grn_ctx *ctx, const char *path) { - grn_dat *dat = static_cast<grn_dat *>(GRN_MALLOC(sizeof(grn_dat))); - if (dat == NULL) { - return NULL; +/* + if (path) { + strcpy(path2, path); + strcat(path2, ".d"); + dat->handle = grn::dat::Trie::create(path2); + } else { + dat->handle = grn::dat::Trie::create(path); + } + if (dat->handle == NULL) { + GRN_FREE(dat); + return NULL; + } } - grn_dat_init(dat); +*/ + +/* dat->handle = grn::dat::Trie::open(path); if (dat->handle == NULL) { GRN_FREE(dat); return NULL; } +*/ + +grn_dat * +grn_dat_open(grn_ctx *ctx, const char *path) +{ + grn_dat *dat = static_cast<grn_dat *>(GRN_MALLOC(sizeof(grn_dat))); + if (dat) { + grn_dat_init(dat); + if ((dat->io = grn_io_open(ctx, path, grn_io_auto))) { + if ((dat->header = (struct grn_dat_header *)grn_io_header(dat->io))) { + dat->file_id = dat->header->file_id; + dat->encoding = dat->header->encoding; + dat->obj.header.flags = dat->header->flags; + dat->tokenizer = grn_ctx_at(ctx, dat->header->tokenizer); + return dat; + } + grn_io_close(ctx, dat->io); + } + GRN_FREE(dat); + dat = NULL; + } return dat; } grn_rc -grn_dat_close(grn_ctx *ctx, grn_dat *dat) { +grn_dat_close(grn_ctx *ctx, grn_dat *dat) +{ grn_dat_end(dat); + grn_io_close(ctx, dat->io); GRN_FREE(dat); return GRN_SUCCESS; } grn_rc -grn_dat_remove(grn_ctx *ctx, const char *path) { - return GRN_SUCCESS; +grn_dat_remove(grn_ctx *ctx, const char *path) +{ + /* FIXME: trie file must be removed */ + grn_io_remove(ctx, path); + return ctx->rc; } grn_id @@ -122,9 +166,7 @@ grn_dat_get_key2(grn_ctx *ctx, grn_dat *dat, grn_id id, grn_obj *bulk) unsigned int grn_dat_size(grn_ctx *ctx, grn_dat *dat) { - if (dat == NULL) { - return GRN_INVALID_ARGUMENT; - } + if (!dat || !dat->handle) { return 0; } return static_cast<grn::dat::Trie *>(dat->handle)->num_keys(); } Modified: lib/dat.h (+3 -11) =================================================================== --- lib/dat.h 2011-06-18 05:27:13 +0000 (6dbc855) +++ lib/dat.h 2011-06-18 07:22:59 +0000 (ab2996b) @@ -33,13 +33,11 @@ typedef struct _grn_dat_cursor grn_dat_cursor; struct _grn_dat { grn_db_obj obj; - grn_dat_handle handle; -// grn_db_obj obj; grn_io *io; struct grn_dat_header *header; + uint32_t file_id; grn_encoding encoding; -// uint32_t key_size; -// uint32_t value_size; + grn_dat_handle handle; grn_obj *tokenizer; }; @@ -47,19 +45,13 @@ struct grn_dat_header { uint32_t flags; grn_encoding encoding; grn_id tokenizer; + uint32_t file_id; }; struct _grn_dat_cursor { grn_db_obj obj; grn_id curr_rec; grn_dat *dat; -// grn_ctx *ctx; -// unsigned int size; -// unsigned int sp; -// grn_id tail; -// unsigned int rest; -// grn_dat_cursor_entry *ss; -// uint8_t curr_key[GRN_TABLE_MAX_KEY_SIZE]; }; GRN_API grn_dat *grn_dat_create(grn_ctx *ctx, const char *path, unsigned int key_size,