forked from NatLabRockies/nodehaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHProj.js
More file actions
151 lines (140 loc) · 5.19 KB
/
HProj.js
File metadata and controls
151 lines (140 loc) · 5.19 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
//
// Copyright (c) 2015, Shawn Jacobson
// Licensed under the Academic Free License version 3.0
//
// Ported from @see {@link https://bitbucket.org/brianfrank/haystack-java|Haystack Java Toolkit}
//
// History:
// 21 Mar 2015 Shawn Jacobson Creation
//
/**
* HProj is the common interface for HClient and HServer to provide
* access to a database tagged entity records.
* @see {@link http://project-haystack.org/doc/TagModel|Project Haystack}
* @constructor
*/
function HProj() {}
module.exports = HProj;
/** Abstract functions that must be defined in inheriting classes
* about(): HDict - Get the summary "about" information.
* onReadById(HRef): HDict - Subclass hook for readById, return null if not found.
* onReadByIds(HRef[]): HGrid - Subclass hook for readByIds, return rows with nulls cells for each id not found.
* onReadAll(String, int): HGrid - Subclass hook for read and readAll.
* watchOpen(String, HNum): HWatch - Create a new watch with an empty subscriber list. The dis string is a debug string to keep track of who created the watch. Pass the desired lease time or null to use default.
* watches(): HWatch[] - List the open watches.
* watch(String, boolean checked): HWatch - if 'checked' is undefined, default to true. Lookup a watch by its unique identifier. If not found then raise Error or return null based on checked flag.
* hisRead(HRef, Object): HGrid - Read history time-series data for given record and time range. The items returned are exclusive of start time and inclusive of end time.
* Raise exception if id does not map to a record with the required tags "his" or "tz". The range may be either a String or a HDateTimeRange. If HTimeDateRange is passed then must match the timezone configured on
* the history record. Otherwise if a String is passed, it is resolved relative to the history record's timezone.
* hisWrite(HRef, HHisItem[]): void - Write a set of history time-series data to the given point record. The record must already be defined and must be properly tagged as
* a historized point. The timestamp timezone must exactly match the point's configured "tz" tag. If duplicate or out-of-order items are inserted then they must be gracefully merged.
*/
/**
* Call "read" to lookup an entity record by it's unique identifier.
* If not found then return null or throw an UnknownRecException based
* on checked.
* @param {HRef} id
* @param {boolean} checked
* @param {function} callback
* @return {HDict}
*/
HProj.prototype.readById = function(id, checked, callback) {
var _checked = checked;
var _callback = callback;
if (typeof(_checked)==='function') {
_callback = _checked;
_checked = true;
}
this.onReadById(id, function(err, rec) {
if (typeof(rec) !== 'undefined' && rec !== null) {
_callback(null, rec);
return;
}
if (_checked) {
_callback(new Error("Unknown Rec: " + id));
return;
}
_callback(null, null);
});
};
/**
* Read a list of entity records by their unique identifier.
* Return a grid where each row of the grid maps to the respective
* id array (indexes line up). If checked is true and any one of the
* ids cannot be resolved then raise UnknownRecException for first id
* not resolved. If checked is false, then each id not found has a
* row where every cell is null.
* @param {HRef[]} ids
* @param {boolean} checked
* @param {function} callback
* @return {HGrid}
*/
HProj.prototype.readByIds = function(ids, checked, callback) {
var _checked = checked;
var _callback = callback;
if (typeof(_checked)==='function') {
_callback = _checked;
_checked = true;
}
this.onReadByIds(ids, function(err, grid) {
if (_checked) {
for (var i = 0; i < grid.numRows(); ++i) {
if (grid.row(i).missing("id")) {
_callback(new Error("Unknown Rec: " + ids[i]));
return;
}
}
}
_callback(null, grid);
});
};
/**
* Query one entity record that matches the given filter. If
* there is more than one record, then it is undefined which one is
* returned. If there are no matches than return null or raise
* UnknownRecException based on checked flag.
* @param {string} filter
* @param {boolean} checked
* @param {function} callback
* @return {HDict}
*/
HProj.prototype.read = function(filter, checked, callback) {
var _checked = checked;
var _callback = callback;
if (typeof(_checked)==='function') {
_callback = _checked;
_checked = true;
}
this.readAll(filter, 1, function(err, grid) {
if (err) {
_callback(err);
return;
}
if (grid.numRows() > 0) {
_callback(null, grid.row(0));
return;
}
if (_checked) {
_callback(new Error("Unknown Rec: " + filter));
return;
}
_callback(null, null);
});
};
/**
* Call "read" to query every entity record
* that matches given filter. Clip number of results by "limit" parameter.
* @param {string} filter
* @param {int} limit
* @param {function} callback
* @return {HGrid}
*/
HProj.prototype.readAll = function(filter, limit, callback) {
var _limit = limit;
var _callback = callback;
if (typeof(_limit)==='function') {
_callback = _limit;
_limit = 2147483647;
}
this.onReadAll(filter, _limit, _callback);
};