func TestOutputValueFile(t *testing.T) {
	if err := nodedb.Initialize(); err != nil {
		t.Fatalf("nodedb.Initialize() %s", err)
	}

	info, err := tools.NewFileSpaceInfo(nodedb.NodeDB)
	if err != nil {
		t.Fatalf("NewFileSpaceInfo( %s", err)
	}

	valueFile, err := NewOutputValueFile(info)
	if err != nil {
		t.Fatalf("NewOutputValueFile %s", err)
	}

	collectionID := uint32(42)
	segmentID := uint64(88)
	dataSize := 1024
	data := bytes.Repeat([]byte{'a'}, dataSize)

	if _, err := valueFile.Store(collectionID, segmentID, data); err != nil {
		t.Fatalf("valueFile.Store %s", err)
	}

	if valueFile.Size() != uint64(dataSize) {
		t.Fatalf("size mismatch: expecting %d, found %d", dataSize,
			valueFile.Size())
	}

	if err := valueFile.Close(); err != nil {
		t.Fatalf("valueFile.Close() %s", err)
	}
}
示例#2
0
// NewNimbusioWriter returns an entity that implements the NimbusioWriter interface
func NewNimbusioWriter() (NimbusioWriter, error) {
	var err error
	var state writerState
	state.SegmentMap = make(map[segmentKey]segmentMapEntry)

	if state.NodeIDMap, err = centraldb.GetNodeIDMap(); err != nil {
		return nil, fmt.Errorf("centraldb.GetNodeIDMap() failed %s", err)
	}

	if state.FileSpaceInfo, err = tools.NewFileSpaceInfo(nodedb.NodeDB); err != nil {
		return nil, err
	}

	if state.ValueFile, err = NewOutputValueFile(state.FileSpaceInfo); err != nil {
		return nil, err
	}

	writerChan := make(chan interface{}, writerChanCapacity)
	state.WriterChan = nimbusioWriterChan(writerChan)

	if state.StatGrabber, err = gostatgrabber.NewStatGrabber(); err != nil {
		return nil, fmt.Errorf("NewStatGrabber failed %s", err)
	}

	startSyncTimer(&state)

	go func() {
		for item := range writerChan {
			switch request := item.(type) {
			case requestSync:
				handleSync(&state)
			case requestStartSegment:
				handleStartSegment(&state, request)
			case requestStoreSequence:
				handleStoreSequence(&state, request)
			case requestCancelSegment:
				handleCancelSegment(&state, request)
			case requestWaitSyncForFinishSegment:
				handleWaitSyncForFinishSegment(&state, request)
			case requestFinishSegment:
				handleFinishSegment(&state, request)
			case requestDestroyKey:
				handleDestroyKey(&state, request)
			case requestStartConjoinedArchive:
				handleStartConjoinedArchive(&state, request)
			case requestAbortConjoinedArchive:
				handleAbortConjoinedArchive(&state, request)
			case requestFinishConjoinedArchive:
				handleFinishConjoinedArchive(&state, request)
			default:
				fog.Error("unknown request type %T", item)
			}
		}
	}()

	return state.WriterChan, nil
}