needle_read_write_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package needle
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  7. "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. )
  9. func TestAppend(t *testing.T) {
  10. n := &Needle{
  11. Cookie: types.Cookie(123), // Cookie Cookie `comment:"random number to mitigate brute force lookups"`
  12. Id: types.NeedleId(123), // Id NeedleId `comment:"needle id"`
  13. Size: 8, // Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
  14. DataSize: 4, // DataSize uint32 `comment:"Data size"` //version2
  15. Data: []byte("abcd"), // Data []byte `comment:"The actual file data"`
  16. Flags: 0, // Flags byte `comment:"boolean flags"` //version2
  17. NameSize: 0, // NameSize uint8 //version2
  18. Name: nil, // Name []byte `comment:"maximum 256 characters"` //version2
  19. MimeSize: 0, // MimeSize uint8 //version2
  20. Mime: nil, // Mime []byte `comment:"maximum 256 characters"` //version2
  21. PairsSize: 0, // PairsSize uint16 //version2
  22. Pairs: nil, // Pairs []byte `comment:"additional name value pairs, json format, maximum 6
  23. LastModified: 123, // LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes
  24. Ttl: nil, // Ttl *TTL
  25. Checksum: 123, // Checksum CRC `comment:"CRC32 to check integrity"`
  26. AppendAtNs: 123, // AppendAtNs uint64 `comment:"append timestamp in nano seconds"` //version3
  27. Padding: nil, // Padding []byte `comment:"Aligned to 8 bytes"`
  28. }
  29. tempFile, err := ioutil.TempFile("", ".dat")
  30. if err != nil {
  31. t.Errorf("Fail TempFile. %v", err)
  32. return
  33. }
  34. /*
  35. uint8 : 0 to 255
  36. uint16 : 0 to 65535
  37. uint32 : 0 to 4294967295
  38. uint64 : 0 to 18446744073709551615
  39. int8 : -128 to 127
  40. int16 : -32768 to 32767
  41. int32 : -2147483648 to 2147483647
  42. int64 : -9223372036854775808 to 9223372036854775807
  43. */
  44. fileSize := int64(4294967296) + 10000
  45. tempFile.Truncate(fileSize)
  46. defer func() {
  47. tempFile.Close()
  48. os.Remove(tempFile.Name())
  49. }()
  50. datBackend := backend.NewDiskFile(tempFile)
  51. defer datBackend.Close()
  52. offset, _, _, _ := n.Append(datBackend, CurrentVersion)
  53. if offset != uint64(fileSize) {
  54. t.Errorf("Fail to Append Needle.")
  55. }
  56. }