KiokuDB for persistent storage
2012-09-07 perl KiokuDB Moose persistent storageI learned about KiokuDB few years ago, but so far did not have a good project to give it a try. Recently I started working on small web application to connect JIRA we use for end-user support with Trac that we use to control the development. It will be utility mostly for my own use, so I can also take opportunity to try out fancy technology.
The main goal of the app is to keep track of JIRA tickets analysis, with focus to make sure that all development tickets are covered in Tracs.
Both JIRA and Tracs are outside of my control and pretty far network-wise, which makes them rather slow for use. I already have a mirror of trac databases. Since we use many instances of Trac for various projects, the mirror is putting all data into single mysql database. I can easily find any details about any ticket ever created.
The JIRA is new addition into the mix. Fortunately there is a REST API provided
and I am able to get the data of all issues opened for our team. Returned JSON
data can be easily inflated into Moose objects via coercions
and init_arg
setting of attributes.
Objects does not apparently need any change to be stored. We need to init the instance of KiokuDB and create new scope that keeps live references to live objects:
my $database = KiokuDB->connect(
"dbi:mysql:host=localhost;database=jira",
user => '**username**',
password => '**password**',
create => 1,
extract => Search::GIN::Extract::Class->new,
columns => [
updated => { is_nullable => 1, data_type => "varchar" },
status => { is_nullable => 1, data_type => "varchar" },
],
);
my $scope = $database->new_scope;
Now it is rather easy to store any Moose objects into the database with
my $myself = JIRA::User->new(
name => 'bvr',
displayName => 'Beaver',
);
my $reporter = JIRA::User->new(
name => 'Someone',
displayName => 'Some One',
);
my $issue = JIRA::Issue->new(
key => '201',
summary => 'Important problem',
assignee => $myself,
reporter => $reporter,
);
$database->store($issue->key => $issue);
Whole graph based on the issue is stored in the database, including both related users. Issue has assigned the id from calling store method, user ids are determined automatically (as UUIDs).
Retrieval is also similar:
my $issue_found = database->lookup('201');
During same session ($scope
still active), this will return back
reference to same object. Otherwise the object gets created.