func (e *segmentEntry) write(writer typedio.Writer) (err os.Error) { err = writer.WriteUint64(e.position) // position if err != nil { return err } buf := gsbuffer.New() err = e.mutation.serialize(buf) if err != nil { return err } e.mutsize = uint32(buf.Size) err = writer.WriteUint16(uint16(e.token)) // token if err != nil { return err } err = writer.WriteUint32(e.mutsize) // size of mutation if err != nil { return err } buf.Seek(0, 0) written, err := writer.Write(buf.Bytes()) // mutation if err != nil { return err } if written != int(e.mutsize) { return os.NewError(fmt.Sprintf("Couldn't write the whole mutation entry! Written %d of %d", written, e.mutsize)) } return }
func (cl *CommitLog) Execute(mutation Mutation) { typ := reflect.TypeOf(mutation) mutInfo, found := cl.mutationsType[typ] if !found { log.Fatal("CommitLog: Tried to execute an unknown mutation: %s of type %s", mutation, typ) } // write the modification into a buffer buf := buffer.New() buf.WriteUint8(mutInfo.id) // mutation id mutation.Serialize(buf) // mutation record buf.WriteInt64(buf.Size + 8) // record length // write record to disk buf.Seek(0, 0) cl.fdMutex.Lock() cl.fd.Seek(cl.writePtr, 0) io.Copyn(cl.fd, buf, buf.Size) // update commit log write pointer cl.fd.Seek(HEADER_WRITE_POS, 0) cl.writePtr += buf.Size cl.typedFd.WriteInt64(cl.writePtr) cl.fdMutex.Unlock() // execute the mutation mutation.Execute() }
func TestResize(t *testing.T) { buffer := buffer.New() buffer.Write([]byte{1, 2, 3}) buffer.Seek(1, 0) buffer.Write([]byte{7, 7, 7}) if buffer.Size != 4 { t.Errorf("Should have resized to 4, but size is now %d", buffer.Size) } buf := make([]byte, 4) n, _ := buffer.Read(buf) if n != 0 { t.Errorf("Pointer was at the end, shouldn't have read anything but had %d bytes returned", n) } buffer.Seek(1, 0) n, _ = buffer.Read(buf) if n != 3 { t.Errorf("Should have read 4, but returned %d", n) } if string(buf) != string([]byte{7, 7, 7, 0}) { t.Errorf("Should have read 7 7 7 0, but got %s", buf) } }
func TestWriteRead(t *testing.T) { buff := buffer.New() buff.Write([]byte{1, 3, 1}) if buff.Size != 3 { t.Errorf("Buffer size should be 3, got %d", buff.Size) } if buff.Pointer != 3 { t.Errorf("Pointer should be at 3, got %d", buff.Pointer) } buff.Seek(0, 0) if buff.Pointer != 0 { t.Errorf("Pointer should bt at 0 after seek, got %d", buff.Pointer) } rb := make([]byte, 3) n, err := buff.Read(rb) if err != nil { t.Errorf("Got an error reading: %s", err) } if n != 3 { t.Errorf("Should have read 3 bytes, got %d", n) } }
func newMessage(comm *Comm) *Message { r := new(Message) r.comm = comm r.Id = comm.nextSequenceId() r.SetSourceNode(r.comm.Cluster.MyNode) r.middleNodePresent = false r.Message = buffer.New() r.Wait = make(chan bool, 1) return r }
func TestWriteOnce(t *testing.T) { buff := buffer.New() buff.WriteUint8(100) buff.Seek(0, 0) val, err := buff.ReadUint8() if val != 100 { t.Errorf("Read not equal 100: %d", val) } if err != nil { t.Errorf("Got an error: %s", err) } }
func TestSpecific(t *testing.T) { buffer := buffer.New() buffer.WriteUint8(233) buffer.Seek(0, 0) val, err := buffer.ReadUint8() if err != nil { t.Errorf("Got an error reading: %s", err) } if val != 233 { t.Errorf("Should have read 233, but got %d", val) } }