Method and system for storing and accessing data in a compound document using object linking5715441Abstract A method and system for interfacing an application program with a compound document storage system. The present invention provides an interface which an application program uses to manipulate compound documents. In a preferred embodiment, this interface is implemented in a multilayered architecture. The first layer provides methods which an application program uses to access a compound document using the functions of the second layer. The second layer maps requests to store data in the compound document to a storage format using the functions of the third layer. The third layer maps requests to write to a file to an arbitrary storage medium. Claims We claim: Description TECHNICAL FIELD
TABLE 1
______________________________________
Class IStorage
______________________________________
virtual
SCODE Release()=0;
virtual
SCODE CreateStream(lpsName, grfMode, dwIfThere, reserved,
ppstm)=0;
virtual
SCODE OpenStream(lpszName, grfMode, reserved, ppstm)=0;
virtual
SCODE CreateStorage(lpszName, grfMode, dwIfThere,
reserved, ppstg)=0;
virtual
SCODE OpenStorage(lpszName, pstgPriority, grfMode,
snbExclude, reserved, ppstg)=0;
virtual
SCODE CopyTo(pstgDest)=0;
virtual
SCODE Commit(grfCommitFlags)=0;
virtual
SCODE Revert()=0;
virtual
SCODE EnumElements(reserved1, reserved2, reserved3,
ppenun)=0;
virtual
SCODE DestroyElement(lpszName)=0;
virtual
SCODE RenameElement(lpszOldName, lpszNewName)=0;
virtual
SCODE Stat(pstatstg)=0.
}
______________________________________
IStorage::Release The Release method closes the specified IStorage instance, which makes the instance invalid. (In the following, the term "specified" refers to the instance for which the method is invoked, and the term "designated" refers to a parameter.) In a preferred embodiment, the IStorage interface includes a method that increments a reference counter. The Release method decrements the reference counter and only invalidates the specified instance when the count goes to 0. IStorage::CreateStream The CreateStream method creates a new stream of the designated name within the storage of the specified IStorage instance and opens the stream in the designated access mode. The parameter dwIfThere indicates the behavior of the method when an element of the designated name already exists. If such a element exists, the method optionally returns an error or deletes the existing element and creates a new stream. The method instantiates an IStream instance for the created stream and returns a pointer to it. IStorage::OpenStream The OpenStream method opens an existing stream of the designated name within the storage of the specified IStorage instance in the designated access mode. If a stream of the designated name does not exist within the storage, or it cannot be opened in the designated access mode, an error is returned. The access mode is discussed in detail below. The method instantiates an IStream instance for the open stream and returns a pointer to it. IStorage::CreateStorage The method CreateStorage creates a new storage of the designated name within the specified IStorage instance and opens the new storage in the designated access mode. The parameter dwIfThere indicates the behavior of the method when an element of the designated name already exists. If an element of the same name exists, the method optionally returns an error or deletes the existing element and creates a new storage. If a stream of the same name exists, then the stream is replaced by a new storage that contains one stream named "CONTENTS." This stream contains the data that was in the old stream. The method instantiates an IStorage instance (and an IStream instance, if necessary) for the created storage and returns a pointer to it. IStorage::OpenStorage The OpenStorage method opens an existing storage of the designated name within the specified Istorage instance in the designated access mode. If a storage of the designated name does not exist or it cannot be opened in the designated access mode, an error is returned. IStorage::CopyTo The method CopyTo copies the entire contents of the storage of the specified IStorage instance into the designated storage. This method replaces the contents of the designated storage. The designated storage may be a different implementation than the specified storage. Thus, the CopyTo implementation should only use the methods of the designated storage. IStorage::Commit The Commit method commits any transactions for the specified storage. The behavior of the Commit method depends upon the access mode in which the specified storage was opened. If the specified storage was opened in direct mode, then this method has no effect, with one exception. If the specified IStorage instance is a root storage, then this method acts like a traditional "flush" operation, which ensures that any internal memory buffers are written out to the underlying storage device. If the specified storage was opened in transacted mode, then this method causes any changes that have been made to the specified storage since it was opened or last committed (whichever is later) to be reflected in the parent of the specified storage. Permanent update of these changes is subject to the transaction mode of the parent storage. That is, the parent storage may be transacted, in which case a commit would need to be done on the parent storage before the changes are actually written to the storage device. This method has no effect on uncommitted transactions of the currently opened elements of specified storage. In particular, this method does not automatically commit changes within the elements of the specified storage. The action of committing in essence "publishes" the changes that occur in the specified storage to the parent storage. There are two modes for committing a root storage as designated by the parameter grfCommitFlags. In the first mode, the committed changes are written to the storage device in a robust manner. In a second mode, the data within the storage device can be overwritten with the committed changes. In the robust manner, all changes are written to unused space in the underlying storage device. The storage device is enlarged as necessary. Once this has been completed, a single write is performed on the storage device to indicate that the new data is to be used in place of the old data. The old data becomes free space that can be used during the next commit. A third commit mode indicates that the commit should be made only if the current persistent content of the storage device is the same content as that upon which the changes are based. If the content is not the same, then an error code is returned. IStorage::Revert The Revert method discards all changes that have been committed to the storage by its elements, since the specified storage was opened or last committed (whichever is later). After completion of this method, any elements that are opened for the specified storage are invalid (an error is returned by all methods of these elements except Release). IStorage::EnumElements The EnumElements method enumerates the elements immediately contained within the storage for the specified IStorage instance. The specified storage must be opened in read mode. IStorage::DestroyElement The DestroyElement method removes the element of the designated name from the specified storage instance. Any openings of the destroyed element (through the specified storage) become invalid. If the element does not exist, then an error is returned. IStorage::RenameElement The RenameElement method renames the designated element in the specified storage from the old name to the new name. If the new name already exists or if the old name does not exist, an error is returned. IStorage::Stat The method Stat returns statistical information about the storage specified Istorage instance. IStream Interface Table 3 defines the IStream interface. The parameters of the methods of the IStream interface are described in Table 4. The IStream interface defines a seek pointer that is used as the location in the stream for each read and write. The behavior of each of the methods is specified in the following.
TABLE 3
______________________________________
Class IStream
______________________________________
virtual SCODE Release()=0;
virtual SCODE Read(pv, cb pcbRead)=0;
virtual SCODE Write(pv, cb, pcbWritten)=0;
virtual SCODE Seek(dlibMove, dwOrigin, plibNewPosition)=0;
virtual SCODE SetSize(libNewSize)=0;
virtual SCODE Clone(ppstm)=0;
virtual SCODE CopyTo(ppstm, cb, pcbRead, pcbWritten)=0;
virtual SCODE Commit(grfCommitFlags)=0;
virtual SCODE Revert()=0;
virtual SCODE LockRegion(cb, dwLockType)=0;
virtual SCODE UnlockRegion(cb, dwLockType)=0;
virtual SCODE Stat(pstatstg)=0;
}
______________________________________
IStream::Release The Release method closes the stream of the specified IStream instance. In a preferred embodiment, the IStream interface includes a method that increments a reference counter. The Release method decrements the reference counter and invalidates the specified IStream instance when the count goes to 0. IStream::Read The Read method reads the designated number of bytes into the designated buffer from the stream of the specified IStream instance starting at the current seek pointer. The method returns the number of bytes actually read. The method adjusts the seek pointer by the number of bytes actually read. If the stream is not open in read mode, then an error is returned. The number of bytes actually read may be fewer than requested if the end of the stream is encountered during the read. IStream::Write The Write method writes the designated number of bytes from the designated buffer into the stream of the specified IStream instance starting at the current seek pointer. When a non-zero byte count is specified and the seek pointer is currently past the end of the stream, then the size of the stream is increased to reach the seek pointer. The added bytes are not initialized. If the stream is not open in the appropriate mode, then an error is returned. The number of bytes actually written is returned. The seek pointer is adjusted by the number of bytes actually written. IStream::Seek The Seek method adjusts the location of the seek pointer for the stream of the specified IStream instance. It is an error to seek before the beginning of the stream. It is not an error to seek past the end of the stream. The parameter dlibMove indicates the displacement from either the beginning of the stream, the current seek pointer, or the end of the stream, as designated by parameter dwOrigin. The new seek pointer is returned in parameter plibNewPosition, which may be null if the caller is not interested in the new seek pointer. IStream::SetSize The SetSize method changes the size of the stream of the specified IStream instance to be that designated by the parameter libNewSize. The seek pointer is not affected by this operation. The stream is either increased in size or decreased in size. If the stream is increased in size, the contents of the new bytes are undefined. IStream::Clone The Clone method creates a new IStream instance that points to the same underlying stream as the specified IStream instance. Changes to the stream through one IStream instance are immediately visible through the other IStream instance. The locking state (discussed below) is shared between the two IStream instances. However, the cloned IStream instance does have its own seek pointer, which is independent of the seek pointer of the specified IStream instance. The initial setting of the seek pointer in the cloned IStream instance is the same as the current setting of the seek pointer in the specified IStream instance at the time of the Clone method is invoked. IStream::CopyTo The CopyTo method copies the designated number of bytes from the stream of the specified IStream instance starting at its current seek pointer to the stream of the designated IStream instance starting at its current seek pointer. The number of bytes actually read or written is returned. The seek pointer for each IStream instance is adjusted by the number of bytes actually read or written. If the IStream instances are not open with the appropriate access modes, an error is returned. This method is semantically equivalent to a read followed by a write. IStream::Commit The Commit method behavior depends upon the mode in which the IStream instance is open. In direct mode, this method has no behavior. In transacted mode, this method causes changes that have been made to the IStream instance since it was opened or last committed, whichever is later, to be reflected in the parent storage. The designated commit mode operates in an analogous way to the description in the IStorage::Commit method. IStream::Revert The method Revert has a behavior that depends upon the mode in which the stream of the specified IStream instance is open. In direct mode, this method performs no operation. In transacted mode, all changes that have been made to the IStream instance since the IStream instance was opened or last committed, whichever is later, are discarded. IStream::LockRegion The LockRegion method attempts to lock a region of the stream of the specified IStream instance. The region begins with the current seek pointer and extends for the designated number of bytes toward the end of the stream. It is legal to indicate a region that extends past the current end of the stream. The supporting of region locking on stream is optional. This method supports two types of locking: locking to exclude other writers (write lock) and locking to exclude other writers and readers (exclusive lock). If a write lock is granted, then the designated region of the stream may be accessed by the Read method from any IStream instance of the underlying stream. However, if a Write method is invoked through any IStream instance, except the specified IStream instance and its clones, then an error is returned. If an exclusive lock is granted, then both the Read and Write methods invoked through other IStream instances will return an error. If a requested mode is not supported by an implementation, then an error message is returned. If the requested lock is supported but cannot be presently granted because of other locks, then an error is returned. Any region locked with this method must later be specifically unlocked using the UnlockRegion method with exactly the same seek pointer, the same byte count, and the same lock type. Thus, two adjacent regions cannot be locked separately and then be unlocked with the single unlock call. All locks on a stream through an IStream instance must be explicitly unlocked before the IStream instance is released. IStream::UnlockRegion The UnlockRegion method unlocks a previously locked region of the stream of the specified IStream instance. IStream::Stat The Stat method returns information about the stream of the specified IStream instance. Implementation FIG. 4 is a block diagram showing an overview of a preferred implementation of the present invention. The preferred implementation uses a layered approach comprising an IStorage/IStream Implementation 401, a Docfile Implementation 402, and an ILockBytes Implementation 403. The preferred implementation is in the C++ language, which is an object-oriented language. Consequently, the implementation is described in object-oriented terms. The IStorage/IStream Implementation 401 provides functions to create and open a document file and methods of the IStorage and IStream interfaces. The support IStorage/IStream Implementation 401 implements transaction and invokes the services of the Docfile Implementation 402. The DocFile Implementation 402 provides an interface to the persistent storage of the storages and streams within a document file. The DocFile Implementation 402 maps the storages and streams within a document file. The DocFile Implementation 402 maps the storages and streams to the underlying persistent storage using the ILockBytes interface. The ILockBytes interface is defined as an abstract class that defines methods for manipulating persistent storage. In the preferred implementation, the ILockBytes Implementation 403 uses the underlying file system to store data. Alternatively, implementation of the ILockBytes interface could use other underlying storage media, such as a data base, as indicated by Other Storage 405. IStorage/IStream Implementation The IStorage/IStream Implementation 401 uses a layered approach to organize the data and methods needed to support the IStorage and IStream interfaces. The preferred implementation organizes the IStorage and IStream instances into three layers: an exposed layer, a public layer, and a wrapped layer. FIG. 5 is a block diagram showing the IStorage and IStream layers. Each layer is implemented as an instance of a C++ class. The exposed layer 511 of the IStorage instance 510 provides the methods of the IStorage interface. The methods of the exposed layer 511 validate the parameters and invoke the methods of the public layer 512. The public layer 512 maintains a list of the storage elements, enforces the access modes, and invokes the methods of the wrapped layer when in transacted mode and invokes the methods a direct layer when in direct mode. The wrapped layer 513 implements the transaction support. The exposed layer 521 of the IStream instance 520 provides the methods of the IStream interface. The methods of the exposed layer 521 validate the parameters, maintain the seek pointer for the stream, and invoke the methods of public layer 522. The public layer 522 enforces the access modes and invokes the methods of the wrapped layer 523 when in transacted mode, and invokes the methods of a direct layer when in direct mode. The wrapped layer 523 implements the transaction support. In a preferred embodiment, the IStream interface is implemented without transaction support. That is, the IStream::Write method writes directly to the next level in the transaction hierarchy. The IStream::Commit and IStream::Revert methods, while supported, perform no operation. The layers of the IStorage and IStream implementation are described below in detail. Exposed Layer The exposed layer is the outermost layer and implements the methods of the IStorage and IStream interfaces. The exposed layer validates the parameters of the IStorage and IStream methods. In a preferred embodiment, the seek pointer for streams is not transacted. The only layer that is aware of seek pointers is the exposed layer. The exposed layer of the IStream implementation maintains the seek pointer. When the methods of the exposed layer invoke the methods of the public layers to read and write, the methods are passed the offset at which to read or write. Consequently, all internal layers are shielded from having to maintain a seek pointer. Public Layer The exposed layer points to the public layer. The public layer enforces most of the access modes for the instance. The public layer maintains a list of all its elements and their access modes. It uses this list to determine whether to allow or deny a particular access to an element. If an instance is in transacted mode, then the public layer invokes the methods of the wrapped layer, which it points to. Otherwise, the public layer invokes the direct layer. Wrapped Layer The wrapped and direct layer both support the same interface and can be used in a polymorphic manner. This allows the public layer to invoke the methods of an abstract interface and not worry about whether it is invoking the wrapped layer or the direct layer. The wrapped layer supports buffering of changes until commit or revert time. The wrapped layer communicates with the direct layer for managing its dirty information (data buffered for a transaction) and for managing its base information (data in the persistent storage). The wrapped layer also enforces certain access modes. For example, the IStorage interface specifies that the write access mode can be requested for any instance, regardless of the access mode of its parent. This means that an instance can be in write mode while its parent is in read-only access. However, the commits of the instance with write access will fail because the parent has read-only access. The direct layer invokes the methods of the DocFile Implementation directly to alter the persistent storage. Transactioning In a preferred implementation, each IStorage and IStream instance may be opened in direct or transacted mode. IStorage and IStream instances are instantiated with a wrapped layer when open in transacted mode. The wrapped layer tracks the changes made to its elements and either commits or reverts upon request. Since every IStorage and IStream instance can be opened in transacted mode, each level of the hierarchy of instances can be open in a different mode. FIG. 6 shows a typical object hierarchy. If the root IStorage instance 601 and the graph object IStorage instance 602 are open in a transacted mode, then the root IStorage instance 601 and the graph instance 602 each buffer the changes made to the stream of the XY data 603 and the stream of the title data 604. For example, if a change is made to the title data 604, then the graph object instance 602 buffers these changes in a scratch file. When the graph object storage instance commits its changes, then the root IStorage instance buffers these committed changes in a scratch file. Finally, when the root IStorage instance commits its changes, the persistent storage is only then modified. Conversely, if the graph object IStorage instance is open in direct mode, then the graph object IStorage instance does not buffer the changes of its elements. Rather, the changes are sent directly to the root IStorage, which buffers the changes. The present invention implements transactioning by maintaining a set of changes (referred to as a transaction level) for each IStorage and IStream instance. These transaction levels buffer committed changes for each element that descends from the instance and the uncommitted changes of the instance itself. A transaction level comprises a linked list of instances of transaction set members. A transaction set member buffers the changes for one element of the IStorage or IStream instance itself. Each transaction level includes a transaction set member for the IStorage or IStream instance itself to buffer the changes to the instance. Additional transaction set members are created when elements are instantiated. Transaction set members can be either direct or wrapped. A direct transaction set member does not buffer changes that have been made to it. Rather, direct transaction set member allows persistent storage to be directly manipulated. Wrapped transaction set members buffer changes and point to the data upon which the changes are based. For each element, the base data is what its parent thinks is the current state of the element. Each wrapped transaction set member contains two pointers: one pointer points to a direct member manipulating the scratch where the change information is buffered (called the dirty pointer) and the other pointer points to a directed or wrapped member which identifies the base data (called the base pointer). The base pointers of a transaction set members form a chain of transacted versions of a stream or storage. When a stream or storage is opened, its parent IStorage instance is requested to get its base pointer. If the parent does not have a transaction set member for the element, it will instantiate a transaction set member for its element. The parent IStorage instance then requests its parent to get a base pointer for the element and instantiates a transaction set member if needed. Thus, the request may percolate to the root IStorage instance which has access to the persistent file. FIG. 7 is a block diagram illustrating the transaction levels in a preferred embodiment. FIG. 6 is a sample object hierarchy diagram. In this example, the root object and the graph object are open in transacted mode. When the root storage is open in transaction mode, IStorage instance 710 and XSM instance 711 are instantiated. The XSM instance 711 buffers the changes made to the root storage. The XSM instance 711 contains a dirty pointer and a base pointer. The dirty pointer points to a stream in a scratch file where the buffered changes are stored. The base pointer points to the persistent storage. When the graph object storage is opened in transacted mode, the IStorage instance 720 and the XSM instance 721 are instantiated. When initializing the XSM instance 721, the instance requests a base pointer from its parent storage. In this example, IStorage instance 710 creates XSM instance 712 and initializes the dirty pointer to point to a scratch stream and the base pointer to point to the persistent storage. The IStorage instance 710 returns the pointer to XSM instance 712 to the instance 721, which stores the pointer as the base pointer. When the XY data stream is opened in direct mode, the XY data IStream instance 730 is instantiated. The initialization method for the IStream instance requests the parent IStorage for the pointer to its base. The graph object IStorage instance 730 creates a transaction set member for the XY data stream. The XSM instance 722 then requests its parent storage, root IStorage instance 710, for a base pointer. The root IStorage instance 710 instantiates XSM instance 710 for the XY data stream. The root IStorage instance 710 initializes the base pointer of XSM instance 713 to point to the persistent storage and returns a pointer to itself to the XSM instance 722, which stores the pointer as its base. The XSM instance 722 then returns the pointer to itself to the XY data IStream instance 730. The XY data IStream instance 730 stores that pointer as its base. FIG. 9 is a block diagram illustrating the transaction levels in a preferred embodiment. FIG. 8 is a sample object hierarchy diagram. FIGS. 7 and 9 illustrate the transaction levels when a storage is opened in transacted and direct modes, respectively. When the graph object storage is opened in direct mode, the IStorage instance 920 requests the root IStorage instance 910 for a pointer to its base. In response, the root IStorage instance 910 instantiates XSM instance 912 and sets its base to point to persistent storage. The root IStorage instance 910 returns a pointer to the graph object IStorage instance 920, which stores the pointer as its base. Similarly, when the XY data and title data streams are instantiated, they are returned pointers to XSM instances of the root IStorage 910. Since the graph object storage is not transacted, it has no corresponding transaction level and all changes made to the graph object are sent to the root storage transaction set members. FIG. 10 is a block diagram showing the various layers of an IStorage instance. Each block represents a member C++ class with member methods. An IStorage instance as discussed above comprises an exposed, public, and a wrapped layer. These layers are represented by class instances CExposedStorage 1010, CPubStorage 1020, and CWrappedStorage 1030. The exposed storage layer contains a pointer to the public storage layer and base and dirty pointers, which it would use if the instance is not in transacted mode. The public layer contains a pointer to the wrapped layer, a pointer to the parent public layer, a pointer to a link list containing the access modes of its children, and the access modes corresponding to the IStorage instance. The wrapped layer contains a pointer to a transaction level instance, a pointer to an update list, and dirty and base pointers. The transaction level instance contains a pointer to a linked list of transaction set members, a pointer indicating which transaction set member corresponds to this IStorage instance, and information indicating where to store the transacted data. The update list contains the transactions on the names of the elements of the IStorage instance. The transaction set member points to a corresponding transacted stream data structure, which stores the changes to a stream. FIG. 11 is a block diagram of C++ classes which support an IStream instance. In a preferred implementation, IStream instances are not transacted. Consequently, IStream instance has only an exposed layer 1101 and a public layer 1103. The exposed layer 1101 points to the seek pointer 1102. If an IStream instance is cloned, a new exposed layer 1105 is created along with a new seek pointer 1106. The new exposed layer 1105 points to the public layer 1103 of the cloned instance and points to the new seek pointer 1106. DocFile Implementation The Docfile Implementation maps the object hierarchy of the IStorage and IStream interfaces into the persistent storage using the ILockBytes interface. The ILockBytes interface as described below presents a view of persistent storage as an array of bytes. The DocFile Implementation logically divides the array into a header and the plurality of fixed length sectors. FIG. 12 is a diagram of the layout of the sectors in one document file. The format of this implementation is referred to as the multistream file format (MSFF). In a preferred embodiment, the header is 512 bytes. The sector size is stored in the header and is typically a multiple of 512 bytes. The sectors are typed according to the type of information each contains. The types are FAT, directory, MiniFAT, DIF, and data. The MSFF uses a file allocation table (FAT) and a doubly indirect file allocation table (DIF) to link sectors of the same type to form a logically contiguous stream. FIG. 13 is a diagram showing the allocation structures of the MSFF. This example assumes the sector size is 512 bytes and the pointers to sectors are 4 bytes. In this example, the header, 1301, points to a link list of DIF sectors 1302-1304. The DIF sectors form a linked list using links 1302A, 1303A, and 1304A. Each DIF sector contains a table 1302B, 1303B, and 1304B of pointers to FAT sectors. The number of pointers in the table depends on the sector size. As shown in FIG. 13, each DIF sector table contains 127 pointers to FAT sectors. The FAT sectors contain a logically contiguous array of FAT entries. There is one FAT entry for each sector in the persistent storage array. Each FAT sector contains 128 FAT entries. The FAT entries contain pointers that link together the sectors that form MSFF streams. An MSFF stream is a logically contiguous set of sectors of the same type. For each sector in a stream, the FAT entry corresponding to that sector contains a pointer to the next logically contiguous sector in the stream. For example, the data stream 1307-1310 contains four data sectors. Sector 1307 is stored in sector 3; sector 1308 is stored in sector 129; sector 1309 is stored in sector 140; and sector 1310 is stored in sector 2. The FAT entries link this stream together in the following way: Sinde sector 3 contains the first sector in the stream, FAT entry 3 (FAT›3!) contains 129, which is the sector that contains the second sector in the data stream. FAT entry 129 (FAT›129!) contains 140, which is the sector that contains the third sector in the data stream. FAT entry 140 (FAT›140!) contains 2, which is the sector that contains the last sector in the data stream. Since sector 2 contains the last sector in the data stream, FAT entry 2 (FAT›2!) contains an end of the data stream indicator. Once the start sector for a stream is given, the other sectors in the stream can be located by traversing the linked list in the FAT. The entries of the DIF sectors indicate where each FAT sector is actually stored. The first DIF sector contains the sector locations of the first 127 FAT sectors; the second DIF sector contains the sector location of the second 127 FAT sectors; etc. Thus, given the location of the first DIF sector, which is stored in the header, all FAT sectors can be located. The MSFF header contains a pointer to a directory stream. The directory stream contains the directory structure for the MSFF. FIG. 14 is a block diagram showing the layout of a sample directory stream. FIG. 15 is the sample object hierarchy illustrated by FIG. 14. A directory corresponds to an IStorage instance, and a data stream corresponds to an IStream instance. As shown in FIG. 15, the directory contains a root directory and sub-directories B, C, and D, and data streams B1 and B2 within sub-directory B. The MSFF preferably stores four directory entries within each directory sector. Template 1401 shows the structure of each directory entry. Each entry contains name and status information. If the entry corresponds to a data stream, it contains the start sector of that data stream. Each directory entry also contains the directory entry index of its parent, its left-most child, its previous sibling, and its next sibling. The header A02 contains the sector number of the first directory sector 1403. The directory sectors are linked by the FAT. In this example, the directory entries of the hierarchy are stored in directory sectors 1405, 1404, and 1405. The directory entries 1406-1411 each correspond to a storage stream in the hierarchy. Directory entry 0 (DIR›0!) 1406 corresponds to the root storage and indicates that its left-most child is stored in directory entry 2 (DIR›2!). Directory entry 2 (DIR›2!) 1407 contains the entry for sub-directory B. Directory entry 2 indicates that its parent is the root node (DIR›0!), it has a child stored in directory entry 4 (Dir›4!), it has no previous sibling, and its next sibling is stored in directory entry 5 (DIR›5!). Directory entry 4 (DIR›4!) 1408 corresponds to data stream B1, and contains the starting sector number (not shown) of data stream B1. Directory entry 4 indicates that its parent is stored in directory entry 2 (DIR›2!), it has no children, it has no previous sibling, and its next sibling is stored in directory entry 6 (DIR›6!). Directory entry 6 (Dir›6!) 1410 corresponds to data stream B2, and contains the starting sector number for data stream B2. Directory entry 6 indicates that its parent is stored in directory entry 2, it has no children, its previous sibling is stored in directory entry 4, and it has no next sibling. Directory entry 5 (DIR›5!) 1409 contains the entry for sub-directory C. Directory entry 5 indicates that its parent is stored in directory entry 0, it has no children, its previous sibling is stored in directory entry 2, and its next sibling is stored in directory entry 9. Directory entry 9 (DIR›9!) 1411 contains the entry for sub-directory D. Directory entry 9 indicates that its parent is stored in directory entry 0, it has no children, its previous sibling is stored in directory entry 5, and it has no next sibling. The MSFF is designed to facilitate the updating of the persistent storage in a robust manner. This robust manner helps ensure that the persistent storage will not be left in an inconsistent state. When the persistent storage is updated in a robust manner, existing data is not overwritten, rather updated sectors of information are written to free blocks. The process of writing an updated sector to a free block is referred to as "shadowing." Once all the sectors that need to be modified are shadowed, then the header is overwritten to link the new data into the directory hierarchy. The updating of the persistent storage in a robust manner tends to minimize the risk that the file will be left in an inconsistent state due to a system failure. FIG. 16 is a flow diagram showing a procedure to update a data stream in a robust manner. In step 1601, the routine writes the new data to free sectors within the file. The file is extended if there are no free sectors. In step 1602, the routine reads the FAT sectors that contain the FAT entries corresponding to the sectors containing the old and new data, modifies the FAT entries to indicate which sectors are now part of the data stream, and writes the FAT sectors to a free block. In step 1603, the routine reads each DIF sector that corresponds to a FAT sector that has been shadowed, updates the DIF sector to point to the new sector of the FAT sector, and writes the DIF sector to a free block. The routine also shadows every DIF sector that is logically prior to the one that was just shadowed. This shadowing of the DIF sectors is used to update the pointers in the linked list of DIF sectors. In step 1604, the routine determines whether the updates to the data stream result in information to the directory being changed (which it always will if the directory entry contains time of last modification). If a change is needed, the routine continues at step 1605 or else the routine continues at step 1608. In steps 1605 through steps 1607, the routine shadows the directory sector containing the directory entry corresponding to the updated data stream, shadows the FAT sectors that correspond to the old and new directory sectors, and shadows the appropriated DIF sectors. Finally, in step 1608, the routine updates the header data to point to the new shadowed sectors by modifying the pointer to the DIF. FIG. 17 is a diagram illustrating the shadowing methods of the present invention. Structure 1701 represents to storage array before being updated. The data shown in this file corresponds to the data stream example of FIG. 13. In this example, the data in sector 140 is updated. DIF sector 0 ((DIF›0!) is stored in sector 139. DIF entry 1 (DIF›1!) indicates that FAT sector 1 is stored in sector 141. FAT sector 1 contains FAT entry 129 (FAT›129!), which indicates that sector 140 contains the next sector in the data stream, and contains FAT entry 140 (FAT›140!), which indicates that sector 2 contains the next sector in the data stream. File 1702 illustrates shadowing when the data in sector 140 is updated. The shadow sectors are indicated by dash lines. The new data is written to sector 152. The FAT sector 1 from sector 141 is retrieved and FAT entry 129 is modified to point to sector 152, FAT entry 152 is modified to point to sector is 2, and FAT entry 140 is modified to indicate that sector 140 is no longer in the data stream and is free. The modified FAT sector 1 is then written to sector 153. DIF sector ›0! is retrieved and modified to indicate that FAT sector ›1! is now stored in sector 152, and written to sector 154. At this point, the persistent storage is in a consistent state because the old data is left intact and the new data has just been added to the end of the file (or stored within free sectors). Finally, a new header sector as shown in block 1703 indicating that the DIF sector 0 is now in sector 154. In a preferred embodiment, the sector size is 512 bytes. However, in many situations there may be a need to store data streams that are significantly shorter than 512 bytes. Since a sector can contain data for only one data stream, a significant portion of a persistent storage may contain "unused" space. To reduce the amount of unused space within a persistent storage, the present invention stores "small" data streams in special sectors referred to as MiniStream sectors, a stream is designated whose allocation is tracked by data stored in MiniFAT sectors. In the MiniStream, each sector is logically divided into 64-byte minisectors. The MiniFAT, which is analogous to the FAT, contains the linking information for the MiniStream. In a preferred embodiment, the header points to the first sector in the MiniFAT, and the root directory entry (Dir›0!) points to the first sector in the MiniStream. The FAT is used to link the sectors in the MiniFAT and MiniStream. In a preferred embodiment, when a data stream is smaller than 256 bytes, it is stored in the MiniStream. When data is written to the MiniStream, the directory entry in the directory stream for the data stream points to the first minisector for the data stream. Subsequent, minisectors in the data stream are linked through the MiniFAT. FIG. 18 is a block diagram showing the layout of the MiniStream and MiniFAT. In an alternate embodiment, very small data streams can be stored in the directory entry itself. If a very small data stream were to grow too large for the directory entry, the data stream would then be stored in the MiniStream. If a data stream stored in the MiniStream grew to be larger than a specified size, it would then be stored in a data stream. To optimize performance, the header information can contain the locations of the first few FAT sectors to improve performance when accessing a small persistent storage. Ilockbytes Implementation The ILockBytes Implementation 403 implements the ILockBytes interface. The ILockBytes interface as described below provides a model of the underlying storage media. In a preferred embodiment, the ILockBytes Implementation 403 operates on the underlying file system. However, other implementation could be developed to operate in other media. In the above description of the IStorage/IStream Implementation 403, multiple instances of the ILockBytes interface are used. One instance points to the scratch file and another is used to point to the actual document file. Also, an additional parameter can be added to the StgCreateDocFile and StgOpenDocFile function to allow the application program to specify an ILockBytes instance. Table 5 defines the ILockBytes interface. The parameters of the methods of the ILockBytes interface are described in Table 6. The behavior of each method is specified in the following.
TABLE 5
______________________________________
Class ILockBytes
______________________________________
virtual
SCODE ReadAt(libOffset, pv, cb, pcbRead)=0;
virtual
SCODE WriteAt(libOffset, pv, cb, pcbWritten) =0;
virtual
SCODE FIush() =0;
virtual
SCODE SetSize(cb)=0;
virtual
SCODE LockRegion(libOffset, cb, dwLockType) =0;
virtual
SCODE UnlockRegion(libOffset, cb, dwLockType)=0;
virtual
SCODE Stat(pstatstg)=0.
}
______________________________________
ILockBytes::ReadAt The ReadAt method reads the designated number of bytes into the designated buffer from the storage array of the specified ILockBytes instance starting at the designated offset. The number of bytes actually read is returned. The number of bytes actually read may be fewer than requested if the end of the storage array is encountered during the read. If the read overlaps a read locked region of the storage array, an error is returned. ILockBytes::WriteAt The WriteAt method writes the designated number of bytes from the designated buffer to storage array of the ILockBytes instance starting at the designated offset. If the offset is past the current end of the storage array, then the storage array is increased in size with uninitialized bytes. The number of bytes actually written is returned in pcbWritten. If the write overlaps a locked region, then an error is returned. ILockBytes::Flush The Flush method writes any internal buffers maintained by the implementation of the specified ILockBytes instance to the storage array. ILockBytes::SetSize The SetSize method changes the size of the storage array of the specified ILockBytes instance to the designated number of bytes. The storage array is either increased in size or decreased in size. If the storage array is increased in size, the contents of the new bytes are undefined. ILockBytes::LockRegion The LockRegion method attempts to lock a region of the storage array of the specified ILockBytes instance. The region begins with the designated offset and extends for the designated number of bytes towards the end of the storage array. It is legal to indicate a range of bytes that extends past the current end of the storage array. The locking behavior of this method is analogous to that of the IStream::LockRegion method. ILockBytes::UnlockRegion The UnlockRegion method unlocks a previously locked region of the storage array of the specified ILockBytes instance. ILockBytes::Stat The Star method returns information about the storage array of the specified ILockBytes instance. Although the present invention has been described in terms of preferred embodiments, it is not intended that the invention be limited to these embodiments. Modifications within the spirit of the invention will be apparent to those skilled in the art. The scope of the present invention is defined by the claims that follow.
|
Same subclass Same class Consider this |
||||||||||
