corpus.go 1.9 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
package coreindex

import (
	"encoding/json"
	"fmt"

	cid "gitlab.dms3.io/dms3/go-cid"
	logging "gitlab.dms3.io/dms3/go-log"
)

// log is the command logger
var log = logging.Logger("coreindex")

type corpusProps struct {
	Rclass string   // repo class
	Rkind  string   // repo kind
	Rindex int64    // repo index in reposet
	Rcid   *cid.Cid // corpus document cid
}

// Corpus provides an abstraction for corpus document cid tracking.
type CorpusProps interface {
	GetRclass() string
	GetRkind() string
	GetRindex() int64
	GetRcid() *cid.Cid

	SetRclass(rc string)
	SetRkind(rk string)
	SetRindex(ri int64)
	SetRcid(id *cid.Cid)

	Equals(o CorpusProps) bool

	Marshal() ([]byte, error)
	Unmarshal(b []byte) error
}

func NewCorpusProps(rc string, rk string, ri int64, id *cid.Cid) CorpusProps {
	return &corpusProps{
		Rclass: rc,
		Rkind:  rk,
		Rindex: ri,
		Rcid:   id,
	}
}

func (c *corpusProps) GetRclass() string {
	return c.Rclass
}

func (c *corpusProps) GetRkind() string {
	return c.Rkind
}

func (c *corpusProps) GetRindex() int64 {
	return c.Rindex
}

func (c *corpusProps) GetRcid() *cid.Cid {
	return c.Rcid
}

func (c *corpusProps) SetRclass(rc string) {
	c.Rclass = rc
}

func (c *corpusProps) SetRkind(rk string) {
	c.Rkind = rk
}

func (c *corpusProps) SetRindex(ri int64) {
	c.Rindex = ri
}

func (c *corpusProps) SetRcid(id *cid.Cid) {
	c.Rcid = id
}

func (c *corpusProps) Equals(o CorpusProps) bool {
	return c.Rclass == o.GetRclass() &&
		c.Rkind == o.GetRkind() &&
		c.Rindex == o.GetRindex() &&
		c.Rcid.Equals(*o.GetRcid())
}

func (c *corpusProps) Marshal() ([]byte, error) {

	b, err := json.Marshal(*c)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal corpus properties: %v", err)
	} else {
		return b, nil
	}
}

func (c *corpusProps) Unmarshal(b []byte) error {

	err := json.Unmarshal(b, c)
	if err != nil {
		return fmt.Errorf("failed to unmarshal corpus doc properties: %v", err)
	}
	return nil
}