--- trunk/perl/HyperEstraierWrapper.cpp 2005/09/03 18:04:41 2 +++ trunk/perl/HyperEstraierWrapper.cpp 2005/10/11 14:04:20 32 @@ -9,8 +9,23 @@ #include #include #include +#include +#include + +/* backward compatibility for 0.5.4 */ +/* +#ifndef ESTCONDAGITO +#define ESTCONDAGITO ESTCONDAGIT +#endif +*/ namespace estraier { + + class IOError : public std::runtime_error { + public: + explicit IOError (const std::string& w) : std::runtime_error(w) {} + }; + class Condition { public: enum { // enumeration for options @@ -71,7 +86,6 @@ std::string text_buf; public: ESTDOC *doc; - Document() { /** * constructor @@ -142,8 +156,7 @@ /** * get a list of sentences of the text of a document object */ - // return est_doc_cat_texts(doc); - return "This is mockup!"; + return est_doc_cat_texts(doc); } std::vector* texts() { /** @@ -169,24 +182,26 @@ */ CBLIST * words; std::vector::iterator iter; - words = cblistopen(); - for (iter = _words.begin(); _words.end() != iter; iter++) { cblistpush(words, iter->c_str(), -1); } - const char *result = est_doc_make_snippet(doc, words, wwidth, hwidth, awidth); - cblistclose(words); - return result; } + const char * hidden_texts() { + /** + * get the hidden texts of a document object. + */ + return est_doc_hidden_texts(doc); + } }; class Database { private: ESTMTDB *db; + int ecode; public: enum { // enumeration for error codes ERRNOERR = ESTENOERR, // no error @@ -225,41 +240,47 @@ /** * constructor(dummy) */ + db = NULL; + ecode = ERRNOERR; } ~Database() { - close(); + if (db) close(); } bool open(const char * dbname, int mode) { /** * open the database */ - int ecode; - db = est_mtdb_open(dbname, mode, &ecode); + if (db) close(); + int ec; + db = est_mtdb_open(dbname, mode, &ec); + if (!db) ecode = ec; return db; } bool close() { /** * close the database */ - if (db) { - int ecode; - bool result = est_mtdb_close(db, &ecode); - db = NULL; - return result; - } else { - return false; - } + if (!db) throw IOError("closed database"); + int ec; + bool result = est_mtdb_close(db, &ec); + if (!result) ecode = ec; + db = NULL; + return result; } bool put_doc(Document *doc, int options) { /** * add a document to a database */ - return est_mtdb_put_doc(db, doc->doc, options); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_put_doc(db, doc->doc, options); + if (!result) ecode = est_mtdb_error(db); + return result; } std::vector * search(Condition * cond, int options) { /** * search documents corresponding a condition for a database */ + if (!db) throw IOError("closed database"); int resnum; int * result = est_mtdb_search(db, cond->cond, &resnum, NULL); std::vector *numbers = new std::vector; @@ -278,44 +299,68 @@ /** * get the last happended error code of a database */ - return est_mtdb_error(db); + return ecode; } bool fatal() { /** * check whether a database has a fatal error */ + if (!db) throw IOError("closed database"); return est_mtdb_fatal(db); } bool flush(int _max) { /** * flush index words in the cache of a database */ - return est_mtdb_flush(db, _max); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_flush(db, _max); + if (!result) ecode = est_mtdb_error(db); + return result; } bool sync() { /** * synchronize updating contents of a database */ - return est_mtdb_sync(db); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_sync(db); + if (!result) ecode = est_mtdb_error(db); + return result; } bool optimize(int options) { /** * optimize a database */ - return est_mtdb_optimize(db, options); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_optimize(db, options); + if (!result) ecode = est_mtdb_error(db); + return result; } bool out_doc(int id, int options) { /** * remove a document from a database */ - return est_mtdb_out_doc(db, id, options); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_out_doc(db, id, options); + if (!result) ecode = est_mtdb_error(db); + return result; + } + bool edit_doc(Document *doc) { + /** + * edit an attribute of a document in a database + */ + if (!db) throw IOError("closed database"); + bool result = est_mtdb_edit_doc(db, doc->doc); + if (!result) ecode = est_mtdb_error(db); + return result; } Document * get_doc(int id, int options) { /** * retrieve a document in a database */ + if (!db) throw IOError("closed database"); ESTDOC *doc = est_mtdb_get_doc(db, id, options); if (!doc) { + ecode = est_mtdb_error(db); throw est_err_msg(est_mtdb_error(db)); } else { return new Document(doc); @@ -325,16 +370,18 @@ /** * get the ID of a document spacified by URI */ - return est_mtdb_uri_to_id(db, uri); + if (!db) throw IOError("closed database"); + int result = est_mtdb_uri_to_id(db, uri); + if(result == -1) ecode = est_mtdb_error(db); + return result; } std::map * etch_doc(Document * doc, int max) { /** * extract keywords of a document object */ + if (!db) throw IOError("closed database"); std::map * mss = new std::map; - CBMAP * keys = est_mtdb_etch_doc(db, doc->doc, max); - cbmapiterinit(keys); int ksiz; while (const char *key = cbmapiternext(keys, &ksiz)) { @@ -342,47 +389,40 @@ } return mss; } - bool iter_init() { - /** - * initialize the iterator of a database - */ - return est_mtdb_iter_init(db); - } - int iter_next() { - /** - * get the next ID of the iterator of a database - */ - return est_mtdb_iter_next(db); - } const char * name() { /** * get the name of a database */ + if (!db) throw IOError("closed database"); return est_mtdb_name(db); } int doc_num() { /** * get the number of documents in a database */ + if (!db) throw IOError("closed database"); return est_mtdb_doc_num(db); } int word_num() { /** * get the number of unique words in a database */ + if (!db) throw IOError("closed database"); return est_mtdb_word_num(db); } double size() { /** * get the size of a database */ + if (!db) throw IOError("closed database"); return est_mtdb_size(db); } - void set_cache_size(size_t size, int anum, int tnum) { + void set_cache_size(size_t size, int anum, int tnum, int rnum) { /** * set the maximum size of the cache memory of a database */ - est_mtdb_set_cache_size(db, size, anum, tnum); + if (!db) throw IOError("closed database"); + est_mtdb_set_cache_size(db, size, anum, tnum, rnum); } void set_special_cache(const char *name, int num) { /** @@ -392,4 +432,183 @@ est_mtdb_set_special_cache(db, name, num); } }; + + static std::vector * break_text(const char *text, bool norm, bool tail) { + std::vector * vs = new std::vector; + CBLIST *list; + list = cblistopen(); + est_break_text(text, list, norm, tail); + for (int i=0; i < cblistnum(list); i++) { + vs->push_back(cblistval(list, i, NULL)); + } + cblistclose(list); + return vs; + } + + static std::vector * break_text_perfng(const char *text, bool norm, bool tail) { + std::vector * vs = new std::vector; + CBLIST *list; + list = cblistopen(); + est_break_text_perfng(text, list, norm, tail); + for (int i=0; i < cblistnum(list); i++) { + vs->push_back(cblistval(list, i, NULL)); + } + cblistclose(list); + return vs; + } + + class ResultDocument { + public: + ESTRESDOC *rdoc; + ResultDocument(ESTRESDOC *_rdoc) { + rdoc = _rdoc; + } + const char *uri(void) { + return est_resdoc_uri(rdoc); + } + std::vector * attr_names() { + std::vector * vs = new std::vector; + CBLIST * attr_names = est_resdoc_attr_names(rdoc); + for (int i=0; i < cblistnum(attr_names); i++) { + vs->push_back(cblistval(attr_names, i, NULL)); + } + cblistclose(attr_names); + return vs; + } + const char *attr(const char *name) { + return est_resdoc_attr(rdoc, name); + } + const char *snippet(void) { + return est_resdoc_snippet(rdoc); + } + }; + + class NodeRes { + private: + ESTNODERES *nres; + public: + NodeRes(ESTNODE *node, Condition *cond, int depth) { + nres = est_node_search(node, cond->cond, depth); + } + ~NodeRes() { + est_noderes_delete(nres); + } + std::map * hints(void) { + std::map * hints = new std::map; + CBMAP * keys = est_noderes_hints(nres); + cbmapiterinit(keys); + int ksiz; + while (const char *key = cbmapiternext(keys, &ksiz)) { + hints->insert(std::make_pair(key, cbmapget(keys, key, ksiz, NULL))); + } + return hints; + } + int doc_num(void) { + return est_noderes_doc_num(nres); + } + ResultDocument * get_doc(int index) { + ESTRESDOC *rdoc = est_noderes_get_doc(nres, index); + if (rdoc) { + return new ResultDocument(rdoc); + } else { + return NULL; + } + } + }; + + class Node { + private: + ESTNODE *node; + int netenv_ok; + public: + Node(const char *url) { + netenv_ok = est_init_net_env(); + if (! netenv_ok) throw IOError("can't init net env"); + node = est_node_new(url); + if (! node) throw IOError("can't create node"); + } + ~Node() { + est_node_delete(node); + est_free_net_env(); + } + void set_proxy(const char *host, int port) { + est_node_set_proxy(node, host, port); + } + void set_timeout(int sec) { + est_node_set_timeout(node, sec); + } + void set_auth(const char *name, const char *passwd) { + est_node_set_auth(node, name, passwd); + } + int status(void) { + return est_node_status(node); + } + bool put_doc(Document *doc) { + return est_node_put_doc(node, doc->doc); + } + bool out_doc(int id) { + return est_node_out_doc(node, id); + } + bool out_doc_by_uri(const char *uri) { + return est_node_out_doc_by_uri(node, uri); + } +#ifdef est_node_edit_doc + bool edit_doc(Document *doc) { + return est_node_edit_doc(node, doc->doc); + } +#endif + Document * get_doc(int id) { + ESTDOC *doc = est_node_get_doc(node, id); + if (!doc) { + return NULL; + } else { + return new Document(doc); + } + } + Document * get_doc_by_uri(const char *uri) { + ESTDOC *doc = est_node_get_doc_by_uri(node, uri); + if (!doc) { + return NULL; + } else { + return new Document(doc); + } + } + char * get_doc_attr(int id, const char *name) { + /* is this leeking memory? shouldn't I create + * object and free memory region returned? + */ + return est_node_get_doc_attr(node, id, name); + } + char * get_doc_attr_by_uri(const char *uri, const char *name) { + return est_node_get_doc_attr_by_uri(node, uri, name); + } + int uri_to_id(const char *uri) { + return est_node_uri_to_id(node, uri); + } + const char * name(void) { + return est_node_name(node); + } + const char * label(void) { + return est_node_label(node); + } + int doc_num(void) { + return est_node_doc_num(node); + } + int word_num(void) { + return est_node_word_num(node); + } + double size(void) { + return est_node_size(node); + } + NodeRes * search(Condition *cond, int depth) { + return new NodeRes(node, cond, depth); + } + int set_user(const char *name, int mode) { + return est_node_set_user(node, name, mode); + } + int set_link(const char *url, const char *label, int credit) { + return est_node_set_link(node, url, label, credit); + } + }; + };