-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add compact database encoding and compression options
- Loading branch information
Showing
21 changed files
with
210 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package event | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"testing" | ||
|
||
"realy.lol/event/examples" | ||
) | ||
|
||
func TestFromCanonical(t *testing.T) { | ||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache)) | ||
var rem, out, can by | ||
var err er | ||
for scanner.Scan() { | ||
b := scanner.Bytes() | ||
c := make(by, 0, len(b)) | ||
c = append(c, b...) | ||
ea := New() | ||
if rem, err = ea.Unmarshal(b); chk.E(err) { | ||
t.Fatal(err) | ||
} | ||
if len(rem) != 0 { | ||
t.Fatalf("some of input remaining after marshal/unmarshal: '%s'", rem) | ||
} | ||
can = ea.ToCanonical(can) | ||
ea.Sig = ea.Sig[:0] | ||
mrsh := ea.Marshal(nil) | ||
eb := &T{} | ||
if rem, err = eb.FromCanonical(can); chk.E(err) { | ||
t.Fatal(err) | ||
} | ||
if len(rem) != 0 { | ||
t.Fatalf("some of input remaining after FromCanonical: '%s'", rem) | ||
} | ||
mrsh2 := eb.Marshal(nil) | ||
if !bytes.Equal(mrsh, mrsh2) { | ||
t.Fatalf("canonical mismatch:\n\n%s\n%s", mrsh, mrsh2) | ||
} | ||
out, can, rem = out[:0], can[:0], rem[:0] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package event | ||
|
||
import ( | ||
"realy.lol/ec/schnorr" | ||
) | ||
|
||
// MarshalCompact encodes an event as the canonical form folowed by the raw binary | ||
// signature (64 bytes) which hashes to form the ID, thus a compact form for the | ||
// database that is smaller and fast to decode. | ||
func (ev *T) MarshalCompact(dst by) (b by) { | ||
b = dst | ||
b = ev.ToCanonical(b) | ||
b = append(b, ev.Sig...) | ||
return | ||
} | ||
|
||
func (ev *T) UnmarshalCompact(b by) (rem by, err er) { | ||
if rem, err = ev.FromCanonical(b); chk.E(err) { | ||
return | ||
} | ||
ev.Sig = rem[:schnorr.SignatureSize] | ||
rem = rem[schnorr.SignatureSize:] | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package event | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"testing" | ||
|
||
"realy.lol/event/examples" | ||
) | ||
|
||
func TestFromCompact(t *testing.T) { | ||
scanner := bufio.NewScanner(bytes.NewBuffer(examples.Cache)) | ||
var rem, com by | ||
var err er | ||
for scanner.Scan() { | ||
b := scanner.Bytes() | ||
c := make(by, 0, len(b)) | ||
c = append(c, b...) | ||
ea := New() | ||
if rem, err = ea.Unmarshal(b); chk.E(err) { | ||
t.Fatalf("error: %s", err) | ||
} | ||
if len(rem) != 0 { | ||
t.Fatalf("some of input remaining after marshal/unmarshal: '%s'", rem) | ||
} | ||
com = ea.MarshalCompact(com) | ||
mrsh := ea.Marshal(nil) | ||
eb := &T{} | ||
if rem, err = eb.UnmarshalCompact(com); chk.E(err) { | ||
t.Fatalf("error: %s", err) | ||
} | ||
if len(rem) != 0 { | ||
t.Fatalf("some of input remaining after FromCanonical: '%0x'", rem) | ||
} | ||
mrsh2 := eb.Marshal(nil) | ||
if !bytes.Equal(mrsh, mrsh2) { | ||
t.Fatalf("compact mismatch:\n\n%s\n%s", mrsh, mrsh2) | ||
} | ||
com, rem = com[:0], rem[:0] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ratel | ||
|
||
import ( | ||
"realy.lol/event" | ||
) | ||
|
||
func (r *T) Unmarshal(ev *event.T, evb by) (rem by, err er) { | ||
if r.UseCompact { | ||
if rem, err = ev.UnmarshalCompact(evb); chk.E(err) { | ||
ev = nil | ||
evb = evb[:0] | ||
return | ||
} | ||
} else { | ||
if rem, err = ev.Unmarshal(evb); chk.E(err) { | ||
ev = nil | ||
evb = evb[:0] | ||
return | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (r *T) Marshal(ev *event.T, dst by) (b by) { | ||
if r.UseCompact { | ||
b = ev.MarshalCompact(dst) | ||
} else { | ||
b = ev.Marshal(dst) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.