cayleygraph/cayley · critical

couldn't write version: %v

Error message

couldn't write version: %v

What it means

setVersion failed to persist the schema/format version into the meta bucket because the underlying transaction Put returned an error. The version record is required for the KV store to open correctly, so this aborts store initialization.

Source

Thrown at graph/kv/quadstore.go:197

		return nil, err
	}
	if !qs.exists.disabled {
		if sz, err := qs.getSize(); err != nil {
			return nil, err
		} else if sz == 0 {
			qs.mapBloom = make(map[string]*boom.BloomFilter)
			qs.mapNodes = boom.NewBloomFilter(100*1000*1000, 0.05)
		}
	}
	return qs, nil
}

func setVersion(ctx context.Context, db kv.KV, version int64) error {
	return kv.Update(ctx, db, func(tx kv.Tx) error {
		var buf [8]byte
		binary.LittleEndian.PutUint64(buf[:], uint64(version))
		if err := tx.Put(ctx, metaBucket.AppendBytes([]byte("version")), buf[:]); err != nil {
			return fmt.Errorf("couldn't write version: %v", err)
		}
		return nil
	})
}

func (qs *QuadStore) getMetaInt(ctx context.Context, key string) (int64, error) {
	var v int64
	err := kv.View(ctx, qs.db, func(tx kv.Tx) error {
		val, err := tx.Get(ctx, metaBucket.AppendBytes([]byte(key)))
		if err == kv.ErrNotFound {
			return ErrNoBucket
		} else if err != nil {
			return err
		}
		v, err = asInt64(val, 0)
		if err != nil {
			return err
		}

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Check the DB path permissions and that the filesystem is writable, then retry opening the store.
  2. Close any other process holding the DB open/locked (or delete a stale lock file if safe).
  3. Inspect the wrapped error (%v) for the root cause; if the DB is corrupt, back it up and re-initialize a fresh store.
  4. Free disk space if the disk is full.

Example fix

// before
cayley init --db_path /mnt/ro/data/db
// after
cayley init --db_path /var/lib/cayley/db  # writable path
Defensive patterns

Strategy: try-catch

Validate before calling

if st, err := os.Stat(dbPath); err != nil || !isDirWritable(dbPath) { fail fast with a clear message }

Try / catch

if err := setVersion(ctx, db, version); err != nil {
    var perr *fs.PathError
    if errors.As(err, &perr) { /* check permissions/disk */ }
    return fmt.Errorf("store init failed: %w", err)
}

Prevention

When it happens

Trigger: Opening/initializing a KV database (e.g. leveldb/bolt path) where tx.Put on the meta bucket fails — unwritable database path, corrupt or locked DB file, disk full, or the underlying KV refusing the write.

Common situations: Database directory with wrong permissions or read-only filesystem, another process holding a lock on the DB, full disk, or corrupted DB files after a crash.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/2569f6dd8feea437. Report an issue: GitHub.