Skip to content

Commit

Permalink
Create a DNA codec from Alias codec
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Jun 29, 2024
1 parent dacae51 commit 652536e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
32 changes: 29 additions & 3 deletions v2/transform/AliasCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ func (this sortAliasByFreq) Swap(i, j int) {

// AliasCodec is a simple codec replacing 2-byte symbols with 1-byte aliases whenever possible
type AliasCodec struct {
ctx *map[string]any
ctx *map[string]any
onlyDNA bool
}

// NewAliasCodec creates a new instance of AliasCodec
func NewAliasCodec() (*AliasCodec, error) {
this := &AliasCodec{}
this.onlyDNA = false
return this, nil
}

Expand All @@ -67,6 +69,14 @@ func NewAliasCodec() (*AliasCodec, error) {
func NewAliasCodecWithCtx(ctx *map[string]any) (*AliasCodec, error) {
this := &AliasCodec{}
this.ctx = ctx
this.onlyDNA = false

if ctx != nil {
if val, containsKey := (*this.ctx)["packOnlyDNA"]; containsKey {
this.onlyDNA = val.(bool)
}
}

return this, nil
}

Expand All @@ -90,9 +100,9 @@ func (this *AliasCodec) Forward(src, dst []byte) (uint, uint, error) {
return 0, 0, fmt.Errorf("Input block is too small - size: %d, required %d", len(src), _ALIAS_MIN_BLOCKSIZE)
}

if this.ctx != nil {
dt := internal.DT_UNDEFINED
dt := internal.DT_UNDEFINED

if this.ctx != nil {
if val, containsKey := (*this.ctx)["dataType"]; containsKey {
dt = val.(internal.DataType)
}
Expand All @@ -104,6 +114,10 @@ func (this *AliasCodec) Forward(src, dst []byte) (uint, uint, error) {
if (dt == internal.DT_EXE) || (dt == internal.DT_BIN) {
return 0, 0, errors.New("Alias Codec: forward transform skip, binary data")
}

if (this.onlyDNA == true) && (dt != internal.DT_EXE) && (dt != internal.DT_DNA) {
return 0, 0, errors.New("DNA Alias Codec: forward transform skip, not DNA data")
}
}

// Find missing 1-byte symbols
Expand All @@ -123,6 +137,18 @@ func (this *AliasCodec) Forward(src, dst []byte) (uint, uint, error) {
return 0, 0, errors.New("Alias Codec: forward transform skip, not enough free slots")
}

if dt == internal.DT_UNDEFINED {
dt = internal.DetectSimpleType(len(src), freqs0[:])

if (this.ctx != nil) && (dt != internal.DT_UNDEFINED) {
(*this.ctx)["dataType"] = dt
}

if (dt != internal.DT_DNA) && (this.onlyDNA == true) {
return 0, 0, errors.New("DNA Alias Codec: forward transform skip, not DNA data")
}
}

var srcIdx int
var dstIdx int
count := len(src)
Expand Down
12 changes: 11 additions & 1 deletion v2/transform/Factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
LZX_TYPE = uint64(16) // Lempel Ziv Extra
UTF_TYPE = uint64(17) // UTF codec
PACK_TYPE = uint64(18) // Alias Codec
RESERVED2 = uint64(19) // Reserved
DNA_TYPE = uint64(19) // DNA Alias Codec
RESERVED3 = uint64(20) // Reserved
RESERVED4 = uint64(21) // Reserved
RESERVED5 = uint64(22) // Reserved
Expand Down Expand Up @@ -141,6 +141,10 @@ func newToken(ctx *map[string]any, functionType uint64) (kanzi.ByteTransform, er
case PACK_TYPE:
return NewAliasCodecWithCtx(ctx)

case DNA_TYPE:
(*ctx)["packOnlyDNA"] = true
return NewAliasCodecWithCtx(ctx)

case SRT_TYPE:
return NewSRTWithCtx(ctx)

Expand Down Expand Up @@ -258,6 +262,9 @@ func getByteFunctionNameToken(functionType uint64) (string, error) {
case PACK_TYPE:
return "PACK", nil

case DNA_TYPE:
return "DNA", nil

case NONE_TYPE:
return "NONE", nil

Expand Down Expand Up @@ -365,6 +372,9 @@ func getByteFunctionTypeToken(name string) (uint64, error) {
case "PACK":
return PACK_TYPE, nil

case "DNA":
return DNA_TYPE, nil

case "NONE":
return NONE_TYPE, nil

Expand Down

0 comments on commit 652536e

Please sign in to comment.