Example #1
0
func initFilePath() (string, error) {
	cfg := kaiju.GetConfig()
	path := filepath.Join(kaiju.ConfigFileDir(), cfg.DataDir)
	if err := os.MkdirAll(path, os.ModePerm); err != nil {
		return "", err
	} else {
		return path, nil
	}
}
Example #2
0
func (u *outputDB) Commit(tag uint32, force bool) error {
	if force || u.db.WAValueLen() > kaiju.GetConfig().MaxKdbWAValueLen {
		log.Infof("Committing blocks up to number %d ...", tag)
		err := u.db.Commit(tag)
		log.Infof("Committed blocks up to number %d", tag)
		return err
	}
	return nil
}
Example #3
0
func (c *Cold) Init() error {
	path, err := initFilePath()
	if err != nil {
		return err
	}

	f, _, err := openFile(path, kaiju.GetConfig().HeadersFileName)
	if err != nil {
		return err
	}
	c.hfile = f
	c.h = newHeaders(f)
	c.h.loadHeaders()

	dbf, dbfi, err := openFile(path, kaiju.GetConfig().KdbFileName)
	if err != nil {
		return err
	}
	waf, _, err := openFile(path, kaiju.GetConfig().KdbWAFileName)
	if err != nil {
		return err
	}
	var db *kdb.KDB
	if dbfi.Size() == 0 {
		db, err = kdb.New(kaiju.GetConfig().KDBCapacity, dbf, waf)
		if err != nil {
			return err
		}
	} else {
		db, err = kdb.Load(dbf, waf)
		if err != nil {
			return err
		}
	}
	c.dbFile = dbf
	c.waFile = waf
	c.db = newOutputDB(db)
	return nil
}
Example #4
0
func TestScanKeys(t *testing.T) {
	cfg := kaiju.GetConfig()
	path := filepath.Join(kaiju.ConfigFileDir(), cfg.DataDir)
	f := openFile(t, path, cfg.KdbFileName)
	wa := openFile(t, path, cfg.KdbWAFileName)
	db, err := Load(f, wa)
	if err != nil {
		t.Errorf("Failed load db %s", err)
	}
	r, g, err := db.enumerate(nil)
	if err != nil {
		t.Errorf("Failed to scan keys: %s", err)
	}
	t.Logf("KDB scanKeys %d %d", r, g)
}
Example #5
0
// Start KNet module, should be called before any other calls in knet
func Start(count int) (<-chan struct{}, error) {
	if instance != nil {
		return nil, errors.New("KNet.Start should only be called once")
	}
	pm, err := peer.Init()
	if err != nil {
		return nil, err
	}
	cc := newCC()
	instance = &KNet{cc, pm}
	seeds := kaiju.GetConfig().SeedPeers
	for _, ip := range seeds {
		instance.cc.addSeedAddr(ip, kaiju.ListenPort)
	}
	instance.cc.start([]peer.Monitor{instance.cc})
	return pm.Wait(count), nil
}
Example #6
0
func _TestRebuild(t *testing.T) {
	cfg := kaiju.GetConfig()
	path := filepath.Join(kaiju.ConfigFileDir(), cfg.DataDir)
	f := openFile(t, path, cfg.KdbFileName)
	wa := openFile(t, path, cfg.KdbWAFileName)
	rebf := createFile(t, path, cfg.KdbFileName+".reb")
	rebwa := createFile(t, path, cfg.KdbWAFileName+".reb")
	db, err := Load(f, wa)
	if err != nil {
		t.Errorf("Failed to load db %s", err)
	}
	db2, err := db.Rebuild(cfg.KDBCapacity, rebf, rebwa)
	if err != nil {
		t.Errorf("Failed to rebuild db %s", err)
	}

	r, g, err := db2.enumerate(nil)
	if err != nil {
		t.Errorf("Failed to scan keys: %s", err)
	}
	t.Logf("KDB scanKeys %d %d", r, g)
}
Example #7
0
func TestKDB(t *testing.T) {

	cfg := kaiju.GetConfig()
	path := filepath.Join(kaiju.ConfigFileDir(), cfg.TempDataDir)
	os.MkdirAll(path, os.ModePerm)

	f := createFile(t, path, "testkdb.dat")
	wa := createFile(t, path, "testkdb.wa")

	capacity := uint32(1000)
	db, dberr := New(capacity, f, wa)
	if dberr != nil {
		t.Errorf("Failed to create KDB: %s", dberr)
	}

	for i := uint32(0); i < capacity/2; i++ {
		writeUint32(t, db, uint32(i), uint32(i))
	}

	commit(t, db, 1)

	for i := uint32(0); i < capacity/2; i += 3 {
		removeUint32(t, db, uint32(i), uint32(i))
	}

	commit(t, db, 2)

	for i := uint32(0); i < capacity/2; i += 3 {
		writeUint32(t, db, uint32(i), uint32(i))
	}

	commit(t, db, 3)

	for i := capacity / 2; i < capacity; i++ {
		writeUint32(t, db, uint32(i), uint32(i))
	}

	commit(t, db, 4)

	for i := uint32(0); i < capacity; i++ {
		testUint32(t, db, uint32(i), uint32(i))
	}

	for i := uint32(0); i < capacity/10000; i++ {
		removeUint32(t, db, uint32(i), uint32(i))
	}

	for i := uint32(0); i < capacity/10000; i++ {
		testNotUint32(t, db, uint32(i), uint32(i))
	}

	for i := uint32(0); i < capacity/10000; i++ {
		writeUint32(t, db, uint32(i), uint32(i))
	}

	for i := uint32(0); i < capacity; i++ {
		testUint32(t, db, uint32(i), uint32(i))
	}

	r, g, err := db.enumerate(nil)
	if dberr != nil {
		t.Errorf("Failed to scan keys: %s", err)
	}
	t.Logf("KDB:keys %d %d", r, g)

	f.Seek(0, 0)
	wa.Seek(0, 0)
	db, dberr = Load(f, wa)
	if dberr != nil {
		t.Errorf("Failed to load KDB: %s", dberr)
	} else {
		for i := uint32(0); i < capacity; i++ {
			testUint32(t, db, uint32(i), uint32(i))
		}
	}

	r, g, err = db.enumerate(nil)
	if dberr != nil {
		t.Errorf("Failed to scan keys: %s", err)
	}
	t.Logf("KDB:keys %d %d", r, g)

	if closeErr := f.Close(); closeErr != nil {
		t.Errorf("Error closing file: %s", closeErr)
	}
}