mirror of
https://github.com/mudler/luet.git
synced 2025-09-04 00:34:41 +00:00
Imeplement PackageDatabase
Consume InMemoryDatabase for now, which act as a singleton in-memory db.
This commit is contained in:
@@ -17,4 +17,13 @@ package pkg
|
|||||||
|
|
||||||
// Database is a merely simple in-memory db.
|
// Database is a merely simple in-memory db.
|
||||||
// FIXME: Use a proper structure or delegate to third-party
|
// FIXME: Use a proper structure or delegate to third-party
|
||||||
var Database map[string]string = map[string]string{}
|
type PackageDatabase interface {
|
||||||
|
Get(s string) (string, error)
|
||||||
|
Set(k, v string) error
|
||||||
|
|
||||||
|
Create([]byte) (string, error)
|
||||||
|
Retrieve(ID string) ([]byte, error)
|
||||||
|
|
||||||
|
GetPackage(ID string) (Package, error)
|
||||||
|
CreatePackage(p Package) (string, error)
|
||||||
|
}
|
||||||
|
110
pkg/package/database_mem.go
Normal file
110
pkg/package/database_mem.go
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation; either version 2 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License along
|
||||||
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"hash/crc32"
|
||||||
|
)
|
||||||
|
|
||||||
|
var DBInMemoryInstance PackageDatabase
|
||||||
|
|
||||||
|
type InMemoryDatabase struct {
|
||||||
|
Database map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInMemoryDatabase() PackageDatabase {
|
||||||
|
// In memoryDB is a singleton
|
||||||
|
if DBInMemoryInstance == nil {
|
||||||
|
DBInMemoryInstance = &InMemoryDatabase{Database: map[string]string{}}
|
||||||
|
}
|
||||||
|
return DBInMemoryInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *InMemoryDatabase) Get(s string) (string, error) {
|
||||||
|
|
||||||
|
pa, ok := db.Database[s]
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("No key found with that id")
|
||||||
|
}
|
||||||
|
return pa, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *InMemoryDatabase) Set(k, v string) error {
|
||||||
|
db.Database[k] = v
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *InMemoryDatabase) Create(v []byte) (string, error) {
|
||||||
|
enc := base64.StdEncoding.EncodeToString(v)
|
||||||
|
crc32q := crc32.MakeTable(0xD5828281)
|
||||||
|
ID := fmt.Sprintf("%08x\n", crc32.Checksum([]byte(enc), crc32q))
|
||||||
|
|
||||||
|
return ID, db.Set(ID, enc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *InMemoryDatabase) Retrieve(ID string) ([]byte, error) {
|
||||||
|
pa, err := db.Get(ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
enc, err := base64.StdEncoding.DecodeString(pa)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return enc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *InMemoryDatabase) GetPackage(ID string) (Package, error) {
|
||||||
|
|
||||||
|
enc, err := db.Retrieve(ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &DefaultPackage{}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(enc, &p); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode encodes the package to string.
|
||||||
|
// It returns an ID which can be used to retrieve the package later on.
|
||||||
|
func (db *InMemoryDatabase) CreatePackage(p Package) (string, error) {
|
||||||
|
|
||||||
|
pd, ok := p.(*DefaultPackage)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("InMemoryDatabase suports only DefaultPackage")
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := json.Marshal(pd)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
ID, err := db.Create(res)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return ID, nil
|
||||||
|
}
|
@@ -16,12 +16,6 @@
|
|||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"hash/crc32"
|
|
||||||
|
|
||||||
"github.com/crillab/gophersat/bf"
|
"github.com/crillab/gophersat/bf"
|
||||||
version "github.com/hashicorp/go-version"
|
version "github.com/hashicorp/go-version"
|
||||||
|
|
||||||
@@ -98,16 +92,7 @@ func (p *DefaultPackage) RemoveUse(use string) {
|
|||||||
// Encode encodes the package to string.
|
// Encode encodes the package to string.
|
||||||
// It returns an ID which can be used to retrieve the package later on.
|
// It returns an ID which can be used to retrieve the package later on.
|
||||||
func (p *DefaultPackage) Encode() (string, error) {
|
func (p *DefaultPackage) Encode() (string, error) {
|
||||||
res, err := json.Marshal(p)
|
return NewInMemoryDatabase().CreatePackage(p)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
enc := base64.StdEncoding.EncodeToString(res)
|
|
||||||
crc32q := crc32.MakeTable(0xD5828281)
|
|
||||||
ID := fmt.Sprintf("%08x\n", crc32.Checksum([]byte(enc), crc32q))
|
|
||||||
Database[ID] = base64.StdEncoding.EncodeToString(res)
|
|
||||||
return ID, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DefaultPackage) WithState(state State) Package {
|
func (p *DefaultPackage) WithState(state State) Package {
|
||||||
@@ -175,22 +160,7 @@ func (p *DefaultPackage) Expand(world []Package) ([]Package, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DecodePackage(ID string) (Package, error) {
|
func DecodePackage(ID string) (Package, error) {
|
||||||
|
return NewInMemoryDatabase().GetPackage(ID)
|
||||||
pa, ok := Database[ID]
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("No package found with that id")
|
|
||||||
}
|
|
||||||
|
|
||||||
enc, err := base64.StdEncoding.DecodeString(pa)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
p := &DefaultPackage{}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(enc, &p); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return p, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NormalizeFlagged(p Package) {
|
func NormalizeFlagged(p Package) {
|
||||||
|
Reference in New Issue
Block a user