large_objects.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package pgx
  2. import (
  3. "context"
  4. "errors"
  5. "io"
  6. "github.com/jackc/pgx/v5/pgtype"
  7. )
  8. // The PostgreSQL wire protocol has a limit of 1 GB - 1 per message. See definition of
  9. // PQ_LARGE_MESSAGE_LIMIT in the PostgreSQL source code. To allow for the other data
  10. // in the message,maxLargeObjectMessageLength should be no larger than 1 GB - 1 KB.
  11. var maxLargeObjectMessageLength = 1024*1024*1024 - 1024
  12. // LargeObjects is a structure used to access the large objects API. It is only valid within the transaction where it
  13. // was created.
  14. //
  15. // For more details see: http://www.postgresql.org/docs/current/static/largeobjects.html
  16. type LargeObjects struct {
  17. tx Tx
  18. }
  19. type LargeObjectMode int32
  20. const (
  21. LargeObjectModeWrite LargeObjectMode = 0x20000
  22. LargeObjectModeRead LargeObjectMode = 0x40000
  23. )
  24. // Create creates a new large object. If oid is zero, the server assigns an unused OID.
  25. func (o *LargeObjects) Create(ctx context.Context, oid uint32) (uint32, error) {
  26. err := o.tx.QueryRow(ctx, "select lo_create($1)", oid).Scan(&oid)
  27. return oid, err
  28. }
  29. // Open opens an existing large object with the given mode. ctx will also be used for all operations on the opened large
  30. // object.
  31. func (o *LargeObjects) Open(ctx context.Context, oid uint32, mode LargeObjectMode) (*LargeObject, error) {
  32. var fd int32
  33. err := o.tx.QueryRow(ctx, "select lo_open($1, $2)", oid, mode).Scan(&fd)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &LargeObject{fd: fd, tx: o.tx, ctx: ctx}, nil
  38. }
  39. // Unlink removes a large object from the database.
  40. func (o *LargeObjects) Unlink(ctx context.Context, oid uint32) error {
  41. var result int32
  42. err := o.tx.QueryRow(ctx, "select lo_unlink($1)", oid).Scan(&result)
  43. if err != nil {
  44. return err
  45. }
  46. if result != 1 {
  47. return errors.New("failed to remove large object")
  48. }
  49. return nil
  50. }
  51. // A LargeObject is a large object stored on the server. It is only valid within the transaction that it was initialized
  52. // in. It uses the context it was initialized with for all operations. It implements these interfaces:
  53. //
  54. // io.Writer
  55. // io.Reader
  56. // io.Seeker
  57. // io.Closer
  58. type LargeObject struct {
  59. ctx context.Context
  60. tx Tx
  61. fd int32
  62. }
  63. // Write writes p to the large object and returns the number of bytes written and an error if not all of p was written.
  64. func (o *LargeObject) Write(p []byte) (int, error) {
  65. nTotal := 0
  66. for {
  67. expected := len(p) - nTotal
  68. if expected == 0 {
  69. break
  70. } else if expected > maxLargeObjectMessageLength {
  71. expected = maxLargeObjectMessageLength
  72. }
  73. var n int
  74. err := o.tx.QueryRow(o.ctx, "select lowrite($1, $2)", o.fd, p[nTotal:nTotal+expected]).Scan(&n)
  75. if err != nil {
  76. return nTotal, err
  77. }
  78. if n < 0 {
  79. return nTotal, errors.New("failed to write to large object")
  80. }
  81. nTotal += n
  82. if n < expected {
  83. return nTotal, errors.New("short write to large object")
  84. } else if n > expected {
  85. return nTotal, errors.New("invalid write to large object")
  86. }
  87. }
  88. return nTotal, nil
  89. }
  90. // Read reads up to len(p) bytes into p returning the number of bytes read.
  91. func (o *LargeObject) Read(p []byte) (int, error) {
  92. nTotal := 0
  93. for {
  94. expected := len(p) - nTotal
  95. if expected == 0 {
  96. break
  97. } else if expected > maxLargeObjectMessageLength {
  98. expected = maxLargeObjectMessageLength
  99. }
  100. res := pgtype.PreallocBytes(p[nTotal:])
  101. err := o.tx.QueryRow(o.ctx, "select loread($1, $2)", o.fd, expected).Scan(&res)
  102. // We compute expected so that it always fits into p, so it should never happen
  103. // that PreallocBytes's ScanBytes had to allocate a new slice.
  104. nTotal += len(res)
  105. if err != nil {
  106. return nTotal, err
  107. }
  108. if len(res) < expected {
  109. return nTotal, io.EOF
  110. } else if len(res) > expected {
  111. return nTotal, errors.New("invalid read of large object")
  112. }
  113. }
  114. return nTotal, nil
  115. }
  116. // Seek moves the current location pointer to the new location specified by offset.
  117. func (o *LargeObject) Seek(offset int64, whence int) (n int64, err error) {
  118. err = o.tx.QueryRow(o.ctx, "select lo_lseek64($1, $2, $3)", o.fd, offset, whence).Scan(&n)
  119. return n, err
  120. }
  121. // Tell returns the current read or write location of the large object descriptor.
  122. func (o *LargeObject) Tell() (n int64, err error) {
  123. err = o.tx.QueryRow(o.ctx, "select lo_tell64($1)", o.fd).Scan(&n)
  124. return n, err
  125. }
  126. // Truncate the large object to size.
  127. func (o *LargeObject) Truncate(size int64) (err error) {
  128. _, err = o.tx.Exec(o.ctx, "select lo_truncate64($1, $2)", o.fd, size)
  129. return err
  130. }
  131. // Close the large object descriptor.
  132. func (o *LargeObject) Close() error {
  133. _, err := o.tx.Exec(o.ctx, "select lo_close($1)", o.fd)
  134. return err
  135. }