Debian Codegraph @

gmt-web/content/documentation.html

raw symbols SCIP
1 <h2>UR User Manual</h2>
2 <hr />
3 <p><a name="__index__"></a></p>
4 <!-- INDEX BEGIN -->
5
6 <ul>
7
8 <li><a href="#name">NAME</a></li>
9 <li><a href="#perspective_on_objects">Perspective on Objects</a></li>
10 <li><a href="#objectrelational_mapping">Object-Relational Mapping</a></li>
11 <li><a href="#object_context">Object Context</a></li>
12 <ul>
13
14 <li><a href="#benefits">Benefits</a></li>
15 <li><a href="#issues">Issues</a></li>
16 </ul>
17
18 <li><a href="#class_definitions">Class Definitions</a></li>
19 <li><a href="#metadata">Metadata</a></li>
20 <li><a href="#documentation_system">Documentation System</a></li>
21 <li><a href="#iterators">Iterators</a></li>
22 <li><a href="#command_line_tools">Command Line Tools</a></li>
23 <li><a href="#example">Example</a></li>
24 <ul>
25 <li><a href="#item_paththing_2fpath_2epm">PathThing/Path.pm</a></li>
26 <li><a href="#item_paththing_2fnode_2epm">PathThing/Node.pm</a></li>
27 </ul>
28 </ul>
29 <!-- INDEX END -->
30
31 <hr />
32 <h1><a name="name">NAME</a></h1>
33 <p>UR::Manual::Overview - UR from Ten Thousand Feet</p>
34 <hr />
35
36 <h1><a name="perspective_on_objects">Perspective on Objects</a></h1>
37 <p>Standard software languages provide a facility for making objects. Those
38 objects have certain characteristics which are different with UR objects.</p>
39
40 <p>A standard object in most languages:</p>
41 <ul>
42 <li>exists only as long as the program which created it has a reference to it</li>
43 <li>requires that the developer manage organizing the <code>object(s)</code> into a structure
44 to support any searching required</li>
45 <li>handles persistence between processes explicitly, by saving or loading the object to external storage</li>
46 <li>references other objects only if explicitly linked to those objects</li>
47 <li>acts as a functional software device, but any meaning associated with the object is implied by how it is used</li>
48 </ul>
49
50 <p>Regular objects like those described above are the building blocks of most software.</p>
51
52 <p>In many cases, however, they are often used for a second, higher-level
53 purpose: defining entities in the domain model of the problem area the
54 software addresses. UR objects are tailored to represent domain model
55 entities well. In some sense, UR objects follow many of the design principles
56 present in relational databases, and as such mapping to a database for UR
57 objects is trivial, and can be done in complex ways.</p>
58 <p>UR objects differ from a standard object in the following key ways:</p>
59 <ul>
60 <li>the object exists after creation until explicitly deleted, or the transaction it is in rolled-back</li>
61 <li>managing loaded objects is done automatically by a Context object, which handles queries, saving, lazy-loading and caching</li>
62 <li>it is possible to query for an object by specifying the class and the matching characteristics</li>
63 <li>the object can reference other objects which are not loaded in the current process, and be referenced by objects not in the current process</li>
64 <li>the object is a particular truth-assertion in the context in which it exists</li>
65 </ul>
66
67 <hr />
68 <h1><a name="objectrelational_mapping">Object-Relational Mapping</a></h1>
69 <p>UR's primary reason for existing is to function as an ORM. That is, managing
70 how to store instances of objects in memory of a running program with more
71 persistant storage in a relational database, and retrieve them later. It
72 handles the common cases where each table is implemented by a class their
73 columns are properties of the classes; retrieving objects by arbitrary
74 properties; creating, updating and deleting objects with enforced database
75 constraints; and named relationships between classes.</p>
76
77 <p>It can also handle more complicated things like:</p>
78 <ul>
79 <li>classes for things which are not database entities at all</li>
80 <li>derived classes where the data spans multiple tables between the parent and child classes</li>
81 <li>loading an object through a parent class and having it automatically reblessed into the appropriate subclass</li>
82 <li>properties with no DB column behind them</li>
83 <li>calculated properties with a formula behind them</li>
84 <li>inheritance hierarchies that may have tables missing at some or all stages</li>
85 <li>meta-data about Properties, Classes and the relationships between them</li>
86 </ul>
87 <hr />
88
89 <h1><a name="object_context">Object Context</a></h1>
90 <p>With UR, every object you create is made a part of the current ``Context''.
91 Conceptually, the Context is the lens by which your application views the
92 data that exists in the world. At one level, you can think of the current
93 context as an in-memory transaction. All changes to the object are tracked
94 by the context. The Context knows how to map objects to their storage
95 locations, called Data Sources. Saving your changes is simply a matter of
96 asking the current context to commit.</p>
97 <p>The Context can also reverse the saving process, and map a request for an
98 object to a query of external storage. Requests for objects go through the
99 Context, are loaded from outside as needed, and are returned to the caller
100 after being made part of the current context's transaction.</p>
101 <p>Objects never reference each other by actual Perl reference internally,
102 instead they use the referent's ID. Accessors on an object which return
103 another object send the ID through the context to get the object back,
104 allowing the context to load the referenced object only when it is actually
105 needed. This means that your objects can hook together until references
106 span an entire database schema, and pulling one object from the database
107 will not load the entire database into memory.</p>
108 <p>The context handles caching, and by default will cache everything it
109 touches. This means that you can ask for the same thing multiple times,
110 and only the first request will actually hit the underlying database.
111 It also means that requests for objects which map to the same ID will return
112 the exact same instance of the object.</p>
113 <p>The net effect is that each process's context is an in-memory database. All
114 object creation, deletion, and change is occurring directly to that database.
115 For objects configured to have external persistence, this database manages
116 itself as a ``diff'' vs. the external database, allowing it to simulate
117 representing all UR data everywhere, while only actually tracking what is needed.</p>
118
119 <h2><a name="benefits">Benefits</a></h2>
120 <ul>
121 <li>database queries don't repeat themselves again and again</li>
122 <li>you never write insert/update/delete statements, or work out constraint order yourself</li>
123 <li>allows you to write methods which address an object individually, with ways to avoid many individual database queries</li>
124 <li>explicitly clearing the cache is less complex than explicitly managing the caching of data</li>
125 </ul>
126
127 <h2><a name="issues">Issues</a></h2>
128 <ul>
129 <li>the cache grows until you explicitly clear it, or allow the Context to prune the cache by setting object count limits explicitly</li>
130 <li>there is CPU overhead checking the cache if you really are always going directly to the database</li>
131 </ul>
132
133 <hr />
134 <h1><a name="class_definitions">Class Definitions</a></h1>
135 <p>At the top of every module implementing a UR class is a block of code that
136 defines the class to explicitly spell out its inheritance, properties and
137 types, constraints, relationships to other classes and where the persistent
138 storage is located. It's meant to be easy to read and edit, if necessary. If
139 the class is backed by a database table, then it can also maintain itself.</p>
140
141 <hr />
142 <h1><a name="metadata">Metadata</a></h1>
143 <p>Besides the object instances representing data used by the program, the UR
144 system has other objects representing metadata about the classes (class
145 information, properties, relationships, etc), database entities (databases,
146 tables, columns, constraints, etc), transactions, data sources, etc. All the
147 metadata is accessable through the same API as any of the database-backed
148 data.</p>
149 <p>For classes backed by the database, after a schema change (like adding tables
150 or columns, altering types or constraints), a command-line tool can
151 automatically detect the change and alter the class definition in the Perl
152 module to keep the metadata in sync with the database.</p>
153
154 <hr />
155 <h1><a name="documentation_system">Documentation System</a></h1>
156 <p>At the simplest level, most entities have a 'doc' metadata attribute to
157 attach some kind of documentation to. There's also a set of tools that can
158 be run from the command line or a web browser to view the documentation. It
159 can also be used to browse through the class and database metadata, and
160 generate diagrams about the metadata.</p>
161
162 <hr />
163 <h1><a name="iterators">Iterators</a></h1>
164 <p>If a retrieval from the database is likely to result in the generation of
165 tons of objects, you can choose to get them back in a list and keep them all
166 in memory, or get back a special Iterator object that the program can use to
167 get back objects in batches.</p>
168
169 <hr />
170 <h1><a name="command_line_tools">Command Line Tools</a></h1>
171 <p>UR has a central command-line tool that cam be used to manipulate the
172 metadata in different ways. Setting up namespaces, creating data sources,
173 syncing classes with schemas, accessing documentation, etc.</p>
174 <p>There is also a framework for creating classes that represent command line
175 tools, their parameters and results, and makes it easy to create tools
176 through the Command Pattern.</p>
177
178 <hr />
179 <h1><a name="example">Example</a></h1>
180 <p>Given these classes:</p>
181
182 <p><strong><a name="item_paththing_2fpath_2epm">PathThing/Path.pm</a></strong></p>
183 <pre class="terminal">
184 use strict;
185 use warnings;
186
187 use PathThing; # The application's UR::Namespace module
188
189 class PathThing::Path {
190 id_by =&gt; 'path_id',
191 has =&gt; [
192 desc =&gt; { is =&gt; 'String' },
193 length =&gt; { is =&gt; 'Integer' },
194 ],
195 data_source =&gt; 'PathThing::DataSource::TheDB',
196 table_name =&gt; 'PATHS',
197 };
198 </pre>
199
200 <p><strong><a name="item_paththing_2fnode_2epm">PathThing/Node.pm</a></strong></p>
201 <pre class="terminal">
202 class PathThing::Node {
203 id_by =&gt; 'node_id',
204 has =&gt; [
205 left_path =&gt; { is =&gt; 'PathThing::Path', id_by =&gt; 'left_path_id' },
206 left_path_desc =&gt; { via =&gt; 'left_path', to =&gt; 'desc' },
207 left_path_length =&gt; { via =&gt; 'left_path', to =&gt; 'length' },
208 right_path =&gt; { is =&gt; 'PathThing::Path', id_by =&gt; 'right_path_id' },
209 right_path_desc =&gt; { via =&gt; 'right_path', to =&gt; 'desc' },
210 right_path_length =&gt; { via =&gt; 'right_path', to =&gt; 'length' },
211 map_coord_x =&gt; { is =&gt; 'Integer' },
212 map_coord_y =&gt; { is =&gt; 'String' },
213 ],
214 data_source =&gt; 'PathThing::DataSource::TheDB',
215 table_name =&gt; 'NODES',
216 };
217 </pre>
218
219 <p>For a script like this one:</p>
220 <pre class="terminal">
221 use PathThing::Node;
222 my @results = PathThing::Node-&gt;get(
223 right_path_desc =&gt; 'over the river',
224 left_path_desc =&gt; 'through the woods',
225 right_path_length =&gt; 10,
226 );
227 </pre>
228 <p>It will generate SQL like this:</p>
229 <pre class="terminal">
230 select NODES.NODE_ID, NODES.LEFT_PATH_ID, NODES.RIGHT_PATH_ID,
231 NODES.MAP_COORD_X, NODES.MAP_COORD_Y,
232 left_path_1.PATH_ID, left_path_1.DESC, left_path_1.LENGTH
233 right_path_1.PATH_ID, right_path_1.DESC, right_path_1.LENGTH
234 from NODES
235 join PATHS left_path_1 on NODES.LEFT_PATH_ID = left_path_1.PATH_ID
236 join PATHS right_path_1 on NODES.RIGHT_PATH_ID = right_path1.PATH_ID
237 where left_path_1.DESC = 'through the woods'
238 and right_path_1.DESC = 'over the river',
239 and right_path_1.LENGTH = 10
240 </pre>
241 <p>And for every row returned by the query, a PathThing::Node and two
242 PathThing::Path objects will be instantiated and stored in the Context's
243 cache. <code>@results</code> will contain a list of matching PathThing::Node objects.</p>
244