-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleDriveStorage.js
More file actions
235 lines (203 loc) · 5.67 KB
/
Copy pathGoogleDriveStorage.js
File metadata and controls
235 lines (203 loc) · 5.67 KB
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
/**
* Implements the {@link Storage} interface with data being saved to a Google
* Drive file.
*/
function GoogleDriveStorage() {
/**
* This is needed because GAS doesn't offer any API to determine the
* webapp deployment mode.
*/
var IN_DEV_MODE = true;
var FOLDER_NAME = "zzz-brain-dump";
var FILE_NAME = "brain-dump.json";
var CACHE_KEY_PREFIX = "raw-data";
var CACHE_DURATION_IN_SECONDS = 60 * 30; // 30 minutes
// GAS allows 100KB, but we just round it down
var CACHE_MAX_VALUE_SIZE_IN_BYTES = 100000;
if ( IN_DEV_MODE ) {
FOLDER_NAME += "-dev";
CACHE_KEY_PREFIX += "-dev";
}
var inMemoryStorage;
var file;
var cache = CacheService.getUserCache();
/**
* Loads the content of the file storage into the {@link InMemoryStorage}
* object. The file will be created if it does not exist.
*/
this.initalize = function() {
var rawData = this.readRawDataFromCache(cache);
if ( null == rawData ) {
var folder = this.initFolder();
file = this.getActiveFile(folder);
var blob = file.getBlob();
rawData = blob.getDataAsString();
this.putRawDataToCache(cache, rawData);
}
inMemoryStorage = new InMemoryStorage(rawData);
/*
if ( 0 == inMemoryStorage.getEntryCount() ) {
for (var i = 0; i < 1000; ++i) {
var tagList2 = ['crypto-currency', 'learning'];
inMemoryStorage.addEntry("Read more on crypto currencies " + i, tagList2);
}
file.setContent(inMemoryStorage.toString());
}
*/
}
/**
* Reads the raw data from {CacheService}. CacheService limits the value to
* a specific size, thus, the raw data might be distributed in multiple keys.
* @param cache {CacheService}
* @return {string} null if data is not in cache
*/
this.readRawDataFromCache = function(cache) {
var rawData = "";
var keyIndex = 0;
while ( true ) {
var key = this.getRawDataCacheKey(keyIndex);
var value = cache.get(key);
if ( null != value ) {
rawData += value;
keyIndex++;
}
else {
break;
}
}
return 0 == rawData.length ? null : rawData;
}
/**
* Stores the input data in the {CacheService}. The data might be splitted
* into mulitiple values if it is larger than the maximum value size
* permitted by {CacheService}.
* @param cache {CacheService}
* @param rawData {string}
*/
this.putRawDataToCache = function(cache, rawData) {
var keyIndex = 0;
while ( true ) {
var str = rawData.substr(keyIndex * CACHE_MAX_VALUE_SIZE_IN_BYTES,
CACHE_MAX_VALUE_SIZE_IN_BYTES);
if (0 == str.length) {
break;
}
else {
var key = this.getRawDataCacheKey(keyIndex);
cache.put(key, str, CACHE_DURATION_IN_SECONDS);
if ( str.length < CACHE_MAX_VALUE_SIZE_IN_BYTES ) {
break;
}
else {
keyIndex++;
}
}
}
}
/**
* Returns the cache key for the given key index.
* @return {string}
*/
this.getRawDataCacheKey = function(keyIndex) {
return CACHE_KEY_PREFIX + "-" + keyIndex;
}
/**
* @return {int} the number of entries
*/
this.getEntryCount = function() {
return inMemoryStorage.getEntryCount();
}
/**
* @return {Tags}
*/
this.getTagManager = function() {
return inMemoryStorage.getTagManager();
}
/**
* Adds a new entry and immediately write to the file.
* @param {string} content
* @param {array} tags - array of strings
* @return {object}
*/
this.addEntry = function(content, tags) {
var entry = inMemoryStorage.addEntry(content, tags);
this.writeRawDataToFile(cache, inMemoryStorage.toString());
return entry;
}
/**
* Updates an existing entry.
* @param entry {object}
* @throw error if the entry is not valid.
*/
this.updateEntry = function(entry) {
inMemoryStorage.updateEntry(entry);
}
/**
* Writes the data to file and updates the cache.
* @param cache {CacheService}
* @param rawData {string}
*/
this.writeRawDataToFile = function(cache, rawData) {
if ( null == file ) {
var folder = this.initFolder();
file = this.getActiveFile(folder);
}
file.setContent(rawData);
// invalidate cache
if ( null != cache ) {
var keyIndex = 0;
while ( true ) {
var key = CACHE_KEY_PREFIX + "-" + keyIndex;
if (null == cache.get(key)) {
break;
}
else {
cache.remove(key);
key++;
}
}
}
this.putRawDataToCache(cache, rawData);
}
/**
* Returns an array of the entries matching the given parameters..
* @type {Array.<object>}
*/
this.findEntriesByTagImpl = function(tagName, offset, count) {
return inMemoryStorage.findEntriesByTagImpl(tagName, offset, count);
};
/**
* Checks if {@link FOLDER_NAME} exists; if not, create it at root level.
* @return {Folder}
*/
this.initFolder = function() {
var brainDumpFolder = null;
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var tmpFolder = folders.next();
if (FOLDER_NAME == tmpFolder.getName()) {
brainDumpFolder = tmpFolder;
break;
}
}
if ( null == brainDumpFolder ) {
brainDumpFolder = DriveApp.createFolder(FOLDER_NAME);
}
return brainDumpFolder;
}
/**
* Gets or creates file {@link FILE_NAME} in {@code folder}.
* @return {File}
*/
this.getActiveFile = function(folder) {
var file;
var fileIterator = folder.getFilesByName(FILE_NAME);
if (fileIterator.hasNext()) {
file = fileIterator.next();
}
else {
file = folder.createFile(FILE_NAME, '');
}
return file;
}
}