File: xferhint.ml

package info (click to toggle)
unison2.32.52 2.32.52-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,184 kB
  • ctags: 4,027
  • sloc: ml: 23,058; objc: 4,161; makefile: 514; ansic: 494; sh: 80
file content (115 lines) | stat: -rw-r--r-- 3,818 bytes parent folder | download
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
(* Unison file synchronizer: src/xferhint.ml *)
(* Copyright 1999-2009, Benjamin C. Pierce 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)


let debug = Trace.debug "xferhint"

let xferbycopying =
  Prefs.createBool "xferbycopying" true
    "!optimize transfers using local copies"
    ("When this preference is set, Unison will try to avoid transferring "
   ^ "file contents across the network by recognizing when a file with the "
   ^ "required contents already exists in the target replica.  This usually "
   ^ "allows file moves to be propagated very quickly.  The default value is"
   ^ "\\texttt{true}.  ")

module PathMap =
  Hashtbl.Make
    (struct
       type t = Fspath.t * Path.local
       let hash (fspath, path) =
         (Uutil.hash (Fspath.toString fspath) + 13217 * Uutil.hash path)
           land
         0x3FFFFFFF
       let equal = (=)
     end)
module FPMap =
  Hashtbl.Make
    (struct
       type t = Os.fullfingerprint
       let hash = Uutil.hash
       let equal = (=)
     end)

(* map(path, fingerprint) *)
let path2fingerprintMap =  PathMap.create 101
(* map(fingerprint, path) *)
let fingerprint2pathMap = FPMap.create 101

(*  Now we don't clear it out anymore
let initLocal () =
  debug (fun () -> Util.msg "initLocal\n");
  path2fingerprintMap := PathMap.empty;
  fingerprint2pathMap := FPMap.empty
*)

let lookup fp =
  assert (Prefs.read xferbycopying);
  debug (fun () ->
    Util.msg "lookup: fp = %s\n" (Os.fullfingerprint_to_string fp));
  try
    Some (FPMap.find fingerprint2pathMap fp)
  with Not_found ->
    None

let insertEntry p fp =
  if Prefs.read xferbycopying then begin
    debug (fun () ->
      let (fspath, path) = p in
      Util.msg "insertEntry: fspath=%s, path=%s, fp=%s\n"
        (Fspath.toString fspath)
        (Path.toString path) (Os.fullfingerprint_to_string fp));
    (* Neither of these should be able to raise Not_found *)
    PathMap.replace path2fingerprintMap p fp;
    FPMap.replace fingerprint2pathMap fp p
  end

let deleteEntry p =
  if Prefs.read xferbycopying then begin
    debug (fun () ->
      let (fspath, path) = p in
      Util.msg "deleteEntry: fspath=%s, path=%s\n"
        (Fspath.toString fspath) (Path.toString path));
    try
      let fp = PathMap.find path2fingerprintMap p in
      PathMap.remove path2fingerprintMap p;
      let p' = FPMap.find fingerprint2pathMap fp in
      (* Maybe we should do this unconditionally *)
      if p' = p then FPMap.remove fingerprint2pathMap fp
    with Not_found ->
      ()
  end
      
let renameEntry pOrig pNew =
  if Prefs.read xferbycopying then begin
    debug (fun () ->
      let (fspathOrig, pathOrig) = pOrig in
      let (fspathNew, pathNew) = pNew in
      Util.msg "renameEntry: fsOrig=%s, pOrig=%s, fsNew=%s, pNew=%s\n"
        (Fspath.toString fspathOrig) (Path.toString pathOrig)
        (Fspath.toString fspathNew) (Path.toString pathNew));
    try
      let fp = PathMap.find path2fingerprintMap pOrig in
      PathMap.remove path2fingerprintMap pOrig;
      PathMap.replace path2fingerprintMap pNew fp;
      FPMap.replace fingerprint2pathMap fp pNew
    with Not_found ->
      ()
  end

let _ =
  Os.initializeXferFunctions deleteEntry renameEntry