-
Notifications
You must be signed in to change notification settings - Fork 98
/
table.js
1337 lines (1228 loc) · 34.5 KB
/
table.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var arrify = require('arrify');
var common = require('@google-cloud/common');
var commonGrpc = require('@google-cloud/common-grpc');
var concat = require('concat-stream');
var flatten = require('lodash.flatten');
var is = require('is');
var propAssign = require('prop-assign');
var pumpify = require('pumpify');
var through = require('through2');
var Family = require('./family.js');
var Filter = require('./filter.js');
var Mutation = require('./mutation.js');
var Row = require('./row.js');
const ChunkTransformer = require('./chunktransformer.js');
// See protos/google/rpc/code.proto
// (4=DEADLINE_EXCEEDED, 10=ABORTED, 14=UNAVAILABLE)
const RETRYABLE_STATUS_CODES = new Set([4, 10, 14]);
/**
* Create a Table object to interact with a Cloud Bigtable table.
*
* @class
* @param {Instance} instance Name of the table.
* @param {string} name Name of the table.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*/
function Table(instance, name) {
this.bigtable = instance.bigtable;
this.instance = instance;
var id = Table.formatName_(instance.id, name);
this.id = id;
this.name = id.split('/').pop();
}
/**
* The view to be applied to the returned table's fields.
* Defaults to schema if unspecified.
*
* @private
*/
Table.VIEWS = {
unspecified: 0,
name: 1,
schema: 2,
full: 4,
};
/**
* Formats the table name to include the Bigtable cluster.
*
* @private
*
* @param {string} instanceName The formatted instance name.
* @param {string} name The table name.
*
* @example
* Table.formatName_(
* 'projects/my-project/zones/my-zone/instances/my-instance',
* 'my-table'
* );
* // 'projects/my-project/zones/my-zone/instances/my-instance/tables/my-table'
*/
Table.formatName_ = function(instanceName, name) {
if (name.indexOf('/') > -1) {
return name;
}
return instanceName + '/tables/' + name;
};
/**
* Creates a range based off of a key prefix.
*
* @private
*
* @param {string} start The key prefix/starting bound.
* @returns {object} range
*
* @example
* Table.createPrefixRange_('start');
* // => {
* // start: 'start',
* // end: {
* // value: 'staru',
* // inclusive: false
* // }
* // }
*/
Table.createPrefixRange_ = function(start) {
var prefix = start.replace(new RegExp('[\xff]+$'), '');
var endKey = '';
if (prefix) {
var position = prefix.length - 1;
var charCode = prefix.charCodeAt(position);
var nextChar = String.fromCharCode(charCode + 1);
endKey = prefix.substring(0, position) + nextChar;
}
return {
start: start,
end: {
value: endKey,
inclusive: !endKey,
},
};
};
/**
* Create a table.
*
* @param {object} [options] See {@link Instance#createTable}.
* @param {object} [options.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
*
* @example
* table.create(function(err, table, apiResponse) {
* if (!err) {
* // The table was created successfully.
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.create().then(function(data) {
* var table = data[0];
* var apiResponse = data[1];
* });
*/
Table.prototype.create = function(options, callback) {
if (is.fn(options)) {
callback = options;
options = {};
}
this.instance.createTable(this.name, options, callback);
};
/**
* Create a column family.
*
* Optionally you can send garbage collection rules and when creating a family.
* Garbage collection executes opportunistically in the background, so it's
* possible for reads to return a cell even if it matches the active expression
* for its family.
*
* @see [Garbage Collection Proto Docs]{@link https://github.com/googleapis/googleapis/blob/master/google/bigtable/admin/table/v1/bigtable_table_data.proto#L59}
*
* @throws {error} If a name is not provided.
*
* @param {string} name The name of column family.
* @param {object} [options] Configuration object.
* @param {object} [options.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
* @param {object} [options.rule] Garbage collection rule
* @param {object} [options.rule.age] Delete cells in a column older than the
* given age. Values must be at least 1 millisecond.
* @param {number} [options.rule.versions] Maximum number of versions to delete
* cells in a column, except for the most recent.
* @param {boolean} [options.rule.intersect] Cells to delete should match all
* rules.
* @param {boolean} [options.rule.union] Cells to delete should match any of the
* rules.
* @param {function} callback The callback function.
* @param {?error} callback.err An error returned while making this request.
* @param {Family} callback.family The newly created Family.
* @param {object} callback.apiResponse The full API response.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* const callback = function(err, family, apiResponse) {
* // `family` is a Family object
* };
*
* const rule = {
* age: {
* seconds: 0,
* nanos: 5000
* },
* versions: 3,
* union: true
* };
*
* table.createFamily('follows', rule, callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.createFamily('follows').then(function(data) {
* const family = data[0];
* const apiResponse = data[1];
* });
*/
Table.prototype.createFamily = function(name, options, callback) {
var self = this;
if (is.function(options)) {
callback = options;
options = {};
}
if (!name) {
throw new Error('A name is required to create a family.');
}
var mod = {
id: name,
create: {},
};
if (options.rule) {
mod.create.gcRule = Family.formatRule_(options.rule);
}
var reqOpts = {
name: this.id,
modifications: [mod],
};
this.bigtable.request(
{
client: 'BigtableTableAdminClient',
method: 'modifyColumnFamilies',
reqOpts: reqOpts,
gaxOpts: options.gaxOptions,
},
function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}
var family = self.family(resp.name);
family.metadata = resp;
callback(null, family, resp);
}
);
};
/**
* Get {@link Row} objects for the rows currently in your table as a
* readable object stream.
*
* @param {object} [options] Configuration object.
* @param {boolean} [options.decode=true] If set to `false` it will not decode
* Buffer values returned from Bigtable.
* @param {string} [options.end] End value for key range.
* @param {Filter} [options.filter] Row filters allow you to
* both make advanced queries and format how the data is returned.
* @param {object} [options.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {string[]} [options.keys] A list of row keys.
* @param {number} [options.limit] Maximum number of rows to be returned.
* @param {string} [options.prefix] Prefix that the row key must match.
* @param {object[]} [options.ranges] A list of key ranges.
* @param {string} [options.start] Start value for key range.
* @returns {stream}
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* table.createReadStream()
* .on('error', console.error)
* .on('data', function(row) {
* // `row` is a Row object.
* })
* .on('end', function() {
* // All rows retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing.
* //-
* table.createReadStream()
* .on('data', function(row) {
* this.end();
* });
*
* //-
* // Specify arbitrary keys for a non-contiguous set of rows.
* // The total size of the keys must remain under 1MB, after encoding.
* //-
* table.createReadStream({
* keys: [
* 'alincoln',
* 'gwashington'
* ]
* });
*
* //-
* // Scan for row keys that contain a specific prefix.
* //-
* table.createReadStream({
* prefix: 'gwash'
* });
*
* //-
* // Specify a contiguous range of rows to read by supplying `start` and `end`
* // keys.
* //
* // If the `start` key is omitted, it is interpreted as an empty string.
* // If the `end` key is omitted, it is interpreted as infinity.
* //-
* table.createReadStream({
* start: 'alincoln',
* end: 'gwashington'
* });
*
* //-
* // Specify multiple ranges.
* //-
* table.createReadStream({
* ranges: [{
* start: 'alincoln',
* end: 'gwashington'
* }, {
* start: 'tjefferson',
* end: 'jadams'
* }]
* });
*
* //-
* // Apply a {@link Filter} to the contents of the specified rows.
* //-
* table.createReadStream({
* filter: [
* {
* column: 'gwashington'
* }, {
* value: 1
* }
* ]
* });
*/
Table.prototype.createReadStream = function(options) {
var self = this;
options = options || {};
let maxRetries = is.number(this.maxRetries) ? this.maxRetries : 3;
let rowKeys;
let ranges = options.ranges || [];
let filter;
let rowsLimit;
let rowsRead = 0;
let numRequestsMade = 0;
if (options.start || options.end) {
ranges.push({
start: options.start,
end: options.end,
});
}
if (options.keys) {
rowKeys = options.keys;
}
if (options.prefix) {
ranges.push(Table.createPrefixRange_(options.prefix));
}
if (options.filter) {
filter = Filter.parse(options.filter);
}
if (options.limit) {
rowsLimit = options.limit;
}
const userStream = through.obj();
let chunkTransformer;
const makeNewRequest = () => {
let lastRowKey = chunkTransformer ? chunkTransformer.lastRowKey : '';
chunkTransformer = new ChunkTransformer({decode: options.decode});
var reqOpts = {
tableName: this.id,
appProfileId: this.bigtable.appProfileId,
};
var retryOpts = {
currentRetryAttempt: numRequestsMade,
};
if (lastRowKey) {
const lessThan = (lhs, rhs) => {
const lhsBytes = Mutation.convertToBytes(lhs);
const rhsBytes = Mutation.convertToBytes(rhs);
return lhsBytes.compare(rhsBytes) === -1;
};
const greaterThan = (lhs, rhs) => lessThan(rhs, lhs);
const greaterThanOrEqualTo = (lhs, rhs) => !lessThan(rhs, lhs);
if (ranges.length === 0) {
ranges.push({
start: {
value: lastRowKey,
inclusive: false,
},
});
} else {
// Readjust and/or remove ranges based on previous valid row reads.
// Iterate backward since items may need to be removed.
for (let index = ranges.length - 1; index >= 0; index--) {
const range = ranges[index];
const startValue = is.object(range.start)
? range.start.value
: range.start;
const endValue = is.object(range.end) ? range.end.value : range.end;
const isWithinStart =
!startValue || greaterThanOrEqualTo(startValue, lastRowKey);
const isWithinEnd = !endValue || lessThan(lastRowKey, endValue);
if (isWithinStart) {
if (isWithinEnd) {
// The lastRowKey is within this range, adjust the start value.
range.start = {
value: lastRowKey,
inclusive: false,
};
} else {
// The lastRowKey is past this range, remove this range.
ranges.splice(index, 1);
}
}
}
}
// Remove rowKeys already read.
if (rowKeys) {
rowKeys = rowKeys.filter(rowKey => greaterThan(rowKey, lastRowKey));
if (rowKeys.length === 0) {
rowKeys = null;
}
}
}
if (rowKeys || ranges.length) {
reqOpts.rows = {};
if (rowKeys) {
reqOpts.rows.rowKeys = rowKeys.map(Mutation.convertToBytes);
}
if (ranges.length) {
reqOpts.rows.rowRanges = ranges.map(function(range) {
return Filter.createRange(range.start, range.end, 'Key');
});
}
}
if (filter) {
reqOpts.filter = filter;
}
if (rowsLimit) {
reqOpts.rowsLimit = rowsLimit - rowsRead;
}
const requestStream = this.bigtable.request({
client: 'BigtableClient',
method: 'readRows',
reqOpts: reqOpts,
gaxOpts: options.gaxOptions,
retryOpts: retryOpts,
});
requestStream.on('request', () => numRequestsMade++);
const rowStream = pumpify.obj([
requestStream,
chunkTransformer,
through.obj(function(rowData, enc, next) {
if (chunkTransformer._destroyed || userStream._writableState.ended) {
return next();
}
numRequestsMade = 0;
rowsRead++;
const row = self.row(rowData.key);
row.data = rowData.data;
next(null, row);
}),
]);
rowStream.on('error', error => {
rowStream.unpipe(userStream);
if (
numRequestsMade <= maxRetries &&
RETRYABLE_STATUS_CODES.has(error.code)
) {
makeNewRequest();
} else {
userStream.emit('error', error);
}
});
rowStream.pipe(userStream);
};
makeNewRequest();
return userStream;
};
/**
* Delete the table.
*
* @param {object} [gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} [callback] The callback function.
* @param {?error} callback.err An error returned while making this
* request.
* @param {object} callback.apiResponse The full API response.
*
* @example
* table.delete(function(err, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.delete().then(function(data) {
* var apiResponse = data[0];
* });
*/
Table.prototype.delete = function(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
this.bigtable.request(
{
client: 'BigtableTableAdminClient',
method: 'deleteTable',
reqOpts: {
name: this.id,
},
gaxOpts: gaxOptions,
},
callback
);
};
/**
* Delete all rows in the table, optionally corresponding to a particular
* prefix.
*
* @param {object} [options] Configuration object.
* @param {string} [options.prefix] Row key prefix, when omitted all rows
* will be deleted.
* @param {function} callback The callback function.
* @param {?error} callback.err An error returned while making this request.
* @param {object} callback.apiResponse The full API response.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* //-
* // You can supply a prefix to delete all corresponding rows.
* //-
* const callback = function(err, apiResponse) {
* if (!err) {
* // Rows successfully deleted.
* }
* };
*
* table.deleteRows({
* prefix: 'alincoln'
* }, callback);
*
* //-
* // If you choose to omit the prefix, all rows in the table will be deleted.
* //-
* table.deleteRows(callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.deleteRows().then(function(data) {
* const apiResponse = data[0];
* });
*/
Table.prototype.deleteRows = function(options, callback) {
if (is.function(options)) {
callback = options;
options = {};
}
var reqOpts = {
name: this.id,
};
if (options.prefix) {
reqOpts.rowKeyPrefix = Mutation.convertToBytes(options.prefix);
} else {
reqOpts.deleteAllDataFromTable = true;
}
this.bigtable.request(
{
client: 'BigtableTableAdminClient',
method: 'dropRowRange',
reqOpts: reqOpts,
gaxOpts: options.gaxOptions,
},
callback
);
};
/**
* Check if a table exists.
*
* @param {object} [gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback The callback function.
* @param {?error} callback.err An error returned while making this
* request.
* @param {boolean} callback.exists Whether the table exists or not.
*
* @example
* table.exists(function(err, exists) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.exists().then(function(data) {
* var exists = data[0];
* });
*/
Table.prototype.exists = function(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
this.getMetadata(gaxOptions, function(err) {
if (err) {
if (err.code === 5) {
callback(null, false);
return;
}
callback(err);
return;
}
callback(null, true);
});
};
/**
* Get a reference to a Table Family.
*
* @throws {error} If a name is not provided.
*
* @param {string} name The family name.
* @returns {Family}
*
* @example
* const family = table.family('my-family');
*/
Table.prototype.family = function(name) {
if (!name) {
throw new Error('A family name must be provided.');
}
return new Family(this, name);
};
/**
* Get a table if it exists.
*
* You may optionally use this to "get or create" an object by providing an
* object with `autoCreate` set to `true`. Any extra configuration that is
* normally required for the `create` method must be contained within this
* object as well.
*
* @param {object} [options] Configuration object.
* @param {boolean} [options.autoCreate=false] Automatically create the
* instance if it does not already exist.
* @param {object} [options.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {?error} callback.error An error returned while making this request.
* @param {Table} callback.table The Table object.
* @param {object} callback.apiResponse The resource as it exists in the API.
*
* @example
* table.get(function(err, table, apiResponse) {
* // The `table` data has been populated.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.get().then(function(data) {
* var table = data[0];
* var apiResponse = data[0];
* });
*/
Table.prototype.get = function(options, callback) {
var self = this;
if (is.fn(options)) {
callback = options;
options = {};
}
var autoCreate = !!options.autoCreate;
var gaxOptions = options.gaxOptions;
this.getMetadata({gaxOptions}, function(err, metadata) {
if (err) {
if (err.code === 5 && autoCreate) {
self.create({gaxOptions}, callback);
return;
}
callback(err);
return;
}
callback(null, self, metadata);
});
};
/**
* Get Family objects for all the column familes in your table.
*
* @param {object} [gaxOptions] Request configuration options, outlined here:
* https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback The callback function.
* @param {?error} callback.err An error returned while making this request.
* @param {Family[]} callback.families The list of families.
* @param {object} callback.apiResponse The full API response.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* table.getFamilies(function(err, families, apiResponse) {
* // `families` is an array of Family objects.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.getFamilies().then(function(data) {
* var families = data[0];
* var apiResponse = data[1];
* });
*/
Table.prototype.getFamilies = function(gaxOptions, callback) {
var self = this;
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
this.getMetadata({gaxOptions}, function(err, metadata) {
if (err) {
callback(err);
return;
}
var families = Object.keys(metadata.columnFamilies).map(function(familyId) {
var family = self.family(familyId);
family.metadata = metadata.columnFamilies[familyId];
return family;
});
callback(null, families, metadata.columnFamilies);
});
};
/**
* Get the table's metadata.
*
* @param {object} [options] Table request options.
* @param {object} [options.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {string} [options.view] The view to be applied to the table fields.
* @param {function} [callback] The callback function.
* @param {?error} callback.err An error returned while making this
* request.
* @param {object} callback.metadata The table's metadata.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* table.getMetadata(function(err, metadata) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.getMetadata().then(function(data) {
* var metadata = data[0];
* var apiResponse = data[1];
* });
*/
Table.prototype.getMetadata = function(options, callback) {
var self = this;
if (is.function(options)) {
callback = options;
options = {};
}
var reqOpts = {
name: this.id,
view: Table.VIEWS[options.view || 'unspecified'],
};
this.bigtable.request(
{
client: 'BigtableTableAdminClient',
method: 'getTable',
reqOpts: reqOpts,
gaxOpts: options.gaxOptions,
},
function() {
if (arguments[1]) {
self.metadata = arguments[1];
}
callback.apply(null, arguments);
}
);
};
/**
* Get {@link Row} objects for the rows currently in your table.
*
* This method is not recommended for large datasets as it will buffer all rows
* before returning the results. Instead we recommend using the streaming API
* via {@link Table#createReadStream}.
*
* @param {object} [options] Configuration object. See
* {@link Table#createReadStream} for a complete list of options.
* @param {object} [options.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback The callback function.
* @param {?error} callback.err An error returned while making this request.
* @param {Row[]} callback.rows List of Row objects.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* table.getRows(function(err, rows) {
* if (!err) {
* // `rows` is an array of Row objects.
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.getRows().then(function(data) {
* var rows = data[0];
* });
*/
Table.prototype.getRows = function(options, callback) {
if (is.function(options)) {
callback = options;
options = {};
}
this.createReadStream(options)
.on('error', callback)
.pipe(
concat(function(rows) {
callback(null, rows);
})
);
};
/**
* Insert or update rows in your table. It should be noted that gRPC only allows
* you to send payloads that are less than or equal to 4MB. If you're inserting
* more than that you may need to send smaller individual requests.
*
* @param {object|object[]} entries List of entries to be inserted.
* See {@link Table#mutate}.
* @param {object} [gaxOptions] Request configuration options, outlined here:
* https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback The callback function.
* @param {?error} callback.err An error returned while making this request.
* @param {object[]} callback.err.errors If present, these represent partial
* failures. It's possible for part of your request to be completed
* successfully, while the other part was not.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* const callback = function(err) {
* if (err) {
* // An API error or partial failure occurred.
*
* if (err.name === 'PartialFailureError') {
* // err.errors[].code = 'Response code'
* // err.errors[].message = 'Error message'
* // err.errors[].entry = The original entry
* }
* }
* };
*
* const entries = [
* {
* key: 'alincoln',
* data: {
* follows: {
* gwashington: 1
* }
* }
* }
* ];
*
* table.insert(entries, callback);
*
* //-
* // By default whenever you insert new data, the server will capture a
* // timestamp of when your data was inserted. It's possible to provide a
* // date object to be used instead.
* //-
* const entries = [
* {
* key: 'gwashington',
* data: {
* follows: {
* jadams: {
* value: 1,
* timestamp: new Date('March 22, 2016')
* }
* }
* }
* }
* ];
*
* table.insert(entries, callback);
*
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.insert(entries).then(function() {
* // All requested inserts have been processed.
* });
* //-
*/