2
2
Function: LocalStorageDB()
3
3
Author: Aaron Gustafson (aaron at easy-designs dot net)
4
4
Creation Date: 2011-10-03
5
- Version: 1.0
5
+ Version: 0.3
6
6
Homepage: http://github.com/easy-designs/LocalStorageDB.js
7
7
License: MIT License (see homepage)
8
8
------------------------------------------------------------------------------*/
9
9
; if ( 'localStorage' in window )
10
10
{
11
+
11
12
/**
12
13
* LocalStorageDB()
13
14
* a simple cross-browser DB using localStorage
@@ -21,8 +22,11 @@ License: MIT License (see homepage)
21
22
22
23
var
23
24
UNDEFINED ,
25
+ TRUE = true ,
26
+ FALSE = false ,
27
+ NULL = null ,
28
+ __cache ,
24
29
WINDOW = window ,
25
- __cache = WINDOW . localStorage ,
26
30
HYPHEN = '-' ,
27
31
PREFIX = 'LocalStorageDB-' ,
28
32
TABLES = '::tables::' ,
@@ -99,6 +103,20 @@ License: MIT License (see homepage)
99
103
return this . splice ( start , end ) ;
100
104
} ;
101
105
106
+
107
+ // for Firefox when users manually disable localStorage
108
+ try {
109
+ __cache = WINDOW . localStorage ;
110
+ } catch ( e ) {
111
+ if ( 'console' in WINDOW )
112
+ {
113
+ console . log ( 'localStorage is not available' , e ) ;
114
+ }
115
+ // die
116
+ return FALSE ;
117
+ }
118
+
119
+
102
120
/**
103
121
* init() -> undefined
104
122
* Initializes the DB and loads its contents in to memory
@@ -112,17 +130,14 @@ License: MIT License (see homepage)
112
130
{
113
131
// store for easier access
114
132
DB [ TABLES ] = tables ;
115
- // loop and load
116
- while ( t -- )
117
- {
118
- DB [ tables [ t ] ] = readFromCache ( tables [ t ] ) ;
119
- }
120
133
}
121
134
else
122
135
{
123
136
DB [ TABLES ] = [ ] ;
124
137
cache ( TABLES ) ;
125
138
}
139
+ // garbage collection
140
+ tables = NULL ;
126
141
}
127
142
/**
128
143
* tableExists( table ) -> boolean
@@ -141,11 +156,13 @@ License: MIT License (see homepage)
141
156
{
142
157
if ( table == tables [ t ] )
143
158
{
144
- return true ;
159
+ return TRUE ;
145
160
}
146
161
}
162
+ // garbage collection
163
+ tables = t = NULL ;
147
164
}
148
- return false ;
165
+ return FALSE ;
149
166
}
150
167
/**
151
168
* insertData( table, data ) -> undefined
@@ -157,9 +174,8 @@ License: MIT License (see homepage)
157
174
function insertData ( table , data )
158
175
{
159
176
var
160
- t = DB [ table ] ,
161
- p = t . dfn ,
162
- i = t . index ++ ,
177
+ p = DB [ table ] . dfn ,
178
+ i = DB [ table ] . index ++ ,
163
179
d = { } ,
164
180
k ;
165
181
for ( k in p )
@@ -172,18 +188,23 @@ License: MIT License (see homepage)
172
188
}
173
189
DB [ table ] . data . push ( d ) ;
174
190
AFFECTED_ROWS ++ ;
191
+ // garbage collection
192
+ d = data = NULL ;
175
193
}
176
194
/**
177
- * findMatches( d , c ) -> array
178
- * Finds items within the data set that match the supplied criteria
195
+ * findMatches( t , c ) -> array
196
+ * Finds items within the table that match the supplied criteria
179
197
*
180
- * @param array d - the data set
198
+ * @param array t - the table
181
199
* @param mixed c - the criteria object to be matched or the criteria function
182
200
*/
183
- function findMatches ( d , c )
201
+ function findMatches ( t , c )
184
202
{
203
+ console . log ( t ) ;
204
+ t = load ( t ) ;
205
+ console . log ( t ) ;
185
206
var
186
- d = clone ( d ) , // never let a select mutate a row
207
+ d = clone ( t . data ) , // never let a select mutate a row
187
208
i = d . length ,
188
209
a = new RESULT_SET ( ) ,
189
210
r , p ;
@@ -218,6 +239,11 @@ License: MIT License (see homepage)
218
239
{
219
240
a = d . reverse ( ) ;
220
241
}
242
+
243
+ // garbage collection
244
+ t = d = c = NULL ;
245
+
246
+ // return
221
247
return a . reverse ( ) ;
222
248
}
223
249
/**
@@ -247,6 +273,17 @@ License: MIT License (see homepage)
247
273
// trigger the callback, supplying the index
248
274
f ( i ) ;
249
275
}
276
+
277
+ // garbage collection
278
+ d = c = f = i = r = p = NULL ;
279
+ }
280
+ /**
281
+ * load( table ) -> boolean
282
+ * Loads the supplied table name
283
+ */
284
+ function load ( table )
285
+ {
286
+ return readFromCache ( table ) ;
250
287
}
251
288
/**
252
289
* cache( table ) -> boolean
@@ -328,29 +365,39 @@ License: MIT License (see homepage)
328
365
tableExists ( table ) )
329
366
{
330
367
throw new Error ( table + ' already exists' ) ;
331
- return false ;
368
+ return FALSE ;
332
369
}
333
370
334
371
// set up the table
335
372
DB [ table ] = { } ;
336
373
DB [ table ] . data = [ ] ;
337
374
DB [ table ] . dfn = dfn ;
338
- DB [ table ] . index = 0 ;
375
+ DB [ table ] . index = 1 ;
376
+
377
+ // cache the table index
378
+ DB [ TABLES ] . push ( table ) ;
379
+ cache ( TABLES ) ;
339
380
340
381
// insert the data (if asked)
341
382
if ( data &&
342
383
( data instanceof Array ||
343
384
data instanceof Object ) )
344
385
{
345
386
this . INSERT_INTO ( table , data ) ;
387
+ // caching and garbage collection handled elsewhere
388
+ }
389
+ else
390
+ {
391
+ // cache the table
392
+ cache ( table ) ;
393
+ // garbage collection
394
+ delete DB [ table ] ;
346
395
}
347
396
348
- // cache the table
349
- cache ( table ) ;
350
- // cache the table index
351
- DB [ TABLES ] . push ( table ) ;
352
- cache ( TABLES ) ;
353
- return true ;
397
+ // garbage collection
398
+ dfn = data = NULL ;
399
+
400
+ return TRUE ;
354
401
} ;
355
402
356
403
/**
@@ -363,7 +410,7 @@ License: MIT License (see homepage)
363
410
{
364
411
if ( tableExists ( table ) )
365
412
{
366
- return DB [ table ] . dfn ;
413
+ return load ( table ) . dfn ;
367
414
}
368
415
else
369
416
{
@@ -384,10 +431,13 @@ License: MIT License (see homepage)
384
431
{
385
432
if ( tableExists ( table ) )
386
433
{
434
+ DB [ table ] = load ( table ) ;
387
435
AFFECTED_ROWS = DB [ table ] . data . length ;
388
436
DB [ table ] . index = 0 ;
389
437
DB [ table ] . data = [ ] ;
390
438
cache ( table ) ;
439
+ // garbage collection
440
+ delete DB [ table ] ;
391
441
}
392
442
else
393
443
{
@@ -425,6 +475,8 @@ License: MIT License (see homepage)
425
475
}
426
476
cache ( TABLES ) ;
427
477
removeFromCache ( table ) ;
478
+ // garbage collection
479
+ tables = NULL ;
428
480
}
429
481
else
430
482
{
@@ -458,6 +510,10 @@ License: MIT License (see homepage)
458
510
AFFECTED_ROWS = 0 ;
459
511
if ( tableExists ( table ) )
460
512
{
513
+ if ( DB [ table ] == UNDEFINED )
514
+ {
515
+ DB [ table ] = load ( table ) ;
516
+ }
461
517
if ( data instanceof Array )
462
518
{
463
519
for ( var i = 0 , len = data . length ; i < len ; i ++ )
@@ -477,6 +533,9 @@ License: MIT License (see homepage)
477
533
throw new Error ( 'LocalStorageDB.insert() expects an Object or an array of Objects to be inserted as data' ) ;
478
534
}
479
535
cache ( table ) ;
536
+ // garbage collection
537
+ delete DB [ table ] ;
538
+ data = NULL ;
480
539
}
481
540
else
482
541
{
@@ -506,7 +565,7 @@ License: MIT License (see homepage)
506
565
{
507
566
if ( tableExists ( table ) )
508
567
{
509
- return findMatches ( DB [ table ] . data , criteria ) ;
568
+ return findMatches ( table , criteria ) ;
510
569
}
511
570
else
512
571
{
@@ -540,14 +599,15 @@ License: MIT License (see homepage)
540
599
AFFECTED_ROWS = 0 ;
541
600
if ( tableExists ( table ) )
542
601
{
602
+ DB [ table ] = load ( table ) ;
603
+ var i = DB [ table ] . data . length ,
604
+ o_data , n_data , p , changed ;
605
+
543
606
if ( mutation instanceof Function )
544
607
{
545
- var
546
- i = DB [ table ] . data . length ,
547
- o_data , n_data , p , changed ;
548
608
while ( i -- )
549
609
{
550
- changed = false ;
610
+ changed = FALSE ;
551
611
o_data = DB [ table ] . data [ i ] ;
552
612
n_data = mutation ( clone ( o_data ) ) ; // clone before mutating
553
613
if ( ! ! n_data )
@@ -557,7 +617,7 @@ License: MIT License (see homepage)
557
617
if ( o_data . hasOwnProperty ( p ) &&
558
618
o_data [ p ] != n_data [ p ] )
559
619
{
560
- changed = true ;
620
+ changed = TRUE ;
561
621
break ;
562
622
}
563
623
}
@@ -567,14 +627,12 @@ License: MIT License (see homepage)
567
627
AFFECTED_ROWS ++ ;
568
628
}
569
629
}
570
- }
630
+ }
571
631
}
572
632
else if ( mutation instanceof Object )
573
633
{
574
634
withMatches ( DB [ table ] . data , criteria , function ( i ) {
575
- var
576
- newData = DB [ table ] . data [ i ] ,
577
- p ;
635
+ var newData = DB [ table ] . data [ i ] ;
578
636
for ( p in DB [ table ] . dfn )
579
637
{
580
638
if ( DB [ table ] . dfn . hasOwnProperty ( p ) &&
@@ -585,13 +643,18 @@ License: MIT License (see homepage)
585
643
}
586
644
DB [ table ] . data [ i ] = newData ;
587
645
AFFECTED_ROWS ++ ;
646
+ // garbage collection
647
+ i = newData = NULL ;
588
648
} ) ;
589
649
}
590
650
else
591
651
{
592
652
throw new Error ( 'LocalStorageDB.UPDATE() expects a mutation object or function as the second argument' ) ;
593
653
}
594
654
cache ( table ) ;
655
+ // garbage collection
656
+ delete DB [ table ] ;
657
+ o_data = n_data = NULL ;
595
658
}
596
659
else
597
660
{
@@ -621,11 +684,14 @@ License: MIT License (see homepage)
621
684
}
622
685
else
623
686
{
687
+ DB [ table ] = load ( table ) ;
624
688
withMatches ( DB [ table ] . data , criteria , function ( i ) {
625
689
DB [ table ] . data . splice ( i , 1 ) ;
626
690
AFFECTED_ROWS ++ ;
627
691
} ) ;
628
692
cache ( table ) ;
693
+ // garbage collection
694
+ delete DB [ table ] ;
629
695
}
630
696
} ;
631
697
@@ -634,6 +700,4 @@ License: MIT License (see homepage)
634
700
// load it up!
635
701
init ( ) ;
636
702
}
637
- }
638
-
639
-
703
+ }
0 commit comments