Giter VIP home page Giter VIP logo

Comments (3)

rgayon avatar rgayon commented on May 18, 2024

I've been playing with some code to try and implement this in a transparent way, for the code base

import mmap                                                                      
                                                                                   
from google.cloud import storage                                                 
                                                                                 
def slice_big(file, slices, total_size, filename):                                                     
  storage_client = storage.Client()                                              
  bucket = storage_client.bucket('somebbucket')                                  
                                                                                   
  if int(slices) < 1:                                                            
    raise Exception('Need at least 1 slice')                                     
                                                                                 
  number_of_pages = int(total_size / int(slices) /  mmap.PAGESIZE)               
                                                                                 
  slice_size = number_of_pages * mmap.PAGESIZE                                   
                                                                                 
  for i, seek in enumerate(range(0, total_size, slice_size)):                    
    length = slice_size                                                          
    if seek+slice_size > total_size: 
      # No trying to read past end of filedescriptor                                          
      length = total_size - seek                                                 
    print(f'sending slice {i} from bytes {seek} to {seek+length}')               
    m = mmap.mmap(                                                               
        fd.fileno(), length=length, offset=seek, access=mmap.ACCESS_READ)        
    blob = bucket.blob('large_split/'+filename+f'_{i}')                          
    blob.upload_from_file(m)                                               
                                                                                   
 slice_big(open('/dev/loop0', 'rb'), 12, 1048576000, 'big')   

from giftstick.

rgayon avatar rgayon commented on May 18, 2024

This seem to work.
But I can't use that mmap method when reading from dcfldd

from giftstick.

rgayon avatar rgayon commented on May 18, 2024

We can also skip already present slices

import mmap                                                                      
                                                                                   
from google.cloud import storage                                                                                                                                
                                                                                   
def slice_big(fd, slices, total_size, filename, skip_exist=False):               
  storage_client = storage.Client()                  
  bucket = storage_client.bucket('somebbucket')                              
                                                                                   
  if int(slices) < 1:                                                            
    raise Exception('Need at least 1 slice')                                     
  # mmap requires that the an offset is a multiple of mmap.PAGESIZE              
  # so we can't just equally divide the total size in the specified number of    
  # slices.                                                                      
  number_of_pages = int(total_size / int(slices) /  mmap.PAGESIZE)               
                                                                                 
  slice_size = number_of_pages * mmap.PAGESIZE                                   
                                                                                 
  for i, seek in enumerate(range(0, total_size, slice_size)):                    
    blob_path = 'large_split/'+filename+f'_{i}'                                  
    blob = bucket.blob(blob_path)                                                
                                                                                 
    if skip_exist and blob.exists():                                             
      print(f'{blob_path} already exists, skipping')                             
      continue                                                                   
                                                                                 
    length = slice_size                                                          
    if seek+slice_size > total_size:                                             
      length = total_size - seek                                                 
    print(f'sending slice {i} from bytes {seek} to {seek+length}')               
    m = mmap.mmap(                                                               
        fd.fileno(), length=length, offset=seek, access=mmap.ACCESS_READ)        
    blob.upload_from_file(m)                                                     
                                                                                 
slice_big(open('/dev/loop0', 'rb'), 12, 1048576000, 'biggy', skip_exist=True) 

from giftstick.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.