]> gitweb.factorcode.org Git - factor-unmaintained.git/blob - forestdb/paths/paths.factor
forestdb: not a maintained db
[factor-unmaintained.git] / forestdb / paths / paths.factor
1 ! Copyright (C) 2014 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators.short-circuit combinators.smart
4 io.directories io.pathnames kernel math math.parser sequences
5 sorting sorting.human splitting sets ;
6 IN: forestdb.paths
7
8 CONSTANT: fdb-filename-base "fq"
9
10 : fdb-filename? ( path -- ? )
11     "." split {
12         [ length 3 = ]
13         [ second fdb-filename-base = ]
14         [ first string>number ]
15         [ third string>number ]
16     } 1&& ;
17
18 ERROR: not-an-fdb-filename string ;
19
20 : ensure-fdb-filename ( string -- string )
21     dup fdb-filename? [ not-an-fdb-filename ] unless ;
22
23 ERROR: not-a-string-number string ;
24
25 : ?string>number ( string -- n )
26     dup string>number [ ] [ not-a-string-number ] ?if ;
27
28 : change-string-number ( string quot -- string' )
29     [ [ string>number ] dip call number>string ] 2keep drop
30     length CHAR: 0 pad-head ; inline
31
32 : next-vnode-name ( string -- string' )
33     [
34         "." split
35         first [ 1 + ] change-string-number
36         "." fdb-filename-base ".0"
37     ] "" append-outputs-as ;
38
39 : trim-head-zeros ( string -- string' )
40     [ CHAR: 0 = ] trim-head 1 CHAR: 0 pad-head ;
41
42 : canonical-fdb-name ( string -- string' )
43     ensure-fdb-filename
44     "." split first3
45     [ trim-head-zeros ]
46     [ ]
47     [ trim-head-zeros ] tri* 3array "." join ;
48
49 : next-vnode-version-name ( string -- string' )
50     "." split
51     [ but-last "." join ]
52     [ last [ 1 + ] change-string-number ] bi "." glue ;
53
54 : path>next-vnode-path ( path -- path' )
55     dup directory-files
56     [ fdb-filename? ] filter
57     [ human<=> ] sort [
58         fdb-filename-base "0." ".0" surround append-path
59     ] [
60         last "." split first [ 1 + ] change-string-number
61         ".fq.0" append append-path
62     ] if-empty ;
63
64 : path-has-fdb? ( path -- ? )
65     directory-files [ fdb-filename? ] filter length 0 > ;
66
67 : path-only-fdb? ( path -- ? )
68     directory-files
69     [ length ]
70     [ [ fdb-filename? ] filter length ] bi = ;
71
72 : path-fdb-duplicates ( path -- seq )
73     directory-files [ canonical-fdb-name ] map members ;
74
75 ! : path>next-vnode-version-name ( path -- path' )
76     ! [ file-name ]