Change tree keys logic

This commit is contained in:
mcrakhman 2022-10-15 17:09:25 +02:00 committed by Mikhail Iudin
parent ba7beb4d5a
commit 998e00d245
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 28 additions and 21 deletions

View File

@ -25,14 +25,24 @@ func (a aclKeys) RawRecordKey(id string) []byte {
type treeKeys struct { type treeKeys struct {
id string id string
headsKey []byte
rootKey []byte
}
func newTreeKeys(id string) treeKeys {
return treeKeys{
id: id,
headsKey: joinStringsToBytes("t", id, "heads"),
rootKey: joinStringsToBytes("t", id, "rootId"),
}
} }
func (t treeKeys) HeadsKey() []byte { func (t treeKeys) HeadsKey() []byte {
return joinStringsToBytes("t", t.id, "heads") return t.headsKey
} }
func (t treeKeys) RootIdKey() []byte { func (t treeKeys) RootIdKey() []byte {
return joinStringsToBytes("t", t.id, "rootId") return t.rootKey
} }
func (t treeKeys) RawChangeKey(id string) []byte { func (t treeKeys) RawChangeKey(id string) []byte {

View File

@ -13,12 +13,11 @@ type treeStorage struct {
db *pogreb.DB db *pogreb.DB
keys treeKeys keys treeKeys
id string id string
headsKey []byte
root *treechangeproto.RawTreeChangeWithId root *treechangeproto.RawTreeChangeWithId
} }
func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err error) { func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err error) {
keys := treeKeys{treeId} keys := newTreeKeys(treeId)
has, err := db.Has(keys.RootIdKey()) has, err := db.Has(keys.RootIdKey())
if err != nil { if err != nil {
return return
@ -57,7 +56,6 @@ func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err e
ts = &treeStorage{ ts = &treeStorage{
db: db, db: db,
keys: keys, keys: keys,
headsKey: keys.HeadsKey(),
id: treeId, id: treeId,
root: rootWithId, root: rootWithId,
} }
@ -65,7 +63,7 @@ func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err e
} }
func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) { func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) {
keys := treeKeys{id: payload.TreeId} keys := newTreeKeys(payload.TreeId)
has, err := db.Has(keys.RootIdKey()) has, err := db.Has(keys.RootIdKey())
if err != nil { if err != nil {
return return
@ -102,7 +100,6 @@ func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload)
ts = &treeStorage{ ts = &treeStorage{
db: db, db: db,
keys: keys, keys: keys,
headsKey: keys.HeadsKey(),
id: payload.RootRawChange.Id, id: payload.RootRawChange.Id,
root: payload.RootRawChange, root: payload.RootRawChange,
} }
@ -132,7 +129,7 @@ func (t *treeStorage) Heads() (heads []string, err error) {
func (t *treeStorage) SetHeads(heads []string) (err error) { func (t *treeStorage) SetHeads(heads []string) (err error) {
payload := createHeadsPayload(heads) payload := createHeadsPayload(heads)
return t.db.Put(t.headsKey, payload) return t.db.Put(t.keys.HeadsKey(), payload)
} }
func (t *treeStorage) AddRawChange(change *treechangeproto.RawTreeChangeWithId) (err error) { func (t *treeStorage) AddRawChange(change *treechangeproto.RawTreeChangeWithId) (err error) {