Skip to content

Instantly share code, notes, and snippets.

@timburks
Created December 10, 2010 05:58
Show Gist options
  • Save timburks/735843 to your computer and use it in GitHub Desktop.
Save timburks/735843 to your computer and use it in GitHub Desktop.
Tokyo Cabinet BSON store prototype
(load "NuTokyo")
(load "NuMongoDB") ;; for BSON wrapper
;; Tokyo Cabinet BSON document store example.
;; stores documents in a HashDB and builds indexes using BTreeDBs.
;; clear all files
(system "rm hashdb; rm permalinkindexdb; rm dateindexdb")
;; hashdb is used to store all objects
(set hashdb ((TokyoHashDB alloc) init))
(hashdb openWithDatabaseName:"hashdb" modes:(array "HDBOWRITER" "HDBOCREAT"))
;; we will index places by permalink and town
(set permalinkindexdb ((TokyoBTreeDB alloc) init))
(set comparatorBSON (NuBSON bsonWithList:(list permalink:1 town:1)))
(permalinkindexdb setComparator:(NuBSONComparator comparatorWithBSONSpecification:comparatorBSON))
(permalinkindexdb openWithDatabaseName:"permalinkindexdb" modes:(array "BDBOWRITER" "BDBOCREAT"))
;; we will index places by date added
(set dateindexdb ((TokyoBTreeDB alloc) init))
(set dateComparatorBSON (NuBSON bsonWithList:(list date:-1)))
(set dateComparator (NuBSONComparator comparatorWithBSONSpecification:dateComparatorBSON))
(dateindexdb setComparator:dateComparator)
(dateindexdb openWithDatabaseName:"dateindexdb" modes:(array "BDBOWRITER" "BDBOCREAT"))
;; load archived place data from a BSON file exported from MongoDB
(set placedata (NSData dataWithContentsOfFile:"places.bson"))
(set places (NuBSON bsonArrayWithData:placedata))
(places each:
(do (place)
;; add place object to main table
(set _id (place _id:))
(hashdb setData:(place dataRepresentation)
forKey:(_id dataRepresentation))
;; add place to permalink index
(set indexKey (dict town:(place town_id:) permalink:(place permalink:)))
(permalinkindexdb setData:(_id dataRepresentation)
forKey:((NuBSON bsonWithDictionary:indexKey) dataRepresentation))
;; add place to date index
(set indexKey (dict date:(place created_at:)))
(dateindexdb setData:(_id dataRepresentation)
forKey:((NuBSON bsonWithDictionary:indexKey) dataRepresentation))))
;; now use the date index to traverse places added over range of dates
(set beginKey
((NuBSON bsonWithDictionary:(dict date:(NSDate dateWithNaturalLanguageString:"May 1, 2010")))
dataRepresentation))
(set endKey
((NuBSON bsonWithDictionary:(dict date:(NSDate dateWithNaturalLanguageString:"April 1, 2010")))
dataRepresentation))
;; get object ids from the index, use them to look up actual objects
(set dataInRange (dateindexdb dataFromKey:beginKey toKey:endKey))
(dataInRange each:
(do (idData)
(set objectData (hashdb dataForKey:idData))
(set object (NuBSON bsonWithData:objectData))
(puts (((object dictionaryValue) description)))))
(dateindexdb close)
(permalinkindexdb close)
(hashdb close)
(puts "DONE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment