Files
kopia/internal/server/api_mount.go
Jarek Kowalski 9bf9cac7fb refactor(repository): ensure we always parse content.ID and object.ID (#1960)
* refactor(repository): ensure we always parse content.ID and object.ID

This changes the types to be incompatible with string to prevent direct
conversion to and from string.

This has the additional benefit of reducing number of memory allocations
and bytes for all IDs.

content.ID went from 2 allocations to 1:
   typical case 32 characters + 16 bytes per-string overhead
   worst-case 65 characters + 16 bytes per-string overhead
   now: 34 bytes

object.ID went from 2 allocations to 1:
   typical case 32 characters + 16 bytes per-string overhead
   worst-case 65 characters + 16 bytes per-string overhead
   now: 36 bytes

* move index.{ID,IDRange} methods to separate files

* replaced index.IDFromHash with content.IDFromHash externally

* minor tweaks and additional tests

* Update repo/content/index/id_test.go

Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>

* Update repo/content/index/id_test.go

Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>

* pr feedback

* post-merge fixes

* pr feedback

* pr feedback

* fixed subtle regression in sortedContents()

This was actually not producing invalid results because of how base36
works, just not sorting as efficiently as it could.

Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>
2022-05-25 14:15:56 +00:00

94 lines
2.2 KiB
Go

package server
import (
"context"
"encoding/json"
"github.com/kopia/kopia/internal/serverapi"
"github.com/kopia/kopia/repo/object"
)
func handleMountCreate(ctx context.Context, rc requestContext) (interface{}, *apiError) {
req := &serverapi.MountSnapshotRequest{}
if err := json.Unmarshal(rc.body, req); err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "malformed request body")
}
oid, err := object.ParseID(req.Root)
if err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "unable to parse OID")
}
c, err := rc.srv.getMountController(ctx, rc.rep, oid, true)
if err != nil {
return nil, internalServerError(err)
}
log(ctx).Debugf("mount for %v => %v", oid, c.MountPath())
return &serverapi.MountedSnapshot{
Path: c.MountPath(),
Root: oid,
}, nil
}
func handleMountGet(ctx context.Context, rc requestContext) (interface{}, *apiError) {
oid, err := object.ParseID(rc.muxVar("rootObjectID"))
if err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "invalid root object ID")
}
c, err := rc.srv.getMountController(ctx, rc.rep, oid, false)
if err != nil {
return nil, internalServerError(err)
}
if c == nil {
return nil, notFoundError("mount point not found")
}
return &serverapi.MountedSnapshot{
Path: c.MountPath(),
Root: oid,
}, nil
}
func handleMountDelete(ctx context.Context, rc requestContext) (interface{}, *apiError) {
oid, err := object.ParseID(rc.muxVar("rootObjectID"))
if err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "invalid root object ID")
}
c, err := rc.srv.getMountController(ctx, rc.rep, oid, false)
if err != nil {
return nil, internalServerError(err)
}
if c == nil {
return nil, notFoundError("mount point not found")
}
if err := c.Unmount(ctx); err != nil {
return nil, internalServerError(err)
}
rc.srv.deleteMount(oid)
return &serverapi.Empty{}, nil
}
func handleMountList(ctx context.Context, rc requestContext) (interface{}, *apiError) {
res := &serverapi.MountedSnapshots{
Items: []*serverapi.MountedSnapshot{},
}
for oid, c := range rc.srv.listMounts() {
res.Items = append(res.Items, &serverapi.MountedSnapshot{
Path: c.MountPath(),
Root: oid,
})
}
return res, nil
}