<commit_before>package hashmap

import (
	"encoding/gob"
	"os"
	"time"

	"github.com/marpio/img-store/photo"
)

type HashmapMetadataStore struct {
	data map[string]*photo.Photo
}

const dbName string = "photo.db"

func NewHashmapMetadataStore() *HashmapMetadataStore {
	var decodedMetadata map[string]*photo.Photo
	if _, err := os.Stat("photo.db"); os.IsNotExist(err) {
		decodedMetadata = make(map[string]*photo.Photo)
	} else {
		f, err := os.Open(dbName)
		if err != nil {
			return nil
		}
		defer f.Close()
		dec := gob.NewDecoder(f)
		if err := dec.Decode(&decodedMetadata); err != nil {
			decodedMetadata = make(map[string]*photo.Photo)
		}
	}
	return &HashmapMetadataStore{data: decodedMetadata}
}

func (datastore *HashmapMetadataStore) GetAll() (all []*photo.Photo, err error) {
	for _, p := range datastore.data {
		all = append(all, p)
	}
	return all, nil
}

func (datastore *HashmapMetadataStore) GetByMonth(month time.Time) ([]*photo.Photo, error) {
	var res = []*photo.Photo{}

	for _, p := range datastore.data {
		if p.CreatedAt == month {
			res = append(res, p)
		}
	}
	return res, nil
}

func (datastore *HashmapMetadataStore) GetByID(id string) ([]*photo.Photo, error) {
	res := make([]*photo.Photo, 0)
	if p, ok := datastore.data[id]; ok {
		res = append(res, p)
	}
	return res, nil
}

func (datastore *HashmapMetadataStore) Save(metadataEntities []*photo.Photo) (ok bool) {
	f, err := os.Create(dbName)
	if err != nil {
		return false
	}
	defer f.Close()
	e := gob.NewEncoder(f)
	if err := e.Encode(metadataEntities); err != nil {
		return false
	}
	return true
}

func (datastore *HashmapMetadataStore) Delete(imgID string) error {
	delete(datastore.data, imgID)
	return nil
}

func (datastore *HashmapMetadataStore) GetMonths() ([]*time.Time, error) {
	var res = make(map[time.Time]interface{})
	for _, p := range datastore.data {
		if _, ok := res[p.CreatedAtMonth]; !ok {
			res[p.CreatedAtMonth] = nil
		}
	}
	list := make([]*time.Time, len(res))
	for t, _ := range res {
		list = append(list, &t)<commit_msg>save encoded map<commit_after>package hashmap

import (
	"encoding/gob"
	"os"
	"time"

	"github.com/marpio/img-store/photo"
)

type HashmapMetadataStore struct {
	data map[string]*photo.Photo
}

const dbName string = "photo.db"

func NewHashmapMetadataStore() *HashmapMetadataStore {
	var decodedMetadata map[string]*photo.Photo
	if _, err := os.Stat("photo.db"); os.IsNotExist(err) {
		decodedMetadata = make(map[string]*photo.Photo)
	} else {
		f, err := os.Open(dbName)
		if err != nil {
			return nil
		}
		defer f.Close()
		dec := gob.NewDecoder(f)
		if err := dec.Decode(&decodedMetadata); err != nil {
			decodedMetadata = make(map[string]*photo.Photo)
		}
	}
	return &HashmapMetadataStore{data: decodedMetadata}
}

func (datastore *HashmapMetadataStore) GetAll() (all []*photo.Photo, err error) {
	for _, p := range datastore.data {
		all = append(all, p)
	}
	return all, nil
}

func (datastore *HashmapMetadataStore) GetByMonth(month time.Time) ([]*photo.Photo, error) {
	var res = []*photo.Photo{}

	for _, p := range datastore.data {
		if p.CreatedAt == month {
			res = append(res, p)
		}
	}
	return res, nil
}

func (datastore *HashmapMetadataStore) GetByID(id string) ([]*photo.Photo, error) {
	res := make([]*photo.Photo, 0)
	if p, ok := datastore.data[id]; ok {
		res = append(res, p)
	}
	return res, nil
}

func (datastore *HashmapMetadataStore) Save(metadataEntities []*photo.Photo) (ok bool) {
	res := make(map[string]*photo.Photo)
	for _, p := range metadataEntities {
		res[p.PathHash] = p
	}
	f, err := os.Create(dbName)
	if err != nil {
		return false
	}
	defer f.Close()
	e := gob.NewEncoder(f)
	if err := e.Encode(res); err != nil {
		return false
	}
	return true
}

func (datastore *HashmapMetadataStore) Delete(imgID string) error {
	delete(datastore.data, imgID)
	return nil
}

func (datastore *HashmapMetadataStore) GetMonths() ([]*time.Time, error) {
	var res = make(map[time.Time]interface{})
	for _, p := range datastore.data {
		if _, ok := res[p.CreatedAtMonth]; !ok {
			res[p.CreatedAtMonth] = nil
		}
	}
	list := make([]*time.Time, len(res))
	for t, _ := range res {
		list = append(list, &t)
{'fraction_non_alphanumeric': 0.1552675785937125, 'fraction_numerical': 0.00047996160307175426, 'mean_word_length': 4.171215880893301, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1664114', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1552, 'n_words': 369}