opener-opinion-detector-basic 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +30 -0
- data/bin/opinion-detector-basic +19 -0
- data/bin/opinion-detector-basic-server +10 -0
- data/config.ru +4 -0
- data/core/opinion_detector_basic_multi.py +499 -0
- data/core/packages/KafNafParser-1.3.tar.gz +0 -0
- data/core/packages/VUA_pylib-1.4.tar.gz +0 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/PKG-INFO +10 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/SOURCES.txt +7 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/dependency_links.txt +1 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/installed-files.txt +11 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/top_level.txt +1 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafDataObjectsMod.py +165 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafDataObjectsMod.pyc +0 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafParserMod.py +439 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafParserMod.pyc +0 -0
- data/core/site-packages/pre_build/VUKafParserPy/__init__.py +7 -0
- data/core/site-packages/pre_build/VUKafParserPy/__init__.pyc +0 -0
- data/core/vendor/src/crfsuite/AUTHORS +1 -0
- data/core/vendor/src/crfsuite/COPYING +27 -0
- data/core/vendor/src/crfsuite/ChangeLog +103 -0
- data/core/vendor/src/crfsuite/INSTALL +236 -0
- data/core/vendor/src/crfsuite/Makefile.am +19 -0
- data/core/vendor/src/crfsuite/Makefile.in +783 -0
- data/core/vendor/src/crfsuite/README +183 -0
- data/core/vendor/src/crfsuite/aclocal.m4 +9018 -0
- data/core/vendor/src/crfsuite/autogen.sh +38 -0
- data/core/vendor/src/crfsuite/compile +143 -0
- data/core/vendor/src/crfsuite/config.guess +1502 -0
- data/core/vendor/src/crfsuite/config.h.in +198 -0
- data/core/vendor/src/crfsuite/config.sub +1714 -0
- data/core/vendor/src/crfsuite/configure +14273 -0
- data/core/vendor/src/crfsuite/configure.in +149 -0
- data/core/vendor/src/crfsuite/crfsuite.sln +42 -0
- data/core/vendor/src/crfsuite/depcomp +630 -0
- data/core/vendor/src/crfsuite/example/chunking.py +49 -0
- data/core/vendor/src/crfsuite/example/crfutils.py +179 -0
- data/core/vendor/src/crfsuite/example/ner.py +270 -0
- data/core/vendor/src/crfsuite/example/pos.py +78 -0
- data/core/vendor/src/crfsuite/example/template.py +88 -0
- data/core/vendor/src/crfsuite/frontend/Makefile.am +29 -0
- data/core/vendor/src/crfsuite/frontend/Makefile.in +640 -0
- data/core/vendor/src/crfsuite/frontend/dump.c +116 -0
- data/core/vendor/src/crfsuite/frontend/frontend.vcxproj +129 -0
- data/core/vendor/src/crfsuite/frontend/iwa.c +273 -0
- data/core/vendor/src/crfsuite/frontend/iwa.h +65 -0
- data/core/vendor/src/crfsuite/frontend/learn.c +439 -0
- data/core/vendor/src/crfsuite/frontend/main.c +137 -0
- data/core/vendor/src/crfsuite/frontend/option.c +93 -0
- data/core/vendor/src/crfsuite/frontend/option.h +86 -0
- data/core/vendor/src/crfsuite/frontend/readdata.h +38 -0
- data/core/vendor/src/crfsuite/frontend/reader.c +136 -0
- data/core/vendor/src/crfsuite/frontend/tag.c +427 -0
- data/core/vendor/src/crfsuite/genbinary.sh.in +15 -0
- data/core/vendor/src/crfsuite/include/Makefile.am +11 -0
- data/core/vendor/src/crfsuite/include/Makefile.in +461 -0
- data/core/vendor/src/crfsuite/include/crfsuite.h +1063 -0
- data/core/vendor/src/crfsuite/include/crfsuite.hpp +555 -0
- data/core/vendor/src/crfsuite/include/crfsuite_api.hpp +400 -0
- data/core/vendor/src/crfsuite/include/os.h +61 -0
- data/core/vendor/src/crfsuite/install-sh +520 -0
- data/core/vendor/src/crfsuite/lib/cqdb/COPYING +28 -0
- data/core/vendor/src/crfsuite/lib/cqdb/Makefile.am +21 -0
- data/core/vendor/src/crfsuite/lib/cqdb/Makefile.in +549 -0
- data/core/vendor/src/crfsuite/lib/cqdb/cqdb.vcxproj +86 -0
- data/core/vendor/src/crfsuite/lib/cqdb/include/cqdb.h +524 -0
- data/core/vendor/src/crfsuite/lib/cqdb/src/cqdb.c +587 -0
- data/core/vendor/src/crfsuite/lib/cqdb/src/lookup3.c +976 -0
- data/core/vendor/src/crfsuite/lib/crf/Makefile.am +46 -0
- data/core/vendor/src/crfsuite/lib/crf/Makefile.in +721 -0
- data/core/vendor/src/crfsuite/lib/crf/crf.vcxproj +216 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d.h +353 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_context.c +705 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_encode.c +943 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_feature.c +352 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_model.c +994 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_tag.c +550 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crfsuite.c +492 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crfsuite_internal.h +236 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crfsuite_train.c +272 -0
- data/core/vendor/src/crfsuite/lib/crf/src/dataset.c +106 -0
- data/core/vendor/src/crfsuite/lib/crf/src/dictionary.c +118 -0
- data/core/vendor/src/crfsuite/lib/crf/src/holdout.c +80 -0
- data/core/vendor/src/crfsuite/lib/crf/src/logging.c +91 -0
- data/core/vendor/src/crfsuite/lib/crf/src/logging.h +48 -0
- data/core/vendor/src/crfsuite/lib/crf/src/params.c +335 -0
- data/core/vendor/src/crfsuite/lib/crf/src/params.h +80 -0
- data/core/vendor/src/crfsuite/lib/crf/src/quark.c +172 -0
- data/core/vendor/src/crfsuite/lib/crf/src/quark.h +46 -0
- data/core/vendor/src/crfsuite/lib/crf/src/rumavl.c +1107 -0
- data/core/vendor/src/crfsuite/lib/crf/src/rumavl.h +160 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_arow.c +408 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_averaged_perceptron.c +242 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_l2sgd.c +507 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_lbfgs.c +338 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_passive_aggressive.c +435 -0
- data/core/vendor/src/crfsuite/lib/crf/src/vecmath.h +341 -0
- data/core/vendor/src/crfsuite/ltmain.sh +8413 -0
- data/core/vendor/src/crfsuite/missing +376 -0
- data/core/vendor/src/crfsuite/swig/Makefile.am +13 -0
- data/core/vendor/src/crfsuite/swig/Makefile.in +365 -0
- data/core/vendor/src/crfsuite/swig/crfsuite.cpp +2 -0
- data/core/vendor/src/crfsuite/swig/export.i +32 -0
- data/core/vendor/src/crfsuite/swig/python/README +92 -0
- data/core/vendor/src/crfsuite/swig/python/crfsuite.py +329 -0
- data/core/vendor/src/crfsuite/swig/python/export_wrap.cpp +14355 -0
- data/core/vendor/src/crfsuite/swig/python/export_wrap.h +63 -0
- data/core/vendor/src/crfsuite/swig/python/prepare.sh +9 -0
- data/core/vendor/src/crfsuite/swig/python/sample_tag.py +52 -0
- data/core/vendor/src/crfsuite/swig/python/sample_train.py +68 -0
- data/core/vendor/src/crfsuite/swig/python/setup.py +44 -0
- data/core/vendor/src/crfsuite/win32/stdint.h +679 -0
- data/core/vendor/src/liblbfgs/AUTHORS +1 -0
- data/core/vendor/src/liblbfgs/COPYING +22 -0
- data/core/vendor/src/liblbfgs/ChangeLog +120 -0
- data/core/vendor/src/liblbfgs/INSTALL +231 -0
- data/core/vendor/src/liblbfgs/Makefile.am +10 -0
- data/core/vendor/src/liblbfgs/Makefile.in +638 -0
- data/core/vendor/src/liblbfgs/NEWS +0 -0
- data/core/vendor/src/liblbfgs/README +71 -0
- data/core/vendor/src/liblbfgs/aclocal.m4 +6985 -0
- data/core/vendor/src/liblbfgs/autogen.sh +38 -0
- data/core/vendor/src/liblbfgs/config.guess +1411 -0
- data/core/vendor/src/liblbfgs/config.h.in +64 -0
- data/core/vendor/src/liblbfgs/config.sub +1500 -0
- data/core/vendor/src/liblbfgs/configure +21146 -0
- data/core/vendor/src/liblbfgs/configure.in +107 -0
- data/core/vendor/src/liblbfgs/depcomp +522 -0
- data/core/vendor/src/liblbfgs/include/lbfgs.h +745 -0
- data/core/vendor/src/liblbfgs/install-sh +322 -0
- data/core/vendor/src/liblbfgs/lbfgs.sln +26 -0
- data/core/vendor/src/liblbfgs/lib/Makefile.am +24 -0
- data/core/vendor/src/liblbfgs/lib/Makefile.in +499 -0
- data/core/vendor/src/liblbfgs/lib/arithmetic_ansi.h +133 -0
- data/core/vendor/src/liblbfgs/lib/arithmetic_sse_double.h +294 -0
- data/core/vendor/src/liblbfgs/lib/arithmetic_sse_float.h +298 -0
- data/core/vendor/src/liblbfgs/lib/lbfgs.c +1371 -0
- data/core/vendor/src/liblbfgs/lib/lib.vcxproj +95 -0
- data/core/vendor/src/liblbfgs/ltmain.sh +6426 -0
- data/core/vendor/src/liblbfgs/missing +353 -0
- data/core/vendor/src/liblbfgs/sample/Makefile.am +15 -0
- data/core/vendor/src/liblbfgs/sample/Makefile.in +433 -0
- data/core/vendor/src/liblbfgs/sample/sample.c +81 -0
- data/core/vendor/src/liblbfgs/sample/sample.cpp +126 -0
- data/core/vendor/src/liblbfgs/sample/sample.vcxproj +105 -0
- data/core/vendor/src/svm_light/LICENSE.txt +59 -0
- data/core/vendor/src/svm_light/Makefile +105 -0
- data/core/vendor/src/svm_light/kernel.h +40 -0
- data/core/vendor/src/svm_light/svm_classify.c +197 -0
- data/core/vendor/src/svm_light/svm_common.c +985 -0
- data/core/vendor/src/svm_light/svm_common.h +301 -0
- data/core/vendor/src/svm_light/svm_hideo.c +1062 -0
- data/core/vendor/src/svm_light/svm_learn.c +4147 -0
- data/core/vendor/src/svm_light/svm_learn.h +169 -0
- data/core/vendor/src/svm_light/svm_learn_main.c +397 -0
- data/core/vendor/src/svm_light/svm_loqo.c +211 -0
- data/ext/hack/Rakefile +17 -0
- data/ext/hack/support.rb +88 -0
- data/lib/opener/opinion_detector_basic.rb +91 -0
- data/lib/opener/opinion_detector_basic/public/markdown.css +284 -0
- data/lib/opener/opinion_detector_basic/server.rb +16 -0
- data/lib/opener/opinion_detector_basic/version.rb +5 -0
- data/lib/opener/opinion_detector_basic/views/index.erb +97 -0
- data/lib/opener/opinion_detector_basic/views/result.erb +15 -0
- data/opener-opinion-detector-basic.gemspec +36 -0
- data/pre_build_requirements.txt +1 -0
- metadata +309 -0
@@ -0,0 +1,587 @@
|
|
1
|
+
/*
|
2
|
+
* Constant Quark Database (CQDB).
|
3
|
+
*
|
4
|
+
* Copyright (c) 2007, Naoaki Okazaki
|
5
|
+
* All rights reserved.
|
6
|
+
*
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
9
|
+
* * Redistributions of source code must retain the above copyright
|
10
|
+
* notice, this list of conditions and the following disclaimer.
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright
|
12
|
+
* notice, this list of conditions and the following disclaimer in the
|
13
|
+
* documentation and/or other materials provided with the distribution.
|
14
|
+
* * Neither the name of the Northwestern University, University of Tokyo,
|
15
|
+
* nor the names of its contributors may be used to endorse or promote
|
16
|
+
* products derived from this software without specific prior written
|
17
|
+
* permission.
|
18
|
+
*
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
23
|
+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
24
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
25
|
+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
26
|
+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
27
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
28
|
+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
29
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
*/
|
31
|
+
|
32
|
+
/* $Id$ */
|
33
|
+
|
34
|
+
#include <stdio.h>
|
35
|
+
#include <stdlib.h>
|
36
|
+
#include <stdint.h>
|
37
|
+
#include <string.h>
|
38
|
+
|
39
|
+
#include <cqdb.h>
|
40
|
+
|
41
|
+
#define CHUNKID "CQDB"
|
42
|
+
#define BYTEORDER_CHECK (0x62445371)
|
43
|
+
#define NUM_TABLES (256)
|
44
|
+
#define OFFSET_REFS (0 + sizeof(header_t))
|
45
|
+
#define OFFSET_DATA (OFFSET_REFS + sizeof(tableref_t) * NUM_TABLES)
|
46
|
+
|
47
|
+
/**
|
48
|
+
* An element of a hash table.
|
49
|
+
*/
|
50
|
+
typedef struct {
|
51
|
+
uint32_t hash; /**< Hash value of the record. */
|
52
|
+
uint32_t offset; /**< Offset address to the actual record. */
|
53
|
+
} bucket_t;
|
54
|
+
|
55
|
+
/**
|
56
|
+
* A hash table.
|
57
|
+
*/
|
58
|
+
typedef struct {
|
59
|
+
uint32_t num; /**< Number of elements in the table. */
|
60
|
+
uint32_t size; /**< Maximum number of elements. */
|
61
|
+
bucket_t* bucket; /**< Bucket (array of bucket_t). */
|
62
|
+
} table_t;
|
63
|
+
|
64
|
+
/**
|
65
|
+
* CQDB chunk header.
|
66
|
+
*/
|
67
|
+
typedef struct {
|
68
|
+
int8_t chunkid[4]; /**< Chunk identifier, "CQDB". */
|
69
|
+
uint32_t size; /**< Chunk size including this header. */
|
70
|
+
uint32_t flag; /**< Global flags. */
|
71
|
+
uint32_t byteorder; /**< Byte-order indicator. */
|
72
|
+
uint32_t bwd_size; /**< Number of elements in the backward array. */
|
73
|
+
uint32_t bwd_offset; /**< Offset to the backward array. */
|
74
|
+
} header_t;
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Reference to a hash table.
|
78
|
+
*/
|
79
|
+
typedef struct {
|
80
|
+
uint32_t offset; /**< Offset to a hash table. */
|
81
|
+
uint32_t num; /**< Number of elements in the hash table. */
|
82
|
+
} tableref_t;
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Writer for a constant quark database.
|
86
|
+
*/
|
87
|
+
struct tag_cqdb_writer {
|
88
|
+
uint32_t flag; /**< Operation flag. */
|
89
|
+
FILE* fp; /**< File pointer. */
|
90
|
+
uint32_t begin; /**< Offset address to the head of this database. */
|
91
|
+
uint32_t cur; /**< Offset address to a new key/data pair. */
|
92
|
+
table_t ht[NUM_TABLES]; /**< Hash tables (string -> id). */
|
93
|
+
|
94
|
+
uint32_t* bwd; /**< Backlink array. */
|
95
|
+
uint32_t bwd_num; /**< */
|
96
|
+
uint32_t bwd_size; /**< Number of elements in the backlink array. */
|
97
|
+
};
|
98
|
+
|
99
|
+
/**
|
100
|
+
* Constant quark database (CQDB).
|
101
|
+
*/
|
102
|
+
struct tag_cqdb {
|
103
|
+
uint8_t* buffer; /**< Pointer to the memory block. */
|
104
|
+
size_t size; /**< Size of the memory block. */
|
105
|
+
|
106
|
+
header_t header; /**< Chunk header. */
|
107
|
+
table_t ht[NUM_TABLES]; /**< Hash tables (string -> id). */
|
108
|
+
|
109
|
+
uint32_t* bwd; /**< Array for backward look-up (id -> string). */
|
110
|
+
|
111
|
+
int num; /**< Number of key/data pairs. */
|
112
|
+
};
|
113
|
+
|
114
|
+
|
115
|
+
uint32_t hashlittle(const void *key, size_t length, uint32_t initval);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
static size_t write_uint32(cqdb_writer_t* wt, uint32_t value)
|
121
|
+
{
|
122
|
+
uint8_t buffer[4];
|
123
|
+
buffer[0] = (uint8_t)(value & 0xFF);
|
124
|
+
buffer[1] = (uint8_t)(value >> 8);
|
125
|
+
buffer[2] = (uint8_t)(value >> 16);
|
126
|
+
buffer[3] = (uint8_t)(value >> 24);
|
127
|
+
return fwrite(buffer, sizeof(uint8_t), 4, wt->fp) / sizeof(value);
|
128
|
+
}
|
129
|
+
|
130
|
+
static size_t write_data(cqdb_writer_t* wt, const void *data, size_t size)
|
131
|
+
{
|
132
|
+
return fwrite(data, size, 1, wt->fp);
|
133
|
+
}
|
134
|
+
|
135
|
+
cqdb_writer_t* cqdb_writer(FILE *fp, int flag)
|
136
|
+
{
|
137
|
+
int i;
|
138
|
+
cqdb_writer_t* dbw = (cqdb_writer_t*)calloc(1, sizeof(cqdb_writer_t));
|
139
|
+
|
140
|
+
if (dbw != NULL) {
|
141
|
+
/* Initialize cqdb_writer_t members. */
|
142
|
+
memset(dbw, 0, sizeof(*dbw));
|
143
|
+
dbw->flag = flag;
|
144
|
+
dbw->fp = fp;
|
145
|
+
dbw->begin = ftell(dbw->fp);
|
146
|
+
dbw->cur = OFFSET_DATA;
|
147
|
+
|
148
|
+
/* Initialize the hash tables.*/
|
149
|
+
for (i = 0;i < NUM_TABLES;++i) {
|
150
|
+
dbw->ht[i].bucket = NULL;
|
151
|
+
}
|
152
|
+
|
153
|
+
dbw->bwd = NULL;
|
154
|
+
dbw->bwd_num = 0;
|
155
|
+
dbw->bwd_size = 0;
|
156
|
+
|
157
|
+
/* Move the file pointer to the offset to the first key/data pair. */
|
158
|
+
if (fseek(dbw->fp, dbw->begin + dbw->cur, SEEK_SET) != 0) {
|
159
|
+
goto error_exit; /* Seek error. */
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
return dbw;
|
164
|
+
|
165
|
+
error_exit:
|
166
|
+
free(dbw);
|
167
|
+
return NULL;
|
168
|
+
}
|
169
|
+
|
170
|
+
static int cqdb_writer_delete(cqdb_writer_t* dbw)
|
171
|
+
{
|
172
|
+
int i;
|
173
|
+
|
174
|
+
/* Free allocated memory blocks. */
|
175
|
+
for (i = 0;i < NUM_TABLES;++i) {
|
176
|
+
free(dbw->ht[i].bucket);
|
177
|
+
}
|
178
|
+
free(dbw->bwd);
|
179
|
+
free(dbw);
|
180
|
+
return 0;
|
181
|
+
}
|
182
|
+
|
183
|
+
int cqdb_writer_put(cqdb_writer_t* dbw, const char *str, int id)
|
184
|
+
{
|
185
|
+
int ret = 0;
|
186
|
+
const void *key = str;
|
187
|
+
uint32_t ksize = (uint32_t)(strlen(str) + 1);
|
188
|
+
|
189
|
+
/* Compute the hash value and choose a hash table. */
|
190
|
+
uint32_t hv = hashlittle(key, ksize, 0);
|
191
|
+
table_t* ht = &dbw->ht[hv % 256];
|
192
|
+
|
193
|
+
/* Check for non-negative identifier. */
|
194
|
+
if (id < 0) {
|
195
|
+
ret = CQDB_ERROR_INVALIDID;
|
196
|
+
goto error_exit;
|
197
|
+
}
|
198
|
+
|
199
|
+
/* Write out the current data. */
|
200
|
+
write_uint32(dbw, (uint32_t)id);
|
201
|
+
write_uint32(dbw, (uint32_t)ksize);
|
202
|
+
write_data(dbw, key, ksize);
|
203
|
+
if (ferror(dbw->fp)) {
|
204
|
+
ret = CQDB_ERROR_FILEWRITE;
|
205
|
+
goto error_exit;
|
206
|
+
}
|
207
|
+
|
208
|
+
/* Expand the bucket if necessary. */
|
209
|
+
if (ht->size <= ht->num) {
|
210
|
+
ht->size = (ht->size+1) * 2;
|
211
|
+
ht->bucket = (bucket_t*)realloc(ht->bucket, sizeof(bucket_t) * ht->size);
|
212
|
+
if (ht->bucket == NULL) {
|
213
|
+
ret = CQDB_ERROR_OUTOFMEMORY;
|
214
|
+
goto error_exit;
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
/* Set the hash value and current offset position. */
|
219
|
+
ht->bucket[ht->num].hash = hv;
|
220
|
+
ht->bucket[ht->num].offset = dbw->cur;
|
221
|
+
++ht->num;
|
222
|
+
|
223
|
+
/* Store the backlink if specified. */
|
224
|
+
if (!(dbw->flag & CQDB_ONEWAY)) {
|
225
|
+
/* Expand the backlink array if necessary. */
|
226
|
+
if (dbw->bwd_size <= (uint32_t)id) {
|
227
|
+
uint32_t size = dbw->bwd_size;
|
228
|
+
|
229
|
+
while (size <= (uint32_t)id) size = (size + 1) * 2;
|
230
|
+
dbw->bwd = (uint32_t*)realloc(dbw->bwd, sizeof(uint32_t) * size);
|
231
|
+
if (dbw->bwd == NULL) {
|
232
|
+
ret = CQDB_ERROR_OUTOFMEMORY;
|
233
|
+
goto error_exit;
|
234
|
+
}
|
235
|
+
while (dbw->bwd_size < size) {
|
236
|
+
dbw->bwd[dbw->bwd_size++] = 0;
|
237
|
+
}
|
238
|
+
}
|
239
|
+
|
240
|
+
if (dbw->bwd_num <= (uint32_t)id) {
|
241
|
+
dbw->bwd_num = (uint32_t)id+1;
|
242
|
+
}
|
243
|
+
|
244
|
+
dbw->bwd[id] = dbw->cur;
|
245
|
+
}
|
246
|
+
|
247
|
+
/* Increment the current position. */
|
248
|
+
dbw->cur += sizeof(uint32_t) + sizeof(uint32_t) + ksize;
|
249
|
+
return 0;
|
250
|
+
|
251
|
+
error_exit:
|
252
|
+
dbw->flag |= CQDB_ERROR_OCCURRED;
|
253
|
+
return ret;
|
254
|
+
}
|
255
|
+
|
256
|
+
int cqdb_writer_close(cqdb_writer_t* dbw)
|
257
|
+
{
|
258
|
+
uint32_t i, j;
|
259
|
+
int k, ret = 0;
|
260
|
+
long offset = 0;
|
261
|
+
header_t header;
|
262
|
+
|
263
|
+
/* If an error have occurred, just free the memory blocks. */
|
264
|
+
if (dbw->flag & CQDB_ERROR_OCCURRED) {
|
265
|
+
cqdb_writer_delete(dbw);
|
266
|
+
return 0;
|
267
|
+
}
|
268
|
+
|
269
|
+
/* Initialize the file header. */
|
270
|
+
strncpy((char*)header.chunkid, CHUNKID, 4);
|
271
|
+
header.byteorder = BYTEORDER_CHECK;
|
272
|
+
header.bwd_offset = 0;
|
273
|
+
header.bwd_size = dbw->bwd_num;
|
274
|
+
|
275
|
+
/*
|
276
|
+
Store the hash tables. At this moment, the file pointer refers to
|
277
|
+
the offset succeeding the last key/data pair.
|
278
|
+
*/
|
279
|
+
for (i = 0;i < NUM_TABLES;++i) {
|
280
|
+
table_t* ht = &dbw->ht[i];
|
281
|
+
|
282
|
+
/* Do not write empty hash tables. */
|
283
|
+
if (ht->bucket != NULL) {
|
284
|
+
/*
|
285
|
+
Actual bucket will have the double size; half elements
|
286
|
+
in the bucket are kept empty.
|
287
|
+
*/
|
288
|
+
int n = ht->num * 2;
|
289
|
+
|
290
|
+
/* Allocate the bucket. */
|
291
|
+
bucket_t* dst = (bucket_t*)calloc(n, sizeof(bucket_t));
|
292
|
+
if (dst == NULL) {
|
293
|
+
ret = CQDB_ERROR_OUTOFMEMORY;
|
294
|
+
goto error_exit;
|
295
|
+
}
|
296
|
+
|
297
|
+
/*
|
298
|
+
Put hash elements to the bucket with the open-address method.
|
299
|
+
*/
|
300
|
+
for (j = 0;j < ht->num;++j) {
|
301
|
+
const bucket_t* src = &ht->bucket[j];
|
302
|
+
int k = (src->hash >> 8) % n;
|
303
|
+
|
304
|
+
/* Find a vacant element. */
|
305
|
+
while (dst[k].offset != 0) {
|
306
|
+
k = (k+1) % n;
|
307
|
+
}
|
308
|
+
|
309
|
+
/* Store the hash element. */
|
310
|
+
dst[k].hash = src->hash;
|
311
|
+
dst[k].offset = src->offset;
|
312
|
+
}
|
313
|
+
|
314
|
+
/* Write the bucket. */
|
315
|
+
for (k = 0;k < n;++k) {
|
316
|
+
write_uint32(dbw, dst[k].hash);
|
317
|
+
write_uint32(dbw, dst[k].offset);
|
318
|
+
}
|
319
|
+
|
320
|
+
/* Free the bucket. */
|
321
|
+
free(dst);
|
322
|
+
}
|
323
|
+
}
|
324
|
+
|
325
|
+
/* Write the backlink array if specified. */
|
326
|
+
if (!(dbw->flag & CQDB_ONEWAY) && 0 < dbw->bwd_size) {
|
327
|
+
/* Store the offset to the head of this array. */
|
328
|
+
header.bwd_offset = ftell(dbw->fp) - dbw->begin;
|
329
|
+
/* Store the contents of the backlink array. */
|
330
|
+
for (i = 0;i < dbw->bwd_num;++i) {
|
331
|
+
write_uint32(dbw, dbw->bwd[i]);
|
332
|
+
}
|
333
|
+
}
|
334
|
+
|
335
|
+
/* Check for an occurrence of a file-related error. */
|
336
|
+
if (ferror(dbw->fp)) {
|
337
|
+
ret = CQDB_ERROR_FILEWRITE;
|
338
|
+
goto error_exit;
|
339
|
+
}
|
340
|
+
|
341
|
+
/* Store the current position. */
|
342
|
+
offset = ftell(dbw->fp);
|
343
|
+
if (offset == -1) {
|
344
|
+
ret = CQDB_ERROR_FILETELL;
|
345
|
+
goto error_exit;
|
346
|
+
}
|
347
|
+
header.size = (uint32_t)offset - dbw->begin;
|
348
|
+
|
349
|
+
/* Rewind the current position to the beginning. */
|
350
|
+
if (fseek(dbw->fp, dbw->begin, SEEK_SET) != 0) {
|
351
|
+
ret = CQDB_ERROR_FILESEEK;
|
352
|
+
goto error_exit;
|
353
|
+
}
|
354
|
+
|
355
|
+
/* Write the file header. */
|
356
|
+
write_data(dbw, header.chunkid, 4);
|
357
|
+
write_uint32(dbw, header.size);
|
358
|
+
write_uint32(dbw, header.flag);
|
359
|
+
write_uint32(dbw, header.byteorder);
|
360
|
+
write_uint32(dbw, header.bwd_size);
|
361
|
+
write_uint32(dbw, header.bwd_offset);
|
362
|
+
|
363
|
+
/*
|
364
|
+
Write references to hash tables. At this moment, dbw->cur points
|
365
|
+
to the offset succeeding the last key/data pair.
|
366
|
+
*/
|
367
|
+
for (i = 0;i < NUM_TABLES;++i) {
|
368
|
+
/* Offset to the hash table (or zero for non-existent tables). */
|
369
|
+
write_uint32(dbw, dbw->ht[i].num ? dbw->cur : 0);
|
370
|
+
/* Bucket size is double to the number of elements. */
|
371
|
+
write_uint32(dbw, dbw->ht[i].num * 2);
|
372
|
+
/* Advance the offset counter. */
|
373
|
+
dbw->cur += (dbw->ht[i].num * 2) * sizeof(bucket_t);
|
374
|
+
}
|
375
|
+
|
376
|
+
/* Check an occurrence of a file-related error. */
|
377
|
+
if (ferror(dbw->fp)) {
|
378
|
+
ret = CQDB_ERROR_FILEWRITE;
|
379
|
+
goto error_exit;
|
380
|
+
}
|
381
|
+
|
382
|
+
/* Seek to the last position. */
|
383
|
+
if (fseek(dbw->fp, offset, SEEK_SET) != 0) {
|
384
|
+
ret = CQDB_ERROR_FILESEEK;
|
385
|
+
goto error_exit;
|
386
|
+
}
|
387
|
+
|
388
|
+
cqdb_writer_delete(dbw);
|
389
|
+
return ret;
|
390
|
+
|
391
|
+
error_exit:
|
392
|
+
/* Seek to the first position. */
|
393
|
+
fseek(dbw->fp, dbw->begin, SEEK_SET);
|
394
|
+
cqdb_writer_delete(dbw);
|
395
|
+
return ret;
|
396
|
+
}
|
397
|
+
|
398
|
+
|
399
|
+
|
400
|
+
static uint32_t read_uint32(uint8_t* p)
|
401
|
+
{
|
402
|
+
uint32_t value;
|
403
|
+
value = ((uint32_t)p[0]);
|
404
|
+
value |= ((uint32_t)p[1] << 8);
|
405
|
+
value |= ((uint32_t)p[2] << 16);
|
406
|
+
value |= ((uint32_t)p[3] << 24);
|
407
|
+
return value;
|
408
|
+
}
|
409
|
+
|
410
|
+
static uint8_t *read_tableref(tableref_t* ref, uint8_t *p)
|
411
|
+
{
|
412
|
+
ref->offset = read_uint32(p);
|
413
|
+
p += sizeof(uint32_t);
|
414
|
+
ref->num = read_uint32(p);
|
415
|
+
p += sizeof(uint32_t);
|
416
|
+
return p;
|
417
|
+
}
|
418
|
+
|
419
|
+
static bucket_t* read_bucket(uint8_t* p, uint32_t num)
|
420
|
+
{
|
421
|
+
uint32_t i;
|
422
|
+
bucket_t *bucket = (bucket_t*)calloc(num, sizeof(bucket_t));
|
423
|
+
for (i = 0;i < num;++i) {
|
424
|
+
bucket[i].hash = read_uint32(p);
|
425
|
+
p += sizeof(uint32_t);
|
426
|
+
bucket[i].offset = read_uint32(p);
|
427
|
+
p += sizeof(uint32_t);
|
428
|
+
}
|
429
|
+
return bucket;
|
430
|
+
}
|
431
|
+
|
432
|
+
static uint32_t* read_backward_links(uint8_t* p, uint32_t num)
|
433
|
+
{
|
434
|
+
uint32_t i;
|
435
|
+
uint32_t *bwd = (uint32_t*)calloc(num, sizeof(uint32_t));
|
436
|
+
for (i = 0;i < num;++i) {
|
437
|
+
bwd[i] = read_uint32(p);
|
438
|
+
p += sizeof(uint32_t);
|
439
|
+
}
|
440
|
+
return bwd;
|
441
|
+
}
|
442
|
+
|
443
|
+
cqdb_t* cqdb_reader(void *buffer, size_t size)
|
444
|
+
{
|
445
|
+
int i;
|
446
|
+
cqdb_t* db = NULL;
|
447
|
+
|
448
|
+
/* The minimum size of a valid CQDB is OFFSET_DATA. */
|
449
|
+
if (size < OFFSET_DATA) {
|
450
|
+
return NULL;
|
451
|
+
}
|
452
|
+
|
453
|
+
/* Check the file chunkid. */
|
454
|
+
if (memcmp(buffer, CHUNKID, 4) != 0) {
|
455
|
+
return NULL;
|
456
|
+
}
|
457
|
+
|
458
|
+
db = (cqdb_t*)calloc(1, sizeof(cqdb_t));
|
459
|
+
if (db != NULL) {
|
460
|
+
uint8_t* p = NULL;
|
461
|
+
|
462
|
+
/* Set memory block and size. */
|
463
|
+
db->buffer = buffer;
|
464
|
+
db->size = size;
|
465
|
+
|
466
|
+
/* Read the database header. */
|
467
|
+
p = db->buffer;
|
468
|
+
strncpy((char*)db->header.chunkid, (const char*)p, 4);
|
469
|
+
p += sizeof(uint32_t);
|
470
|
+
db->header.size = read_uint32(p);
|
471
|
+
p += sizeof(uint32_t);
|
472
|
+
db->header.flag = read_uint32(p);
|
473
|
+
p += sizeof(uint32_t);
|
474
|
+
db->header.byteorder = read_uint32(p);
|
475
|
+
p += sizeof(uint32_t);
|
476
|
+
db->header.bwd_size = read_uint32(p);
|
477
|
+
p += sizeof(uint32_t);
|
478
|
+
db->header.bwd_offset = read_uint32(p);
|
479
|
+
p += sizeof(uint32_t);
|
480
|
+
|
481
|
+
/* Check the consistency of byte order. */
|
482
|
+
if (db->header.byteorder != BYTEORDER_CHECK) {
|
483
|
+
free(db);
|
484
|
+
return NULL;
|
485
|
+
}
|
486
|
+
|
487
|
+
/* Check the chunk size. */
|
488
|
+
if (size < db->header.size) {
|
489
|
+
free(db);
|
490
|
+
return NULL;
|
491
|
+
}
|
492
|
+
|
493
|
+
/* Set pointers to the hash tables. */
|
494
|
+
db->num = 0; /* Number of records. */
|
495
|
+
p = (db->buffer + OFFSET_REFS);
|
496
|
+
for (i = 0;i < NUM_TABLES;++i) {
|
497
|
+
tableref_t ref;
|
498
|
+
p = read_tableref(&ref, p);
|
499
|
+
if (ref.offset) {
|
500
|
+
/* Set buckets. */
|
501
|
+
db->ht[i].bucket = read_bucket(db->buffer + ref.offset, ref.num);
|
502
|
+
db->ht[i].num = ref.num;
|
503
|
+
} else {
|
504
|
+
/* An empty hash table. */
|
505
|
+
db->ht[i].bucket = NULL;
|
506
|
+
db->ht[i].num = 0;
|
507
|
+
}
|
508
|
+
|
509
|
+
/* The number of records is the half of the table size.*/
|
510
|
+
db->num += ref.num / 2;
|
511
|
+
}
|
512
|
+
|
513
|
+
/* Set the pointer to the backlink array if any. */
|
514
|
+
if (db->header.bwd_offset) {
|
515
|
+
db->bwd = read_backward_links(db->buffer + db->header.bwd_offset, db->num);
|
516
|
+
} else {
|
517
|
+
db->bwd = NULL;
|
518
|
+
}
|
519
|
+
}
|
520
|
+
|
521
|
+
return db;
|
522
|
+
}
|
523
|
+
|
524
|
+
void cqdb_delete(cqdb_t* db)
|
525
|
+
{
|
526
|
+
int i;
|
527
|
+
|
528
|
+
if (db != NULL) {
|
529
|
+
for (i = 0;i < NUM_TABLES;++i) {
|
530
|
+
free(db->ht[i].bucket);
|
531
|
+
}
|
532
|
+
free(db->bwd);
|
533
|
+
free(db);
|
534
|
+
}
|
535
|
+
}
|
536
|
+
|
537
|
+
int cqdb_to_id(cqdb_t* db, const char *str)
|
538
|
+
{
|
539
|
+
uint32_t hv = hashlittle(str, strlen(str)+1, 0);
|
540
|
+
int t = hv % 256;
|
541
|
+
table_t* ht = &db->ht[t];
|
542
|
+
|
543
|
+
if (ht->num && ht->bucket != NULL) {
|
544
|
+
int n = ht->num;
|
545
|
+
int k = (hv >> 8) % n;
|
546
|
+
bucket_t* p = NULL;
|
547
|
+
|
548
|
+
while (p = &ht->bucket[k], p->offset) {
|
549
|
+
if (p->hash == hv) {
|
550
|
+
int value;
|
551
|
+
uint32_t ksize;
|
552
|
+
uint8_t *q = db->buffer + p->offset;
|
553
|
+
value = (int)read_uint32(q);
|
554
|
+
q += sizeof(uint32_t);
|
555
|
+
ksize = read_uint32(q);
|
556
|
+
q += sizeof(uint32_t);
|
557
|
+
if (strcmp(str, (const char *)q) == 0) {
|
558
|
+
return value;
|
559
|
+
}
|
560
|
+
}
|
561
|
+
k = (k+1) % n;
|
562
|
+
}
|
563
|
+
}
|
564
|
+
|
565
|
+
return CQDB_ERROR_NOTFOUND;
|
566
|
+
}
|
567
|
+
|
568
|
+
const char* cqdb_to_string(cqdb_t* db, int id)
|
569
|
+
{
|
570
|
+
/* Check if the current database supports the backward look-up. */
|
571
|
+
if (db->bwd != NULL && (uint32_t)id < db->header.bwd_size) {
|
572
|
+
uint32_t offset = db->bwd[id];
|
573
|
+
if (offset) {
|
574
|
+
uint8_t *p = db->buffer + offset;
|
575
|
+
p += sizeof(uint32_t); /* Skip key data. */
|
576
|
+
p += sizeof(uint32_t); /* Skip value size. */
|
577
|
+
return (const char *)p;
|
578
|
+
}
|
579
|
+
}
|
580
|
+
|
581
|
+
return NULL;
|
582
|
+
}
|
583
|
+
|
584
|
+
int cqdb_num(cqdb_t* db)
|
585
|
+
{
|
586
|
+
return db->num;
|
587
|
+
}
|