|
| 1 | +//go:build js || wasip1 |
| 2 | + |
| 3 | +package bbolt |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "time" |
| 9 | + "unsafe" |
| 10 | + |
| 11 | + berrors "go.etcd.io/bbolt/errors" |
| 12 | + "go.etcd.io/bbolt/internal/common" |
| 13 | +) |
| 14 | + |
| 15 | +// mmap memory maps a DB's data file. |
| 16 | +func mmap(db *DB, sz int) error { |
| 17 | + // Check MaxSize constraint for WASM platforms |
| 18 | + if !db.readOnly && db.MaxSize > 0 && sz > db.MaxSize { |
| 19 | + // The max size only limits future writes; however, we don't block opening |
| 20 | + // and mapping the database if it already exceeds the limit. |
| 21 | + fileSize, err := db.fileSize() |
| 22 | + if err != nil { |
| 23 | + return fmt.Errorf("could not check existing db file size: %s", err) |
| 24 | + } |
| 25 | + |
| 26 | + if sz > fileSize { |
| 27 | + return berrors.ErrMaxSizeReached |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Truncate and fsync to ensure file size metadata is flushed. |
| 32 | + // https://github.com/boltdb/bolt/issues/284 |
| 33 | + if !db.NoGrowSync && !db.readOnly { |
| 34 | + if err := db.file.Truncate(int64(sz)); err != nil { |
| 35 | + return fmt.Errorf("file resize error: %s", err) |
| 36 | + } |
| 37 | + if err := db.file.Sync(); err != nil { |
| 38 | + return fmt.Errorf("file sync error: %s", err) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Map the data file to memory. |
| 43 | + b := make([]byte, sz) |
| 44 | + if sz > 0 { |
| 45 | + // Read the data file. |
| 46 | + if _, err := db.file.ReadAt(b, 0); err != nil && err != io.EOF { |
| 47 | + return err |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Save the original byte slice and convert to a byte array pointer. |
| 52 | + db.dataref = b |
| 53 | + db.datasz = sz |
| 54 | + if sz > 0 { |
| 55 | + db.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0])) |
| 56 | + } |
| 57 | + |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// munmap unmaps a DB's data file from memory. |
| 62 | +func munmap(db *DB) error { |
| 63 | + // In WASM, we just clear the references |
| 64 | + db.dataref = nil |
| 65 | + db.data = nil |
| 66 | + db.datasz = 0 |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// madvise is not supported in WASM. |
| 71 | +func madvise(b []byte, advice int) error { |
| 72 | + // Not implemented - no memory advice in WASM |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// mlock is not supported in WASM. |
| 77 | +func mlock(db *DB, fileSize int) error { |
| 78 | + // Not implemented - no memory locking in WASM |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// munlock is not supported in WASM. |
| 83 | +func munlock(db *DB, fileSize int) error { |
| 84 | + // Not implemented - no memory unlocking in WASM |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// flock acquires an advisory lock on a file descriptor. |
| 89 | +func flock(db *DB, exclusive bool, timeout time.Duration) error { |
| 90 | + // Not implemented - no file locking in WASM |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// funlock releases an advisory lock on a file descriptor. |
| 95 | +func funlock(db *DB) error { |
| 96 | + // Not implemented - no file unlocking in WASM |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// fdatasync flushes written data to a file descriptor. |
| 101 | +func fdatasync(db *DB) error { |
| 102 | + if db.file == nil { |
| 103 | + return nil |
| 104 | + } |
| 105 | + return db.file.Sync() |
| 106 | +} |
| 107 | + |
| 108 | +// txInit refreshes the memory buffer from the file for WASM platforms. |
| 109 | +// This is needed because WASM doesn't have real mmap, so we need to manually |
| 110 | +// sync the memory buffer with the file to see changes from previous transactions. |
| 111 | +func (db *DB) txInit() error { |
| 112 | + // For read-only databases or initial state, skip refresh |
| 113 | + if db.file == nil { |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + // Check if the file has grown |
| 118 | + fileInfo, err := db.file.Stat() |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + fileSize := int(fileInfo.Size()) |
| 123 | + |
| 124 | + // If file has grown or we need to initialize, refresh memory |
| 125 | + if fileSize > db.datasz || db.datasz == 0 { |
| 126 | + // Re-mmap with the new size |
| 127 | + if err := mmap(db, fileSize); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + } else if db.datasz > 0 { |
| 131 | + // Refresh the existing buffer |
| 132 | + b := make([]byte, db.datasz) |
| 133 | + if _, err := db.file.ReadAt(b, 0); err != nil && err != io.EOF { |
| 134 | + return err |
| 135 | + } |
| 136 | + db.dataref = b |
| 137 | + db.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0])) |
| 138 | + } |
| 139 | + |
| 140 | + // Update meta page pointers |
| 141 | + if db.pageSize > 0 && db.datasz >= db.pageSize*2 { |
| 142 | + db.meta0 = db.page(0).Meta() |
| 143 | + db.meta1 = db.page(1).Meta() |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
0 commit comments