Skip to content

Commit

Permalink
refactor(renterd): migrate objects and multipart apis
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Sep 17, 2024
1 parent 87ac058 commit 39cb991
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 176 deletions.
7 changes: 7 additions & 0 deletions .changeset/brown-pianos-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

Removed deprecated object search and object directory APIs.
5 changes: 5 additions & 0 deletions .changeset/cyan-jokes-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'renterd': minor
---

The files directory and global mode explorers now use the new list objects API.
7 changes: 7 additions & 0 deletions .changeset/dry-elephants-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

Updated the multipart upload APIs with the new payloads.
5 changes: 5 additions & 0 deletions .changeset/wild-ties-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/design-system': minor
---

Added ellipsis boolean to truncate, deprecated humanId.
4 changes: 2 additions & 2 deletions apps/explorer/components/EntityHeading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {
copyToClipboard,
Heading,
humanId,
truncate,
Button,
Link,
} from '@siafoundation/design-system'
Expand All @@ -25,7 +25,7 @@ export function EntityHeading({ label, type, value, href }: Props) {
{upperFirst(label)}{' '}
<Link href={href} underline="hover">
{type === 'block' && Number(value).toLocaleString()}
{type !== 'block' && humanId(value, 15)}
{type !== 'block' && truncate(value, 15)}
</Link>
</Heading>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ScrollArea,
Separator,
Text,
truncate,
} from '@siafoundation/design-system'
import { useObject } from '@siafoundation/renterd-react'
import { cx } from 'class-variance-authority'
Expand Down Expand Up @@ -62,7 +63,7 @@ export function FilesHealthColumnContents({
// multipart uploads might reference the same slab over and over but at
// different offsets and lengths. So we should not assume that they are
// always unique.
id: `${s.offset}${s.length}${s.slab.key}`,
id: `${s.offset}${s.length}${s.slab.encryptionKey}`,
isPartialSlab: !!s.slab.shards,
contractSetShards: s.slab.shards?.length
? computeSlabContractSetShards({
Expand Down Expand Up @@ -91,7 +92,7 @@ export function FilesHealthColumnContents({
className="flex items-center"
font="mono"
>
Slab {slab.key.replace('key:', '').slice(0, 4)}:
Slab {truncate(slab.encryptionKey, 4, false)}:
</Text>
<Text size="12" className="flex items-center">
{slab.isPartialSlab
Expand Down
22 changes: 11 additions & 11 deletions apps/renterd/components/Files/FilesCmd/FilesSearchCmd/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandGroup, CommandItemSearch } from '../../../CmdRoot/Item'
import { Page } from '../../../CmdRoot/types'
import { useObjectSearch } from '@siafoundation/renterd-react'
import { useObjectList } from '@siafoundation/renterd-react'
import { isDirectory } from '../../../../lib/paths'
import { Text } from '@siafoundation/design-system'
import { Document16, FolderIcon } from '@siafoundation/react-icons'
Expand Down Expand Up @@ -31,13 +31,13 @@ export function FilesSearchCmd({
useFilesManager()
const onSearchPage = currentPage?.namespace === filesSearchPage.namespace
const searchBucket = activeBucket || 'default'
const results = useObjectSearch({
const results = useObjectList({
disabled: !onSearchPage,
params: {
bucket: searchBucket,
key: debouncedSearch,
offset: 0,
prefix: debouncedSearch,
limit: 10,
delimiter: '',
},
config: {
swr: {
Expand All @@ -46,34 +46,34 @@ export function FilesSearchCmd({
},
})

if (!onSearchPage || !results.data) {
if (!onSearchPage || !results.data?.objects) {
return null
}

return (
<CommandGroup currentPage={currentPage} commandPage={filesSearchPage}>
{results.data.map(({ name: path }) => {
const compressedPath = compressPath(path, search, 55)
{results.data?.objects.map(({ key }) => {
const compressedPath = compressPath(key, search, 55)
const { startIndex, endIndex } = findLastMatch(compressedPath, search)

return (
<CommandItemSearch
commandPage={filesSearchPage}
currentPage={currentPage}
key={path}
key={key}
onSelect={() => {
beforeSelect()
navigateToModeSpecificFiltering(searchBucket + path)
navigateToModeSpecificFiltering(searchBucket + key)
afterSelect()
}}
value={path}
value={key}
>
<div className="flex items-center gap-2 overflow-hidden">
<Text
color="verySubtle"
className="group-data-[selected=true]:text-gray-1000 dark:group-data-[selected=true]:text-graydark-1000"
>
{isDirectory(path) ? <FolderIcon size={16} /> : <Document16 />}
{isDirectory(key) ? <FolderIcon size={16} /> : <Document16 />}
</Text>
<Text className="flex items-center">
<Text color="verySubtle" ellipsis>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PaginatorUnknownTotal } from '@siafoundation/design-system'
import { PaginatorMarker } from '@siafoundation/design-system'
import { useFilesDirectory } from '../../../contexts/filesDirectory'
import { FilesStatsMenuShared } from '../../Files/FilesStatsMenuShared'
import { FilesFilterDirectoryMenu } from '../../Files/FilesFilterDirectoryMenu'
import { useFilesManager } from '../../../contexts/filesManager'

export function FilesStatsMenu() {
const { isViewingABucket, isViewingBuckets } = useFilesManager()
const { limit, offset, pageCount, dataState } = useFilesDirectory()
const { limit, marker, isMore, pageCount, dataState } = useFilesDirectory()
return (
<div className="flex gap-3 w-full">
{isViewingBuckets ? (
Expand All @@ -16,8 +16,9 @@ export function FilesStatsMenu() {
)}
<FilesStatsMenuShared />
{isViewingABucket && (
<PaginatorUnknownTotal
offset={offset}
<PaginatorMarker
isMore={isMore}
marker={marker}
limit={limit}
pageTotal={pageCount}
isLoading={dataState === 'loading'}
Expand Down
40 changes: 24 additions & 16 deletions apps/renterd/contexts/filesDirectory/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ObjectDirectoryParams } from '@siafoundation/renterd-types'
import { useObjectDirectory } from '@siafoundation/renterd-react'
import { useObjectList } from '@siafoundation/renterd-react'
import { useDataset as useDatasetGeneric } from '../filesManager/dataset'
import { bucketAndKeyParamsFromPath } from '../../lib/paths'
import { useRouter } from 'next/router'
import { useMemo } from 'react'
import { useFilesManager } from '../filesManager'
import { defaultDatasetRefreshInterval } from '../../config/swr'
import { ObjectListParams } from '@siafoundation/renterd-types'

const defaultLimit = 50

Expand All @@ -19,32 +19,39 @@ export function useDataset() {
} = useFilesManager()
const router = useRouter()
const limit = Number(router.query.limit || defaultLimit)
const offset = Number(router.query.offset || 0)
const marker = router.query.marker as string

const pathParams = bucketAndKeyParamsFromPath(activeDirectoryPath)

const params = useMemo(() => {
const p: ObjectDirectoryParams = {
...bucketAndKeyParamsFromPath(activeDirectoryPath),
let prefix = pathParams.key
if (fileNamePrefixFilter) {
prefix += fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter.slice(1)
: fileNamePrefixFilter
}
const p: ObjectListParams = {
prefix,
bucket: pathParams.bucket,
sortBy: sortField,
sortDir: sortDirection,
offset,
limit,
delimiter: '/',
}
if (fileNamePrefixFilter) {
p.prefix = fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter.slice(1)
: fileNamePrefixFilter
if (marker) {
p.marker = marker
}
return p
}, [
activeDirectoryPath,
fileNamePrefixFilter,
pathParams,
sortField,
sortDirection,
offset,
marker,
limit,
])

const response = useObjectDirectory({
const response = useObjectList({
disabled: !activeBucketName,
params,
config: {
Expand All @@ -57,9 +64,9 @@ export function useDataset() {
const objects = useMemo(
() => ({
isValidating: response.isValidating,
data: response.data?.entries,
data: response.data?.objects,
}),
[response.isValidating, response.data?.entries]
[response.isValidating, response.data?.objects]
)

const d = useDatasetGeneric({
Expand All @@ -69,7 +76,8 @@ export function useDataset() {

return {
limit,
offset,
marker,
isMore: response.data?.hasMore,
response,
dataset: d.data,
refresh: response.mutate,
Expand Down
5 changes: 3 additions & 2 deletions apps/renterd/contexts/filesDirectory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function useFilesDirectoryMain() {
enabledColumns,
} = useFilesManager()

const { limit, offset, response, refresh, dataset } = useDataset()
const { limit, marker, isMore, response, refresh, dataset } = useDataset()

const {
onDragEnd,
Expand Down Expand Up @@ -104,7 +104,8 @@ function useFilesDirectoryMain() {
columns: filteredTableColumns,
refresh,
limit,
offset,
marker,
isMore,
datasetPage,
pageCount: dataset?.length || 0,
onDragStart,
Expand Down
23 changes: 14 additions & 9 deletions apps/renterd/contexts/filesFlat/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ObjectListPayload } from '@siafoundation/renterd-types'
import { useObjectList } from '@siafoundation/renterd-react'
import { SortField } from '../filesManager/types'
import { useDataset as useDatasetGeneric } from '../filesManager/dataset'
import { useRouter } from 'next/router'
import { useMemo } from 'react'
import { useFilesManager } from '../filesManager'
import { defaultDatasetRefreshInterval } from '../../config/swr'
import { ObjectListParams } from '@siafoundation/renterd-types'

type Props = {
sortDirection: 'asc' | 'desc'
Expand All @@ -21,22 +21,27 @@ export function useDataset({ sortDirection, sortField }: Props) {
const marker = router.query.marker as string

const params = useMemo(() => {
const p: ObjectListPayload = {
let prefix = ''
if (fileNamePrefixFilter) {
prefix += fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter.slice(1)
: fileNamePrefixFilter
}
const p: ObjectListParams = {
prefix,
bucket: activeBucketName,
sortBy: sortField,
sortDir: sortDirection,
marker,
limit,
delimiter: '',
}
if (fileNamePrefixFilter) {
p.prefix = fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter
: '/' + fileNamePrefixFilter
if (marker) {
p.marker = marker
}
return p
}, [
activeBucketName,
fileNamePrefixFilter,
activeBucketName,
sortField,
sortDirection,
marker,
Expand All @@ -45,7 +50,7 @@ export function useDataset({ sortDirection, sortField }: Props) {

const response = useObjectList({
disabled: !activeBucketName,
payload: params,
params,
config: {
swr: {
refreshInterval: defaultDatasetRefreshInterval,
Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/contexts/filesManager/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjEntry } from '@siafoundation/renterd-types'
import { ObjectMetadata } from '@siafoundation/renterd-types'
import { sortBy, toPairs } from '@technically/lodash'
import useSWR from 'swr'
import { useContracts } from '../contracts'
Expand All @@ -16,7 +16,7 @@ type Props = {
id: string
objects: {
isValidating: boolean
data?: ObjEntry[]
data?: ObjectMetadata[]
}
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export function useDataset({ id, objects }: Props) {
}
})
} else if (objects.data || uploadsList.length) {
objects.data?.forEach(({ name: key, size, health }) => {
objects.data?.forEach(({ key, size, health }) => {
const path = join(activeBucketName, key)
const name = getFilename(key)
dataMap[path] = {
Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/contexts/filesManager/uploads.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { triggerErrorToast } from '@siafoundation/design-system'
import { Bucket } from '@siafoundation/renterd-types'
import { Bucket, busListObjectsRoute } from '@siafoundation/renterd-types'
import {
useBuckets,
useMultipartUploadAbort,
Expand Down Expand Up @@ -106,7 +106,7 @@ export function useUploads({ activeDirectoryPath }: Props) {
const key = getKeyFromPath(path)
const multipartUpload = new MultipartUpload({
file: uploadFile,
path: key,
key,
bucket: bucket.name,
api: ref.current,
partSize: getMultipartUploadPartSize(
Expand All @@ -133,7 +133,7 @@ export function useUploads({ activeDirectoryPath }: Props) {
}, 1000)
)
multipartUpload.setOnComplete(async () => {
await ref.current.mutate((key) => key.startsWith('/bus/objects'))
await ref.current.mutate((key) => key.startsWith(busListObjectsRoute))
ref.current.removeUpload(uploadId)
setTimeout(() => {
ref.current.checkAndStartUploads()
Expand Down
Loading

0 comments on commit 39cb991

Please sign in to comment.