TweetFollow Us on Twitter

July 93 - NeoPersist–An Easy Way to Persistent Objects?

NeoPersist–An Easy Way to Persistent Objects?

Mary Elaine Califf

Last year, Bob Krause introduced the MADA community to NeoAccess™, NeoLogic's cross-platform object-oriented database component. At MADACON '93 Bob discovered that a number of developers were interested in the object persistence provided by NeoAccess, but did not need the full set of database features and didn't want to pay for them. The result is NeoPersist, a component which provides persistent object management for applications, simplifying object storage and retrieval. NeoPersist would be helpful to developers who want to store objects of multiple classes in a single file or who want to avoid keeping all of a document's data in memory.

What is NeoPersist?

NeoPersist is a C++ class library that provides a way to create and manage up to five classes of persistent objects with up to 64k of objects of each class. It works with MacApp and the Think Class Library. NeoPersist provides a subset of the features of its bigger sibling NeoAccess at a lower cost with no runtime fees. It uses the same interface as NeoAccess so that developers who outgrow the functionality of NeoPersist can move to NeoAccess with minimal effort.

NeoPersist provides classes which support storage and retrieval of objects, keeping track of where the objects are in the file. Objects can be retrieved from the file in groups or individually. NeoPersist also provides object-level locking. This provides for concurrency control within an application, allowing an object to be marked "busy."

HOW IT WORKS

NeoPersist provides four classes that implement object persistence. The first is CNeoFile, a subclass of TFile (under MacApp) or CDataFile (under TCL). This class encapsulates all of the file management code necessary to support persistent objects and is usually accessed via a group of routines which add and remove objects to and from a file, add classes to a file, and "sync" the file with the objects in memory. CNeoFile only updates the disk file when its sync() method is called, so "Revert" capability is easily supported. Individual objects in a file have IDs which allow them to be identified. These IDs are used by NeoPersist to index objects within their classes and thus can be used for fast retrieval of specific objects. The IDs are usually unique within a class and file, but do not have to be. Unique IDs are necessary to unambiguously identify an individual object, but multiple objects of the same class in a file may share an ID.

A second class, CNeoPersist, is the ancestor of all persistent objects. The application developer creates subclasses of CNeoPersist and overrides several of its methods, including those that provide information about the class (such as getClassID(), getFileLength() and getMetaClass()) and those that handle I/O. With MacApp, the latter involve overriding static methods ReadFrom() and WriteTo() to handle reading and writing any permanent attribute values from and to a TStream.

The search methods provided by CNeoPersist are FindByID(), which finds objects of a given class with the ID specified, FindIDRange(), which finds objects of a given class whose IDs fall within the range specified, and FindEvery(), which finds all objects of the given class:

static CNeoPersist * FindByID(CNeoFile *aFile, const NeoID aClassID,
            const NeoID aID, const Boolean aSubclass, 
            NeoTestFunc1 aFunc, void *aParam);

static CNeoPersist * FindIDRange(CNeoFile *aFile,
            const NeoID aClassID, const NeoID aMinID,
            const NeoID aMaxID, const Boolean aSubclass,
            NeoTestFunc1 aFunc, void *aParam);

static CNeoPersist * FindEvery(CNeoFile *aFile, const NeoID aClassID,
            const Boolean aSubclass, NeoTestFunc1 aFunc,
            void *aParam);

In all of these methods, aClassID specifies the base class desired; aSubclass indicates whether or not subclasses are also desired (as in a draw program where all of the TShapes in a given area should be updated whether they're TCircles or TSquares); aFunc if not nil indicates a function to be applied to each object found; and aParam can be a parameter to aFunc, a pointer to an array in which to store multiple objects, or nil. If aFunc and aParam are both nil, the methods return the first object found that meets the class and ID criteria; otherwise, all objects meeting the criteria are retrieved and either passed to the function or stored in the array.

Two other methods are provided to aid in a sequential traversal of a particular class in a file. These are getNextSibling and getPreviousSibling which return the object of the same class immediately preceding or following this object in the list. Both return nil if the object requested does not exist.

CNeoBlob is a subclass of CNeoPersist which provides for storage of variable-length data in a NeoPersist file. CNeoBlob objects have two parts on disk, an object part just like the CNeoPersist ancestor and a blob part, which can contain free-form variable-length data. The separation allows the object to be in memory while the potentially large blob stays on disk. CNeoBlob provides methods to get and set the blob's data and to mark the state of the blob (whether it has changed, whether it is in use and in a potentially inconsistent state) separately from the object part of the CNeoBlob. setBlob reads data from a handle, and getBlob returns a handle to the data. Note that the data should be in a handle.

CNeoMetaClass is used by the NeoPersist classes to keep track of information about each of the persistent classes belonging to the application. NeoPersist uses an array of metaclass objects belonging to a file. The developer must add a CNeoMetaClass object to the array for each of the application-specific persistent classes.

There are several differences in working with NeoPersist between the MacApp environment and the TCL environment. These differences show up because of the different structures of the two application frameworks. CNeoFile, CNeoPersist and CNeoBlob differ somewhat in their methods and ancestry between the two. Under MacApp, NeoPersist also provides subclasses of TApplication, TEditionDocument, and TFileHandler for developers to base their classes on. These subclasses create and manipulate CNeoFiles rather that TFiles and automatically handle a few things like CNeoDocument::DoWrite() calls sync() on the file. Under TCL such subclasses are not available, but the documentation spends several pages discussing the design of a document object which uses a CNeoFile. In this review, I will focus on the MacApp environment because the examples and documentation provided for TCL are slightly better and because I don't have Think C.

Using NeoPersist

Documentation

If I have one gripe about NeoPersist, it is with the lack of a tutorial in the documentation. The documentation that exists is easy to read, thorough, and fairly clear, with a Quick Reference section explaining what metaclasses and blobs are, how to do various operations, and how some things work in NeoPersist; a section on the application, document, and file handler classes for use with MacApp; a section on designing a document class using NeoPersist with TCL, and full descriptions of CNeoFile, CNeoPersist, CNeoBlob, and CNeoMetaClass. There are also three sample applications provided on disk, one written with MacApp and two written with TCL. The MacApp example is a rewrite of the Calc sample using NeoPersist. I found these invaluable in figuring out how to go about writing an application using NeoPersist.

However, the package would benefit from a document that walks the developer through the definition of a persistent object class, specifying which methods have to be overridden and why, and which ones can be overridden and under what circumstances one would want to override them. A section on setting up MacApp document and application classes would also be helpful, as would some advice on how application design might be affected by the use of NeoPersist. For instance, most applications have some permanent data associated with each document (e.g. Calc documents store the dimensions of the spreadsheet, the CalcMode, the allocated cells, and the edit row and column). When using NeoPersist, you'll want (need) to create a persistent object to hold the document's permanent data on disk. Then in DoRead() the document can either retrieve the object, copy its data into the document, and release the object, or retrieve the object and retain a reference to it.

The information belonging in a tutorial document is in the current documentation and examples, but it is not always immediately apparent to the novice user. This makes the learning curve feel artificially steep.

Creating an application

That said, NeoPersist is fairly easy to use once you figure it out. For the MacApp developer, the first step is to create subclasses of CNeoApplication and CNeoDocument. These are fairly straightforward. The application must override MakeNeoFile() and add the application specific persistent classes to the metaclass table. The document must override DoMakeFile() and add each of the application specific classes to the file using CNeoFile::addClass(). The document's DoRead method will need to read in any objects which should be resident in memory or are needed to track down other data, e.g. the spreadsheet object to hold Calc's document data. The document may or may not need to override DoWrite(). CNeoDocument::DoWrite() calls the file's sync method, but some applications will need to update objects in the DoWrite() method before synchronizing the file.

The interesting part of using NeoPersist is in creating the persistent object classes and manipulating the objects. Several methods must be written for each persistent object class. In order for objects to be retrieved, you must write a class method which creates and initializes an object of that type, usually called New.

CNeoPersist *CComic::New(void)
{
    CComic *    aComic;

    aComic = new CComic;
    aComic->IComic();

    return aComic;
}

You specify that class method when calling INeoMetaClass in the application's MakeNeoFile() method.

// Add CComic class to metaclass table
metaClass = new CNeoMetaClass;
metaClass->INeoMetaClass(kComicID, kNeoPersistID, "\pCComic", 
                                CComic::New, nil);

You must also override the methods getClassID(), getLength() and getFileLength(), which should return, respectively, a unique ID for the class, the length of the class in memory, and the length of the class on disk (which may differ from the in memory length). For each class you must also provide I/O methods. In TCL this involves overriding readObject and writeObject. In MacApp, you override ReadFrom() and WriteTo(), calling the inherited method and then reading or writing the permanent attributes of the object.

pascal void CComic::ReadFrom(TStream* aStream)
{
    inherited::ReadFrom(aStream);
    aStream->ReadString(fSeries, sizeof(fSeries));
    aStream->ReadBytes(&fNumber, sizeof(fNumber));
    aStream->ReadString(fPublisher, sizeof(fPublisher));
    aStream->ReadString(fCondition, sizeof(fCondition));
    aStream->ReadBytes(&fCoverPrice, sizeof(fCoverPrice));
    aStream->ReadBytes(&fPurchasePrice, sizeof(fPurchasePrice));
    aStream->ReadBytes(&fValue, sizeof(fValue));
}

pascal void CComic::WriteTo(TStream* aStream)
{
    inherited::WriteTo(aStream);
    aStream->WriteString(fSeries);
    aStream->WriteBytes(&fNumber, sizeof(fNumber));
    aStream->WriteString(fPublisher);
    aStream->WriteString(fCondition);
    aStream->WriteBytes(&fCoverPrice, sizeof(fCoverPrice));
    aStream->WriteBytes(&fPurchasePrice, sizeof(fPurchasePrice));
    aStream->WriteBytes(&fValue, sizeof(fValue));
}

Manipulating the objects is fairly straightforward. Adding objects to the file and removing them from the file are accomplished easily with the files addObject() and removeObject() methods. Updating an object is as simple as calling anObject->setDirty() and then calling the file's sync() method. To retrieve an object, you can use any of search methods provided by CNeoPersist. NeoPersist keeps track of the number of references to objects and releases an object's memory only if space is low, there are no references to the object, and the object is not dirty. Retrieving an object automatically creates a reference to the object. To create additional references to an object, you call anObject->referTo. To drop a reference, you call anObject->unrefer.

One important thing to remember in manipulating persistent objects is that adding, removing, and Find...ing an object all require a reference to the file object. This means that you could find yourself passing a reference to the file object around quite a bit.

The Bottom line

Is NeoPersist for you? That probably depends on the application. If you don't need to keep track of data from multiple object classes for a single document and you can easily keep all of your data in memory, then NeoPersist probably won't gain you anything. If on the other hand, you need to store objects from a number of different objects or you need multiple indexes, then you may want to look beyond NeoPersist to NeoAccess.

However, applications that need to store objects of a few different classes or that want to keep their objects on disk, reading them in as needed, will probably be much easier to write with NeoPersist, especially once the initial learning curve is conquered. The API is fairly simple and straightforward. And to bring up the ever-popular issue of cross platform development, NeoPersist itself is currently available for the Macintosh with both MacApp and TCL. Other platforms and/or frameworks require moving up to NeoAccess, which is a cross-platform class library .

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »

Price Scanner via MacPrices.net

Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more

Jobs Board

Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.