From f5f268dff727e1f84306fd4777222e0021b481f9 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Mon, 6 Feb 2023 23:03:15 -0800 Subject: [PATCH] atart work on caching repos --- commits/cache.go | 22 ++++++++++++++++++++++ types/types.go | 15 +++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/commits/cache.go b/commits/cache.go index d36f03d..ded46a2 100644 --- a/commits/cache.go +++ b/commits/cache.go @@ -14,10 +14,32 @@ import ( var ( mapTex sync.RWMutex hashCache map[int]map[string]map[string]types.ExpFreq + // the Repo Cache holds a list of all commits from HEAD back to parent + // the key is the repo path + // if the hash of the first commit / HEAD commit doesn't match the current HEAD, + // then it can be discarded and reloaded + repoCache map[string][]types.Commit ) func init() { hashCache = make(map[int]map[string]map[string]types.ExpFreq) + repoCache = make(map[string][]types.Commit) +} + +func IsRepoCached(path string, head string) bool { + mapTex.RLock() + defer mapTex.RUnlock() + if commits, ok := repoCache[path]; !ok { + return false + } else { + return len(commits) > 0 && commits[0].Hash == head + } +} + +func CacheRepo(path string, commits []types.Commit) { + mapTex.Lock() + defer mapTex.Unlock() + repoCache[path] = commits } func hashSlice(in []string) string { diff --git a/types/types.go b/types/types.go index c9d4087..0f9dbc8 100644 --- a/types/types.go +++ b/types/types.go @@ -7,12 +7,15 @@ import ( type ( Month string Commit struct { - LOC int `json:"loc,omitempty"` - Message string `json:"message,omitempty"` - TimeStamp time.Time `json:"ts,omitempty"` - Author string `json:"author,omitempty"` - Repo string `json:"repo,omitempty"` - Path string `json:"path,omitempty"` + Deleted int `json:"deleted,omitempty"` + Added int `json:"added,omitempty"` + FilesChanged int `json:"files_changed,omitempty"` + Message string `json:"message,omitempty"` + Hash string `json:"hash,omitempty"` + TimeStamp time.Time `json:"ts,omitempty"` + Author string `json:"author,omitempty"` + Repo string `json:"repo,omitempty"` + Path string `json:"path,omitempty"` } DataSet map[time.Time]WorkDay Freq []int