TweetFollow Us on Twitter

Changing Frameworks

Volume Number: 14 (1998)
Issue Number: 3
Column Tag: Rhapsody

Handling a Changing Framework

by Kris Jensen

What to do when the latest update to your framework no longer provides an object essential to your project

Data storage and retrieval is an important part of most computer applications. In some cases, writing a document to a file is sufficient; in others, the information to be stored and retrieved is more complex, and your program may rely on a package of objects that implement data storage and retrieval for you. What do you do if that set of objects is no longer available?

This article shows how the modularity of object-oriented programming allows one to quickly modify programs without modifying code by plugging in a different set of objects and how the high-level Foundation Kit objects can be used to quickly code interim solutions.

An OS update broke our application

Stone Design's database management application, DataPhile, suffered from an updated OS removing objects essential to the application. DataPhile was originally built using the Indexing Kit package of data storage and indexing objects, a package that was included with the original NeXTSTEP programming tools. The Indexing Kit was never problem-free at its higher levels, and, at its lower levels, was difficult to port to other platforms. As NeXTSTEP became OPENSTEP and migrated to other hardware, the Indexing Kit disappeared.

DataPhile needed a new back-end. While we were researching the possibilities for replacing or porting the Indexing Kit, we also wanted to show that we could get the front-end up and running under OpenStep and Rhapsody. The solution was a quick and dirty back-end built using OpenStep Foundation Kit objects. We did not, at this point, want to change DataPhile's data storage paradigm, nor did we want to build a functional and efficient b-tree package; we just wanted to "plug in" some quickly built objects that would provide the surface functionality that we had been using in the Indexing Kit. Efficiency, either in space or time, was not an issue; speed in implementing something that would allow us to demo our front-end under Rhapsody was our goal.

An advantage of working in an object-oriented environment is that you can use a speedily-written, temporary set of objects that provides the functionality of the objects that are no longer available or provides a general storage model that works for your application. Later, you can re-implement the objects more efficiently. Or you can use your objects as glue to implement your storage model using another set of objects. As long as you don't change your objects' interface, you don't need to touch the rest of your code.

Our solution may interest you if:

  • You're porting an application and your data storage package is not available for your new platform.
  • You're developing an application and you haven't yet decided on what you're going to use for your data storage needs.
  • You're developing a small-scale project and you need a simple, free persistent data store.

Implementing a fix with Foundation Kit

Our solution followed these steps:

  1. Identify which classes and methods in the obsolete package are being used. We used five classes: IXStore, IXStoreFile, IXStoreDirectory, IXBTree and IXBTreeCursor and we used most of the public API of those classes. Our data storage model was based on being able to store key-value pairs (with the keys and values being of any size) and to retrieve the values either directly (via the key) or sequentially.
  2. Implement those classes and methods. Don't worry about how they "really" work; our goal is to provide functionality without modifying our client code. For example, IXStore was based on the idea of "blocks" of storage; I kept the idea of blocks, but they just became id's used for access into a dictionary.
  3. To do the implementation, figure out what you need to do and look for Foundation Kit classes that will provide that functionality. We needed three things: to store key-value pairs to disk and to access those pairs directly (by key value) and sequentially. The Foundation Kit includes NSPPLs (persistent property lists). The NSPPL class description in the Rhapsody Foundation Kit documentation, states:

"Like serialization, a persistent property list stores data in a binary format, provides fast access, and is lazy. It also allows you to make incremental changes to an NSPPL (even one that contains tens of megabytes of data), while still ensuring that your data is never corrupted. In this sense, an NSPPL is analogous to a database. Because of their ability to incrementally store and retrieve data, NSPPLs are particularly well-suited for working with large amounts of data (that is, data that has several elements, that occupies a large number of bytes, or both)."

NSPPLs can store NSDictionaries and NSArrays. NSDictionaries provide fast key-value lookup, but don't allow sequential access in any kind of sorted order. NSArrays can provide sequential access. I decided to use an NSPPL to implement an IXStoreFile, an NSDictionary to implement an IXStoreDirectory, and both an NSDictionary and an NSArray to implement an IXBTree.

IXStore is "a transaction based, compacting storage allocator designed for data-intensive applications." and IXStoreFile puts the storage on disk. For a temporary back-end, I decided to forego transactions and compacting and concurrency control and just implement the calls my client code used: the transaction calls from IXStore and the initialization methods from IXStoreFile. Even though I didn't plan to implement transactions, I didn't want to remove the transaction-oriented code from DataPhile, so the methods had to be there.

IXStoreDirectory and IXBTree conform to the IXBlockAndStore protocols, which use the notion of a handle to a block of storage. Because of this, I kept the idea of block numbers allocated by IXStore, but eliminated the idea of blocks as constant-sized storage units (meaning that something stored in an IXStore might extend over multiple blocks). Instead, each IXStore client would be assigned a block number which would provide access to that client's storage. So in IXStore and IXStoreFile, I needed to map block numbers to data. Since an NSPPL incorporates an NSDictionary to provide access to its storage, I decided to use that dictionary to store block number / data pairs.

I initially planned for the stored data to be the actual structures needed by the store clients: for example, to store the IXBTree's structure (an NSDictionary of key/data pairs) directly in the root dictionary. However, all NSDictionaries stored directly in an NSPPL must use NSStrings as keys: this didn't match the IXBTree's paradigm of arbitrary data for both keys and data. So I used the rootDictionary of the NSPPL to map block numbers to NSData objects; the NSData contained an archived version of the object to be stored.

IXStoreDirectory "provides access to store clients by name instead of by block handle." This obviously called for an NSDictionary mapping names to a structure that would keep track of an object, its class, and its block number.

An IXBTree "provides ordered associative storage and retrieval of untyped data. It identifies and orders data items, called values, by key using a comparator function". The information in an IXBTree can be accessed directly by key or sequentially, by using an IXBTreeCursor to traverse the b-tree. It's a fairly standard undergraduate computer science project to write a b-tree program, but this is for a quick back-end, so I decided to use prefab parts. Direct access could be provided by an NSDictionary mapping NSData keys to NSData values. Sequential access could be provided by a sorted NSArray of keys. The sorted array could be built easily by using the sortedArrayUsingFunction NSArray method. A cursor can be represented as an integer index into the sorted key array. It can be positioned (for a key that's in the b-tree) using the method indexOfObject. If the key is not already present, positioning becomes a little more complicated; I implemented a quick and dirty binary search on the array contents. If contents are added to or deleted from the key array, any cursors pointing into the array become invalid; I use an NSNotification to notify the IXBTreeCursors that are accessing the b-tree.

Conclusions

Putting all this together, I have a cover for the parts of the Indexing Kit that we needed (b-trees, cursors, and storage) that will allow us to continue to develop the DataPhile front end while we look for a replacement back end. It's not particularly robust or efficient, but it was built very quickly using Foundation objects. When we decide on another storage package, we can rewrite the guts of the cover objects without rewriting the code that uses the cover objects. You can take a look at my b-tree solution in the example code provided at the MacTech ftp site ftp://ftp.mactech.com/.

I also found that a simple way to store and index record objects of some type is to create a b-tree that maps an id number to an NSData that holds the record (serialized in whatever way you wish). Then create index b-trees that map keys (have your records create their own keys) to the record id number. This is demonstrated in the sample code that uses the b-tree code.


Kris Jensen, at the time an attorney for the State of California, bought an Apple II in 1980. It changed her life. She taught herself BASIC, started taking computer science courses, moved to New Mexico, entered the Ph.D. program in Computer Science at the University of New Mexico, bought a Macintosh, passed her comprehensive exams, became president of the local Apple Users Group in Albuquerque, met Andrew Stone, got NeXT representatives to demonstrate the NeXT cube at a users group meeting, joined Andrew in becoming NeXT developers, and didn't finish her Ph.D. because she was designing and writing NeXTSTEP software. Kris is happy to be back in the Macintosh world with Rhapsody. In her spare time, Kris calls square dances around the country.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.