Class ChangeSet::ChangeSetBuilder
In: lib/fastcst/changeset.rb
Parent: Object

Introduction

ChangeSet performs an analysis of a source and target directory and then allows you to write the changeset to two files. To apply a ChangeSet you simply use the ChangeSet#apply_changeset function. Applying a changeset is incredibly simple because of the way a changeset file is designed.

The initialize function does most of the work, but leaves the moved files detection to detect_moved_files. This is done since moved files detection is really optional, and may not be requested.

Design

A ChangeSet object performs an analysis of the source and target directory. It then writes a YAML dump of a series of Operation objects (MoveOperation, DeltaOperation, DeleteOperation, and CreateOperation) and write necessary data to a raw data output. This makes creating a changeset file incredibly easy and makes it easy to create new operations. Once all the operations are written to disk then the changeset is finished.

Operations which need to store data use a given data output stream (which writes to a file) and keep track of how much they write. When an Operation is applied it then knows how much to read from the data stream to do its job. The consequence of this is that the operations must be run in order. Each operation supports a skip method in order to allow it to be skipped.

Applying a changeset becomes incredibly easy then: just load each operation from the YAML file and call its run method. The run method knows what it needs to do, and only needs the directory to do it in and the data stream to read stuff from (if necessary).

One key thing is that, if skip is called, and the operation expects to read a certain amount of data from the data stream, then it should seek ahead that much so the next operation is not messed up.

Usage

For more information on how it is used refer to MakeChangeSetCommand#run.

Future Directions

  * It doesn't record directory change information yet.  This is coming soon.
  * No crypto yet.  That's a requirement in the future.

Weirdness

One thing that you will notice is that this class uses the UI module in ui.rb to give all it’s presentation to the user. This module is an abstraction away from the actual display method which will eventually be designed to support different UI systems depending on user preference or build options.

Methods

Attributes

changed  [R] 
common  [R] 
created  [R] 
deleted  [R] 
moved  [R] 

Public Class methods

Does the majority of the change detection using the Set class. It basically scans both source and target, and then determines the deleted, created, and common files in each by full pathname (this does not include directories yet). Then it scans the common files to see which ones have changed mtimes and assumes these are changed. Even if the files didn’t really change, they did have an attribute (mtime) change so they need to be recorded. The DeltaOperation class will figure out what really changed and record appropriately.

Moved file detection is done with detect_moved_files since this is optional.

Public Instance methods

Performs a simple moved file detection operation. A moved file is defined as:

  1.  Has same file name.
  2.  Has different base path.
  3.  Has same file size.
  4.  Has same md5 digest.

The algorithm first indexes the basenames of the @deleted and @created files and then goes through the deleted files and created files looking for basenames with only 1 element in the value array. When it finds one then it checks their sizes and md5 digests. If everything checks out then it records this as a move and removes the files from the @created and @deleted lists.

Returns true if there are detected changes.

Write journal and data to the two output streams. It basically just creates the correct operation objects in order and writes then to the journal output stream as a series of YAML documents.

[Validate]