| 1 | package Data::Chronicle; | ||
| 2 | 8 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 8 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Data::Chronicle - Chronicle storage system | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | 8 | our $VERSION = '0.21'; | |
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This package contains three modules (Reader, Writer, and Subscriber) which can be used to store and retrieve information | ||
| 16 | on an efficient storage with below properties: | ||
| 17 | |||
| 18 | =head2 Timeliness | ||
| 19 | |||
| 20 | It is assumed that data to be stored are time-based meaning they change over time and the latest version is most important for us. | ||
| 21 | |||
| 22 | =head2 Efficient | ||
| 23 | |||
| 24 | The module uses Redis cache to provide efficient data storage and retrieval. | ||
| 25 | |||
| 26 | =head2 Persistent | ||
| 27 | |||
| 28 | In addition to caching every incoming data, it is also stored in PostgreSQL for future retrieval. | ||
| 29 | |||
| 30 | =head2 Transparent | ||
| 31 | |||
| 32 | This modules hides all the details about caching, database structure and ... from developer. He only needs to call a method | ||
| 33 | to save data and another method to retrieve it. All the underlying complexities are handled by the module. | ||
| 34 | |||
| 35 | Note that you will need to pass `cache_writer`, `cache_reader` and `dbic` to the `Data::Chronicle::Reader/Writer` modules. These three arguments, provide access to your Redis and PostgreSQL which will be used by Chronicle modules. | ||
| 36 | |||
| 37 | `cache_writer` and `cache_reader` should be to be able to get/set given data under given key (both of type string). `dbic` should be capable to store and retrieve data with `category`,`name` in addition to the timestamp of data insertion. So it should be able to retrieve data for a specific timestamp, category and name. Category, name and data are all string. This can easily be achieved by defining a table in you database containing these columns: `timestamp, category, name, value`. | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | There are four important methods this module provides: | ||
| 42 | |||
| 43 | =head2 L<Data::Chronicle::Writer/set> | ||
| 44 | |||
| 45 | Given a category, name and value stores the JSONified value in Redis and PostgreSQL database under "category::name" group and also stores current | ||
| 46 | system time as the timestamp for the data (Which can be used for future retrieval if we want to get data as of a specific time). Note that the value | ||
| 47 | MUST be either hash-ref or array-ref. | ||
| 48 | |||
| 49 | $writer->set("category1", "name1", "value1"); | ||
| 50 | $writer->set("category1", "name2", "value2", Date::Utility->new("2016-08-01 00:06:00")); | ||
| 51 | |||
| 52 | =head2 L<Data::Chronicle::Writer/mset> | ||
| 53 | |||
| 54 | Given multiple categories, names and values atomically performs the set operation on each corresponding category, name, value set. | ||
| 55 | |||
| 56 | $writer->mset([["category1", "name1", $value1], ["category2, "name2", $value2], ...]); | ||
| 57 | |||
| 58 | =head2 L<Data::Chronicle::Reader/get> | ||
| 59 | |||
| 60 | Given a category and name returns the latest version of the data according to current Redis cache | ||
| 61 | |||
| 62 | my $value1 = $reader->get("category1, "name1"); #value1 | ||
| 63 | |||
| 64 | =head2 L<Data::Chronicle::Reader/mget> | ||
| 65 | |||
| 66 | Given multiple categories and name atomically performs the get operation on each corresponding category, name set. | ||
| 67 | |||
| 68 | my @values = $reader->mget([["category1", "name1"], ["category2", "name2"], ...]) | ||
| 69 | |||
| 70 | =head2 L<Data::Chronicle::Reader/get_for> | ||
| 71 | |||
| 72 | Given a category, name and timestamp returns version of data under "category::name" as of the given date (using a DB lookup). | ||
| 73 | |||
| 74 | my $some_old_data = $reader->get_for("category1", "name2", Date::Utility->new("2016-08-01 00:06:00")); | ||
| 75 | |||
| 76 | |||
| 77 | =head2 L<Data::Chronicle::Reader/get_for_period> | ||
| 78 | |||
| 79 | Given a category, name, start_timestamp and end_timestamp returns an array-ref containing all data stored between given period for the given "category::name" (using a DB lookup). | ||
| 80 | |||
| 81 | my $arrayref = $reader->get_for_period("category1", "name2", Date::Utility->new("2015-08-01 00:06:00"), Date::Utility->new("2015-08-01 00:06:00")); | ||
| 82 | |||
| 83 | =head2 L<Data::Chronicle::Reader/get_history> | ||
| 84 | |||
| 85 | Given a category, name, and revision returns version of the data the specified number of revisions in the past. | ||
| 86 | If revision 0 is chosen, the latest version of the data will be returned. | ||
| 87 | If revision 1 is chosen, the previous version of the data will be returned. | ||
| 88 | |||
| 89 | my $some_old_data = $reader->get_for("category1", "name2", 2); | ||
| 90 | |||
| 91 | =head2 L<Data::Chronicle::Subscriber/subscribe> | ||
| 92 | |||
| 93 | Given a category, name, and callback assigns the callback to be called when a new value is set for the specified category and name (if the writer has publish_on_set enabled). | ||
| 94 | |||
| 95 | $subscriber->subscribe("category1", "name2", sub { print 'Hello World' }); | ||
| 96 | |||
| 97 | =head2 L<Data::Chronicle::Subscriber/unsubscribe> | ||
| 98 | |||
| 99 | Given a category, name, clears the callbacks associated with the specified category and name. | ||
| 100 | |||
| 101 | $subscriber->unsubscribe("category1", "name2"); | ||
| 102 | |||
| 103 | =head1 EXAMPLES | ||
| 104 | |||
| 105 | my $d = get_some_log_data(); | ||
| 106 | |||
| 107 | my $chronicle_w = Data::Chronicle::Writer->new( | ||
| 108 | cache_writer => $writer, | ||
| 109 | dbic => $dbic); | ||
| 110 | |||
| 111 | my $chronicle_r = Data::Chronicle::Reader->new( | ||
| 112 | cache_reader => $reader, | ||
| 113 | dbic => $dbic); | ||
| 114 | |||
| 115 | |||
| 116 | #store data into Chronicle - each time we call `set` it will also store | ||
| 117 | #a copy of the data for historical data retrieval | ||
| 118 | $chronicle_w->set("log_files", "syslog", $d); | ||
| 119 | |||
| 120 | #retrieve latest data stored for syslog under log_files category | ||
| 121 | my $dt = $chronicle_r->get("log_files", "syslog"); | ||
| 122 | |||
| 123 | #find historical data for `syslog` at given point in time | ||
| 124 | my $some_old_data = $chronicle_r->get_for("log_files", "syslog", $epoch1); | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | 8 | 1; | |