| 1 |
|
| 2 |
<!DOCTYPE html> |
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 |
<style> |
| 7 |
body { |
| 8 |
background: black; |
| 9 |
color: rgb(80, 80, 80); |
| 10 |
} |
| 11 |
body, pre, #legend span { |
| 12 |
font-family: Menlo, monospace; |
| 13 |
font-weight: bold; |
| 14 |
} |
| 15 |
#topbar { |
| 16 |
background: black; |
| 17 |
position: fixed; |
| 18 |
top: 0; left: 0; right: 0; |
| 19 |
height: 42px; |
| 20 |
border-bottom: 1px solid rgb(80, 80, 80); |
| 21 |
} |
| 22 |
#content { |
| 23 |
margin-top: 50px; |
| 24 |
} |
| 25 |
#nav, #legend { |
| 26 |
float: left; |
| 27 |
margin-left: 10px; |
| 28 |
} |
| 29 |
#legend { |
| 30 |
margin-top: 12px; |
| 31 |
} |
| 32 |
#nav { |
| 33 |
margin-top: 10px; |
| 34 |
} |
| 35 |
#legend span { |
| 36 |
margin: 0 5px; |
| 37 |
} |
| 38 |
.cov0 { color: rgb(192, 0, 0) } |
| 39 |
.cov1 { color: rgb(128, 128, 128) } |
| 40 |
.cov2 { color: rgb(116, 140, 131) } |
| 41 |
.cov3 { color: rgb(104, 152, 134) } |
| 42 |
.cov4 { color: rgb(92, 164, 137) } |
| 43 |
.cov5 { color: rgb(80, 176, 140) } |
| 44 |
.cov6 { color: rgb(68, 188, 143) } |
| 45 |
.cov7 { color: rgb(56, 200, 146) } |
| 46 |
.cov8 { color: rgb(44, 212, 149) } |
| 47 |
.cov9 { color: rgb(32, 224, 152) } |
| 48 |
.cov10 { color: rgb(20, 236, 155) } |
| 49 |
|
| 50 |
</style> |
| 51 |
</head> |
| 52 |
<body> |
| 53 |
<div id="topbar"> |
| 54 |
<div id="nav"> |
| 55 |
<select id="files"> |
| 56 |
|
| 57 |
<option value="file0">github.com\twinj\uuid\savers\filesystem.go (100.0%)</option> |
| 58 |
|
| 59 |
</select> |
| 60 |
</div> |
| 61 |
<div id="legend"> |
| 62 |
<span>not tracked</span> |
| 63 |
|
| 64 |
<span class="cov0">not covered</span> |
| 65 |
<span class="cov8">covered</span> |
| 66 |
|
| 67 |
</div> |
| 68 |
</div> |
| 69 |
<div id="content"> |
| 70 |
|
| 71 |
<pre class="file" id="file0" style="display: none">package savers |
| 72 |
|
| 73 |
/**************** |
| 74 |
* Date: 30/05/16 |
| 75 |
* Time: 5:48 PM |
| 76 |
***************/ |
| 77 |
|
| 78 |
import ( |
| 79 |
"encoding/gob" |
| 80 |
"github.com/twinj/uuid" |
| 81 |
"log" |
| 82 |
"os" |
| 83 |
"path" |
| 84 |
"time" |
| 85 |
) |
| 86 |
|
| 87 |
var _ uuid.Saver = &FileSystemSaver{} |
| 88 |
|
| 89 |
// This implements the Saver interface for UUIDs |
| 90 |
type FileSystemSaver struct { |
| 91 |
// A file to save the state to |
| 92 |
// Used gob format on uuid.State entity |
| 93 |
file *os.File |
| 94 |
|
| 95 |
// Preferred location for the store |
| 96 |
Path string |
| 97 |
|
| 98 |
// Whether to log each save |
| 99 |
Report bool |
| 100 |
|
| 101 |
// The amount of time between each save call |
| 102 |
time.Duration |
| 103 |
|
| 104 |
// The next time to save |
| 105 |
uuid.Timestamp |
| 106 |
} |
| 107 |
|
| 108 |
func (o *FileSystemSaver) Save(pStore *uuid.Store) <span class="cov8" title="1">{ |
| 109 |
|
| 110 |
if pStore.Timestamp >= o.Timestamp </span><span class="cov8" title="1">{ |
| 111 |
err := o.openAndDo(o.encode, pStore) |
| 112 |
if err == nil </span><span class="cov8" title="1">{ |
| 113 |
if o.Report </span><span class="cov8" title="1">{ |
| 114 |
log.Printf("UUID Saved State Storage: %s", pStore) |
| 115 |
}</span> |
| 116 |
} |
| 117 |
<span class="cov8" title="1">o.Timestamp = pStore.Add(o.Duration)</span> |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func (o *FileSystemSaver) Read() (err error, store uuid.Store) <span class="cov8" title="1">{ |
| 122 |
store = uuid.Store{} |
| 123 |
gob.Register(&uuid.Store{}) |
| 124 |
|
| 125 |
if _, err = os.Stat(o.Path); os.IsNotExist(err) </span><span class="cov8" title="1">{ |
| 126 |
dir, file := path.Split(o.Path) |
| 127 |
if dir == "" || dir == "/" </span><span class="cov8" title="1">{ |
| 128 |
dir = os.TempDir() |
| 129 |
}</span> |
| 130 |
<span class="cov8" title="1">o.Path = path.Join(dir, file) |
| 131 |
|
| 132 |
err = os.MkdirAll(dir, os.ModeDir | 0755) |
| 133 |
if err == nil </span><span class="cov8" title="1">{ |
| 134 |
// If new encode blank store |
| 135 |
err = o.openAndDo(o.encode, &store) |
| 136 |
if err == nil </span><span class="cov8" title="1">{ |
| 137 |
log.Println("uuid.FileSystemSaver created", o.Path) |
| 138 |
}</span> |
| 139 |
} |
| 140 |
<span class="cov8" title="1">log.Println("uuid.FileSystemSaver.Read: error will autogenerate", err) |
| 141 |
return</span> |
| 142 |
} |
| 143 |
|
| 144 |
<span class="cov8" title="1">o.openAndDo(o.decode, &store) |
| 145 |
return</span> |
| 146 |
} |
| 147 |
|
| 148 |
func (o *FileSystemSaver) openAndDo(fDo func (*uuid.Store), pStore *uuid.Store) (err error) <span class="cov8" title="1">{ |
| 149 |
o.file, err = os.OpenFile(o.Path, os.O_RDWR|os.O_CREATE, os.ModeExclusive) |
| 150 |
defer o.file.Close() |
| 151 |
if err == nil </span><span class="cov8" title="1">{ |
| 152 |
fDo(pStore) |
| 153 |
}</span><span class="cov8" title="1"> else { |
| 154 |
log.Println("uuid.FileSystemSaver.openAndDo error:", err) |
| 155 |
}</span> |
| 156 |
<span class="cov8" title="1">return</span> |
| 157 |
} |
| 158 |
|
| 159 |
func (o *FileSystemSaver) encode(pStore *uuid.Store) <span class="cov8" title="1">{ |
| 160 |
// ensure reader state is ready for use |
| 161 |
enc := gob.NewEncoder(o.file) |
| 162 |
// swallow error for encode as its only for cyclic pointers |
| 163 |
enc.Encode(pStore) |
| 164 |
}</span> |
| 165 |
|
| 166 |
func (o *FileSystemSaver) decode(pStore *uuid.Store) <span class="cov8" title="1">{ |
| 167 |
// ensure reader state is ready for use |
| 168 |
o.file.Seek(0, 0) |
| 169 |
dec := gob.NewDecoder(o.file) |
| 170 |
// swallow error for encode as its only for cyclic pointers |
| 171 |
dec.Decode(pStore) |
| 172 |
}</span> |
| 173 |
</pre> |
| 174 |
|
| 175 |
</div> |
| 176 |
</body> |
| 177 |
<script> |
| 178 |
(function() { |
| 179 |
var files = document.getElementById('files'); |
| 180 |
var visible; |
| 181 |
files.addEventListener('change', onChange, false); |
| 182 |
function select(part) { |
| 183 |
if (visible) |
| 184 |
visible.style.display = 'none'; |
| 185 |
visible = document.getElementById(part); |
| 186 |
if (!visible) |
| 187 |
return; |
| 188 |
files.value = part; |
| 189 |
visible.style.display = 'block'; |
| 190 |
location.hash = part; |
| 191 |
} |
| 192 |
function onChange() { |
| 193 |
select(files.value); |
| 194 |
window.scrollTo(0, 0); |
| 195 |
} |
| 196 |
if (location.hash != "") { |
| 197 |
select(location.hash.substr(1)); |
| 198 |
} |
| 199 |
if (!visible) { |
| 200 |
select("file0"); |
| 201 |
} |
| 202 |
})(); |
| 203 |
</script> |
| 204 |
</html> |