DBCacheContrib
Page contents
Summary of Contents
This module supports structured queries over a database built on the fly from the forms in TWiki topics. It does not support any tags, as it is provided as a service for other plugins that want to treat a TWiki web as a simple database; for example, the TWiki:Plugins/FormQueryPlugin
Features
- Perform complex queries on the TWiki database
- Cache TWiki topics for rapid queries
How the database gets built
You can think of the database as an array of all the topics in a web. Each array entry is a map (or hash in perl terms) that maps a set of field names to values. Each topic in the web automatically gets a number of standard fields, generated by reading the metadata from the topic (see TWikiMetaData)-
name
- name of the topic -
parent
- name of parent topic -
_up
- reference to the Map of the parent topic, if it exists -
attachments
- array of Maps, each of which contains:-
_up
- reference to the Map for the topic -
name
- attachment name -
attr
- e.g hidden -
comment
- attachment comment -
path
- client path used to upload attachment -
size
- size in Kb -
user
- who uploaded the attachment -
version
- e.g. 1.3
-
-
info
- Map containing:-
_up
- reference to the Map for the topic -
author
- most recent author -
date
- date of last change -
format
- topic format version -
version
- topic version number
-
-
moved
- Map containing:-
_up
- reference to the Map for the topic -
by
- who moved it -
date
- when they moved it -
from
- where they moved it from -
to
- where they moved it to
-
-
form
- form type - form name - e.g. if a "MyForm" is attached, this will be
MyForm
. This is a reference to a Map containing a key for each field in the form. Each key maps to the value in the form data for that key. The Map will also have an_up
reference to the Map for the topic. -
text
- raw text of the topic)
info
, form name, moved
, and each row in attachments
also have a reference back to the topic Map, called _up
.
Other fields may be added by subclasses. Refer to the documentation for the plugin that is using the DBCache for more details.
The cache
To achieve best perfomance the plugin caches the database read from the TWiki topics. The cache is stored in the work area for the DBCacheContrib (see{RCS}{WorkAreaDir}
in configure
). If any topic changes in the web, this cache is automatically rebuilt. The cache file can be deleted at any point with no ill effects.
Extending or customising
Extension or customisation is welcome, as long as all extensions are described and code provided back to the author. The module is shipped with a perl build file, which should be used for installation and testing. Testing is done using CPAN:Test::Unit
Detailed Documentation
Clients use the DBCache by defining a subclass of theTWiki::Contrib::DBCacheContrib
class. The following POD documentation describes the methods of this class and the various other classes provided by the plugin..
class DBCacheContrib
General purpose cache that treats TWiki topics as hashes. Useful for rapid read and search of the database. Only works on one web. Typical usage:use TWiki::Contrib::DBCacheContrib; $db = new TWiki::Contrib::DBCacheContrib( $web ); # always done $db->load(); # may be always done, or only on demand when a tag is parsed that needs it # the DB is a hash of topics keyed on their name foreach my $topic ($db->getKeys()) { my $attachments = $db->get($topic)->get("attachments"); # attachments is an array foreach my $val ($attachments->getValues()) { my $aname = $attachments->get("name"); my $acomment = $attachments->get("comment"); my $adate = $attachments->get("date"); ... } }As topics are loaded, the readTopicLine method gives subclasses an opportunity to apply special processing to indivual lines, for example to extract special syntax such as %ACTION lines, or embedded tables in the text. See FormQueryPlugin for an example of this.
new($web, $cacheName)
-
$web
name of web to create the object for. -
$cacheName
name of cache file (default "_DBCache")
readTopicLine($topic, $meta, $line, $fh) --> text
- $topic - name of the topic being read
- $meta - reference to the hash object for this topic
- line - the line being read
- $fh - the file handle of the file
- return text to insert in place of line in the text field of the topic
onReload($topics)
-
$topics
- perl array of topic names that have just been loaded (or reloaded)
load( [updateCache] ) -> ($readFromCache, $readFromFile, $removed)
Load the web into the database. Returns a list containing 3 numbers that give the number of topics read from the cache, the number read from file, and the number of previously cached topics that have been removed. if $TWiki::cfg{DBCache}{AlwaysUpdateCache} is set to FALSE (defaults to TRUE for compatibility) then avoid calling _updateCache unless requested. DBCachePlugin now only asked for it from the afterSaveHandler and from the new REST updateCache handlerpackage TWiki::Contrib::DBCacheContrib::Search
Search operators work on the fields of a TWiki::Contrib::DBCacheContrib::Map. Fields are given by name, and values by strings or numbers. Strings should always be surrounded by 'single-quotes'. Strings which are regular expressions (RHS of,
~ operators) use 'perl' regular expression syntax (google for =perlre
for help). Numbers can be signed integers or decimals. Single quotes in values may be escaped using backslash (\).
The following operators are available:
Operator![]() |
Result | Meaning |
---|---|---|
= |
Boolean | LHS exactly matches the regular expression on the RHS. The expression must match the whole string. |
! |
Boolean | Unary NOT |
!= |
Boolean | Inverse of = |
() |
any | Bracketed subexpression |
AND |
Boolean | AND |
EARLIER_THAN |
BOOLEAN | Date is earlier than the given date |
> |
Boolean | Numeric > |
>= |
Boolean | Numeric >= |
LATER_THAN |
Boolean | LHS is later than the given date (string containing a date e.g. '1 Apr 2003') |
lc |
String | Unary lower case |
< |
Boolean | Numeric < |
<= |
Boolean | Numeric <= |
OR |
Boolean | OR |
uc |
String | Unary UPPER CASE |
WITHIN_DAYS |
Boolean | Date (which must be in the future) is within n working days of todays date |
=~ |
Boolean | LHS contains RHS i.e. the RHS is found somewhere in the field value. |
EARLIER_THAN
, LATER_THAN
and WITHIN_DAYS
must be dates in the format expected by Time::ParseDate
(like the ActionTrackerPlugin). WITHIN_DAYS
works out the number of working days (i.e. excluding Saturday and Sunday). Apologies in advance if your weekend is offset ± a day! Integers will automatically be converted to dates, by assuming they represent a number of seconds since midnight GMT on 1st January 1970.
Example
Get a list of attachments that have a date earlier than 1st January 2000$db = new TWiki::Contrib::DBCacheContrib::DBCache( $web ); # always done $db->load(); my $search = new TWiki::Contrib::DBCacheContrib::Search("date EARLIER_THAN '1st January 2000'"); foreach my $topic ($db->getKeys()) { my $attachments = $topic->get("attachments"); foreach my $val ($attachments->getValues()) { if ($search->matches($val)) { print $val->get("name") . "\n"; } } }A search object implements the "matches" method as its general contract with the rest of the world.
new($string)
-
$string
- string containing an expression to parse
toString()
-> string
Generates a string representation of the object.
--+++ =addOperator(%oper)
Add an operator to the parser
%oper
is a hash, containing the following fields: -
name
- operator string -
prec
- operator precedence, positive non-zero integer. Larger number => higher precedence. -
arity
- set to 1 if this operator is unary, 2 for binary. Arity 0 is legal, should you ever need it. -
exec
- the handler to implement the new operator
package TWiki::Contrib::DBCacheContrib::FileTime
Object that handles a file/time tuple for use in Storable andTWiki::Contrib::DBCacheContrib::Archive
.
new($file)
-
$file
- filename
uptodate()
-> boolean
Check the file time against what is seen on disc. Return 1 if consistent, 0 if inconsistent.
toString()
-> string
Generates a string representation of the object.
write()
TWiki::Contrib::DBCacheContrib::Archive hook
read()
TWiki::Contrib::DBCacheContrib::Archive hook
package TWiki::Contrib::DBCacheContrib::Array
Generic array object. This is required because perl arrays are not objects, and cannot be subclassed e.g. for serialisation. To avoid lots of horrid code to handle special cases of the different perl data structures, we use this array object instead. new()
Create a new, empty array object
add($object)
-
$object
any perl data type
find($object)
-> integer
- $object datum of the same type as the content of the array
remove($index)
-
$index
- integer index
get($key, $root)
-> datum
-
$k
- key - $root - what # refers to
-
get("9", $r)
where $n is a number will get the 9th entry in the array -
get("[9]", $r)
will also get the 9th entry -
get(".9", $r)
will also get the 9th entry -
get(".X", $r)
will return the sum of the subfieldX
of each entry -
get("[?search]", $r)
will perform the given search over the entries in the array. Always returns an array result, even when there is only one result. For example:[?name='Sam']
will return an array of all the entries that have their subfieldname
set toSam
. -
#
means "reset to root". Soget("#[3]", $r)
will return the 4th entry of $r (assuming $r is an array!). -
get("[*X]", $r)
will get a new array made from subfield X of each entry in this array.
get("parent.UserTable[?SubTopic='ThisTopic'].UserName", $web);See also
TWiki::Contrib::DBCacheContrib::Map
for syntax that applies to maps.
size()
-> integer
Get the size of the array
sum($field)
-> number
-
$field
- name of a field in the class of objects stored by this array
search($search)
-> search result
-
$search
- TWiki::Contrib::DBCacheContrib::Search object to use in the search
TWiki::Contrib::DBCacheContrib::Array
of matching entries.
getValues()
-> perl array
Get a "perl" array of the values in the array, suitable for use with foreach
toString($limit, $level, $strung)
-> string
-
$limit
- recursion limit for expansion of elements -
$level
- currentl recursion level
write($archive)
-
$archive
- the TWiki::Contrib::DBCacheContrib::Archive being written to
read($archive)
-
$archive
- the TWiki::Contrib::DBCacheContrib::Archive being read from
package TWiki::Contrib::DBCacheContrib::Map
Generic map object for mapping names to things. A name is defined asname = \w+ | \w+ "." name
The . indicates a field reference in a sub-map.
Objects in the map are either strings, or other objects that must
support toString.
new($string)
- $string - optional attribute string in standard TWiki syntax
fastget($k)
-> datum
-
$k
- key
get($k, $root)
-> datum
-
$k
- key -
$root
what # refers to
$k
; return undef if not set.
Subfield syntax -
get("X",$r)
will get the subfield namedX
. -
get("X.Y",$r)
will get the subfieldY
of the subfield namedX
. -
get("[X]",$r) = will get the subfield named =X
(so X[Y] and X.Y are synonymous).. -
#
means "reset to root". Soget("#.Y", $r) will return the subfield =Y
of $r (assuming $r is a map!), as willget("#[Y]"
.
get("UserTable[0].Surname", $web);See also
TWiki::Contrib::DBCacheContrib::Array
for syntax that applies to arrays.
set($k, $v)
-
$k
- key -
$v
- value
size()
-> integer
Get the size of the map
remove($index)
-> old value
-
$index
- integer index
getKeys()
-> perl array
Get a "perl" array of the keys in the map, suitable for use with foreach
getValues()
-> perl array
Get a "perl" array of the values in the Map, suitable for use with foreach
search($search)
-> search result
-
$search
- TWiki::Contrib::DBCacheContrib::Search object to use in the search
TWiki::Contrib::DBCacheContrib::Array
of matching keys.
toString($limit, $level, $strung)
-> string
-
$limit
- recursion limit for expansion of elements -
$level
- currentl recursion level
write($archive)
-
$archive
- the TWiki::Contrib::DBCacheContrib::Archive being written to
read($archive)
-
$archive
- the TWiki::Contrib::DBCacheContrib::Archive being read from
Installation Instructions
{DBCache}{AlwaysUpdateCache}
If $TWiki::cfg{DBCache}{AlwaysUpdateCache} is set to FALSE (defaults to TRUE for compatibility) then DBCacheContrib will avoid calling_updateCache
unless requested. _updateCache
accesses every topic file in the web, so its an unnecessary performance hit. DBCacePlugin now only requests _updateCache
from the afterSaveHandler and from the new REST updateCache handler.
You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server where TWiki is running.
Like many other TWiki extensions, this module is shipped with a fully
automatic installer script written using the BuildContrib. - If you have TWiki 4.2 or later, you can install from the
configure
interface (Go to Plugins->Find More Extensions)- See the installation supplement
on TWiki.org for more information.
- See the installation supplement
- If you have any problems, then you can still install manually from the command-line:
- Download one of the
.zip
or.tgz
archives - Unpack the archive in the root directory of your TWiki installation.
- Run the installer script (
perl <module>_installer
) - Run
configure
and enable the module, if it is a plugin. - Repeat for any missing dependencies.
- Download one of the
- If you are still having problems, then instead of running the installer script:
- Make sure that the file permissions allow the webserver user to access all files.
- Check in any installed files that have existing
,v
files in your existing install (take care not to lock the files when you check in) - Manually edit LocalSite.cfg to set any configuration variables.
Contrib Info
Author: | TWiki:Main.CrawfordCurrie![]() |
|||||||||
Copyright: | This code is based on an original development of Motorola Inc. and is protected by the following copyrights: © 2002-2003 Motorola Inc. All Rights Reserved. Portions copyright © 2004. Crawford Currie http://www.c-dot.co.uk ![]() © 2004-2011, TWiki:TWiki.TWikiContributor ![]() |
|||||||||
License: | As required for the publication of all extensions to TWiki, this software is published under the terms of the GNU General Public License![]() |
|||||||||
Version: | 21578 (2011-07-07) | |||||||||
Change History: | ||||||||||
2011-07-07: | TWikibug:Item6711![]() |
|||||||||
2010-04-29: | TWikibug:Item6433![]() |
|||||||||
16347 | remove META data from text hash; include META data in all hash. TWiki:Main/MichaelDaum![]() |
|||||||||
16346 | caching all topic elements to an all field to allow th search in all of the text and the formfields like the normal grep-based SEARCH does. TWiki:Main/MichaelDaum![]() |
|||||||||
15868 | fixed WITHIN_DAYS and EARLIER_THAN. TWiki:Main/MichaelDaum![]() |
|||||||||
15583 | made query parser pluggable so that other plugins can implement their own predicates. TWiki:Main/MichaelDaum![]() |
|||||||||
15019 | added {DBCache}{AlwaysUpdateCache} to remove the updateCache from every operation. TWiki:Main/SvenDowideit![]() |
|||||||||
13562 | Bugs:Item3985 - fixed failures with hierarchical webs | |||||||||
13527 | Moved the cache into the extensions work areas, instead of the web directory | |||||||||
12943 | Bugs:Item3659: added automatic conversion of integers to dates | |||||||||
12923 | added REF operator; added link to web object to hashes; fixed parent relation to end in WebHome; added "web" property to topic hashes; caching META:PREFERENCES now | |||||||||
11537 | Added lc and uc operators for case-insensitive searches | |||||||||
9303 | TWikibug:Item1844![]() |
|||||||||
8682 | TWikibug:Item1580![]() |
|||||||||
8110 | TWikibug:Item663![]() |
|||||||||
7552 | TWikibug:Item997![]() |
|||||||||
7274 | TWikibug:Item719![]() |
|||||||||
7262 | TWikibug:Item719![]() |
|||||||||
7260 | TWikibug:Item727![]() |
|||||||||
6353 | TWikibug:Item380![]() |
|||||||||
5720 | Updated tests | |||||||||
5719 | Fix for correct handling of parent relations | |||||||||
5229 | Small improvement to the way it handles errors from Storable and Archive | |||||||||
5223 | Documentation fixes, adding gifs. | |||||||||
5048 | Cairo readiness | |||||||||
5036 | Split from SharedCode | |||||||||
5031 | Moving to new name | |||||||||
5030 | About to rename | |||||||||
5019 | Improved topic data model, cleaned up tests | |||||||||
5008 | Added extended access syntax, [?], [*] etc. | |||||||||
5006 | Doc fixes | |||||||||
5005 | Poddified documentation | |||||||||
5003 | Initial version | |||||||||
8 Jul 2004 | Initial version, split out from FormQueryPlugin | |||||||||
Dependencies: |
|
|||||||||
Perl Version: | 5.0 | |||||||||
Plugin Home: | http://TWiki.org/cgi-bin/view/Plugins/DBCacheContrib![]() |
|||||||||
Feedback: | http://TWiki.org/cgi-bin/view/Plugins/DBCacheContribDev![]() |
|||||||||
Appraisal: | http://TWiki.org/cgi-bin/view/Plugins/DBCacheContribAppraisal![]() |