github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/journal_dirty_bcache.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkbfs
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/keybase/client/go/kbfs/data"
    11  	"github.com/keybase/client/go/kbfs/tlf"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  type journalDirtyBlockCache struct {
    16  	jManager     *JournalManager
    17  	syncCache    data.DirtyBlockCache
    18  	journalCache data.DirtyBlockCache
    19  }
    20  
    21  var _ data.DirtyBlockCache = journalDirtyBlockCache{}
    22  
    23  func (j journalDirtyBlockCache) Get(
    24  	ctx context.Context, tlfID tlf.ID, ptr data.BlockPointer,
    25  	branch data.BranchName) (data.Block, error) {
    26  	if j.jManager.hasTLFJournal(tlfID) {
    27  		return j.journalCache.Get(ctx, tlfID, ptr, branch)
    28  	}
    29  
    30  	return j.syncCache.Get(ctx, tlfID, ptr, branch)
    31  }
    32  
    33  func (j journalDirtyBlockCache) Put(
    34  	ctx context.Context, tlfID tlf.ID, ptr data.BlockPointer,
    35  	branch data.BranchName, block data.Block) error {
    36  	if j.jManager.hasTLFJournal(tlfID) {
    37  		return j.journalCache.Put(ctx, tlfID, ptr, branch, block)
    38  	}
    39  
    40  	return j.syncCache.Put(ctx, tlfID, ptr, branch, block)
    41  }
    42  
    43  func (j journalDirtyBlockCache) Delete(tlfID tlf.ID, ptr data.BlockPointer,
    44  	branch data.BranchName) error {
    45  	if j.jManager.hasTLFJournal(tlfID) {
    46  		return j.journalCache.Delete(tlfID, ptr, branch)
    47  	}
    48  
    49  	return j.syncCache.Delete(tlfID, ptr, branch)
    50  }
    51  
    52  func (j journalDirtyBlockCache) IsDirty(tlfID tlf.ID, ptr data.BlockPointer,
    53  	branch data.BranchName) bool {
    54  	if j.jManager.hasTLFJournal(tlfID) {
    55  		return j.journalCache.IsDirty(tlfID, ptr, branch)
    56  	}
    57  
    58  	return j.syncCache.IsDirty(tlfID, ptr, branch)
    59  }
    60  
    61  func (j journalDirtyBlockCache) IsAnyDirty(tlfID tlf.ID) bool {
    62  	return j.journalCache.IsAnyDirty(tlfID) || j.syncCache.IsAnyDirty(tlfID)
    63  }
    64  
    65  func (j journalDirtyBlockCache) RequestPermissionToDirty(ctx context.Context,
    66  	tlfID tlf.ID, estimatedDirtyBytes int64) (data.DirtyPermChan, error) {
    67  	if j.jManager.hasTLFJournal(tlfID) {
    68  		return j.journalCache.RequestPermissionToDirty(ctx, tlfID,
    69  			estimatedDirtyBytes)
    70  	}
    71  
    72  	return j.syncCache.RequestPermissionToDirty(ctx, tlfID, estimatedDirtyBytes)
    73  }
    74  
    75  func (j journalDirtyBlockCache) UpdateUnsyncedBytes(tlfID tlf.ID,
    76  	newUnsyncedBytes int64, wasSyncing bool) {
    77  	if j.jManager.hasTLFJournal(tlfID) {
    78  		j.journalCache.UpdateUnsyncedBytes(tlfID, newUnsyncedBytes, wasSyncing)
    79  	} else {
    80  		j.syncCache.UpdateUnsyncedBytes(tlfID, newUnsyncedBytes, wasSyncing)
    81  	}
    82  }
    83  
    84  func (j journalDirtyBlockCache) UpdateSyncingBytes(tlfID tlf.ID, size int64) {
    85  	if j.jManager.hasTLFJournal(tlfID) {
    86  		j.journalCache.UpdateSyncingBytes(tlfID, size)
    87  	} else {
    88  		j.syncCache.UpdateSyncingBytes(tlfID, size)
    89  	}
    90  }
    91  
    92  func (j journalDirtyBlockCache) BlockSyncFinished(tlfID tlf.ID, size int64) {
    93  	if j.jManager.hasTLFJournal(tlfID) {
    94  		j.journalCache.BlockSyncFinished(tlfID, size)
    95  	} else {
    96  		j.syncCache.BlockSyncFinished(tlfID, size)
    97  	}
    98  }
    99  
   100  func (j journalDirtyBlockCache) SyncFinished(tlfID tlf.ID, size int64) {
   101  	if j.jManager.hasTLFJournal(tlfID) {
   102  		j.journalCache.SyncFinished(tlfID, size)
   103  	} else {
   104  		j.syncCache.SyncFinished(tlfID, size)
   105  	}
   106  }
   107  
   108  func (j journalDirtyBlockCache) ShouldForceSync(tlfID tlf.ID) bool {
   109  	if j.jManager.hasTLFJournal(tlfID) {
   110  		return j.journalCache.ShouldForceSync(tlfID)
   111  	}
   112  
   113  	return j.syncCache.ShouldForceSync(tlfID)
   114  }
   115  
   116  func (j journalDirtyBlockCache) Shutdown() error {
   117  	journalErr := j.journalCache.Shutdown()
   118  	syncErr := j.syncCache.Shutdown()
   119  	if journalErr == nil {
   120  		return syncErr
   121  	} else if syncErr == nil {
   122  		return journalErr
   123  	}
   124  	return fmt.Errorf("Multiple errors on dirty bcache shutdown: %v",
   125  		[]error{journalErr, syncErr})
   126  }