Skip to content

Commit

Permalink
int64 castfest
Browse files Browse the repository at this point in the history
  • Loading branch information
likeazir committed Aug 29, 2023
1 parent f99bd78 commit c3672b9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
24 changes: 12 additions & 12 deletions cache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ type CacheBlock struct {

// CachedFile supports contiguous reads via cache
type CachedFile struct {
lru *lru.Cache[int, *CacheBlock]
dataRequestCallback func(offset, length int) ([]byte, error)
fileSize int
lru *lru.Cache[int64, *CacheBlock]
dataRequestCallback func(offset, length int64) ([]byte, error)
fileSize int64
mu sync.Mutex
}

func NewCachedFile(fSize int, dataRequestCallback func(offset int, length int) ([]byte, error)) *CachedFile {
blockLru, _ := lru.New[int, *CacheBlock](MEM_PER_FILE_CACHE_B / BLOCKSIZE)
func NewCachedFile(fSize int64, dataRequestCallback func(offset int64, length int64) ([]byte, error)) *CachedFile {
blockLru, _ := lru.New[int64, *CacheBlock](MEM_PER_FILE_CACHE_B / BLOCKSIZE)
cf := &CachedFile{
dataRequestCallback: dataRequestCallback,
fileSize: fSize,
Expand All @@ -35,7 +35,7 @@ func NewCachedFile(fSize int, dataRequestCallback func(offset int, length int) (
return cf
}

func (cf *CachedFile) fillLruBlock(blockNumber int, block *CacheBlock) error {
func (cf *CachedFile) fillLruBlock(blockNumber int64, block *CacheBlock) error {
for i := 0; i < 5; i++ {
buf, err := cf.dataRequestCallback(blockNumber*BLOCKSIZE, BLOCKSIZE)
if err != nil {
Expand All @@ -50,7 +50,7 @@ func (cf *CachedFile) fillLruBlock(blockNumber int, block *CacheBlock) error {
return errors.New("Failed to fill block")
}

func (cf *CachedFile) Read(offset, length int) ([]byte, error) {
func (cf *CachedFile) Read(offset, length int64) ([]byte, error) {
if offset > cf.fileSize {
return []byte{}, nil
}
Expand All @@ -74,20 +74,20 @@ func (cf *CachedFile) Read(offset, length int) ([]byte, error) {
}
blck.lock.Lock()
defer blck.lock.Unlock()
for i := 0; i < 3; i++ {
for i := int64(0); i < 3; i++ {
go cf.ReadNewData(lruBlock + i)
time.Sleep(10 * time.Nanosecond)
}
if len(blck.data) < blockOffset {
if int64(len(blck.data)) < blockOffset {
return []byte{}, nil
}
if len(blck.data) < blockOffset+length {
length = len(blck.data) - blockOffset
if int64(len(blck.data)) < blockOffset+length {
length = int64(len(blck.data)) - blockOffset
}
return blck.data[blockOffset : blockOffset+length], nil
}

func (cf *CachedFile) ReadNewData(lrublock int) {
func (cf *CachedFile) ReadNewData(lrublock int64) {
if cf.lru.Contains(lrublock) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion common/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package common

type Finfo struct {
NameLength int `struc:"int16,sizeof=Name"`
NameLength int64 `struc:"int16,sizeof=Name"`
Name string
Size int64
IsDir bool
Expand Down
10 changes: 5 additions & 5 deletions fileretriever/fileclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (r RemotePath) Append(name string) RemotePath {

type PeerInfo struct {
Load int64
Rate int
Rate int64
CurrentRequests *semaphore.Weighted
}

Expand Down Expand Up @@ -239,7 +239,7 @@ func (f *FileClient) localFileInfo(path RemotePath) (*common.Finfo, error) {
}, nil
}

func (f *FileClient) netRead(path RemotePath, offset int, length int) ([]byte, error) {
func (f *FileClient) netRead(path RemotePath, offset int64, length int64) ([]byte, error) {
nextLoad := new(int64)
*nextLoad = 3000
log.Trace().Msgf("doing net read at %d for len %d", offset, length)
Expand Down Expand Up @@ -308,7 +308,7 @@ func (f *FileClient) netRead(path RemotePath, offset int, length int) ([]byte, e
return response.Content, nil
}

func (f *FileClient) localRead(remotePath RemotePath, off, length int) ([]byte, error) {
func (f *FileClient) localRead(remotePath RemotePath, off, length int64) ([]byte, error) {
localPath := f.Re2Lo(remotePath)
file, err := os.Open(localPath.String())
if err != nil {
Expand All @@ -320,7 +320,7 @@ func (f *FileClient) localRead(remotePath RemotePath, off, length int) ([]byte,
log.Warn().Err(err).Msgf("Failed to stat file %s", localPath)
return nil, err
}
if off >= int(finfo.Size()) {
if off >= int64(finfo.Size()) {
return []byte{}, nil
}
seek, err := file.Seek(int64(off), 0)
Expand All @@ -336,7 +336,7 @@ func (f *FileClient) localRead(remotePath RemotePath, off, length int) ([]byte,
return buf[:read], nil
}

func (f *FileClient) Read(path RemotePath, off, length int) ([]byte, error) {
func (f *FileClient) Read(path RemotePath, off, length int64) ([]byte, error) {
log.Trace().Msgf("doing read at %d for len %d", off, length)
buf, err := f.localRead(path, off, length)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions fileretriever/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ const (

// TODO use remote path type and custom packer
type FileRequest struct {
Offset int
Length int
PathLength int `struc:"int16,sizeof=Path"`
Offset int64
Length int64
PathLength int64 `struc:"int16,sizeof=Path"`
Path string
}

type FileResponse struct {
Length int `struc:"int64,sizeof=Content"`
FileSize int
Length int64 `struc:"int64,sizeof=Content"`
FileSize int64
Content []byte
}

type DirResponse struct {
DirLength int `struc:"int32,sizeof=Dirs"`
DirLength int64 `struc:"int32,sizeof=Dirs"`
Dirs []byte
FileLength int `struc:"int32,sizeof=Files"`
FileLength int64 `struc:"int32,sizeof=Files"`
Files []byte
}

Expand Down
9 changes: 5 additions & 4 deletions readnetfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func (n *VirtNode) Read(ctx context.Context, fh fusefs.FileHandle, dest []byte,
log.Trace().Msgf("Reading at %d from %s", off, n.path)
cacheEntry := n.fc.GetCachedFile(n.path)
if cacheEntry != nil {
buf, err := cacheEntry.Read(int(off), len(dest))
buf, err := cacheEntry.Read(int64(off), int64(len(dest)))
if err != nil {
log.Warn().Err(err).Msgf("Failed to read %s", n.path)
return nil, syscall.EIO
}
if len(buf) < len(dest) && len(buf) > 0 {
nb, err := cacheEntry.Read(int(off)+len(buf), len(dest)-len(buf))
nb, err := cacheEntry.Read((off)+int64(len(buf)), int64(len(dest)-len(buf)))
if err != nil {
log.Warn().Err(err).Msgf("Failed to read %s", n.path)
return fuse.ReadResultData(buf), 0
Expand All @@ -54,15 +54,16 @@ func (n *VirtNode) Read(ctx context.Context, fh fusefs.FileHandle, dest []byte,
log.Debug().Err(err).Msgf("Failed to read file info for %s", n.path)
return nil, syscall.EIO
}
cf := cache.NewCachedFile(int(fInfo.Size), func(offset, length int) ([]byte, error) {
cf := cache.NewCachedFile(int64(fInfo.Size), func(offset, length int64) ([]byte, error) {
return n.fc.Read(n.path, offset, length)
})
cf = n.fc.PutOrGet(n.path, cf)
buf, err := cf.Read(int(off), fuse.MAX_KERNEL_WRITE)
buf, err := cf.Read(int64(off), fuse.MAX_KERNEL_WRITE)
if err != nil {
log.Warn().Err(err).Msgf("Failed to read %s", n.path)
return nil, syscall.EIO
}

return fuse.ReadResultData(buf), 0
}

Expand Down

0 comments on commit c3672b9

Please sign in to comment.