Skip to content

Commit

Permalink
Restructure cache search
Browse files Browse the repository at this point in the history
  • Loading branch information
dullgiulio committed Apr 7, 2015
1 parent 71a3d4a commit c8d062a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 45 deletions.
76 changes: 35 additions & 41 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package main
import (
"net/mail"
"sort"
"time"
)

type cacheString map[string]mailFiles

type caches struct {
str map[string]cacheString
data map[string]cacheString
cancelCh chan struct{}
mailCh chan cacheMail
requestCh chan cacheRequest
Expand All @@ -18,12 +17,12 @@ type caches struct {
}

type cacheRequest struct {
name string
time time.Time
str string
submatch bool
lower bool
data chan mailFiles
header string
value string
index int
limit int
oldest bool
data chan mailFiles
}

type cacheEntry struct {
Expand All @@ -45,7 +44,7 @@ func newCacheRequest() *cacheRequest {

func newCaches(root string) *caches {
return &caches{
str: make(map[string]cacheString),
data: make(map[string]cacheString),
cancelCh: make(chan struct{}),
requestCh: make(chan cacheRequest),
addCh: make(chan cacheEntry),
Expand All @@ -54,40 +53,24 @@ func newCaches(root string) *caches {
}

func (c *caches) initCachesString(name string) {
c.str[name] = make(map[string]mailFiles)
c.data[name] = make(map[string]mailFiles)
}

func (c *caches) add(entry cacheEntry) {
name, key, value := entry.name, entry.key, entry.value

if _, found := c.str[name][key]; !found {
c.str[name][key] = newMailFiles()
if _, found := c.data[name][key]; !found {
c.data[name][key] = newMailFiles()
}

c.str[name][key] = append(c.str[name][key], value)
}

func (c *caches) getString(name string, key string) mailFiles {
return c.str[name][key]
}

func (c *caches) getKeysString(name string) []string {
keys := make([]string, 0)

for k := range c.str[name] {
keys = append(keys, k)
}

sort.Strings(keys)

return keys
c.data[name][key] = append(c.data[name][key], value)
}

func (c *caches) remove(files mailFiles) {
for name := range c.str {
for k := range c.str[name] {
sort.Sort(c.str[name][k])
c.str[name][k] = sliceDiff(c.str[name][k], files)
for name := range c.data {
for k := range c.data[name] {
sort.Sort(c.data[name][k])
c.data[name][k] = sliceDiff(c.data[name][k], files)
}
}
}
Expand All @@ -106,15 +89,26 @@ func (c *caches) run() {
case <-c.cancelCh:
return
case r := <-c.requestCh:
if r.str == "" {
r.data <- nil
continue
}
if cache, found := c.data[r.header]; found {
ids := cache[r.value]
if r.limit == 0 {
r.limit = len(ids)
}

// Copy only elements between r.index and r.index + r.limit
result := mailFiles(make([]mailFile, r.limit))
for i, j := 0, r.index; i < r.limit; i, j = i+1, j+1 {
result[i] = ids[j]
}

// Invert result. This assumes the result is ordered by date
// TODO: Not sure this is already ordered by date
if r.oldest {
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
}

if cache, found := c.str[r.name]; found {
ids := cache[r.str]
result := mailFiles(make([]mailFile, len(ids)))
copy(result, ids)
r.data <- result
continue
}
Expand Down
4 changes: 4 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (m mailFile) String() string {
}

func (m mailFile) WriteTo(w io.Writer) error {
file := m.String()
if file == "" {
return nil
}
r, err := os.Open(m.String())
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (m *mailIndexer) cacheEntries(file mailFile, msg *mail.Message) []cacheEntr
}

switch key {
case "":
entries = append(entries, cacheEntry{
name: "", key: "", value: file,
})
case "to", "from":
if addresses, err := headers.AddressList(headerKey); err == nil {
for _, a := range addresses {
Expand Down
10 changes: 6 additions & 4 deletions perso.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (

func findCached(header, value string, cache *caches) {
cacheReq := cacheRequest{
name: "to",
str: value,
data: make(chan mailFiles),
header: "to",
value: value,
index: 2,
limit: 1,
data: make(chan mailFiles),
}

cache.requestCh <- cacheReq
Expand All @@ -24,7 +26,7 @@ func main() {
email := flag.Arg(0)
root := flag.Arg(1)

indexKeys := []string{"from", "to"}
indexKeys := []string{"", "from", "to"}

caches := newCaches(root)
for i := range indexKeys {
Expand Down
14 changes: 14 additions & 0 deletions urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

/*
Example URLs:
/latest/1
/from/[email protected]
/from/[email protected]/latest/1
/oldest/1
/from/[email protected]/oldest
/from/[email protected]/oldest/1
*/

0 comments on commit c8d062a

Please sign in to comment.