more of that.

This commit is contained in:
fiatjaf 2024-07-22 16:25:06 -03:00
parent 80db5b1c0a
commit 874ab3411d
3 changed files with 13 additions and 14 deletions

View File

@ -79,8 +79,8 @@ func (n *Negentropy) Reconcile(query []byte) (output []byte, haveIds []string, n
func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]string) ([]byte, error) {
n.lastTimestampIn, n.lastTimestampOut = 0, 0 // Reset for each message
var fullOutput []byte
fullOutput = append(fullOutput, protocolVersion)
fullOutput := bytes.NewBuffer(make([]byte, 0, 5000))
fullOutput.WriteByte(protocolVersion)
pv, err := reader.ReadByte()
if err != nil {
@ -94,7 +94,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
if n.IsInitiator {
return nil, fmt.Errorf("unsupported negentropy protocol version requested")
}
return fullOutput, nil
return fullOutput.Bytes(), nil
}
var prevBound Bound
@ -199,7 +199,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
endBound := currBound
n.storage.Iterate(lower, upper, func(item Item, index int) bool {
if n.ExceededFrameSizeLimit(len(fullOutput) + len(*responseIdsPtr)) {
if n.ExceededFrameSizeLimit(fullOutput.Len() + len(*responseIdsPtr)) {
endBound = Bound{item}
upper = index
return false
@ -222,7 +222,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
partialOutput.Write(encodeVarInt(numResponseIds))
partialOutput.Write(responseIds)
fullOutput = append(fullOutput, partialOutput.Bytes()...)
partialOutput.WriteTo(fullOutput)
partialOutput.Reset()
}
@ -231,7 +231,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
}
// Check if the frame size limit is exceeded
if n.ExceededFrameSizeLimit(len(fullOutput) + partialOutput.Len()) {
if n.ExceededFrameSizeLimit(fullOutput.Len() + partialOutput.Len()) {
// Frame size limit exceeded, handle by encoding a boundary and fingerprint for the remaining range
remainingFingerprint, err := n.storage.Fingerprint(upper, n.storage.Size())
if err != nil {
@ -242,21 +242,22 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
if err != nil {
panic(err)
}
fullOutput = append(fullOutput, encodedBound...)
fullOutput = append(fullOutput, encodeVarInt(FingerprintMode)...)
fullOutput = append(fullOutput, remainingFingerprint.SV()...)
fullOutput.Write(encodedBound)
fullOutput.WriteByte(FingerprintMode)
fullOutput.Write(remainingFingerprint.SV())
break // Stop processing further
} else {
// Append the constructed output for this iteration
fullOutput = append(fullOutput, partialOutput.Bytes()...)
partialOutput.WriteTo(fullOutput)
}
prevIndex = upper
prevBound = currBound
}
return fullOutput, nil
return fullOutput.Bytes(), nil
}
func (n *Negentropy) SplitRange(lower, upper int, upperBound Bound, output *bytes.Buffer) {

View File

@ -22,7 +22,6 @@ type Storage interface {
Insert(nostr.Timestamp, string) error
Seal() error
IDSize() int
Size() int
Iterate(begin, end int, cb func(item Item, i int) bool) error
FindLowerBound(begin, end int, value Bound) (int, error)

View File

@ -44,8 +44,7 @@ func (v *Vector) Seal() error {
return nil
}
func (v *Vector) Size() int { return len(v.items) }
func (v *Vector) IDSize() int { return v.idSize }
func (v *Vector) Size() int { return len(v.items) }
func (v *Vector) Iterate(begin, end int, cb func(Item, int) bool) error {
for i := begin; i < end; i++ {