Example #1
0
func OpenRepositoryWithName(name string, memory_quota uint64) (repo *Repository, err error) {

	if memory_quota < common.MIN_FOREST_DB_CACHE_SIZE {
		memory_quota = common.MIN_FOREST_DB_CACHE_SIZE
	}

	log.Current.Debugf("Repo.OpenRepositoryWithName(): open repo with name %s, buffer cache size %d", name, memory_quota)

	config := fdb.DefaultConfig()
	config.SetBufferCacheSize(memory_quota)
	dbfile, err := fdb.Open(name, config)
	if err != nil {
		return nil, err
	}

	cleanup := common.NewCleanup(func() {
		dbfile.Close()
	})
	defer cleanup.Run()

	stores := make(map[RepoKind]*fdb.KVStore)

	if stores[MAIN], err = dbfile.OpenKVStore("MAIN", nil); err != nil {
		return nil, err
	}
	if stores[COMMIT_LOG], err = dbfile.OpenKVStore("COMMIT_LOG", nil); err != nil {
		return nil, err
	}
	if stores[SERVER_CONFIG], err = dbfile.OpenKVStore("SERVER_CONFIG", nil); err != nil {
		return nil, err
	}
	if stores[LOCAL], err = dbfile.OpenKVStore("LOCAL", nil); err != nil {
		return nil, err
	}
	cleanup.Cancel()

	snapshots := make(map[RepoKind][]*Snapshot)
	snapshots[MAIN] = nil
	snapshots[COMMIT_LOG] = nil
	snapshots[SERVER_CONFIG] = nil
	snapshots[LOCAL] = nil

	repo = &Repository{dbfile: dbfile,
		stores:    stores,
		snapshots: snapshots}

	return repo, nil
}
func TestForestDBIterator(t *testing.T) {
	defer os.RemoveAll("test")

	dbfile, err := forestdb.Open("test", nil)
	if err != nil {
		t.Fatal(err)
	}
	defer dbfile.Close()

	kvstore, err := dbfile.OpenKVStoreDefault(nil)
	if err != nil {
		t.Fatal(err)
	}
	defer kvstore.Close()

	// store a bunch of values to test the iterator

	kvstore.SetKV([]byte("a"), []byte("vala"))
	kvstore.SetKV([]byte("b"), []byte("valb"))
	kvstore.SetKV([]byte("c"), []byte("valc"))
	kvstore.SetKV([]byte("d"), []byte("vald"))
	kvstore.SetKV([]byte("e"), []byte("vale"))
	kvstore.SetKV([]byte("f"), []byte("valf"))
	kvstore.SetKV([]byte("g"), []byte("valg"))
	kvstore.SetKV([]byte("h"), []byte("valh"))
	kvstore.SetKV([]byte("i"), []byte("vali"))
	kvstore.SetKV([]byte("j"), []byte("valj"))

	dbfile.Commit(forestdb.COMMIT_MANUAL_WAL_FLUSH)

	info, err := kvstore.Info()
	if err != nil {
		t.Fatal(err)
	}
	lastSeqNum := info.LastSeqNum()

	iter, err := newForestDBIterator(nil, kvstore, lastSeqNum)
	if err != nil {
		t.Fatal(err)
	}

	defer iter.Close()

	iter.SeekFirst()

	count := 0
	var firstKey, lastKey []byte

	for ; iter.Valid(); iter.Next() {
		if firstKey == nil {
			firstKey = iter.Key()
		}
		count++
		lastKey = iter.Key()
	}

	if count != 10 {
		t.Errorf("exptected to iterate 10, saw %d", count)
	}
	if string(firstKey) != "a" {
		t.Errorf("expected fist key to be a, got %s", firstKey)
	}
	if string(lastKey) != "j" {
		t.Errorf("expected last key to be j, got %s", lastKey)
	}

}