-
Notifications
You must be signed in to change notification settings - Fork 45
/
schema_retriever.js
147 lines (131 loc) · 4.82 KB
/
schema_retriever.js
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
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2016 Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See COPYING for details
"use strict";
const Q = require('q');
const ThingTalk = require('thingtalk');
const Type = ThingTalk.Type;
const schema = require('../../model/schema');
// A copy of ThingTalk SchemaRetriever
// that uses schema.getDeveloperMetas instead of ThingpediaClient
// (and also ignore builtins)
module.exports = class SchemaRetriever {
constructor(dbClient, language, parseTypes) {
this._schemaRequest = null;
this._pendingSchemaRequests = [];
this._metaRequest = null;
this._pendingMetaRequests = [];
this._cache = {};
this._metaCache = {};
this._dbClient = dbClient;
this._language = language;
this._parseTypes = !!parseTypes;
}
_ensureMetaRequest() {
if (this._metaRequest !== null)
return;
this._metaRequest = Q.delay(0).then(() => {
var pending = this._pendingMetaRequests;
this._pendingMetaRequests = [];
this._metaRequest = null;
console.log('Batched schema-meta request for ' + pending);
return schema.getDeveloperMetas(this._dbClient, pending, this._language);
}).then((rows) => {
rows.forEach((row) => {
if (this._parseTypes) {
this._parseMetaTypes(row.triggers);
this._parseMetaTypes(row.actions);
this._parseMetaTypes(row.queries);
}
this._metaCache[row.kind] = {
triggers: row.triggers,
actions: row.actions,
queries: row.queries
};
});
return this._metaCache;
});
}
_parseMetaTypes(channels) {
for (var name in channels)
channels[name].schema = channels[name].schema.map(ThingTalk.Type.fromString);
}
getFullMeta(kind) {
if (kind in this._metaCache)
return Q(this._metaCache[kind]);
if (this._pendingMetaRequests.indexOf(kind) < 0)
this._pendingMetaRequests.push(kind);
this._ensureMetaRequest();
return this._metaRequest.then(function(everything) {
if (kind in everything)
return everything[kind];
else
throw new Error('Invalid kind ' + kind);
});
}
getMeta(kind, where, name) {
return this.getFullMeta(kind).then((fullSchema) => {
if (!(name in fullSchema[where]))
throw new Error("Schema " + kind + " has no " + where + " " + name);
return fullSchema[where][name];
});
}
_getFullSchema(kind) {
if (typeof kind !== 'string')
throw new TypeError();
if (kind in this._cache)
return Q(this._cache[kind]);
if (this._pendingSchemaRequests.indexOf(kind) < 0)
this._pendingSchemaRequests.push(kind);
this._ensureSchemaRequest();
return this._schemaRequest.then((everything) => {
if (kind in everything)
return everything[kind];
else
throw new Error('Invalid kind ' + kind);
});
}
getSchemaAndNames(kind, where, name) {
return this._getFullSchema(kind).then((fullSchema) => {
if (!(name in fullSchema[where]))
throw new Error("Schema " + kind + " has no " + where + " " + name);
return fullSchema[where][name];
});
}
_ensureSchemaRequest() {
if (this._schemaRequest !== null)
return;
this._schemaRequest = Q.delay(0).then(() => {
var pending = this._pendingSchemaRequests;
this._pendingSchemaRequests = [];
this._schemaRequest = null;
if (pending.length === 0)
return {};
if (!this._silent)
console.log('Batched schema request for ' + pending);
return schema.getDeveloperMetas(this._dbClient, pending, this._language);
}).then((rows) => {
rows.forEach((row) => {
this._parseSchemaTypes(row, row.triggers);
this._parseSchemaTypes(row, row.actions);
this._parseSchemaTypes(row, row.queries);
this._cache[row.kind] = {
triggers: row.triggers,
actions: row.actions,
queries: row.queries
};
});
return this._cache;
});
}
_parseSchemaTypes(schema, channels) {
for (var name in channels) {
channels[name].kind_type = schema.kind_type;
channels[name].types = channels[name].schema.map(Type.fromString);
}
}
}