hgbook

view en/ch03-concepts.xml @ 699:a17d6390a480

More fixes to chapters 1 and 2.
author Bryan O'Sullivan <bos@serpentine.com>
date Sun May 03 20:27:09 2009 -0700 (2009-05-03)
parents b338f5490029
children 477d6a3e5023
line source
1 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->
3 <chapter id="chap:concepts">
4 <?dbhtml filename="behind-the-scenes.html"?>
5 <title>Behind the scenes</title>
7 <para id="x_2e8">Unlike many revision control systems, the concepts
8 upon which Mercurial is built are simple enough that it's easy to
9 understand how the software really works. Knowing these details
10 certainly isn't necessary, so it is certainly safe to skip this
11 chapter. However, I think you will get more out of the software
12 with a <quote>mental model</quote> of what's going on.</para>
14 <para id="x_2e9">Being able to understand what's going on behind the
15 scenes gives me confidence that Mercurial has been carefully
16 designed to be both <emphasis>safe</emphasis> and
17 <emphasis>efficient</emphasis>. And just as importantly, if it's
18 easy for me to retain a good idea of what the software is doing
19 when I perform a revision control task, I'm less likely to be
20 surprised by its behavior.</para>
22 <para id="x_2ea">In this chapter, we'll initially cover the core concepts
23 behind Mercurial's design, then continue to discuss some of the
24 interesting details of its implementation.</para>
26 <sect1>
27 <title>Mercurial's historical record</title>
29 <sect2>
30 <title>Tracking the history of a single file</title>
32 <para id="x_2eb">When Mercurial tracks modifications to a file, it stores
33 the history of that file in a metadata object called a
34 <emphasis>filelog</emphasis>. Each entry in the filelog
35 contains enough information to reconstruct one revision of the
36 file that is being tracked. Filelogs are stored as files in
37 the <filename role="special"
38 class="directory">.hg/store/data</filename> directory. A
39 filelog contains two kinds of information: revision data, and
40 an index to help Mercurial to find a revision
41 efficiently.</para>
43 <para id="x_2ec">A file that is large, or has a lot of history, has its
44 filelog stored in separate data
45 (<quote><literal>.d</literal></quote> suffix) and index
46 (<quote><literal>.i</literal></quote> suffix) files. For
47 small files without much history, the revision data and index
48 are combined in a single <quote><literal>.i</literal></quote>
49 file. The correspondence between a file in the working
50 directory and the filelog that tracks its history in the
51 repository is illustrated in <xref
52 linkend="fig:concepts:filelog"/>.</para>
54 <figure id="fig:concepts:filelog">
55 <title>Relationships between files in working directory and
56 filelogs in repository</title>
57 <mediaobject>
58 <imageobject><imagedata fileref="figs/filelog.png"/></imageobject>
59 <textobject><phrase>XXX add text</phrase></textobject>
60 </mediaobject>
61 </figure>
63 </sect2>
64 <sect2>
65 <title>Managing tracked files</title>
67 <para id="x_2ee">Mercurial uses a structure called a
68 <emphasis>manifest</emphasis> to collect together information
69 about the files that it tracks. Each entry in the manifest
70 contains information about the files present in a single
71 changeset. An entry records which files are present in the
72 changeset, the revision of each file, and a few other pieces
73 of file metadata.</para>
75 </sect2>
76 <sect2>
77 <title>Recording changeset information</title>
79 <para id="x_2ef">The <emphasis>changelog</emphasis> contains information
80 about each changeset. Each revision records who committed a
81 change, the changeset comment, other pieces of
82 changeset-related information, and the revision of the
83 manifest to use.</para>
85 </sect2>
86 <sect2>
87 <title>Relationships between revisions</title>
89 <para id="x_2f0">Within a changelog, a manifest, or a filelog, each
90 revision stores a pointer to its immediate parent (or to its
91 two parents, if it's a merge revision). As I mentioned above,
92 there are also relationships between revisions
93 <emphasis>across</emphasis> these structures, and they are
94 hierarchical in nature.</para>
96 <para id="x_2f1">For every changeset in a repository, there is exactly one
97 revision stored in the changelog. Each revision of the
98 changelog contains a pointer to a single revision of the
99 manifest. A revision of the manifest stores a pointer to a
100 single revision of each filelog tracked when that changeset
101 was created. These relationships are illustrated in
102 <xref linkend="fig:concepts:metadata"/>.</para>
104 <figure id="fig:concepts:metadata">
105 <title>Metadata relationships</title>
106 <mediaobject>
107 <imageobject><imagedata fileref="figs/metadata.png"/></imageobject>
108 <textobject><phrase>XXX add text</phrase></textobject>
109 </mediaobject>
110 </figure>
112 <para id="x_2f3">As the illustration shows, there is
113 <emphasis>not</emphasis> a <quote>one to one</quote>
114 relationship between revisions in the changelog, manifest, or
115 filelog. If the manifest hasn't changed between two
116 changesets, the changelog entries for those changesets will
117 point to the same revision of the manifest. If a file that
118 Mercurial tracks hasn't changed between two changesets, the
119 entry for that file in the two revisions of the manifest will
120 point to the same revision of its filelog.</para>
122 </sect2>
123 </sect1>
124 <sect1>
125 <title>Safe, efficient storage</title>
127 <para id="x_2f4">The underpinnings of changelogs, manifests, and filelogs are
128 provided by a single structure called the
129 <emphasis>revlog</emphasis>.</para>
131 <sect2>
132 <title>Efficient storage</title>
134 <para id="x_2f5">The revlog provides efficient storage of revisions using a
135 <emphasis>delta</emphasis> mechanism. Instead of storing a
136 complete copy of a file for each revision, it stores the
137 changes needed to transform an older revision into the new
138 revision. For many kinds of file data, these deltas are
139 typically a fraction of a percent of the size of a full copy
140 of a file.</para>
142 <para id="x_2f6">Some obsolete revision control systems can only work with
143 deltas of text files. They must either store binary files as
144 complete snapshots or encoded into a text representation, both
145 of which are wasteful approaches. Mercurial can efficiently
146 handle deltas of files with arbitrary binary contents; it
147 doesn't need to treat text as special.</para>
149 </sect2>
150 <sect2 id="sec:concepts:txn">
151 <title>Safe operation</title>
153 <para id="x_2f7">Mercurial only ever <emphasis>appends</emphasis> data to
154 the end of a revlog file. It never modifies a section of a
155 file after it has written it. This is both more robust and
156 efficient than schemes that need to modify or rewrite
157 data.</para>
159 <para id="x_2f8">In addition, Mercurial treats every write as part of a
160 <emphasis>transaction</emphasis> that can span a number of
161 files. A transaction is <emphasis>atomic</emphasis>: either
162 the entire transaction succeeds and its effects are all
163 visible to readers in one go, or the whole thing is undone.
164 This guarantee of atomicity means that if you're running two
165 copies of Mercurial, where one is reading data and one is
166 writing it, the reader will never see a partially written
167 result that might confuse it.</para>
169 <para id="x_2f9">The fact that Mercurial only appends to files makes it
170 easier to provide this transactional guarantee. The easier it
171 is to do stuff like this, the more confident you should be
172 that it's done correctly.</para>
174 </sect2>
175 <sect2>
176 <title>Fast retrieval</title>
178 <para id="x_2fa">Mercurial cleverly avoids a pitfall common to all earlier
179 revision control systems: the problem of <emphasis>inefficient
180 retrieval</emphasis>. Most revision control systems store
181 the contents of a revision as an incremental series of
182 modifications against a <quote>snapshot</quote>. To
183 reconstruct a specific revision, you must first read the
184 snapshot, and then every one of the revisions between the
185 snapshot and your target revision. The more history that a
186 file accumulates, the more revisions you must read, hence the
187 longer it takes to reconstruct a particular revision.</para>
189 <figure id="fig:concepts:snapshot">
190 <title>Snapshot of a revlog, with incremental deltas</title>
191 <mediaobject>
192 <imageobject><imagedata fileref="figs/snapshot.png"/></imageobject>
193 <textobject><phrase>XXX add text</phrase></textobject>
194 </mediaobject>
195 </figure>
197 <para id="x_2fc">The innovation that Mercurial applies to this problem is
198 simple but effective. Once the cumulative amount of delta
199 information stored since the last snapshot exceeds a fixed
200 threshold, it stores a new snapshot (compressed, of course),
201 instead of another delta. This makes it possible to
202 reconstruct <emphasis>any</emphasis> revision of a file
203 quickly. This approach works so well that it has since been
204 copied by several other revision control systems.</para>
206 <para id="x_2fd"><xref linkend="fig:concepts:snapshot"/> illustrates
207 the idea. In an entry in a revlog's index file, Mercurial
208 stores the range of entries from the data file that it must
209 read to reconstruct a particular revision.</para>
211 <sect3>
212 <title>Aside: the influence of video compression</title>
214 <para id="x_2fe">If you're familiar with video compression or have ever
215 watched a TV feed through a digital cable or satellite
216 service, you may know that most video compression schemes
217 store each frame of video as a delta against its predecessor
218 frame. In addition, these schemes use <quote>lossy</quote>
219 compression techniques to increase the compression ratio, so
220 visual errors accumulate over the course of a number of
221 inter-frame deltas.</para>
223 <para id="x_2ff">Because it's possible for a video stream to <quote>drop
224 out</quote> occasionally due to signal glitches, and to
225 limit the accumulation of artefacts introduced by the lossy
226 compression process, video encoders periodically insert a
227 complete frame (called a <quote>key frame</quote>) into the
228 video stream; the next delta is generated against that
229 frame. This means that if the video signal gets
230 interrupted, it will resume once the next key frame is
231 received. Also, the accumulation of encoding errors
232 restarts anew with each key frame.</para>
234 </sect3>
235 </sect2>
236 <sect2>
237 <title>Identification and strong integrity</title>
239 <para id="x_300">Along with delta or snapshot information, a revlog entry
240 contains a cryptographic hash of the data that it represents.
241 This makes it difficult to forge the contents of a revision,
242 and easy to detect accidental corruption.</para>
244 <para id="x_301">Hashes provide more than a mere check against corruption;
245 they are used as the identifiers for revisions. The changeset
246 identification hashes that you see as an end user are from
247 revisions of the changelog. Although filelogs and the
248 manifest also use hashes, Mercurial only uses these behind the
249 scenes.</para>
251 <para id="x_302">Mercurial verifies that hashes are correct when it
252 retrieves file revisions and when it pulls changes from
253 another repository. If it encounters an integrity problem, it
254 will complain and stop whatever it's doing.</para>
256 <para id="x_303">In addition to the effect it has on retrieval efficiency,
257 Mercurial's use of periodic snapshots makes it more robust
258 against partial data corruption. If a revlog becomes partly
259 corrupted due to a hardware error or system bug, it's often
260 possible to reconstruct some or most revisions from the
261 uncorrupted sections of the revlog, both before and after the
262 corrupted section. This would not be possible with a
263 delta-only storage model.</para>
265 </sect2>
266 </sect1>
267 <sect1>
268 <title>Revision history, branching, and merging</title>
270 <para id="x_304">Every entry in a Mercurial revlog knows the identity of its
271 immediate ancestor revision, usually referred to as its
272 <emphasis>parent</emphasis>. In fact, a revision contains room
273 for not one parent, but two. Mercurial uses a special hash,
274 called the <quote>null ID</quote>, to represent the idea
275 <quote>there is no parent here</quote>. This hash is simply a
276 string of zeroes.</para>
278 <para id="x_305">In <xref linkend="fig:concepts:revlog"/>, you can see
279 an example of the conceptual structure of a revlog. Filelogs,
280 manifests, and changelogs all have this same structure; they
281 differ only in the kind of data stored in each delta or
282 snapshot.</para>
284 <para id="x_306">The first revision in a revlog (at the bottom of the image)
285 has the null ID in both of its parent slots. For a
286 <quote>normal</quote> revision, its first parent slot contains
287 the ID of its parent revision, and its second contains the null
288 ID, indicating that the revision has only one real parent. Any
289 two revisions that have the same parent ID are branches. A
290 revision that represents a merge between branches has two normal
291 revision IDs in its parent slots.</para>
293 <figure id="fig:concepts:revlog">
294 <title>The conceptual structure of a revlog</title>
295 <mediaobject>
296 <imageobject><imagedata fileref="figs/revlog.png"/></imageobject>
297 <textobject><phrase>XXX add text</phrase></textobject>
298 </mediaobject>
299 </figure>
301 </sect1>
302 <sect1>
303 <title>The working directory</title>
305 <para id="x_307">In the working directory, Mercurial stores a snapshot of the
306 files from the repository as of a particular changeset.</para>
308 <para id="x_308">The working directory <quote>knows</quote> which changeset
309 it contains. When you update the working directory to contain a
310 particular changeset, Mercurial looks up the appropriate
311 revision of the manifest to find out which files it was tracking
312 at the time that changeset was committed, and which revision of
313 each file was then current. It then recreates a copy of each of
314 those files, with the same contents it had when the changeset
315 was committed.</para>
317 <para id="x_309">The <emphasis>dirstate</emphasis> contains Mercurial's
318 knowledge of the working directory. This details which
319 changeset the working directory is updated to, and all of the
320 files that Mercurial is tracking in the working
321 directory.</para>
323 <para id="x_30a">Just as a revision of a revlog has room for two parents, so
324 that it can represent either a normal revision (with one parent)
325 or a merge of two earlier revisions, the dirstate has slots for
326 two parents. When you use the <command role="hg-cmd">hg
327 update</command> command, the changeset that you update to is
328 stored in the <quote>first parent</quote> slot, and the null ID
329 in the second. When you <command role="hg-cmd">hg
330 merge</command> with another changeset, the first parent
331 remains unchanged, and the second parent is filled in with the
332 changeset you're merging with. The <command role="hg-cmd">hg
333 parents</command> command tells you what the parents of the
334 dirstate are.</para>
336 <sect2>
337 <title>What happens when you commit</title>
339 <para id="x_30b">The dirstate stores parent information for more than just
340 book-keeping purposes. Mercurial uses the parents of the
341 dirstate as <emphasis>the parents of a new
342 changeset</emphasis> when you perform a commit.</para>
344 <figure id="fig:concepts:wdir">
345 <title>The working directory can have two parents</title>
346 <mediaobject>
347 <imageobject><imagedata fileref="figs/wdir.png"/></imageobject>
348 <textobject><phrase>XXX add text</phrase></textobject>
349 </mediaobject>
350 </figure>
352 <para id="x_30d"><xref linkend="fig:concepts:wdir"/> shows the
353 normal state of the working directory, where it has a single
354 changeset as parent. That changeset is the
355 <emphasis>tip</emphasis>, the newest changeset in the
356 repository that has no children.</para>
358 <figure id="fig:concepts:wdir-after-commit">
359 <title>The working directory gains new parents after a
360 commit</title>
361 <mediaobject>
362 <imageobject><imagedata fileref="figs/wdir-after-commit.png"/></imageobject>
363 <textobject><phrase>XXX add text</phrase></textobject>
364 </mediaobject>
365 </figure>
367 <para id="x_30f">It's useful to think of the working directory as
368 <quote>the changeset I'm about to commit</quote>. Any files
369 that you tell Mercurial that you've added, removed, renamed,
370 or copied will be reflected in that changeset, as will
371 modifications to any files that Mercurial is already tracking;
372 the new changeset will have the parents of the working
373 directory as its parents.</para>
375 <para id="x_310">After a commit, Mercurial will update the
376 parents of the working directory, so that the first parent is
377 the ID of the new changeset, and the second is the null ID.
378 This is shown in <xref
379 linkend="fig:concepts:wdir-after-commit"/>. Mercurial
380 doesn't touch any of the files in the working directory when
381 you commit; it just modifies the dirstate to note its new
382 parents.</para>
384 </sect2>
385 <sect2>
386 <title>Creating a new head</title>
388 <para id="x_311">It's perfectly normal to update the working directory to a
389 changeset other than the current tip. For example, you might
390 want to know what your project looked like last Tuesday, or
391 you could be looking through changesets to see which one
392 introduced a bug. In cases like this, the natural thing to do
393 is update the working directory to the changeset you're
394 interested in, and then examine the files in the working
395 directory directly to see their contents as they were when you
396 committed that changeset. The effect of this is shown in
397 <xref linkend="fig:concepts:wdir-pre-branch"/>.</para>
399 <figure id="fig:concepts:wdir-pre-branch">
400 <title>The working directory, updated to an older
401 changeset</title>
402 <mediaobject>
403 <imageobject><imagedata fileref="figs/wdir-pre-branch.png"/></imageobject>
404 <textobject><phrase>XXX add text</phrase></textobject>
405 </mediaobject>
406 </figure>
408 <para id="x_313">Having updated the working directory to an
409 older changeset, what happens if you make some changes, and
410 then commit? Mercurial behaves in the same way as I outlined
411 above. The parents of the working directory become the
412 parents of the new changeset. This new changeset has no
413 children, so it becomes the new tip. And the repository now
414 contains two changesets that have no children; we call these
415 <emphasis>heads</emphasis>. You can see the structure that
416 this creates in <xref
417 linkend="fig:concepts:wdir-branch"/>.</para>
419 <figure id="fig:concepts:wdir-branch">
420 <title>After a commit made while synced to an older
421 changeset</title>
422 <mediaobject>
423 <imageobject><imagedata fileref="figs/wdir-branch.png"/></imageobject>
424 <textobject><phrase>XXX add text</phrase></textobject>
425 </mediaobject>
426 </figure>
428 <note>
429 <para id="x_315"> If you're new to Mercurial, you should keep in mind a
430 common <quote>error</quote>, which is to use the <command
431 role="hg-cmd">hg pull</command> command without any
432 options. By default, the <command role="hg-cmd">hg
433 pull</command> command <emphasis>does not</emphasis>
434 update the working directory, so you'll bring new changesets
435 into your repository, but the working directory will stay
436 synced at the same changeset as before the pull. If you
437 make some changes and commit afterwards, you'll thus create
438 a new head, because your working directory isn't synced to
439 whatever the current tip is.</para>
441 <para id="x_316"> I put the word <quote>error</quote> in
442 quotes because all that you need to do to rectify this
443 situation is <command role="hg-cmd">hg merge</command>, then
444 <command role="hg-cmd">hg commit</command>. In other words,
445 this almost never has negative consequences; it's just
446 something of a surprise for newcomers. I'll discuss other
447 ways to avoid this behavior, and why Mercurial behaves in
448 this initially surprising way, later on.</para>
449 </note>
451 </sect2>
452 <sect2>
453 <title>Merging changes</title>
455 <para id="x_317">When you run the <command role="hg-cmd">hg
456 merge</command> command, Mercurial leaves the first parent
457 of the working directory unchanged, and sets the second parent
458 to the changeset you're merging with, as shown in <xref
459 linkend="fig:concepts:wdir-merge"/>.</para>
461 <figure id="fig:concepts:wdir-merge">
462 <title>Merging two heads</title>
463 <mediaobject>
464 <imageobject>
465 <imagedata fileref="figs/wdir-merge.png"/>
466 </imageobject>
467 <textobject><phrase>XXX add text</phrase></textobject>
468 </mediaobject>
469 </figure>
471 <para id="x_319">Mercurial also has to modify the working directory, to
472 merge the files managed in the two changesets. Simplified a
473 little, the merging process goes like this, for every file in
474 the manifests of both changesets.</para>
475 <itemizedlist>
476 <listitem><para id="x_31a">If neither changeset has modified a file, do
477 nothing with that file.</para>
478 </listitem>
479 <listitem><para id="x_31b">If one changeset has modified a file, and the
480 other hasn't, create the modified copy of the file in the
481 working directory.</para>
482 </listitem>
483 <listitem><para id="x_31c">If one changeset has removed a file, and the
484 other hasn't (or has also deleted it), delete the file
485 from the working directory.</para>
486 </listitem>
487 <listitem><para id="x_31d">If one changeset has removed a file, but the
488 other has modified the file, ask the user what to do: keep
489 the modified file, or remove it?</para>
490 </listitem>
491 <listitem><para id="x_31e">If both changesets have modified a file,
492 invoke an external merge program to choose the new
493 contents for the merged file. This may require input from
494 the user.</para>
495 </listitem>
496 <listitem><para id="x_31f">If one changeset has modified a file, and the
497 other has renamed or copied the file, make sure that the
498 changes follow the new name of the file.</para>
499 </listitem></itemizedlist>
500 <para id="x_320">There are more details&emdash;merging has plenty of corner
501 cases&emdash;but these are the most common choices that are
502 involved in a merge. As you can see, most cases are
503 completely automatic, and indeed most merges finish
504 automatically, without requiring your input to resolve any
505 conflicts.</para>
507 <para id="x_321">When you're thinking about what happens when you commit
508 after a merge, once again the working directory is <quote>the
509 changeset I'm about to commit</quote>. After the <command
510 role="hg-cmd">hg merge</command> command completes, the
511 working directory has two parents; these will become the
512 parents of the new changeset.</para>
514 <para id="x_322">Mercurial lets you perform multiple merges, but you must
515 commit the results of each individual merge as you go. This
516 is necessary because Mercurial only tracks two parents for
517 both revisions and the working directory. While it would be
518 technically possible to merge multiple changesets at once, the
519 prospect of user confusion and making a terrible mess of a
520 merge immediately becomes overwhelming.</para>
522 </sect2>
524 <sect2>
525 <title>Merging and renames</title>
527 <para id="x_69a">A surprising number of revision control systems pay little
528 or no attention to a file's <emphasis>name</emphasis> over
529 time. For instance, it used to be common that if a file got
530 renamed on one side of a merge, the changes from the other
531 side would be silently dropped.</para>
533 <para id="x_69b">Mercurial records metadata when you tell it to perform a
534 rename or copy. It uses this metadata during a merge to do the
535 right thing in the case of a merge. For instance, if I rename
536 a file, and you edit it without renaming it, when we merge our
537 work the file will be renamed and have your edits
538 applied.</para>
539 </sect2>
540 </sect1>
542 <sect1>
543 <title>Other interesting design features</title>
545 <para id="x_323">In the sections above, I've tried to highlight some of the
546 most important aspects of Mercurial's design, to illustrate that
547 it pays careful attention to reliability and performance.
548 However, the attention to detail doesn't stop there. There are
549 a number of other aspects of Mercurial's construction that I
550 personally find interesting. I'll detail a few of them here,
551 separate from the <quote>big ticket</quote> items above, so that
552 if you're interested, you can gain a better idea of the amount
553 of thinking that goes into a well-designed system.</para>
555 <sect2>
556 <title>Clever compression</title>
558 <para id="x_324">When appropriate, Mercurial will store both snapshots and
559 deltas in compressed form. It does this by always
560 <emphasis>trying to</emphasis> compress a snapshot or delta,
561 but only storing the compressed version if it's smaller than
562 the uncompressed version.</para>
564 <para id="x_325">This means that Mercurial does <quote>the right
565 thing</quote> when storing a file whose native form is
566 compressed, such as a <literal>zip</literal> archive or a JPEG
567 image. When these types of files are compressed a second
568 time, the resulting file is usually bigger than the
569 once-compressed form, and so Mercurial will store the plain
570 <literal>zip</literal> or JPEG.</para>
572 <para id="x_326">Deltas between revisions of a compressed file are usually
573 larger than snapshots of the file, and Mercurial again does
574 <quote>the right thing</quote> in these cases. It finds that
575 such a delta exceeds the threshold at which it should store a
576 complete snapshot of the file, so it stores the snapshot,
577 again saving space compared to a naive delta-only
578 approach.</para>
580 <sect3>
581 <title>Network recompression</title>
583 <para id="x_327">When storing revisions on disk, Mercurial uses the
584 <quote>deflate</quote> compression algorithm (the same one
585 used by the popular <literal>zip</literal> archive format),
586 which balances good speed with a respectable compression
587 ratio. However, when transmitting revision data over a
588 network connection, Mercurial uncompresses the compressed
589 revision data.</para>
591 <para id="x_328">If the connection is over HTTP, Mercurial recompresses
592 the entire stream of data using a compression algorithm that
593 gives a better compression ratio (the Burrows-Wheeler
594 algorithm from the widely used <literal>bzip2</literal>
595 compression package). This combination of algorithm and
596 compression of the entire stream (instead of a revision at a
597 time) substantially reduces the number of bytes to be
598 transferred, yielding better network performance over most
599 kinds of network.</para>
601 <para id="x_329">(If the connection is over <command>ssh</command>,
602 Mercurial <emphasis>doesn't</emphasis> recompress the
603 stream, because <command>ssh</command> can already do this
604 itself.)</para>
606 </sect3>
607 </sect2>
608 <sect2>
609 <title>Read/write ordering and atomicity</title>
611 <para id="x_32a">Appending to files isn't the whole story when
612 it comes to guaranteeing that a reader won't see a partial
613 write. If you recall <xref linkend="fig:concepts:metadata"/>,
614 revisions in
615 the changelog point to revisions in the manifest, and
616 revisions in the manifest point to revisions in filelogs.
617 This hierarchy is deliberate.</para>
619 <para id="x_32b">A writer starts a transaction by writing filelog and
620 manifest data, and doesn't write any changelog data until
621 those are finished. A reader starts by reading changelog
622 data, then manifest data, followed by filelog data.</para>
624 <para id="x_32c">Since the writer has always finished writing filelog and
625 manifest data before it writes to the changelog, a reader will
626 never read a pointer to a partially written manifest revision
627 from the changelog, and it will never read a pointer to a
628 partially written filelog revision from the manifest.</para>
630 </sect2>
631 <sect2>
632 <title>Concurrent access</title>
634 <para id="x_32d">The read/write ordering and atomicity guarantees mean that
635 Mercurial never needs to <emphasis>lock</emphasis> a
636 repository when it's reading data, even if the repository is
637 being written to while the read is occurring. This has a big
638 effect on scalability; you can have an arbitrary number of
639 Mercurial processes safely reading data from a repository
640 safely all at once, no matter whether it's being written to or
641 not.</para>
643 <para id="x_32e">The lockless nature of reading means that if you're
644 sharing a repository on a multi-user system, you don't need to
645 grant other local users permission to
646 <emphasis>write</emphasis> to your repository in order for
647 them to be able to clone it or pull changes from it; they only
648 need <emphasis>read</emphasis> permission. (This is
649 <emphasis>not</emphasis> a common feature among revision
650 control systems, so don't take it for granted! Most require
651 readers to be able to lock a repository to access it safely,
652 and this requires write permission on at least one directory,
653 which of course makes for all kinds of nasty and annoying
654 security and administrative problems.)</para>
656 <para id="x_32f">Mercurial uses locks to ensure that only one process can
657 write to a repository at a time (the locking mechanism is safe
658 even over filesystems that are notoriously hostile to locking,
659 such as NFS). If a repository is locked, a writer will wait
660 for a while to retry if the repository becomes unlocked, but
661 if the repository remains locked for too long, the process
662 attempting to write will time out after a while. This means
663 that your daily automated scripts won't get stuck forever and
664 pile up if a system crashes unnoticed, for example. (Yes, the
665 timeout is configurable, from zero to infinity.)</para>
667 <sect3>
668 <title>Safe dirstate access</title>
670 <para id="x_330">As with revision data, Mercurial doesn't take a lock to
671 read the dirstate file; it does acquire a lock to write it.
672 To avoid the possibility of reading a partially written copy
673 of the dirstate file, Mercurial writes to a file with a
674 unique name in the same directory as the dirstate file, then
675 renames the temporary file atomically to
676 <filename>dirstate</filename>. The file named
677 <filename>dirstate</filename> is thus guaranteed to be
678 complete, not partially written.</para>
680 </sect3>
681 </sect2>
682 <sect2>
683 <title>Avoiding seeks</title>
685 <para id="x_331">Critical to Mercurial's performance is the avoidance of
686 seeks of the disk head, since any seek is far more expensive
687 than even a comparatively large read operation.</para>
689 <para id="x_332">This is why, for example, the dirstate is stored in a
690 single file. If there were a dirstate file per directory that
691 Mercurial tracked, the disk would seek once per directory.
692 Instead, Mercurial reads the entire single dirstate file in
693 one step.</para>
695 <para id="x_333">Mercurial also uses a <quote>copy on write</quote> scheme
696 when cloning a repository on local storage. Instead of
697 copying every revlog file from the old repository into the new
698 repository, it makes a <quote>hard link</quote>, which is a
699 shorthand way to say <quote>these two names point to the same
700 file</quote>. When Mercurial is about to write to one of a
701 revlog's files, it checks to see if the number of names
702 pointing at the file is greater than one. If it is, more than
703 one repository is using the file, so Mercurial makes a new
704 copy of the file that is private to this repository.</para>
706 <para id="x_334">A few revision control developers have pointed out that
707 this idea of making a complete private copy of a file is not
708 very efficient in its use of storage. While this is true,
709 storage is cheap, and this method gives the highest
710 performance while deferring most book-keeping to the operating
711 system. An alternative scheme would most likely reduce
712 performance and increase the complexity of the software, each
713 of which is much more important to the <quote>feel</quote> of
714 day-to-day use.</para>
716 </sect2>
717 <sect2>
718 <title>Other contents of the dirstate</title>
720 <para id="x_335">Because Mercurial doesn't force you to tell it when you're
721 modifying a file, it uses the dirstate to store some extra
722 information so it can determine efficiently whether you have
723 modified a file. For each file in the working directory, it
724 stores the time that it last modified the file itself, and the
725 size of the file at that time.</para>
727 <para id="x_336">When you explicitly <command role="hg-cmd">hg
728 add</command>, <command role="hg-cmd">hg remove</command>,
729 <command role="hg-cmd">hg rename</command> or <command
730 role="hg-cmd">hg copy</command> files, Mercurial updates the
731 dirstate so that it knows what to do with those files when you
732 commit.</para>
734 <para id="x_337">When Mercurial is checking the states of files in the
735 working directory, it first checks a file's modification time.
736 If that has not changed, the file must not have been modified.
737 If the file's size has changed, the file must have been
738 modified. If the modification time has changed, but the size
739 has not, only then does Mercurial need to read the actual
740 contents of the file to see if they've changed. Storing these
741 few extra pieces of information dramatically reduces the
742 amount of data that Mercurial needs to read, which yields
743 large performance improvements compared to other revision
744 control systems.</para>
746 </sect2>
747 </sect1>
748 </chapter>
750 <!--
751 local variables:
752 sgml-parent-document: ("00book.xml" "book" "chapter")
753 end:
754 -->