Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit a8be0b6

Browse files
Added better garbage collection & memory management
1 parent f38d02b commit a8be0b6

File tree

2 files changed

+111
-39
lines changed

2 files changed

+111
-39
lines changed

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
*v0.3* (2011-11-04)
2+
3+
* Added better garbage collection & memory management
4+
5+
*v0.2* (2011-10-06)
6+
7+
* Made the result sets LIMIT-able and ORDER-able (by number, string & RANDOM at least)
8+
19
*v0.1* (2011-10-04)
210

311
* Putting a little something together

src/LocalStorageDB.js

Lines changed: 103 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
Function: LocalStorageDB()
33
Author: Aaron Gustafson (aaron at easy-designs dot net)
44
Creation Date: 2011-10-03
5-
Version: 1.0
5+
Version: 0.3
66
Homepage: http://github.com/easy-designs/LocalStorageDB.js
77
License: MIT License (see homepage)
88
------------------------------------------------------------------------------*/
99
;if ( 'localStorage' in window )
1010
{
11+
1112
/**
1213
* LocalStorageDB()
1314
* a simple cross-browser DB using localStorage
@@ -21,8 +22,11 @@ License: MIT License (see homepage)
2122

2223
var
2324
UNDEFINED,
25+
TRUE = true,
26+
FALSE = false,
27+
NULL = null,
28+
__cache,
2429
WINDOW = window,
25-
__cache = WINDOW.localStorage,
2630
HYPHEN = '-',
2731
PREFIX = 'LocalStorageDB-',
2832
TABLES = '::tables::',
@@ -99,6 +103,20 @@ License: MIT License (see homepage)
99103
return this.splice( start, end );
100104
};
101105

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+
102120
/**
103121
* init() -> undefined
104122
* Initializes the DB and loads its contents in to memory
@@ -112,17 +130,14 @@ License: MIT License (see homepage)
112130
{
113131
// store for easier access
114132
DB[TABLES] = tables;
115-
// loop and load
116-
while ( t-- )
117-
{
118-
DB[ tables[t] ] = readFromCache( tables[t] );
119-
}
120133
}
121134
else
122135
{
123136
DB[TABLES] = [];
124137
cache( TABLES );
125138
}
139+
// garbage collection
140+
tables = NULL;
126141
}
127142
/**
128143
* tableExists( table ) -> boolean
@@ -141,11 +156,13 @@ License: MIT License (see homepage)
141156
{
142157
if ( table == tables[t] )
143158
{
144-
return true;
159+
return TRUE;
145160
}
146161
}
162+
// garbage collection
163+
tables = t = NULL;
147164
}
148-
return false;
165+
return FALSE;
149166
}
150167
/**
151168
* insertData( table, data ) -> undefined
@@ -157,9 +174,8 @@ License: MIT License (see homepage)
157174
function insertData( table, data )
158175
{
159176
var
160-
t = DB[table],
161-
p = t.dfn,
162-
i = t.index++,
177+
p = DB[table].dfn,
178+
i = DB[table].index++,
163179
d = {},
164180
k;
165181
for ( k in p )
@@ -172,18 +188,23 @@ License: MIT License (see homepage)
172188
}
173189
DB[table].data.push( d );
174190
AFFECTED_ROWS++;
191+
// garbage collection
192+
d = data = NULL;
175193
}
176194
/**
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
179197
*
180-
* @param array d - the data set
198+
* @param array t - the table
181199
* @param mixed c - the criteria object to be matched or the criteria function
182200
*/
183-
function findMatches( d, c )
201+
function findMatches( t, c )
184202
{
203+
console.log( t );
204+
t = load( t );
205+
console.log( t );
185206
var
186-
d = clone( d ), // never let a select mutate a row
207+
d = clone( t.data ), // never let a select mutate a row
187208
i = d.length,
188209
a = new RESULT_SET(),
189210
r, p;
@@ -218,6 +239,11 @@ License: MIT License (see homepage)
218239
{
219240
a = d.reverse();
220241
}
242+
243+
// garbage collection
244+
t = d = c = NULL;
245+
246+
// return
221247
return a.reverse();
222248
}
223249
/**
@@ -247,6 +273,17 @@ License: MIT License (see homepage)
247273
// trigger the callback, supplying the index
248274
f( i );
249275
}
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 );
250287
}
251288
/**
252289
* cache( table ) -> boolean
@@ -328,29 +365,39 @@ License: MIT License (see homepage)
328365
tableExists( table ) )
329366
{
330367
throw new Error( table + ' already exists' );
331-
return false;
368+
return FALSE;
332369
}
333370

334371
// set up the table
335372
DB[table] = {};
336373
DB[table].data = [];
337374
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 );
339380

340381
// insert the data (if asked)
341382
if ( data &&
342383
( data instanceof Array ||
343384
data instanceof Object ) )
344385
{
345386
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];
346395
}
347396

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;
354401
};
355402

356403
/**
@@ -363,7 +410,7 @@ License: MIT License (see homepage)
363410
{
364411
if ( tableExists( table ) )
365412
{
366-
return DB[table].dfn;
413+
return load( table ).dfn;
367414
}
368415
else
369416
{
@@ -384,10 +431,13 @@ License: MIT License (see homepage)
384431
{
385432
if ( tableExists( table ) )
386433
{
434+
DB[table] = load( table );
387435
AFFECTED_ROWS = DB[table].data.length;
388436
DB[table].index = 0;
389437
DB[table].data = [];
390438
cache( table );
439+
// garbage collection
440+
delete DB[table];
391441
}
392442
else
393443
{
@@ -425,6 +475,8 @@ License: MIT License (see homepage)
425475
}
426476
cache( TABLES );
427477
removeFromCache( table );
478+
// garbage collection
479+
tables = NULL;
428480
}
429481
else
430482
{
@@ -458,6 +510,10 @@ License: MIT License (see homepage)
458510
AFFECTED_ROWS = 0;
459511
if ( tableExists( table ) )
460512
{
513+
if ( DB[table] == UNDEFINED )
514+
{
515+
DB[table] = load( table );
516+
}
461517
if ( data instanceof Array )
462518
{
463519
for ( var i=0, len = data.length; i < len; i++ )
@@ -477,6 +533,9 @@ License: MIT License (see homepage)
477533
throw new Error( 'LocalStorageDB.insert() expects an Object or an array of Objects to be inserted as data' );
478534
}
479535
cache( table );
536+
// garbage collection
537+
delete DB[table];
538+
data = NULL;
480539
}
481540
else
482541
{
@@ -506,7 +565,7 @@ License: MIT License (see homepage)
506565
{
507566
if ( tableExists( table ) )
508567
{
509-
return findMatches( DB[table].data, criteria );
568+
return findMatches( table, criteria );
510569
}
511570
else
512571
{
@@ -540,14 +599,15 @@ License: MIT License (see homepage)
540599
AFFECTED_ROWS = 0;
541600
if ( tableExists( table ) )
542601
{
602+
DB[table] = load( table );
603+
var i = DB[table].data.length,
604+
o_data, n_data, p, changed;
605+
543606
if ( mutation instanceof Function )
544607
{
545-
var
546-
i = DB[table].data.length,
547-
o_data, n_data, p, changed;
548608
while ( i-- )
549609
{
550-
changed = false;
610+
changed = FALSE;
551611
o_data = DB[table].data[i];
552612
n_data = mutation( clone( o_data ) ); // clone before mutating
553613
if ( !! n_data )
@@ -557,7 +617,7 @@ License: MIT License (see homepage)
557617
if ( o_data.hasOwnProperty( p ) &&
558618
o_data[p] != n_data[p] )
559619
{
560-
changed = true;
620+
changed = TRUE;
561621
break;
562622
}
563623
}
@@ -567,14 +627,12 @@ License: MIT License (see homepage)
567627
AFFECTED_ROWS++;
568628
}
569629
}
570-
}
630+
}
571631
}
572632
else if ( mutation instanceof Object )
573633
{
574634
withMatches( DB[table].data, criteria, function( i ){
575-
var
576-
newData = DB[table].data[i],
577-
p;
635+
var newData = DB[table].data[i];
578636
for ( p in DB[table].dfn )
579637
{
580638
if ( DB[table].dfn.hasOwnProperty( p ) &&
@@ -585,13 +643,18 @@ License: MIT License (see homepage)
585643
}
586644
DB[table].data[i] = newData;
587645
AFFECTED_ROWS++;
646+
// garbage collection
647+
i = newData = NULL;
588648
});
589649
}
590650
else
591651
{
592652
throw new Error( 'LocalStorageDB.UPDATE() expects a mutation object or function as the second argument' );
593653
}
594654
cache( table );
655+
// garbage collection
656+
delete DB[table];
657+
o_data = n_data = NULL;
595658
}
596659
else
597660
{
@@ -621,11 +684,14 @@ License: MIT License (see homepage)
621684
}
622685
else
623686
{
687+
DB[table] = load( table );
624688
withMatches( DB[table].data, criteria, function( i ){
625689
DB[table].data.splice(i,1);
626690
AFFECTED_ROWS++;
627691
});
628692
cache( table );
693+
// garbage collection
694+
delete DB[table];
629695
}
630696
};
631697

@@ -634,6 +700,4 @@ License: MIT License (see homepage)
634700
// load it up!
635701
init();
636702
}
637-
}
638-
639-
703+
}

0 commit comments

Comments
 (0)