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
$96.13
Apple Inc.
+0.53
MSFT
$42.86
Microsoft Corpora
-0.30
GOOG
$566.07
Google Inc.
-5.53

MacTech Search:
Community Search:

Software Updates via MacUpdate

Data Rescue 3.2.4 - Recover lost data on...
Data Rescue is a robust and reliable hard-drive recovery solution for your Mac. Recover lost or deleted files, mount corrupted drives, and more -- Data Rescue offers complete relief from crippling... Read more
Adobe Lightroom 5.6 - Import, develop, a...
Adobe Lightroom software helps you bring out the best in your photographs, whether you're perfecting one image, searching for ten, processing hundreds, or organizing thousands. Create incredible... Read more
OneNote 15.2 - Free digital notebook fro...
OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that’s too important to forget. Whether you’re at... Read more
iStat Menus 4.22 - Monitor your system r...
iStat Menus lets you monitor your system right from the menubar. Included are 8 menu extras that let you monitor every aspect of your system. Some features: CPU -- Monitor cpu usage. 7 display... Read more
Ember 1.8 - Versatile digital scrapbook....
Ember (formerly LittleSnapper) is your digital scrapbook of things that inspire you: websites, photos, apps or other things. Just drag in images that you want to keep, organize them into relevant... Read more
OmniPlan 2.3.6 - Robust project manageme...
With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more
Command-C 1.1.1 - Clipboard sharing tool...
Command-C is a revolutionary app which makes easy to share your clipboard between iOS and OS X using your local WiFi network, even if the app is not currently opened. Copy anything (text, pictures,... Read more
Knock 1.1.7 - Unlock your Mac by knockin...
Knock is a faster, safer way to sign in. You keep your iPhone with you all the time. Now you can use it as a password. You never have to open the app -- just knock on your phone twice, even when it's... Read more
Mellel 3.3.6 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
LibreOffice 4.3.0.4 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more

Latest Forum Discussions

See All

Bio Inc. is $0.99 for the Weekend, Recei...
Bio Inc. is $0.99 for the Weekend, Receives Small Update Posted by Ellis Spice on August 1st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Happy 7th Birthday Readdle! Thank You fo...
Happy 7th Birthday Readdle! | Read more »
Sharknado: The Video Game Review
Sharknado: The Video Game Review By Lee Hamlet on August 1st, 2014 Our Rating: :: SHARKNA-DON'TUniversal App - Designed for iPhone and iPad Sharknado: The Video Game brings the craziness of the movies to iOS, though it quickly... | Read more »
Clima (Weather)
Clima 1.0 Device: iOS iPhone Category: Weather Price: $.99, Version: 1.0 (iTunes) Description: Clima show you all weather information, just beautifully simple. A series of color bars can tell you at a glance exactly current... | Read more »
Sticky Soccer Review
Sticky Soccer Review By Andrew Fisher on August 1st, 2014 Our Rating: :: STICK THIS GAMEUniversal App - Designed for iPhone and iPad Sticky Soccer puts too much emphasis on the ‘sticky’ and not enough on the ‘Soccer’ or ‘Fun’.   | Read more »
Graphics-less Apocalyptic Adventure A Da...
Graphics-less Apocalyptic Adventure A Dark Room Goes Free for a Limited Time Posted by Rob Rich on August 1st, 2014 [ permalink ] | Read more »
Fraud Tycoon Review
Fraud Tycoon Review By Rob Thomas on August 1st, 2014 Our Rating: :: UNHEALTHY CREDITUniversal App - Designed for iPhone and iPad Fraud Tycoon is a half-baked, messy, promotional tie-in that does their sponsor no favors whatsoever... | Read more »
Guardians of the Galaxy: The Universal W...
Guardians of the Galaxy: The Universal Weapon is on Sale for the Weekend Posted by Rob Rich on August 1st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Mister Beam Review
Mister Beam Review By Jordan Minor on August 1st, 2014 Our Rating: :: ILLUMINATINGUniversal App - Designed for iPhone and iPad Mister Beam’s puzzles are great. But its platforming? Not so much.   | Read more »
Hook Some More Fun With MapHook’s New Up...
Hook Some More Fun With MapHook’s New Update Posted by Jessica Fisher on August 1st, 2014 [ permalink ] iPhone App - Designed for the iPhone, compatible with the iPad | Read more »

Price Scanner via MacPrices.net

13-inch MacBook Airs on sale for $100 off MSR...
B&H Photo has the new 2014 13″ MacBook Airs on sale $100 off MSRP. Shipping is free, and B&H charges NY sales tax only. They also include free copies of Parallels Desktop and LoJack for... Read more
16GB iPad Air on sale for $399, save $100
Best Buy is offering the 16GB WiFi iPad Air for $399.99 on their online store for a limited time. Their price is $100 off MSRP. Choose free shipping or free store pickup (if available). Price is for... Read more
All Over For Tablets Or Just A Maturing, Evol...
CNN’s David Goldman weighs in on tablet sector doom and gloom, asking rhetorically: “Is this the beginning of the end for the tablet?” Answering that, he contends that hysteria and panic are... Read more
Letterspace 1.0.1 – New Free iOS Text Editor...
Bangkok, Thailand based independent developer Sittipon Simasanti has released Letterspace, a new text editor for iPhone, iPad, and iPod touch devices. Letterspace is a note taking app with an... Read more
Save up to $130 on an iPad mini with Apple re...
The Apple Store has Certified Refurbished 2nd generation iPad minis with Retina Displays available for up to $130 off the cost of new models, starting at $339. Apple’s one-year warranty is included... Read more
iPad Cannibalization Threat “Overblown”
Seeking Alpha’s Kevin Greenhalgh observes that while many commentators think Apple’s forthcoming 5.5-inch panel iPhone 6 will cannibalize iPad sales, in his estimation, these concerns are being... Read more
Primate Labs Releases July 2014 MacBook Pro P...
Primate Labs’ John Poole has posted Geekbench 3 results for most of the new MacBook Pro models that Apple released on Tuesday. Poole observes that overall performance improvements for the new MacBook... Read more
Apple Re-Releases Bugfixed MacBook Air EFI Fi...
Apple has posted a bugfixed version EFI Firmware Update 2.9 a for MacBook Air (Mid 2011) models. The update addresses an issue where systems may take longer to wake from sleep than expected, and... Read more
Save $50 on the 2.5GHz Mac mini, plus free sh...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Save up to $140 on an iPad Air with Apple ref...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more

Jobs Board

Position Opening at *Apple* - Apple (United...
**Job Summary** The Apple Store is a retail environment like no other - uniquely focused on delivering amazing customer experiences. As an Expert, you introduce people Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** At the Apple Store, you connect business professionals and entrepreneurs with the tools they need in order to put Apple solutions to work in their 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
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.