Created
June 17, 2013 02:56
-
-
Save ytoshima/5794386 to your computer and use it in GitHub Desktop.
Berkley-DB Java Edition examples in scala...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package persist.gettingStarted | |
import com.sleepycat.persist.model.{Entity, PrimaryKey, SecondaryKey} | |
import com.sleepycat.persist.model.Relationship._ | |
@Entity | |
class Vendor { | |
var address: String = _ | |
var bizPhoneNumber: String = _ | |
var city: String = _ | |
var repName: String = _ | |
var repPhoneNumber: String = _ | |
var state: String = _ | |
@PrimaryKey | |
var vendor: String = _ | |
var zipcode: String = _ | |
} | |
@Entity | |
class Inventory { | |
@PrimaryKey | |
var sku: String = _ | |
@SecondaryKey(relate=MANY_TO_ONE) | |
var itemName: String = _ | |
var category: String = _ | |
var vendor: String = _ | |
var vendorInventory: Int = _ | |
var vendorPrice: Float = _ | |
} | |
//---------------------------- | |
import java.io.File | |
import com.sleepycat.je.DatabaseException | |
import com.sleepycat.je.Environment | |
import com.sleepycat.je.EnvironmentConfig | |
import com.sleepycat.persist.EntityStore | |
import com.sleepycat.persist.StoreConfig | |
class MyDbEnv(val envHome: File, val readOnly: Boolean) { | |
protected val myEnvConfig = getEnvConfig | |
protected val storeConfig = getStoreConfig | |
protected def getEnvConfig = { | |
val ec = new EnvironmentConfig() | |
ec.setReadOnly(readOnly) | |
ec.setAllowCreate(!readOnly) | |
ec | |
} | |
protected def getStoreConfig = { | |
val sc = new StoreConfig() | |
sc.setReadOnly(readOnly) | |
sc.setAllowCreate(!readOnly) | |
sc | |
} | |
val myEnv: Environment = new Environment(envHome, myEnvConfig) | |
val store: EntityStore = new EntityStore(myEnv, "EntityStore", storeConfig) | |
def close() { | |
store.close() | |
myEnv.close() | |
} | |
} | |
//---------------------------- | |
import com.sleepycat.persist.PrimaryIndex | |
import com.sleepycat.persist.SecondaryIndex | |
class DataAccessor(val store: EntityStore) { | |
val inventoryBySku = store.getPrimaryIndex(classOf[String], classOf[Inventory]) | |
val inventoryByName = store.getSecondaryIndex(inventoryBySku, classOf[String], "itemName") | |
val vendorByName = store.getPrimaryIndex(classOf[String], classOf[Vendor]) | |
} | |
//---------------------------- | |
import java.io.FileInputStream | |
import java.io.InputStreamReader | |
import collection.JavaConversions._ | |
class ExampleDatabasePut(val myDbEnvPath: String, val inventoryFilePath: String, val vendorFilePath: String) { | |
/* load data and returns dbenv */ | |
def run = { | |
val dbenv = new MyDbEnv(new File(myDbEnvPath), false) | |
val da = new DataAccessor(dbenv.store) | |
import ExampleDatabasePut._ | |
println("loading vendors db...") | |
loadVendorsDb(new File(vendorFilePath), da) | |
println("loading inventory db...") | |
loadInventoryDb(new File(inventoryFilePath), da) | |
dbenv | |
} | |
} | |
object ExampleDatabasePut extends App { | |
/* | |
val myDbEnvPath0 = new File("JEDB") | |
val inventoryFile0 = new File("inventory.txt") | |
val vendorFile0 = new File("vendors.txt") | |
*/ | |
val myDbEnvPath0 = "JEDB" | |
val inventoryFile0 = "inventory.txt" | |
val vendorFile0 = "vendors.txt" | |
//private var da: DataAccessor = _ | |
//private val myDbEnv = new MyDbEnv | |
def usage { | |
val m = """ExampleDatabasePut [-h <env directory>] | |
[-i <inventory file>] | |
[-v <vendors file>]""" | |
println(m) | |
System.exit(-1) | |
} | |
protected def fixedFilenames: (String, String, String) = { | |
val largs = args.toList | |
def getOptArg(opt: String, defaultVal: String): String = { | |
require(opt != null && opt.startsWith("-")) | |
largs.dropWhile(_ != opt) match { | |
case Nil => defaultVal | |
case x::Nil => { | |
usage | |
throw new IllegalArgumentException("missing arg to " + opt) | |
} | |
case x::xs => xs.head | |
} | |
} | |
val dbep = getOptArg("-h", myDbEnvPath0) | |
val ivf = getOptArg("-v", inventoryFile0) | |
val vdf = getOptArg("-v", vendorFile0) | |
(dbep, ivf, vdf) | |
} | |
def loadVendorsDb(vf: File, da: DataAccessor) { | |
val vendors = loadFile(vf, 8) | |
vendors.map { fl => | |
val thev = new Vendor | |
thev.vendor = fl(0) | |
thev.address = fl(1) | |
thev.city = fl(2) | |
thev.state = fl(3) | |
thev.zipcode = fl(4) | |
thev.bizPhoneNumber = fl(5) | |
thev.repName = fl(6) | |
thev.repPhoneNumber = fl(7) | |
da.vendorByName.put(thev) | |
} | |
} | |
def loadInventoryDb(ivf: File, da: DataAccessor) { | |
val inventories = loadFile(ivf, 6) | |
inventories.map { fl => | |
val iv = new Inventory | |
iv.itemName = fl(0) | |
iv.sku = fl(1) | |
iv.vendorPrice = (new java.lang.Float(fl(2))).floatValue | |
iv.vendorInventory = (new java.lang.Integer(fl(3))).intValue | |
iv.category = fl(4) | |
iv.vendor = fl(5) | |
da.inventoryBySku.put(iv) | |
} | |
} | |
def loadFile(theFile: File, numFields: Int) : List[Array[String]] = { | |
import scala.io.Source | |
(for (l <- Source.fromFile(theFile).getLines()) yield { | |
val fa = l.split("#") | |
require(fa.size == numFields) | |
fa | |
}).toList | |
} | |
// main | |
val (dbep, ivf, vdf) = fixedFilenames | |
val edp = new ExampleDatabasePut(dbep, ivf, vdf) | |
val dbenv = edp.run | |
dbenv.close | |
} | |
class ExampleDatabaseRead(val myDbEnvPath: String, val locateItem: String) { | |
/* read and print data, then returns dbenv */ | |
def run = { | |
val dbenv = new MyDbEnv(new File(myDbEnvPath), true) | |
val da = new DataAccessor(dbenv.store) | |
import ExampleDatabaseRead._ | |
locateItem match { | |
case "" => showAllInventory(da) | |
case str => showItem(da, locateItem) | |
} | |
dbenv | |
} | |
} | |
object ExampleDatabaseRead extends App { | |
//val myDbEnvPath0 = new File("JEDB") | |
val myDbEnvPath0 = "JEDB" | |
def usage { | |
val m = """ExampleDatabaseRead [-h <env directory>] | |
[-s <item to locate>]""" | |
println(m) | |
System.exit(-1) | |
} | |
/** | |
* Returns String representation of an inventory record. DA is | |
* passed since it needs to load Vendor from db. | |
*/ | |
def inventoryRecordStr(iv: Inventory, da: DataAccessor): String = { | |
val tv = da.vendorByName.get(iv.vendor) | |
val vs = """Contact: %s %s %s %s %s Rep: %s %s""".format( | |
tv.address, tv.city, tv.state, tv.zipcode, tv.bizPhoneNumber, | |
tv.repName, tv.repPhoneNumber) | |
"""%s %s %s %s %d %.2f %s""".format( | |
iv.sku, iv.itemName, iv.category, iv.vendor, iv.vendorInventory, iv.vendorPrice, vs) | |
} | |
def displayInventoryRecord(iv: Inventory, da: DataAccessor) { | |
println(inventoryRecordStr(iv, da)) | |
} | |
def showItem(da: DataAccessor, locateItem: String) { | |
val items = da.inventoryByName.subIndex(locateItem).entities() | |
for (item <- items) { | |
displayInventoryRecord(item, da) | |
} | |
items.close | |
} | |
def showAllInventory(da: DataAccessor) { | |
val items = da.inventoryBySku.entities() | |
for (item <- items) { | |
displayInventoryRecord(item, da) | |
} | |
items.close | |
} | |
protected def fixedOpts: (String, String) = { | |
val largs = args.toList | |
def getOptArg(opt: String, defaultVal: String): String = { | |
require(opt != null && opt.startsWith("-")) | |
largs.dropWhile(_ != opt) match { | |
case Nil => defaultVal | |
case x::Nil => { | |
usage | |
throw new IllegalArgumentException("missing arg to " + opt) | |
} | |
case x::xs => xs.head | |
} | |
} | |
val dbep = getOptArg("-h", myDbEnvPath0) | |
val itmn = getOptArg("-s", "") | |
(dbep, itmn) | |
} | |
val (dbpath, sitem) = fixedOpts | |
val edr = new ExampleDatabaseRead(dbpath, sitem) | |
val dbenv = edr.run | |
dbenv.close | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment