Skip to content

Commit

Permalink
save multiple objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjjwright committed Jun 6, 2010
1 parent 352172b commit e1d39ce
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 62 deletions.
15 changes: 8 additions & 7 deletions lib/nosqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
var obj_desc;
obj_desc = {
table: table,
objs: [obj]
objs: _.flatten([obj])
};
return this.save_objs(obj_desc, callback);
};
Expand Down Expand Up @@ -185,11 +185,12 @@
// to match them up with the obj_descs and objs
// we are managing (power of closures)
set_counters = function() {
j = j + 1;
if (j === obj_desc.objs.length) {
i = i + 1;
if (j === (current_obj_desc.objs.length - 1)) {
i += 1;
j = 0;
return j;
} else {
return j += 1;
}
};
_b = []; _d = obj_descs;
Expand Down Expand Up @@ -229,15 +230,15 @@
// we want the transaction error handler to be called
// so we can try to fix the error
current_obj_desc = obj_descs[i];
current_obj = current_obj_desc.objs[j];
set_counters();
errobj = self.parse_error(err.message);
if (errobj.code === self.CONSTRAINT_FAILED) {
if ((typeof (_k = current_obj_desc.ignore_constraint_errors) !== "undefined" && _k !== null) === true) {
return false;
}
}
current_err = err;
current_obj = current_obj_desc.objs[j];
set_counters();
return true;
});
})());
Expand All @@ -247,7 +248,7 @@
}
return _b;
};
return do_save(obj_descs, self.save_hooks);
return do_save(obj_descs);
}, function(err) {
return self.fix_save(current_err, current_obj_desc, current_obj, callback, save_func, save_args);
}, function(transaction) {
Expand Down
33 changes: 18 additions & 15 deletions lib/nsl_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,12 @@
};
// Stores any new objects received from another db
//
// If the obj uuid already exists in nsl_obj, ignore the obj.
// If the obj uuid already exists in nsl_obj, ignore the constraint error.
// Otherwise, store the obj in nsl_obj and in it's table.
// If the never before seen object is a cluster, store each obj
// in the cluster and store the cluster itself as an obj.
NSLSync.prototype.store_objs = function(objs, callback) {
var obj_desc, self;
// if a lot of these already exist
self = this;
obj_desc = {
table: "nsl_obj",
Expand All @@ -255,26 +254,30 @@
ignore_constraint_errors: true
};
return this.save_objs(obj_desc, function(err, res) {
var _a, _b, _c, _d, obj;
if ((typeof err !== "undefined" && err !== null)) {
return callback(err);
}
_a = []; _c = objs;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
obj = _c[_b];
_a.push(obj.tbl_name === "nsl_cluster" ? flow.serialForEach(obj.content.objs, function(uuid) {
return self.uuid_to_obj(uuid, true, this);
}, null, function() {
return callback(null, res.rowsAffected);
}) : null);
}
return _a;
return flow.serialForEach(objs, function(obj) {
if (obj.tbl_name === "nsl_cluster") {
return flow.serialForEach(obj.content.objs, function(uuid) {
return self.uuid_to_obj(uuid, true, this.MULTI());
}, null, null);
} else {
//TODO: test for parent id
return this();
}
}, null, function() {
return callback(null, res.rowsAffected);
});
});
};
// Sends any phantoms over
// Sends any objs over
NSLSync.prototype.send_objs_in_bucket = function(req_or_res, bucket, exclude_bucket, callback) {
return objs_in_bucket(bucket, function(err, objs) {
phantoms.length > 0 ? req_or_res.objs.push(objs) : null;
if (objs.length > 0) {
req_or_res.objs.push(objs);
_.flatten(req_or_res.objs);
}
return callback(null, objs.length);
});
};
Expand Down
22 changes: 12 additions & 10 deletions src/nosqlite.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ class NSLCore
# Saves an object or objects in SQLite.
# A convenience method that wraps save_objs
save: (table, obj, callback) ->

obj_desc: {
table: table
objs: [obj]
objs: _.flatten([obj])
}
@save_objs(obj_desc, callback)

Expand Down Expand Up @@ -174,16 +173,18 @@ class NSLCore
# we have to count the callbacks as they come in
# to match them up with the obj_descs and objs
# we are managing (power of closures)
set_counters: ->
j: j + 1
if j is obj_desc.objs.length
i: i + 1
set_counters: () ->
if j is (current_obj_desc.objs.length - 1)
i += 1
j: 0
else
j += 1


for obj_desc in obj_descs
if not obj_desc.objs? or not _.isArray(obj_desc.objs)
throw Error("Each obj_desc should have an objs array on it")
for obj in obj_desc.objs
for obj in obj_desc.objs
insert_sql: self.sql.insert(obj_desc.table, obj)
transaction.executeSql(
insert_sql.index_placeholder,
Expand All @@ -204,16 +205,16 @@ class NSLCore
# we want the transaction error handler to be called
# so we can try to fix the error
current_obj_desc: obj_descs[i]
current_obj: current_obj_desc.objs[j]
set_counters()
errobj: self.parse_error(err.message)
if errobj.code is self.CONSTRAINT_FAILED
if current_obj_desc.ignore_constraint_errors? is true
return false
current_err: err
current_obj: current_obj_desc.objs[j]
set_counters()
return true
)
do_save(obj_descs, self.save_hooks)
do_save(obj_descs)
(err) ->
self.fix_save(current_err, current_obj_desc, current_obj, callback, save_func, save_args)
(transaction) ->
Expand Down Expand Up @@ -242,6 +243,7 @@ class NSLCore
when @NO_SUCH_TABLE then @sql.create_table(obj_desc.table, obj, obj_desc.rowid_name).sql
when @NO_SUCH_COLUMN then @sql.add_column(obj_desc.table, errobj.column).sql
else null

if not fix_sql?
return callback(err) if callback?
else
Expand Down
37 changes: 23 additions & 14 deletions src/nsl_sync.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,11 @@ class NSLSync extends NSLCore

# Stores any new objects received from another db
#
# If the obj uuid already exists in nsl_obj, ignore the obj.
# If the obj uuid already exists in nsl_obj, ignore the constraint error.
# Otherwise, store the obj in nsl_obj and in it's table.
# If the never before seen object is a cluster, store each obj
# in the cluster and store the cluster itself as an obj.
store_objs: (objs, callback) ->
# if a lot of these already exist
self: this
obj_desc: {
table: "nsl_obj"
Expand All @@ -190,23 +189,33 @@ class NSLSync extends NSLCore
}
@save_objs obj_desc, (err, res) ->
if err? then return callback(err)
for obj in objs
if obj.tbl_name is "nsl_cluster"
flow.serialForEach(
obj.content.objs
(uuid) ->
self.uuid_to_obj(uuid, true, this)
null
->
callback(null, res.rowsAffected)
)
flow.serialForEach(
objs
(obj) ->
if obj.tbl_name is "nsl_cluster"
flow.serialForEach(
obj.content.objs
(uuid) ->
self.uuid_to_obj(uuid, true, this.MULTI())
null
null
)

else
#TODO: test for parent id
this()
null
->
callback(null, res.rowsAffected)
)


# Sends any phantoms over
# Sends any objs over
send_objs_in_bucket: (req_or_res, bucket, exclude_bucket, callback)->
objs_in_bucket bucket, (err, objs)->
if phantoms.length > 0
if objs.length > 0
req_or_res.objs.push(objs)
_.flatten(req_or_res.objs)
callback(null, objs.length)


Expand Down
10 changes: 4 additions & 6 deletions test/test_nosqlite.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test_find: ->
assert.equal(result[0].facts[2], "hello1", "should recreate arrays")
assert.equal(result[0].original.id, 1, "should recreate complex Objects")

test_sync: ->
test_sync: ->
db_file: "./test/test_sync.db"
remove_file(db_file)
db: nosqlite.open db_file, {sync_mode: true}, ->
Expand All @@ -61,7 +61,9 @@ test_sync: ->
facts: ["hello", "hello", "hello1"],
original: {id: 1, text: "some crazy object"}
}
db.save "log", log,(err, res) ->
log1: _.clone(log)
log1.text: "Hello1"
db.save "log", [log, log1], (err, res) ->
throw err if err?
db.find "log", {text: "hello"}, (err, result) ->
throw err if err?
Expand All @@ -77,10 +79,6 @@ test_sync: ->
db1: nosqlite.open peer1_db, {sync_mode: true}, ->
db1.store_objs objs, (err, num_saved) ->
sys.debug("Saved ${num_saved} objs")
sys.debug("Starting second save!!!!!!!!!!!!!")
db1.store_objs objs, (err, num_saved) ->
if err? then throw err
sys.debug("Saved ${num_saved} objs")

test_save_cd: ->
db_file: "./test/test_save_cd.db"
Expand Down
15 changes: 5 additions & 10 deletions test/test_nosqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
db = nosqlite.open(db_file, {
sync_mode: true
}, function() {
var log;
var log, log1;
log = {
text: "hello",
occurred_at: new Date().getTime(),
Expand All @@ -85,7 +85,9 @@
text: "some crazy object"
}
};
return db.save("log", log, function(err, res) {
log1 = _.clone(log);
log1.text = "Hello1";
return db.save("log", [log, log1], function(err, res) {
if ((typeof err !== "undefined" && err !== null)) {
throw err;
}
Expand Down Expand Up @@ -113,14 +115,7 @@
sync_mode: true
}, function() {
return db1.store_objs(objs, function(err, num_saved) {
sys.debug(("Saved " + (num_saved) + " objs"));
sys.debug("Starting second save!!!!!!!!!!!!!");
return db1.store_objs(objs, function(err, num_saved) {
if ((typeof err !== "undefined" && err !== null)) {
throw err;
}
return sys.debug(("Saved " + (num_saved) + " objs"));
});
return sys.debug(("Saved " + (num_saved) + " objs"));
});
});
return db1;
Expand Down

0 comments on commit e1d39ce

Please sign in to comment.