处理解析事件日志的项目,然后根据这些事件的属性更新模型.我一直懒得"完成它",更关心前期优化,精益代码和适当的设计模式.主要是自学实验.我感兴趣的是更有经验的设计师认为相关的模式,或者哪种类型的伪编码对象架构是最好的,最容易维护的等等.
单个日志中可以有500,000个事件,大约有60种类型的事件,所有这些事件共享大约7个基本属性,然后根据事件类型具有0到15个其他属性.事件类型是每行日志文件中的第二个属性.
因此,我尝试了一个非常丑陋的命令式解析器,逐行遍历日志,然后逐行处理事件.然后我尝试了一个使用"nextEvent"模式的词法规范,该模式在循环中被调用并被处理.然后我尝试了一个简单的旧"解析"方法,该方法永远不会返回,只是将事件触发到已注册的侦听器回调.无论事件类型如何,我都尝试过一次回调,以及特定于每种事件类型的回调方法.
我已经尝试了一个基础"事件"类,其中包含所有可能属性的并集.我试图避免"新事件"调用(因为可能存在大量事件并且事件对象通常是短暂的)并且每种类型的回调方法都具有原始属性参数.我已经尝试为60个事件类型中的每一个创建一个子类,并使用具有7个公共基本属性的抽象事件父类.
我最近尝试进一步使用命令模式为每个事件类型放置事件处理代码.我不确定我是否喜欢这个并且它与每种类型的回调方法非常相似,只是代码在类型子类中的执行函数内,而不是每种类型的回调方法.
问题是很多模型更新逻辑是共享的,而且很多都是特定于子类的,我只是开始对整个事情感到困惑.我希望有人能够至少指出我的方向来考虑!
我的返回 json 文件如下所示:
var data = [{"col1":"value1","col2":"value1","col3":"value1"},{"col1":"value2","col2":"value2","col3":"value2"},{"col1":"value3","col2":"value3","col3":"value3"}];
Run Code Online (Sandbox Code Playgroud)
没有 JSON.stringify 数据看起来像这样:
[对象对象],[对象对象],[对象对象]
但有了它,result.length 不是 5,而是字符串的总字符数,这样我就不能循环了
var result = JSON.stringify(data);
for(i=0; i<result.length; i++){
var transaction = db.transaction([STORE], IDBTransaction.READ_WRITE);
var put = transaction.objectStore(STORE).put(result);
};
Run Code Online (Sandbox Code Playgroud) 我有这个简单的示例代码:
var request = mozIndexedDB.open('MyTestDatabase');
request.onsuccess = function(event){
var db = event.target.result;
var request = db.setVersion('1.0');
request.onsuccess = function(event){
console.log("Success version.");
if(!db.objectStoreNames.contains('customers')){
console.log("Creating objectStore");
db.createObjectStore('customers', {keyPath: 'ssn'});
}
var transaction = db.transaction([], IDBTransaction.READ_WRITE, 2000);
transaction.oncomplete = function(){
console.log("Success transaction");
var objectStore = transaction.objectStore('customers');
};
};
};
Run Code Online (Sandbox Code Playgroud)
我得到这个:
在不允许突变的数据库上尝试了突变操作。”代码:“ 6
在线
var objectStore = transaction.objectStore('customers');
Run Code Online (Sandbox Code Playgroud)
不知道-我做错了什么?
我尝试使用以下代码来测试IndexedDB的性能.代码是从http://www.html5rocks.com/en/tutorials/indexeddb/todo/修改的,它适用于chrome,但在Firefox 10中失败,说"db.setVersion不是函数".我想知道如何修改代码以使其在Firefox中运行?
var count=0;
var MAX=10;
var times=3;
var allTime;
var stime;
var etime;
var html5rocks = {};
var indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB;
if ('webkitIndexedDB' in window) {
window.IDBTransaction = window.webkitIDBTransaction;
window.IDBKeyRange = window.webkitIDBKeyRange;
}
html5rocks.indexedDB = {};
html5rocks.indexedDB.db = null;
html5rocks.indexedDB.onerror = function(e) {
//console.log(e);
alert("Why didn't you allow my web app to use IndexedDB?!");
};
html5rocks.indexedDB.open = function(type) {
var request = indexedDB.open("todos");
request.onsuccess = function(e) {
var v = "1.20";
html5rocks.indexedDB.db = …Run Code Online (Sandbox Code Playgroud) 我们正在构建一个在Firefox上广泛使用IndexedDB来存储离线数据的应用程序.
这在大多数情况下运行良好,但偶尔会因以下错误而失败:
Exception... "The operation failed because the requested database object could
not be found. For example, an object store did not exist but was being opened."
code: "3" nsresult: "0x80660003 (NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR)"
Run Code Online (Sandbox Code Playgroud)
它似乎在代码中的各个地方都失败了; 这是罪魁祸首之一:
_writePage: (storeName, startIndex, endIndex, binder) ->
writeTransaction = @connection.transaction([storeName], @idbTransaction.READ_WRITE)
store = writeTransaction.objectStore(storeName)
for index in [startIndex...endIndex] when (item = binder.list[index])?
writeRequest = store.put(item)
writeRequest.onerror = binder.failCallback()
writeRequest.onsuccess = binder.successCallback()
if endIndex >= binder.list.length
binder.finishedRegisteringCallbacks()
return
setTimeout((=> @_writePage(storeName, endIndex, endIndex + @WRITE_EACH_PAGE_SIZE, binder)), @WRITE_EACH_PAGE_DELAY)
null
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,在通常有效的自动化测试期间,故障很少发生(我们在每次数百次执行中都会看到其中一次失败). …
我有一个选择框,我想用objectStore中的值自动填充,因为我需要像这样迭代它:"从TABLE中选择COL1,其中COL2 ='myvalue'",这是我的代码:
var db;
var req = indexedDB.open("DB");
req.onsuccess = function (e) {
db = req.result;
var tran = db.transaction("store");
var singleKeyRange = IDBKeyRange.only("myvalue"); //the value I want to reach from COL2
tran.objectStore("store").openCursor(singleKeyRange).onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) {
var opt = document.getElementById("selectbox");
var option = document.createElement("option");
var optionText=document.createTextNode(cursor.value.COL1); //the matching values in COL1
option.appendChild(optionText);
opt.appendChild(option);
cursor.continue();
}
}
};
Run Code Online (Sandbox Code Playgroud)
我已经在objectStore中正确索引了所有值,现在不知道如何通过其他值达到值.
我在使用 IndexedDB 时遇到了问题。在 Firefox 18 上,当我创建一个新数据库时,onsuccess同时调用该方法具有onupgradeneeded. 在 Chrome 24 上(这是我想要的行为),该onsuccess方法仅在该onupgradeneeded方法完成后调用。
根据关于 IndexedDB 的 MDN 信息,我的印象是,当调用 onsuccess 方法时,使用数据库是安全的,但这使得它似乎不在 Firefox 中。
(function(){
app = {};
// These will hold the data for each store.
app.objectstores = [
{ name: 'UNIVERSITIES',
keyPath: 'UID',
autoIncrement: false,
data_source: 'http://mysites.dev/nddery.ca_www/larelance/data/universite.json' },
];
// Some information pertaining to the DB.
app.indexedDB = {};
app.indexedDB.db = null
app.DB_NAME = 'testdb';
app.DB_VERSION = 1;
/**
* Attempt to open the database. …Run Code Online (Sandbox Code Playgroud) 我正在开始一堆indexeddb操作,并希望它们能够增加一个计数器(并改变其他一些东西,但对于这个问题,只是假设它正在增加一个计数器)完成时.我从IndexedDB规范中知道它在不同的线程中运行回调(尽管有这样的措辞,但我不确定实现是否必须使用线程).但AFAIK,JavaScript/HTML5没有任何保证线程安全的东西,所以我担心以下情况:
/* Sequence involved in incrementing a variable "behind the scenes" */
//First callback calls i++; (it's 0 at this point)
load r0,[i] ; load memory into reg 0
//Second callback calls i++ (it's still 0 at this point)
load r1,[i] ; load memory into reg 1
//First callback's sequence continues and increments the temporary spot to 1
incr r0 ; increment reg 0
//Second callback's sequence continues and also increments the temporary spot to 1
incr r1 …Run Code Online (Sandbox Code Playgroud) 我一直在使用IndexedDB,我可以成功创建一个新数据库,创建一个商店,并在"需要升级时"添加一个值.我不明白的是,数据库是否保持"开放"状态,还是必须在需要访问数据库中读/写信息的每个函数内重新打开它?
例如,这是我的代码(可以工作)来创建一个新数据库,并插入一条记录:
$(document).ready(function() {
var db;
function initDB() {
console.log("opening db...");
var req = indexedDB.open(dbName, version);
req.onsuccess = function(event) {
db = this.result;
console.log("opening db is done");
}
req.onerror = function(event) {
console.log("error opening db: ", event.target.errorCode);
}
req.onupgradeneeded = function(event) {
db = event.target.result;
var store = db.createObjectStore("creds", { autoincrement: true });
store.add({ username:'none', password:'none'}, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
造成我混淆的是,当我需要访问该数据库中的记录,或添加更多记录或删除记录时,我想我可以创建一个新函数并插入一些值.这就是我所拥有的(失败):
function saveCreds() {
usr = $("#username").val();
psw = $("#password").val();
$.getJSON(baseURL + "cred-check/?callback=?", { username:usr, password:psw }, function(data) {
if (data.creds_good …Run Code Online (Sandbox Code Playgroud) 我正在构建一个使用IndexedBD的Web应用程序.我来自SQL背景,因此概念不同.具体来说,我的理解是indexedDB中没有"外键"的真实概念.因此,我试图找出检索相关对象的最快方法,在SQL世界中,这将是一个外键.
例如,使用SQL,假设我们有以下表:
table carBrand:
brandName
brandInitial
table carModel:
modelName
brand (FK)
Run Code Online (Sandbox Code Playgroud)
现在,如果我们有一个记录,carBrand其中保持brandName=Ford,brandInitial='F'然后一个记录,carModel其中保存数据modelName = Mustang和brand = (FK)Ford.
要在SQL世界中显示brandInitial此Mustang记录,我可以使用点"." 符号.简单地说,myCarInstance.brand.brandInitial哪个会显示F.我没有将其存储到brandInitial在carModel记录以来的指针外键保存数据.
现在转移到indexedDB,将设置相同的方案,如下所示:
objectStore "carBrand"
objectStore "carModel"
Run Code Online (Sandbox Code Playgroud)
为了获得相同的记录(福特野马)我的理解是我们会做两个.add电话.首先,我们补充说brandName: "Ford".接下来(以及我对"最佳实践"的理解并不完整)添加"福特野马",我们会在carModel商店中添加一条记录,其中包含以下数据:
modelName: "Mustang"
brand: "Ford"
Run Code Online (Sandbox Code Playgroud)
因此,设置此数据后,是否有"快速"方式来检索记录brandInital?我是否必须打开一个交易只是为了检索F品牌的首字母,还是我应该使用其他方法?
indexeddb ×9
javascript ×6
html5 ×4
asynchronous ×2
firefox ×1
html ×1
java ×1
jquery ×1
json ×1
logging ×1