Skip to content

Commit

Permalink
Add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Aug 25, 2017
1 parent fbff127 commit ac3f40a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 36 deletions.
52 changes: 52 additions & 0 deletions src/cache.go
@@ -0,0 +1,52 @@
package gojot

import (
"encoding/json"
"io/ioutil"
"path"
)

type DocCache struct {
Docs Documents
LoadedFiles map[string]bool
DocumentAndEntryNames map[string]map[string]bool
}

func (gj *gojot) SaveDocCache() (err error) {
cache := DocCache{
Docs: gj.docs,
LoadedFiles: gj.loadedFiles,
DocumentAndEntryNames: gj.documentAndEntryNames,
}
bCache, err := json.Marshal(cache)
if err != nil {
return
}
enc, err := gj.gpg.Encrypt(bCache)
if err != nil {
return
}
err = ioutil.WriteFile(path.Join(gj.root, "cache.json"), enc, 0755)
return
}

func (gj *gojot) LoadDocCache() (err error) {
var cache DocCache
bCache, err := ioutil.ReadFile(path.Join(gj.root, "cache.json"))
if err != nil {
return
}
dec, err := gj.gpg.Decrypt(bCache)
if err != nil {
return
}
err = json.Unmarshal(dec, &cache)
if err != nil {
return
}
gj.docs = cache.Docs
gj.loadedFiles = cache.LoadedFiles
gj.documentAndEntryNames = cache.DocumentAndEntryNames
gj.log.Infof("Loaded %d files from cache", len(gj.loadedFiles))
return
}
25 changes: 20 additions & 5 deletions src/gojot.go
Expand Up @@ -32,6 +32,7 @@ type gojot struct {
root string
docs Documents
documentAndEntryNames map[string]map[string]bool
loadedFiles map[string]bool
repo *gogit.GitRepo
gpg *gogpg.GPGStore
logger *logrus.Logger
Expand Down Expand Up @@ -76,6 +77,9 @@ func New(debug ...bool) (gj *gojot, err error) {
return
}

gj.docs = Documents{}
gj.loadedFiles = make(map[string]bool)
gj.documentAndEntryNames = make(map[string]map[string]bool)
gj.Debug(gj.debug)
return
}
Expand Down Expand Up @@ -353,6 +357,12 @@ func (gj *gojot) NewConfig(overrideIdentityPassword ...string) (err error) {
}

func (gj *gojot) LoadRepo() (err error) {
if exists(path.Join(gj.root, "cache.json")) {
err = gj.LoadDocCache()
if err != nil {
return
}
}
filelist := []string{}
filepath.Walk(gj.root, func(fp string, fi os.FileInfo, err error) error {
if err != nil {
Expand All @@ -367,10 +377,13 @@ func (gj *gojot) LoadRepo() (err error) {
}
if matched {
_, file := filepath.Split(fp)
// 36 = 32 character hash + .asc
// this ensures only actual files go in
if len(file) == 36 {
// 36 = 32 character hash + .asc
// this ensures only actual files go in
filelist = append(filelist, fp)
// ensure it wasn't already loaded (from Cache)
if _, ok := gj.loadedFiles[fp]; !ok {
filelist = append(filelist, fp)
}
}
}
return nil
Expand All @@ -380,22 +393,24 @@ func (gj *gojot) LoadRepo() (err error) {
return err
}

gj.docs = make(Documents, 0, len(data))
gj.documentAndEntryNames = make(map[string]map[string]bool)
for filename := range data {
parsedDocs, err2 := gj.ParseDocuments(data[filename])
if err2 != nil {
err = err2
return
}
gj.docs = append(gj.docs, parsedDocs[0])
gj.loadedFiles[filename] = true
if _, ok := gj.documentAndEntryNames[parsedDocs[0].Front.Document]; !ok {
gj.documentAndEntryNames[parsedDocs[0].Front.Document] = make(map[string]bool)
}
gj.documentAndEntryNames[parsedDocs[0].Front.Document][parsedDocs[0].Front.Entry] = true
}
// gj.log.Infof("%+v", gj.documentAndEntryNames)
sort.Sort(gj.docs)

// Save cache
err = gj.SaveDocCache()
return
}

Expand Down
23 changes: 0 additions & 23 deletions src/gojot_test.go
Expand Up @@ -49,26 +49,3 @@ func TestGojotGeneralOnNewRepo(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, true, strings.Contains(repos["https://github.com/schollz/demo2.git"], ".cache/gojot2/demo2"))
}
func TestGojotGeneralOnOldRepo(t *testing.T) {
os.RemoveAll(path.Join(cacheFolder, "demo3"))
gj, err := New(true)
assert.Nil(t, err)

err = gj.SetRepo("https://github.com/schollz/demo3.git")
id := "Testy McTestFace"
passphrase := "1234"
err = gj.LoadConfig(id, passphrase)
assert.Nil(t, err)

assert.Equal(t, "Testy McTestFace", gj.config.Identity)
assert.Equal(t, 4, strings.Count(gj.config.Salt, "-"))

err = gj.LoadRepo()
assert.Nil(t, err)

err = gj.SaveDocuments(gj.docs)
assert.Nil(t, err)
err = gj.Push()
assert.Nil(t, err)

}
16 changes: 8 additions & 8 deletions src/run.go
Expand Up @@ -9,7 +9,7 @@ import (
func Run() (err error) {
// TODO: Unbundle vim

gj, err := New(false)
gj, err := New(true)
if err != nil {
return
}
Expand Down Expand Up @@ -64,13 +64,13 @@ func Run() (err error) {
}
}

fmt.Print("Pushing...")
err = gj.Push()
if err == nil {
fmt.Println("...done.")
} else {
fmt.Println("...failed.")
}
// fmt.Print("Pushing...")
// err = gj.Push()
// if err == nil {
// fmt.Println("...done.")
// } else {
// fmt.Println("...failed.")
// }

return
}

0 comments on commit ac3f40a

Please sign in to comment.