// WriteUnit implements a method of kcd.Writer. On success, the returned // digest is the kcd.HexDigest of whatever unit.MarshalBinary returned. func (db *DB) WriteUnit(_ context.Context, revision, corpus, formatKey string, unit kcd.Unit) (string, error) { if revision == "" { return "", errors.New("empty revision marker") } bits, err := unit.MarshalBinary() if err != nil { return "", err } digest := kcd.HexDigest(bits) if db.Unit == nil { db.Unit = make(map[string]Unit) } db.Unit[digest] = Unit{FormatKey: formatKey, Data: bits} // Update the index. db.SetIndex(digest, RevisionKey, revision) if corpus != "" { db.SetIndex(digest, CorpusKey, corpus) } idx := unit.Index() if idx.Language != "" { db.SetIndex(digest, LanguageKey, idx.Language) } if idx.Output != "" { db.SetIndex(digest, OutputKey, idx.Output) } for _, src := range idx.Sources { db.SetIndex(digest, SourceKey, src) } if idx.Target != "" { db.SetIndex(digest, TargetKey, idx.Target) } return digest, nil }
// WriteFile implements a method of kcd.Writer. func (db *DB) WriteFile(_ context.Context, r io.Reader) (string, error) { bits, err := ioutil.ReadAll(r) if err != nil { return "", err } digest := kcd.HexDigest(bits) if db.File == nil { db.File = make(map[string]string) } db.File[digest] = string(bits) return digest, nil }