aboutsummaryrefslogtreecommitdiff
path: root/kamus_cache.go
diff options
context:
space:
mode:
Diffstat (limited to 'kamus_cache.go')
-rw-r--r--kamus_cache.go178
1 files changed, 0 insertions, 178 deletions
diff --git a/kamus_cache.go b/kamus_cache.go
deleted file mode 100644
index 52eb1f4..0000000
--- a/kamus_cache.go
+++ /dev/null
@@ -1,178 +0,0 @@
-// Copyright 2020, Shulhan <m.shulhan@gmail.com>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package kamusku
-
-import (
- "bytes"
- "encoding/gob"
- "errors"
- "io/ioutil"
- "log"
- "os"
- "sync"
-
- libio "github.com/shuLhan/share/lib/io"
-)
-
-const (
- defStorageName = "kamus.gob"
-)
-
-//
-// kamusCache contains cache of words and its definitions.
-//
-type kamusCache struct {
- sync.Mutex
- cache map[string]*Kata
- lastSize int
-
- storagePath string
-}
-
-//
-// newKamusCache create and initialize the cache for dictionary.
-//
-func newKamusCache(storagePath string) (kamusc *kamusCache, err error) {
- if len(storagePath) == 0 {
- storagePath = defStorageName
- }
-
- kamusc = &kamusCache{
- cache: make(map[string]*Kata),
- storagePath: storagePath,
- }
-
- err = kamusc.load()
- if err != nil {
- return nil, err
- }
-
- return kamusc, nil
-}
-
-//
-// get the definition of word from cache or nil if not exist.
-//
-func (kamus *kamusCache) get(word string) (kata *Kata) {
- kamus.Lock()
- kata = kamus.cache[word]
- kamus.Unlock()
- return kata
-}
-
-//
-// isChanging will return true if the last cache size is not equal with
-// current size.
-//
-func (kamus *kamusCache) isChanging() bool {
- kamus.Lock()
- defer kamus.Unlock()
- return kamus.lastSize != len(kamus.cache)
-}
-
-//
-// load the cached dictionary from storage.
-//
-func (kamus *kamusCache) load() (err error) {
- kamus.Lock()
- defer kamus.Unlock()
-
- v, err := ioutil.ReadFile(kamus.storagePath)
- if err != nil {
- if errors.Is(err, os.ErrNotExist) {
- return nil
- }
- return err
- }
-
- r := bytes.NewReader(v)
-
- dec := gob.NewDecoder(r)
- err = dec.Decode(&kamus.cache)
- if err != nil {
- return err
- }
-
- // Clean up. Remove all word that contain "→" as definition.
- for k, kata := range kamus.cache {
- for _, def := range kata.Definisi {
- if def.Isi == "→" {
- delete(kamus.cache, k)
- break
- }
- }
- if len(kata.Definisi) == 0 {
- delete(kamus.cache, k)
- }
- }
-
- kamus.lastSize = len(kamus.cache)
-
- return nil
-}
-
-//
-// set save the definition of word into cache.
-//
-func (kamus *kamusCache) set(word string, kata *Kata) {
- if len(word) == 0 || kata == nil {
- return
- }
-
- kamus.Lock()
- kamus.cache[word] = kata
- kamus.Unlock()
-}
-
-//
-// store the cache to file only if the storage path is set.
-//
-func (kamus *kamusCache) store() (err error) {
- if len(kamus.storagePath) == 0 {
- return nil
- }
-
- kamus.Lock()
- defer kamus.Unlock()
-
- if len(kamus.cache) == 0 {
- return nil
- }
-
- newStorage := kamus.storagePath + ".new"
-
- f, err := os.Create(newStorage)
- if err != nil {
- errc := f.Close()
- if errc != nil {
- log.Println("kamusCache: store: ", err)
- }
- return err
- }
-
- enc := gob.NewEncoder(f)
- err = enc.Encode(&kamus.cache)
- if err != nil {
- errc := f.Close()
- if errc != nil {
- log.Println("kamusCache: store: ", err)
- }
- return err
- }
-
- errc := f.Close()
- if errc != nil {
- log.Println("kamusCache: store: ", err)
- }
-
- err = libio.Copy(kamus.storagePath, newStorage)
- if err != nil {
- return err
- }
-
- kamus.lastSize = len(kamus.cache)
-
- return nil
-}