| 1 | package DB::CouchDB; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use JSON -convert_blessed_universally; | ||
| 6 | use LWP::UserAgent; | ||
| 7 | use URI; | ||
| 8 | use Encode; | ||
| 9 | |||
| 10 | $DB::CouchDB::VERSION = 0.2; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | DB::CouchDB - A low level perl module for CouchDB | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | 0.2 | ||
| 19 | |||
| 20 | =head1 RATIONALE | ||
| 21 | |||
| 22 | After working with a lot several of the CouchDB modules already in CPAN I found | ||
| 23 | myself dissatisfied with them. Since the API for Couch is so easy I wrote my own | ||
| 24 | which I find to have an API that better fits a CouchDB Workflow. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | my $db = DB::CouchDB->new(host => $host, | ||
| 29 | db => $dbname); | ||
| 30 | my $doc = $db->get_doc($docname); | ||
| 31 | my $docid = $doc->{_id}; | ||
| 32 | |||
| 33 | my $doc_iterator = $db->view('foo/bar', \%view_query_opts); | ||
| 34 | |||
| 35 | while ( my $result = $doc_iterator->next() ) { | ||
| 36 | ... #do whatever with the result the view returns | ||
| 37 | } | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =head2 new(%dbopts) | ||
| 42 | |||
| 43 | This is the constructor for the DB::CouchDB object. It expects | ||
| 44 | a list of name value pairs for the options to the CouchDB database. | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =item * | ||
| 49 | |||
| 50 | Required options: (host => $hostname, db => $database_name); | ||
| 51 | |||
| 52 | =item * | ||
| 53 | |||
| 54 | Optional options: (port => $db_port) | ||
| 55 | |||
| 56 | =back | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub new{ | ||
| 61 | my $class = shift; | ||
| 62 | my %opts = @_; | ||
| 63 | $opts{port} = 5984 | ||
| 64 | if (!exists $opts{port}); | ||
| 65 | my $obj = {%opts}; | ||
| 66 | $obj->{json} = JSON->new(); | ||
| 67 | return bless $obj, $class; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 Accessors | ||
| 71 | |||
| 72 | =over 4 | ||
| 73 | |||
| 74 | =item * | ||
| 75 | |||
| 76 | host - host name of db | ||
| 77 | |||
| 78 | =item * | ||
| 79 | |||
| 80 | db - database name | ||
| 81 | |||
| 82 | =item * | ||
| 83 | |||
| 84 | port - port number of the database server | ||
| 85 | |||
| 86 | =item * | ||
| 87 | |||
| 88 | json - the JSON object for serialization | ||
| 89 | |||
| 90 | =back | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub host { | ||
| 95 | return shift->{host}; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub port { | ||
| 99 | return shift->{port}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub db { | ||
| 103 | return shift->{db}; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub json { | ||
| 107 | my $self = shift; | ||
| 108 | return $self->{json}; | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 handle_blessed | ||
| 112 | |||
| 113 | Turns on or off the JSON's handling of blessed objects. | ||
| 114 | |||
| 115 | $db->handle_blessed(1) #turn on blessed object handling | ||
| 116 | $db->handle_blessed() #turn off blessed object handling | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub handle_blessed { | ||
| 121 | my $self = shift; | ||
| 122 | my $set = shift; | ||
| 123 | |||
| 124 | my $json = $self->json(); | ||
| 125 | if ($set) { | ||
| 126 | $json->allow_blessed(1); | ||
| 127 | $json->convert_blessed(1); | ||
| 128 | } else { | ||
| 129 | $json->allow_blessed(0); | ||
| 130 | $json->convert_blessed(0); | ||
| 131 | } | ||
| 132 | return $self; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 all_dbs | ||
| 136 | |||
| 137 | my $dbs = $db->all_dbs() #returns an arrayref of databases on this server | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub all_dbs { | ||
| 142 | my $self = shift; | ||
| 143 | my $args = shift; ## do we want to reduce the view? | ||
| 144 | my $uri = $self->_uri_all_dbs(); | ||
| 145 | if ($args) { | ||
| 146 | my $argstring = _valid_view_args($args); | ||
| 147 | $uri->query($argstring); | ||
| 148 | } | ||
| 149 | return $self->_call(GET => $uri); | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 all_docs | ||
| 153 | |||
| 154 | my $dbs = $db->all_dbs() #returns a DB::CouchDB::Iterator of | ||
| 155 | #all documents in this database | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub all_docs { | ||
| 160 | my $self = shift; | ||
| 161 | my $args = shift; | ||
| 162 | my $uri = $self->_uri_db_docs(); | ||
| 163 | if ($args) { | ||
| 164 | my $argstring = _valid_view_args($args); | ||
| 165 | $uri->query($argstring); | ||
| 166 | } | ||
| 167 | return DB::CouchDB::Iter->new($self->_call(GET => $uri)); | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 db_info | ||
| 171 | |||
| 172 | my $dbinfo = $db->db_info() #returns a DB::CouchDB::Result with the db info | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub db_info { | ||
| 177 | my $self = shift; | ||
| 178 | return DB::CouchDB::Result->new($self->_call(GET => $self->_uri_db())); | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 create_db | ||
| 182 | |||
| 183 | Creates the database in the CouchDB server. | ||
| 184 | |||
| 185 | my $result = $db->create_db() #returns a DB::CouchDB::Result object | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub create_db { | ||
| 190 | my $self = shift; | ||
| 191 | return DB::CouchDB::Result->new($self->_call(PUT => $self->_uri_db())); | ||
| 192 | } | ||
| 193 | |||
| 194 | =head2 delete_db | ||
| 195 | |||
| 196 | deletes the database in the CouchDB server | ||
| 197 | |||
| 198 | my $result = $db->delete_db() #returns a DB::CouchDB::Result object | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub delete_db { | ||
| 203 | my $self = shift; | ||
| 204 | return DB::CouchDB::Result->new($self->_call(DELETE => $self->_uri_db())); | ||
| 205 | } | ||
| 206 | |||
| 207 | =head2 create_doc | ||
| 208 | |||
| 209 | creates a doc in the database. The document will have an automatically assigned | ||
| 210 | id/name. | ||
| 211 | |||
| 212 | my $result = $db->create_doc($doc) #returns a DB::CouchDB::Result object | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | sub create_doc { | ||
| 217 | my $self = shift; | ||
| 218 | my $doc = shift; | ||
| 219 | my $jdoc = $self->json()->encode($doc); | ||
| 220 | return DB::CouchDB::Result->new( | ||
| 221 | $self->_call(POST => $self->_uri_db(), $jdoc) | ||
| 222 | ); | ||
| 223 | } | ||
| 224 | |||
| 225 | =head2 temp_view | ||
| 226 | |||
| 227 | runs a temporary view. | ||
| 228 | |||
| 229 | my $results = $db->temp_view($view_object); | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | sub temp_view { | ||
| 234 | my $self = shift; | ||
| 235 | my $doc = shift; | ||
| 236 | my $jdoc = $self->json()->encode($doc); | ||
| 237 | return DB::CouchDB::Iter->new( | ||
| 238 | $self->_call(POST => $self->uri_db_temp_view(), $jdoc) | ||
| 239 | ); | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 create_named_doc | ||
| 243 | |||
| 244 | creates a doc in the database, the document will have the id/name you specified | ||
| 245 | |||
| 246 | my $result = $db->create_named_doc($doc, $docname) #returns a DB::CouchDB::Result object | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | #TODO this really needs to have the same API as all the others. $name first then $doc | ||
| 251 | sub create_named_doc { | ||
| 252 | my $self = shift; | ||
| 253 | my $doc = shift; | ||
| 254 | my $name = shift; | ||
| 255 | my $jdoc = $self->json()->encode($doc); | ||
| 256 | return DB::CouchDB::Result->new($self->_call(PUT => $self->_uri_db_doc($name), $jdoc)); | ||
| 257 | } | ||
| 258 | |||
| 259 | =head2 update_doc | ||
| 260 | |||
| 261 | Updates a doc in the database. | ||
| 262 | |||
| 263 | my $result = $db->update_doc($docname, $doc) #returns a DB::CouchDB::Result object | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub update_doc { | ||
| 268 | my $self = shift; | ||
| 269 | my $name = shift; | ||
| 270 | my $doc = shift; | ||
| 271 | my $jdoc = $self->json()->encode($doc); | ||
| 272 | return DB::CouchDB::Result->new($self->_call(PUT => $self->_uri_db_doc($name), $jdoc)); | ||
| 273 | } | ||
| 274 | |||
| 275 | =head2 delete_doc | ||
| 276 | |||
| 277 | Deletes a doc in the database. you must supply a rev parameter to represent the | ||
| 278 | revision of the doc you are updating. If the revision is not the current revision | ||
| 279 | of the doc the update will fail. | ||
| 280 | |||
| 281 | my $result = $db->delete_doc($docname, $rev) #returns a DB::CouchDB::Result object | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub delete_doc { | ||
| 286 | my $self = shift; | ||
| 287 | my $doc = shift; | ||
| 288 | my $rev = shift; | ||
| 289 | my $uri = $self->_uri_db_doc($doc); | ||
| 290 | $uri->query('rev='.$rev); | ||
| 291 | return DB::CouchDB::Result->new($self->_call(DELETE => $uri)); | ||
| 292 | } | ||
| 293 | |||
| 294 | =head2 get_doc | ||
| 295 | |||
| 296 | Gets a doc in the database. | ||
| 297 | |||
| 298 | my $result = $db->get_doc($docname) #returns a DB::CouchDB::Result object | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub get_doc { | ||
| 303 | my $self = shift; | ||
| 304 | my $doc = shift; | ||
| 305 | return DB::CouchDB::Result->new($self->_call(GET => $self->_uri_db_doc($doc))); | ||
| 306 | } | ||
| 307 | |||
| 308 | =head2 view | ||
| 309 | |||
| 310 | Returns a views results from the database. | ||
| 311 | |||
| 312 | my $rs = $db->view($viewname, \%view_args) #returns a DB::CouchDB::Iter object | ||
| 313 | |||
| 314 | =head3 A note about view args: | ||
| 315 | |||
| 316 | the view args allow you to constrain and/or window the results that the | ||
| 317 | view gives back. Some of the ones you will probably want to use are: | ||
| 318 | |||
| 319 | group => "true" #turn on the reduce portion of your view | ||
| 320 | key => '"keyname"' # only gives back results with a certain key | ||
| 321 | |||
| 322 | #only return results starting at startkey and goint up to endkey | ||
| 323 | startkey => '"startkey"', | ||
| 324 | endkey => '"endkey"' | ||
| 325 | |||
| 326 | count => $num #only returns $num rows | ||
| 327 | offset => $num #return starting from $num row | ||
| 328 | |||
| 329 | All the values should be valid json encoded. | ||
| 330 | See http://wiki.apache.org/couchdb/HttpViewApi for more information on the view | ||
| 331 | parameters | ||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | ## TODO: still need to handle windowing on views | ||
| 336 | sub view { | ||
| 337 | my $self = shift; | ||
| 338 | my $view = shift; | ||
| 339 | my $args = shift; ## do we want to reduce the view? | ||
| 340 | my $uri = $self->_uri_db_view($view); | ||
| 341 | if ($args) { | ||
| 342 | my $argstring = _valid_view_args($args); | ||
| 343 | $uri->query($argstring); | ||
| 344 | } | ||
| 345 | return DB::CouchDB::Iter->new($self->_call(GET => $uri)); | ||
| 346 | } | ||
| 347 | |||
| 348 | sub _valid_view_args { | ||
| 349 | my $args = shift; | ||
| 350 | my $string; | ||
| 351 | my @str_parts = map {"$_=$args->{$_}"} keys %$args; | ||
| 352 | $string = join('&', @str_parts); | ||
| 353 | |||
| 354 | return $string; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub uri { | ||
| 358 | my $self = shift; | ||
| 359 | my $u = URI->new(); | ||
| 360 | $u->scheme("http"); | ||
| 361 | $u->host($self->{host}.':'.$self->{port}); | ||
| 362 | return $u; | ||
| 363 | } | ||
| 364 | |||
| 365 | sub _uri_all_dbs { | ||
| 366 | my $self = shift; | ||
| 367 | my $uri = $self->uri(); | ||
| 368 | $uri->path('/_all_dbs'); | ||
| 369 | return $uri; | ||
| 370 | } | ||
| 371 | |||
| 372 | sub _uri_db { | ||
| 373 | my $self = shift; | ||
| 374 | my $db = $self->{db}; | ||
| 375 | my $uri = $self->uri(); | ||
| 376 | $uri->path('/'.$db); | ||
| 377 | return $uri; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub _uri_db_docs { | ||
| 381 | my $self = shift; | ||
| 382 | my $db = $self->{db}; | ||
| 383 | my $uri = $self->uri(); | ||
| 384 | $uri->path('/'.$db.'/_all_docs'); | ||
| 385 | return $uri; | ||
| 386 | } | ||
| 387 | |||
| 388 | sub _uri_db_doc { | ||
| 389 | my $self = shift; | ||
| 390 | my $db = $self->{db}; | ||
| 391 | my $doc = shift; | ||
| 392 | my $uri = $self->uri(); | ||
| 393 | $uri->path('/'.$db.'/'.$doc); | ||
| 394 | return $uri; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub _uri_db_bulk_doc { | ||
| 398 | my $self = shift; | ||
| 399 | my $db = $self->{db}; | ||
| 400 | my $uri = $self->uri(); | ||
| 401 | $uri->path('/'.$db.'/_bulk_docs'); | ||
| 402 | return $uri; | ||
| 403 | } | ||
| 404 | |||
| 405 | sub _uri_db_view { | ||
| 406 | my $self = shift; | ||
| 407 | my $db = $self->{db}; | ||
| 408 | my $view = shift; | ||
| 409 | my $uri = $self->uri(); | ||
| 410 | $uri->path('/'.$db.'/_view/'.$view); | ||
| 411 | return $uri; | ||
| 412 | } | ||
| 413 | |||
| 414 | sub uri_db_temp_view { | ||
| 415 | my $self = shift; | ||
| 416 | my $db = $self->{db}; | ||
| 417 | my $uri = $self->uri(); | ||
| 418 | $uri->path('/'.$db.'/_temp_view'); | ||
| 419 | return $uri; | ||
| 420 | |||
| 421 | } | ||
| 422 | |||
| 423 | sub _call { | ||
| 424 | my $self = shift; | ||
| 425 | my $method = shift; | ||
| 426 | my $uri = shift; | ||
| 427 | my $content = shift; | ||
| 428 | |||
| 429 | my $req = HTTP::Request->new($method, $uri); | ||
| 430 | $req->content(Encode::encode('utf8', $content)); | ||
| 431 | |||
| 432 | my $ua = LWP::UserAgent->new(); | ||
| 433 | my $return = $ua->request($req); | ||
| 434 | my $response = $return->decoded_content({ | ||
| 435 | default_charset => 'utf8' | ||
| 436 | }); | ||
| 437 | my $decoded; | ||
| 438 | eval { | ||
| 439 | $decoded = $self->json()->decode($response); | ||
| 440 | }; | ||
| 441 | if ($@) { | ||
| 442 | return {error => $return->code, reason => $response}; | ||
| 443 | } | ||
| 444 | return $decoded; | ||
| 445 | } | ||
| 446 | |||
| 447 | package DB::CouchDB::Iter; | ||
| 448 | |||
| 449 | sub new { | ||
| 450 | my $self = shift; | ||
| 451 | my $results = shift; | ||
| 452 | my $rows = $results->{rows}; | ||
| 453 | |||
| 454 | return bless { data => $rows, | ||
| 455 | count => $results->{total_rows}, | ||
| 456 | offset => $results->{offset}, | ||
| 457 | iter => mk_iter($rows), | ||
| 458 | iter_key => mk_iter($rows, 'key'), | ||
| 459 | error => $results->{error}, | ||
| 460 | reason => $results->{reason}, | ||
| 461 | }, $self; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub count { | ||
| 465 | return shift->{count}; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub offset { | ||
| 469 | return shift->{offset}; | ||
| 470 | } | ||
| 471 | |||
| 472 | sub data { | ||
| 473 | return shift->{data}; | ||
| 474 | } | ||
| 475 | |||
| 476 | sub err { | ||
| 477 | return shift->{error}; | ||
| 478 | } | ||
| 479 | |||
| 480 | sub errstr { | ||
| 481 | return shift->{reason}; | ||
| 482 | } | ||
| 483 | |||
| 484 | sub next { | ||
| 485 | my $self = shift; | ||
| 486 | return $self->{iter}->(); | ||
| 487 | } | ||
| 488 | |||
| 489 | sub next_key { | ||
| 490 | my $self = shift; | ||
| 491 | return $self->{iter_key}->(); | ||
| 492 | } | ||
| 493 | |||
| 494 | sub next_for_key { | ||
| 495 | my $self = shift; | ||
| 496 | my $key = shift; | ||
| 497 | my $ph = $key."_iter"; | ||
| 498 | if (! defined $self->{$ph} ) { | ||
| 499 | my $iter = mk_iter($self->{data}, 'value', sub { | ||
| 500 | my $item = shift; | ||
| 501 | return $item | ||
| 502 | if $item->{key} eq $key; | ||
| 503 | return; | ||
| 504 | }); | ||
| 505 | $self->{$ph} = $iter; | ||
| 506 | } | ||
| 507 | return $self->{$ph}->(); | ||
| 508 | } | ||
| 509 | |||
| 510 | sub mk_iter { | ||
| 511 | my $rows = shift; | ||
| 512 | my $key = shift || 'value'; | ||
| 513 | my $filter = shift || sub { return $_ }; | ||
| 514 | my $mapper = sub { | ||
| 515 | my $row = shift; | ||
| 516 | return @{ $row->{$key} } | ||
| 517 | if ref($row->{$key}) eq 'ARRAY'; | ||
| 518 | return $row->{$key}; | ||
| 519 | }; | ||
| 520 | my @list = map { $mapper->($_) } grep { $filter->($_) } @$rows; | ||
| 521 | my $index = 0; | ||
| 522 | return sub { | ||
| 523 | return if $index > $#list; | ||
| 524 | my $row = $list[$index]; | ||
| 525 | $index++; | ||
| 526 | return $row; | ||
| 527 | }; | ||
| 528 | } | ||
| 529 | |||
| 530 | package DB::CouchDB::Result; | ||
| 531 | |||
| 532 | sub new { | ||
| 533 | my $self = shift; | ||
| 534 | my $result = shift; | ||
| 535 | |||
| 536 | return bless $result, $self; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub err { | ||
| 540 | return shift->{error}; | ||
| 541 | } | ||
| 542 | |||
| 543 | sub errstr { | ||
| 544 | return shift->{reason}; | ||
| 545 | } | ||
| 546 | |||
| 547 | =head1 AUTHOR | ||
| 548 | |||
| 549 | Jeremy Wall <jeremy@marzhillstudios.com> | ||
| 550 | |||
| 551 | =head1 DEPENDENCIES | ||
| 552 | |||
| 553 | =over 4 | ||
| 554 | |||
| 555 | =item * | ||
| 556 | |||
| 557 | L<LWP::UserAgent> | ||
| 558 | |||
| 559 | =item * | ||
| 560 | |||
| 561 | L<URI> | ||
| 562 | |||
| 563 | =item * | ||
| 564 | |||
| 565 | L<JSON> | ||
| 566 | |||
| 567 | =back | ||
| 568 | |||
| 569 | =head1 SEE ALSO | ||
| 570 | |||
| 571 | =over 4 | ||
| 572 | |||
| 573 | =item * | ||
| 574 | |||
| 575 | L<DB::CouchDB::Result> - POD for the DB::CouchDB::Result object | ||
| 576 | |||
| 577 | =item * | ||
| 578 | |||
| 579 | L<DB::CouchDB::Iter> - POD for the DB::CouchDB::Iter object | ||
| 580 | |||
| 581 | =item * | ||
| 582 | |||
| 583 | L<DB::CouchDB::Schema> - higher level wrapper with some schema handling functionality | ||
| 584 | |||
| 585 | =back | ||
| 586 | |||
| 587 | =cut | ||
| 588 | |||
| 589 | 1; |