This commit is contained in:
Sugu Sougoumarane 2016-03-30 22:17:54 -07:00
Родитель 3d2fb0c4c2
Коммит aa06883f87
7 изменённых файлов: 96 добавлений и 221 удалений

Просмотреть файл

@ -20,7 +20,7 @@
"Owner": "user"
}
},
"Classes": {
"Tables": {
"user": {
"ColVindexes": [
{
@ -77,23 +77,14 @@
}
]
}
},
"Tables": {
"user": "user",
"user_extra": "user_extra",
"music": "music",
"music_extra": "music_extra"
}
},
"main": {
"Classes": {
"Tables": {
"main1": {},
"seq": {
"Type": "Sequence"
}
},
"Tables": {
"main1": "",
"seq": "seq"
}
}
}

Просмотреть файл

@ -28,7 +28,7 @@
"Type": "numeric"
}
},
"Classes": {
"Tables": {
"user": {
"ColVindexes": [
{
@ -89,26 +89,18 @@
}
]
}
},
"Tables": {
"user": "user",
"user_extra": "user_extra",
"music": "music",
"music_extra": "music_extra",
"music_user_idx": "music_user_idx"
}
},
"lookup": {
"Sharded": false,
"Classes": {
"seq": {
"Type": "Sequence"
}
},
"Tables": {
"user_seq": "seq",
"music_seq": "seq",
"name_user_idx": ""
"user_seq": {
"Type": "Sequence"
},
"music_seq": {
"Type": "Sequence"
},
"name_user_idx": {}
}
}
}

Просмотреть файл

@ -55,7 +55,7 @@ var routerVSchema = createTestVSchema(`
"Type": "numeric"
}
},
"Classes": {
"Tables": {
"user": {
"ColVindexes": [
{
@ -67,10 +67,10 @@ var routerVSchema = createTestVSchema(`
"Name": "name_user_map"
}
],
"Autoinc" : {
"Col": "id",
"Sequence": "user_seq"
}
"Autoinc" : {
"Col": "id",
"Sequence": "user_seq"
}
},
"user_extra": {
"ColVindexes": [
@ -91,10 +91,10 @@ var routerVSchema = createTestVSchema(`
"Name": "music_user_map"
}
],
"Autoinc" : {
"Col": "id",
"Sequence": "user_seq"
}
"Autoinc" : {
"Col": "id",
"Sequence": "user_seq"
}
},
"music_extra": {
"ColVindexes": [
@ -136,34 +136,22 @@ var routerVSchema = createTestVSchema(`
}
]
}
},
"Tables": {
"user": "user",
"user_extra": "user_extra",
"music": "music",
"music_extra": "music_extra",
"music_extra_reversed": "music_extra_reversed",
"noauto_table": "noauto_table",
"ksid_table": "ksid_table"
}
},
"TestBadSharding": {
"Sharded": false,
"Tables": {
"sharded_table": ""
"sharded_table": {}
}
},
"TestUnsharded": {
"Sharded": false,
"Classes": {
"seq": {
"Type": "Sequence"
}
},
"Tables": {
"user_seq": "seq",
"music_user_map": "",
"name_user_map": ""
"user_seq": {
"Type": "Sequence"
},
"music_user_map": {},
"name_user_map": {}
}
}
}

Просмотреть файл

@ -95,27 +95,21 @@ func buildKeyspaces(source *VSchemaFormal, vschema *VSchema) {
func buildSequences(source *VSchemaFormal, vschema *VSchema) error {
for ksname, ks := range source.Keyspaces {
keyspace := vschema.Keyspaces[ksname].Keyspace
for tname, cname := range ks.Tables {
if cname == "" {
for tname, table := range ks.Tables {
if table.Type != "Sequence" {
continue
}
class, ok := ks.Classes[cname]
if !ok {
return fmt.Errorf("class %s not found for table %s", cname, tname)
t := &Table{
Name: tname,
Keyspace: keyspace,
IsSequence: true,
}
if class.Type == "Sequence" {
t := &Table{
Name: tname,
Keyspace: keyspace,
IsSequence: true,
}
if _, ok := vschema.tables[tname]; ok {
vschema.tables[tname] = nil
} else {
vschema.tables[tname] = t
}
vschema.Keyspaces[ksname].Tables[tname] = t
if _, ok := vschema.tables[tname]; ok {
vschema.tables[tname] = nil
} else {
vschema.tables[tname] = t
}
vschema.Keyspaces[ksname].Tables[tname] = t
}
}
return nil
@ -138,12 +132,8 @@ func buildTables(source *VSchemaFormal, vschema *VSchema) error {
}
vindexes[vname] = vindex
}
for tname, cname := range ks.Tables {
class, ok := ks.Classes[cname]
if !ok && cname != "" {
return fmt.Errorf("class %s not found for table %s", cname, tname)
}
if class.Type == "Sequence" {
for tname, table := range ks.Tables {
if table.Type == "Sequence" {
continue
}
t := &Table{
@ -159,10 +149,10 @@ func buildTables(source *VSchemaFormal, vschema *VSchema) error {
if !keyspace.Sharded {
continue
}
for i, ind := range class.ColVindexes {
for i, ind := range table.ColVindexes {
vindexInfo, ok := ks.Vindexes[ind.Name]
if !ok {
return fmt.Errorf("vindex %s not found for class %s", ind.Name, cname)
return fmt.Errorf("vindex %s not found for table %s", ind.Name, tname)
}
vindex := vindexes[ind.Name]
owned := false
@ -179,10 +169,10 @@ func buildTables(source *VSchemaFormal, vschema *VSchema) error {
if i == 0 {
// Perform Primary vindex check.
if _, ok := columnVindex.Vindex.(Unique); !ok {
return fmt.Errorf("primary vindex %s is not Unique for class %s", ind.Name, cname)
return fmt.Errorf("primary vindex %s is not Unique for table %s", ind.Name, tname)
}
if owned {
return fmt.Errorf("primary vindex %s cannot be owned for class %s", ind.Name, cname)
return fmt.Errorf("primary vindex %s cannot be owned for table %s", ind.Name, tname)
}
}
t.ColVindexes = append(t.ColVindexes, columnVindex)
@ -191,11 +181,11 @@ func buildTables(source *VSchemaFormal, vschema *VSchema) error {
}
}
t.Ordered = colVindexSorted(t.ColVindexes)
if class.Autoinc != nil {
t.Autoinc = &Autoinc{Col: class.Autoinc.Col, ColVindexNum: -1}
seq, ok := vschema.tables[class.Autoinc.Sequence]
if table.Autoinc != nil {
t.Autoinc = &Autoinc{Col: table.Autoinc.Col, ColVindexNum: -1}
seq, ok := vschema.tables[table.Autoinc.Sequence]
if !ok {
return fmt.Errorf("sequence %s not found for class %s", class.Autoinc.Sequence, cname)
return fmt.Errorf("sequence %s not found for table %s", table.Autoinc.Sequence, tname)
}
t.Autoinc.Sequence = seq
for i, cv := range t.ColVindexes {
@ -278,8 +268,7 @@ type VSchemaFormal struct {
type KeyspaceFormal struct {
Sharded bool
Vindexes map[string]VindexFormal
Classes map[string]ClassFormal
Tables map[string]string
Tables map[string]TableFormal
}
// VindexFormal is the info for each index as loaded from
@ -290,9 +279,9 @@ type VindexFormal struct {
Owner string
}
// ClassFormal is the info for each table class as loaded from
// TableFormal is the info for each table as loaded from
// the source.
type ClassFormal struct {
type TableFormal struct {
Type string
ColVindexes []ColVindexFormal
Autoinc *AutoincFormal

Просмотреть файл

@ -85,8 +85,8 @@ func TestUnshardedVSchema(t *testing.T) {
good := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
"unsharded": {
Tables: map[string]string{
"t1": "",
Tables: map[string]TableFormal{
"t1": {},
},
},
},
@ -138,7 +138,7 @@ func TestShardedVSchemaOwned(t *testing.T) {
Owner: "t1",
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -151,9 +151,6 @@ func TestShardedVSchemaOwned(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
@ -229,7 +226,7 @@ func TestShardedVSchemaNotOwned(t *testing.T) {
Owner: "",
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -242,9 +239,6 @@ func TestShardedVSchemaNotOwned(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
@ -312,39 +306,6 @@ func TestLoadVSchemaFail(t *testing.T) {
}
}
func TestBuildVSchemaClassNotFoundFail(t *testing.T) {
bad := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
"sharded": {
Sharded: true,
Vindexes: map[string]VindexFormal{
"stfu": {
Type: "stfu",
},
},
Classes: map[string]ClassFormal{
"notexist": {
ColVindexes: []ColVindexFormal{
{
Col: "c1",
Name: "noexist",
},
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
_, err := BuildVSchema(&bad)
want := "class t1 not found for table t1"
if err == nil || err.Error() != want {
t.Errorf("BuildVSchema: %v, want %v", err, want)
}
}
func TestBuildVSchemaVindexNotFoundFail(t *testing.T) {
bad := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
@ -355,7 +316,7 @@ func TestBuildVSchemaVindexNotFoundFail(t *testing.T) {
Type: "noexist",
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -365,9 +326,6 @@ func TestBuildVSchemaVindexNotFoundFail(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
@ -388,7 +346,7 @@ func TestBuildVSchemaInvalidVindexFail(t *testing.T) {
Type: "stf",
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -398,9 +356,6 @@ func TestBuildVSchemaInvalidVindexFail(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
@ -415,24 +370,18 @@ func TestBuildVSchemaDupSeq(t *testing.T) {
good := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
"ksa": {
Classes: map[string]ClassFormal{
"seq": {
Tables: map[string]TableFormal{
"t1": {
Type: "Sequence",
},
},
Tables: map[string]string{
"t1": "seq",
},
},
"ksb": {
Classes: map[string]ClassFormal{
"seq": {
Tables: map[string]TableFormal{
"t1": {
Type: "Sequence",
},
},
Tables: map[string]string{
"t1": "seq",
},
},
},
}
@ -483,13 +432,13 @@ func TestBuildVSchemaDupTable(t *testing.T) {
good := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
"ksa": {
Tables: map[string]string{
"t1": "",
Tables: map[string]TableFormal{
"t1": {},
},
},
"ksb": {
Tables: map[string]string{
"t1": "",
Tables: map[string]TableFormal{
"t1": {},
},
},
},
@ -545,7 +494,7 @@ func TestBuildVSchemaNoindexFail(t *testing.T) {
Type: "stfu",
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -555,14 +504,11 @@ func TestBuildVSchemaNoindexFail(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
_, err := BuildVSchema(&bad)
want := "vindex notexist not found for class t1"
want := "vindex notexist not found for table t1"
if err == nil || err.Error() != want {
t.Errorf("BuildVSchema: %v, want %v", err, want)
}
@ -578,7 +524,7 @@ func TestBuildVSchemaNotUniqueFail(t *testing.T) {
Type: "stln",
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -588,14 +534,11 @@ func TestBuildVSchemaNotUniqueFail(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
_, err := BuildVSchema(&bad)
want := "primary vindex stln is not Unique for class t1"
want := "primary vindex stln is not Unique for table t1"
if err == nil || err.Error() != want {
t.Errorf("BuildVSchema: %v, want %v", err, want)
}
@ -612,7 +555,7 @@ func TestBuildVSchemaPrimaryNonFunctionalFail(t *testing.T) {
Owner: "t1",
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -622,14 +565,11 @@ func TestBuildVSchemaPrimaryNonFunctionalFail(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
_, err := BuildVSchema(&bad)
want := "primary vindex stlu cannot be owned for class t1"
want := "primary vindex stlu cannot be owned for table t1"
if err == nil || err.Error() != want {
t.Errorf("BuildVSchema: %v, want %v", err, want)
}
@ -639,14 +579,11 @@ func TestSequence(t *testing.T) {
good := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
"unsharded": {
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"seq": {
Type: "Sequence",
},
},
Tables: map[string]string{
"seq": "seq",
},
},
"sharded": {
Sharded: true,
@ -658,7 +595,7 @@ func TestSequence(t *testing.T) {
},
},
},
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
ColVindexes: []ColVindexFormal{
{
@ -672,9 +609,6 @@ func TestSequence(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
@ -750,7 +684,7 @@ func TestBadSequence(t *testing.T) {
Keyspaces: map[string]KeyspaceFormal{
"sharded": {
Sharded: true,
Classes: map[string]ClassFormal{
Tables: map[string]TableFormal{
"t1": {
Autoinc: &AutoincFormal{
Col: "c1",
@ -758,14 +692,11 @@ func TestBadSequence(t *testing.T) {
},
},
},
Tables: map[string]string{
"t1": "t1",
},
},
},
}
_, err := BuildVSchema(&bad)
want := "sequence seq not found for class t1"
want := "sequence seq not found for table t1"
if err == nil || err.Error() != want {
t.Errorf("BuildVSchema: %v, want %v", err, want)
}
@ -775,19 +706,16 @@ func TestFind(t *testing.T) {
input := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
"ksa": {
Tables: map[string]string{
"ta": "",
"t1": "",
Tables: map[string]TableFormal{
"ta": {},
"t1": {},
},
},
"ksb": {
Sharded: true,
Classes: map[string]ClassFormal{
"t": {},
},
Tables: map[string]string{
"tb": "t",
"t1": "t",
Tables: map[string]TableFormal{
"tb": {},
"t1": {},
},
},
},
@ -847,9 +775,9 @@ func TestFindSingleKeyspace(t *testing.T) {
input := VSchemaFormal{
Keyspaces: map[string]KeyspaceFormal{
"ksa": {
Tables: map[string]string{
"ta": "",
"t1": "",
Tables: map[string]TableFormal{
"ta": {},
"t1": {},
},
},
},
@ -869,12 +797,9 @@ func TestFindSingleKeyspace(t *testing.T) {
Keyspaces: map[string]KeyspaceFormal{
"ksb": {
Sharded: true,
Classes: map[string]ClassFormal{
"t": {},
},
Tables: map[string]string{
"tb": "t",
"t1": "t",
Tables: map[string]TableFormal{
"tb": {},
"t1": {},
},
},
},

Просмотреть файл

@ -34,7 +34,7 @@ func init() {
"TestUnsharded": {
"Sharded": false,
"Tables": {
"t1": ""
"t1": {}
}
}
}

Просмотреть файл

@ -126,7 +126,7 @@ vschema = '''{
"Owner": "vt_music"
}
},
"Classes": {
"Tables": {
"vt_user": {
"ColVindexes": [
{
@ -203,29 +203,19 @@ vschema = '''{
}
]
}
},
"Tables": {
"vt_user": "vt_user",
"vt_user2": "vt_user2",
"vt_user_extra": "vt_user_extra",
"vt_music": "vt_music",
"vt_music_extra": "vt_music_extra",
"join_user": "join_user",
"join_user_extra": "join_user_extra"
}
},
"lookup": {
"Sharded": false,
"Classes" : {
"seq": {
"Type": "Sequence"
}
},
"Tables": {
"vt_user_seq": "seq",
"vt_music_seq": "seq",
"music_user_map": "",
"name_user2_map": ""
"vt_user_seq": {
"Type": "Sequence"
},
"vt_music_seq": {
"Type": "Sequence"
},
"music_user_map": {},
"name_user2_map": {}
}
}
}