1 | 'use strict';
|
2 | var load = require('./load');
|
3 | var insert = require('./insert');
|
4 | var uuid = require('node-uuid');
|
5 | var utils = require('./utils');
|
6 | var Promise = require('bluebird');
|
7 | var checkNew = require('./checkNew');
|
8 | function toBatch(item) {
|
9 | return {
|
10 | value: item,
|
11 | key: item.id
|
12 | };
|
13 | }
|
14 | module.exports = bulk;
|
15 | function bulk (self, inArray, store) {
|
16 | store = store || self.store;
|
17 | return Promise.all(inArray.map(function (item) {
|
18 | return checkNew(self, item.id, item.bbox, store);
|
19 | })).then(function () {
|
20 | var array = load(self, inArray);
|
21 | var inRoot = array[0];
|
22 | if (!self.root) {
|
23 | inRoot.id = 'root';
|
24 | return store.batch(array.map(toBatch)).then(function (resp) {
|
25 | self.root = 'root';
|
26 | return resp;
|
27 | });
|
28 | }
|
29 | return store.get(self.root).then(function (root) {
|
30 | if (root.level === inRoot.level) {
|
31 | root.id = uuid.v4();
|
32 | array.push(root);
|
33 | array.push({
|
34 | id: 'root',
|
35 | children: [
|
36 | {
|
37 | id: root.id,
|
38 | bbox: [root.bbox[0].slice(), root.bbox[1].slice()]
|
39 | },
|
40 | {
|
41 | id: inRoot.id,
|
42 | bbox: [inRoot.bbox[0].slice(), inRoot.bbox[1].slice()]
|
43 | }
|
44 | ],
|
45 | level: root.level + 1,
|
46 | bbox: utils.enlarge(root.bbox, inRoot.bbox)
|
47 | });
|
48 | return store.batch(array.map(toBatch));
|
49 | } else if(root.level < inRoot) {
|
50 | inRoot.id = 'root';
|
51 | root.id = uuid.v4();
|
52 | array.push(root);
|
53 | return store.batch(array.map(toBatch)).then(function () {
|
54 | return insert(self, root.id, root.bbox, 'root', inRoot.level - root.level, store);
|
55 | });
|
56 | } else {
|
57 | return store.batch(array.map(toBatch)).then(function () {
|
58 | return insert(self, inRoot.id, inRoot.bbox, 'root', root.level - inRoot.level, store);
|
59 | });
|
60 | }
|
61 | });
|
62 | });
|
63 | } |
\ | No newline at end of file |