github.com/cs3org/reva/v2@v2.27.7/pkg/store/memory/utils.go (about)

     1  package memory
     2  
     3  import (
     4  	"time"
     5  
     6  	"go-micro.dev/v4/store"
     7  )
     8  
     9  func toStoreRecord(src *store.Record, options store.WriteOptions) *storeRecord {
    10  	newRecord := &storeRecord{}
    11  	newRecord.Key = src.Key
    12  	newRecord.Value = make([]byte, len(src.Value))
    13  	copy(newRecord.Value, src.Value)
    14  
    15  	// set base ttl duration and expiration time based on the record
    16  	newRecord.Expiry = src.Expiry
    17  	if src.Expiry != 0 {
    18  		newRecord.ExpiresAt = time.Now().Add(src.Expiry)
    19  	}
    20  
    21  	// overwrite ttl duration and expiration time based on options
    22  	if !options.Expiry.IsZero() {
    23  		// options.Expiry is a time.Time, newRecord.Expiry is a time.Duration
    24  		newRecord.Expiry = time.Until(options.Expiry)
    25  		newRecord.ExpiresAt = options.Expiry
    26  	}
    27  
    28  	// TTL option takes precedence over expiration time
    29  	if options.TTL != 0 {
    30  		newRecord.Expiry = options.TTL
    31  		newRecord.ExpiresAt = time.Now().Add(options.TTL)
    32  	}
    33  
    34  	newRecord.Metadata = make(map[string]interface{})
    35  	for k, v := range src.Metadata {
    36  		newRecord.Metadata[k] = v
    37  	}
    38  	return newRecord
    39  }
    40  
    41  func fromStoreRecord(src *storeRecord) *store.Record {
    42  	newRecord := &store.Record{}
    43  	newRecord.Key = src.Key
    44  	newRecord.Value = make([]byte, len(src.Value))
    45  	copy(newRecord.Value, src.Value)
    46  	if src.Expiry != 0 {
    47  		newRecord.Expiry = time.Until(src.ExpiresAt)
    48  	}
    49  
    50  	newRecord.Metadata = make(map[string]interface{})
    51  	for k, v := range src.Metadata {
    52  		newRecord.Metadata[k] = v
    53  	}
    54  	return newRecord
    55  }
    56  
    57  func reverseString(s string) string {
    58  	r := []rune(s)
    59  	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
    60  		r[i], r[j] = r[j], r[i]
    61  	}
    62  	return string(r)
    63  }