2. The Repository Class

DMS access works somewhat like database access. You create a DMS::Repository object and use it as a kind of "connection" to set/get files. Each repository instance represents a single connection to the repository. It manages its own distinct set of locks on behalf of the client. The DMS::Repository class takes three arguments:

The following illustrates creating a repository instance:


require 'jw/dbi'
require 'jw/dms/repository'

db = JW::DBI::Database.new()

connected = db.open( host '127.0.0.1',
                     db: 'docs.db', 
                     username: 'jujyfruit',
                     password: 'yumyum',
                     driver: 'QPSQL' )

if not connected
  puts db.error()
end

$path  = '/path/to/dms/root'
@uuid  = '4ca89a4e-e649-11dc-b1f5-0019b96bd466' 
dms    = JW::DMS::Repository.new(db, $path, uuid)

The repository UUID corresponds to the UUID of the repository identity file. Each repository must have a single file whose sole contents contain the UUID of the repository. This file is located in {dmsroot}/000/001. Furthermore, there has to be a record in the database that points to this file whose guid value matches the UUID in the respository identity file. This mechanism ensures that the database corresponds to the file store. Writing to a file store with a different database would corrupt the file store and the database, most likely beyond the capacity to accurately determine or reverse the damage. Thus, the Repository class's constructor always performs this check and will throw an exception if the UUID does not match up with the contents of the identity file. (Setting up the identity file is covered in Section 2, “Configuration”.)

The main Repository class methods are listed in Table 2.1, “Repository Methods”. The complete RDoc documentation is here.

Table 2.1. Repository Methods

NameDescription

createFile(name)

Registers a new file in the system. Takes a single argument — name which is stored as the file name in the database record. Initially, that's the only metadata used to create the file. It returns the document ID of the created file.

openFile(fileid, mode)

Opens the document with id of fileid with the access modes given by mode. Returns a Ruby File object opened on the file, or nil if the file could not be opened (e.g. requested lock could not be obtained). The exact error can be obtained from the @error member.

deleteFile(fileid)

Deletes a file from the system with the id given be fileid. This deletes both the metadata record as well as the file.

fileExists?(fileid)

Returns true if there is a file with the id given by fileid in the system, false otherwise.

fileInfo(fileid)

Returns a read-only FileAttrs object for the file with id given by fileid, which contains all of the file's metadata. It returns nil if the file does not exist.

updateFile(fileid,attrs)

Updates the metadata for the file whose id is given by fileid. The metadata is provided by the attrs object, which is a FileAttrs object.

create(path)

Creates a repository directory structure on disk given by path. This is covered in Section 2.1, “Creation via API”.