Lines Matching refs:db

9 package db  package
40 db := &DB{
43 f, err := os.OpenFile(db.filename, os.O_RDONLY|os.O_CREATE, osutil.DefaultFilePerm)
47 db.Version, db.Records, db.uncompacted = deserializeDB(bufio.NewReader(f))
49 if len(db.Records) == 0 || db.uncompacted/10*9 > len(db.Records) {
50 if err := db.compact(); err != nil {
54 return db, nil
57 func (db *DB) Save(key string, val []byte, seq uint64) {
61 if rec, ok := db.Records[key]; ok && seq == rec.Seq && bytes.Equal(val, rec.Val) {
64 db.Records[key] = Record{val, seq}
65 db.serialize(key, val, seq)
66 db.uncompacted++
69 func (db *DB) Delete(key string) {
70 if _, ok := db.Records[key]; !ok {
73 delete(db.Records, key)
74 db.serialize(key, nil, seqDeleted)
75 db.uncompacted++
78 func (db *DB) Flush() error {
79 if db.uncompacted/10*9 > len(db.Records) {
80 return db.compact()
82 if db.pending == nil {
85 f, err := os.OpenFile(db.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, osutil.DefaultFilePerm)
90 if _, err := f.Write(db.pending.Bytes()); err != nil {
93 db.pending = nil
97 func (db *DB) BumpVersion(version uint64) error {
98 if db.Version == version {
99 return db.Flush()
101 db.Version = version
102 return db.compact()
105 func (db *DB) compact() error {
107 serializeHeader(buf, db.Version)
108 for key, rec := range db.Records {
111 f, err := os.Create(db.filename + ".tmp")
120 if err := os.Rename(f.Name(), db.filename); err != nil {
123 db.uncompacted = len(db.Records)
124 db.pending = nil
128 func (db *DB) serialize(key string, val []byte, seq uint64) {
129 if db.pending == nil {
130 db.pending = new(bytes.Buffer)
132 serializeRecord(db.pending, key, val, seq)