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

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.