File size: 3,605 Bytes
ae4627d | 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 | var path = require('path')
var fs = require('fs')
var concat = require('concat-stream')
var extend = require('extend')
var request = require('request')
var debug = require('debug')('dat.meta')
module.exports = Meta
function Meta(dat, ready) {
var self = this
if (!(this instanceof Meta)) return new Meta(dat, ready)
if (!ready) ready = function noop() {}
this.dat = dat
this.reserved = ['key', 'change', 'version', 'deleted']
this.rowCount = 0
this.file = path.join(this.dat.dir, '.dat', 'schema.json')
this.read(function(err, json) {
if (err) json = {}
if (!json.columns) json.columns = []
self.json = json
ready(err)
})
}
Meta.prototype.read = function(cb) {
debug('read', this.file)
fs.readFile(this.file, function(err, buf) {
if (err) return cb(err)
cb(null, JSON.parse(buf))
})
}
Meta.prototype.write = function(json, cb) {
var self = this
debug('write', this.file, json)
fs.writeFile(this.file, JSON.stringify(json, null, ' ') + '\n', function(err) {
self.json = json
cb(err)
})
}
Meta.prototype.update = function(json, cb) {
var self = this
self.read(function(err, obj) {
if (err) obj = {}
var updated = extend({}, obj, json)
self.write(updated, cb)
})
}
Meta.prototype.addColumns = function(columns, cb) {
var self = this
if (!(columns instanceof Array)) columns = [columns]
this.json.columns = this.json.columns.concat(columns)
this.update(this.json, cb)
}
Meta.prototype.getNewColumns = function(b, opts) {
if (!opts) opts = { strict: false }
var existing = [].concat(this.json.columns)
// remove reserved columns
var incoming = []
for (var i = 0; i < b.length; i++) {
if (this.reserved.indexOf(b[i]) > -1) continue
incoming.push(b[i])
}
// return early if there aren't any existing columns
if (existing.length === 0) return incoming
existing.sort()
incoming.sort()
var newColumns = []
// strict mode matches based on order. non strict matches based on indexOf
if (opts.strict) {
// loop over all potentially new columns
for (var y = 0; y < incoming.length; y++) {
var newer = incoming[y]
var older = existing[y]
if (y >= existing.length) {
newColumns.push(newer)
} else if (newer === older) {
continue
} else {
var err = {
message: 'Incoming data has different schema than existing data',
type: 'columnMismatch',
existing: existing,
incoming: incoming,
error: true
}
return err
}
}
} else {
for (var y = 0; y < incoming.length; y++) {
var col = incoming[y]
if (existing.indexOf(col) === -1) newColumns.push(col)
}
}
return newColumns
}
Meta.prototype.pullSchema = function(remote, cb) {
var self = this
request({ json: true, url: remote + '/api/package' }, function(err, resp, json) {
if (err || resp.statusCode > 299) {
return cb(err || new Error(resp.statusCode), "Remote schema request failed")
}
var existing = self.dat.schema.toJSON()
var incoming = json.columns
// if they match return early
if (JSON.stringify(existing) === JSON.stringify(incoming)) return cb(null)
var conflict = false
var newColumns = []
var mismatch = new Error('Remote schema mismatch')
if (incoming.length < existing.length) return cb(mismatch)
if (JSON.stringify(existing) !== JSON.stringify(incoming.slice(0, existing.length))) return cb(mismatch)
self.dat.schema.merge(incoming.slice(existing.length), cb)
})
}
|