1. ww
  2. gold

Source

gold / memrepo.go

package gold

import (
	"github.com/kless/goconfig/config"
	"fmt"
	"os"
	"sync"
)

type MemRepo struct {
	Catalog
	mutex    sync.RWMutex
	datasets map[string]Dataset
}

func NewMemRepo(cfg *config.Config) (repo Repository) {
	mr := &MemRepo{}
	mr.datasets = make(map[string]Dataset)
	mr.CatalogConfig(cfg)
	return mr
}

func (mr *MemRepo) String() string {
	return fmt.Sprintf("memory (%d)", mr.Count())
}

func (mr *MemRepo) Count() int {
	return len(mr.datasets)
}

func (mr *MemRepo) Datasets() (ch chan Dataset, err os.Error) {
	ch = make(chan Dataset)
	go func() {
		mr.mutex.RLock()
		for _, dset := range mr.datasets {
			ch <- dset
		}
		mr.mutex.RUnlock()
		close(ch)
	}()
	return
}

func (mr *MemRepo) GetDataset(id string) (dset Dataset, err os.Error) {
	mr.mutex.RLock()
	dset, ok := mr.datasets[id]
	mr.mutex.RUnlock()
	if !ok {
		errs := fmt.Sprintf("no such dataset: %s", id)
		err = os.ErrorString(errs)
	}
	return
}

func (mr *MemRepo) PutDataset(dset Dataset) (err os.Error) {
	mr.mutex.Lock()
	mr.datasets[dset.Id()] = dset
	mr.mutex.Unlock()
	return
}