2022-09-21 05:45:23 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
2022-09-22 16:57:21 +00:00
|
|
|
"errors"
|
2022-09-21 05:45:23 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ObjRegistry struct {
|
2022-09-26 18:05:28 +00:00
|
|
|
boltDB *bbolt.DB
|
2022-09-21 05:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-09-22 16:57:21 +00:00
|
|
|
// Indicates that an object was created, but its data wasn't verified yet.
|
2022-09-21 05:45:23 +00:00
|
|
|
statusCreated = "created"
|
|
|
|
)
|
|
|
|
|
|
|
|
const bucketName = "_object"
|
|
|
|
|
|
|
|
// ObjectInfo represents information about neoFS object that has been created
|
|
|
|
// via gRPC/HTTP/S3 API.
|
|
|
|
type ObjectInfo struct {
|
2022-09-26 18:05:28 +00:00
|
|
|
Id uint64 // Identifier in bolt DB
|
|
|
|
CreatedAt time.Time // UTC date&time when the object was created
|
|
|
|
CID string // Container ID in gRPC/HTTP
|
|
|
|
OID string // Object ID in gRPC/HTTP
|
|
|
|
S3Bucket string // Bucket name in S3
|
|
|
|
S3Key string // Object key in S3
|
|
|
|
Status string // Status of the object
|
|
|
|
PayloadHash string // SHA256 hash of object payload that can be used for verification
|
2022-09-21 05:45:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
// NewObjRegistry creates a new instance of object registry that stores information
|
|
|
|
// about objects in the specified bolt database. As registry uses read-write
|
|
|
|
// connection to the database, there may be only one instance of object registry
|
|
|
|
// per database file at a time.
|
2022-09-21 05:45:23 +00:00
|
|
|
func NewObjRegistry(dbFilePath string) *ObjRegistry {
|
|
|
|
options := bbolt.Options{Timeout: 100 * time.Millisecond}
|
|
|
|
boltDB, err := bbolt.Open(dbFilePath, os.ModePerm, &options)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
objRepository := &ObjRegistry{boltDB: boltDB}
|
2022-09-21 05:45:23 +00:00
|
|
|
return objRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ObjRegistry) AddObject(cid, oid, s3Bucket, s3Key, payloadHash string) error {
|
|
|
|
return o.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := b.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
object := ObjectInfo{
|
|
|
|
Id: id,
|
2022-09-26 18:05:28 +00:00
|
|
|
CreatedAt: time.Now().UTC(),
|
2022-09-21 05:45:23 +00:00
|
|
|
CID: cid,
|
|
|
|
OID: oid,
|
|
|
|
S3Bucket: s3Bucket,
|
|
|
|
S3Key: s3Key,
|
|
|
|
PayloadHash: payloadHash,
|
|
|
|
Status: statusCreated,
|
|
|
|
}
|
|
|
|
objectJson, err := json.Marshal(object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Put(encodeId(id), objectJson)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ObjRegistry) SetObjectStatus(id uint64, newStatus string) error {
|
|
|
|
return o.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
objBytes := b.Get(encodeId(id))
|
|
|
|
if objBytes == nil {
|
2022-09-22 16:57:21 +00:00
|
|
|
return errors.New("object doesn't exist")
|
2022-09-21 05:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj := new(ObjectInfo)
|
|
|
|
if err := json.Unmarshal(objBytes, &obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obj.Status = newStatus
|
|
|
|
|
|
|
|
objBytes, err = json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.Put(encodeId(id), objBytes)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ObjRegistry) Close() error {
|
|
|
|
return o.boltDB.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeId(id uint64) []byte {
|
|
|
|
idBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(idBytes, id)
|
|
|
|
return idBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeId(idBytes []byte) uint64 {
|
|
|
|
return binary.BigEndian.Uint64(idBytes)
|
|
|
|
}
|