Skip to content
Browse files

add serializer to bucket hash

  • Loading branch information...
1 parent aab8009 commit dbb8c77e5048706cf640855c10f5c9ea6796661a @cloudshift committed Feb 18, 2012
Showing with 18 additions and 9 deletions.
  1. +1 −1 cloudshift/Data.hx
  2. +14 −5 cloudshift/data/Sqlite3BHash.hx
  3. +3 −3 cloudshift/data/Sqlite3Store.hx
View
2 cloudshift/Data.hx
@@ -16,7 +16,7 @@ typedef Serializer = {
interface Store {
function bucket<T>(bucketName:String,?serialize:Serializer):Outcome<String,Bucket<T>>;
- function hash<T>(bucketName:String):Outcome<String,BHash<T>>;
+ function hash<T>(bucketName:String,?serialize:Serializer):Outcome<String,BHash<T>>;
function name():String;
function lookupBucket<T>(bucketName:String):Option<Bucket<T>>;
}
View
19 cloudshift/data/Sqlite3BHash.hx
@@ -8,16 +8,25 @@ import cloudshift.externs.Sqlite3;
class Sqlite3BHash<T> implements BHash<T> {
var _db:Database;
var _table:String;
-
- public function new(store:Sqlite3Store,name:String) {
+ var _serialize:Dynamic->String;
+ var _deserialize:String->Dynamic;
+
+ public function new(store:Sqlite3Store,name:String,?serialize:Serializer) {
_db = store.db();
_table = name;
+
+ if (serialize == null)
+ serialize = Data.jsonSerializer();
+
+ _serialize = serialize.serialize;
+ _deserialize= serialize.deSerialize;
+
}
public function set(key:String,val:T):Outcome<String,T> {
var
p = Core.outcome(),
- obj = haxe.Serializer.run(val),
+ obj = _serialize(val),
ins = 'insert or replace into ';
_db.run(ins + _table + "(__hash,obj) values(?,?)",[key,obj],function(err) {
@@ -30,7 +39,7 @@ class Sqlite3BHash<T> implements BHash<T> {
public function get(key):Outcome<String,T> {
var p = Core.outcome();
_db.get("select obj from "+_table+" where __hash='"+key+"'",function(err,obj) {
- p.resolve((err != null) ? Left(err) : Right(haxe.Unserializer.run(new String(obj.obj))));
+ p.resolve((err != null) ? Left(err) : Right(_deserialize(new String(obj.obj))));
});
return p;
}
@@ -73,7 +82,7 @@ class Sqlite3BHash<T> implements BHash<T> {
p.resolve(Left(new String(err)));
return;
}
- vals.push(haxe.Unserializer.run(row.obj));
+ vals.push(_deserialize(row.obj));
},function() {
p.resolve(Right(vals));
});
View
6 cloudshift/data/Sqlite3Store.hx
@@ -20,7 +20,7 @@ class Sqlite3Store implements Store {
_name = name;
_db = new Database(name);
_buckets = new Hash();
-
+
_db.run("create table if not exists __links(ch_bkt text, ch_oid int,p_bkt text,p_oid int);",function(err) {
if (err != null)
throw "can't create __links for store:"+name;
@@ -44,15 +44,15 @@ class Sqlite3Store implements Store {
return p;
}
- public function hash<T>(bucketName:String):Outcome<String,BHash<T>> {
+ public function hash<T>(bucketName:String,?serializer:Serializer):Outcome<String,BHash<T>> {
var p = Core.outcome();
_db.run("create table if not exists " + bucketName + " (__hash text primary key unique,obj text);",function(err) {
if (err != null) {
p.resolve(Left("can't create hash bucket:"+bucketName+"->"+err));
return;
}
- var bh:BHash<T> = new cloudshift.data.Sqlite3BHash(this,bucketName);
+ var bh:BHash<T> = new cloudshift.data.Sqlite3BHash(this,bucketName,serializer);
p.resolve(Right(bh));
});
return p;

0 comments on commit dbb8c77

Please sign in to comment.
Something went wrong with that request. Please try again.