Class: Sophia
- Inherits:
-
Object
- Object
- Sophia
- Includes:
- Enumerable
- Defined in:
- lib/sophia/version.rb,
ext/sophia.c
Constant Summary collapse
- VERSION =
'0.0.2'
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #close ⇒ nil
- #closed? ⇒ Boolean
- #delete(key) ⇒ Object
- #each_key ⇒ Object
- #each_pair ⇒ Object (also: #each)
- #each_value ⇒ Object
- #empty? ⇒ Boolean
- #fetch(*args) ⇒ Object
- #get(key) ⇒ Object
- #length ⇒ Object (also: #size)
- #set(key, value) ⇒ Object
Class Method Details
.open("/path/to/db") ⇒ Object .open("/path/to/db") {|sophia| ... } ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'ext/sophia.c', line 120
static VALUE
sophia_s_open(int argc, VALUE *argv, VALUE self)
{
VALUE sophia = sophia_alloc(self);
if (NIL_P(sophia_initialize(argc, argv, sophia))) {
return Qnil;
}
if (rb_block_given_p()) {
return rb_ensure(rb_yield, sophia, sophia_close, sophia);
}
return sophia;
}
|
Instance Method Details
#[](key) ⇒ Object
213 214 215 216 217 |
# File 'ext/sophia.c', line 213
static VALUE
sophia_aref(VALUE self, VALUE key)
{
return sophia_get(self, key, Qnil);
}
|
#[]=(key, value) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'ext/sophia.c', line 162
static VALUE
sophia_set(VALUE self, VALUE key, VALUE value)
{
Sophia *sophia;
GetSophia(self, sophia);
key = rb_obj_as_string(key);
value = rb_obj_as_string(value);
if (sp_set(sophia->db,
RSTRING_PTR(key), RSTRING_LEN(key),
RSTRING_PTR(value), RSTRING_LEN(value))) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
return value;
}
|
#close ⇒ nil
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'ext/sophia.c', line 91
static VALUE
sophia_close(VALUE self)
{
Sophia *sophia;
GetSophia(self, sophia);
if (sophia->db) {
if (sp_destroy(sophia->db)) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
sophia->db = NULL;
}
if (sophia->env) {
if (sp_destroy(sophia->env)) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
sophia->env = NULL;
}
return Qnil;
}
|
#closed? ⇒ Boolean
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'ext/sophia.c', line 140
static VALUE
sophia_closed_p(VALUE self)
{
Sophia *sophia;
Data_Get_Struct(self, Sophia, sophia);
if (sophia == NULL) {
return Qtrue;
}
if (sophia->env == NULL) {
return Qtrue;
}
if (sophia->db == NULL) {
return Qtrue;
}
return Qfalse;
}
|
#delete(key) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'ext/sophia.c', line 228
static VALUE
sophia_delete(VALUE self, VALUE key)
{
Sophia *sophia;
int result;
VALUE val;
GetSophia(self, sophia);
ExportStringValue(key);
val = sophia_aref(self, key);
result = sp_delete(sophia->db,
RSTRING_PTR(key), RSTRING_LEN(key));
if (result == 0) {
return val;
} else if (result == -1) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
rb_raise(rb_eSophiaError, "error");
return Qnil;
}
|
#each_key ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'ext/sophia.c', line 316
static VALUE
sophia_each_key(VALUE self)
{
Sophia *sophia;
void *cursor;
RETURN_ENUMERATOR(self, 0, 0);
GetSophia(self, sophia);
cursor = sp_cursor(sophia->db, SPGT, NULL, 0);
if (cursor == NULL) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
while (sp_fetch(cursor)) {
rb_yield(rb_str_new(sp_key(cursor), sp_keysize(cursor)));
}
sp_destroy(cursor);
return self;
}
|
#each_pair ⇒ Object Also known as: each
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'ext/sophia.c', line 293
static VALUE
sophia_each_pair(VALUE self)
{
Sophia *sophia;
void *cursor;
RETURN_ENUMERATOR(self, 0, 0);
GetSophia(self, sophia);
cursor = sp_cursor(sophia->db, SPGT, NULL, 0);
if (cursor == NULL) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
while (sp_fetch(cursor)) {
rb_yield(rb_assoc_new(rb_str_new(sp_key(cursor), sp_keysize(cursor)),
rb_str_new(sp_value(cursor), sp_valuesize(cursor))));
}
sp_destroy(cursor);
return self;
}
|
#each_value ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'ext/sophia.c', line 338
static VALUE
sophia_each_value(VALUE self)
{
Sophia *sophia;
void *cursor;
RETURN_ENUMERATOR(self, 0, 0);
GetSophia(self, sophia);
cursor = sp_cursor(sophia->db, SPGT, NULL, 0);
if (cursor == NULL) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
while (sp_fetch(cursor)) {
rb_yield(rb_str_new(sp_value(cursor), sp_valuesize(cursor)));
}
sp_destroy(cursor);
return self;
}
|
#empty? ⇒ Boolean
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'ext/sophia.c', line 274
static VALUE
sophia_empty_p(VALUE self)
{
Sophia *sophia;
int result;
void *cursor;
GetSophia(self, sophia);
cursor = sp_cursor(sophia->db, SPGT, NULL, 0);
if (cursor == NULL) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
result = sp_fetch(cursor);
sp_destroy(cursor);
return result == 0 ? Qtrue : Qfalse;
}
|
#fetch(*args) ⇒ Object
219 220 221 222 223 224 225 226 |
# File 'ext/sophia.c', line 219
static VALUE
sophia_fetch(int argc, VALUE *argv, VALUE self)
{
VALUE key, ifnone;
rb_scan_args(argc, argv, "11", &key, &ifnone);
return sophia_get(self, key, ifnone);
}
|
#get(key) ⇒ Object
213 214 215 216 217 |
# File 'ext/sophia.c', line 213
static VALUE
sophia_aref(VALUE self, VALUE key)
{
return sophia_get(self, key, Qnil);
}
|
#length ⇒ Object Also known as: size
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'ext/sophia.c', line 253
static VALUE
sophia_length(VALUE self)
{
Sophia *sophia;
int length = 0;
void *cursor;
GetSophia(self, sophia);
cursor = sp_cursor(sophia->db, SPGT, NULL, 0);
if (cursor == NULL) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
while (sp_fetch(cursor)) {
length += 1;
}
sp_destroy(cursor);
return INT2FIX(length);
}
|
#set(key, value) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'ext/sophia.c', line 162
static VALUE
sophia_set(VALUE self, VALUE key, VALUE value)
{
Sophia *sophia;
GetSophia(self, sophia);
key = rb_obj_as_string(key);
value = rb_obj_as_string(value);
if (sp_set(sophia->db,
RSTRING_PTR(key), RSTRING_LEN(key),
RSTRING_PTR(value), RSTRING_LEN(value))) {
rb_raise(rb_eSophiaError, sp_error(sophia->env));
}
return value;
}
|