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 .

 
AAPL
$116.47
Apple Inc.
+0.16
MSFT
$47.98
Microsoft Corpora
-0.72
GOOG
$537.50
Google Inc.
+2.67

MacTech Search:
Community Search:

Software Updates via MacUpdate

Cobook 3.0.7 - Intelligent address book....
Cobook Contacts is an intuitive, engaging address book. Solve the problem of contact management with Cobook Contacts and its simple interface and powerful syncing and integration possibilities.... Read more
StatsBar 1.9 - Monitor system processes...
StatsBar gives you a comprehensive and detailed analysis of the following areas of your Mac: CPU usage Memory usage Disk usage Network and bandwidth usage Battery power and health (MacBooks only)... Read more
Cyberduck 4.6 - FTP and SFTP browser. (F...
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Maya 2015 - Professional 3D modeling and...
Maya is an award-winning software and powerful, integrated 3D modeling, animation, visual effects, and rendering solution. Because Maya is based on an open architecture, all your work can be scripted... Read more
Evernote 6.0.1 - Create searchable notes...
Evernote allows you to easily capture information in any environment using whatever device or platform you find most convenient, and makes this information accessible and searchable at anytime, from... Read more
calibre 2.11 - Complete e-library manage...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital... Read more
Herald 5.0.1 - Notification plugin for M...
Note: Versions 2.1.3 (for OS X 10.7), 3.0.6 (for OS X 10.8), and 4.0.8 (for OS X 10.9) are no longer supported by the developer. Herald is a notification plugin for Mail.app, Apple's Mac OS X email... Read more
Firetask 3.7 - Innovative task managemen...
Firetask uniquely combines the advantages of classical priority-and-due-date-based task management with GTD. Stay focused and on top of your commitments - Firetask's "Today" view shows all relevant... Read more
TechTool Pro 7.0.6 - Hard drive and syst...
TechTool Pro is now 7, and this is the most advanced version of the acclaimed Macintosh troubleshooting utility created in its 20-year history. Micromat has redeveloped TechTool Pro 7 to be fully 64... Read more
PhotoDesk 3.0.1 - Instagram client for p...
PhotoDesk lets you view, like, comment, and download Instagram pictures/videos! (NO Uploads! / Image Posting! Instagram forbids that! AND you *need* an *existing* Instagram account). But you can do... Read more

Latest Forum Discussions

See All

Ubisoft Gives Everyone Two New Ways to E...
Ubisoft Gives Everyone Two New Ways to Earn In-Game Stuff for Far Cry 4 Posted by Jessica Fisher on November 21st, 2014 [ permalink ] | Read more »
Golfinity – Tips, Tricks, Strategies, an...
Dig this: Would you like to know what we thought of being an infinite golfer? Check out our Golfinity review! Golfinity offers unlimited ways to test your skills at golf. Here are a few ways to make sure your score doesn’t get too high and your... | Read more »
Dark Hearts, The Sequel to Haunting Meli...
Dark Hearts, The Sequel to Haunting Melissa, is Available Now Posted by Jessica Fisher on November 21st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Meowza! Toyze Brings Talking Tom to Life...
Meowza! | Read more »
Square Enix Announces New Tactical RPG f...
Square Enix Announces New Tactical RPG for Mobile, Heavenstrike Rivals. Posted by Jessica Fisher on November 21st, 2014 [ permalink ] With their epic stories and gorgeous graphics, | Read more »
Quest for Revenge (Games)
Quest for Revenge 1.0.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.0 (iTunes) Description: The great Kingdom of the west has fallen. The gods ignore the prayers of the desperate. A dark warlord has extinguished... | Read more »
Threadz is a New Writing Adventure for Y...
Threadz is a New Writing Adventure for You and Your Friends Posted by Jessica Fisher on November 21st, 2014 [ permalink ] In the tradition of round-robin storytelling, | Read more »
SteelSeries Stratus XL Hardware Review
Made by: SteelSeries Price: $59.99 Hardware/iOS Integration Rating: 4 out of 5 stars Usability Rating: 4.5 out of 5 stars Reuse Value Rating: 4.25 out of 5 stars Build Quality Rating: 4.5 out of 5 stars Overall Rating: 4.31 out of 5 stars | Read more »
ACDSee (Photography)
ACDSee 1.0.0 Device: iOS iPhone Category: Photography Price: $1.99, Version: 1.0.0 (iTunes) Description: Capture, perfect, and share your photos with ACDSee. The ACDSee iPhone app combines an innovative camera, a powerful photo... | Read more »
ProTube for YouTube (Entertainment)
ProTube for YouTube 2.0.2 Device: iOS Universal Category: Entertainment Price: $1.99, Version: 2.0.2 (iTunes) Description: ProTube is the ultimate, fully featured YouTube app. With it's highly polished design, ProTube offers ad-free... | Read more »

Price Scanner via MacPrices.net

Save up to $400 with Apple refurbished 2014 1...
The Apple Store has restocked Apple Certified Refurbished 2014 15″ Retina MacBook Pros for up to $400 off the cost of new models. An Apple one-year warranty is included with each model, and shipping... Read more
New 13-inch 1.4GHz MacBook Air on sale for $8...
 Adorama has the 2014 13″ 1.4GHz/128GB MacBook Air on sale for $899.99 including free shipping plus NY & NJ tax only. Their price is $100 off MSRP. B&H Photo has the 13″ 1.4GHz/128GB MacBook... Read more
Apple Expected to Reverse Nine-Month Tablet S...
Apple and Samsung combined accounted for 62 percent of the nearly 36 million branded tablets shipped in 3Q 2014, according to early vendor shipment share estimates from market intelligence firm ABI... Read more
Stratos: 30 Percent of US Smartphone Owners t...
Stratos, Inc., creator of the Bluetooth Connected Card Platform, has announced results from its 2014 Holiday Mobile Payments Survey. The consumer survey found that nearly one out of three (30 percent... Read more
2014 1.4GHz Mac mini on sale for $449, save $...
 B&H Photo has lowered their price on the new 1.4GHz Mac mini to $449.99 including free shipping plus NY tax only. Their price is $50 off MSRP, and it’s the lowest price available for this new... Read more
Check Apple prices on any device with the iTr...
MacPrices is proud to offer readers a free iOS app (iPhones, iPads, & iPod touch) and Android app (Google Play and Amazon App Store) called iTracx, which allows you to glance at today’s lowest... Read more
64GB iPod touch on sale for $249, save $50
Best Buy has the 64GB iPod touch on sale for $249 on their online store for a limited time. Their price is $50 off MSRP. Choose free shipping or free local store pickup (if available). Sale price for... Read more
15″ 2.2GHz Retina MacBook Pro on sale for $17...
 B&H Photo has the 2014 15″ 2.2GHz Retina MacBook Pro on sale for $1799.99 for a limited time. Shipping is free, and B&H charges NY sales tax only. B&H will also include free copies of... Read more
New Logitech AnyAngle Case/Stand Brings Flexi...
Logitec has announced the newest addition to its suite of tablet products — the Logitech AnyAngle. A protective case with an any-angle stand for iPad Air 2 and all iPad mini models, AnyAngle is the... Read more
Notebook PC Shipments Rise Year-Over-Year as...
According to preliminary results from the upcoming DisplaySearch Quarterly Mobile PC Shipment and Forecast Report, the global notebook PC market grew 10 percent year-over-year in Q3’14 to 49.4... Read more

Jobs Board

*Apple* Solutions Consultant (ASC)- Retail S...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Project Manager, *Apple* Financial Services...
**Job Summary** Apple Financial Services (AFS) offers consumers, businesses and educational institutions ways to finance Apple purchases. We work with national and Read more
*Apple* Store Leader Program - College Gradu...
Job Description: Job Summary As an Apple Store Leader Program agent, you can continue your education as you major in the art of leadership at the Apple Store. You'll Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.