MCPcopy Index your code
hub / github.com/totaljs/framework / updDocument

Function updDocument

graphdb.js:1164–1248  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1162}
1163
1164function updDocument(self) {
1165
1166 if (!self.ready || self.states.update || !self.pending.update.length || F.isKilled)
1167 return;
1168
1169 var upd = self.pending.update.shift();
1170 if (upd) {
1171 self.states.update = true;
1172
1173 var offset = offsetDocument(self, upd.id);
1174 var buf = U.createBufferSize(self.header.documentsize);
1175
1176 Fs.read(self.fd, buf, 0, buf.length, offset, function(err, size) {
1177
1178 if (err) {
1179 self.states.update = false;
1180 upd.callback(err);
1181 setImmediate(self.cb_next, NEXT_UPDATE);
1182 return;
1183 }
1184
1185 if (!size) {
1186 upd.callback(null, 0);
1187 self.states.update = false;
1188 setImmediate(self.cb_next, NEXT_UPDATE);
1189 return;
1190 }
1191
1192 var save = function(err) {
1193 self.states.update = false;
1194 !err && Fs.write(self.fd, buf, 0, buf.length, offset, self.cb_error);
1195 upd.callback(err, err ? 0 : 1);
1196 setImmediate(self.cb_next, NEXT_UPDATE);
1197 };
1198
1199 var data = buf.slice(DATAOFFSET, buf.readUInt16LE(15) + DATAOFFSET);
1200 var limit = self.header.documentsize - DATAOFFSET;
1201 var schema = self.$classes[buf[1]].schema;
1202 var doc;
1203
1204 if (buf[2] === STATE_COMPRESSED) {
1205 Zlib.inflate(data, ZLIBOPTIONS, function(err, buffer) {
1206 doc = parseData(schema, buffer.toString('utf8').split('|'));
1207 buffer = U.createBuffer(stringifyData(schema, upd.fn(doc, upd.value)));
1208 if (buffer.length > limit) {
1209 Zlib.deflate(buffer, ZLIBOPTIONS, function(err, buffer) {
1210 if (buffer.length <= limit) {
1211 buf.writeUInt16LE(buffer.length, 15);
1212 buf.writeUInt8(STATE_COMPRESSED, 2);
1213 buffer.copy(buf, DATAOFFSET);
1214 save();
1215 } else
1216 save(new Error('GraphDB: Data too long'));
1217 });
1218 } else {
1219 buf.writeUInt16LE(buffer.length, 15);
1220 buf.writeUInt8(STATE_UNCOMPRESSED, 2);
1221 buffer.copy(buf, DATAOFFSET);

Callers 1

graphdb.jsFile · 0.85

Calls 4

offsetDocumentFunction · 0.85
parseDataFunction · 0.85
stringifyDataFunction · 0.85
saveFunction · 0.85

Tested by

no test coverage detected