put inserts a key/value.
(oldKey, newKey, value []byte, pgId common.Pgid, flags uint32)
| 115 | |
| 116 | // put inserts a key/value. |
| 117 | func (n *node) put(oldKey, newKey, value []byte, pgId common.Pgid, flags uint32) { |
| 118 | if pgId >= n.bucket.tx.meta.Pgid() { |
| 119 | panic(fmt.Sprintf("pgId (%d) above high water mark (%d)", pgId, n.bucket.tx.meta.Pgid())) |
| 120 | } else if len(oldKey) <= 0 { |
| 121 | panic("put: zero-length old key") |
| 122 | } else if len(newKey) <= 0 { |
| 123 | panic("put: zero-length new key") |
| 124 | } |
| 125 | |
| 126 | // Find insertion index. |
| 127 | index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].Key(), oldKey) != -1 }) |
| 128 | |
| 129 | // Add capacity and shift nodes if we don't have an exact match and need to insert. |
| 130 | exact := len(n.inodes) > 0 && index < len(n.inodes) && bytes.Equal(n.inodes[index].Key(), oldKey) |
| 131 | if !exact { |
| 132 | n.inodes = append(n.inodes, common.Inode{}) |
| 133 | copy(n.inodes[index+1:], n.inodes[index:]) |
| 134 | } |
| 135 | |
| 136 | inode := &n.inodes[index] |
| 137 | inode.SetFlags(flags) |
| 138 | inode.SetKey(newKey) |
| 139 | inode.SetValue(value) |
| 140 | inode.SetPgid(pgId) |
| 141 | common.Assert(len(inode.Key()) > 0, "put: zero-length inode key") |
| 142 | } |
| 143 | |
| 144 | // del removes a key from the node. |
| 145 | func (n *node) del(key []byte) { |