~mrooney/etherpad/ubuntu

« back to all changes in this revision

Viewing changes to trunk/trunk/infrastructure/framework-src/modules/sqlbase/sqlbase.js

  • Committer: Aaron Iba
  • Date: 2009-12-18 07:40:23 UTC
  • Revision ID: hg-v1:a9f8774a2e00cc15b35857471fecea17f649e3c9
initial code push

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS-IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
import("jsutils.*");
 
18
import("sqlbase.sqlcommon");
 
19
import("fastJSON");
 
20
import("timer");
 
21
 
 
22
jimport("java.lang.System.out.println");
 
23
 
 
24
function _sqlbase() {
 
25
  return sqlcommon.getSqlBase();
 
26
}
 
27
 
 
28
/**
 
29
 * Creates a SQL table suitable for storing a mapping from String to JSON value.
 
30
 * Maximum key length is 128 characters. Has no effect if the table already exists.
 
31
 */
 
32
function createJSONTable(tableName) {
 
33
  _sqlbase().createJSONTable(String(tableName));
 
34
}
 
35
 
 
36
/**
 
37
 * Retrieves a JavaScript object or value from a table.  Returns undefined
 
38
 * if there is no mapping for the given string key.  Requires that the table
 
39
 * exist.
 
40
 */
 
41
function getJSON(tableName, stringKey) {
 
42
  var result = _sqlbase().getJSON(String(tableName), String(stringKey));
 
43
  if (result) {
 
44
 
 
45
    return fastJSON.parse(String(result))['x'];
 
46
    
 
47
    /* performance-testing JSON
 
48
    var obj1 = timer.time("JSON.parse (json2)", function() {
 
49
      return JSON.parse(String(result))['x'];
 
50
    });
 
51
    var obj2 = timer.time("JSON.parse (fastJSON)", function() {
 
52
      return fastJSON.parse(String(result))['x'];
 
53
    });
 
54
    return obj2;
 
55
    */
 
56
  }
 
57
  return undefined;
 
58
}
 
59
 
 
60
function getAllJSON(tableName, start, count) {
 
61
  var result = _sqlbase().getAllJSON(String(tableName), Number(start), Number(count));
 
62
  return Array.prototype.map.call(result, function(x) {
 
63
    return {id: x.id(), value: fastJSON.parse(String(x.value()))['x']};
 
64
  })
 
65
}
 
66
 
 
67
function getAllJSONKeys(tableName) {
 
68
  var result = _sqlbase().getAllJSONKeys(String(tableName));
 
69
  return Array.prototype.map.call(result, function(x) { return String(x); });
 
70
}
 
71
 
 
72
/**
 
73
 * Assigns a JavaScript object or primitive value to a string key in a table.
 
74
 * Maximum key length is 128 characters. Requires that the table exist.
 
75
 */
 
76
function putJSON(tableName, stringKey, objectOrValue) {
 
77
  var obj = ({x:objectOrValue});
 
78
  
 
79
  var json = fastJSON.stringify(obj);
 
80
 
 
81
  /* performance-testing JSON
 
82
 
 
83
  var json1 = timer.time("JSON.stringify (json2)", function() { 
 
84
    return JSON.stringify(obj);
 
85
  });
 
86
  var json2 = timer.time("JSON.stringify (fastJSON)", function() {
 
87
    return fastJSON.stringify(obj);
 
88
  });
 
89
 
 
90
  if (json1 != json2) {
 
91
    println("json strings do not match!");
 
92
    println("\n\n");
 
93
    println(json1);
 
94
    println("\n");
 
95
    println(json2);
 
96
    println("\n\n");
 
97
  }*/
 
98
 
 
99
  _sqlbase().putJSON(String(tableName), String(stringKey), json);
 
100
}
 
101
 
 
102
/**
 
103
 * Removes the mapping for a string key from a table.  Requires that the table
 
104
 * exist.
 
105
 */
 
106
function deleteJSON(tableName, stringKey) {
 
107
  _sqlbase().deleteJSON(String(tableName), String(stringKey));
 
108
}
 
109
 
 
110
/**
 
111
 * Creates a SQL table suitable for storing a mapping from (key,n) to string.
 
112
 * The mapping may be sparse, but storage is most efficient when n are consecutive.
 
113
 * The "length" of the array is not stored and must be externally maintained.
 
114
 * Maximum key length is 128 characters.  This call has no effect if the table
 
115
 * already exists.
 
116
 */
 
117
function createStringArrayTable(tableName) {
 
118
  _sqlbase().createStringArrayTable(String(tableName));
 
119
}
 
120
 
 
121
/**
 
122
 * Assigns a string value to a (key,n) pair in a StringArray table.  Maximum key length
 
123
 * is 128 characters.  Requires that the table exist.
 
124
 */
 
125
function putStringArrayElement(tableName, stringKey, n, value) {
 
126
  _sqlbase().putStringArrayElement(String(tableName), String(stringKey),
 
127
                                Number(n), String(value));
 
128
}
 
129
 
 
130
/**
 
131
 * Equivalent to a series of consecutive puts of the elements of valueArray, with the first
 
132
 * one going to n=startN, the second to n=startN+1, and so on, but much more efficient.
 
133
 */
 
134
function putConsecutiveStringArrayElements(tableName, stringKey, startN, valueArray) {
 
135
  var putter = _sqlbase().putMultipleStringArrayElements(String(tableName), String(stringKey));
 
136
  for(var i=0;i<valueArray.length;i++) {
 
137
    putter.put(Number(startN)+i, String(valueArray[i]));
 
138
  }
 
139
  putter.finish();
 
140
}
 
141
 
 
142
/**
 
143
 * Equivalent to a series of puts of the (key,value) entries of the JavaScript object
 
144
 * nToValue, using as few database operations as possible.
 
145
 */
 
146
function putDictStringArrayElements(tableName, stringKey, nToValue) {
 
147
  var nArray = [];
 
148
  for(var n in nToValue) {
 
149
    nArray.push(n);
 
150
  }
 
151
  nArray.sort(function(a,b) { return Number(a) - Number(b); });
 
152
  
 
153
  var putter = _sqlbase().putMultipleStringArrayElements(String(tableName), String(stringKey));
 
154
  nArray.forEach(function(n) {
 
155
    putter.put(Number(n), String(nToValue[n]));
 
156
  });
 
157
  putter.finish();
 
158
}
 
159
 
 
160
/**
 
161
 * Retrieves a string value from a StringArray table.  Returns undefined
 
162
 * if there is no mapping for the given (key,n) pair.  Requires that the table
 
163
 * exist.
 
164
 */
 
165
function getStringArrayElement(tableName, stringKey, n) {
 
166
  var result = _sqlbase().getStringArrayElement(String(tableName),
 
167
    String(stringKey), Number(n));
 
168
  if (result) {
 
169
    return String(result);
 
170
  }
 
171
  return undefined;
 
172
}
 
173
 
 
174
/**
 
175
 * Retrieves all values from the database page that contains the mapping for n.
 
176
 * Properties are added to destMap for n, if present in the database, and any other
 
177
 * numeric entries in the same page.  No return value.
 
178
 */
 
179
function getPageStringArrayElements(tableName, stringKey, n, destMap) {
 
180
  var array = _sqlbase().getPageStringArrayElements(String(tableName), String(stringKey), n);
 
181
  for(var i=0;i<array.length;i++) {
 
182
    var entry = array[i];
 
183
    destMap[entry.index()] = String(entry.value());
 
184
  }
 
185
}
 
186
 
 
187
/**
 
188
 * Removes the mapping for a (key,n) pair from a StringArray table.  Requires that the table
 
189
 * exist.
 
190
 */
 
191
function deleteStringArrayElement(tableName, stringKey, n) {
 
192
  _sqlbase().putStringArrayElement(String(tableName), String(stringKey), Number(n), null);
 
193
}
 
194
 
 
195
/**
 
196
 * Removes all mappings and metadata associated with a given key in a table.
 
197
 */
 
198
function clearStringArray(tableName, stringKey) {
 
199
  _sqlbase().clearStringArray(String(tableName), stringKey);
 
200
}
 
201
 
 
202
function getStringArrayAllKeys(tableName) {
 
203
  var result = _sqlbase().getStringArrayAllKeys(String(tableName));
 
204
  return Array.prototype.map.call(result, function(x) { return String(x); });
 
205
}