Class: CDB::CDBReader
- Inherits:
-
Object
- Object
- CDB::CDBReader
- Defined in:
- lib/cdb/reader_util.rb,
ext/cdb_ext.c
Instance Method Summary collapse
- #each ⇒ Object
- #each_for_key(key) ⇒ Object
- #each_key ⇒ Object
- #each_value ⇒ Object
- #has_key?(key) ⇒ Boolean
- #initialize(io) ⇒ Object constructor
- #values(key) ⇒ Object
Constructor Details
#initialize(io) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'ext/cdb_ext.c', line 118
VALUE
CDBReader_initialize(VALUE self,VALUE io) {
OpenFile *fptr = NULL;
cdb *db = NULL;
rb_check_type(io,T_FILE);
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
Data_Get_Struct(self,cdb,db);
cdb_init(db,fileno(GetReadFile(fptr)));
return self;
}
|
Instance Method Details
#each ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'ext/cdb_ext.c', line 183
VALUE
CDBReader_each(VALUE self) {
VALUE key,value;
cdb *db = NULL;
uint32 eod,klen,dlen,pos=0;
Data_Get_Struct(self,cdb,db);
getnum(db,&eod,&pos);
pos = 2048;
while(pos < eod) {
getnum(db,&klen,&pos);
getnum(db,&dlen,&pos);
key = rb_str_buf_new(klen);
RSTRING(key)->len = klen;
value = rb_str_buf_new(dlen);
RSTRING(value)->len = dlen;
_cdb_read(db,RSTRING(key)->ptr,klen,&pos);
_cdb_read(db,RSTRING(value)->ptr,dlen,&pos);
rb_yield(rb_ary_new3(2,key,value));
}
return Qnil;
}
|
#each_for_key(key) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'ext/cdb_ext.c', line 208
VALUE
CDBReader_each_for_key(VALUE self,VALUE key) {
cdb *db = NULL;
Data_Get_Struct(self,cdb,db);
VALUE value;
key = StringValue(key);
#ifndef TINYCDB_VERSION
if(cdb_find(db,RSTRING(key)->ptr,RSTRING(key)->len) == -1) rb_sys_fail(0);
do {
value = rb_str_buf_new(cdb_datalen(db));
RSTRING(value)->len = cdb_datalen(db);
_cdb_read(db,RSTRING(value)->ptr,cdb_datalen(db),&cdb_datapos(db));
rb_yield(value);
} while(cdb_findnext(db,RSTRING(key)->ptr,RSTRING(key)->len));
#else
struct cdb_find find;
cdb_findinit(&find,db,RSTRING(key)->ptr,RSTRING(key)->len);
while(cdb_findnext(&find) > 0) {
value = rb_str_buf_new(cdb_datalen(db));
RSTRING(value)->len = cdb_datalen(db);
_cdb_read(db,RSTRING(value)->ptr,cdb_datalen(db),&cdb_datapos(db));
rb_yield(value);
}
#endif /* TINYCDB_VERSION */
return Qnil;
}
|
#each_key ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'ext/cdb_ext.c', line 140
VALUE
CDBReader_each_key(VALUE self) {
VALUE key;
cdb *db = NULL;
uint32 eod,klen,dlen,pos=0;
Data_Get_Struct(self,cdb,db);
getnum(db,&eod,&pos);
pos = 2048;
while(pos < eod) {
getnum(db,&klen,&pos);
getnum(db,&dlen,&pos);
key = rb_str_buf_new(klen);
RSTRING(key)->len = klen;
_cdb_read(db,RSTRING(key)->ptr,klen,&pos);
pos+=dlen;
rb_yield(key);
}
return Qnil;
}
|
#each_value ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'ext/cdb_ext.c', line 162
VALUE
CDBReader_each_value(VALUE self) {
VALUE value;
cdb *db = NULL;
uint32 eod,klen,dlen,pos=0;
Data_Get_Struct(self,cdb,db);
getnum(db,&eod,&pos);
pos = 2048;
while(pos < eod) {
getnum(db,&klen,&pos);
getnum(db,&dlen,&pos);
value = rb_str_buf_new(dlen);
RSTRING(value)->len = dlen;
pos+=klen;
_cdb_read(db,RSTRING(value)->ptr,dlen,&pos);
rb_yield(value);
}
return Qnil;
}
|
#has_key?(key) ⇒ Boolean
130 131 132 133 134 135 136 |
# File 'ext/cdb_ext.c', line 130
VALUE
CDBReader_has_key(VALUE self,VALUE key) {
cdb *db = NULL;
Data_Get_Struct(self,cdb,db);
key = StringValue(key);
return cdb_find(db,RSTRING(key)->ptr,RSTRING(key)->len) ? Qtrue : Qfalse;
}
|
#values(key) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/cdb/reader_util.rb', line 5 def values(key) ret = Array.new each_for_key(key) do |value| ret << value end ret end |