Class: Radix
- Inherits:
-
Object
- Object
- Radix
- Defined in:
- ext/radix.c
Instance Method Summary collapse
-
#[](key) ⇒ nil
Return a value from the database by locating the key string provided.
- #[]=(*args) ⇒ Object
-
#add(key[,prefixlen]) ⇒ Hash
Stores the hash object in the database, indexed via the string key provided.
-
#clear ⇒ Object
Deletes all data from the database.
- #delete(*args) ⇒ Object
-
#each_key ⇒ Object
radix.each_key {|key| block} -> self.
-
#each_pair ⇒ Object
radix.each {|key, value| block} -> self.
-
#each_value ⇒ Object
radix.each_value {|val| block} -> self.
-
#new ⇒ Object
constructor
Open a radix database.
-
#keys ⇒ Array
Returns an array of all the string keys in the database.
-
#length ⇒ Integer
Returns the number of entries in the database.
-
#search_best(key[, prefixlen]) ⇒ Hash
Return a value from the database by locating the key string provided.
-
#search_best(key[, prefixlen]) ⇒ Hash
Return a value from the database by locating the key string provided.
-
#add(key[,prefixlen]) ⇒ Hash
Stores the hash object in the database, indexed via the string key provided.
-
#to_hash ⇒ Object
radix.to_hash -> hash.
-
#values ⇒ Array
Returns an array of all the string values in the database.
Constructor Details
#new ⇒ Object
Open a radix database.
173 174 175 176 177 |
# File 'ext/radix.c', line 173 static VALUE rb_radix_initialize(int argc, VALUE *argv, VALUE self) { return self; } |
Instance Method Details
#[](key) ⇒ nil
Return a value from the database by locating the key string provided. If the key is not found, returns nil.
442 443 444 445 446 |
# File 'ext/radix.c', line 442 static VALUE rb_radix_aref(int argc, VALUE *argv, VALUE self) { return rb_radix_search_best(argc, argv, self); } |
#[]=(*args) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'ext/radix.c', line 258 static VALUE rb_radix_add0(int argc, VALUE *argv, VALUE self) { struct radixdata *radixp; VALUE v_addr, v_plen, v_msg; prefix_t *prefix; int plen; VALUE obj; GetRadix(self, radixp); if (argc == 3) { rb_scan_args(argc, argv, "3", &v_addr, &v_plen, &v_msg); plen = FIX2INT(v_plen); } else { rb_scan_args(argc, argv, "2", &v_addr, &v_msg); plen = -1; } if (TYPE(v_addr) != T_STRING) v_addr = rb_obj_as_string(v_addr); prefix = args_to_prefix(RSTRING_PTR(v_addr), plen); if (prefix == NULL) return Qnil; obj = object_node_add(radixp, prefix, self, v_msg); Deref_Prefix(prefix); return obj; } |
#add(key[,prefixlen]) ⇒ Hash
Stores the hash object in the database, indexed via the string key provided.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'ext/radix.c', line 226 static VALUE rb_radix_add(int argc, VALUE *argv, VALUE self) { struct radixdata *radixp; VALUE v_addr, v_plen; prefix_t *prefix; int plen; VALUE obj; GetRadix(self, radixp); if (argc == 2) { rb_scan_args(argc, argv, "2", &v_addr, &v_plen); plen = FIX2INT(v_plen); } else { rb_scan_args(argc, argv, "1", &v_addr); plen = -1; } if (TYPE(v_addr) != T_STRING) v_addr = rb_obj_as_string(v_addr); prefix = args_to_prefix(RSTRING_PTR(v_addr), plen); if (prefix == NULL) return Qnil; obj = object_node_add(radixp, prefix, self, Qtrue); Deref_Prefix(prefix); return obj; } |
#clear ⇒ Object
Deletes all data from the database.
558 559 560 561 562 563 564 565 566 567 568 |
# File 'ext/radix.c', line 558 static VALUE rb_radix_clear(VALUE self) { struct radixdata *radixp; GetRadix(self, radixp); free_radixdata0(radixp); init_radixdata(radixp); return self; } |
#delete(*args) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'ext/radix.c', line 290 static VALUE rb_radix_delete(int argc, VALUE *argv, VALUE self) { struct radixdata *radixp; VALUE v_addr, v_plen; prefix_t *prefix; radix_node_t *node; int plen; GetRadix(self, radixp); if (argc == 2) { rb_scan_args(argc, argv, "11", &v_addr, &v_plen); plen = FIX2INT(v_plen); } else { rb_scan_args(argc, argv, "1", &v_addr); plen = -1; } if (TYPE(v_addr) != T_STRING) v_addr = rb_obj_as_string(v_addr); prefix = args_to_prefix(RSTRING_PTR(v_addr), plen); if (prefix == NULL) return Qnil; if ((node = radix_search_exact(PICKRT(prefix, radixp), prefix)) == NULL) { Deref_Prefix(prefix); return Qnil; } if (node->data != NULL) { node->data = NULL; } radix_remove(PICKRT(prefix, radixp), node); Deref_Prefix(prefix); radixp->gen_id++; return Qnil; } |
#each_key ⇒ Object
radix.each_key {|key| block} -> self
Calls the block once for each key string in the database. Returns self.
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'ext/radix.c', line 576 static VALUE rb_radix_each_key(VALUE self) { struct radixdata *radixp; radix_node_t *rn; char prefix[256]; unsigned int gen_id_cur; int i; GetRadix(self, radixp); gen_id_cur = radixp->gen_id; for (i = 0; i < RTNUM; i++) { RADIX_WALK(radixp->rt[i]->head, rn) { RaiseModified(gen_id_cur, radixp->gen_id); if (rn->data != NULL) { prefix_ntop(rn->prefix, prefix, sizeof(prefix)); rb_yield(rb_tainted_str_new(prefix, strlen(prefix))); } } RADIX_WALK_END; } return self; } |
#each_pair ⇒ Object
radix.each {|key, value| block} -> self
Calls the block once for each [key, value] pair int the database. Returns self.
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'ext/radix.c', line 640 static VALUE rb_radix_each_pair(VALUE self) { struct radixdata *radixp; radix_node_t *rn; char prefix[256]; unsigned int gen_id_cur; VALUE keystr; int i; GetRadix(self, radixp); gen_id_cur = radixp->gen_id; for (i = 0; i < RTNUM; i++) { RADIX_WALK(radixp->rt[i]->head, rn) { RaiseModified(gen_id_cur, radixp->gen_id); if (rn->data != NULL) { prefix_ntop(rn->prefix, prefix, sizeof(prefix)); keystr = rb_tainted_str_new(prefix, strlen(prefix)); rb_yield(rb_assoc_new(keystr, (VALUE)rn->data)); } } RADIX_WALK_END; } return self; } |
#each_value ⇒ Object
radix.each_value {|val| block} -> self
Calls the block once for each value string in the database. Returns self.
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'ext/radix.c', line 609 static VALUE rb_radix_each_value(VALUE self) { struct radixdata *radixp; radix_node_t *rn; unsigned int gen_id_cur; int i; GetRadix(self, radixp); gen_id_cur = radixp->gen_id; for (i = 0; i < RTNUM; i++) { RADIX_WALK(radixp->rt[i]->head, rn) { RaiseModified(gen_id_cur, radixp->gen_id); if (rn->data != NULL) { rb_yield((VALUE)rn->data); } } RADIX_WALK_END; } return self; } |
#keys ⇒ Array
Returns an array of all the string keys in the database.
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'ext/radix.c', line 485 static VALUE rb_radix_keys(VALUE self) { struct radixdata *radixp; radix_node_t *rn; VALUE ary; char prefix[256]; unsigned int gen_id_cur; int i; GetRadix(self, radixp); ary = rb_ary_new(); gen_id_cur = radixp->gen_id; for (i = 0; i < RTNUM; i++) { RADIX_WALK(radixp->rt[i]->head, rn) { RaiseModified(gen_id_cur, radixp->gen_id); if (rn->data != NULL) { prefix_ntop(rn->prefix, prefix, sizeof(prefix)); rb_ary_push(ary, rb_tainted_str_new( prefix, strlen(prefix))); } } RADIX_WALK_END; } return ary; } |
#length ⇒ Integer
Returns the number of entries in the database.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'ext/radix.c', line 455 static VALUE rb_radix_length(VALUE self) { struct radixdata *radixp; radix_node_t *rn; unsigned int rn_count = 0; unsigned int gen_id_cur; int i; GetRadix(self, radixp); gen_id_cur = radixp->gen_id; for (i = 0; i < RTNUM; i++) { RADIX_WALK(radixp->rt[i]->head, rn) { RaiseModified(gen_id_cur, radixp->gen_id); if (rn->data != NULL) rn_count++; } RADIX_WALK_END; } return INT2FIX(rn_count); } |
#search_best(key[, prefixlen]) ⇒ Hash
Return a value from the database by locating the key string provided. Search strategy is to match best suited for the key. If the key is not found, returns nil.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'ext/radix.c', line 340 static VALUE rb_radix_search_best(int argc, VALUE *argv, VALUE self) { struct radixdata *radixp; radix_node_t *node; struct radixnode *rn; VALUE obj; VALUE v_addr, v_plen; prefix_t *prefix; int plen; GetRadix(self, radixp); if (argc == 2) { rb_scan_args(argc, argv, "11", &v_addr, &v_plen); plen = FIX2INT(v_plen); } else { rb_scan_args(argc, argv, "1", &v_addr); plen = -1; } if (TYPE(v_addr) != T_STRING) v_addr = rb_obj_as_string(v_addr); prefix = args_to_prefix(RSTRING_PTR(v_addr), plen); if (prefix == NULL) return Qnil; if ((node = radix_search_best(PICKRT(prefix, radixp), prefix)) == NULL || node->data == NULL) { Deref_Prefix(prefix); return Qnil; } Deref_Prefix(prefix); obj = rb_obj_alloc(rb_cRadixNode); Data_Get_Struct(obj, struct radixnode, rn); rn->msg = (VALUE)node->data; memcpy(&rn->prefix, node->prefix, sizeof(prefix_t)); return obj; } |
#search_best(key[, prefixlen]) ⇒ Hash
Return a value from the database by locating the key string provided. Search strategy is to match exactly. If the key is not found, returns nil.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'ext/radix.c', line 392 static VALUE rb_radix_search_exact(int argc, VALUE *argv, VALUE self) { struct radixdata *radixp; radix_node_t *node; struct radixnode *rn; VALUE v_addr, v_plen; VALUE obj; prefix_t *prefix; int plen; GetRadix(self, radixp); if (argc == 2) { rb_scan_args(argc, argv, "11", &v_addr, &v_plen); plen = FIX2INT(v_plen); } else { rb_scan_args(argc, argv, "1", &v_addr); plen = -1; } if (TYPE(v_addr) != T_STRING) v_addr = rb_obj_as_string(v_addr); prefix = args_to_prefix(RSTRING_PTR(v_addr), plen); if (prefix == NULL) return Qnil; if ((node = radix_search_exact(PICKRT(prefix, radixp), prefix)) == NULL || node->data == NULL) { Deref_Prefix(prefix); return Qnil; } Deref_Prefix(prefix); obj = rb_obj_alloc(rb_cRadixNode); Data_Get_Struct(obj, struct radixnode, rn); rn->msg = (VALUE)node->data; memcpy(&rn->prefix, node->prefix, sizeof(prefix_t)); return obj; } |
#add(key[,prefixlen]) ⇒ Hash
Stores the hash object in the database, indexed via the string key provided.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'ext/radix.c', line 226 static VALUE rb_radix_add(int argc, VALUE *argv, VALUE self) { struct radixdata *radixp; VALUE v_addr, v_plen; prefix_t *prefix; int plen; VALUE obj; GetRadix(self, radixp); if (argc == 2) { rb_scan_args(argc, argv, "2", &v_addr, &v_plen); plen = FIX2INT(v_plen); } else { rb_scan_args(argc, argv, "1", &v_addr); plen = -1; } if (TYPE(v_addr) != T_STRING) v_addr = rb_obj_as_string(v_addr); prefix = args_to_prefix(RSTRING_PTR(v_addr), plen); if (prefix == NULL) return Qnil; obj = object_node_add(radixp, prefix, self, Qtrue); Deref_Prefix(prefix); return obj; } |
#to_hash ⇒ Object
radix.to_hash -> hash
Converts the contents of the database to an in-memory Hash object, and returns it.
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'ext/radix.c', line 676 static VALUE rb_radix_to_hash(VALUE self) { struct radixdata *radixp; radix_node_t *rn; char prefix[256]; unsigned int gen_id_cur; VALUE keystr; VALUE hash; int i; GetRadix(self, radixp); gen_id_cur = radixp->gen_id; hash = rb_hash_new(); for (i = 0; i < RTNUM; i++) { RADIX_WALK(radixp->rt[i]->head, rn) { RaiseModified(gen_id_cur, radixp->gen_id); if (rn->data != NULL) { prefix_ntop(rn->prefix, prefix, sizeof(prefix)); keystr = rb_tainted_str_new(prefix, strlen(prefix)); rb_hash_aset(hash, keystr, (VALUE)rn->data); } } RADIX_WALK_END; } return hash; } |
#values ⇒ Array
Returns an array of all the string values in the database.
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'ext/radix.c', line 520 static VALUE rb_radix_values(VALUE self) { struct radixdata *radixp; radix_node_t *rn; VALUE ary; unsigned int gen_id_cur; int i; GetRadix(self, radixp); ary = rb_ary_new(); gen_id_cur = radixp->gen_id; for (i = 0; i < RTNUM; i++) { RADIX_WALK(radixp->rt[i]->head, rn) { RaiseModified(gen_id_cur, radixp->gen_id); if (rn->data != NULL) { rb_ary_push(ary, (VALUE)rn->data); } } RADIX_WALK_END; } return ary; } |