MIT6.5830 Lab1-GoDB实验记录(二)

MIT6.5830 Lab1-GoDB实验记录(二) -- WhiteNight's Site

标签:Golang, 数据库

接下来我们将完成tuple.go的缺失代码,并通过tuple_test.go的测试。

实验步骤
观察tuple.go

观察肯定是第一步,先打开tuple.go。

快300行代码,勉勉强强还在接受范围之内。接下来看看我们要做什么,首要目的肯定就是通过go test tuple_test.go。

实验步骤
分析数据结构

开头就给出了DBType的定义,tuple field?那我们暂且把它当作是元组字段--即每一行的每一个数据可以是什么类型的。说白了就是列的域(domain)是{int,string}。这里限定了存储在数据库中的数据只能是int或者string,对应了lab1.md中提到的"Data types except integers and fixed length strings."。

复制代码
// DBType is the type of a tuple field, in GoDB, e.g., IntType or StringType
type DBType int

const (
	IntType     DBType = iota
	StringType  DBType = iota
	UnknownType DBType = iota //used internally, during parsing, because sometimes the type is unknown
)

var typeNames map[DBType]string = map[DBType]string{IntType: "int", StringType: "string"}

如果你不记得iota是啥了,这里有一个使用例:Golang中的iota。这里还新建了一个map,看起来是匹配int和string两种数据类型的。

接下来定义了一个FieldType结构。FieldType?字段类型又是什么玩意?

复制代码
// FieldType is the type of a field in a tuple, e.g., its name, table, and [godb.DBType].
// TableQualifier may or may not be an emtpy string, depending on whether the table
// was specified in the query
type FieldType struct {
	Fname          string
	TableQualifier string
	Ftype          DBType
}

先别着急。我们可以观察到,Ftype的类型是DBType。说明DBType对于FieldType而言是更加底层的方法。

上面的DBType,指向的是每一行的数据,它限定了每个元组中字段的类型。而FieldType指向的是每一列,它限定的是字段所构成的"元组"。举个例子

假设有个table,有四列,或者说是四个字段

id,name,email,address

如果id限定的类型为int,其他三者的类型为string。那么id的域就是{int},相当于Ftype=int;Fname就是"id";那么tableQualifier,顾名思义,"表限定符",指代的是数据库中的表。我们这里没碰到暂时就不管它。

还剩下最后一段:TupelDesc。

复制代码
// TupleDesc is "type" of the tuple, e.g., the field names and types
type TupleDesc struct {
	Fields []FieldType
}

Desc又是什么?不知道,又是没打注释。Descent?Describe?我们回到lab1.md中看看。

复制代码
Tuples also have a type (or schema), called a tuple descriptor, represented by a TupleDesc struct, which consists of a collection of FieldType objects, one per field in the tuple, each of which describes the type of the corresponding field.

Fieids的类型是FieldType,说明TupleDesc要比FieldType更高级,它从FieldType继承了它的方法和数据类型。

总结一下,TupleDesc>FieldType>DBType。而TupleDesc存储着每一列字段的名字,类型。

那么接下来就是完成实验一了,下篇文章在慢慢记录。

相关推荐
JIngJaneIL40 分钟前
口腔健康系统|口腔医疗|基于java和小程序的口腔健康系统小程序设计与实现(源码+数据库+文档)
java·数据库·spring boot·小程序·论文·毕设·口腔医疗小程序
白帽子黑客罗哥1 小时前
常见Web安全漏洞全解析:从原理到防御的实战指南
数据库·web安全·渗透测试·漏洞利用·权限提升
寒山李白3 小时前
IDEA连接MySQL服务器数据库指南
java·数据库·mysql·intellij-idea·idea·database
人邮异步社区4 小时前
推荐几本学习计算机语言的书
java·c语言·c++·python·学习·golang
亿坊电商6 小时前
PHP后端项目中多环境配置管理:开发、测试、生产的优雅解决方案!
服务器·数据库·php
韩立学长6 小时前
基于Springboot的影视评论网站的设计与实现58py6238(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
未来之窗软件服务6 小时前
未来之窗昭和仙君(四十七)开发商品进销存——东方仙盟筑基期
数据库·进销存·仙盟创梦ide·东方仙盟·昭和仙君·东方仙盟架构
IDOlaoluo7 小时前
TinyRDM 1.2.3 Windows版安装教程(附Redis客户端下载及详细步骤)
数据库·redis·缓存
小光学长8 小时前
基于微信小程序的背单词系统x1o5sz72(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·微信小程序·小程序
我命由我123459 小时前
Derby - Derby 服务器(Derby 概述、Derby 服务器下载与启动、Derby 连接数据库与创建数据表、Derby 数据库操作)
java·运维·服务器·数据库·后端·java-ee·后端框架