@@ -9,6 +9,13 @@ const CompileState = require('./object_factory').CompileState;
9
9
const PatternGraphDot = require ( './pattern_graph_dot' ) ;
10
10
const PatternRegistry = require ( './pattern_registry' ) ;
11
11
12
+ /**
13
+ * The most recent version of the pattern graph. This is used to rebuild the graph when
14
+ * the version of a serialized graph does not match the current version.
15
+ * @type {number }
16
+ */
17
+ const PATTERN_GRAPH_VERSION = 1 ;
18
+
12
19
/**
13
20
* Wrapper around a graph library to build a dependency graph of patterns.
14
21
* Each node in the graph will maintain a {@link CompileState}. This allows finding all
@@ -21,13 +28,14 @@ const PatternRegistry = require('./pattern_registry');
21
28
*
22
29
* @param {Graph } graph The graphlib graph object
23
30
* @param {int } timestamp The unix timestamp
31
+ * @param {int } version The graph version.
24
32
*
25
33
* @returns {{PatternGraph: PatternGraph} }
26
34
27
35
* @see PatternGraph#fromJson
28
36
* @see <a href="https://github.com/pattern-lab/patternlab-node/issues/540">#540</a>
29
37
*/
30
- const PatternGraph = function ( graph , timestamp ) {
38
+ const PatternGraph = function ( graph , timestamp , version ) {
31
39
32
40
this . graph = graph || new Graph ( {
33
41
directed : true
@@ -38,6 +46,7 @@ const PatternGraph = function (graph, timestamp) {
38
46
// The idea here is to make a pattern known to the graph as soon as it exists
39
47
this . patterns = new PatternRegistry ( ) ;
40
48
this . timestamp = timestamp || new Date ( ) . getTime ( ) ;
49
+ this . version = version || PATTERN_GRAPH_VERSION ;
41
50
} ;
42
51
43
52
// shorthand. Use relPath as it is always unique, even with subPatternType
@@ -53,7 +62,7 @@ PatternGraph.prototype = {
53
62
clone : function ( ) {
54
63
const json = graphlib . json . write ( this . graph ) ;
55
64
const graph = graphlib . json . read ( json ) ;
56
- return new PatternGraph ( graph , this . timestamp ) ;
65
+ return new PatternGraph ( graph , this . timestamp , this . version ) ;
57
66
} ,
58
67
59
68
/**
@@ -267,6 +276,7 @@ PatternGraph.prototype = {
267
276
*/
268
277
toJson : function ( ) {
269
278
return {
279
+ version : this . version ,
270
280
timestamp : this . timestamp ,
271
281
graph : graphlib . json . write ( this . graph )
272
282
} ;
@@ -277,26 +287,56 @@ PatternGraph.prototype = {
277
287
*/
278
288
nodes : function ( ) {
279
289
return this . graph . nodes ( ) ;
290
+ } ,
291
+
292
+ /**
293
+ * Updates the version to the most recent one
294
+ */
295
+ upgradeVersion : function ( ) {
296
+ this . version = PATTERN_GRAPH_VERSION ;
280
297
}
281
298
} ;
282
299
283
300
/**
284
301
* Creates an empty graph with a unix timestamp of 0 as last compilation date.
285
- *
302
+ * @param { int } [version=PATTERN_GRAPH_VERSION]
286
303
* @return {PatternGraph }
287
304
*/
288
- PatternGraph . empty = function ( ) {
289
- return new PatternGraph ( null , 0 ) ;
305
+ PatternGraph . empty = function ( version ) {
306
+ return new PatternGraph ( null , 0 , version || PATTERN_GRAPH_VERSION ) ;
307
+ } ;
308
+
309
+ /**
310
+ * Checks if the version of
311
+ * @param {PatternGraph|Object } graphOrJson
312
+ * @return {boolean }
313
+ */
314
+ PatternGraph . checkVersion = function ( graphOrJson ) {
315
+ return graphOrJson . version === PATTERN_GRAPH_VERSION ;
290
316
} ;
291
317
318
+ /**
319
+ * Error that is thrown if the given version does not match the current graph version.
320
+ *
321
+ * @param oldVersion
322
+ * @constructor
323
+ */
324
+ function VersionMismatch ( oldVersion ) {
325
+ this . message = `Version of graph on disk ${ oldVersion } != current version ${ PATTERN_GRAPH_VERSION } . Please clean your patterns output directory.` ;
326
+ this . name = "VersionMismatch" ;
327
+ }
328
+
292
329
/**
293
330
* Parse the graph from a JSON object.
294
331
* @param {object } o The JSON object to read from
295
332
* @return {PatternGraph }
296
333
*/
297
334
PatternGraph . fromJson = function ( o ) {
335
+ if ( ! PatternGraph . checkVersion ( o ) ) {
336
+ throw new VersionMismatch ( o . version ) ;
337
+ }
298
338
const graph = graphlib . json . read ( o . graph ) ;
299
- return new PatternGraph ( graph , o . timestamp ) ;
339
+ return new PatternGraph ( graph , o . timestamp , o . version ) ;
300
340
} ;
301
341
302
342
/**
@@ -328,6 +368,9 @@ PatternGraph.loadFromFile = function (patternlab, file) {
328
368
}
329
369
330
370
const obj = fs . readJSONSync ( jsonGraphFile ) ;
371
+ if ( ! PatternGraph . checkVersion ( obj ) ) {
372
+ return PatternGraph . empty ( obj . version )
373
+ }
331
374
return this . fromJson ( obj ) ;
332
375
} ;
333
376
@@ -356,5 +399,6 @@ PatternGraph.exportToDot = function (patternlab, file) {
356
399
} ;
357
400
358
401
module . exports = {
359
- PatternGraph : PatternGraph
402
+ PatternGraph : PatternGraph ,
403
+ PATTERN_GRAPH_VERSION : PATTERN_GRAPH_VERSION
360
404
} ;
0 commit comments