forked from airportyh/browsercouch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser-couch.js
1345 lines (1176 loc) · 39.8 KB
/
browser-couch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Ubiquity.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Atul Varma <[email protected]>
* Peter Braden <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// = BrowserCouch =
//
// BrowserCouch is a client side map-reduce data store, inspired by CouchDB. It
// utilizes the browser's local storage where possible, and syncs to a CouchDB
// server.
//
var BrowserCouch = function(opts){
var bc = {};
// == Utility Functions ==
// === {{{isArray()}}} ===
//
// A helper function to determine whether an object is an Array or
// not. Taken from jQuery
var isArray = function(value) {
return Object.prototype.toString.call(value) === "[object Array]";
}
function keys(obj){
var ret = []
for (var key in obj)
ret.push(key)
return ret
}
function values(obj){
var ret = []
for (var key in obj)
ret.push(obj[key])
return ret
}
var Couch = function Couch(options){
if (options.url)
this.baseUrl = options.url
else{
this.name = options.name
this.host = options.host || 'localhost'
this.port = options.port || 5984
this.baseUrl = 'http://' + this.host + ':' + this.port + '/' + this.name + '/'
}
}
Couch.prototype = {
get: function(id, params, callback, context){
var qs = this.qs(params)
this.request('GET', id + qs, params, callback, context)
},
post: function(id, doc, callback, context){
this.request('POST', id, JSON.stringify(doc), callback, context)
},
put: function(id, doc, callback, context){
this.request('PUT', id, JSON.stringify(doc), callback, context)
},
del: function(doc, callback, context){
this.request('DELETE', doc._id + '?rev=' + doc._rev, null, callback, context)
},
view: function(viewPath, params, callback, context){
this.get(this.expandViewPath(viewPath, params), callback, context)
},
drop: function(callback, context){
this.request('DELETE', '', null, callback, context)
},
create: function(callback, context){
this.request('PUT', '', null, callback, context)
},
encodeValue: function(value){
if (value && value.constructor === Array || value.constructor === Object)
return encodeURI(JSON.stringify(value))
else return encodeURI(value)
},
qs: function(params){
var encodeValue = this.encodeValue
if (!params) return ''
return '?' + keys(params).map(function(key){
return key + '=' + encodeValue(params[key])
}).join('&')
},
expandViewPath: function expandViewPath(viewPath, params){
var parts = viewPath.split('/')
var viewPath = '_design/' + parts[0] + '/_view/' + parts[1]
viewPath += this.qs(params)
//sys.debug('viewPath: ' + viewPath)
return viewPath
},
request: function request(verb, uri, data, callback, context){
function _callback(){
if (this.readyState == 4){
var result
try{
result = JSON.parse(this.responseText)
}catch(e){
result = null
}
callback.call(context, result, this.status)
}else if(this.readyState == 1){
this.setRequestHeader('Accept', 'application/json')
this.setRequestHeader('Content-Type', 'application/json')
}
}
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = callback ? _callback : null
var url = this.baseUrl + uri
//console.log(verb + ': ' + url)
try{
xhr.open(verb, url, true)
xhr.send(data)
}catch(e){
callback.call(context, null, null)
}
}
}
window.Couch = Couch
// == MapReducer Implementations ==
//
// //MapReducer// is a generic interface for any map-reduce
// implementation. Any object implementing this interface will need
// to be able to work asynchronously, passing back control to the
// client at a given interval, so that the client has the ability to
// pause/cancel or report progress on the calculation if needed.
// === {{{WebWorkerMapReducer}}} ===
//
// A MapReducer that uses
// [[https://developer.mozilla.org/En/Using_DOM_workers|Web Workers]]
// for its implementation, allowing the client to take advantage of
// multiple processor cores and potentially decouple the map-reduce
// calculation from the user interface.
//
// The script run by spawned Web Workers is
// [[#js/worker-map-reducer.js|worker-map-reducer.js]].
bc.WebWorkerMapReducer = function WebWorkerMapReducer(numWorkers, Worker) {
if (!Worker){
Worker = window.Worker;
}
var pool = [];
function MapWorker(id) {
var worker = new Worker('js/worker-map-reducer.js');
var onDone;
worker.onmessage = function(event) {
onDone(event.data);
};
this.id = id;
this.map = function MW_map(map, dict, cb) {
onDone = cb;
worker.postMessage({map: map.toString(), dict: dict});
};
}
for (var i = 0; i < numWorkers; i++){
pool.push(new MapWorker(i));
}
this.map = function WWMR_map(map, dict, progress, chunkSize, finished) {
var keys = dict.getKeys();
var size = keys.length;
var workersDone = 0;
var mapDict = {};
function getNextChunk() {
if (keys.length) {
var chunkKeys = keys.slice(0, chunkSize);
keys = keys.slice(chunkSize);
var chunk = {};
for (var i = 0; i < chunkKeys.length; i++){
chunk[chunkKeys[i]] = dict.get(chunkKeys[i]);
}
return chunk;
} else {
return null;
}
}
function nextJob(mapWorker) {
var chunk = getNextChunk();
if (chunk) {
mapWorker.map(
map,
chunk,
function jobDone(aMapDict) {
for (var name in aMapDict){
if (name in mapDict) {
var item = mapDict[name];
item.keys = item.keys.concat(aMapDict[name].keys);
item.values = item.values.concat(aMapDict[name].values);
} else{
mapDict[name] = aMapDict[name];
}
}
if (keys.length){
progress("map",
(size - keys.length) / size,
function() { nextJob(mapWorker); });
}else{
workerDone();
}
});
} else{
workerDone();
}
}
function workerDone() {
workersDone += 1;
if (workersDone == numWorkers){
allWorkersDone();
}
}
function allWorkersDone() {
var mapKeys = [];
for (var name in mapDict){
mapKeys.push(name);
}
mapKeys.sort();
finished({dict: mapDict, keys: mapKeys});
}
for (var i = 0; i < numWorkers; i++){
nextJob(pool[i]);
}
};
// TODO: Actually implement our own reduce() method here instead
// of delegating to the single-threaded version.
this.reduce = bc.SingleThreadedMapReducer.reduce;
};
// === {{{SingleThreadedMapReducer}}} ===
//
// A MapReducer that works on the current thread.
bc.SingleThreadedMapReducer = {
map: function STMR_map(map, storage, docPrefix, progress,
chunkSize, finished) {
storage.keys(docPrefix, function(keys){
var mapDict = {};
var currDoc;
function emit(key, value) {
// TODO: This assumes that the key will always be
// an indexable value. We may have to hash the value,
// though, if it's e.g. an Object.
var item = mapDict[key];
if (!item){
item = mapDict[key] = {keys: [], values: []};
}
item.keys.push(currDoc._id);
item.values.push(value);
}
var i = 0;
function continueMap() {
var iAtStart = i;
do {
storage.get(docPrefix + keys[i], function(d){
currDoc=d;
map(d, emit)
});
i++;
} while (i - iAtStart < chunkSize &&
i < keys.length);
if (i >= keys.length) {
var mapKeys = [];
for (name in mapDict)
mapKeys.push(name);
mapKeys.sort();
finished({dict: mapDict, keys: mapKeys});
} else
progress("map", i / keys.length, continueMap);
}
continueMap();
});
},
reduce: function STMR_reduce(reduce, mapResult, progress,
chunkSize, finished) {
var rows = [];
var mapDict = mapResult.dict;
var mapKeys = mapResult.keys;
var i = 0;
function continueReduce() {
var iAtStart = i;
do {
var key = mapKeys[i];
var item = mapDict[key];
var keys = [];
for (var j = 0; j < keys.length; j++)
newKeys.push([key, item.keys[j]]);
rows.push({key: key,
value: reduce(keys, item.values)});
i++;
} while (i - iAtStart < chunkSize &&
i < mapKeys.length)
if (i == mapKeys.length)
finished(rows);
else
progress("reduce", i / mapKeys.length, continueReduce);
}
continueReduce();
}
};
// == View ==
bc._View = function BC__View(rows) {
this.rows = rows;
function findRow(key, rows) {
if (rows.length > 1) {
var midpoint = Math.floor(rows.length / 2);
var row = rows[midpoint];
if (key < row.key)
return findRow(key, rows.slice(0, midpoint));
if (key > row.key)
return midpoint + findRow(key, rows.slice(midpoint));
return midpoint;
} else
return 0;
}
this.findRow = function V_findRow(key) {
return findRow(key, rows);
};
},
// == MapView ==
bc._MapView = function BC__MapView(mapResult) {
var rows = [];
var keyRows = [];
var mapKeys = mapResult.keys;
var mapDict = mapResult.dict;
for (var i = 0; i < mapKeys.length; i++) {
var key = mapKeys[i];
var item = mapDict[key];
keyRows.push({key: key, pos: rows.length});
var newRows = [];
for (var j = 0; j < item.keys.length; j++) {
var id = item.keys[j];
var value = item.values[j];
newRows.push({_id: id,
key: key,
value: value});
}
newRows.sort(function(a, b) {
if (a._id < b._id)
return -1;
if (a._id > b._id)
return 1;
return 0;
});
rows = rows.concat(newRows);
}
function findRow(key, keyRows) {
if (keyRows.length > 1) {
var midpoint = Math.floor(keyRows.length / 2);
var keyRow = keyRows[midpoint];
if (key < keyRow.key)
return findRow(key, keyRows.slice(0, midpoint));
if (key > keyRow.key)
return findRow(key, keyRows.slice(midpoint));
return keyRow.pos;
} else
return keyRows[0].pos;
}
this.rows = rows;
this.findRow = function MV_findRow(key) {
return findRow(key, keyRows);
};
}
// == Storage Implementations ==
//
// //Storage// is a generic interface for a persistent storage
// implementation capable of storing JSON-able objects.
// === {{{FakeStorage}}} ===
//
// This Storage implementation isn't actually persistent; it's just
// a placeholder that can be used for testing purposes, or when no
// persistent storage mechanisms are available.
bc.FakeStorage = function FakeStorage() {
var db = {};
function deepCopy(obj) {
if (typeof(obj) == "object") {
var copy;
if (isArray(obj))
copy = new Array();
else
copy = new Object();
for (name in obj) {
if (obj.hasOwnProperty(name)) {
var property = obj[name];
if (typeof(property) == "object")
copy[name] = deepCopy(property);
else
copy[name] = property;
}
}
return copy;
} else
return obj;
}
this.get = function FS_get(name, cb) {
if (!(name in db))
cb(null);
else
cb(db[name]);
};
this.put = function FS_put(name, obj, cb) {
db[name] = deepCopy(obj);
cb();
};
this.remove = function(name, cb){
delete db[name];
if(cb){cb();}
}
this.keys = function(prefix, cb){
var out = [];
for (var i in db){
if (i.slice(0, prefix.length)===prefix){
out.push(i);
}
}
cb(out);
}
};
// === {{{LocalStorage}}} ===
//
// This Storage implementation uses the browser's HTML5 support for
// {{{localStorage}}} or {{{globalStorage}}} for object persistence.
//
// Each database is stored in a key, as a JSON encoded string. In
// future we may want to rethink this as it's horribly innefficient
bc.LocalStorage = function LocalStorage() {
var storage;
if (window.localStorage)
storage = window.localStorage;
else {
throw new Error("globalStorage/localStorage not available.");
}
this.get = function LS_get(name) {
if (name in storage && storage[name])
try{
return JSON.parse(storage[name])
}catch(e){
throw new Error("Error trying to parse JSON: " + storage[name] + " at " + name)
}
else
return null;
};
this.put = function LS_put(name, obj) {
storage[name] = JSON.stringify(obj);
};
this.remove = function(name){
delete storage[name];
}
this.keys = function(prefix){
var out = [];
for (var i = 0; i < storage.length; i++){
var key = storage.key(i);
if (key.slice(0, prefix.length)===prefix){
out.push(key.slice(prefix.length, key.length));
}
}
return out;
}
}
bc.GlobalStorage = function GlobalStorage() {
var storage
if (window.globalStorage){
storage = window.globalStorage[location.hostname];
}else
throw new Error("globalStorage/localStorage not available.");
this.get = function LS_get(name) {
if (name in storage && storage[name])
return JSON.parse(storage[name].value)
else
return null;
};
this.put = function LS_put(name, obj) {
storage[name] = JSON.stringify(obj);
};
this.remove = function(name){
delete storage[name];
}
this.keys = function(prefix){
var out = [];
for (var i = 0; i < storage.length; i++){
var key = storage.key(i);
if (key.slice(0, prefix.length)===prefix){
out.push(key.slice(prefix.length, key.length));
}
}
return out;
}
}
bc.StorageCache = function StorageCache(storage){
var cache = {}
this.get = function(name){
if (name in cache)
return cache[name]
else{
var ret = cache[name] = storage.get(name)
return ret
}
}
this.put = function(name, obj){
storage.put(name, obj)
if (name in cache)
cache[name] = storage.get(name)
}
this.remove = function(name){
delete cache[name]
storage.remove(name)
}
this.keys = function(prefix){
return storage.keys(prefix)
}
}
// == Database Wrapper Interface ==
//
// A basic database interface. Implementing objects
// should support methods that emulate the basic REST commands
// that CouchDB uses.
//
// === Local Storage Database ===
// TODO, rename this
bc.BrowserDatabase = function(name, storage, options) {
var self = {},
dbName = 'BC_DB_' + name,
seqPrefix = dbName + '__seq_',
docPrefix = dbName + '_doc_',
dbInfo = storage.get(dbName) || {lastSeq: 0, docCount: 0},
changeListeners = [],
browserID = initBrowserID(),
isRemoteSyncing = false
self.name = name;
function initBrowserID(){
var browserID = storage.get('BC_BROWSER_ID')
if (!browserID){
browserID = new UUID().createUUID()
storage.put('BC_BROWSER_ID', browserID)
}
return browserID
}
function shallowCopy(obj){
var ret = {}
for (var key in obj){
if (obj.hasOwnProperty(key))
ret[key] = obj[key]
}
return ret
}
self.get = function DB_get(id, options) {
options = options || {}
var docInfo = storage.get(docPrefix + id)
var doc
if (docInfo){
if (options.rev){
if (docInfo._conflict_revisions){
doc = docInfo._conflict_revisions[options.rev]
}
}else{
doc = shallowCopy(docInfo.doc)
if (docInfo._conflict_revisions){
var confs = keys(docInfo._conflict_revisions)
if (confs.length > 0)
doc._conflicts = confs
}if (options.revs === true){
doc._revisions = docInfo.revisions
}
}
if (!doc || doc._deleted) return null
else return doc
}else
return null
};
// === {{{PUT}}} ===
//
// This method is vaguely isomorphic to a
// [[http://wiki.apache.org/couchdb/HTTP_Document_API#PUT|HTTP PUT]] to a
// url with the specified {{{id}}}.
//
// It creates or updates a document
self.put = function DB_put(document, options) {
options = options || {};
var newEdits = 'new_edits' in options ? options.new_edits: true;
var self = this;
var putObj = function(obj){
function newHash(){
return (Math.random()*Math.pow(10,20));
}
function revIndex(doc){
return parseInt(doc._rev.split('-')[0])
}
function revHash(doc){
return doc._rev.split('-')[1]
}
function calculateRevId(doc){
var deleted = doc._deleted || false
var oldRev = doc._rev
var oldId = oldRev ? Number(oldRev.split('-')[0]) : 0
var oldHash = oldRev ? oldRev.split('-')[1] : 0
var attrs2 = []
var attrs = []
for (var key in doc){
if (doc.hasOwnProperty(key)){
if (key.charCodeAt(0) != 95){ // skip attrs that start w underscore
var value = doc[key]
if (value === undefined) continue
if (typeof(value) == 'string')
value = new BertBinary(value)
attrs.push(new BertTuple([new BertBinary(key), value]))
}
}
}
var body = new BertTuple([attrs])
var term = [
deleted, oldId, oldHash, body, attrs2
]
//console.log(Bert.pp_term(term))
return MD5(Bert.encode(term))
}
if (!obj._id)
throw new Error("Cannot put w/o ID.")
var docInfo = storage.get(docPrefix + obj._id) || {}
var orig = docInfo.doc
if (newEdits && orig && orig._rev != obj._rev && (
!docInfo._conflict_revisions || !(obj._rev in docInfo._conflict_revisions)
)){
//console.log('original: ' + JSON.stringify(orig));
//console.log('new: ' + JSON.stringify(obj));
throw new Error('Document update conflict for ID ' + obj._id + ', revs (' + obj._rev + ', ' + orig._rev + ').');
}else{
//= Update Rev =
if (!obj._rev){
// We're using the naive random versioning, rather
// than the md5 deterministic hash.
obj._rev = "1-" + calculateRevId(obj);
}else{
if (newEdits){
if (obj._rev != orig._rev){
// conflict management
if (obj._deleted){
// if deleting a conflict revision, we delete only that revision
delete docInfo._conflict_revisions[obj._rev];
// promote original back as current doc
obj = orig;
}else{
var conflictRevisions = docInfo._conflict_revisions;
var confDoc = conflictRevisions[obj._rev];
delete conflictRevisions[obj._rev];
conflictRevisions[orig._rev] = orig;
}
}
var revId = calculateRevId(obj);
if (revHash(obj) == revId){
// no change, skip
return
}
obj._rev = (revIndex(obj)+1) + '-' + revId;
}else if (orig && obj._rev != orig._rev){
function hasMatchingRev(rev, revisions){
for (var i = 0; i < revisions.ids.length; i++){
if (rev == (revisions.start - i) + '-' + revisions.ids[i])
return true
}
return false
}
if (hasMatchingRev(obj._rev, docInfo.revisions)){
// If we already have this rev, do nothing
return
}
var revisions = obj._revisions
if (!revisions || !hasMatchingRev(orig._rev, revisions)){
var winner;
// use deterministic winner picking algorithm
if (revIndex(obj) > revIndex(orig))
winner = obj
else if (revIndex(orig) > revIndex(obj))
winner = orig
else if (revHash(obj) > revHash(orig))
winner = obj
else
winner = orig
var loser = obj === winner ? orig : obj;
obj = winner
if (!docInfo._conflict_revisions)
docInfo._conflict_revisions = {}
docInfo._conflict_revisions[loser._rev] = loser
}
}
}
if (!orig && !obj._deleted){
dbInfo.docCount = self.docCount() + 1;
storage.put(dbName, dbInfo)
}
if (obj._deleted){
if (!orig) return; // forget about it
docInfo._revWhenDeleted = orig._rev;
dbInfo.docCount = self.docCount() - 1;
storage.put(dbName, dbInfo)
}
var doc = shallowCopy(obj)
// filter out special attributes
for (var key in doc){
if (key == '_conflicts' || key == '_revisions')
delete doc[key]
}
docInfo.doc = doc
if (!docInfo.revisions)
docInfo.revisions = {ids: []}
docInfo.revisions.start = revIndex(obj)
var rh = revHash(obj)
if (docInfo.revisions.ids.indexOf(rh) == -1)
docInfo.revisions.ids.splice(0, 0, rh)
if ('seq' in docInfo)
storage.remove(seqPrefix + docInfo.seq)
var seq = self.lastSeq() + 1;
docInfo.seq = seq
storage.put(docPrefix + obj._id, docInfo);
storage.put(seqPrefix + seq, obj._id);
dbInfo.lastSeq = seq;
storage.put(dbName, dbInfo)
// notify change listeners
for (var i = 0; i < changeListeners.length; i++){
var listener = changeListeners[i]
listener(obj)
}
}
}
if (isArray(document)) {
for (var i = 0; i < document.length; i++){
putObj(document[i]);
}
} else{
putObj(document);
}
};
// === {{{POST}}} ===
//
// Roughly isomorphic to the two POST options
// available in the REST interface. If an ID is present,
// then the functionality is the same as a PUT operation,
// however if there is no ID, then one will be created.
//
self.post =function(data, options){
if (!data._id)
data._id = new UUID().createUUID();
this.put(data, options);
}
// === {{{DELETE}}} ===
//
// Delete the document.
self.del = function(doc, options){
this.put({_id : doc._id, _rev : doc._rev, _deleted : true});
}
//
self.docCount = function DB_docCount() {
return dbInfo.docCount;
};
self.allDocs = function DB_allDocs(){
var docs = {}
for (var seq = self.lastSeq(); seq >= 1; seq--){
var id = storage.get(seqPrefix + seq)
if (id in docs) continue
var doc = self.get(id)
if (doc)
docs[id] = doc
}
var docs = values(docs)
return {
total_rows: docs.length,
rows: docs
}
}
self.wipe = function DB_wipe(cb) {
for (var seq = self.lastSeq(); seq >= 1; seq--){
var id = storage.get(seqPrefix + seq)
storage.remove(docPrefix + id)
storage.remove(seqPrefix + seq)
}
dbInfo = {lastSeq: 0, docCount: 0}
storage.put(dbName, dbInfo)
}
self.lastSeq = function BC_lastSeq(){
return dbInfo.lastSeq;
}
// === View ===
//
// Perform a query on the data. Queries are in the form of
// map-reduce functions.
//
// takes object of options:
//
// * {{{options.map}}} : The map function to be applied to each document
// (REQUIRED)
//
// * {{{options.finished}}} : A callback for the result.
// (REQUIRED)
//
// * {{{options.chunkSize}}}
// * {{{options.progress}}} : A callback to indicate progress of a query
// * {{{options.mapReducer}}} : A Map-Reduce engine, by default uses a
// single thread
// * {{{options.reduce}}} : The reduce function
self.view = function DB_view(options) {
if (!options.map)
throw new Error('map function not provided');
if (!options.finished)
throw new Error('finished callback not provided');
// Maximum number of items to process before giving the UI a chance
// to breathe.
var DEFAULT_CHUNK_SIZE = 1000;
// If no progress callback is given, we'll automatically give the
// UI a chance to breathe for this many milliseconds before continuing
// processing.
var DEFAULT_UI_BREATHE_TIME = 50;
var chunkSize = options.chunkSize;
if (!chunkSize)
chunkSize = DEFAULT_CHUNK_SIZE;
var progress = options.progress;
if (!progress)
progress = function defaultProgress(phase, percent, resume) {
window.setTimeout(resume, DEFAULT_UI_BREATHE_TIME);
};
var mapReducer = options.mapReducer;
if (!mapReducer)
mapReducer = bc.SingleThreadedMapReducer;
mapReducer.map(
options.map,
storage,
docPrefix,
progress,
chunkSize,
function(mapResult) {
if (options.reduce)
mapReducer.reduce(
options.reduce,
mapResult,
progress,
chunkSize,
function(rows) {
options.finished(new BrowserCouch._View(rows));
});
else
options.finished(new BrowserCouch._MapView(mapResult));
});
};
self.getChanges = function(options){
options = options || {};
since = options.since || 0;
var changes = [];