Ejemplo n.º 1
0
func TestQuickGranularStorage(t *testing.T) {
	gs := newGranularStorage(8, adapters.NewMemoryStorage(make([]byte, 0, 4096)), adapters.NewMemoryStorage(make([]byte, 0, 4096)))
	//add
	if err := quick.Check(func(b []byte) bool {
		i, err := gs.add(b)
		if err != nil || i != gs.len()-1 {
			return false
		}
		if i > 2 {
			err = gs.set(i-1, []byte{})
			if err != nil {
				return false
			}
		}
		if i > 5 {
			err = gs.del(i - 3)
			if err != nil {
				return false
			}
		}
		return true
	}, nil); err != nil {
		t.Error(err)
	}
}
Ejemplo n.º 2
0
Archivo: basic.go Proyecto: mrkovec/str
func newDataFile(name string) (*basicData, error) {
	df := &basicData{
		ver:       -1,
		dbname:    name,
		storage:   newGranularStorage(64, adapters.NewMemoryStorage(make([]byte, 0, 4096)), adapters.NewMemoryStorage(make([]byte, 0, 4096))),
		storeLog:  make([]*Entity, 0, 256),
		deleteLog: make([]*Entity, 0, 256),
		idx:       newbitmapIndex()}
	return df, nil
}
Ejemplo n.º 3
0
func (ms *mmapStorage) Open() (err error) {
	ms.f, err = os.OpenFile(ms.name, os.O_RDWR, 0644)
	if err != nil {
		return err
	}
	fs, err := ms.f.Stat()
	if err != nil {
		return err
	}
	ms.length = int(fs.Size())

	mm, err := mmap.Map(ms.f, mmap.RDWR, 0)
	if err != nil {
		return err
	}
	ms.mm = &mm
	ms.mStor = adapters.NewMemoryStorage(mm)

	//ms.fStor = adapters.NewFileStorage(ms.name)
	return nil
}
Ejemplo n.º 4
0
func TestGranularStorage(t *testing.T) {
	gs := newGranularStorage(2, adapters.NewMemoryStorage(make([]byte, 0, 64)), adapters.NewMemoryStorage(make([]byte, 0, 64)))
	e, g = 0, gs.len()
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	//add
	i, err := gs.add([]byte{1, 1})
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = 0, i
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	i, err = gs.add([]byte{2})
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = 1, i
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}
	e, g = 2, gs.len()
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	//get
	b, err := gs.get(0)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[1 1]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	b, err = gs.get(1)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[2]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	//add + rebuild
	i, err = gs.add([]byte{3, 3, 3, 3, 3, 3, 3, 3})
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = 2, i
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}
	e, g = 3, gs.len()
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	b, err = gs.get(0)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[1 1]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	b, err = gs.get(1)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[2]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	b, err = gs.get(2)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[3 3 3 3 3 3 3 3]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	//set + rebuild
	err = gs.set(1, []byte{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4})
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = 3, gs.len()
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	b, err = gs.get(0)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[1 1]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	b, err = gs.get(1)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	b, err = gs.get(2)
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = "[3 3 3 3 3 3 3 3]", fmt.Sprintf("%v", b)
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	//del
	err = gs.del(1)
	if err != nil {
		t.Errorf(err.Error())
	}

	b, err = gs.get(1)
	e, g = errDeleted, err
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}
	if b != nil {
		t.Errorf("expected: nil and got: %v (type %T)", b, b)
	}

	oldl := gs.len()
	_, err = gs.add([]byte{4, 4})
	if err != nil {
		t.Errorf(err.Error())
	}
	e, g = oldl, gs.len()
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	//err
	_, err = gs.get(55)
	e, g = ErrIndexOutOfRange, err
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	err = gs.set(55, []byte{0})
	e, g = ErrIndexOutOfRange, err
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}

	err = gs.del(55)
	e, g = ErrIndexOutOfRange, err
	if e != g {
		t.Errorf("expected: %v (type %T) and got: %v (type %T)", e, e, g, g)
	}
}
Ejemplo n.º 5
0
func BenchmarkUpdateMemoryStorage(b *testing.B) {
	benchmarkUpdate(b, adapters.NewMemoryStorage(make([]byte, b.N)))
}
Ejemplo n.º 6
0
func BenchmarkWriteMemoryStorage(b *testing.B) {
	benchmarkWrite(b, adapters.NewMemoryStorage(make([]byte, 0, 4096)))
}