package coreindex import ( "fmt" "path" "strconv" "strings" ds "gitlab.dms3.io/dms3/go-datastore" ) // // index keystore conventions // reposet key convention // - /reposet//- // const rootPrefix = "/index/reposet" // // corpus key convention // - /reposet//-//corpus // const corpusDocPrefix = "/corpus" // GetRepoSetKey - return a datastore key for a reposet func GetRepoSetKey(t, k, n string) (ds.Key, error) { key := ds.NewKey(path.Join(rootPrefix, k, t, "-", n)) return key, nil } // DecomposeRepoSetKey - decompose a giver datastore key to constituent parts func DecomposeRepoSetKey(k string) (rtype, rkind, rname string, err error) { // verify key length key := ds.NewKey(k) kl := key.List() if len(kl) < 4 { err = fmt.Errorf("invalid reposet key length %v", key) return } // verify key prefix rootKey := ds.NewKey(rootPrefix) rl := rootKey.List() for i := range rl { if rl[i] != kl[i] { err = fmt.Errorf("invalid reposet key prefix %v", key) return } } // extract and return reposet class and name switch len(kl) { case 4: rtype = "" rkind = kl[len(rl)+1] rname = kl[len(rl)+2] sep := strings.Index(rname, "-") if sep > 0 { rtype = rname[0 : sep-1] rname = rname[sep:rname[len(rname)]] } default: err = fmt.Errorf("invalid reposet key length %v", key) } return } // GetDocKey - return a datastore key for a corpus document func GetDocKey(rc string, rn string, ri int64, di int64) (ds.Key, error) { // Key: rootPrefix + "/_class_/_name_/_n_/corpus/_i_" key := ds.NewKey(path.Join(rootPrefix, rc, rn, strconv.FormatInt(ri, 10), corpusDocPrefix, strconv.FormatInt(di, 10))) return key, nil }