forked from brucemcpherson/cUseful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqueeze.gs
525 lines (440 loc) · 14.2 KB
/
Squeeze.gs
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
/**
* utils for squeezing more out of Apps Script quotas
* @namespace Squeeze
*/
var Squeeze = (function (ns) {
/**
* utilities for zipping and chunking data for property stores and cache
* @constructor ChunkingUtils
*/
ns.Chunking = function () {
// the default maximum chunksize
var chunkSize_ = 9*1024,
self = this,
store_,
prefix_ = "chunking_",
overhead_ = 12,
digestOverhead_ = 40 + 10,
respectDigest_ = true,
compressMin_ = 300;
//--default functions for these operations
// how to get an object
var getObject_ = function (store , key) {
var result = readFromStore_ (store, key );
return result ? JSON.parse (result) : null;
};
// how to set an object
var setObject_ = function (store, key , ob , expire) {
var s = JSON.stringify(ob || {});
writeToStore_ ( store , key, s , expire );
return s.length;
};
// how to write a string
var writeToStore_ = function ( store, key, str) {
return Utils.expBackoff(function () {
return store.setProperty (key , str);
});
};
// how to read a string
var readFromStore_ = function (store, key) {
return Utils.expBackoff(function () {
return store.getProperty (key);
});
};
// how to remove an object
var removeObject_ = function (store, key) {
return Utils.expBackoff(function () {
return store.deleteProperty (key);
});
};
/**
* set the max chunksize
* @param {number} chunkSize the max size
* @return {Chunking} self
*/
self.setChunkSize = function (chunkSize) {
chunkSize_ = chunkSize;
return self;
};
/**
* minimum size over which to compress
* @return {boolean} respectDigest the max size
*/
self.getCompressMin = function () {
return compressMin_;
};
/**
* whether to respect digest to avoid rewriting unchanged records
* @param {boolean} compressMin the min size
* @return {Chunking} self
*/
self.setCompressMin = function (compressMin) {
if (!Utils.isUndefined(compressMin))compressMin_ = compressMin;
return self;
};
/**
* whether to respect digest to avoid rewriting unchanged records
* @return {boolean} respectDigest
*/
self.getRespectDigest = function () {
return respectDigest_;
};
/**
* whether to respect digest to avoid rewriting unchanged records
* @param {boolean} respectDigest the max size
* @return {Chunking} self
*/
self.setRespectDigest = function (respectDigest) {
if (!Utils.isUndefined(respectDigest_))respectDigest_ = respectDigest;
return self;
};
/**
* get the max chunksize
* @return {number} chunkSize the max size
*/
self.getChunkSize = function () {
return chunkSize_;
};
/**
* set the key prefix
* @param {string} prefix the key prefix
* @return {Chunking} self
*/
self.setPrefix = function (prefix) {
if (!Utils.isUndefined(prefix))prefix_ = prefix.toString() ;
return self;
};
/**
* get the prefix
* @return {string} prefix the prefix
*/
self.getPrefix = function () {
return prefix_;
};
/**
* set the store
* @param {object} store the store
* @return {Chunking} self
*/
self.setStore = function (store) {
store_ = store;
return self;
};
/**
* get the store
* @return {object} the store
*/
self.getStore = function () {
return store_;
};
/**
* set how to get an object
* @param {function} func how to get an object
* @return {Chunking} self
*/
self.funcGetObject = function (func) {
// func should take a store, key and return an object
getObject_ = checkAFunc(func);
return self;
};
/**
* set how to get an object
* @param {function} func how to set an object
* @return {Chunking} self
*/
self.funcSetObject = function (func) {
// func should take a store, key and an object, and return the size of the stringified object
setObject_ = checkAFunc(func);
return self;
};
/**
* set how to read from store
* @param {function} func how to read from store
* @return {Chunking} self
*/
self.funcReadFromStore = function (func) {
// func should take a store key, and return a string
readFromStore_ = checkAFunc(func);
return self;
};
/**
* set how to write to store
* @param {function} func how to set an object
* @return {Chunking} self
*/
self.funcWriteToStore = function (func) {
// func should take a store key and a string to write
writeToStore_ = checkAFunc(func);
return self;
};
/**
* set how to remove an object
* @param {function} func how to remove an object
* @return {Chunking} self
*/
self.funcRemoveObject = function (func) {
// func should take a store, key
removeObject_ = checkAFunc(func);
return self;
};
/**
* check that a variable is a function and throw if not
* @param {function} [func] optional function to check
* @return {function} the func
*/
function checkAFunc (func) {
if (func && typeof func !== 'function') {
throw new Error('argument should be a function');
}
return func;
}
function payloadSize_ () {
if (chunkSize_ <= overhead_) {
throw 'chunksize must be at least '+ ( overhead_ +1);
}
return chunkSize_ - overhead_;
}
function digest_ (what) {
return Utils.keyDigest (what);
}
function uid_ () {
return Utils.generateUniqueString();
}
function getChunkKey_ (key) {
return key + "_" + uid_ ();
}
function fudgeKey_ (key) {
if (Utils.isUndefined(key) || key === null)throw 'property key must have a value';
return typeof key === "object" ? digest_ (key) : key;
}
/**
* get the keys of multiple entries if it was too big
* @param {PropertiesService} props the service to use
* @param {object} propKey the key
* @return {object} the result {chunks:[],data:{}} - an array of keys, or some actual data
*/
self.getChunkKeys = function (propKey) {
// in case the key is an object
propKey = fudgeKey_(propKey);
var data ,
crushed = getObject_ (self.getStore(), propKey);
// at this point, crushed is an object with either
// a .chunk property with a zipped version of the data, or
// a .chunks property with an array of other entries to get
// a .digest property with the digest of all the data which identifies it as a master
// its a non split item
if (crushed && crushed.chunk && crushed.digest) {
// uncrush the data and parse it back to an object if there are no associated records
data = crushed.chunk ? JSON.parse ( crushed.skipZip ? crushed.chunk : self.unzip(crushed.chunk)) : null;
}
// return either the data or where to find the data
return {
chunks: crushed && crushed.chunks ? crushed.chunks: null,
data: data,
digest:crushed ? crushed.digest : "",
skipZip: crushed && crushed.skipZip
}
};
/**
* remove an entry and its associated stuff
* @param {object} propKey the key
* @return {Props} self
*/
self.removeBigProperty = function (propKey) {
// in case the key is an object
propKey = fudgeKey_(propKey);
var removed = 0;
// always big properties are always crushed
var chunky = self.getChunkKeys (prefix_ + propKey);
// now remove the properties entries
if (chunky && chunky.chunks) {
chunky.chunks.forEach(function (d) {
removeObject_ (self.getStore(), d);
removed++;
});
}
// now remove the master property
if (chunky.digest) {
removeObject_ (self.getStore() , prefix_ + propKey);
removed++;
}
return removed;
};
/**
* updates a property using multiple entries if its going to be too big
* @param {object} propKey the key
* @param {object} ob the thing to write
* @param {number} expire secs to expire
* @return {size} of data written - if nothing done, size is 0
*/
self.setBigProperty = function (propKey,ob,expire) {
// in case the key is an object
propKey = fudgeKey_(propKey);
// donbt allow undefined
if (Utils.isUndefined (ob) ) {
throw 'cant write undefined to store';
}
// blob pulls it out
if (Utils.isBlob (ob) ) {
var slob = {
contentType:ob.getContentType(),
name:ob.getName(),
content:Utilities.base64Encode(ob.getBytes()),
blob:true
}
}
// convery to timestamp
else if ( isDateObject (ob)) {
var slob = {
date: true,
content: ob.getTime()
}
}
// strinfigy
else if (typeof (ob) === "object") {
var slob = {
content:JSON.stringify(ob),
parse: true
}
}
else {
var slob = {
content: ob
}
}
// pack all that up to write to the store
const sob = JSON.stringify (slob);
// get the digest
var digest = Utils.keyDigest (sob);
// now get the master if there is one
var master = getObject_ (self.getStore(), prefix_ + propKey);
if (master && master.digest && master.digest === digest && respectDigest_ && !expire) {
// nothing to do
return 0;
}
else {
// need to remove the previous entries and add this new one
self.removeBigProperty (propKey);
return setBigProperty_ (prefix_ + propKey,sob, expire);
}
};
/**
* gets a property using multiple entries if its going to be too big
* @param {object} propKey the key
* @return {object} what was retrieved
*/
self.getBigProperty = function (propKey) {
// in case the key is an object
propKey = fudgeKey_(propKey);
// always big properties are always crushed
var chunky = self.getChunkKeys ( prefix_ + propKey);
// that'll return either some data, or a list of keys
if (chunky && chunky.chunks) {
var p = chunky.chunks.reduce (function (p,c) {
var r = getObject_ ( self.getStore() , c);
// should always be available
if (!r) {
throw 'missing chunked property ' + c + ' for key ' + propKey;
}
// rebuild the crushed string
return p + r.chunk;
},"");
// now uncrush the result
var package = JSON.parse(chunky.skipZip ? p : self.unzip (p));
}
else {
// it was just some data
var package = chunky ? chunky.data : null;
}
// now need to unpack;
if (package) {
if (package.parse) {
return JSON.parse (package.content);
}
else if(package.date) {
return new Date (package.content);
}
else if (package.blob) {
return Utilities.newBlob(Utilities.base64Decode(package.content), package.contentType, package.name);
}
else {
return package.content;
}
}
else {
return null;
}
};
/**
* sets a property using multiple entries if its going to be too big
* use self.setBigProperty() from outside, which first deletes existing stuff
* as well as checking the digest
* @param {object} propKey the key
* @param {string} sob the thing to write
* @return {number} total length of everything written
*/
function setBigProperty_ (propKey,sob, expire) {
// always crush big properties
var size=0;
// crush the object
var skipZip = sob.length < compressMin_;
var chunks, crushed = skipZip ? sob : self.zip (sob) ;
// get the digest
// the digest is used to avoid updates when theres no change
var digest = digest_ (sob);
// if we have an overflow, then need to write multiple properties
if (crushed.length > payloadSize_() - digestOverhead_) {
chunks = [];
}
// now split up the big thing if needed
// expire should be a little bigger for the chunks to make sure they dont go away
do {
// peel off a piece
var chunk = crushed.slice(0,payloadSize_());
crushed = crushed.slice (chunk.length);
if (chunks) {
// make a new entry for the key
var key = getChunkKey_ (propKey);
size += setObject_ (self.getStore(), key , {
chunk:chunk
},expire ? expire + 1: expire);
// remember the key
chunks.push (key);
}
else {
size += setObject_ (self.getStore(), propKey , {
chunk:chunk,
digest:digest,
skipZip:skipZip
},expire);
}
} while (crushed.length);
// now write the index if there were chunks
if (chunks) {
size += setObject_ (self.getStore(), propKey,{
chunks:chunks,
digest:digest,
skipZip: skipZip
},expire );
}
return size;
};
/**
* crush for writing to cache.props
* @param {string} crushThis the string to crush
* @return {string} the b64 zipped version
*/
self.zip = function (crushThis) {
return Utilities.base64Encode(Utilities.zip ([Utilities.newBlob(crushThis)]).getBytes());
};
/**
* uncrush for writing to cache.props
* @param {string} crushed the crushed string
* @return {string} the uncrushed string
*/
self.unzip = function (crushed) {
return Utilities.unzip(Utilities.newBlob(Utilities.base64Decode(crushed),'application/zip'))[0].getDataAsString();
};
}
return ns;
})(Squeeze || {});