2
* Copyright 2009 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
import("sqlbase.sqlcommon");
22
jimport("java.lang.System.out.println");
25
return sqlcommon.getSqlBase();
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.
32
function createJSONTable(tableName) {
33
_sqlbase().createJSONTable(String(tableName));
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
41
function getJSON(tableName, stringKey) {
42
var result = _sqlbase().getJSON(String(tableName), String(stringKey));
45
return fastJSON.parse(String(result))['x'];
47
/* performance-testing JSON
48
var obj1 = timer.time("JSON.parse (json2)", function() {
49
return JSON.parse(String(result))['x'];
51
var obj2 = timer.time("JSON.parse (fastJSON)", function() {
52
return fastJSON.parse(String(result))['x'];
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']};
67
function getAllJSONKeys(tableName) {
68
var result = _sqlbase().getAllJSONKeys(String(tableName));
69
return Array.prototype.map.call(result, function(x) { return String(x); });
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.
76
function putJSON(tableName, stringKey, objectOrValue) {
77
var obj = ({x:objectOrValue});
79
var json = fastJSON.stringify(obj);
81
/* performance-testing JSON
83
var json1 = timer.time("JSON.stringify (json2)", function() {
84
return JSON.stringify(obj);
86
var json2 = timer.time("JSON.stringify (fastJSON)", function() {
87
return fastJSON.stringify(obj);
91
println("json strings do not match!");
99
_sqlbase().putJSON(String(tableName), String(stringKey), json);
103
* Removes the mapping for a string key from a table. Requires that the table
106
function deleteJSON(tableName, stringKey) {
107
_sqlbase().deleteJSON(String(tableName), String(stringKey));
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
117
function createStringArrayTable(tableName) {
118
_sqlbase().createStringArrayTable(String(tableName));
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.
125
function putStringArrayElement(tableName, stringKey, n, value) {
126
_sqlbase().putStringArrayElement(String(tableName), String(stringKey),
127
Number(n), String(value));
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.
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]));
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.
146
function putDictStringArrayElements(tableName, stringKey, nToValue) {
148
for(var n in nToValue) {
151
nArray.sort(function(a,b) { return Number(a) - Number(b); });
153
var putter = _sqlbase().putMultipleStringArrayElements(String(tableName), String(stringKey));
154
nArray.forEach(function(n) {
155
putter.put(Number(n), String(nToValue[n]));
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
165
function getStringArrayElement(tableName, stringKey, n) {
166
var result = _sqlbase().getStringArrayElement(String(tableName),
167
String(stringKey), Number(n));
169
return String(result);
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.
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());
188
* Removes the mapping for a (key,n) pair from a StringArray table. Requires that the table
191
function deleteStringArrayElement(tableName, stringKey, n) {
192
_sqlbase().putStringArrayElement(String(tableName), String(stringKey), Number(n), null);
196
* Removes all mappings and metadata associated with a given key in a table.
198
function clearStringArray(tableName, stringKey) {
199
_sqlbase().clearStringArray(String(tableName), stringKey);
202
function getStringArrayAllKeys(tableName) {
203
var result = _sqlbase().getStringArrayAllKeys(String(tableName));
204
return Array.prototype.map.call(result, function(x) { return String(x); });