Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catchup to python-herald schema. Plus lots of refactoring. #49

Merged
merged 18 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
2569a55
Make prefixes_test.go more resilient against garbage left
moodyjon Aug 9, 2022
36d8d97
Don't do the ones' complement thing with DBStateValue fields
moodyjon Aug 9, 2022
0c1840c
Simplify type-specific partial packing, and simplify dispatch for pac…
moodyjon Aug 10, 2022
1e467a7
Add struct field annotations and refactor to prepare for
moodyjon Aug 10, 2022
9d1992e
Add dynamic pack/unpack based on "restruct" module.
moodyjon Aug 10, 2022
d7f52bd
Add 5 new prefixes/tables (TrendingNotifications..HashXMempoolStatus).
moodyjon Aug 10, 2022
2501584
Undo rename. TouchedOrDeleted -> ClaimDiff.
moodyjon Aug 10, 2022
441da6b
Fixup callers of eliminated partial pack functions. Have them use key…
moodyjon Aug 10, 2022
02f7a41
Add pluggable SerializationAPI. Use it in prefixes_test.
moodyjon Aug 10, 2022
9b6bbe9
Undo accidental bump of rocksdb version.
moodyjon Aug 10, 2022
b7c109a
Add .vscode dir to gitignore.
moodyjon Aug 10, 2022
3f2af41
Fix ClaimToChannelValue annotation. Implement BlockTxsValue workaround
moodyjon Aug 10, 2022
8a5b6ca
Strengthen partial packing verification. Fix bugs
moodyjon Aug 10, 2022
99a93e5
Remove .DS_Store, and ignore in future.
moodyjon Aug 11, 2022
d42ac9f
Fix MempoolTxKey, TouchedHashXValue. Remove some unneeded struct tags.
moodyjon Aug 11, 2022
972bc00
Generate test data and complete the tests for the new tables.
moodyjon Aug 11, 2022
9e72f9c
Move tableRegistry to prefixes.go and rename it prefixRegistry.
moodyjon Aug 12, 2022
84caed6
Add test that runs through GetPrefixes() contents, and verifies
moodyjon Aug 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/
.vscode/
.DS_Store
12 changes: 6 additions & 6 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,8 @@ func ReadPrefixN(db *grocksdb.DB, prefix []byte, n int) []*prefixes.PrefixRowKV
value := it.Value()

res[i] = &prefixes.PrefixRowKV{
Key: key.Data(),
Value: value.Data(),
RawKey: key.Data(),
RawValue: value.Data(),
}

key.Free()
Expand Down Expand Up @@ -908,8 +908,8 @@ func readWriteRawNCF(db *grocksdb.DB, options *IterOptions, out string, n int, f
if i >= n {
return
}
key := kv.Key.([]byte)
value := kv.Value.([]byte)
key := kv.RawKey
value := kv.RawValue
keyHex := hex.EncodeToString(key)
valueHex := hex.EncodeToString(value)
//log.Println(keyHex)
Expand Down Expand Up @@ -947,8 +947,8 @@ func ReadWriteRawN(db *grocksdb.DB, options *IterOptions, out string, n int) {
if i >= n {
return
}
key := kv.Key.([]byte)
value := kv.Value.([]byte)
key := kv.RawKey
value := kv.RawValue
keyHex := hex.EncodeToString(key)
valueHex := hex.EncodeToString(value)
log.Println(keyHex)
Expand Down
18 changes: 9 additions & 9 deletions db/db_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (db *ReadOnlyDBColumnFamily) GetStreamsAndChannelRepostedByChannelHashes(re

for _, reposterChannelHash := range reposterChannelHashes {
key := prefixes.NewChannelToClaimKeyWHash(reposterChannelHash)
rawKeyPrefix := prefixes.ChannelToClaimKeyPackPartial(key, 1)
rawKeyPrefix := key.PartialPack(1)
options := NewIterateOptions().WithCfHandle(handle).WithPrefix(rawKeyPrefix)
options = options.WithIncludeKey(false).WithIncludeValue(true)
ch := IterCF(db.DB, options)
Expand Down Expand Up @@ -167,7 +167,7 @@ func (db *ReadOnlyDBColumnFamily) GetShortClaimIdUrl(name string, normalizedName
partialClaimId := claimId[:j]
partialKey := prefixes.NewClaimShortIDKey(normalizedName, partialClaimId)
log.Printf("partialKey: %#v\n", partialKey)
keyPrefix := prefixes.ClaimShortIDKeyPackPartial(partialKey, 2)
keyPrefix := partialKey.PartialPack(2)
// Prefix and handle
options := NewIterateOptions().WithPrefix(prefix).WithCfHandle(handle)
// Start and stop bounds
Expand Down Expand Up @@ -218,7 +218,7 @@ func (db *ReadOnlyDBColumnFamily) GetRepostedCount(claimHash []byte) (int, error
}

key := prefixes.NewRepostedKey(claimHash)
keyPrefix := prefixes.RepostedKeyPackPartial(key, 1)
keyPrefix := key.PartialPack(1)
// Prefix and handle
options := NewIterateOptions().WithPrefix(keyPrefix).WithCfHandle(handle)
// Start and stop bounds
Expand Down Expand Up @@ -267,8 +267,8 @@ func (db *ReadOnlyDBColumnFamily) GetActiveAmount(claimHash []byte, txoType uint
startKey := prefixes.NewActiveAmountKey(claimHash, txoType, 0)
endKey := prefixes.NewActiveAmountKey(claimHash, txoType, height)

startKeyRaw := prefixes.ActiveAmountKeyPackPartial(startKey, 3)
endKeyRaw := prefixes.ActiveAmountKeyPackPartial(endKey, 3)
startKeyRaw := startKey.PartialPack(3)
endKeyRaw := endKey.PartialPack(3)
// Prefix and handle
options := NewIterateOptions().WithPrefix([]byte{prefixes.ActiveAmount}).WithCfHandle(handle)
// Start and stop bounds
Expand Down Expand Up @@ -416,7 +416,7 @@ func (db *ReadOnlyDBColumnFamily) ControllingClaimIter() <-chan *prefixes.Prefix

key := prefixes.NewClaimTakeoverKey("")
var rawKeyPrefix []byte = nil
rawKeyPrefix = prefixes.ClaimTakeoverKeyPackPartial(key, 0)
rawKeyPrefix = key.PartialPack(0)
options := NewIterateOptions().WithCfHandle(handle).WithPrefix(rawKeyPrefix)
options = options.WithIncludeValue(true) //.WithIncludeStop(true)
ch := IterCF(db.DB, options)
Expand Down Expand Up @@ -527,7 +527,7 @@ func (db *ReadOnlyDBColumnFamily) EffectiveAmountNameIter(normalizedName string)

key := prefixes.NewEffectiveAmountKey(normalizedName)
var rawKeyPrefix []byte = nil
rawKeyPrefix = prefixes.EffectiveAmountKeyPackPartial(key, 1)
rawKeyPrefix = key.PartialPack(1)
options := NewIterateOptions().WithCfHandle(handle).WithPrefix(rawKeyPrefix)
options = options.WithIncludeValue(true) //.WithIncludeStop(true)
ch := IterCF(db.DB, options)
Expand All @@ -542,9 +542,9 @@ func (db *ReadOnlyDBColumnFamily) ClaimShortIdIter(normalizedName string, claimI
key := prefixes.NewClaimShortIDKey(normalizedName, claimId)
var rawKeyPrefix []byte = nil
if claimId != "" {
rawKeyPrefix = prefixes.ClaimShortIDKeyPackPartial(key, 2)
rawKeyPrefix = key.PartialPack(2)
} else {
rawKeyPrefix = prefixes.ClaimShortIDKeyPackPartial(key, 1)
rawKeyPrefix = key.PartialPack(1)
}
options := NewIterateOptions().WithCfHandle(handle).WithPrefix(rawKeyPrefix)
options = options.WithIncludeValue(true) //.WithIncludeStop(true)
Expand Down
2 changes: 1 addition & 1 deletion db/db_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (db *ReadOnlyDBColumnFamily) ResolveClaimInChannel(channelHash []byte, norm
}

key := prefixes.NewChannelToClaimKey(channelHash, normalizedName)
rawKeyPrefix := prefixes.ChannelToClaimKeyPackPartial(key, 2)
rawKeyPrefix := key.PartialPack(2)
options := NewIterateOptions().WithCfHandle(handle).WithPrefix(rawKeyPrefix)
options = options.WithIncludeValue(true) //.WithIncludeStop(true)
ch := IterCF(db.DB, options)
Expand Down
6 changes: 3 additions & 3 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,9 @@ func TestIter(t *testing.T) {
// log.Println(kv.Key)
gotKey := kv.Key.(*prefixes.RepostedKey).PackKey()

keyPartial3 := prefixes.RepostedKeyPackPartial(kv.Key.(*prefixes.RepostedKey), 3)
keyPartial2 := prefixes.RepostedKeyPackPartial(kv.Key.(*prefixes.RepostedKey), 2)
keyPartial1 := prefixes.RepostedKeyPackPartial(kv.Key.(*prefixes.RepostedKey), 1)
keyPartial3 := kv.Key.(*prefixes.RepostedKey).PartialPack(3)
keyPartial2 := kv.Key.(*prefixes.RepostedKey).PartialPack(2)
keyPartial1 := kv.Key.(*prefixes.RepostedKey).PartialPack(1)

// Check pack partial for sanity
if !bytes.HasPrefix(gotKey, keyPartial3) {
Expand Down
27 changes: 19 additions & 8 deletions db/iteroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type IterOptions struct {
RawValue bool
CfHandle *grocksdb.ColumnFamilyHandle
It *grocksdb.Iterator
Serializer *prefixes.SerializationAPI
}

// NewIterateOptions creates a defualt options structure for a db iterator.
Expand All @@ -41,6 +42,7 @@ func NewIterateOptions() *IterOptions {
RawValue: false,
CfHandle: nil,
It: nil,
Serializer: prefixes.ProductionAPI,
}
}

Expand Down Expand Up @@ -99,6 +101,11 @@ func (o *IterOptions) WithRawValue(rawValue bool) *IterOptions {
return o
}

func (o *IterOptions) WithSerializer(serializer *prefixes.SerializationAPI) *IterOptions {
o.Serializer = serializer
return o
}

// ReadRow reads a row from the db, returns nil when no more rows are available.
func (opts *IterOptions) ReadRow(prevKey *[]byte) *prefixes.PrefixRowKV {
it := opts.It
Expand All @@ -117,8 +124,10 @@ func (opts *IterOptions) ReadRow(prevKey *[]byte) *prefixes.PrefixRowKV {
valueData := value.Data()
valueLen := len(valueData)

var outKey interface{} = nil
var outValue interface{} = nil
var outKey prefixes.BaseKey = nil
var outValue prefixes.BaseValue = nil
var rawOutKey []byte = nil
var rawOutValue []byte = nil
var err error = nil

log.Trace("keyData:", keyData)
Expand All @@ -136,12 +145,12 @@ func (opts *IterOptions) ReadRow(prevKey *[]byte) *prefixes.PrefixRowKV {
newKeyData := make([]byte, keyLen)
copy(newKeyData, keyData)
if opts.IncludeKey && !opts.RawKey {
outKey, err = prefixes.UnpackGenericKey(newKeyData)
outKey, err = opts.Serializer.UnpackKey(newKeyData)
if err != nil {
log.Error(err)
}
} else if opts.IncludeKey {
outKey = newKeyData
rawOutKey = newKeyData
}

// Value could be quite large, so this setting could be important
Expand All @@ -150,18 +159,20 @@ func (opts *IterOptions) ReadRow(prevKey *[]byte) *prefixes.PrefixRowKV {
newValueData := make([]byte, valueLen)
copy(newValueData, valueData)
if !opts.RawValue {
outValue, err = prefixes.UnpackGenericValue(newKeyData, newValueData)
outValue, err = opts.Serializer.UnpackValue(newKeyData, newValueData)
if err != nil {
log.Error(err)
}
} else {
outValue = newValueData
rawOutValue = newValueData
}
}

kv := &prefixes.PrefixRowKV{
Key: outKey,
Value: outValue,
Key: outKey,
Value: outValue,
RawKey: rawOutKey,
RawValue: rawOutValue,
}
*prevKey = newKeyData

Expand Down
Loading